9 years, 2 months ago.

K64F: USBSerial vs .NET

Dear all,

Target: K64F. I need a sustained transmission of about 30KB/s from a PC to the MCU, which will acknowledge and send complementary information to the PC. Note: I've previously tried MODSERIAL, but soon realized I couldn't get reliable transmissions from the K64F to the PC above 115'200 baud, and so didn't bother testing transmission reliability from the PC to the board.

I had high hopes for USBSerial, so I've tried this simple program, copied from http://developer.mbed.org/handbook/USBSerial.

#include "mbed.h"
#include "USBSerial.h"
 
//Virtual serial port over USB
USBSerial serial;
 
int main(void) {
 
    while(1)
    {
        serial.printf("I am a virtual serial port\r\n");
        wait(1);
    }
}

From Tera Term on a Windows 7 Pro 64-bit, I choose "Serial", then "COM4: Mbed Virtual Serial Port", but nothing gets displayed. I know the cable is good. Device Manager shows "Mbed Virtual Serial Port (COM4)" under the "Ports (COM & LPT)" node.

Q1. There's no baud rate specified on the mbed side (transfer will presumably occur at USB speed), but how's Tera Term supposed to handle this? I've tried allbaud rates to no avail. Maybe I shouldn't be using Tera Term in the first place in the case of USBSerial.

Q2. From Visual Studio (VS2013 Pro, .NET FW 4.5) I've tried : ...Private WithEvents testPort As New SerialPort("COM4")... The DataReceived event is raised only once (after I press Reset on the K64F board), and testPort.ReadExisting() systematically returns an empty string.

Any leads would be appreciated. Thanks.

DG

2 Answers

9 years, 2 months ago.

I just tried it with my K64F and it runs fine.

Some ideas: Make sure both your mbed library and USBDevice library are the latest version (right mouse button, update).Can you blink an LED in the loop, to see if the loop is running?

Baudrate is not relevant since it is via USB anyway. You can attach an interrupt which is fired when the user changes baudrate settings, for example when you want to make a USB <> UART converter. But for just using serial over USB it is not required.

And yeah problem with USBTX/USBRX serial is that the interface chip does not use its fractional baudrate prescaler. On the KL25, which don't have a fractional baudrate prescaler, this meant that the two errors cancelled and highest baudrate worked fine with USBTX and USBRX. On the K64F which does have the correct baudrate it gives errors.

However the good news: Since the K64F has here a too accurate baudrate, we can simply change it to have the same error as the interface chip. I don't know exactly what it needs to be, you could get it from the user manual, by calculating baudrate when fractional prescaler is disabled. Or trial and error: 921600 * 1.1 gives me error free result. Probably worth it to figure out the boundaries (921600 gives some errors, so find the upper boundary where it again starts to give some errors), then take average for correct baudrate.

Edit: Also this is with only the K64 usb port plugged in? Not that you accidently have the wrong COM port.

Accepted Answer

Eric, thanks for your help. A baud rate of 1000000 somehow works flawlessly, whereas values in the vincinity, including the standard 921200, mangle the data.

(Edit: additional details.) mbed revision: 94:9ad691361fac. K64F firmware version: 0221.

Functional code for high speed USBTX/USBRX serial on FRDM-K64F target

#include "mbed.h"
 
Serial pc(USBTX, USBRX);
 
int main(void) {
    pc.baud(1000000);
 
    while(1)
    {
        wait(0.1);
        pc.printf("The quick brown fox jumps over the lazy dog. 1234567890.\r\n");
    }
}
posted by D G 24 Feb 2015

The maximum allowed error on the baudrate is something like 5% (10 bits, assuming you start exactly in the center the first time you may have half a bit error at the end). So you cannot be too far off.

But 1000000 makes sense: That is of course exactly divisable for the probably 48MHz clock of the interface IC.

posted by Erik - 24 Feb 2015
9 years, 2 months ago.

"There is no baud rate specified"
This is not true, default is 9600 bauds, 8 bits, no parity one stop !
look at the hanbook, clic and you should get the documentation.
or the easy way: you open a project with the online compiler, open the folder Mbed/Classes/Serial and you get the documentation
I hope this can help you

Nowhere in USBSerial do I find anything about baud rates, but then I may be looking at it the way I look for stuff in the fridge when my wife asks me for something.

Besides: https://developer.mbed.org/questions/2654/How-to-change-the-baud-rate-for-USBSeria/ https://developer.mbed.org/questions/2857/Absolute-max-baudrate-for-USBSerial/

posted by D G 24 Feb 2015