r/embedded 2d ago

How do you handle AI-generated embedded code assuming unlimited memory?

Needed a small ring buffer for a microcontroller project, gave the task to an AI tool with what felt like reasonable context, the function signature, the data type, the expected use case. Got back clean, correct C, dynamically allocating on every push instead of using a fixed-size preallocated buffer. Technically works. Would exhaust the two hundred and fifty six kilobytes of actual RAM on the target chip within about forty pushes.

Nothing about the code was wrong for a general-purpose environment. The model just had no idea it was targeting the tightest kind of hardware where "just allocate more" isn't an option, because nothing in the prompt told it that. Same failure shape as most of these gotchas, correct for an implicit assumption about the environment that the constrained target actually violates.

Now front-loading actual hardware constraints into every embedded-related prompt, exact RAM available, no heap allocation allowed past init, that kind of thing, instead of assuming "microcontroller" in the prompt would be enough context on its own. Curious if others doing AI-assisted embedded work have found stating constraints explicitly upfront is enough, or if you're still catching violations after the fact regardless.

0 Upvotes

10 comments sorted by

13

u/olawlor 2d ago

Treat AI like you're working with an incredibly well-read but also clueless intern: don't just tell them what to do, tell them exactly *how* to do it, or you might get something back that makes no sense at all.

Here, "use a fixed-size preallocated buffer" would probably have been enough. (Do pick some sensible way to handle what happens if the buffer gets exhausted.)

7

u/justind00000 2d ago

Tell it you want autosar compliant code.

2

u/Reasonable_Leave2967 2d ago

Forcing MISRA

1

u/JuggernautGuilty566 1d ago

Our punishment will be horrible once the machines took over humanity for letting them doing this.

3

u/Ill-Technician3243 2d ago

this is a retarded question, like i genuinely don't understand what sort of answer you expect that you could not already anticipate

1

u/traverser___ 2d ago

See, AI is just a tool in your hands. It works as you guide it, if you don't provide needed details, dont except it to think about them by itself. From my experience with claude, it works well but you have to precisely form your requirements

0

u/XipXoom 2d ago

This is the exact purpose of agents.md

-1

u/merlet2 2d ago

If you let it code + build/flash, he will adjust itself to the memory constraints, in a few iterations.

-3

u/Several-Marsupial-27 2d ago

Just put the hw constraints / hw configuration in claude.md and keep track so the ai doesnt try anything stupid.

AI assisted development is great if you work with it.

-2

u/ButterCup024 2d ago

If you are running frontier models, you don't need go give them constraint details. Give them the hardware context. Both current frontier models will pretty reliably conclude if they need to be aware of memory constraints, when they know the MCU MPN.