USB to PC communications not working

I’ve been trying to get communications between PC and STMf406VG Discovery board. For a program I have the following but get no indication anywhere of communications. Does anyone have ideas about what I could be doing wrong? I’ve tried serial monitor in mbed studio and tera term.

#include “mbed.h”

Serial pc(USBTX, USBRX); // tx, rx

int main() {
while(1){
pc.printf(“Hello World!\n”);
}
}

Hi,

  • First use these symbols ``` before and after a code section for better formating of a code what you want to show to others
  • Place here a link to your board I can not found it.
  • A continuous loop of the printf without a delay is not a good implementation.
  • Add a led for info about your code is alive.
  • If adding a delay will not help, try the printf without pc object for default console (STDIO).
  • And also you can where are the macros USBTX and USBRX conected
#include “mbed.h”
DigitalOut led(LED1);
Serial pc(USBTX, USBRX); // tx, rx

int main() {
    while(1){
    pc.printf(“Hello World!\n”);
    //printf(“Hello World!\n”); //default STDIO
    led = !led;
    ThisThread::sleep_for(1000); // you can use this or  this `thread_sleep_for()` because wait() is obsoalte or thread_sleep_for();
    }
}

BR, Jan

Jan,
Thank you for your suggestions. I’ve made the edits and the LED works, but I still don’t have any output from the board to the PC. I’ve looked for the output on the serial monitor and tera term. Please let me know where to look for output besides those two places.

Thank you.

Dan

Does the baud rate from the microcontroller match that of the software you’re using on your PC? I assume your code defaults to 9600 - definitely worth being explicit so that you can rule that out. Do the USBTX/RX pins actually connect to the USB connector, or are they just badly named? Maybe the board breaks out other UART pins that are referenced by USBTX/RX?

Hi Adam,

Thank you for your suggestions. The baud rate is set to 9600 on tera term and the usb port is also set to 9600 according to the serial monitor.

As for the USB pin connections; programs load into the board ok. Doesn’t that mean the USB pin connections are correct? However, it may be that the board uses other UART pins that are referenced by USBTX/RX. How would I go about finding out if that’s the case?

Thank you.

Dan

Hey Dan,

Yes, the USB data +/- connections must be working. What I meant is that, although the pins are referenced as USBTX \ USBRX in software, they may not actually be the pins that are connected to the USB UART. You’ll have to check the target’s PinMap file in the targets directory of Mbed and then reference that against the schematic.

This is a long shot but I have experienced issues with incorrect pin mappings in the past. You should also ensure that you’re selecting the right COM port (assuming you’re on Windows) in Tera Term.

Adam

Dan, that is what I wrote in your previous question and why I ask you about a link what board you have, because I can not find here STMf406VG Discovery

BR, Jan

Jan,

The board is STMF407VG, not F406.

Thank you for your help, BTW!

Dan

Ok, that is better :slight_smile:

  • See here for pins configuration.
  • See here for masks of pins

However I probably have the magic, please see here and check the instructions in the big yellow box.

BR, Jan

1 Like

2020-04-02T18:42:00Z
I have similar issues with mbedStudio downloaded 2 days ago. The serial monitor just does not work, but coolterm does. yes the compiling and uploading functions.
Simple things like “printf” is not recognised. also it wont permit pc.printf either
also “stdio.h” is not prefered, suggests i should use “cstdio.h” but the latter file cannot be found.
Has there been a major change recently as many examples just don’t work.
also “unbufferedserial” not recognised.

Hi George,

Unfortunately, I couldn’t get it to work so I gave up. Please post here if you figure it out.

Thanks.

Dan

Dan, if my previous answer was unclear for you, sorry but do not have fear to ask again.

You can’t have output from the serial via the ST-link like on another ST boards, because MCU’s UART is not connected to ST-link’s UART. You need to use a USB to Serial converter, for example based on the FTDI chip and connect it to PA2 and PA3(crossed ofc). Or if you have some another board or an arduino you can achieve that also with it.

BR, Jan

Putting everything but #incudes inside the int main() magically everything works, even the serial monitor on mbedStudio!!!
been using arduino too long!

#include “mbed.h”
#include “platform/mbed_thread.h”

int main()
{
Serial pc(USBTX, USBRX); // tx, rx
Serial device(PTC17,PTC16 ,9600); // tx, rx
printf(“M_XbeeRelayV1 \n”);

// Initialise the digital pin LED2 as an output

DigitalOut led(LED2);

while(1) {
                 // do stuff here
                    }

}

George,

Thanks a lot for the info. I tried it out but still didn’t get a good result. Would you please post the entire main.c so that I can find out what I’m doing wrong?

Thank you.

Dan

//This is the whole code, now including various printf at the start.
// very useful when you revisit it weeks later and can’t find the correct code etc.

#include “mbed.h”

Serial pc(USBTX, USBRX); // tx, rx

Serial XbeeSerial(PTC17,PTC16 ,9600); // tx, rx // for K64f , change to match your device

// ---------------------------------------------

int main(){

pc.baud(9600);

XbeeSerial.baud(9600);

printf("M_XbeeRelayV1 \n");   

printf( "Compile date : " __DATE__ ", " __TIME__ "\n");

printf("mbed OS : %d", MBED_MAJOR_VERSION);

printf(".%d", MBED_MINOR_VERSION); 

printf(".%d", MBED_PATCH_VERSION);

printf("\n");

printf("Board : K64F\n");  // my board

// -------------------- Loop ------------------------------

while(1){

    if(pc.readable()) {

        XbeeSerial.putc(pc.getc());

    }

    if(XbeeSerial.readable()) {

        pc.putc(XbeeSerial.getc());    

    }

}

}

Hi George.

I’ve tried having the “Serial” lines outside and inside main but still don’t get anything on the USB port or the serial monitor. Are you still getting USB test in and out?

Thanks for the code. It works great on the serial to USB adapter.

Dan

I have discovered that using EthernetInterface stops the serial code from working! I get time from ntp ok on its own. But when added to the serial code above, only the time works.

Is your problem a conflict between Ethernet and UART3 on K64F? If so, please post a new question. I have a possible solution.