Bare metal STM32 programming

Related to the the forum.
miki
Posts: 26
Joined: Mon Jan 27, 2020 3:39 pm

Bare metal STM32 programming

Post by miki »

Idea:
A topic about using bare metal STM32 programming based on Maple core and its derivatives.It can be compiled in ARduino IDE.Code snippets,what to look for,familiarization with.
I have used many times direct register manipulation in many of my codes.I just wonder if there are some topics on this subject.
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Bare metal STM32 programming

Post by fpiSTM »

Hi @miki
I moved your post to Idea section ;)
Well there is already a code and snippet sub forum where you can share your code.
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: Bare metal STM32 programming

Post by dannyf »

a good idea. but difficult to implement with the arduino ide.

this approach would requirement multiple .c/.h files -> each dealing with a specific functionality / hardware modules. a typical project for me is 20+ files.

unfortunately that's where the arduino ide fails. it has the ability to use multiple files but it is not designed to use multiple files. An (awkward) example of that is here: https://github.com/dannyf00/ghetto-ball ... rono_0.ino. it is simply the augmentation of multiple .c/.h files into one .ino file. Works but isn't pretty.

So if you are into coding simple tasks without the arduino add-on, the arduino ide works but not the best and most efficient venue.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Bare metal STM32 programming

Post by ag123 »

well, even in stm32duino official and libmaple core, one can always access the registers
you can look at the core source codes, it is pretty much 'templated' bare metal programming
the core is literally a framework so that it setup the clocks at reset, most newbies would stumble just at that step alone
on stm32duino it setup usb-serial as well
arduino/wiring simplify the template to just setup() and loop(), that makes it much less intimidating vs having to deal with the clocks
in fact one should always have the stm32 ref manual handy, even if one is calling 'library' codes,
i'd guess many people are 'spoil' and depended on 'libraries' for even some rather simple sensors,
it seemed some did not even attempt to interface a sensor from the datasheet and manuals and simply looked for a 'library', a library that works on Atmega is unlikely to just work on stm32 unless 'high level' interfaces are used which'd probably be slow first hand
feluga
Posts: 64
Joined: Wed Mar 18, 2020 2:50 am

Re: Bare metal STM32 programming

Post by feluga »

My two cents on this topic:

https://github.com/RoCorbera/BlueVGA/bl ... gadriver.c

This is an arduino library that I built for driving VGA displays.
This file uses Bare Metal programming using Arduino IDE for building it.
It demonstrates how to use STM32F103 Registers directly in C code using both Roger's Core and the official STM32 Core.

I setup GPIOs, TIMERs, RCC and use its register value (reading/writing them) directly in C code. The same can be done in CPP, of course.

I hope it can help you!
miki
Posts: 26
Joined: Mon Jan 27, 2020 3:39 pm

Re: Bare metal STM32 programming

Post by miki »

I don't need help just a discussion.
I don't think is complicated.And I used many times code compiled in Arduino IDE. Working directly with registers. It feels to me faster to write code and easier to understand.Sometimes I combine the bare metal code with direct functions from libmaple
See an example initialization timer2.

Code: Select all

void InitTimer2()
{
	RCC_BASE->APB1ENR |= RCC_APB1ENR_TIM2EN ;// Enable clock
	TIMER2_BASE->ARR = 0xFFFF ;
	TIMER2_BASE->PSC = 35 ;	// 0.5uS
	TIMER2_BASE->CCER = 0 ;	
	TIMER2_BASE->CCMR1 = 0 ;
	TIMER2_BASE->EGR = 0 ;
	TIMER2_BASE->CR1 = 1 ;
}
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: Bare metal STM32 programming

Post by dannyf »

It is not complicated at all - reading the datasheet will get you there.

and if you don't care about code modularity / re-usability, you can simply dump all the files into one .ino file and call it a day. To me, that's the wrong way of doing things.

as to your code below, I would suggest
1. use commentary to make the code easier to read, even to yourself later on;
2. try to use the device definitions in the standard header files. for example, use "TIM2->" instead of "TIMER2_BASE->", so if you ever want to port the code to other environments.
3. I would provide it with some configurability. for example, let the user specify the prescaler.

but that's highly subjective: my philosophy is that the only reason I write code is so that I don't have to rewrite the same code in the future.

hope that helps.
feluga
Posts: 64
Joined: Wed Mar 18, 2020 2:50 am

Re: Bare metal STM32 programming

Post by feluga »

As for the discussion about Bare Metal, I think it's a personal decision.

Of course, there are many ways to program some STM32s beside using C or C++.
Some higher end STM32 can be programmed using Python as even Java Script.
I have seen people writing programs in Rust for the Blue Pill.

Bare Metal is the closest you can get to the processor, therefore the fastest way to program it, but it is only for those who want to read a 1,400 page user manual and those who have time to experiment a lot using it, thus the learning curve may be very long.

A potential problem about Bare Metal is how portable your code will be.
At least, using HAL your code at least will be portable among several CPUs.
But, of course, everything depends on your objectives.

For 99% of the regular people, writing a program (in a fast and effective way) using the Arduino API for prototyping is good enough...
By the way, this is probably the most portable way to write code for microcontrollers!
miki
Posts: 26
Joined: Mon Jan 27, 2020 3:39 pm

Re: Bare metal STM32 programming

Post by miki »

Some functions you may need in a project and you cannot find them in arduino.
like:
RCC_DeInit();//In standard STM32 library
NVIC_SystemReset();//CMSIS
and you need to improvise(like bare metal programming) if you work in an Arduino environment.
I like flexibility not to be bound by a specific orthodoxy on how the code should be.
I combined in a project Arduino functions with bare metal and asm code.Some people may not like it but it worked.
racemaniac
Posts: 20
Joined: Wed Dec 18, 2019 6:53 pm

Re: Bare metal STM32 programming

Post by racemaniac »

Bare metal with the STM core or even without a core is also very interesting :).

Once i got better acquainted with the STM32's, i really liked the cubemx code generator to generate basic code configuring the peripherals, and then continue bare metal programming with that code & the datasheet as a basis.
Post Reply

Return to “Ideas & suggestions”