Changing default baud rate

03 Sep 2012

Hello, Is there any possibility to change default baud rate from 9600 without creating an Serial object?

I don't want to use:

#include "mbed.h"

Serial pc(USBTX, USBRX);

int main() {
    pc.baud (115200);
    pc.printf ("Hello World!");
}

I would like to use:

#include "mbed.h"

int main() {
    printf ("Hello World!");//with baud 115200
}

Regards, Michal.

03 Sep 2012

I dont think this will possible. You would have to use some low level code to modify the baudrate of the lpc1768, which is possible but not as easy as pc.baud(). A bigger problem is that you may also need to update the baudrate on the magic chip. This is the actual interface between your target lpc1768 and the PC USB link. I guess that selecting the (USBTX, USBRX) automatically includes code in the Serial lib that sets both the serial port speed on the target and on the interface chip.

I would avoid all that headache and just use the provided libs. You can easily 'search and replace' printf() for pc.printf() in case you want to port existing code to mbed.

03 Sep 2012

Hi Steve,

As these end up on the same physical UART, you can actually do:

#include "mbed.h"

Serial pc(USBTX, USBRX);

int main() {
    pc.baud (115200);
    printf ("Hello World!");
}

Related to Wim's comment, the baudrate off the interface is actually set by the PC/Terminal program.

Hope that helps,

Simon

04 Sep 2012

Hello,

Thank you for your replies. If it is impossible, I will try to sort it out in the other way. The reason I wanted to change it without using pc.baud() is that I would like to use:

class MYCLASS {
    MYCLASS() {
    fprintf(stderr, "Error msg");
    }
};

in a constructor of my class. fprintf use USB port like pc.printf with the same baudrate, but in the case of fprintf, I don't need to work with Serial pc(USBTX, USBRX) object inside the class. The object of that class is created before main(), where I can use pc.baud().

Serial pc(USBTX, USBRX);
MYCLASS myobject();

main() {
    pc.baud(115200);
}

I could use pc.baud() inside class

extern Serial pc;

class MYCLASS {
    MYCLASS() {
    pc.baud(115200);
    fprintf(stderr, "Error msg");
    }
};

but I would like to avoid it.

Thanks, Michal.

04 Sep 2012

I've also had this problem, initialiser "debug" is rather difficult if you subsequently want a fast data rate. I don't think there is a particularly easy way round on the mbed platform, without having a instance of the class available. It would be nice to see a companion non-class function baud(int rate) to go with printf(...) but I can see this being a bit tricky. Who owns the sample rate control? mbed already allows multiple objects to own the same pins, which can rather confuse matters, so I guess it wouldn't be such a departure...

For now, could you use either a FTDI cable on another serial port (messy) or just make do with 9600 baud? either that or you could change baud mid session on the pc (also horribly messy).

04 Sep 2012

Hi,

You could try something like this:

Import program

00001 #include "mbed.h"
00002 
00003 void baud(int baudrate) {
00004     Serial s(USBTX, USBRX);
00005     s.baud(baudrate);
00006 }
00007 
00008 int main() {
00009     baud(115200);
00010     printf("Hello, faster world!");
00011 }

This assumes stdout is mapped to the USB UART (which is a good assumption!); However, stdout is really a default output filehandle, so could actually be mapped to a UART, file, or even a dedicated semihosted debug channel, which ultimately why there isn't a generic baud() function.

Ownership is only implemented where virtualisation really makes sense e.g. you can have two instances of a SPI class that map to the same physical hardware, and they will switch "owners" and corresponding configurations as needed (e.g. if the bitrate needed to be updated). For other things where virtualisation doesn't really make sense, the class instancies really jus become proxies for each other. It turned out this was the most useful trade-off between trying to do useful things vs. trusting the user knew what they were doing.

Hope that is useful!

Simon

05 Sep 2012

Hello Simon,

Yes, that is useful. I have put baud(int baudrate) as a class member function and then in a constructor baud(115200);

Many thanks!

Michal.

29 Jan 2014

Hi, please, could you explain me, where can I find transmitted data through this code:

  1. include "mbed.h" int main() { printf ("Hello World!"); }

I am using this code :

  1. include "mbed.h" Serial pc(USBTX, USBRX); int main() { pc.baud (115200); pc.printf ("Hello World!"); } and PC is connected through FTDI chip and "Hello World" is displayed in any Terminal. .. and it works well. How does it function in first example? Which one is faster? And do I need FTDI chip in first example?

Vojtěch

13 Jan 2017

I realize this is an old thread. This isn't usable in my current code ATM, as I think it is forked from a pretty dated project, but I think this would be useful for some folks. Found a code snippet that ended up bringing me back to this:

STDIO retargeting

The mbed-drivers defines retargeting of stdin, stdout and stderr to UART. The default baudrate for STDIO UART peripheral is set via YOTTA_CFG_MBED_OS_STDIO_DEFAULT_BAUD. If this yotta config is not defined, the default value is 115200.

To change STDIO serial settings in the runtime, retrieve the Serial STDIO object get_stdio_serial().

Serial& pc = get_stdio_serial(); pc.baud(9600);

15 Jan 2017

How about initializing the baud rate in the constructor:

#include "mbed.h"
Serial pc(USBTX,USBRX,115200); // 3rd parameter is baud rate
int main () {   while(1) printf("hi");  } 
25 Sep 2017

For mbed os 5.x application using mbed cli, we can override the stdio baud rate setting in mbed_app.json as follows:

{
    "target_overrides": {
        "*": {
            "platform.stdio-baud-rate": 115200
        }
    }
}
31 Jan 2019

Dear Max:

Changing platform.stdio-baud-rate in mbed_app.json does not appear to work.

Any ideas?

Thanks

04 Mar 2019

"platform.stdio-baud-rate" : 115200 should still work. We're using this in a wide variety of examples as well.

17 Apr 2019

In case anyone gets here, I added the last setting to my file, using the online compiler and does work for faster terminal printf.

mbed_app.json

{
    "target_overrides": {
        "*": {
            "target.features": ["LWIP"],
            "target.release_versions": ["2", "5"],
            "platform.stdio-baud-rate": 460800
        }
    }
}