Page 1 of 1

Remapping Hardware serial pins

Posted: Mon Aug 22, 2022 1:07 pm
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

Re: Remapping Hardware serial pins

Posted: Mon Aug 22, 2022 1:34 pm
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

Re: Remapping Hardware serial pins

Posted: Mon Aug 22, 2022 2:17 pm
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

Re: Remapping Hardware serial pins

Posted: Mon Aug 22, 2022 2:17 pm
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);

Re: Remapping Hardware serial pins

Posted: Mon Aug 22, 2022 2:18 pm
by Alextrical
ooh, that looks like a much more elegant approach, i will give that a try now
Thank you

Re: Remapping Hardware serial pins

Posted: Mon Aug 22, 2022 3:45 pm
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 ;)