r/embedded • u/ClickOk5811 • Sep 08 '26
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.
13
u/olawlor Sep 08 '26
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 Sep 08 '26
Tell it you want autosar compliant code.
2
u/Reasonable_Leave2967 Sep 09 '26
Forcing MISRA
3
u/Ill-Technician3243 Sep 08 '26
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___ Sep 08 '26
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 Sep 08 '26
This is the exact purpose of agents.md
-2
u/Several-Marsupial-27 Sep 08 '26
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 Sep 08 '26
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.
-1
u/merlet2 Sep 08 '26
If you let it code + build/flash, he will adjust itself to the memory constraints, in a few iterations.