Bare metal STM32 programming

Related to the the forum.
dannyf
Posts: 30
Joined: Sat Jul 04, 2020 7:46 pm

Re: Bare metal STM32 programming

Post by dannyf »

the cubemx code generator
I guess there are differing flavors of "bare metal".

racemaniac
Posts: 8
Joined: Wed Dec 18, 2019 6:53 pm

Re: Bare metal STM32 programming

Post by racemaniac »

dannyf wrote:
Fri Aug 28, 2020 12:38 am
the cubemx code generator
I guess there are differing flavors of "bare metal".
Picking apart the generated code and writing your own version of manually configuring all the peripherals & DMA?

It really helped me when going bare metal. Interpreting the datasheet & getting every step right was annoying. Having the CubeMx code as a starting point really helped a lot :).

dannyf
Posts: 30
Joined: Sat Jul 04, 2020 7:46 pm

Re: Bare metal STM32 programming

Post by dannyf »

coding to the datasheet may sound challenging initially but it is not that hard. it allows you to migrate away from vendor libraries and develop a better understanding of the hardware -> the essence of "bare metal programming".

racemaniac
Posts: 8
Joined: Wed Dec 18, 2019 6:53 pm

Re: Bare metal STM32 programming

Post by racemaniac »

dannyf wrote:
Sun Aug 30, 2020 10:48 pm
coding to the datasheet may sound challenging initially but it is not that hard. it allows you to migrate away from vendor libraries and develop a better understanding of the hardware -> the essence of "bare metal programming".
Yeah, and the generated code just adds to that documentation :). I mean, we were originally talking about "bare metal programming" using libmaple that also already maps all the registers etc... When learning bare metal programming, i started with the datasheet approach, which really sucked, getting I2C running on DMA took me days of searching and frustration. (i was very new, so that's probably also a reason), a single mistake is fatal. Later i used the generated code to get started from, which is both a good example of all the datasheet info combined, and a working example. If my own code didn't work, i'd compare register values, and quickly find the things i was missing :).
I'm not saying that generating cubemx code & using that is bare metal programming. I'm saying you can do great bare metal programming using its register mappings & the generated code as an example. But the generated code is very inefficient code. It always assumes nothing is set up when you initialize a peripheral/dma. So if you know one dma channel is always doing the same type of transfers, you can greatly optimize starting the transfer after the first setup, and throw away a lot of bloat :). And that requires bare metal programming, getting to know every register, know which things you need to set, vs which are still correct :).
If i'd do some bare metal programming in libmaple for a peripheral where there aren't any examples yet, i'd start generating the cubemx code for what i want to do, and use that as a working example/documentation, and together with the datasheet, i'd get anything working in a single evening :).

MGeo
Posts: 25
Joined: Thu Dec 19, 2019 10:29 am

Re: Bare metal STM32 programming

Post by MGeo »

Nothing stopping you, go ahead and do it.

The challenges I've run into with bare metal (which I think of as CMSIS / LL / c and asm register access to hardware as opposed to using Arduino higher level functions) is name collisions, multiple definitions and hard coded interrupts in core implementations. It has forced me into undesirable workarounds and sometimes help from our resident hero fpiSTM with core mods.

With libmaple and target F1 hardware are getting to be end-of-life I would recommend a more current core or just going direct to using VSCode/CubeMX/gcc. A cheap hardware debugger to track register manipulations and logic analyzer to view multiple pin outputs are super useful as well.

racemaniac
Posts: 8
Joined: Wed Dec 18, 2019 6:53 pm

Re: Bare metal STM32 programming

Post by racemaniac »

MGeo wrote:
Wed Sep 16, 2020 9:07 am
Nothing stopping you, go ahead and do it.

The challenges I've run into with bare metal (which I think of as CMSIS / LL / c and asm register access to hardware as opposed to using Arduino higher level functions) is name collisions, multiple definitions and hard coded interrupts in core implementations. It has forced me into undesirable workarounds and sometimes help from our resident hero fpiSTM with core mods.

With libmaple and target F1 hardware are getting to be end-of-life I would recommend a more current core or just going direct to using VSCode/CubeMX/gcc. A cheap hardware debugger to track register manipulations and logic analyzer to view multiple pin outputs are super useful as well.
Indeed :)

When doing low level stuff, i was really happy i bought a license of VisualGDB. It allowed me to import a cubemx project into visual studio, and then continue developing in that awesome IDE. The functionality that VisualGDB brings is great. It knows all the registers of all STM32's, and supports pretty much any debugger. I used an ST-link V2 to program & debug the STM32, at a breakpoint you can view all the registers by name, you can take any piece of c(++) code, and immediatly go to the disassembly to see what it was assembled to (had some fun learning some of the instructions the stm32 has, and then see if the compiler was using them), etc... I usually avoid not open source platforms a bit, but as microsoft & visual studio are currently on a course of being more open, open source & accessible rather than the other way (and i use it professionally too), this really was an awesome option :). And VisualGDB is worth the money imo :). Guys making such great tools really earned it :).

dannyf
Posts: 30
Joined: Sat Jul 04, 2020 7:46 pm

Re: Bare metal STM32 programming

Post by dannyf »

most of the code you find will be developed against CMSIS and a set of vendor header files. so I would encourage you to at least try to align there.

alfrond
Posts: 4
Joined: Tue Nov 24, 2020 5:02 pm

Re: Bare metal STM32 programming

Post by alfrond »

Being obviously much less experienced than the rest of you, would you please point me in the right direction in the following:
- What is the most general syntax in writing directly to registers? If core dependent, where can I find the complete syntax?
- "Where"/"How" is that syntax defined?

I'm a beginner programmer and my project is to build a dc-dc with the STM32 as brains. Thus configure pwm of opposite polarity and perhaps use the dead-time features of the periferal.
I have some experience with the arduino IDE and direct register manipulation on the arduino boards, but something in all this escapes me.. I am quite familiar with the STM32 Reference Manual at this time, but I lack the overall picture of what bits of software actually adds what level of features. I understand the basic concepts of libraries and I understand that a 'digitalWrite' ultimately results in some register manipulation and reads, but - considering for instance felugas vga driver above - the "TIM1_REG" appearently is his(/her?) own syntax and "TIMER1_BASE" and "TIM1" that of rogers core and Arduino_Core_STM32 respectively.. But is there another underlying level in terms of which these two cores are defined? How can I trace this dependency back-wards towards raw hardware?
I would love to get a 'complete' picture of how, e g, timer functionality can be implemented and where in this hierarchy for instance HAL and the timer.h of leaflabs should be placed.

I don't expect a full reply here but perhaps a point in the right direction. And if I'm in the wrong thread please relocate me:)

stevestrong
Posts: 354
Joined: Fri Dec 27, 2019 4:53 pm
Answers: 7
Location: Munich, Germany
Contact:

Re: Bare metal STM32 programming

Post by stevestrong »

Here you can find an example how to mix timer register settings on low and higher level using Libmaple core.

dannyf
Posts: 30
Joined: Sat Jul 04, 2020 7:46 pm

Re: Bare metal STM32 programming

Post by dannyf »

Bare Metal is the closest you can get to the processor,
you can always code assembly. or even hand-generate binary.
A potential problem about Bare Metal is how portable your code will be.
I usually code to cmsis. it has the most portability.

Post Reply

Return to “Ideas & suggestions”