I'm fiddling around with different options for configuring SP1 on the Nucleo F446RE board. One drag is that I set it all up to use PA5 as the SCL on SPI1 and it all works fine BUT I realized that PA5 is also the boards user LED and I now want to use that.
After several hours scouring the web, datasheets and manuals it dawned on me that it would be very helpful to be able to refer to GPIO pins by their common names like PA1, PB6 and so on.
HAL seems to not bother with this and so we have to explicitly state GPIOA and PIN6 when we want to use PA6 as the MISO pin for SPI1.
So I devised these simple macros:
Then I can do this
You get the picture, this then lets me write helper functions that accept a 64 bit encoded port/pin and decode them inside the function when I'm setting up the SPI and GPIO. The code can be more generic looking because it just gets the PORT and the PIN from the encoded value and the caller only ever worries about the universal PIN ID (PA1, PB7, PD8 etc).
But does HAL do this already somewhere so I don't reinvent the wheel...
By the way, in case people think that's a lot of typing it isn't. Visual Studio leverages Copilot AI and is able to correctly infer what I'm doing, just pressing enter after PB9 leads to this:

I just press TAB and it inserts that suggested line, it even automatically knew that after PA15 it needed to start using GPIOB_BASE, that's very powerful.
After several hours scouring the web, datasheets and manuals it dawned on me that it would be very helpful to be able to refer to GPIO pins by their common names like PA1, PB6 and so on.
HAL seems to not bother with this and so we have to explicitly state GPIOA and PIN6 when we want to use PA6 as the MISO pin for SPI1.
So I devised these simple macros:
C:
#define ENCODE_PIN(B,P) ((uint64_t)(((uint64_t)(B) << 16) | (uint16_t)(P)))
#define DECODE_PIN(P) ((uint16_t)((uint64_t)(P) & 0xFFFF))
#define DECODE_BASE(P) ((uint32_t)((uint64_t)(P) >> 16))
Then I can do this
C:
#define PA1 ENCODE_PIN(GPIOA_BASE,GPIO_PIN_1)
#define PA2 ENCODE_PIN(GPIOA_BASE,GPIO_PIN_2)
#define PA3 ENCODE_PIN(GPIOA_BASE,GPIO_PIN_3)
#define PA4 ENCODE_PIN(GPIOA_BASE,GPIO_PIN_4)
#define PA5 ENCODE_PIN(GPIOA_BASE,GPIO_PIN_5)
#define PA6 ENCODE_PIN(GPIOA_BASE,GPIO_PIN_6)
But does HAL do this already somewhere so I don't reinvent the wheel...
By the way, in case people think that's a lot of typing it isn't. Visual Studio leverages Copilot AI and is able to correctly infer what I'm doing, just pressing enter after PB9 leads to this:

I just press TAB and it inserts that suggested line, it even automatically knew that after PA15 it needed to start using GPIOB_BASE, that's very powerful.



