How to use interrupt in the bare-metal mode on Cyclone V?


I try to run the simple code for GPIO’s interrupt on a SoCkit board (Cyclone V).
When I configure interrupt as level-sensitive, it seems to work properly, and the ISR is called many times when i press a buttton on the board. But when i configure the interrupt as edge sensitive, it doesn’t work properly. The ISR is called when i press the button first time, and then called again and again, as if interrupt in level mode. I tried all possible combinations of level/edge modes and polarity modes, but it doesn’t work.

My example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <inttypes.h>
#include “alt_interrupt.h”
#include “alt_timers.h”
#include “alt_generalpurpose_io.h”

        volatile bool blink = false;

         * ISR Callback
         * \param       icciar
         * \param       context ISR context.
         * \return      none
        static void gpio_isr_callback() {

        	long mask = alt_gpio_port_int_status_get(ALT_GPIO_PORTC);
        	// Clear interrupt source don't care about the return value
        	long status = alt_gpio_port_int_status_clear(ALT_GPIO_PORTC, 0x00200000);
        	mask = alt_gpio_port_int_status_get(ALT_GPIO_PORTC);
        	alt_gpio_port_data_write(ALT_GPIO_PORTB, 0x0F000000, blink << 24);
        	blink = !blink;

         * Main entry point
        int main(void) {
        	alt_gpio_port_datadir_set(ALT_GPIO_PORTB, 0x0F000000, 0x0F000000);
                // System init
                // Setup Interrupt
                // Initialize CPU interrupts
                // Set interrupt distributor target
                int cpu_target = 0x1; //CPU0 will handle the interrupts
                alt_int_dist_target_set(ALT_INT_INTERRUPT_GPIO2, cpu_target);
                // Set interrupt trigger type
                alt_int_dist_trigger_set(ALT_INT_INTERRUPT_GPIO2, ALT_INT_TRIGGER_EDGE);
                // Enable interrupt at the distributor level
                // Enable CPU interrupts
                // Enable global interrupts
                alt_gpio_port_datadir_set(ALT_GPIO_PORTC, 0x01e00000, 0x00000000);
        	alt_gpio_port_debounce_set(ALT_GPIO_PORTC, 0x01e00000, 0x00200000);
        	alt_gpio_port_int_type_set(ALT_GPIO_PORTC, 0x01e00000, 0x002000000);
        	alt_gpio_port_int_pol_set(ALT_GPIO_PORTC, 0x01e00000, 0x00200000);

        	// Register gpio ISR
        	alt_int_isr_register(ALT_INT_INTERRUPT_GPIO2, gpio_isr_callback, NULL);
        	alt_gpio_port_int_enable(ALT_GPIO_PORTC, 0x00200000);

        	return 0; //unreachable

Thanks in advance.


Hi. There is an error: undefined reference to “alt_int_global_init” when I compile the interrupt program. Can u help me solve the problem? Thanks a lot!


You need to include “alt_interrupt.h” in your project.


I included “alt_interrupt.h” already. I don’t know why the error occurred. Thanks.


Do u know how to use the interrupt in a C++ project? Maybe the error occurred because of the compiler. Thanks.


No, it is not because of the compiler.
I’ve solved my problem, error occured because of error in the function “alt_gpio_port_int_status_clear”.
If you are interested, you can download full source from my github repository: