pinName vs pinNumber

The official STMicroelectronics Arduino core
Post Reply
User avatar
hoek67
Posts: 59
Joined: Thu Mar 28, 2019 1:11 am
Location: Mount Beauty, Victoria, Australia
OS: Windows 10, Linux
IDE: STM32duino via Visual Micro in VS 2017
Core: STM official (I think)
Board: STM32F407VET6
Contact:

pinName vs pinNumber

Post by hoek67 » Thu Mar 28, 2019 6:35 am

So... for a physical pin... say PC4 as it appear on the board has 2 definitions... mainly PC_4 and PC4.

Seems these ARE different and handled differently.

PC4 is just some incremental number that is unique but has nothing to do with ports etc while with PC_4 a port/pin combination can be directly calculated.

BASICALLY PA_1 is port base 0, pin mask 0x0001
PA_2 is port base 0, pin mask 0x0002
...
PA_15 is port base 0, pin mask 0x8000
...
PB_xx is port base 1,
PC_xx is port base 2... etc

Just scratching my head as to why 2 different approaches were implemented... not like pin numbers are compatible.

Anyway... I'm going to experiment and see what it takes to just go the PA_1 type direction. Was wondering why program froze as had used PC_4 in 1 place and PC4 in another and just cast to (uint32_t) to keep it happy.

User avatar
zoomx
Posts: 43
Joined: Tue Mar 05, 2019 8:10 am
Location: Mt Etna - Italy
OS: Windows 7 & 10
IDE: 1.8.8
Core: Roger & STM official
Board: Bluepill, Maple mini

Re: pinName vs pinNumber

Post by zoomx » Thu Mar 28, 2019 9:47 am

As far as I know they are the same but I don't know why there are two notations.

User avatar
Vassilis
Posts: 150
Joined: Wed Feb 27, 2019 5:09 pm
Answers: 2
Location: Thessaloniki, Greece
OS: Linux, Win10, MacOS
IDE: Arduino 1.8.9
Core: Roger, STM official
Board: Bluepill, Maple mini, STM32F4xx
Contact:

Re: pinName vs pinNumber

Post by Vassilis » Thu Mar 28, 2019 3:34 pm

I took as example the Nucleo STM32F411RE board:

It seems that PC4 is referenced to the arduino pin definition on Nucleo boards (D45) and PC_4 is referenced to the actual PC4 pin (PortC, pin 4).That means [PA0,...,PA15] + [PB0,...,PB15] + [PC0,...,PC4] = 16 + 16 + 5 - 1 = 36
It is a little bit confusing that

Test sketch

Code: Select all

// the setup routine runs once when you press reset:
void setup() {                
  Serial.begin(115200);
  Serial.print("PC4  ");
  Serial.println(PC4);
  Serial.print("PC_4 ");
  Serial.println(PC_4);  
}

// the loop routine runs over and over again forever:
void loop() {
}
-Vassilis Serasidis

User avatar
hoek67
Posts: 59
Joined: Thu Mar 28, 2019 1:11 am
Location: Mount Beauty, Victoria, Australia
OS: Windows 10, Linux
IDE: STM32duino via Visual Micro in VS 2017
Core: STM official (I think)
Board: STM32F407VET6
Contact:

Re: pinName vs pinNumber

Post by hoek67 » Fri Mar 29, 2019 12:07 am

:shock: Had a late night (ie 2AM) code hacking session... for lack of a better word and totally "went-to-town" on PA1, PA2, PA3, A0, A1 and all his friends.

First step was to find and remove all such constants (wrapped in warm cuddly comment blocks) and just got an idea as to how deep things wanted to complain.

Seemed only the top "arduino" layer as most code... instantly converts "Arduino digital pin number" to a "pin name" by a lookup table.

So swung the axe until code that referenced A0 and friends went...

End result is a LOT less code and basically a layer of complexity gone. The stm boards don't have a A0 pin... but basically... find a pin that does and use it (ie. PC_4)

Anyway... all worked apart from 1 small hiccup that took ages to track down. As fate would have it... 74 pins was defined as the maximum however I used PE_13, PE_11 which meant ... (4*16+13) was > the number of pins defined. Ports A-E defines 80 Port/pin combinations however only 74 pins was defined... seems this is because not all Port/Pin combinations are broken out. Since my program checked the passed in value being < max pins it did nothing and ... well OLED was not too happy.

