Forum Discussion

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

Nios II interrupt design example required

I want to understand how to write code for a timer interrupt for the NIOS II in Eclipse. I understand that this will require me to use a timer block in Qsys and than use it to generate the interrupt signal. I am blank on how the C code is to be written and need clarification on this issue.

Is it possible to get a reference design that demonstrates how one may be able to implement interrupts in the NIOS II, with the Qsys side as well as the Eclipse side made manifest? A most humble design shall be well recieved if it can demonstrate the concept.

2 Replies

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

    --- Quote Start ---

    Is it possible to get a reference design that demonstrates how one may be able to implement interrupts in the NIOS II, with the Qsys side as well as the Eclipse side made manifest? A most humble design shall be well recieved if it can demonstrate the concept.

    --- Quote End ---

    See example (LED_PWM is my own Qsys components with HAL):

    #include <stdio.h># include <string.h># include "sys/alt_flash.h"
    # include "sys/alt_irq.h"# include "system.h"
    # include "altera_avalon_pio_regs.h"# include "altera_avalon_timer_regs.h"
    //#include "altera_avalon_timer.h"
    # include "pwm_avalon_interface_regs.h"# include "pwm_avalon_interface.h"# include "pwm_avalon_interface.c"
    //********************** Global Variables ***********************************************
    volatile unsigned short int led_dir;
    volatile int return_code;
    volatile unsigned char i = 0;
    volatile unsigned int duty_cycle = {499999, 150000, 60000, 10000, 5000, 2500, 800, 1};
    //********************** ISR functions **************************************************
    static void timer_isr (void * context)
    { volatile unsigned char* led_ptr;
      volatile unsigned char* dir_ptr;
      IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_BASE, 0);
      dir_ptr = (volatile unsigned char*)context;
      led_ptr = dir_ptr + 1;
      return_code = altera_avalon_pwm_change_duty_cycle(LED_PWM_BASE, duty_cycle);
      IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, *led_ptr);
      if(*dir_ptr)
        { *led_ptr >>= 1;
          i--;
        }
      else
        { *led_ptr <<= 1;
          i++;
        }
      if(*led_ptr & 0x81)
        *dir_ptr ^= 0x1;
    }
    //********************** Auxiliary functions ********************************************
    void check_return_code(unsigned int address, int return_code)
    { if(return_code != ALTERA_AVALON_PWM_OK)
    ;
    //          print_error(address, return_code);
    }
    //********************* Main function ***************************************************
    int main (void)
    { void* led_dir_ptr = (void*)&led_dir;
      led_dir = 0x0100;
      //Timer Initialization
      IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_BASE, 0x0003);
      IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER_BASE, 0);
      IOWR_ALTERA_AVALON_TIMER_PERIODL(TIMER_BASE, 0x9f00);
      IOWR_ALTERA_AVALON_TIMER_PERIODH(TIMER_BASE, 0x0024);
      //Register ISR for timer event
      alt_ic_isr_register(TIMER_IRQ_INTERRUPT_CONTROLLER_ID, TIMER_IRQ, timer_isr, led_dir_ptr, 0);
      //Initialize PWM and Check Return Code
      return_code = altera_avalon_pwm_init(LED_PWM_BASE, 500000, 1);
      check_return_code(LED_PWM_BASE, return_code);
      //Enable PWM and Check Return Code
      return_code = altera_avalon_pwm_enable(LED_PWM_BASE);
      check_return_code(LED_PWM_BASE, return_code);
      //Start timer and begin the work
      IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER_BASE, 0x0007);
    //  IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE, led);
      while (1)
       check_return_code(LED_PWM_BASE, return_code);
    }
    
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    This is interesting. I suppose that the same shall be done if we have a slave SPI IP as it shall also release interrupts when it recieves data. Is that really the case?