convert PORT to int

aster
Mon Jun 26, 2017 9:55 pm
hello,

i am porting a library to stm that uses a bluetooth module to comunicate with an android app
i need to assign the port to a variable, something like:
int a = PB1;
i was wondering which is the correct type i should use?

by the way i tested the int and it works but it led me to another question:

using this on a maple mini and on a black pill i get differnt values from the print, why?
int a = PB12;
int b = PB1;

void setup() {
// put your setup code here, to run once:
Serial1.begin(115200);
pinMode(a, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(a, HIGH);
delay(100);
digitalWrite(a, LOW);
delay(900);
Serial1.println(a);
Serial1.println(b);
}


RogerClark
Mon Jun 26, 2017 10:08 pm
If you want to read an entire port ( 16 bits), at the moment you will need to use the built in reference to the port struct ( device) and its input reguster.

I am sure other people have posted some snippets of code. try googling for fast GPIO access or port access


aster
Mon Jun 26, 2017 10:29 pm
[RogerClark – Mon Jun 26, 2017 10:08 pm] –
If you want to read an entire port ( 16 bits)

my need is not to read an entire port. I just would like to know which variable i should use to assign a pin to a variable.
and then why in different board (with almost the same mcu) the corresponding int value for PAx is different


RogerClark
Tue Jun 27, 2017 1:20 am
Pins are just an index into a PIN_MAP array

I think you could use a single byte, but as the MCU is natively 32 bits, is usually best just to use int for integers unless you need to do some specific bit shifting or masking that requires an 8 bit value

So… use

int


stevestrong
Tue Jun 27, 2017 12:30 pm
Different port pins are part of an enum, like this:
https://github.com/rogerclarkmelbourne/ … oard.h#L76
Well, actually they should be identical, but I don’t know the logic behind why the pins are not taken in the same order for each variant…

RogerClark
Tue Jun 27, 2017 12:49 pm
[stevestrong – Tue Jun 27, 2017 12:30 pm] –
Different port pins are part of an enum, like this:
https://github.com/rogerclarkmelbourne/ … oard.h#L76
Well, actually they should be identical, but I don’t know the logic behind why the pins are not taken in the same order for each variant…

The reason the pin maps are not in the same order, is because they are all derived from the Maple boards, and those boards have strange pin mappings, as their pins are numbered, rather than using the Port letter and number, e.g PB5.

Originally the Generic F103C mapping was the same as the Maple mini, but I thought I had cleaned it up ages ago. Though I may have not cleaned all the boards, and some other boards may use pin numbers.


aster
Tue Jun 27, 2017 1:06 pm
[RogerClark – Tue Jun 27, 2017 12:49 pm] –

The reason the pin maps are not in the same order, is because they are all derived from the Maple boards, and those boards have strange pin mappings, as their pins are numbered, rather than using the Port letter and number, e.g PB5.

Originally the Generic F103C mapping was the same as the Maple mini, but I thought I had cleaned it up ages ago. Though I may have not cleaned all the boards, and some other boards may use pin numbers.

great now it makes sense, i had the same thought as stevestrong: “actually they should be identical”


dannyf
Tue Jun 27, 2017 1:12 pm
If the original code is done right it should define a whim type for the pins.

If not, you may see it discretely defined .

It isn’t a good practice to code them via a variable. I use macros instead. If you have to use a variable, use an unsigned but should work.


stevestrong
Tue Jun 27, 2017 1:48 pm
[dannyf – Tue Jun 27, 2017 1:12 pm] – It isn’t a good practice to code them via a variable. I use macros instead.

Unfortunately, most original Arduino sketches use the “int” (variable) version.
Me, I also favor macros instead, so that when I port a sketch for STM32 the first thing I do is to replace the “int” by macro.


RogerClark
Tue Jun 27, 2017 9:37 pm
AFIK

const int , would also be OK,


zoomx
Wed Jun 28, 2017 6:29 am
In Arduino forum there is a post about the differences between using const int vs #define.
The combination preprocessor/compilerr make no differences, it will be used the same memory space.
Anyway it seems that const int is better because if you make mistakes errors will arise
for example this
if(ledPin = 13)
{
}

aster
Fri Jul 07, 2017 7:48 am
Interesting thank you zoomx! I always use the macros but now i will use the constant in same situation :)

