Page 1 of 1

Defines for ALT functions of pins.

Posted: Mon Feb 23, 2015 6:15 pm
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?

Re: Defines for ALT functions of pins.

Posted: Mon Feb 23, 2015 7:14 pm
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

Re: Defines for ALT functions of pins.

Posted: Sun Mar 01, 2015 2:30 pm
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".