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++
12620 Discussions

nios+timer+uart dont work! why?

Altera_Forum
Honored Contributor II
1,298 Views

This is my code: 

 

# include <unistd.h> 

# include <fcntl.h> 

# include "alt_types.h" 

# include "altera_avalon_pio_regs.h" 

# include "altera_avalon_uart_regs.h" 

# include "altera_avalon_timer_regs.h" 

# include "sys/alt_irq.h" 

# include "system.h" 

 

unsigned char text_buffer; 

 

int send_char_buffer_to_uart(unsigned char *buf, int size){ 

 

int comport,status; 

comport=open("/dev/uart",O_WRONLY); 

status=(write(comport, buf, size)); 

close(comport); 

return status; 

 

int get_char_buffer_from_uart(unsigned char *buf, int size){ 

 

int comport,received_bytes_num; 

comport=open("/dev/uart",O_RDONLY); 

received_bytes_num=(read(comport, buf, size)); 

close(comport); 

return received_bytes_num; 

 

 

void handle_timer_interrupt (void* context, alt_u32 id) 

static alt_u8 value = 0x00; 

 

value = value + 1; 

IOWR_ALTERA_AVALON_PIO_DATA(LEDS_BASE, ~value); 

send_char_buffer_to_uart("hello",5); 

IOWR_ALTERA_AVALON_TIMER_STATUS(SYSTEM_TIMER_BASE, 0); 

 

 

 

 

int main(void){ 

 

//send_char_buffer_to_uart("Hello",5); 

alt_irq_register(SYSTEM_TIMER_IRQ, 0, handle_timer_interrupt); 

while(1); 

 

 

Timer fires every 100 ms. Data are not sent to the serial port and LEDs dont blink (WHY ?). If I comment out the line in bold and uncomment same line in main function, the data will be sent in serial port and LEDs will blink, so my SoPC system is configured correctly. What is my mistake?  

 

P.S. sorry for my english)
0 Kudos
3 Replies
Altera_Forum
Honored Contributor II
294 Views

The open, close and write functions are not designed to be called from within the context of an ISR. This fact can be found in the hal api reference (http://www.altera.com/literature/hb/nios2/n2sw_nii52010.pdf). For example, the documentation for the open() function indicates, "Available from ISR: No." 

 

If you really want to write to the UART from an ISR, you can write your own UART code instead of using the HAL driver. Alternatively, you can create a mechanism for your ISR to queue the data and have non ISR code actually perform the write. There are many ways the problem can be addressed.
0 Kudos
Altera_Forum
Honored Contributor II
294 Views

Thank you kevin, I have decided this issue as follows: 

 

# include <unistd.h> 

# include <fcntl.h> 

# include "alt_types.h" 

# include "altera_avalon_pio_regs.h" 

# include "altera_avalon_uart_regs.h" 

# include "altera_avalon_timer_regs.h" 

# include "sys/alt_irq.h" 

# include "system.h" 

 

unsigned char text_buffer; 

unsigned char int_flag; 

 

int send_char_buffer_to_uart(unsigned char *buf, int size){ 

 

int comport,status; 

comport=open("/dev/uart",O_WRONLY); 

status=(write(comport, buf, size)); 

close(comport); 

return status; 

 

int get_char_buffer_from_uart(unsigned char *buf, int size){ 

 

int comport,received_bytes_num; 

comport=open("/dev/uart",O_RDONLY); 

received_bytes_num=(read(comport, buf, size)); 

close(comport); 

return received_bytes_num; 

 

 

void handle_timer_interrupt (void* context, alt_u32 id) 

static alt_u8 value = 0x00; 

 

value = value + 1; 

int_flag=1; 

IOWR_ALTERA_AVALON_PIO_DATA(LEDS_BASE, ~value); 

IOWR_ALTERA_AVALON_TIMER_STATUS(SYSTEM_TIMER_BASE, 0); 

 

 

 

 

int main(void){ 

 

alt_irq_register(SYSTEM_TIMER_IRQ, 0, handle_timer_interrupt); 

int_flag=0; 

while(1){ 

if(int_flag){ 

send_char_buffer_to_uart("hello",5); 

int_flag=0; 

 

}
0 Kudos
Altera_Forum
Honored Contributor II
294 Views

Hello guys, 

I am very beginner at this. I am using the DE0 board and I want to implement a timer interrupt. The DE0 borad has a cyclone III and I use Quartus II (9.1sp2) & NIOS II (9.1). I have setup the nios system as given in Nios II hardware development tutorial in Altera website.  

 

With this nios configuration is it possible for me to use the above given program to blink a LED at every 1 s interval. Also for the above shown program how can I configure my interval timer in nios. Thanks.
0 Kudos
Reply