zmemw16
Fri Jul 07, 2017 11:00 am
if(ledPin = 13)
{
}

aster
Fri Jul 07, 2017 12:06 pm
It was an example of a possible mistake ;)

zmemw16
Fri Jul 07, 2017 12:35 pm
i’m glad i got that right then :lol:
oh that tickled
stephen

aster
Wed Jul 12, 2017 2:08 pm
i was wondering is it possible to do the reverse: convert a pin (for example the maple mini built in led = 33) to the PORT (i.e. PB1)

i had a look at roger’s core but i didn’t find a way to do it, this is the only information i could find similar to what i am looking

typedef struct stm32_pin_info {
gpio_dev *gpio_device; /**< Maple pin's GPIO device */
timer_dev *timer_device; /**< Pin's timer device, if any. */
adc_dev *adc_device; /**< ADC device, if any. */
uint8 gpio_bit; /**< Pin's GPIO port bit. */
uint8 timer_channel; /**< Timer channel, or 0 if none. */
uint8 adc_channel; /**< Pin ADC channel, or ADCx if none. */
uint8 pinMode; /**< mode specific by pinMode call (Roger Clark added to optimize compatibility with Arduino API*/
} stm32_pin_info;


aster
Thu Jul 13, 2017 9:04 pm
I didn’t managed to extract from the structure the port but this is better than nothings:
#include "Streaming.h"
uint8_t pin, port, n;

void setup() {

Serial.begin(115200);
while (!Serial);
delay(10);

Serial << "Analog Pins: " << BOARD_NR_ADC_PINS << endl;
for (n = 0; n < BOARD_NR_ADC_PINS ; n++) {

pin = boardADCPins[n];
if (pin < 16) {
Serial.print("PA");
} else if (pin >= 16 && pin < 32) {
Serial.print("PB");
pin -= 16;
} else if (pin >= 32 && pin < 48) {
Serial.print("PC");
pin -= 32;
}
Serial.println(pin);
}
Serial.println();

Serial << "PWM Pins: " << BOARD_NR_PWM_PINS << endl;
for (n = 0; n < BOARD_NR_PWM_PINS ; n++) {

pin = boardPWMPins[n];
if (pin < 16) {
Serial.print("PA");
} else if (pin >= 16 && pin < 32) {
Serial.print("PB");
pin -= 16;
} else if (pin >= 32 && pin < 48) {
Serial.print("PC");
pin -= 32;
}
Serial.println(pin);
}
Serial.println();

Serial << "Board Pins: " << BOARD_NR_USED_PINS << endl;
for (n = 0; n < BOARD_NR_USED_PINS ; n++) {
pin = boardUsedPins[n];
if (pin < 16) {
Serial.print("PA");
} else if (pin >= 16 && pin < 32) {
Serial.print("PB");
pin -= 16;
} else if (pin >= 32 && pin < 48) {
Serial.print("PC");
pin -= 32;
}
Serial.println(pin);
}
Serial.println();

Serial << "All GPIO Pins: " << BOARD_NR_GPIO_PINS << endl;
for (n = 0; n < BOARD_NR_GPIO_PINS ; n++) {

pin = PIN_MAP[n].gpio_bit;
//port = PIN_MAP[n].gpio_device -> regs; //conversion?

//workaround
port = n;
if (port < 16) {
Serial.print("PA");
} else if (port >= 16 && port < 32) {
Serial.print("PB");
} else if (port >= 32 && port < 48) {
Serial.print("PC");
}
Serial.println(pin);

//more info
Serial << "Mode: " << PIN_MAP[n].pinMode << endl;
Serial << "ADC: " << PIN_MAP[n].adc_channel << endl;
Serial << "Timer: " << PIN_MAP[n].timer_channel << endl << endl;
//if adc_channes is 255 (0xFF) the pin is not analog

}
}

void loop() {}


Leave a Reply

Your email address will not be published. Required fields are marked *