Remapping Hardware serial pins

Post here first, or if you can't find a relevant section!
Post Reply
User avatar
Alextrical
Posts: 39
Joined: Tue Aug 02, 2022 12:59 pm
Location: Cornwall, UK

Remapping Hardware serial pins

Post by Alextrical »

Hi
I'm currently trying to map the UART2 Hardware serial pins on a STM32G0B1CBT to PA15 and PA14, however it doesn't seem to be taking effect.

On a different board, the BlackPill / STM32F401CC the code below works to change the hardware pins of the serial port

Code: Select all

//                      RX    TX
//HardwareSerial Serial2(PA10, PA9);
HardwareSerial Serial2(PB7, PB6);

void setup() {
  Serial2.begin(9600); 
}

void loop() {
  Serial2.println("HB");
  delay(10);
}
However using the code adapted for the STM32G0B1CBT6 it does not, nor does it show any compiling issues.

Code: Select all

//                      RX    TX
//HardwareSerial Serial2(PD6, PD5); //Pins not available on STM32G0B1CBT6
HardwareSerial Serial2(PA15, PA14);
//HardwareSerial Serial2(PA3, PA2);


void setup() {
  Serial2.begin(9600); 
}

void loop() {
  Serial2.println("HB");
  delay(10);
}
Looking at the data sheet USART 2 can be mapped to the following pins
USART2_RX PD6, PA15, PA3
USART2_TX PD5, PA14, PA2
With PD5 & PD6 being the default for USART2

Any help that can be offered is greatly appreciated
by ag123 » Mon Aug 22, 2022 1:34 pm
there are some additional pin mapping codes in the variant e.g.
https://github.com/stm32duino/Arduino_C ... ins.c#L175
you may like to take a look at those definitions and perhaps review HardwareSerial codes?
but it do look like PA14 TX, PA15 RX are valid pins
there is a catch though

Code: Select all

WEAK const PinMap PinMap_UART_TX[] = {
...
{PA_14,      LPUART2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF10_LPUART2)},
{PA_14_ALT1, USART2,  STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_USART2)},
...
}

WEAK const PinMap PinMap_UART_RX[] = {
...
{PA_13,     LPUART2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF10_LPUART2)},
{PA_15,     USART2,  STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_USART2)},
...
}
PA14 has 2 alt function pin mappings, it may take more study there
Go to full post
ag123
Posts: 1656
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Remapping Hardware serial pins

Post by ag123 »

there are some additional pin mapping codes in the variant e.g.
https://github.com/stm32duino/Arduino_C ... ins.c#L175
you may like to take a look at those definitions and perhaps review HardwareSerial codes?
but it do look like PA14 TX, PA15 RX are valid pins
there is a catch though

Code: Select all

WEAK const PinMap PinMap_UART_TX[] = {
...
{PA_14,      LPUART2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF10_LPUART2)},
{PA_14_ALT1, USART2,  STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_USART2)},
...
}

WEAK const PinMap PinMap_UART_RX[] = {
...
{PA_13,     LPUART2, STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF10_LPUART2)},
{PA_15,     USART2,  STM_PIN_DATA(STM_MODE_AF_PP, GPIO_PULLUP, GPIO_AF1_USART2)},
...
}
PA14 has 2 alt function pin mappings, it may take more study there
User avatar
Alextrical
Posts: 39
Joined: Tue Aug 02, 2022 12:59 pm
Location: Cornwall, UK

Re: Remapping Hardware serial pins

Post by Alextrical »

As you have pointed out USART2 and LPUART2 share the same TX Pin, but use different RX Pins, according to the data sheet.
Using the following code it now works

Code: Select all

//#define HAL_UART_MODULE_ENABLED
//                      RX    TX
HardwareSerial Serial2(USART2);


void setup() {
  Serial2.setRx(PA_15); // using USART pin name PY_n
  Serial2.setTx(PA_14_ALT1); // using USART pin name PY_n
  Serial2.begin(9600);
  pinMode(PA13, OUTPUT);
}

void loop() {
  digitalWrite(PA13, HIGH);   // turn the LED on (HIGH is the voltage level)
  Serial2.println("HB");
  delay(250);                       // wait for a 1/4 second
  digitalWrite(PA13, LOW);    // turn the LED off by making the voltage LOW
  Serial2.println("HB");
  delay(250);                       // wait for a 1/4 second
}
Thank you so much, you are a godsend, this project has been driving me somewhat up the walls for the last few weeks, the next step is getting the register to work as defined in 33.8.3: Bit 15 on page 1057 of the reference manual
User avatar
fpiSTM
Posts: 1745
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Remapping Hardware serial pins

Post by fpiSTM »

In fact it is an Alternative pins not linked to a Alternate function. You have to use same suffix, like this:

Code: Select all

HardwareSerial Serial2(PA15, PA14_ALT1);
User avatar
Alextrical
Posts: 39
Joined: Tue Aug 02, 2022 12:59 pm
Location: Cornwall, UK

Re: Remapping Hardware serial pins

Post by Alextrical »

ooh, that looks like a much more elegant approach, i will give that a try now
Thank you
User avatar
fpiSTM
Posts: 1745
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Remapping Hardware serial pins

Post by fpiSTM »

Alextrical wrote: Mon Aug 22, 2022 2:18 pm ooh, that looks like a much more elegant approach, i will give that a try now
Thank you
Yes, beacause giving the USART instance to use take the first pins found, in your case it is the ones you want but it could not ;)
Post Reply

Return to “General discussion”