pinball
Posts: 4
Joined: Fri Oct 17, 2014 3:12 pm

UART Bug? Can't set baud rate higher than 115200 in C?

Sun Oct 19, 2014 1:59 am

Hi! Quick issue that's been giving me trouble all afternoon...

From the tutorial, the baud rate can be configured as:

Code: Select all

 //CONFIGURE THE UART
 //The flags (defined in /usr/include/termios.h - see http://pubs.opengroup.org/onlinepubs/007908799/xsh/termios.h.html):
 //  Baud rate:- B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B500000, B576000, B921600, B1000000, B1152000, B1500000, B2000000, B2500000, B3000000, B3500000, B4000000

I was trying to operate at a baud rate of 500000 but was getting no RX or TX with the other device unless I shorted the two RPI pins together in which case it would talk to itself (but not the other device). On the scope I could see data going in and out, so I figured it might be a major baud rate mismatch.

Checking the device with stty I confirmed it:

Code: Select all

pi@raspberrypi /dev $ stty -F ttyAMA0
speed 9600 baud; line = 0;
After this I started experimenting with other B# values, I didn't test all of them but it seems trying to set a baud rate higher than 115200 will fail.

I'm running the NOOBS install of Raspbian from NOOBS_v1_3_10.zip

Hoping this can be fixed! Also would be nice to have more baud rate options like 250kbps

Thanks!

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: UART Bug? Can't set baud rate higher than 115200 in C?

Sun Oct 19, 2014 2:42 am

I think you need to add an entry to /boot/config.txt

Search for serial + ghans or serial + rattus , one or the other is the knowledgeable one in this field.

blavery
Posts: 95
Joined: Sun Jul 01, 2012 2:57 am
Location: QLD. Australia

Re: UART Bug? Can't set baud rate higher than 115200 in C?

Sun Oct 19, 2014 10:46 am

Try this:

(sudo) edit /boot/config.txt by adding a new line
init_uart_clock=16000000

My rPi works at 500000 baud very happily.

Brian

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: UART Bug? Can't set baud rate higher than 115200 in C?

Sun Oct 19, 2014 11:35 am

pinball wrote:I was trying to operate at a baud rate of 500000 but was getting no RX or TX with the other device unless I shorted the two RPI pins together in which case it would talk to itself (but not the other device).
That sounds to me as though it might be your other device - or the path between the two - that is the problem, not the RPi?

pinball
Posts: 4
Joined: Fri Oct 17, 2014 3:12 pm

Re: UART Bug? Can't set baud rate higher than 115200 in C?

Mon Oct 20, 2014 1:41 am

Hi all thanks for the replies

Today confirmed the program is happily chatting away with my target device at 115200, so all is right in this corner of the embedded world :)

BTW it was a good idea to try to force the baud rate higher external to the program in config.txt, this will be a good work around if I end up absolutely needing the higher speed...

For the C library, hopefully RPI can eventually either fix the higher baud rate options so they actually work, or delete them from the .h file and UART tutorial so folks like me don't accidentally try to use them

Thanks again!

blavery
Posts: 95
Joined: Sun Jul 01, 2012 2:57 am
Location: QLD. Australia

Re: UART Bug? Can't set baud rate higher than 115200 in C?

Mon Oct 20, 2014 2:43 am

Setting init_uart_clock in the config .txt doesn't itself force a new baudrate. The default uart clock setting is too slow to cope with higher baudrates. Setting it higher as indicated "PERMITS" you to successfully ask for a higher baudrate.

pinball
Posts: 4
Joined: Fri Oct 17, 2014 3:12 pm

Re: UART Bug? Can't set baud rate higher than 115200 in C?

Tue Oct 21, 2014 3:24 am

Hi Brian, thanks for your help/patience you were 100% right... glad you corrected me on it

Adding the init_uart_clock=16000000 line opened up the higher baud rate, I am now successfully configured @ 500kbps.

Thanks so much!

Return to “C/C++”