Forum Discussion

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

NIOS Uart : change baud rate

I am having trouble to change the baud rate of a NIOS avalon uart.

Note that I am not running against any OS, just the "standard" native NIOS environment.

I tried :

alt_avalon_uart_ioctl( fd, TIOCMGET, &options) Failed the link ( API not defined )

tcgetattr( fd, &options ) Returns failure.

ioctl( fd, TIOCMGET, &options ) Returns failure.

I am able to get char in/out of the uart, with the default baud rate, after the open(), thus fd is ok.

Any insights ?

Cheers,

-- Richard --

4 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I believe the UART hardware has to be configured to allow for adjustable baud rates. Unless you remember doing this, in SOPC Builder, I believe the default is off.

    Cheers,

    - slacker
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    For being able of modifying the UART baudrate, check theese:

    - In SOPCBuilder open the editor dialog for the UART component in your system (double click on it), there is a parametter for including an UART bauderate register;

    - Make sure that you're not using the reduced footprint driver's HAL mode in your system library;

    - Include this define ALTERA_AVALON_UART_USE_IOCTL in the compiler preprocessor configuration settings for your system library and recompile it;

    - Add the includes# include <sys/ioctl.h> and# include <sys/termios.h> to your source file;

    Now you can use code like this for changing the uart speed:

    struct termios termios;

    int res = ioctl( uartfd, TIOCMGET, &termios );

    termios.c_ispeed = your_new_bauderate;

    termios.c_ospeed = your_new_bauderate;

    res = ioctl( uartfd, TIOCMSET, &termios );

    The above code can be used provided that you opened the uart using code like:

    int uartfd;

    uartfd = open( your_uart_device_name, O_RDWR | O_NONBLOCK | O_NOCTTY );

    if (uartfd == -1)

    {

    printf( "FATAL ERROR with uart \"%s\"\r\n", uart_name );

    return 1;

    }

    The options are ORed according to my needs, which could be a little bit different from yours.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thanks very much for the precise response.

    It works. I wasn&#39;t aware of the ALTERA_AVALON_UART_USE_IOCTL.

    Cheers,

    -- Richard --

    --- Quote Start ---

    originally posted by vlorenzo@Jan 14 2006, 11:05 AM

    for being able of modifying the uart baudrate, check theese:

    - in sopcbuilder open the editor dialog for the uart component in your system (double click on it), there is a parametter for including an uart bauderate register;

    - make sure that you&#39;re not using the reduced footprint driver&#39;s hal mode in your system library;

    - include this define altera_avalon_uart_use_ioctl in the compiler preprocessor configuration settings for your system library and recompile it;

    - add the includes# include <sys/ioctl.h> and# include <sys/termios.h> to your source file;

    now you can use code like this for changing the uart speed:

    struct termios termios;

    int res = ioctl( uartfd, tiocmget, &termios );

    termios.c_ispeed = your_new_bauderate;

    termios.c_ospeed = your_new_bauderate;

    res = ioctl( uartfd, tiocmset, &termios );

    the above code can be used provided that you opened the uart using code like:

    int uartfd;

    uartfd = open( your_uart_device_name, o_rdwr | o_nonblock | o_noctty );

    if (uartfd == -1)

    {

    printf( "fatal error with uart \"%s\"\r\n", uart_name );

    return 1;

    }

    the options are ored according to my needs, which could be a little bit different from yours.

    <div align='right'><{post_snapback}> (index.php?act=findpost&pid=12196)

    --- quote end ---

    --- Quote End ---

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    For being able of modifying the UART baudrate, check theese:

    - In SOPCBuilder open the editor dialog for the UART component in your system (double click on it), there is a parametter for including an UART bauderate register;

    - Make sure that you're not using the reduced footprint driver's HAL mode in your system library;

    - Include this define ALTERA_AVALON_UART_USE_IOCTL in the compiler preprocessor configuration settings for your system library and recompile it;

    - Add the includes# include <sys/ioctl.h> and# include <sys/termios.h> to your source file;

    Now you can use code like this for changing the uart speed:

    struct termios termios;

    int res = ioctl( uartfd, TIOCMGET, &termios );

    termios.c_ispeed = your_new_bauderate;

    termios.c_ospeed = your_new_bauderate;

    res = ioctl( uartfd, TIOCMSET, &termios );

    The above code can be used provided that you opened the uart using code like:

    int uartfd;

    uartfd = open( your_uart_device_name, O_RDWR | O_NONBLOCK | O_NOCTTY );

    if (uartfd == -1)

    {

    printf( "FATAL ERROR with uart \"%s\"\r\n", uart_name );

    return 1;

    }

    The options are ORed according to my needs, which could be a little bit different from yours.

    --- Quote End ---

    Hi,

    Regarding the sentence : Include this define ALTERA_AVALON_UART_USE_IOCTL in the compiler preprocessor configuration settings for your system library and recompile it;

    Can you please explain how its done?

    Thanks