Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
150 Views

UART Interrupt and timer interrupt at the same time

Jump to solution

I am using max10, DE10-Lite board.

 

I try to use uart interrupt for get command code.

an i also want use timer for periodic work until input command code.

 

but uart interrupt is work only. timer interrupt does not work.

 

it is my code. i want know what is wrong.

 

#include <stdio.h>

#include "system.h"

#include "Nios_Variables.h"

#include "string.h"

#include <stdlib.h>

#include <stdint.h>

 

 

volatile uint16_t flag = 0u;

int gpio;

 

void handle_timer_interrupt(void*p, alt_u32 param);

void uart_init();

void handle_uart_interrupt(void *context,alt_u32 interrupt);

extern int alt_irq_register (alt_u32 id, void*  context,void (*alt_isr_func)(void* isr_context, alt_u32 id) );

 

int main()

{

 

  // register the timer irq to be serviced by handle_timer_interrupt() function

   alt_irq_register(TIMER_0_IRQ, 0, handle_timer_interrupt);

 

   // activate the time

   IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_0_BASE,

            ALTERA_AVALON_TIMER_CONTROL_CONT_MSK

           | ALTERA_AVALON_TIMER_CONTROL_START_MSK

           | ALTERA_AVALON_TIMER_CONTROL_ITO_MSK);

 

   uart_init();

 

   for(;;)

   {

      if(gpio == 1)

      {

         printf("SW Lock\n\r");

      }

      else

      {

         if(flag > 0)

         {

         printf("A\n");

           flag = 0;

         }

      }

   }

 

 

 return 0;

}

 

void handle_timer_interrupt(void*p, alt_u32 param)

{

  // clear irq status in order to prevent retriggering

  IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_0_BASE, 0);

  gpio = IORD_ALTERA_AVALON_PIO_DATA(PIO_0_BASE);

  // your isr code here

  flag++;

  

}

 

void handle_uart_interrupt(void *context,alt_u32 interrupt)

{

 unsigned short int data,status;

 status = IORD_ALTERA_AVALON_UART_STATUS(E_UART_BASE);

 while (!(status & ALTERA_AVALON_UART_STATUS_RRDY_MSK))

  status = IORD_ALTERA_AVALON_UART_STATUS(E_UART_BASE);

 data =IORD_ALTERA_AVALON_UART_RXDATA(E_UART_BASE);

 //write status reg;

 status = ALTERA_AVALON_UART_STATUS_TRDY_MSK;

 IOWR_ALTERA_AVALON_UART_STATUS(E_UART_BASE, status);

 IOWR_ALTERA_AVALON_UART_TXDATA(E_UART_BASE, data);

 IOWR_ALTERA_AVALON_UART_STATUS(E_UART_BASE, 0);

}

 

void uart_init()

{

 alt_u32 control;

 

 control = ALTERA_AVALON_UART_CONTROL_TRDY_MSK | ALTERA_AVALON_UART_CONTROL_RRDY_MSK | ALTERA_AVALON_UART_CONTROL_E_MSK;

 IOWR_ALTERA_AVALON_UART_CONTROL(E_UART_BASE, control);

 alt_irq_register(E_UART_IRQ, 0, handle_uart_interrupt);

 

}

 

Tags (1)
0 Kudos

Accepted Solutions
Highlighted
Employee
71 Views

Hi Junbum, sorry for the late response. I was looking for the right answer.

 

Here you have an example code with the UART and Timer interruptions, the only difference is that it uses a timer init, could you compare your code with this.

 

/* 

* "Hello World" example. 

* This example prints 'Hello from Nios II' to the STDOUT stream. It runs on 

* the Nios II 'standard', 'full_featured', 'fast', and 'low_cost' example 

* designs. It runs with or without the MicroC/OS-II RTOS and requires a STDOUT 

* device in your system's hardware. 

* The memory footprint of this hosted application is ~69 kbytes by default 

* using the standard reference design. 

* For a reduced footprint version of this template, and an explanation of how 

* to reduce the memory footprint for a given application, see the 

* "small_hello_world" template. 

*/# include <stdio.h># include <unistd.h># include <fcntl.h># include "sys/alt_stdio.h"# include "system.h"# include "alt_types.h"# include "altera_avalon_uart_regs.h"# include "altera_avalon_timer_regs.h"# include "sys/alt_irq.h" 

# define Switches (volatile char *) 0x00041040# define LEDs (char *) 0x00041050# define UART0_BASE (char *) 0x00041000# define UART0_IRQ 1# define TIMER0_IRQ 2# define TIMER0_BASE (char *) 0x00041020 

 

void uart0_put_char(unsigned char ch) 

while((IORD_ALTERA_AVALON_UART_STATUS(UART0_BASE) & 0x040) != 0x040){ ;} 

IOWR_ALTERA_AVALON_UART_TXDATA(UART0_BASE,ch); 

 