Anyway... it's all working happy again. Long term... the enum PinName probably could be turned into constants to uint32_t can be used as the pin parameter.

So... as a proof of concept... more than doable and as a bonus... a lot of code disappears which in turn makes things a lot easier to maintain. :shock:

mrburnette
Posts: 93
Joined: Fri Mar 29, 2019 2:22 am
Answers: 1
OS: Linux
IDE: Arduino
Core: Any
Board: Blue, MM, Black

Re: pinName vs pinNumber

Post by mrburnette » Fri Mar 29, 2019 2:32 am

hoek67 wrote:
Fri Mar 29, 2019 12:07 am
...
So... as a proof of concept... more than doable and as a bonus... a lot of code disappears which in turn makes things a lot easier to maintain. :shock:
I suspect you broke "arduino'ish" in that you will be unable to loop through the analog ports...

Code: Select all

int aValue ;
int settlingmS = 5000;

void setup() {
    // Configure the ADC pins
    for (int x =3; x < 12; x++) {
      pinMode(x, INPUT_ANALOG);
    }
  delay(settlingmS);
}

void loop() {
  for ( int analogInPin = 3; analogInPin < 12; analogInPin++)
  {
    // read the analog in value:
    aValue = analogRead(analogInPin);
    // print the results to the serial monitor:
    Serial.print("Analog pin#");
    Serial.print(analogInPin);
    Serial.print("\t = ");
    Serial.println(aValue);
  }
  delay(settlingmS);
}


/*  Sample console output:

Analog pin#3	 = 2009
Analog pin#4	 = 2061
Analog pin#5	 = 2077
Analog pin#6	 = 2536
Analog pin#7	 = 2678
Analog pin#8	 = 2681
Analog pin#9	 = 2697
Analog pin#10	= 2135
Analog pin#11	= 1984
I remember Frederic testing to ensure that the Official core performed like real Arduinos. Of course, the layers do slow down access times and adds complexity to the core code. Price for compatibility.

Ray

User avatar
hoek67
Posts: 59
Joined: Thu Mar 28, 2019 1:11 am
Location: Mount Beauty, Victoria, Australia
OS: Windows 10, Linux
IDE: STM32duino via Visual Micro in VS 2017
Core: STM official (I think)
Board: STM32F407VET6
Contact:

Re: pinName vs pinNumber

Post by hoek67 » Fri Mar 29, 2019 2:47 am

More than likely this got napalmed so to speak.

How often would people be looping all analog pins?

I usually just have an array as such...

Code: Select all


uint32_t aPins[] = { PC_1, PC_4, PC_13, PE_12 }; // even if these were A0, A1 etc... would have to still look on my board to see what this relates to

void fx(void)
{
	for (int i = 0; i < sizeof(aPins); i++)
	{
		checkPin(aPins[i]);
	}
}

mrburnette
Posts: 93
Joined: Fri Mar 29, 2019 2:22 am
Answers: 1
OS: Linux
IDE: Arduino
Core: Any
Board: Blue, MM, Black

Re: pinName vs pinNumber

Post by mrburnette » Fri Mar 29, 2019 2:59 am

hoek67 wrote:
Fri Mar 29, 2019 2:47 am
More than likely this got napalmed so to speak.

How often would people be looping all analog pins?
...
Academically speaking, "compatibility" is a binary function: true, false. I'm just stating there was a reason Frederic and his STM team built the pin maps as they did.

Ray

User avatar
BennehBoy
Posts: 67
Joined: Tue Mar 05, 2019 7:43 pm
Location: Yorkshire
OS: Windows 10
IDE: 1.8.9, Sloeber
Core: Roger's & STM
Board: Blue/Blackpill, MM, HYTiny, Black407Z/VET6, DiyMroe, FK407M1
Contact:

Re: pinName vs pinNumber

Post by BennehBoy » Fri Mar 29, 2019 7:40 am

Many of the core arduino examples loop through the pins... The CI rig for STM's core test compiles the core examples...
-Ben

Post Reply