Defines for ALT functions of pins.

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
rew
Posts: 383
Joined: Sat Jul 19, 2014 12:59 pm
Has thanked: 2 times
Been thanked: 14 times

Defines for ALT functions of pins.

Post by rew »

I made a stupid mistake this weekend:

I copied:

Code: Select all

 palSetPadMode(GPIOB, 13, PAL_MODE_ALTERNATE(5) |
                           PAL_STM32_OSPEED_HIGHEST);           /* SCK.     */
from the '407-mems demo to my project on the '072 because I wanted PB13 to be SCK for SPI2, just like on the '407 discovery board. The problem is that the "magic number" 5 in there does not hold for the '072.

So, I would like to write:

Code: Select all

palSetPadMode(GPIOB, 13, PAL_MODE_ALTERNATE(AF_SPI2));  
to indicate that I want the SPI2 AF. This should be defined to "5" for STM32F4, and to "0" for STM32F0.

Possibly we'd need the define to read "AF_GPIOB_SPI2" if the define needs to be different for the higher ports. Hmm. The defines are (on STM32F0) specific for the PIN. Then maybe I'd prefer to write something like make_PB13_SPI2_SCK().

Any other suggestions to make it possible to catch errors like this at compile-time instead of after a deep debugging session?
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Defines for ALT functions of pins.

Post by Giovanni »

It is a good idea, in general, to enclose all the device-specific code (configurations, initialization functions) into a separate module.

Hard to make it error proof however, doing embedded is spend half of time in search of stupid errors :)

Giovanni
rew
Posts: 383
Joined: Sat Jul 19, 2014 12:59 pm
Has thanked: 2 times
Been thanked: 14 times

Re: Defines for ALT functions of pins.

Post by rew »

The project as a whole is "portable": it runs on different processors on the same PCB, with the processors being mostly compatible.

This "mostly" extends to the SPI2 module being hooked up to PB12, 13, 14, 15.... but apparently not to the AF register settings. What ChibiOS does a whole lot better than libopencm3 is hide the differences between those compatible CPUs. I needed a bunch of defines to switch between different ways of making a pin output in libopencm3, while ChibiOS provides me with a PalSetMode (...OUTPUT_PUSHPULL );

While Chibios hides e.g. the different widths of the AF registers between STM32F0 and STM32F4 from me, extending this to the "AF setting for SPI2" would be very nice and not at all "outside of scope".
Post Reply