Forum Discussion
Altera_Forum
Honored Contributor
19 years agoHello TommyCP,
i tried the following in an ep1c3t144 chip, quartus 5.1 nios 5.1, and it works as expected. in to schematic i have: input clk : 50Mhz input reset_n , connected to VCC in fpga inport button [7..0] (PIO_0) outport led [7..0] )PIO_1) cpu nios II/e jtag debug module: 0x00..0x7ff onchip memory 4k add: 0x1000..0x1fff pio_0 8 bit input only,sync capture faling edge, generate irq edge add: 0x800 irq:0 pio_1 8 bit output only add: 0x810 uses hello world small as start and modified it as following switch on input 0 generates irq and toggles output 1 output 0 is toggled in while loop program compiles: make -s all Compiling hello_world_small.c... ../hello_world_small.c: In function `main': ../hello_world_small.c:88: warning: passing arg 2 of `alt_irq_register' discards qualifiers from pointer target type Linking hello_world_small_1.elf... Info: (hello_world_small_1.elf) 2080 Bytes program size (code + initialized data). Info: 2016 Bytes free for stack + heap. Post-processing to create onchip_memory_0.dat Post-processing to create onchip_memory_0.hex Post-processing to create onchip_memory_0.sym Build completed # include <stdio.h># include <unistd.h> // usleep()# include "system.h"# include "sys/alt_irq.h"# include "altera_avalon_pio_regs.h" static void handle_button_interrupts(void* context, alt_u32 id); volatile alt_u8 led=0; /* A variable to hold the value of the button pio edge capture register. */ volatile int edge_capture; volatile int irq_count; # define BUTTON_PIO_BASE PIO_0_BASE# define BUTTON_PIO_IRQ PIO_0_IRQ# define LED_PIO_BASE PIO_1_BASE int main() { // Enable all 4 button interrupts. IOWR_ALTERA_AVALON_PIO_IRQ_MASK(BUTTON_PIO_BASE, 0xf); // Reset the edge capture register. IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0x0); // Register the interrupt handler. alt_irq_register( BUTTON_PIO_IRQ,&edge_capture, handle_button_interrupts ); while(1) { usleep(100000); led ^= 0x01; IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,led); } return 0; } static void handle_button_interrupts(void* context, alt_u32 id) { // Cast context to edge_capture's type. It is important that this be // declared volatile to avoid unwanted compiler optimization. // volatile int* edge_capture_ptr = (volatile int*) context; // Store the value in the Button's edge capture register in *context. *edge_capture_ptr = IORD_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE); // Reset the Button's edge capture register. IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0); led^=0x02; IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,led); // irq_count++; }