Docsity

Microprocessor Lab 6 Interrupts Part 2 - Hardware Abstraction Layer | ECE 332, Lab Reports of Microprocessors

Toggle sidebar
bg1
ECE 332L
Microprocessors Lab Lab 6 Part 2
Spring 2008
Interrupts Part 2
-
1
-
Objective:
In this lab, you will continue the exploration of interrupts in C using a portion of the Hardware
Abstraction Layer (HAL) support from Altera.
Note: Use ptf and sof from last week
Part 1: Interrupt Service Routine with Assembly only
Last week we worked with the modules int1.s, int2.s, int3.s, and int4.s. You were told to use
(Quartus II Version 7.1 Handbook Volume5: Embedded Peripherals) JTAG UART (Chapter 7: JTAG UART
Core with Avalon Interface) to create int5.s that when values are typed into the Terminal Window cause an
interrupt to happen. The value received from keyboard is displayed to the seven-segment LEDs. The ISR should
store the character value received into a variable that is retrieved from the main routine. Look at the steps taken in
examples int3.s and int4.s and compare to the documentation for those devices. Using this knowledge
review the documentation for the JTAG UART to produce code that will achieve the desire results.
Part 2: Interrupt Service Routine with C Programming Language and Minimum HAL
Last week we interfaced to interrupts from the assembler language platform. This week we will interface from
the C platform provided by Altera. When the NIOS II IDE creates the system library (syslib, figure 1 is a block
diagram of a portion of the initialization process and interfaces provided by Altera) it automatically creates low-
level drivers and interfaces for key devices such as the timer and JTAG UART devices. For this lab we wish to
implement these low-level control ourselves (Trust me, this is what you will have to do in real world. So, sit tight
and learn this stuff.). In order to do this we must disable the interfaces provided by Altera.
Figure 1. Software interface generated by Nios IDE
crt0.s
0x20
alt_main ()
main ()
{
...
}
JTAG_UART
TIMER
SD_SPI
LCD
Hardware
alt_sys_init()
init TIMER
init JTAG_UART
init SP_SPI
init LCD
init UART
init SYSID
UART
SYSID
Altera HAL
Altera HAL API
1
3
2
4
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Microprocessor Lab 6 Interrupts Part 2 - Hardware Abstraction Layer | ECE 332 and more Lab Reports Microprocessors in PDF only on Docsity!

Spring 2008 Interrupts Part 2

Objective: In this lab, you will continue the exploration of interrupts in C using a portion of the Hardware

Abstraction Layer (HAL) support from Altera.

Note: Use ptf and sof from last week

Part 1: Interrupt Service Routine with Assembly only

Last week we worked with the modules int1.s, int2.s, int3.s, and int4.s. You were told to use

(Quartus II Version 7.1 Handbook Volume5: Embedded Peripherals) JTAG UART (Chapter 7: JTAG UART

Core with Avalon Interface) to create int5.s that when values are typed into the Terminal Window cause an

interrupt to happen. The value received from keyboard is displayed to the seven-segment LEDs. The ISR should

store the character value received into a variable that is retrieved from the main routine. Look at the steps taken in

examples int3.s and int4.s and compare to the documentation for those devices. Using this knowledge

review the documentation for the JTAG UART to produce code that will achieve the desire results.

Part 2: Interrupt Service Routine with C Programming Language and Minimum HAL

Last week we interfaced to interrupts from the assembler language platform. This week we will interface from

the C platform provided by Altera. When the NIOS II IDE creates the system library ( syslib, figure 1 is a block

diagram of a portion of the initialization process and interfaces provided by Altera) it automatically creates low-

level drivers and interfaces for key devices such as the timer and JTAG UART devices. For this lab we wish to

implement these low-level control ourselves ( Trust me, this is what you will have to do in real world. So, sit tight

and learn this stuff. ). In order to do this we must disable the interfaces provided by Altera.

Figure 1. Software interface generated by Nios IDE

crt0.s

0x

alt_main ()

main ()

TIMER JTAG_UART SD_SPI LCD

Hardware

alt_sys_init()

init TIMER

init JTAG_UART

init SP_SPI

init LCD

init UART

init SYSID

UART SYSID

Altera HAL

Altera HAL API

Spring 2008 Interrupts Part 2

In order to disable the devices for this lab navigate to the syslib directory in the NIOS II IDE directory for you

project and find the “alt_sys_init.c” file and comment out the two lines shown in Figure 2. After the syslib has

been re-compiled the block diagram of the system should be as shown in Figure 3.

Figure 2. Disable low-level support for timer and jtag_uart

Figure 3

Figure 3: Software interface generated by Nios IDE after modification

alt_main ()

main ()

Timer_Init ( );

JTAG_UART_Init ();

TIMER JTAG_UART

SD_SPI LCD

Hardware

alt_sys_init()

init SP_SPI

init LCD

init UART

init SYSID

UART SYSID

Altera HAL

Altera HAL API

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 ); } ...

Spring 2008 Interrupts Part 2

// 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; }

Spring 2008 Interrupts Part 2

// 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; }

Spring 2008 Interrupts Part 2

In the previous three C interrupt examples, it is specialize to just one device ISR. What do you do when you have

to service multiple devices? Following is an implementation in C of the generalize solution presented in part 2.

Take the source code files provided (in int6 folder) and observe the behavior. This is a good base for you

assignment.

main.c

#include "timer.h"

#include "buttons.h"

#include "seg7.h"

#include "ledr.h"

int main ( void )

unsigned int LAST_TIME; // save last time value

unsigned int LAST_BTNS;

timer_init(); // initialise timer ISR

buttons_init(); // initialise buttons ISR

while (1)

if ( SYSTIME != LAST_TIME ) // has time changed

seg7_show(SYSTIME); // show count in hex

LAST_TIME = SYSTIME; // get new time benchmark

if ( BTNS != LAST_BTNS ) // have buttons changed

LEDR_show(BTNS); // show buttons

LAST_BTNS = BTNS; // get new buttons benchmark

return 0;

timer.h

#ifndef TIMER_H_ #define TIMER_H_

void timer_init( void ); extern unsigned int SYSTIME;

#endif /TIMER_H_/

buttons.h

#ifndef BUTTONS_H_ #define BUTTONS_H_

void buttons_init( void ); extern unsigned int BTNS;

#endif /BUTTONS_H_/

Spring 2008 Interrupts Part 2

timer.c

// 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

// 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 }

Spring 2008 Interrupts Part 2

2. Where should the conversion to (HRS:MIN:SEC:1/10SEC) be done? In the ISR or in the display update

routine?

Grading considerations:

1. Does it work as specified or negotiated? (80%)

2. Style or organization. (10%)

3. Code documentation (no report, required a readme file explaining features and how to run it) (10%)

For this assignment, you only need to comment out the timer as the following.

Figure 4. Disable low-level support for timer (for assignment)

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 ); } ...