7 years, 6 months ago.

Simple UART test is not working on NUCLEO_F401RE with serial console minicom

Hello Everyone,

I am trying to do a simple UART interface test on my NUCLEO_F401RE board (https://developer.mbed.org/platforms/ST-Nucleo-F401RE/), just sending a character to the UART interface and receiving it as the following:

#include "mbed.h"
#include <stdio.h>


int main(){

    char test;
    Serial uart(PA_9, PA_10);
    DigitalOut led_green(PB_5);

     test = 'a';
        uart.putc(test);
        printf("Sending to UART %c\n\r",test);

wait(5);

    if(uart.readable()) {
        printf("Value received is %c\n\r", uart.getc());
  led_green = !led_green;

    }
}

I am using Ubuntu 16.04 LTS/ 64 bit and minicom serial console with default baudrate 9600 and mbed OS.5 the only result I am getting is as the following:

Sending to UART a

/media/uploads/Mias/bildschirmfoto_vom_2016-10-13_14-29-14.png

anyone has any idea what I am missing so I am not able to receive the character??? and any other suggestion to test the serial UART interface???

I will be grateful for any contribution, Maria

1 Answer

7 years, 6 months ago.

Please use the <<code>> and <</code>> tags around posted code to keep it readable.

include "mbed.h"
include <stdio.h>

int main(){

char test;
Serial uart(PA_9, PA_10);
DigitalOut led_green(PB_5);

test = 'a'; uart.putc(test); printf("Sending to UART %c\n\r",test);

wait(5);

if (uart.readable()) { printf("Value received is %c\n\r", uart.getc()); led_green = !led_green;}

}

Here are some hints to improve the code:

include "mbed.h"

char test;
Serial pc(SERIAL_TX, SERIAL_RX);
Serial uart(PA_9, PA_10);
DigitalOut led_green(PB_5);

int main(){

  test = 'a';
  uart.putc(test);
  pc.printf("Sending to UART %c\n\r",test); 

  wait(5);

  while (1) {
    if (uart.readable()) {
      pc.printf("Value received is %c\n\r", uart.getc());
      led_green = !led_green;
    }
    else {
      wait(0.25);
    }
  }
}

The original code will probably reach its end and finish execution before the stdout serial port interface has opportunity to print anything. So add an endless loop to make sure the received characters are processed. You may also want to explicitly add the 'pc' serial port rather than just the implicit stdout.

Are you sure that uart actually receives/transmits any data. Maybe you mixed up TX and RX or GND is missing.

Accepted Answer

Dear Wim,

thanks for the suggestion, I have just tried the improved code that you have suggested but unfortunately I still get the same result :(

#include "mbed.h"
char test;
Serial pc(SERIAL_TX, SERIAL_RX);
Serial uart(PA_9, PA_10);
DigitalOut led_green(PB_5);
 
int main(){
 
	pc.baud(9600);
        uart.baud(9600);

  test = 'a';
  uart.putc(test);
  pc.printf("Sending to UART %c\n\r",test); 
 
  wait(5);
 
  while (1) {
    if (uart.readable()) {
      pc.printf("Value received is %c\n\r", uart.getc());
      led_green = !led_green;
    }
    else {
      wait(0.25);
    }
  }
}

also I rechecked the Tx/Rx pins, and they are correct. In my board NUCLEO_F401RE I don`t need GND for the serial UART interface.

/media/uploads/Mias/bildschirmfoto_vom_2016-10-17_09-29-52.png

I don`t know , what I`m missing, the test is so simple and it should work! I`m so frustrated Could it be something wrong in the connection between the serial console minicom and the UART interface, what should I take care of in addition to compatible baud rate?

posted by Maria Sadek 17 Oct 2016

I assumed that the uart (PA_9, PA_10) pins were connected to some external device (eg another nucleo) in which case you need a common GND and you should make sure that data is actually being sent and received by that external device.

Another possible case could be that uart is hooked up to a Serial-to-USB converter which is in turn connected to the same PC that you also use for the programming/serial debug port of the nucluo. You would need two minicom consoles in this case: one for pc.printf channel and one connected to the uart channel. Characters you type in the minicom for the uart channel should show up in the serial debug minicom window.

The code should also work in case PA_9 and PA_10 are shorted and all data is simply looped back. However, with the code above you would receive only the single character 'a' that was first sent out on the uart. You can modify the code to continuously read characters typed in from the debug console, sent it out to the uart, receive that data back through the shorted PA_9 and PA_10, and then display it again on the debug console.

Please describe or show exactly how the nucleo, the uart pins and the PC are connected.

posted by Wim Huiskamp 17 Oct 2016

Dear Wim, thanks for your help :) I closed the loop between PA_9 and PA_10 and the small serial UART test worked and other examples as you have suggested worked as well. now I can move to check other interfaces with inputs from sensors thanks a lot Maria

posted by Maria Sadek 20 Oct 2016