






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







void alt_sys_init( void ) { // ALTERA_AVALON_TIMER_INIT( TIMER, timer ); // ALTERA_AVALON_JTAG_UART_INIT( JTAG_UART, jtag_uart ); ALTERA_AVALON_SPI_INIT( SD_SPI, sd_spi ); ALTERA_AVALON_LCD_16207_INIT( LCD, lcd ); ALTERA_AVALON_UART_INIT( UART, uart ); ALTERA_AVALON_SYSID_INIT( SYSID, sysid ); } ...
// int3.c – Button interrupts
#include "sys/alt_irq.h" #include "system.h" #include "unistd.h" // for usleep
typedef unsigned int REGISTER;
struct PIO_REGS { REGISTER data; // data register REGISTER direction; // direction register REGISTER intmask; // interrupt mask register REGISTER edgecapture; // edge capture register };
struct PIO_REGS *LEDS_RED = ( struct PIO_REGS *)(LEDR_BASE | 0x80000000); struct PIO_REGS *BUTTONS = ( struct PIO_REGS *)(BUTTONS_BASE | 0x80000000);
static unsigned int BTNS = 0;
void handle_button_interrupts() { BTNS = BUTTONS->edgecapture; // get buttons pushed
BUTTONS->edgecapture = 0; // acknowledge interrupt handled }
int main ( void ) { REGISTER LAST_BTNS; // save last buttons pushed
alt_irq_register( BUTTONS_IRQ, NULL , ( void *)handle_button_interrupts );
BUTTONS->intmask = 0x5; // 0b
while (1) { if ( BTNS != LAST_BTNS ) // have buttons changed { LEDS_RED->data = BTNS; // show buttons LAST_BTNS = BTNS; // get new buttons benchmark } usleep(100); } return 0; }
// int4.c – timer interrupts
#include "sys/alt_irq.h" #include "system.h" #include "unistd.h" // for usleep
typedef unsigned int REGISTER; typedef unsigned int BITS;
struct PIO_REGS { REGISTER data; // data register REGISTER direction; // direction register REGISTER intmask; // interrupt mask register REGISTER edgecapture; // edge capture register };
union TIMER_STATUS_REG { REGISTER data; struct { BITS TO : 1; BITS RUN : 1; BITS unused : 30; } bits; };
union TIMER_CONTROL_REG { REGISTER data; struct { BITS ITO : 1; BITS CONT : 1; BITS START : 1; BITS STOP : 1; BITS unused : 28; } bits; };
struct TIMER_REGS { union TIMER_STATUS_REG status; union TIMER_CONTROL_REG control; REGISTER periodl; REGISTER periodh; REGISTER snapl; REGISTER snaph; };
struct PIO_REGS *SEVEN_SEG = ( struct PIO_REGS *)(SEVENSEG_LED_BASE | 0x80000000); struct TIMER_REGS *TIMER = ( struct TIMER_REGS *)(TIMER_BASE | 0x80000000);
static unsigned int SYSTIME = 0;
void handle_timer_interrupts() { SYSTIME++; TIMER->status.bits.TO = 0; // acknowledge interrupt handled }
int main ( void ) { REGISTER LAST_TIME; // save last time value
alt_irq_register( TIMER_IRQ, NULL , ( void *)handle_timer_interrupts ); TIMER->control.bits.ITO = 1; // activate timer interrupts
while (1) { if ( SYSTIME != LAST_TIME ) { // has time changed SEVEN_SEG->data = SYSTIME; // show count in hex LAST_TIME = SYSTIME; // get new time benchmark } } return 0; }
#ifndef TIMER_H_ #define TIMER_H_
void timer_init( void ); extern unsigned int SYSTIME;
#endif /TIMER_H_/
#ifndef BUTTONS_H_ #define BUTTONS_H_
void buttons_init( void ); extern unsigned int BTNS;
#endif /BUTTONS_H_/
// timer.c
#include "sys/alt_irq.h" #include "unistd.h" // for usleep #include "system.h" #include "timer.h" #include "timer_regs.h"
static void handle_timer_interrupts(); // handler is private
static struct TIMER_REGS *TIMER = ( struct TIMER_REGS *)(TIMER_BASE | 0x80000000);
unsigned int SYSTIME = 0; // also defined as extern
void timer_init( void ) { alt_irq_register( TIMER_IRQ, NULL , ( void *)handle_timer_interrupts );
TIMER->control.bits.ITO = 1; }
static void handle_timer_interrupts() { SYSTIME++;
TIMER->status.bits.TO = 0; // acknowledge interrupt handled }
// buttons.c
#include "sys/alt_irq.h" #include "unistd.h" // for usleep #include "system.h" #include "buttons.h" #include "pio_regs.h"
static void handle_button_interrupts(); // handler is private
struct PIO_REGS *BUTTONS = ( struct PIO_REGS *)(BUTTONS_BASE | 0x80000000);
unsigned int BTNS = 0; // also defined as extern
void buttons_init( void ) { alt_irq_register( BUTTONS_IRQ, NULL , ( void *)handle_button_interrupts );
BUTTONS->intmask = 0x5; // 0b }
static void handle_button_interrupts() { BTNS = BUTTONS->edgecapture; // get buttons pushed
BUTTONS->edgecapture = 0; // acknowledge interrupt handled }
void alt_sys_init( void ) { // ALTERA_AVALON_TIMER_INIT( TIMER, timer ); ALTERA_AVALON_JTAG_UART_INIT( JTAG_UART, jtag_uart ); ALTERA_AVALON_SPI_INIT( SD_SPI, sd_spi ); ALTERA_AVALON_LCD_16207_INIT( LCD, lcd ); ALTERA_AVALON_UART_INIT( UART, uart ); ALTERA_AVALON_SYSID_INIT( SYSID, sysid ); } ...