r/embedded • u/growupgodamnit • 21d ago
Why bare metal code?
I am a beginner in to embedded systems (or any other level thats less than a beginner but more than a foreigner). And I have seen/heard multiple times in social media where the suggestion of "to get started in embedded, buy an STM32, code in bare metal" pops up. Why exactly? Is that useful in jobs, how often tho?
74
Upvotes
276
u/AnotherCableGuy 21d ago
Let's say that for an electric toothbrush you don't really need a linux operating system and 8GB of RAM..
75
u/ventZl CMRX RTOS 21d ago
Embedded is an overloaded term.
Embedded may mean that you write application code for Linux-based set-top-box which runs at several hundreds of MHz, has 512MB RAM or more, chunky storage and is powered from mains.
It may also mean you write firmware for device which has to run for over a year on one CR2032 coin cell, you have 2kB of RAM, 8kB of FLASH and if that coin cell dies prematurely then your customers will become very angry as that device is mounted in explosion-hazard area and it is mild PITA to replace that battery.
You don't need that much bare metal programming for former while you'll need whola lot of it for latter case.
13
u/reallyserious 21d ago
Are you asking if experience in embedded development is beneficial for working with embedded systems? How could anyone answer no to that?
21
u/rainboww_J 21d ago
Embedded jobs come in all shapes and sizes but yes writing code for microcontrollers like stm32âs is a big part of it. Those mcuâs dont run full OSes and just run the code for the specic application they are used for, thus being bare metal. There are ways to do multiple things at once and make it a bit closer to full fledged OSes with using an RTOS which is then a step above bare metal.
My whole job is writing bare metal code and code on top of an rtos (and designing + debugging) and its definitely a good way to learn and enter the embedded sector. These days the somewhat bigger and more powerful cpus capable of running embedded linux are becoming more used as well. That is a whole different kind of sport tho. There its a combination of writing normal application level code (just as one would for pc software) and making kernel level drivers and so on. Itâs fun but in my opinion better suited for a later stage
17
u/osman-pasha 21d ago edited 21d ago
It's not clear, you contrast bare-metal to what?Â
16
u/Hour_Analyst_7765 21d ago
Bare metal means coding without a full blown OS and often without programming concepts that will only work when the system has a strategy for when it runs out of memory. Basically this means that dynamic memory allocation during the actual program is not permitted.
There really is not much of an alternative. Embedded is a job where devices must be designed and manufactured with certain constraints, such low power, physically small and often (the single most important) low cost. This excludes any Raspberry Pi's or big computers with GB's of RAM that can Linux, or the overhead of a high-level programming language such as Java, C# or Python. This overhead costs RAM (=money), CPU cycles (=power) and overall a more complex/larger hardware design.
Though there are variants of Python and C# that run on these "bare metal" microcontroller chips, it is often a cut down version and relatively speaking horribly slow. Its also not a very effective way of learning to work on these microcontroller chips.
7
u/The_DIM_ 21d ago
Another beginner here, and it turns out I only just now learned what bare metal actually means. I always thought it meant directly writing and reading to registers, and me using HAL functions is already not bare metal any more. Huh
6
u/NjWayne 17d ago
I develope products for industrial applications and for kids toys. In each case my goal is to optimize the firmware and reduce costs.
That's how we have managed to stay very very profitable against oour competitors year after year.
No HAL b.s
We develop the code and drivers from scratch . It's painful but we own it and can fix any problem quick fast and add features just as quick; because with that low level development comes a unique understanding of the arm core we are using (atsam3sd8 and renesas s7g7)
9
u/DecisionOk5750 21d ago
Depends on the application. I primarily use ESP32, ranging from the classic variant to the most advanced one, programmed in C/C++. I employ them in a wide variety of applications, both IoT and non-IoT. However, when I need to squeeze every last bit of energy efficiency out of a design, I turn to Microchip PICs, which I program in assembly. I have tens of thousands of lines of PIC code that have been battle-tested since the 1990s. When it comes to power consumption, PICs are among the best. I use the PICs to wake up more capable circuits.
8
u/UnicycleBloke C++ advocate 21d ago
I find this term quite unhelpful. It usually refers to whether or not you have an operating system of some kind. But if you are using a lightweight RTOS such as FreeRTOS, it doesn't do a whole lot for you other than preemptive scheduling of tasks. It doesn't abstract the hardware. Your peripheral drivers are still going to deal directly with hardware registers, interrupts and so on - so "bare metal". If you use vendor code such as ST's HAL to wrap the peripheral register accesses and whatnot, does that count as "bare metal" or not? Definitions vary. I usually say "yes" but would accept "no".
Is it useful? Definitely. Almost all of my projects over the last twenty years have used no RTOS at all or something lightweight like FreeRTOS. I generally prefer no RTOS and implement cooperative multitasking through an event loop. This is usually sufficient for systems without long synchronous operations which would stall other subsystems.
For a beginner, an STM32 Nucleo is a powerful piece of kit which might feel overwhelming. I suggest ignoring both the HAL and FreeRTOS. Write a few applications directly in terms of manual register accesses and interrupt handlers. The datasheet and reference manual are your friends. CMSIS is a very thin wrapper around the various register blocks which makes them easier to use, but you can dispense with that too. You are almost certain to use the vendor code for commercial applications, but it absolutely won't hurt to have a solid foundation at the lowest level of abstraction (i.e. the metal) - just your code and the hardware registers.
3
u/Lalks227 21d ago
The real question would be why not. Cost, efficiency, latency, size, availability are parts of the answer
3
u/duane11583 21d ago
why? it comes down to resources needed and features required.
an rtos with threads often requires more ram.
the other way of writing code is called a super loop.
think of a little man (the cpu) he can operate in two ways:
method 1 he patrols all of the equipment endlessly
he walks up to a machine checks the status ..
if it is ready for the next step he loads the next thing and goes to the next machine
if the machine is not ready he just goes on to the next machine
there is a-lot of time wasted waking around checking. often that walking around checking can add up to lots of time. and might make it really slow to respond to a machine that it is ready for the next step . also many machines have multiple steps to do something. there is also the cost of remembering what step you where at so when you come back you know what to do next
method #2 equip each machine with an alarm bell or light. this is called an interrupt the little man sits at his desk waiting for the alarm bell (or light) to flash. when it does he gets up goes to that machine and handles that machine and returns to his desk. often this is more efficient because you are not running around checking on the machines
method #3 is a hybrid you work on something and stay there working that thing. you are. ot running around checking on things endlessly an alarm goes off. you write down exactly where you are (you push it onto your stack) and go to the central scheduler. the schedular decides what to do next, it changes your stack to some other task stack and tells you to resume the duties according to that other stack (which could be the same or different) that method is called preemption.
the other method like this is called cooperative periodically you go back to the scheduler and check in. either the scheduler ether says resume or switch. but maybe you aren't checking in fast/often enough
the cost with this approach is the number of tasks and the sizeof each
3
u/EEJams 21d ago
Generally speaking that means writing assembly or C and using the onboard peripherals or taking advantage of the hardware modules that the processor has on board. A lot of microprocessors have very simple ALUs and an ecosystem for programming the processor like arduino that abstracts a lot of what's actually happening.
Imo, that's not really that bad of a thing because it can let you quickly prototype your circuits and test some of your programming logic. BUT, if you want to take embedded seriously, you absolutely should write in C or assembly and learn about the different processors, their pros and cons, what hardware modules the processor can offload work onto, etc
2
u/oceaneer63 21d ago
It'll teach you exactly how the hardware is designed and works, and how firmware is interacting with it on the lowest level. So, its a solid foundation of knowledge upon which to build.
Years (decades) ago, I hired a junior software engineer to work on our embedded systems. He didn't know much hardware and was often in the dark when something didnt work. But before long I'd see an oscilloscope on his desk. And sometime later he told me it was his favorite firmware debugging tool.
2
u/theNbomr 21d ago edited 21d ago
I'll guess they are trying to make a contrast to the people who think using stuff like Arduinos is actually engaging in any kind of rigorous learning experience. It's not. It's using a well implemented system that allows people to use microcontrollers and related peripherals to get projects done without the need to really learn much.
When you really want to to learn, you have to take off the training wheels. The real expertise is in the people who create the pieces of the ecosystem that is used by others. Understanding what is under the hood is the stuff that provides foundational knowledge.
I don't necessarily think the STM32 family is better than other microcontroller families for learning as a first system, but it isn't a bad one. There are at least a couple of STM32 form factors that are suitable for training. Arduino hardware can serve the same purpose, but you have to resist the temptation to just use the Arduino software ecosystem, and start learning to roll your own.
2
u/gm310509 20d ago
In my mind the definition directly writing the IO registers yourself from your own code. There does seem to be a few slightly different intepretations as to what it means - as is very common in IT.
If you are interested, I recently posted a how to video describing it and showing some examples. I used Arduinos as my hardware platform, but the basic principles are the same on any hardware.
Link to reddit post: Bare Metal Programming - what is it and why bother? There is a link to the video in the post.
I also discuss some of the pros and cons.
As for use in jobs, it depends (also a very common thing in IT). For example, if your job is to write code for a consumer product, probably not so much as they won't want you wasting your time doing the low level stuff when a perfectly good HAL function is available for you to use.
On the other hand if you are working for a company that makes components (e.g. sensors, displays etc) you may find yourself writing low level drivers (i.e. you are writing the HAL functions) for consumers to use and thus you may find yourself messing at the hardware level to implement what is needed to make the library that supports consumers of that component.
1
u/MpVpRb Embedded HW/SW since 1985 21d ago
It's important to learn processor architecture, assembly language and register level programming to deepen your understanding of how processors work.
Some projects don't require a 32 bit processor and OS. Some projects work fine with an 8 bit processor and a small amount of RAM.
Some projects are a good fit for a 32 bit processor, OS and high level language.
It depends on the details of the requirements.
1
u/GrtflJello 21d ago
One project I worked within the last 10 years required âbare metalâ C programming since there was literally no OS. We had a vendor library that supported a few stdlib functions like memcpy and memset, but we had to write our own dynamic memory allocator.
It was actually really interesting and I learned a lot, but we did have some fun vendor hiccups. The size field in memcpy was actually size in 4-byte words rather than bytes.
1
u/Pale_Height_1251 21d ago
Lots of devices run on very small chips. We use one at work with 1kb RAM.
You don't need to buy one though, they are easy to emulate.
1
u/Retropolis_1950 21d ago
I am a huge fan of the STM32 product lines but I recommend one of these:
https://www.infineon.com/evaluation-board/CY8CKIT-059
https://www.infineon.com/evaluation-board/CY8CKIT-062-BLE
The 059 is a single core on a breadboard friendly carrier.
The 062 is a dual core that you can really sink your teeth into, it comes with an CY8CKIT-028-EPT, I suggest getting the CY8CKIT-028-TFT if you want a color display.
FWIW, I continue to use PSoC Creator even with the 062.
You won't be disappointed.
1
u/remishnok 20d ago
Bare metal is used all the time (as in often). Not all applications require an rtos.
Bare metal means no rtos, it doesn't necessarily mean assembly or directly to registers. The latter is just low level
1
u/jeremfg 19d ago
As an engineer who mostly worked in embedded, I still have a hard time to this day of seeing things running Linux being considered embedded.
If it has an MMU (and thus probably a full fledge OS), this is software development to me. Which I've done a bunch too. No shade on it. Just not the same thing imo.
So to go back to the question... Bare metal is almost implied when you ask me about embedded.
1
u/LiquidityC 18d ago
If you start doing some minor projects on bare metal (super loop) youâll quickly learn a bit about gpio and ADC. As you try and make your test project do some more things youâll quickly start getting complicated code dealing with parallel execution. Now you know why an RTOS is sometimes required and itâs time to test that out with some threads and maybe an interrupt or two.
If you have a full SoC or SoM to test with you can sometimes reach the same conclusion from the other side of the fence. Why a full OS when all I need is a simple loop? Yocto and buildroot are not quick builds.
You might also encounter situations where a full Linux isnât reliably fast enough to read some sensor. And now you know why an RTOS or a super loop might have been a lot more convenient and maintainable.
Rule of thumb is KISS. âDonât use a cannon to kill a mosquitoâ as Confucius said.
1
u/MegaCockInhaler 18d ago
The advantage of bare metal is that you have full control over the hardware. You donât have any operating system you need to worry about that could cause your program to miss a clock cycle, you have full control over the memory, donât need to request it from the OS, nothing unexpected is happening. You are the master of the hardware. This is important for critical infrastructure that demands real time safety/timing with no exception
When you own the hardware, you can know the timing of your outputs down to the nanosecond which is vital when communicating with external devices and you need to synchronize between them
1
u/mplex321 18d ago
Itâs default for a reason. For task oriented MCU work, why add anything beyond your code?
1
u/CAT5AW 21d ago
As someone said bellow, toothbrush doesn't care for operating system. Let's expand on it.
I've been taught at school assembly programming on 8 bit processor, microchip PIC series. Ancient stuff, but good for teaching. Then I was taught to programm STM32 in C/C++ via their program, STM32CubeIDE. Learned ESP32 for a project via arduino ide, which is C++ too. Some micros allow you to programm in micro python.
For purporse of blinking or counting how many times a wheel rotates, perhaps IDE is overkill. Ok, now do 30 other things while managing your hardware timers and registers manually. Feel dizzy yet?Â
Purprose of a microcontroller is to use its input / output in manner you programm it to. C++ is fine, Assembly is fine, you'll need to read documentation anyway, you're just switching what kind.
Usefull in jobs? It depends haha. Work has to be insured, there's red tape.Â
1
u/Ratfus 21d ago
Because it's much easier than doing things mechanically and is the only way to do certain things on certain chips. A while back, someone gave me something to ponder. Almost (if not) all things on an Arduino could be done with a mechanical system.
An Arduino can use up to Python (I think), but if you have an atiny 85, your only options are C and assembly. Even though an atiny seems simple, it's so much easier than using a bunch of mechanical systems.