Forum Discussion

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

Interrupt driven UART

Hello everybody,

I'm a NIOS II beginner and I'm having trouble in finding an example how to code an interrupt driven UART application where I can send and receive commands from a camera I have interfaced to the cyclone board RS232 serial port.

I would be grateful for any help

Thanks

1 Reply

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

    This is non HAL code in NIOS1 but it is currently working fine in NIOS2 as well

    include

    # include "excalibur.h"

    global

    //Make sure you have a uart in excalibur.h that matches your uart name (na_uartxxxx)

    np_uart *uart1 = (np_uart *)na_uart_1;

    init the uart

    void uart1_setup(void)

    {

    //UART 1 setup -----------------------

    uart1->np_uartstatus = 0; // clear the interrupt condition

    nr_installuserisr(na_uart_1_irq ,UART1_ISR,(int)uart1);

    uart1->np_uartcontrol=0;

    uart1->np_uartcontrol = uart1->np_uartcontrol | (np_uartcontrol_irrdy_mask);

    }

    irq routine

    void UART1_ISR(int context)

    {

    np_uart *uart1 = (np_uart *)context;

    int status;

    unsigned char rxChar;

    status = uart1->np_uartstatus;

    rxChar = (unsigned char)uart1->np_uartrxdata;

    uart1->np_uartstatus = 0; // clear the interrupt condition

    if(status & np_uartstatus_rrdy_mask) // character arrived?

    {

    // Write code to handle rxchar;

    }

    if(status & np_uartstatus_trdy_mask) // character arrived?

    {

    if (more bytes to transmit?)

    {

    // Write code to send next byte

    uart1->np_uarttxdata=next byte

    }

    else

    {

    // Set the port to read only

    uart1->np_uartcontrol = np_uartcontrol_irrdy_mask; //only Rx enable (if half duplex)

    }

    }

    }

    to start a transmit sequence start by sending a byte and then enabling the Tx IRQ flag.

    uart1->np_uarttxdata=tx1_buffer[0]; // send first byte. IRQ will do the rest

    uart1->np_uartcontrol =np_uartcontrol_itrdy_mask; // only Tx enable

    you can have rx irq flag enabled all the time for full duplex. Just write your code to handle it.

    First use a fix baud rate (as defined in SOPC builder) before playing with your own software settings. Fix baud rates uses less LEs.

    to test connect the 2 pins (Tx,Rx) either in software or hardware and send a few bytes and return the status with a printf or 2.

    To make life easier implement a ring buffer for the received character and parse it outside the irq. Don't put too much inside the IRQ routine. Just grab and store.

    Victor

    http://www.zerksus.com (http://www.zerksus.com)