void uart0_put_str(unsigned char * str){ 

while(*str){ 

uart0_put_char(*str); 

str++; 

 

void uart_handle(void *context,alt_u32 interrupt) 

unsigned short int data,status; 

status = IORD_ALTERA_AVALON_UART_STATUS(UART0_BASE); 

data =IORD_ALTERA_AVALON_UART_RXDATA(UART0_BASE); 

*LEDs=(char)data; 

 

void uart_init() 

alt_u32 control; 

volatile unsigned long uart_capture; 

control = ALTERA_AVALON_UART_CONTROL_TRDY_MSK | ALTERA_AVALON_UART_CONTROL_RRDY_MSK | ALTERA_AVALON_UART_CONTROL_E_MSK; 

IOWR_ALTERA_AVALON_UART_CONTROL(UART0_BASE, control); 

if (alt_irq_register(UART0_IRQ, (void*)uart_capture, uart_handle)){ 

alt_putstr("UART0_IRQ Register Successful\n"); 

}else{ 

alt_putstr("UART0_IRQ Register UnSuccessful\n"); 

void handle_timer_interrupt (void* context, alt_u32 id){ 

static alt_u32 value = 0x00; 

value = value + 1; 

if(value % 1000 == 0){ 

*LEDs=(char)value; 

IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER0_BASE,0); // Clear the interrupt flag 

 

void timer_init(){ 

IOWR_ALTERA_AVALON_TIMER_CONTROL (TIMER0_BASE,ALTERA_AVALON_TIMER_CONTROL_ITO_MSK | ALTERA_AVALON_TIMER_CONTROL_CONT_MSK | ALTERA_AVALON_TIMER_CONTROL_START_MSK); 

alt_irq_register(TIMER0_IRQ, 0, handle_timer_interrupt); 

 

int main(){ 

printf("Hello from NIOS II \n"); 

uart_init(); 

printf("uart init successful"); 

timer_init(); 

printf("timer init successful"); 

//usleep(1000000); 

printf("Hello from NIOS III \n"); 

*LEDs = 0xAA; 

printf("Switch Status : %x " , (char)*Switches); 

uart0_put_str("Hello World"); 

while(1){ 

//*LEDs = *Switches; 

printf("hello"); 

return 0; 

 

 

 Regards,

Isaac

View solution in original post

0 Kudos
3 Replies
Highlighted
Employee
72 Views

Hi Junbum, sorry for the late response. I was looking for the right answer.

 

Here you have an example code with the UART and Timer interruptions, the only difference is that it uses a timer init, could you compare your code with this.

 

/* 

* "Hello World" example. 

* This example prints 'Hello from Nios II' to the STDOUT stream. It runs on 

* the Nios II 'standard', 'full_featured', 'fast', and 'low_cost' example 

* designs. It runs with or without the MicroC/OS-II RTOS and requires a STDOUT 

* device in your system's hardware. 

* The memory footprint of this hosted application is ~69 kbytes by default 

* using the standard reference design. 

* For a reduced footprint version of this template, and an explanation of how 

* to reduce the memory footprint for a given application, see the 

* "small_hello_world" template. 

*/# include <stdio.h># include <unistd.h># include <fcntl.h># include "sys/alt_stdio.h"# include "system.h"# include "alt_types.h"# include "altera_avalon_uart_regs.h"# include "altera_avalon_timer_regs.h"# include "sys/alt_irq.h" 

# define Switches (volatile char *) 0x00041040# define LEDs (char *) 0x00041050# define UART0_BASE (char *) 0x00041000# define UART0_IRQ 1# define TIMER0_IRQ 2# define TIMER0_BASE (char *) 0x00041020 

 

void uart0_put_char(unsigned char ch) 

while((IORD_ALTERA_AVALON_UART_STATUS(UART0_BASE) & 0x040) != 0x040){ ;} 

IOWR_ALTERA_AVALON_UART_TXDATA(UART0_BASE,ch); 

 

void uart0_put_str(unsigned char * str){ 

while(*str){ 

uart0_put_char(*str); 

str++; 

 

void uart_handle(void *context,alt_u32 interrupt) 

unsigned short int data,status; 

status = IORD_ALTERA_AVALON_UART_STATUS(UART0_BASE); 

data =IORD_ALTERA_AVALON_UART_RXDATA(UART0_BASE); 

*LEDs=(char)data; 

 

void uart_init() 

alt_u32 control; 

volatile unsigned long uart_capture; 

control = ALTERA_AVALON_UART_CONTROL_TRDY_MSK | ALTERA_AVALON_UART_CONTROL_RRDY_MSK | ALTERA_AVALON_UART_CONTROL_E_MSK; 

IOWR_ALTERA_AVALON_UART_CONTROL(UART0_BASE, control); 

if (alt_irq_register(UART0_IRQ, (void*)uart_capture, uart_handle)){ 

alt_putstr("UART0_IRQ Register Successful\n"); 

}else{ 

alt_putstr("UART0_IRQ Register UnSuccessful\n"); 

void handle_timer_interrupt (void* context, alt_u32 id){ 

static alt_u32 value = 0x00; 

value = value + 1; 

if(value % 1000 == 0){ 

*LEDs=(char)value; 

IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER0_BASE,0); // Clear the interrupt flag 

 

void timer_init(){ 

IOWR_ALTERA_AVALON_TIMER_CONTROL (TIMER0_BASE,ALTERA_AVALON_TIMER_CONTROL_ITO_MSK | ALTERA_AVALON_TIMER_CONTROL_CONT_MSK | ALTERA_AVALON_TIMER_CONTROL_START_MSK); 

alt_irq_register(TIMER0_IRQ, 0, handle_timer_interrupt); 

 

int main(){ 

printf("Hello from NIOS II \n"); 

uart_init(); 

printf("uart init successful"); 

timer_init(); 

printf("timer init successful"); 

//usleep(1000000); 

printf("Hello from NIOS III \n"); 

*LEDs = 0xAA; 

printf("Switch Status : %x " , (char)*Switches); 

uart0_put_str("Hello World"); 

while(1){ 

//*LEDs = *Switches; 

printf("hello"); 

return 0; 

 

 

 Regards,

Isaac

View solution in original post

0 Kudos
Highlighted
Employee
71 Views

Hi Junbun,

 

Did the code works for your issue?

 

Best regards,

Isaac.

0 Kudos
Highlighted
Beginner
71 Views

I solved this problem and closed it, but it doesn't seem to close.

0 Kudos