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);
}
I am sure other people have posted some snippets of code. try googling for fast GPIO access or port access
[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
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
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…
[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.
[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”
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.
[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.
const int , would also be OK,
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)
{
}if(ledPin = 13)
{
}oh that tickled
stephen
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;
#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() {}



