Forum Discussion

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

interrupt handling for UART

hello everyone.

I am trying to send and receive data between UART on UP3 and hyperterminal. But am not able to set the interrupts properly and as a result my program goes in an infinite loop.

here is a part of my code....could u plz tell me what else needs to be added to the code so that an interrupt is generated when some data comes on the serial port..

int main(void)

{

int led = 0x01;

int ch = 0;

FILE *fd;

/* Initialise the interrupt controller. */

alt_irq_init (ALT_IRQ_BASE);

IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,led); // write initial value to pio

/* initialise the device drivers/software components*/

alt_sys_init();

# ifdef UART_BASE

fd = fopen("/dev/uart", "r+");

# endif

if(fd){

while(1){

if(FLAG_READ){ /* it is set inside the alt_avalon_uart_rxirq() function when some data is received */

alt_avalon_uart_read(fd,ch,sizeof(int));

break;

} // end of if

} // end of while

} // end of if

if(ch == 'a')

led = 0x02;

else

led = 0x04;

IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,led); // write new value to pio

fclose(fd);

}

whenever i run this code it doesnt come out of the while loop even when i type something on the hyperterminal.

Plz helo me out

thnx

sourabh goel

4 Replies

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

    --- Quote Start ---

    originally posted by sourabhgoel83@Sep 9 2005, 07:41 PM

    hello everyone.

    i am trying to send and receive data between uart on up3 and hyperterminal. but am not able to set the interrupts properly and as a result my program goes in an infinite loop.

    here is a part of my code....could u plz tell me what else needs to be added to the code so that an interrupt is generated when some data comes on the serial port..

    int main(void)

    {

    int led = 0x01;

    int ch = 0;

    file *fd;

    /* initialise the interrupt controller. */

    alt_irq_init (alt_irq_base);

    iowr_altera_avalon_pio_data(led_pio_base,led); // write initial value to pio

    /* initialise the device drivers/software components*/

    alt_sys_init();

    # ifdef uart_base

    fd = fopen("/dev/uart", "r+");

    # endif

    if(fd){

    while(1){

    if(flag_read){ /* it is set inside the alt_avalon_uart_rxirq() function when some data is received */

    alt_avalon_uart_read(fd,ch,sizeof(int));

    break;

    } // end of if

    } // end of while

    } // end of if

    if(ch == 'a')

    led = 0x02;

    else

    led = 0x04;

    iowr_altera_avalon_pio_data(led_pio_base,led); // write new value to pio

    fclose(fd);

    }

    whenever i run this code it doesnt come out of the while loop even when i type something on the hyperterminal.

    plz helo me out

    thnx

    sourabh goel

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

    --- quote end ---

    --- Quote End ---

    Hi sourabh goel,

    Why don&#39;t you try an284 on www.altera.com ?

    Note : This application note was written for NIOS system. If you are using NIOSII , you would have to convert some of the functions to be NIOSII-compatible.

    You can reference to an350 to do it.

    Good luck,

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

    Is there a reason you&#39;re not using the HAL provided routines? Even if they&#39;re not exactly what you need, at least you can test the initial functionality...and they might provide a decent starting point for your own code.

    See the Software User&#39;s Guide in the documents directory for more info.

    Cheers,

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

    Hey all,

    Here is some working code for an UART RX interrupt:

    A interrupt function which takes the data from the serial port

    (and put it into for example in a char array)

    void UartReceiver1(void * content,alt_u32 id)

    {

    // get the uart data

    unsigned char * UartData = (unsigned char*)(UART1_BASE);

    // UART1_BASE is defined in system.h

    }

    }

    Setting up the interrupt:

    // UART1 interrupt routine declaration

    alt_irq_register(UART1_IRQ,NULL,UartReceiver1);

    IOWR_ALTERA_AVALON_UART_CONTROL(UART1_BASE, 0x80);

    Kind regards

    Karel