Selecting Alternate Function on STM32F1xx series

Discussions and support about ChibiOS/HAL, the MCU Hardware Abstraction Layer.
Post Reply
Fulcrum Mike
Posts: 14
Joined: Fri Dec 23, 2016 3:33 am

Selecting Alternate Function on STM32F1xx series

Post by Fulcrum Mike »

TL;DR

On STM32F4xx I could use PAL_MODE_ALTERNATE(n) to configure a pin for a particular alternate function.

How to make sure on STM32F1xx that a pin in initialized correctly for a particular alternate function? Do I need to see the reference manual every time to find out the appropriate setting (PAL_MODE_STM32_ALTERNATE_PUSHPULL, PAL_MODE_INPUT_PULLUP, etc.) for each pin against each alternate function or is there some more elegant way to correctly initialize the required pins?


Hi all,

On an STM32F407-Discovery Kit, I could easily tell PA2 and PA3 to connect to SERIAL 2 hardware using:

Code: Select all

   //Pins initialization for STM32F4xx
   palSetPadMode(GPIOA, 2, PAL_MODE_ALTERNATE(7));
   palSetPadMode(GPIOA, 3, PAL_MODE_ALTERNATE(7));


I came across some cheap STM32F103C8-MINIMUM boards. I copied RT-STM32F103-STM3210E_EVAL-FATFS-USB demo project, and made appropriate changes in the Makefile. Then, removed most of the demo code and replaced it with a very simple LED blinking code. Compiled it and after a few tries, it worked fine. Then I tried to enable SERIAL 2 hardware on PA2 and PA3, only to learn that STM32F1xx series doesn't number the alternate function like STM32F4xx.

ST's Reference manual for STM32F1xx says that:

If several peripherals share the same I/O pin, to avoid conflict between these alternate functions only one peripheral should be enabled at a time through the peripheral clock enable bit (in the corresponding RCC peripheral clock enable register).


Following instructions from ST reference manual (Table 24: USARTs), I learnt that I could configure PA2 and PA3 for SERIAL 2 using:

Code: Select all

   //Pins initialization for STM32F1xx
   palSetPadMode(GPIOA, 2, PAL_MODE_STM32_ALTERNATE_PUSHPULL );
   palSetPadMode(GPIOA, 3, PAL_MODE_INPUT_PULLUP );


So here's my question:

On STM32F4xx I could use PAL_MODE_ALTERNATE(n) to configure a pin for a particular alternate function.

How to make sure on STM32F1xx that a pin in initialized correctly for a particular alternate function? Do I need to see the reference manual every time to find out the appropriate setting (PAL_MODE_STM32_ALTERNATE_PUSHPULL, PAL_MODE_INPUT_PULLUP, etc.) for each pin against each alternate function or is there some more elegant way to correctly initialize the required pins?

Cheers.
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Selecting Alternate Function on STM32F1xx series

Post by Giovanni »

Hi,

Like for all other drivers, reading the RM is not optional :)

F1 devices do not support multiple alternate functions, it is either alternate or not alternate. Some functions can be remapped using AFIO registers, the PAL driver does not touch those. It is not very elegant, probably this is why they replaced the GPIO/AFIO logic in newer devices.

Note that AFIO has several erratas, see the ST errata document when designing a board around an F1.

Giovanni
Post Reply