Forum Discussion

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

how could i change my UART baud

My UART baud has been sent to 115200 in the SOPC builder, and can be changed by software,but hou could i chang it to be 9600 with the Language of Nios2,who can help me ,thanks very much!

4 Replies

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

    something like this:

    int uart_set_baudrate(int filedescriptor,int baudrate) {

    struct termios term;

    int res;

    assert(filedescriptor);

    res=ioctl(filedescriptor,TIOCMGET,&term);

    if(res<0) {

    DPRINTF("ERROR ioctl filedescriptor:%d\r\n",filedescriptor);

    return res;

    }

    term.c_ispeed=baudrate;

    term.c_ospeed=baudrate;

    res=ioctl(filedescriptor,TIOCMSET,&term);

    if(res<0) {

    DPRINTF("ERROR ioctl filedescriptor:%d\r\n",filedescriptor);

    }

    return res;

    }

    to get filedescriptor use:

    int fd;

    fd=open("/dev/uart_0",O_RDWR | O_NONBLOCK | O_NOCTTY);
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Fischer I already found this answer .. but never success with this way...

    I suggest :

    In Quartus II v6.0 Handbook -> embedded peripherals.

    You could find this for UART baudrate configuration :

    divisor=int((clok freq/baudrate)+0.5)

    According to your clock freq and the baudrate you want, write into the divisor :

    IOWR_ALTERA_AVALON_UART_DIVISOR(uart_base_addr, new_divisor);

    don&#39;t forget to allow modification of divisor in SOPC Builder

    you can also read your divisor with IORD_ALTERA_AVALON_UART_DIVISOR..

    You don&#39;t need to open a file descriptor on the uart IP device

    example :

    clock freq : 50Mhz

    baudrate : 921600 bds

    divisor = 54
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    sorry, I forget to mention that you have to define

    ALTERA_AVALON_UART_USE_IOCTL in c-compiler settings

    in your system library.

    (see altera_avalon_uart.c)

    it is indeed only the baudrate setting,

    that is supported by the altera device driver&#39;s ioctrl function.

    the ioctrl function is the "linux" way to set the baudrate.

    the direct way to modify the baudrate like Billooj suggest&#39;s works,

    but it ignores the driver.

    this is not so much a problem in the case of only baudrate settings,

    but modifying other uart registers directly may confuse the hal driver.

    it is dangerous to bypass the driver, that means mix hall driver functions

    and direct SDK access, if you do not exactly know what you are doing.

    e.g. in the case of the baudrate, if you set the baudrate with

    IOWR_ALTERA_AVALON_UART_DIVISOR,

    you cannot read it back with "res=ioctl(filedescriptor,TIOCMGET,&term);"

    if you work with filedescriptors you can change "/dev/uart_0" to e.g.

    "/dev/uart1" and everything works again.

    I think result is

    IOWR_ALTERA_AVALON_UART_DIVISOR is

    advantage:

    - simple

    - short

    - works

    disadvantage

    - dangerous to directly access hardware registers,

    which is normally done by the HAL driver.

    (not a good programming style, also not possible in linux"

    - not portable
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thanx for precisions Fisher.

    I&#39;ll try again modify parameters with temrmios structure.