Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
21 years ago

Fix for Baud Rate cannot be changed by Software

G'day,

Just ran into a problem setting the baud rate by software under Nios2 v1.01. My apologies if this has already been posted before. Setup as follows:

Optional Hardware Feature: uart divisor register

Optional Compiler Flag: -DALTERA_AVALON_UART_USE_IOCTL

#include "sys/termios.h"# include "sys/ioctl.h"
struct termios uart_settings;
// default baud rate set to 115200
ioctl(fd, TIOCMGET, &uart_settings);
uart_settings.c_ispeed = 57600;
uart_settings.c_ospeed = 57600; // Must set both c_ispeed & c_ospeed otherwise TIOCMGET fails.
ioctl(fd, TIOCMSET, &uart_settings);

The above code doesn't successfully change the baud rate. The problem is in the "altera_avalon_uart.c" function alt_avalon_uart_tiocmset(). Instead of calculating the new divisor value based on the new baud rate it uses the old baud rate. Also changed the divisor calc in accordance with the Nios 2 Processor Reference Manual Section 10-5, which matches what the SOPC buider generates.

Change line...

IOWR_ALTERA_AVALON_UART_DIVISOR(dev->base, ((dev->freq/speed) - 1));

...to...

IOWR_ALTERA_AVALON_UART_DIVISOR(dev->base, ((dev->freq/dev->termios.c_ispeed) + 0.5));

ciao

--

Bob
No RepliesBe the first to reply