source: Documentation/ReferenceDesigns/InterruptRef_xps_8_2/src/interruptTester.c @ 485

Revision 485, 5.8 KB checked in by chunter, 5 years ago (diff)

Interrupt reference design demonstrates the correct way to initialize and configure interrupts with the interrupt controller.

Line 
1/*
2 *  interruptTester.c
3 * 
4 *      This source describes the proper way to handle multiple interrupts with a PowerPC.
5 *      The code assumes a properly configured base system containing an opb_timer,
6 *      opb_gpio push buttons, a RS232 opb_uartlite, and finally and opb_intc interrupt
7 *      controller.
8 *
9 *      The code will periodically start a timer. By default, this timer will expire every
10 *      6 seconds. Before the timer expires, the user can depress push buttons and see confirmation
11 *      of those events on a terminal connected to the board via the RS232 serial port.
12 *      Upon the expiration of the timer, the "timer_a_int_handler" ISR is called by the
13 *      interrupt controller driver.
14 *
15 *      In this ISR, the program will block for three seconds. This effectively simulates a
16 *      long operation. During this time, users will not be able to trigger interrupts with
17 *      push buttons.
18 *
19 *  When the ISR returns, push buttons become operational again. Pressing the left button
20 *      will disable the periodic timer at the interrupt controller level (i.e. the timer
21 *      itself is still generating interrupt signals). Pressing the right button will re-enable
22 *      the interrupts, returning the system to its normal state.
23 *
24 *  Created by Chris Hunter on 1/24/07.
25 *
26 */
27
28
29#include "xparameters.h"
30#include "xtmrctr.h"
31#include "xintc.h"
32#include "xgpio.h"
33#include "xexception_l.h"
34
35
36// Driver instances
37static XGpio myGpio;
38static XTmrCtr myTimer;
39static XIntc myIntc;
40
41
42// Push Button ISR: function is called upon the depression of ANY of the 4
43// buttons. Which button is determined within the ISR.
44void pb_int_handler(void *baseaddr_p) {
45        Xuint32 dsr;
46        XStatus stat;
47        char lock;
48       
49        //DSR contains the information of which button was depressed, so we can switch
50        //on its value.
51        dsr = XGpio_mGetDataReg(XPAR_PUSH_BUTTONS_4BIT_BASEADDR, 2);
52        switch(dsr) {
53               
54                case 0x01:
55                        xil_printf("Middle\r\n");
56                        break;
57                       
58                case 0x02:
59                        xil_printf("Right: Enabling Timer interrupts\r\n");
60                        XIntc_Enable(&myIntc, XPAR_OPB_INTC_0_OPB_TIMER_0_INTERRUPT_INTR);
61                       
62                        break;
63                       
64                case 0x08:
65                        xil_printf("Up\r\n");
66                        break;
67                       
68                case 0x04:
69                        xil_printf("Left: Disabling Timer interrupts\r\n");
70                        XIntc_Disable(&myIntc, XPAR_OPB_INTC_0_OPB_TIMER_0_INTERRUPT_INTR);
71                        break;
72                       
73                default : {
74                }
75                                       
76        }
77        //Clear the interrupt both in the Gpio instance as well as the interrupt controller
78        XGpio_InterruptClear(&myGpio, 0x3);
79        XIntc_Acknowledge(&myIntc,XPAR_OPB_INTC_0_PUSH_BUTTONS_4BIT_IP2INTC_IRPT_INTR);
80}
81
82// Timer ISR: function is called upon the expiration of either of two timers in a
83// single instance of the opb_timer peripheral. For the purposes of this demonstration,
84// we are only using timer 0.
85void timer_a_int_handler(void *CallBackRef, Xuint8 TmrCtrNumber) {
86
87                if (TmrCtrNumber==0) {
88                        xil_printf("In timer interrupt handler... waiting for 3 seconds\r\n");
89                        usleep(3000000);
90                        xil_printf("Leaving timer interrupt handler ...\r\n");
91                }
92        //Clear the interrupt
93        XIntc_Acknowledge(&myIntc,XPAR_OPB_INTC_0_OPB_TIMER_0_INTERRUPT_INTR);
94        }
95       
96// Main: this is the function that is executed at the start of the program.     
97int main(){
98       
99        /*There are three phases of set-up to get interrupts to work:
100                1) Device initialization and configuration
101                2) Interrupt controller initialization and configuration
102                3) PowerPC exception initialization
103        */
104
105       
106        //********************** 1. Device initialization and configuration *************************
107        xil_printf("Setting up peripherals...\r\n");
108       
109        //Initialize and configuring the timer
110        XTmrCtr_Initialize(&myTimer, XPAR_OPB_TIMER_0_DEVICE_ID);
111        XTmrCtr_SelfTest(&myTimer, 0);
112        XTmrCtr_SetOptions(&myTimer,(Xuint8)0,XTC_INT_MODE_OPTION | XTC_DOWN_COUNT_OPTION | XTC_AUTO_RELOAD_OPTION);
113        XTmrCtr_SetHandler(&myTimer,(XTmrCtr_Handler)timer_a_int_handler,NULL); 
114       
115        //Initialize and configure the push buttons
116        XGpio_Initialize(&myGpio, XPAR_PUSH_BUTTONS_4BIT_DEVICE_ID);
117        XGpio_mSetDataDirection(XPAR_PUSH_BUTTONS_4BIT_BASEADDR, 1, 0x3);
118        XGpio_InterruptEnable(&myGpio, XGPIO_IR_CH1_MASK);
119        XGpio_InterruptGlobalEnable(&myGpio);
120        //*******************************************************************************************
121
122       
123       
124        //*************** 2. Interrupt controller initialization and configuration ******************
125        xil_printf("Setting up interrupt controller...\r\n");
126        XIntc_Initialize(&myIntc, XPAR_OPB_INTC_0_DEVICE_ID);
127       
128        //Attach the ISRs to the interrupt controller driver.
129        //NOTE: The timer is weird. You have to attach "XTmrCtr_InterruptHandler," which is nested
130        //deep within the timer driver. It in turn calls the callback you provide during the
131        //configuration above.
132        XIntc_Connect(&myIntc, XPAR_OPB_INTC_0_PUSH_BUTTONS_4BIT_IP2INTC_IRPT_INTR,
133                           (XInterruptHandler)pb_int_handler,
134                           &myGpio);
135        XIntc_Connect(&myIntc, XPAR_OPB_INTC_0_OPB_TIMER_0_INTERRUPT_INTR,
136                           (XInterruptHandler)XTmrCtr_InterruptHandler,
137                           &myTimer);                                                                   
138                                                                       
139        XIntc_Start(&myIntc, XIN_REAL_MODE);
140        XIntc_Enable(&myIntc, XPAR_OPB_INTC_0_PUSH_BUTTONS_4BIT_IP2INTC_IRPT_INTR);
141        XIntc_Enable(&myIntc, XPAR_OPB_INTC_0_OPB_TIMER_0_INTERRUPT_INTR);
142        //*******************************************************************************************
143
144
145
146        //************************ 3. PowerPC exception initialization ******************************
147        xil_printf("Setting up exceptions...\r\n");             
148    XExc_Init();
149    XExc_RegisterHandler(XEXC_ID_NON_CRITICAL_INT,
150                        (XExceptionHandler)XIntc_InterruptHandler,
151                         &myIntc);
152    XExc_mEnableExceptions(XEXC_NON_CRITICAL);
153        //*******************************************************************************************
154
155       
156        //Set the timer to expire every 6 seconds
157        XTmrCtr_SetResetValue(&myTimer, (Xuint8)0, 6 * 50000000);
158        XTmrCtr_Start(&myTimer, (Xuint8)0);
159       
160       
161        xil_printf("Entering loop...\r\n");
162        while(1){
163        }
164       
165}
166
167
168
Note: See TracBrowser for help on using the repository browser.