Skip to main content

IAR Embedded Workbench for RISC-V 3.40

Simulating a simple interrupt

In this section:

This example demonstrates the method for simulating a timer interrupt. However, the procedure can also be used for other types of interrupts.

To simulate and debug an interrupt:
  1. Assume this simple application for a SiFive E24 Arty 100T device. It contains an interrupt service routine for a timer, which increments a tick variable. The main function sets the necessary status registers. The application exits when 100 interrupts have been generated.

    #pragma language = extended
    #include <sdtio.h>
    #include <stint.h>
    #include <intrinsics.h>
    #include <SiFive/ioe24arty.h>
    
    static volatile uint_fast8_t ticks = 0;
    void main (void)
    {
      // Enable timer interrupt (CLINT)
      __set_bits_csr(_CSR_MIE, 0x80);
     
    __enable_interrupt();   /* Enable interrupts */
     
      while (ticks < 100);  /* Wait loop */
    
      // Disable timer interrupt (CLINT)
      __clear_bits_csr(_CSR_MIE, 0x80);
      __disable_interrupt(); /* Disable interrupts */
      printf("Done\n");
    }
    
    /* Timer interrupt service routine */
    #pragma vector = 7
    __interrupt void basic_timer(void)
    {
     ticks += 1;
    }
  2. Add your interrupt service routine to your application source code and add the file to your project.

  3. Choose Project>Options>Debugger>Setup and select a device description file. The device description file contains information about the interrupt that C-SPY needs to be able to simulate it. Use the Use device description file browse button to locate the ddf file.

  4. Build your project and start the simulator.

  5. Choose Simulator>Interrupt Configuration to open the Interrupt Configuration window. Right-click in the window and select Enable Interrupt Simulation on the context menu. For the timer example, verify these settings:

    Option

    Settings

    Interrupt

    TIMER

    First activation

    4000

    Repeat interval

    2000

    Hold time

    10

    Probability (%)

    100

    Variance (%)

    0

    Table 23. Timer interrupt settings 


    Click OK.

  6. Execute your application. If you have enabled the interrupt properly in your application source code, C-SPY will:

    • Generate an interrupt when the cycle counter has passed 4000

    • Continuously repeat the interrupt after approximately 2000 cycles.

  7. To watch the interrupt in action, choose Simulator>Interrupt Log to open the Interrupt Log window.

  8. From the context menu, available in the Interrupt Log window, choose Enable to enable the logging. If you restart program execution, status information about entrances and exits to and from interrupts will now appear in the Interrupt Log window.

    For information about how to get a graphical representation of the interrupts correlated with a time axis, see Timeline window—Interrupt Log graph.