genericSTM32F405RG serial not working?

Hi,

Im having an problems with serial on the F405. Below is the ini and script. The bluepill works as expected, and changing to the F405 results in the led blinking but no serial.

Serial upload works as expected with uart1 (PA9 and PA10) on both targets

Is there something Im missing? or an actual issue?

Cheers

[env:dev]
platform = ststm32@14.0.1
framework = arduino
; board = genericSTM32F103C8
board = genericSTM32F405RG
upload_protocol = serial
monitor_speed = 9600
#include <Arduino.h>

// #define LEDPIN PC13 // genericSTM32F103C8
#define LEDPIN PA8 // genericSTM32F405RG

void setup() {
  Serial.setTx(PA9);
  Serial.setRx(PA10);
  Serial.begin(9600); 
  pinMode(LEDPIN, OUTPUT);
}
void loop() {
  Serial.println("Hello World!");  
  digitalWrite(LEDPIN, HIGH);
  delay(250);
  digitalWrite(LEDPIN, LOW);
  delay(250);
}

For your chosen board, the Serial object maps to UART4.

Thus the Serial object is hardcoded to what UART4 can do, and the PA9/PA10 pins are on USART1, thus calling a function to set the pins of that serial to the one of USART1 is invalid.

You can also see why it works for the Bluepill:

the serial is already setup for USARt1 and PA9/10.

What you need to do for the F4 board is redirect the serial instance and pins, that is, use the platformio.ini

[env:dev]
platform = ststm32@14.0.1
framework = arduino
; board = genericSTM32F103C8
board = genericSTM32F405RG
upload_protocol = serial
monitor_speed = 9600
build_flags = 
   -DSERIAL_UART_INSTANCE=1
   -DPIN_SERIAL_RX=PA10
   -DPIN_SERIAL_TX=PA9

or create a new instance of the Serial object with the appropriate constructor parameters, as the docs say.

Thanks for the detailed comment, much appreciated!

I tried the below build_flags, but still not serial output.

[env:dev]
platform = ststm32@14.0.1
framework = arduino
board = genericSTM32F405RG
upload_protocol = serial
monitor_speed = 9600
build_flags = 
   -DSERIAL_UART_INSTANCE=1
   -DPIN_SERIAL_RX=PA10
   -DPIN_SERIAL_TX=PA9

Then tried the below with the OG ini, and still nothing.

#include <Arduino.h>

#define LEDPIN PA8 // genericSTM32F405RG

HardwareSerial Serial1(PA10, PA9);

void setup() {
  Serial1.begin(9600); 
  pinMode(LEDPIN, OUTPUT);
}

void loop() {
  Serial1.println("Hello World!");  
  digitalWrite(LEDPIN, HIGH);
  delay(250);
  digitalWrite(LEDPIN, LOW);
  delay(250);
}

So Im at a loss. No idea what is going on :frowning:

A little more testing and I think I have it working :slight_smile:

Im not 100% sure what I was doing wrong before but its alive. Thanks, your comment above helped.

Did you change something in the code or platformio.ini? Can you upload the fully working one?

The below works :+1:

.ini

[env:dev]
    platform = ststm32@14.0.1
    framework = arduino
    board = genericSTM32F405RG
    upload_protocol = serial
    monitor_speed = 9600
    build_flags = 
       -DSERIAL_UART_INSTANCE=1
       -DPIN_SERIAL_RX=PA10
       -DPIN_SERIAL_TX=PA9
       -D HSE_VALUE=8000000U

.cpp

#include <Arduino.h>

#define LEDPIN PA8 // genericSTM32F405RG

// #define GPIO_PIN_RCSIGNAL_TX_1 PA9
// #define GPIO_PIN_RCSIGNAL_RX_1 PA10

#define GPIO_PIN_RCSIGNAL_TX_3 PB10
#define GPIO_PIN_RCSIGNAL_RX_3 PB11

#define GPIO_PIN_RCSIGNAL_TX_6 PC6
#define GPIO_PIN_RCSIGNAL_RX_6 PC7

HardwareSerial SerialPort(1);

// HardwareSerial Port1 = SerialPort;
HardwareSerial Port3 = SerialPort;
HardwareSerial Port6 = SerialPort;

void setup() {
  pinMode(LEDPIN, OUTPUT);

  Serial.begin(9600); 

    // Port1.setTx(GPIO_PIN_RCSIGNAL_TX_1);
    // Port1.setRx(GPIO_PIN_RCSIGNAL_RX_1);
    // Port1.begin(9600);  

    Port3.setTx(GPIO_PIN_RCSIGNAL_TX_3);
    Port3.setRx(GPIO_PIN_RCSIGNAL_RX_3);
    Port3.begin(9600);
 

    Port6.setTx(GPIO_PIN_RCSIGNAL_TX_6);
    Port6.setRx(GPIO_PIN_RCSIGNAL_RX_6);
    Port6.begin(9600);
}

void loop() {
  Serial.println("UART1");  
  // Port1.println("Port 1");  
  Port3.println("Port 3");  
  Port6.println("Port 6");  

  digitalWrite(LEDPIN, HIGH);
  delay(250);
  digitalWrite(LEDPIN, LOW);
  delay(250);
}