Serial communication with host computer

Post here first, or if you can't find a relevant section!
DrMN
Posts: 10
Joined: Sun Dec 04, 2022 3:37 pm

Serial communication with host computer

Post by DrMN »

Hi, I am trying to use the serial port to communicate with the host computer from an STM32L432. Nothing appears in the arduino serial monitor.

Following is the test program. What am I missing?

I should add that I have been using Teensy boards for a few years. I am hoping to migrate to the Nucleo family.

Thank you

Code: Select all

/*
  Comms - test communications with host
 */
#include "Arduino.h"
#include <ctype.h>

char rcvbuffer[256];
char sndbuffer[256];
uint16_t nrcvbuf = 0;

int led = 13;

void setup() {
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);

  Serial.begin(9600);

  Serial.println("I'm awake!\n");

}

void loop() {

  uint16_t n;
  char *pc;
  char c;

  n = 0;
  
  digitalWrite(led, HIGH);   // turn the LED on
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off
  delay(1000);               // wait for a second

  Serial.println( (char *) "Top of loop\n" );

  // Read from the serial interface until we find a control character
  while ( Serial.available() ) {

    c = Serial.read();

    if ( !c || iscntrl( c ) ) {
      rcvbuffer[nrcvbuf] = 0;
      n = nrcvbuf;
      nrcvbuf = 0;
      break;
    }

    else if (n < sizeof(rcvbuffer)-1) {
      rcvbuffer[nrcvbuf++] = c;
    }
    else {
      Serial.println( (char *)"Error: buffer overflow" );
      nrcvbuf = 0;
    }
  }

  // for now, just echo the received buffer
  if ( nrcvbuf > 0 ) {
    rcvbuffer[nrcvbuf] = '\n';
    Serial.println( (char *) rcvbuffer );
  }
  
}
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: Serial communication with host computer

Post by GonzoG »

It depends on what board you use.
For example, on Nucleo32 boards have USB socket connected to ST-Link, not MCUs USB, and USB CDC won't work without some additional wiring.
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Serial communication with host computer

Post by fpiSTM »

Which board you select ?
DrMN
Posts: 10
Joined: Sun Dec 04, 2022 3:37 pm

Re: Serial communication with host computer

Post by DrMN »

I am using the STM32L432KC

At first, I had selected the generic stm32l432 board (I didn't find the board entry under Nucleo-32).

Then, I selected Nucleo-32 and found "L432KC" in the submenu (I didn't realize that was the stm32l432 the first time)

At that point the serial port was working - but not for long.

Then, I turned the mass storage off, using the STLinkUpgrade.jar, and changed the flash method to serial or swd, I forget which.

At that point the serial port stopped working.

Then, I turned mass storage back on again, flashed to mass storage, and the serial port is still not working. Then, I tried changing the other flash methods and mass storage off or on, all in different combinations, and all to no avail.

The serial port at this point is not working.

I have to say, it is a bit frustrating. I have spent way too much time on it.
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: Serial communication with host computer

Post by GonzoG »

It couldn't work with USB CDC as Serial. On Nucleo32 boards USB pins are not connected to USB socket. You can check it yourself. USB is on PA11 (D10) and PA12 (D2) pins.
If you set USB support to "CDC (no generic Serial)" or any other non CDC, and set Nucleo32 L432KC board, it would work, because then Serial refers to UART on pins PA2 and PA15 which are connected to ST-Link, and ST-Link would re-send it to PC.

It's all in the Nucleo32 manual:
https://www.st.com/resource/en/user_man ... ronics.pdf
DrMN
Posts: 10
Joined: Sun Dec 04, 2022 3:37 pm

Re: Serial communication with host computer

Post by DrMN »

@GonzoG Thank you. Can you point me to a page? So, far on quick perusal I am not finding in that manual or anything about how this works.

Do I understand correctly that for a deployed product derived from the nucleo, I need to install a USB connector to PA11 and PA12?

Can I guarantee that USB is connected to those pins in my code?

Alternatively, if I set CDC no generic, or otherwise force UART to pins PA2 and PA15, so that st-link relays the serial traffic to the pc, does that effect throughput?

Thank you again
DrMN
Posts: 10
Joined: Sun Dec 04, 2022 3:37 pm

Re: Serial communication with host computer

Post by DrMN »

@GonzoG P/S So it seems the settings that work are as follows. So, USB refers to what happens on pins PA11/PA12? I think I still don't quite understand what U(S)ART is about or why selecting serial for the upload fails.

Board: Nucleo-32

Board Part Number: Nucleo-32 L432KC

U(S)ART support: Enabled(generic 'Serial')

USB support: CDC(no generic 'Serial')

Upload method: STM32CubeProgrammer (SWD)
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Serial communication with host computer

Post by ag123 »

On Nucleo boards, normally Serial goes to one of the uart ports and it goes to the st-link on board. The st-link bridges that and gives you a com (VCP) on your pc.

So USB isn't there on Nucleo boards, only the pins are left on the headers.
if you really insist on doing usb you probably need to do some wiring e.g. getting 'modules' like such
https://www.aliexpress.com/item/32827910858.html
and connecting the correct usb pins (PA11/PA12) (check and use the correct pin assignments for D+/D- it matters) and +5v, gnd pins appropriately.
Then you can build with USB (CDC) Serial, that will replace the Serial name. And you need to access your uarts from different names Serial1, Serial2 ... etc.

and remember u(s)art are uarts, they have *nothing* to do with usb. you can write skeches like such
viewtopic.php?p=11241#p11241
to bridge usb to uarts
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Serial communication with host computer

Post by fpiSTM »

You don't need to enable USB CDC. Only Serial is used to forward to the STlink VCP as stated by @GonzoG

You mention you used the STLinkUpgrade.jar? I advise you use the STM32CubeProgrammer which have the latest STLink firmware.
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: Serial communication with host computer

Post by GonzoG »

DrMN wrote: Tue Dec 06, 2022 2:00 am @GonzoG P/S So it seems the settings that work are as follows. So, USB refers to what happens on pins PA11/PA12? I think I still don't quite understand what U(S)ART is about or why selecting serial for the upload fails.

Board: Nucleo-32

Board Part Number: Nucleo-32 L432KC

U(S)ART support: Enabled(generic 'Serial')

USB support: CDC(no generic 'Serial')

Upload method: STM32CubeProgrammer (SWD)
To use Serial for upload, you need USB-TTL adapter (CH340, FTDI, etc) or a computer with serial port working on 3.3V - 5V voltage (RS232 is a +/- 12V, so it cannot be used directly) and connect it to PA9 and PA10 pins, and you need to manually reset MCU into bootloader before flashing firmware.

You have Nucleo32 board and if you use USB port on board, SWD (ST-Link) is the only way to flash firmware.

UART is the basic serial interface on most MCUs. U(S)ART is an extension of UART.
UART is an Asynchronous so it does not use clock signal. Just 2 lines: RX, TX.
U(S)ART can be Synchronous (with additional clock signal like IIC or SPI)

And as fpiSTM mentioned you don't need to enable USB. I would say, you even shouldn't, unless you add USB socket to PA11/PA12 pins and wire it properly (USB should use a 1.5k pull-up resistor on D+ for full speed, or pull-down on D- for low speed setup).
Post Reply

Return to “General discussion”