source: Documentation/Tutorials/XPS_Intro/code/UserIO_Intr_Demo.c

Last change on this file was 952, checked in by murphpo, 16 years ago

adding first draft of XPS_Intro tutorial

  • Property svn:executable set to *
File size: 5.8 KB
Line 
1#include "xgpio.h"
2#include "xexception_l.h"
3#include "xintc.h"
4#include "warp_userio.h"
5#include "xparameters.h"
6
7//Shortcuts for constants in xparameters.h
8#define INTC_DEVICE_ID      XPAR_XPS_INTC_0_DEVICE_ID
9#define USERIO_DEVICE_ID    XPAR_USER_IO_DEVICE_ID
10#define USERIO_IRPT_ID      XPAR_XPS_INTC_0_USER_IO_IP2INTC_IRPT_INTR
11
12//Instances of the GPIO and Intc drivers
13static XGpio GPIO_UserIO;
14static XIntc Intc;
15
16//Global variable for tracking active LED outputs
17static unsigned int LED_Outputs = 0;
18
19//Function prototypes
20unsigned char sevenSegmentMap(unsigned char x);
21void UserIO_InterruptHandler(void *InstancePtr);
22void callback_button_center();
23void callback_button_right();
24void callback_button_left();
25void callback_button_up();
26void callback_dipsw(unsigned char dipsw_state);
27
28int main(void)
29{
30    XStatus Status;
31    unsigned char ledDisp = 0;
32   
33    //Initialize the UserIO GPIO core
34    Status = XGpio_Initialize(&GPIO_UserIO, USERIO_DEVICE_ID);
35
36    //We use both channels in the GPIO core- one for inputs, one for outputs
37    XGpio_SetDataDirection(&GPIO_UserIO, USERIO_CHAN_INPUTS, USERIO_MASK_INPUTS);
38    XGpio_SetDataDirection(&GPIO_UserIO, USERIO_CHAN_OUTPUTS, 0);
39
40    //Make sure the LEDs are all off by default
41    XGpio_DiscreteClear(&GPIO_UserIO, USERIO_CHAN_OUTPUTS, USERIO_MASK_OUTPUTS);
42
43/****** START Interrupt Setup ********/
44
45    //Initailize the interrupt controller
46    Status = XIntc_Initialize(&Intc, INTC_DEVICE_ID);
47    if(Status) xil_printf("Intc Setup Error!\r\n");
48
49    //Assign the UserIO ISR; there is a single ISR for any GPIO interrupt event
50    XIntc_Connect(&Intc, USERIO_IRPT_ID, (XInterruptHandler)UserIO_InterruptHandler, &GPIO_UserIO);
51
52    //Configure & enable the GPIO interrupt output
53    // The interrupt is only enabled for the GPIO input channel
54    XGpio_InterruptEnable(&GPIO_UserIO, USERIO_CHAN_INPUTS);
55    XGpio_InterruptGlobalEnable(&GPIO_UserIO);
56
57    //Enable the UserIO interrupt in the interrupt controller
58    XIntc_Enable(&Intc, USERIO_IRPT_ID);
59   
60    //Configure & enable the PPC interrupt input
61    XExc_Init();
62    XExc_RegisterHandler(XEXC_ID_NON_CRITICAL_INT, (XExceptionHandler)XIntc_InterruptHandler, &Intc);
63    XExc_mEnableExceptions(XEXC_NON_CRITICAL);
64
65    //Finally start the interrupt controller
66    XIntc_Start(&Intc, XIN_REAL_MODE);
67
68/****** END Interrupt Setup ********/
69
70    //Manually call the UserIO ISR once to store the initial value of the buttons/switches
71    // This is especially important for applications where the value of the DIP switch means something at boot
72    UserIO_InterruptHandler((void *)&GPIO_UserIO);
73
74    while(1)
75    {
76        xil_printf("Displaying: %x\r\n", ledDisp);
77
78        //Update the global variable we use to track which LED/segments are currently lit
79        // The xps_gpio core doesn't allow outputs to be read from code, so we have to track this internally
80        LED_Outputs = (USERIO_MAP_LEDS(ledDisp) | USERIO_MAP_DISPR(ledDisp) | USERIO_MAP_DISPL(ledDisp));
81       
82        //Set the LEDs & hex displays
83        XGpio_DiscreteSet(&GPIO_UserIO, USERIO_CHAN_OUTPUTS, LED_Outputs);
84
85        usleep(600000);
86
87        //Cycle the displays through [0x0,0xF]
88        if(ledDisp == 15)
89            ledDisp = 0;
90        else
91            ledDisp++;
92    }
93   
94    return 0;
95
96}
97
98//Interrupt service routine for the single GPIO interrupt
99// This function decodes which input caused the interrupt and calls the appropriate callback
100void UserIO_InterruptHandler(void *InstancePtr)
101{
102    xil_printf("UserIO ISR!");
103
104    static unsigned int previousAssertedInputs;
105    unsigned int assertedInputs;
106    unsigned int newAssertedInputs;
107   
108    //Re-interpret the generic input pointer as a GPIO driver instance
109    XGpio *GpioPtr = (XGpio *)InstancePtr;
110
111    //Disable the GPIO core's interrupt output
112    XGpio_InterruptDisable(GpioPtr, USERIO_CHAN_INPUTS);
113
114    //Read the GPIO inputs; each 1 is a currently-asserted input bit
115    assertedInputs = XGpio_DiscreteRead(&GPIO_UserIO, USERIO_CHAN_INPUTS) & USERIO_MASK_INPUTS;
116
117    //XOR the current active bits with the previously active bits
118    newAssertedInputs = assertedInputs ^ previousAssertedInputs;
119    previousAssertedInputs = assertedInputs;
120
121    //Check whether push buttons or DIP switch triggered the interrupt
122    // We assume a user callback per button, and another for the DIP switch
123    if(newAssertedInputs & USERIO_MASK_PBC) callback_button_center();
124    if(newAssertedInputs & USERIO_MASK_PBR) callback_button_right();
125    if(newAssertedInputs & USERIO_MASK_PBL) callback_button_left();
126    if(newAssertedInputs & USERIO_MASK_PBU) callback_button_up();
127    if(newAssertedInputs & USERIO_MASK_DIPSW) callback_dipsw( USERIO_MAP_DIPSW(assertedInputs) );
128
129    //Clear, acknowledge and re-enable the GPIO interrupt output
130    XGpio_InterruptClear(GpioPtr, USERIO_CHAN_INPUTS);
131    XIntc_Acknowledge(&Intc, USERIO_IRPT_ID);
132    XGpio_InterruptEnable(GpioPtr, USERIO_CHAN_INPUTS);
133   
134    return;
135}
136
137//Place holders for user-code callbacks
138void callback_button_center()
139{
140    xil_printf("PB Center!\r\n");
141    return;
142}
143
144void callback_button_right()
145{
146    xil_printf("PB Right!\r\n");
147    return;
148}
149
150void callback_button_left()
151{
152    xil_printf("PB Left!\r\n");
153    return;
154}
155
156void callback_button_up()
157{
158    xil_printf("PB Up!\r\n");
159    return;
160}
161
162void callback_dipsw(unsigned char dipsw_state)
163{
164    xil_printf("DIP SW: 0x%04x\r\n", dipsw_state);
165    return;
166}
167
168unsigned char sevenSegmentMap(unsigned char x)
169{
170    switch(x)
171    {
172        case(0x0) : return 0x007E;
173        case(0x1) : return 0x0030;
174        case(0x2) : return 0x006D;
175        case(0x3) : return 0x0079;
176        case(0x4) : return 0x0033;
177        case(0x5) : return 0x005B;
178        case(0x6) : return 0x005F;
179        case(0x7) : return 0x0070;
180        case(0x8) : return 0x007F;
181        case(0x9) : return 0x007B;
182
183        case(0xA) : return 0x0077;
184        case(0xB) : return 0x007F;
185        case(0xC) : return 0x004E;
186        case(0xD) : return 0x007E;
187        case(0xE) : return 0x004F;
188        case(0xF) : return 0x0047;
189        default : return 0x0000;
190    }
191}
Note: See TracBrowser for help on using the repository browser.