cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F series peripheral syntax

Jay Vincet
Associate II
Posted on December 19, 2016 at 19:45

Greetings,

I need to come up to speed quickly on the STM32F4 series processors. I have been in embedded development for a number of years and have experience with Renesas, PowerPC, and PIC32 processors, etc and now I need to know about ST processors.

I hope I am not asking seriously stupid question but I really would like to find some simple sample code for GPIO, ADC, timers, etc. I have looked through the ST site and I am sure I missed something but I feel like instead of sipping I am being drowned by a fire hose. I am setting up the device using STM32Cube but I really would like some example code for the peripherals I listed above and others. I looked in the programmer's manual thinking it would list commands and syntax for setting up GPIOs, etc but found nothing.

I have found some snippets of code here and there but some of it is confusing. For instance the following line is supposed to OR-IN GPIOB pin 6 as an output  but the shift looks wrong to me, looks like 12 instead of 6 bits.

GBIOP->MODER |= 0x01 << (2*6);      //Set pin 6 as digital out.

So where does one find syntax for setting up peripherals?

Sorry to come off as such a noob, but that is how this old gray haired engineer feels right now.

Thanks,

jv

1 ACCEPTED SOLUTION

Accepted Solutions
Seb
ST Employee
Posted on December 19, 2016 at 20:19

Hi Jay,

Searching something handy on the web this one should contain examples running on some nucleo or discovery boards.

http://www.st.com/en/embedded-software/stm32cubel4.html

 

STM32L4 is a Cortex M4 mid performance and having some analog/low power specifics (ADC, DAC, OpAmp, MSI, etc...)

You'll find 2 types of examples, HAL (which is abstract lib functions mostly compatible across STM32 families), or LL (low layers) which are mostly macros to directly drive the peripheral HW register.

For the GPIO Mode register, each pin has 2 bits, analog, input, output, alternate function. The code is correct.

First choose an STM32, get to the

http://www.st.com/resource/en/reference_manual/dm00083560.pdf

which describe the common peripherals. Then look at the

http://www.st.com/resource/en/datasheet/stm32l486jg.pdf

of the product for specifics. When designing a new board and choosing a part and its package, CubeMX is a PC Tool to configure and init based on what the application need. Otherwise, from the datasheet, get the pinout, alternate table and DMA stream table handy for quick overview.

The peripherals are pretty rich and have grown up over the years to satisfy most of the mass market demand...

Welcome to STM32 !

Another grey hair engineer guy :)

View solution in original post

6 REPLIES 6
Seb
ST Employee
Posted on December 19, 2016 at 20:19

Hi Jay,

Searching something handy on the web this one should contain examples running on some nucleo or discovery boards.

http://www.st.com/en/embedded-software/stm32cubel4.html

 

STM32L4 is a Cortex M4 mid performance and having some analog/low power specifics (ADC, DAC, OpAmp, MSI, etc...)

You'll find 2 types of examples, HAL (which is abstract lib functions mostly compatible across STM32 families), or LL (low layers) which are mostly macros to directly drive the peripheral HW register.

For the GPIO Mode register, each pin has 2 bits, analog, input, output, alternate function. The code is correct.

First choose an STM32, get to the

http://www.st.com/resource/en/reference_manual/dm00083560.pdf

which describe the common peripherals. Then look at the

http://www.st.com/resource/en/datasheet/stm32l486jg.pdf

of the product for specifics. When designing a new board and choosing a part and its package, CubeMX is a PC Tool to configure and init based on what the application need. Otherwise, from the datasheet, get the pinout, alternate table and DMA stream table handy for quick overview.

The peripherals are pretty rich and have grown up over the years to satisfy most of the mass market demand...

Welcome to STM32 !

Another grey hair engineer guy :)

Posted on December 19, 2016 at 21:58

Thanks Seb!

You have certainly brightened my dreary day.

jv

Oliver Beirne
Senior
Posted on December 20, 2016 at 03:32

Hi

jayvincet

Happy to see you found an answer to your question . Just wanted to let you know I&39ve moved your post to

https://community.st.com/community/stm32-community/stm32-forum?sr=search&ampsearchId=66a1fa73-7d43-4166-b40f-ffb9c72d5906&ampsearchIndex=0

 where product-related questions are asked and you can also post future questions here as well.

Thanks

Oli

Posted on December 20, 2016 at 04:49

The Cube F4 sub directories should have an array of example code, for multiple different boards. The earlier SPL (Standard Peripheral Library) provided a reasonably comprehensive set of examples, which has been augmented by postings to the forum here.

The MODER bit definitions can be found in the STM32F4 Reference Manual (RM0090), 2-bits per pin (16 pins in a bank, so a 32-bit register). I wouldn't just OR things like this as it tends to make assumptions about initial state. There are several forum members that like the register level programming method, I've tended to avoid programming at this level because it is not very portable across STM32 family member, and the nuances tend to be harder to communicate.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on December 20, 2016 at 16:34

Thanks for the post. I have found some snippets of GPIO code:

GPIO_WriteBit(GPIOC , GPIO_Pin_9 , x);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA , &GPIO_InitStructure);

GPIO_ReadInputDataBit(GPIOA , GPIO_Pin_0)

Perhaps these are from the SPL.

Pardon my ignorance but where can I find the SPL? Also I would certainly like to see the group's augmentations. BTW, we are using the IAR IDE.

Posted on December 20, 2016 at 17:10

The F4 DISCO has it's own SPL release, with examples, although perhaps dated

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32-standard-peripheral-libraries-expansions/stsw-stm32068.html

 

I'd start with the F4 DSP release of the SPL, it targets primarily the EVAL series boards, but isn't tied to them

http://www.st.com/en/embedded-software/stsw-stm32065.html

 
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..