Page 1 of 1

libopeninv STM32F4

Posted: Tue Jun 29, 2021 3:31 pm
by johu
For a side project I made a branch of libopeninv for the STM32F4.

Again, I will blog a bit about it. Note that at this stage the code is untested, it doesn't run.
USART terminal via DMA and GPIO running.

You can see the changes in the stm32f4 branch: https://github.com/jsphuebner/libopeninv/tree/stm32f4

DMA
DMA is organized a bit differently. Basically there are two DMA controllers (not unique, there also also F1s with 2 DMA controllers). But they are organized in streams and channels. Streams on the F4 is a bit like channels on the F1. Every peripheral is hardware-assigned to a certain stream, sometimes to more than one, to avoid conflicts. You then have to tell the stream which of the possible peripherals it should talk to.
Consequently, only some renaming is needed, e.g. instead of

Code: Select all

dma_enable_channel(DMA2, DMA_CHANNEL1);
we now write

Code: Select all

dma_enable_stream(DMA2, DMA_STREAM0);
The only additional command is selecting the channel of the stream

Code: Select all

dma_channel_select(DMA2, DMA_STREAM0, DMA_SxCR_CHSEL_4);
ADC
Looks to work the same, only the calibration commands are missing.
An additional command is needed to enable continuous conversion via DMA:

Code: Select all

adc_set_dma_continue(ADC1);
CAN
Only slight changes in ISR names

Flash
Seems to be organized in rather large sectors rather than pages. Will have to see how that works

GPIO
Different concept. When using a pin for a peripheral we have to explicitly specify which peripheral is connected to the pin. The indexes are in the manual.
In place of

Code: Select all

gpio_set_mode(GPIO_BANK_CAN1_TX, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_CAN1_TX);
we write

Code: Select all

gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO11 | GPIO12);
gpio_set_af(GPIOA, GPIO_AF9, GPIO11 | GPIO12);
So the additional clear/set command for selecting pull-up/down is not needed. Also there is just one output frequency. Note the convenience macros don't exist (or have been renamed?)

Clock setup

Code: Select all

rcc_clock_setup_in_hse_8mhz_out_72mhz()
changes to

Code: Select all

rcc_clock_setup_pll(&rcc_hse_8mhz_3v3[RCC_CLOCK_3V3_168MHZ])
That probably means going over the various peripheral prescalers to get valid CAN baudrates and scheduler intervals.

RTC
Seems very different, rtc_get_counter_val() doesn't exist anymore.

Makefile
in the CFLAGS and CPPFLAGS we need to change -DSTM32F1 to -DSTM32F4 to select the correct header files of libopencm3
Also some hardware specific flags need to be added/changed:

Code: Select all

-mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16
and finally the linker needs to ling against -lopencm3_stm32f4

Re: libopeninv STM32F4

Posted: Tue Jun 29, 2021 7:12 pm
by johu
Got blinky :)
Using TIM2 for scheduling turned out a bad idea, it is a 32-bit counter.

Re: libopeninv STM32F4

Posted: Wed Jun 30, 2021 4:33 pm
by johu
Boot loader done. Same protocol as always, just different memory organization.
Look here: https://github.com/jsphuebner/tumanako- ... ee/stm32f4

Basically the F4 has 4 sectors that are 16k in size and start at address 0x08000000 (as always). Then follows sector 4 which is 64k. And finally sectors 5-11 which are 128k each. Right now I have reserved space for firmware only in sector 5, so 128k, start address 0x08020000.
The configuration data will live in one of the 16k sectors and no longer at the end of flash.

Re: libopeninv STM32F4

Posted: Wed Jun 30, 2021 5:18 pm
by Jack Bauer
Any f4 in particular?

Re: libopeninv STM32F4

Posted: Wed Jun 30, 2021 5:21 pm
by johu
Currently using the Olimex STM32-H405 which is home to an STM32F405RGT6

Re: libopeninv STM32F4

Posted: Thu Jul 01, 2021 5:12 pm
by Jack Bauer
It sure looks like the f407 and f107 are pin compatible ....

Re: libopeninv STM32F4

Posted: Thu Jul 01, 2021 8:38 pm
by mackoffgrid
Jack Bauer wrote: Thu Jul 01, 2021 5:12 pm It sure looks like the f407 and f107 are pin compatible ....
Yes, one of the strengths of the stm32 families.

Re: libopeninv STM32F4

Posted: Thu Jul 01, 2021 10:16 pm
by mackoffgrid
johu wrote: Tue Jun 29, 2021 3:31 pm For a side project I made a branch of libopeninv for the STM32F4.
Might there be some benefit to look at the stm32F3 ? I've never used them but I understand they have a faster PWM clock?

Re: libopeninv STM32F4

Posted: Thu Dec 22, 2022 7:11 pm
by Pete9008
Just wanted to say thanks for the above :)

BTW - the easiest fix for the loss of the RTC timer functionality could be using systick instead (compiles OK but not tested yet).