Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
12603 Discussions

How to use multiple uarts with NIOS2 using interrupts?

Honored Contributor II



In our project with NIOS2 processor, we have 5 UARTs. What we need to do is actually quite simple. I need to read from and write to those UARTs. Using following code I can write data on each UART. 

FILE* f0; f0 = 0; f0 = fopen(UART_0_NAME, "r+"); if(f0) fprintf(f0, "\nf0 test"); fclose(f0);  


However I am not able to read using interrupts. I can actually poll those ports using getc()and fopen methods but it works as blocking and polling is not a good way to do it.. 


So I tried this: 



static void handle_uart2(void* context_isr) { altera_avalon_uart_state* sp_isr = (altera_avalon_uart_state*) context_isr; // for debugging } ALTERA_AVALON_UART_STATE_INSTANCE(UART_2, uart_context2); void init_uart2_int() { ALTERA_AVALON_UART_STATE_INIT(UART_2, uart_context2); void* uart_context2_ptr = (void*)&uart_context2; alt_ic_isr_register( UART_2_IRQ_INTERRUPT_CONTROLLER_ID, UART_2_IRQ, handle_uart2, &uart_context2, 0x0); alt_ic_irq_enable( UART_2_IRQ_INTERRUPT_CONTROLLER_ID, UART_2_IRQ); }  


When it receives data on uart2, handle_uart2 method is triggered however, it gets stuck in it handle_uart2 is called again and again.. 


When I remove alt_ic_isr_register call, driver handles isr perfectly. Using debugger I have followed the code, first altera_avalon_uart_irq then altera_avalon_uart_rxirq is called on data receive. And altera_avalon_uart_rxirq successfully handles data, I can observe data transmitted on receive buffer (sp->rx_buf). 



I guess I should be using altera_avalon_uart_read(altera_avalon_uart_state* sp, char* ptr, int len, int flags) function, but I m not sure how. Actually I don’t know when to use it? I mean how I am going to be notified when there is data available without polling. 




It would be nicer to use functions like fgetc, or read etc. However, according to API reference, those functions are not ISR friendly. It says, they might lead to a deadlock when used in ISR. 





Thanks in advance
0 Kudos
0 Replies