Forum Discussion

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

UART interrupt handling

hello everyone.

I am using UP3 board.

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

    Why are you trying to write the UART IRQs yourself? The HAL UART driver should have what you need already... If you're using the IDE to build your code, you get this for free. Just read the PDF documentation on the peripherals and you should have enough information to run with...

    Cheers,

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

    hi slacker...

    actually the inbuilt irqs are mainly for multithreaded environment and i m using a single threaded one.....hence i have to make some changes in there...
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    If you must, you must. You should be aware that you've got to disable the driver ISR's or you'll end up with some strange results...

    Also, the code you sent doesn't have a chance of working. Your options are to:

    1. Use a "hosted" approach...i.e.: The fullblown HAL drivers and main() as your code entry point.

    - In this mode the code flows from crt0.s to the Altera-provided alt_main(), and then into your user code.

    2. Use a "freestanding" approach...i.e.: Using none of the HAL drivers and alt_main() as your entry point.

    - In this mode the code flows from crt0.s directly into your alt_main.

    - See the hello_led software example/template for a good example of this.

    - Keep in mind, that, if you do this, you won't have any of the nice features the HAL provides.

    Cheers,

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

    hi slacker,

    thnx for ur reply. I understood the mistake i was doing. I was trying to combine both HAL and freestanding applications. Now I am trying to use HAL only.

    but still i m confused about a few things.

    1.

    in uart when receive request is made, the rxirq method is called, which then sets a receive flag throught the macro ALT_FLAG_POST(dev->events, ALT_UART_READ_RDY, OS_FLAG_SET),

    but when i looked into the alt_avalon_uart_dev structure, i couldnt find any attribute like events there. now in my main program, i want to check whether my flag is set or not so that i can go to the read function. I checked it through

    if(dev->events){....}

    but this gives an error since it couldnt find any events attribute there. So how can i check for the flag being set or not??

    2.

    I now defined my file pointer as alt_fd* fd instead of FILE* fd to match the prototypes. but now its giving error by the way i open the device or close it.

    I know these are basic programming problems, but m not able to figure out the right way.