PlatformIO Community

Blackpill stm32 incoherence with some pin names (PB_12 not equal to PB12)

Hi there,
I’m working in a project for blackpill stm32f411, was having issues with certain pins & digitalWriteFast().
I’ve noticed that those pins that I’m having problems with, have a different value in the different pin name format, and the ones that work fine have the same value in both formats. Any suggestion about how to deal with this would be much appreciated.

PB12 = 27, PB_12 = 28
PB13 = 28, PB_13 = 29
PB14 = 29, PB_14 = 30
PB15 = 30, PB_15 = 31
PC13 = 31, PC_13 = 45
PC14 = 32, PC_14 = 46

The Px_y and Pxy enums / macros do not have to line up at all. Different APIs require different pin number input forms. Take a look at e.g.

Where PB11 = 0 and

PB_11 is (1 << 4) + 0xB = 27.

The STM32Duino’s digitalWrite() etc APIs take the pin macro as defined for each variant, completely arbitrarily. That’s because they are internally remapped through the pin mapping arrays back to their source pin data, see here. Other libraries like digitalWriteFast use the more direct PinName enum (Px_y) (which are static and not dependent on variant definitions) so that they don’t have to perform a remapping.

You always have to use the pin numbering variant that the API you’re calling into expects – there’s not much else to it. Though you can always convert the pin macro (e.g. PB11) to its PinName enum (PB_11) by using the digitalPinToPinName macro, e.g. digitalPinToPinName(PB11) = PB_11.

Thanks a lot for taking time to answer and the detailed info, anyway, the question is that I can’t use those mentioned pins (PB_12, PB_13, PB_14, PB_15, PC_13 & PC_14) with digitalWriteFast(),
I have a multiplexer connected to some of those pins, with digitalWrite() (and using the pin names without “_”) works perfect, but with digitalWriteFast() it does not work as expected.

this works BTW:
digitalWriteFast(digitalPinToPinName(PB12), HIGH);
but I wonder if there’s any drawback about using digitalPinToPinName or if there’s a better solution for this

But digitalWriteFast(PB_12, HIGH); works not? Can you print the result of (int) digitalPinToPinName(PB12)? It should be 0x1c = 28, same as (int) PB_12.