]> rtime.felk.cvut.cz Git - sysless.git/blob - arch/arm/mach-lpc21xx/test/interrupt_t0_test/interrupt_t0_test.c
843647b98a3f995bd6cb9c12320bd2af41d86554
[sysless.git] / arch / arm / mach-lpc21xx / test / interrupt_t0_test / interrupt_t0_test.c
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 //                 Philips LPC210X interrupt use example
4 //
5 // Description
6 // -----------
7 // This example demonstrates
8 //version 1.0 15/08/2005
9 //Author : Guillaume LAGARRIGUE
10 ////////////////////////////////////////////////////////////////////////////////
11
12 #include <LPC210x.h>
13 #include <types.h>
14 #include <cpu_def.h>
15 #include "config.h"
16
17
18 #define NUM_LEDS 4
19 static int leds[] = { 0x10000, 0x20000, 0x40000, 0x80000 };
20
21
22
23 /*////////////////////////////////////////////////////////INITIALISATION FUNCTIONS///////////////////////////////////*/
24
25
26 /**
27 *  Function Name: lowInit()
28 *
29 * Description:
30 *    This function starts up the PLL then sets up the GPIO pins before
31 *    waiting for the PLL to lock.  It finally engages the PLL and
32 *    returns
33 *
34 * Calling Sequence: 
35 *    void
36 *
37 * Returns:
38 *    void
39 *  
40 */
41 static void lowInit(void)
42 {
43     // set PLL multiplier & divisor.
44     // values computed from config.h
45     PLLCFG = PLLCFG_MSEL | PLLCFG_PSEL;
46
47     // enable PLL
48     PLLCON = PLLCON_PLLE;
49     PLLFEED = 0xAA;                       // Make it happen.  These two updates
50     PLLFEED = 0x55;                       // MUST occur in sequence.
51
52     // setup the parallel port pin
53     IOCLR = PIO_ZERO_BITS;                // clear the ZEROs output
54     IOSET = PIO_ONE_BITS;                 // set the ONEs output
55     IODIR = PIO_OUTPUT_BITS;              // set the output bit direction
56
57     // wait for PLL lock
58     while (!(PLLSTAT & PLLSTAT_LOCK))
59         continue;
60
61     // enable & connect PLL
62     PLLCON = PLLCON_PLLE | PLLCON_PLLC;
63     PLLFEED = 0xAA;                       // Make it happen.  These two updates
64     PLLFEED = 0x55;                       // MUST occur in sequence.
65
66     // setup & enable the MAM
67     MAMTIM = MAMTIM_CYCLES;
68     MAMCR = MAMCR_FULL;
69
70     // set the peripheral bus speed
71     // value computed from config.h
72     VPBDIV = VPBDIV_VALUE;                // set the peripheral bus clock speed
73 }
74
75
76 /**
77 *  Function Name: sysInit()
78 *
79 * Description:
80 *    This function is responsible for initializing the program
81 *    specific hardware
82 *
83 * Calling Sequence: 
84 *    void
85 *
86 * Returns:
87 *    void
88 *  
89 */
90 static void sysInit(void)
91 {
92     lowInit();                            // setup clocks and processor port pins
93
94     MEMMAP = 1;              // map interrupt vectors space into FLASH
95
96   //MEMMAP = 1;              // map interrupt vectors space into FLASH
97   MEMMAP = 0x2; /** user RAM mode **/
98
99 }
100
101 /*////////////////////////////LEDS INITIALISATION///////////////////////*/
102
103 /**
104 * Initializes leds.
105 */
106 static void ledInit()
107 {
108     IODIR |= 0x000F0000; /*leds connected to P0.16 17 18 & 19 should blink*/
109     IOSET = 0x00000000;   /* all leds are switched off */
110 }
111
112 /**
113 * Switches the leds off.
114 * @param led  switched off led number. (integer)
115 */
116 static void ledOff(int led)  /* Ioclr.i =1   =>   IOset.i cleared */
117 {
118     IOCLR = led;
119 }
120
121 /**
122 * Switches the leds on. 
123 * @param led  switched on led number. (integer)
124 */
125 static void  ledOn(int led)  /*  Ioset.i = 1   =>  P0.i = 1    */
126 {
127     IOSET = led;
128 }
129
130
131 /**
132 * Creates a delay
133 * @param d duration (unit not defined yet)
134 */
135 void
136 delay(int d)
137 {
138     volatile int x;
139     int i;
140     for (i = 0; i < 10; i++)
141         for(x = d; x; --x)
142             ;
143 }
144
145
146
147 /**
148  * Switches on and off the 4 leds one after one:
149  * 
150  * A led is switched on when an interrupt occurs
151  * then switched off when the next interrupt occurs
152  * @param  void
153  */
154 void timer0_isr(void) __attribute__ ((interrupt));
155
156 int i=0 ; 
157 int led_num=0 ;
158
159 void timer0_isr(void)
160 {   
161      
162     if (i ==0)
163         {
164             ledOn(leds[led_num]);
165             i=1;
166         }  
167     
168         
169         else
170         {
171             ledOff(leds[led_num]);
172             i =0;
173             led_num++;
174         }
175         
176         
177     if (led_num == NUM_LEDS)
178         {
179             led_num=0;
180         }
181
182     T0IR       |= 0x00000001;               // Clear match0 interrupt
183     VICVectAddr  = 0x00000000;
184 }
185
186
187
188
189 /**
190  * initializes timer T0 to raise an interrupt and to reset T0counter when it matches T0MatchRegister0
191  * @param  
192  */
193 static void Init_timer(void)
194 {
195     VICVectAddr0 = (unsigned)timer0_isr;        /* Set the ISR vector address  vector0 = highest priority */
196     VICVectCntl0 = 0x20|4;                      /* Enable this vector and assign Timer IRQ to it */
197     VICIntEnable = 0x00000010;                  /* Enable the interrupt of timer0*/
198
199     
200     T0MR0 = 0x0010000; //Match 0 control register = 65536
201     T0MCR = 0x3; //resets timer0 and generates interrupt when timer0 counter match MR0 register
202
203     T0TCR = 0x3; //Reset timer 0
204
205 }
206
207
208 int main(void)
209 {
210     sysInit();
211     ledInit();
212     Init_timer();
213
214     T0TCR = 0x01; //Run timer 0
215
216     while(1)  //infinite loop
217     {
218     }
219
220     return 0;
221 }