STM32F4 as USB to serial converter

Post Reply
mkals
Posts: 4
Joined: Thu Jan 13, 2022 11:55 am

STM32F4 as USB to serial converter

Post by mkals »

Hello,

I'm trying to use an STM32F411CEU as a USB to serial converter. The goal is to be able to detect when another STM32 is connected and automatically initiate flashing of that chip. For now though, I'm only using it as a simple USB to serial converter.

My problem is that I am not able to get ST tools (STM32CubeProgrammer or stm32flash) to connect to the STM32 to be flashed through this STM32F4-based converter on Windows or Linux platforms. On Mac, everything works as expected, and I am able to use this as a programmer without a problem. I am also able to serial write and read through the STM32F4 on Windows and Linux.

When debugging the problem, I see that no reply is registered for the initial 0x7f message that the ST tools send. However, the 0x7f message is received by the STM32 to be flashed, and the reply 0x79 is written to SerialUSB by the STM32F4 converter.

This is the sketch I am running on the STM32F4 USB to serial converter:

Code: Select all


/* Flash settings:
 * - Board: Generic STM32F4 series
 * - Board part number: Generic F411CEUx
 * - USART support: Enabled, no generic Serial
 * - USB support: CDC, no generic Serial
 * - USB speed: Full speed
 * - Upload method: STM32CubeProgrammer (DFU)
 */

#define PIN_SERIAL_RX PA10
#define PIN_SERIAL_TX PA9

HardwareSerial Serial(PIN_SERIAL_RX, PIN_SERIAL_TX);

#define NRST PA2
#define BOOT0 PA3

#define LED PA4

void resetBoardForProgramming() {
  
  // Place board in boot mode
  digitalWrite(NRST, HIGH);
  pinMode(NRST, OUTPUT);
  
  digitalWrite(BOOT0, HIGH); // set BOOT0 to enter programming mode
  delay(100);
  
  digitalWrite(NRST, LOW); // push RESET button
  delay(100);
  
  digitalWrite(NRST, HIGH); // release RESET button
  delay(100);

  digitalWrite(BOOT0, LOW); // will cause reset at end of programming cycle causes program start
  //pinMode(NRST, INPUT);
}



void setup() {
  Serial.begin(115200, SERIAL_8E1); // 8 bit data length, even parity, 1 stop bit
  SerialUSB.begin();

  Serial.setTimeout(500); // ms

  pinMode(LED, OUTPUT);
  digitalWrite(LED, HIGH);

  pinMode(NRST, INPUT_PULLDOWN);
  pinMode(BOOT0, OUTPUT);
  
  digitalWrite(BOOT0, LOW);
  digitalWrite(NRST, HIGH);

  resetBoardForProgramming();
}

void loop() {
  
  // Blink LED with 1Hz frequency
  digitalWrite(LED, int(millis() / 500) % 2);

  // --- Serial passthrough --- //

  if (Serial.available()) {      // If anything comes in Serial (USB),
    SerialUSB.write(Serial.read());   // read it and send it out Serial1 (pins 0 & 1)
  }

  if (SerialUSB.available()) {      // If anything comes in Serial (USB),
    
    Serial.write(SerialUSB.read());   // read it and send it out Serial1 (pins 0 & 1)
  }
  
  //SerialUSB.println(String(analogRead(NRST)) + " " + String(digitalRead(NRST)) + " - " + String(analogRead(BOOT0)) + " " + String(digitalRead(BOOT0)));
}
Does anyone have an idea of what is wrong, and why the 0x79 acknowledgement is not registered on the Windows or Linux operating systems? I have used packet sniffers, and it looks like the systems are not registering these messages at all.
ag123
Posts: 1388
Joined: Thu Dec 19, 2019 5:30 am
Answers: 18

Re: STM32F4 as USB to serial converter

Post by ag123 »

for Linux
you may need to setup the udev rules, there are some examples in here
https://github.com/rogerclarkmelbourne/ ... ls/linux64

and in Linux, you may need to tell your os that it isn't a modem, otherwise some process keep holding onto it and you can't connect.
https://github.com/stm32duino/wiki/wiki ... itor-fails

for windows normally the installation steps matter. I'm not sure what other things may be needed to tell windows that it is a usb-serial port and that it isn't a modem.

if you are trying to program another stm32, there are other interesting gotchas, here is an implementation but for the old libmaple core.
https://github.com/rogerclarkmelbourne/ ... -over-uart
Accordingly, the line discipline is 8E1. However, in my tests 8N1 actually works as well.
The other gotchas are that, when you reset the target device, you should expect to receive some *noise*, error bytes.
In libmaple, I added a patch that check framing and parity and discard bytes with framing and/or parity errors (this is in the libmaple core).
stm core may passthru all that data.

things like stm32loader https://github.com/jsnyder/stm32loader
'abuse' the DTR/RTS usb 'signals' to toggle boot pin / reset etc. That one is 'hairy' to do, you need to pull the usb (driver) hook to your codes and handle usb requests. The implementation done in libmaple is like this:
https://github.com/rogerclarkmelbourne/ ... -w-signals
mkals
Posts: 4
Joined: Thu Jan 13, 2022 11:55 am

Re: STM32F4 as USB to serial converter

Post by mkals »

Thanks a lot for the suggestions! I'll go through the list and give those a try.
mkals
Posts: 4
Joined: Thu Jan 13, 2022 11:55 am

Re: STM32F4 as USB to serial converter

Post by mkals »

I have done quite a bit more experimentation. No progress on Windows.

On Linux, I have found that adding udev rules does not seem to fix the issue. I have however been able to narrow the issue on Linux down to when flash is erased. Reading from the board etc. works as it should. However, when trying to erase, the stm32flash utility simply gets stuck on the line "Erase memory" - I can leave it for hours without any change. Setting the number of pages to erase does not seem to resolve the issue. I don't know how to go about debugging this!
ag123
Posts: 1388
Joined: Thu Dec 19, 2019 5:30 am
Answers: 18

Re: STM32F4 as USB to serial converter

Post by ag123 »

for stm32flash
you need st-link v2
https://www.st.com/en/development-tools/st-link-v2.html
https://www.adafruit.com/product/2548
and that connects to the pins for SWD, not serial
it helps to connect the pin for reset on the SWD dongle to the reset pin on the actual device.

And for stm32f411 black pill, you don't literally need any dongles. you need a usb cable.
And the button dance is:
- press both reset and boot0
- release reset but hold boot0
- release boot0 2 seconds later

you can use stm32-cube programmer as the programming software in all cases DFU / SWD / serial.
https://www.st.com/en/development-tools ... eprog.html

https://learn.adafruit.com/adafruit-stm ... er-details
Post Reply

Return to “USB to Serial adaptors”