1 | /****************************************************************************
|
2 | *
|
3 | * Project: LPC-E2468 blinking led demo
|
4 | *
|
5 | * Copyright: Ivan Vasilev, Olimex Ltd. All rights reserved.
|
6 | *
|
7 | * File: $File main.c $
|
8 | * Description: Board-specific USB initialization
|
9 | * Developer: Ivan Vasilev, <ivan at l123.org>
|
10 | *
|
11 | * Last change: $Date: 2008-04-08 09:23:26 +0300 (???????, 08 ????? 2008) $
|
12 | * Revision: $Revision: 4 $
|
13 | * Id: $Id: main.c 4 2008-04-08 06:23:26Z Ivan $
|
14 | * Author: $Author: Ivan $
|
15 | *
|
16 | * This program is free software; you can redistribute it and/or
|
17 | * modify it under the terms of the GNU General Public License as
|
18 | * published by the Free Software Foundation; either version 2 of
|
19 | * the License, or (at your option) any later version.
|
20 | *
|
21 | ****************************************************************************/
|
22 |
|
23 | #define PLL_MValue 11
|
24 | #define PLL_NValue 0
|
25 | #define CCLKDivValue 4
|
26 | #define USBCLKDivValue 5
|
27 |
|
28 | #include "lpc2468_registers.h"
|
29 | #include "uart.h"
|
30 | #include <stdio.h>
|
31 |
|
32 | #define STAT1 0x01
|
33 | #define STAT2 0x02
|
34 |
|
35 | #define LED_OFF 0x01
|
36 | #define LED_ON 0x02
|
37 | #define LED_TOGGLE 0x03
|
38 |
|
39 | #define TRUE 1
|
40 | #define FALSE 0
|
41 |
|
42 | void SetLed(int led, int state);
|
43 | void InitLeds(void);
|
44 |
|
45 | void InitPLL(void);
|
46 | void feed(void);
|
47 |
|
48 | void IRQ_Routine (void) __attribute__ ((interrupt("IRQ")));
|
49 | void FIQ_Routine (void) __attribute__ ((interrupt("FIQ")));
|
50 | void SWI_Routine (void) __attribute__ ((interrupt("SWI")));
|
51 | void UNDEF_Routine (void) __attribute__ ((interrupt("UNDEF")));
|
52 |
|
53 | /* return TRUE if the button is pressed */
|
54 | int But1Pressed(void)
|
55 | {
|
56 | if (FIO2PIN & (1<<13)) return FALSE;
|
57 | else return TRUE;
|
58 | }
|
59 | int But2Pressed(void)
|
60 | {
|
61 | if (FIO2PIN & (1<<21)) return FALSE;
|
62 | else return TRUE;
|
63 | }
|
64 |
|
65 | void Delay(void)
|
66 | {
|
67 | int j;
|
68 | for (j = 0; j < 10000; j++ );
|
69 | }
|
70 |
|
71 | /* return TRUE if the RTC is OK */
|
72 | int TestRTC(void)
|
73 | {
|
74 | int timeout = 1000000;
|
75 | RTC_CCR = 0;
|
76 | RTC_SEC = 0x00;
|
77 | RTC_CCR = 1<<1; /* clear the counter */
|
78 | RTC_CCR = 1<<0 | 1<<4; /* enable the clock using the external xtal */
|
79 |
|
80 | while (timeout--);
|
81 | timeout = RTC_CTC;
|
82 |
|
83 | if ((1550 < timeout) & (timeout < 1600)) return TRUE;
|
84 |
|
85 | return FALSE;
|
86 | }
|
87 |
|
88 | int main (void) {
|
89 |
|
90 | int j; // loop counter (stack variable)
|
91 | char chr = 'h';
|
92 | InitPLL();
|
93 |
|
94 | SCS |= 1<<0; /* enable fast IO on ports 0&1 */
|
95 |
|
96 | InitLeds();
|
97 | InitUART0();
|
98 | InitUART0Interrupt();
|
99 |
|
100 | SetLed(STAT1, LED_OFF);
|
101 | for (j = 0; j < 200000; j++);
|
102 |
|
103 | SetLed(STAT2, LED_ON);
|
104 | for (j = 0; j < 100000; j++ );
|
105 | SetLed(STAT2, LED_OFF);
|
106 |
|
107 | SendString("=======================\n");
|
108 | SendString("Hello World.\n");
|
109 | SendString("=======================\n");
|
110 |
|
111 | while (1) {
|
112 |
|
113 | for (j = 0; j < 100000; j++ )
|
114 | {
|
115 | if (But2Pressed() == TRUE) SetLed(STAT2, LED_ON);
|
116 | if (But1Pressed() == TRUE) SetLed(STAT2, LED_OFF);
|
117 | }
|
118 |
|
119 | SetLed(STAT1, LED_TOGGLE);
|
120 | //SendString("Test output ...\n");
|
121 | //SendChar(ReadChar());
|
122 | }
|
123 | }
|
124 |
|
125 | void InitPLL(void)
|
126 | {
|
127 |
|
128 | volatile unsigned long MValue;
|
129 | volatile unsigned long NValue;
|
130 |
|
131 | if ( PLLSTAT & (1 << 25) )
|
132 | {
|
133 | PLLCON = 1; /* Enable PLL, disconnected */
|
134 | PLLFEED = 0xaa;
|
135 | PLLFEED = 0x55;
|
136 | }
|
137 |
|
138 | PLLCON = 0; /* Disable PLL, disconnected */
|
139 | PLLFEED = 0xaa;
|
140 | PLLFEED = 0x55;
|
141 |
|
142 | SCS |= 0x20; /* Enable main OSC */
|
143 | while( !(SCS & 0x40) ); /* Wait until main OSC is usable */
|
144 |
|
145 | CLKSRCSEL = 0x1; /* select main OSC, 12MHz, as the PLL clock source */
|
146 |
|
147 | PLLCFG = PLL_MValue | (PLL_NValue << 16);
|
148 | PLLFEED = 0xaa;
|
149 | PLLFEED = 0x55;
|
150 |
|
151 | PLLCON = 1; /* Enable PLL, disconnected */
|
152 | PLLFEED = 0xaa;
|
153 | PLLFEED = 0x55;
|
154 |
|
155 | CCLKCFG = CCLKDivValue; /* Set clock divider */
|
156 |
|
157 | while ( ((PLLSTAT & (1 << 26)) == 0) ); /* Check lock bit status */
|
158 |
|
159 | MValue = PLLSTAT & 0x00007FFF;
|
160 | NValue = (PLLSTAT & 0x00FF0000) >> 16;
|
161 | while ((MValue != PLL_MValue) && ( NValue != PLL_NValue) );
|
162 |
|
163 | PLLCON = 3; /* enable and connect */
|
164 | PLLFEED = 0xaa;
|
165 | PLLFEED = 0x55;
|
166 | while ( ((PLLSTAT & (1 << 25)) == 0) ); /* Check connect bit status */
|
167 | }
|
168 |
|
169 | void SetLed(int led, int state)
|
170 | {
|
171 | if (led == STAT1)
|
172 | {
|
173 | switch (state)
|
174 | {
|
175 | case LED_OFF:
|
176 | FIO4SET |= 1<<17;
|
177 | break;
|
178 |
|
179 | case LED_ON:
|
180 | FIO4CLR |= 1<<17;
|
181 | break;
|
182 |
|
183 | case LED_TOGGLE:
|
184 | if (FIO4PIN & (1<<17) )
|
185 | {
|
186 | SetLed(led, LED_ON);
|
187 | }
|
188 | else
|
189 | {
|
190 | SetLed(led, LED_OFF);
|
191 | }
|
192 | break;
|
193 | default:
|
194 | break;
|
195 | }
|
196 | }
|
197 | else if (led == STAT2)
|
198 | {
|
199 | switch (state)
|
200 | {
|
201 | case LED_OFF:
|
202 | FIO4SET |= 1<<16;
|
203 | break;
|
204 |
|
205 | case LED_ON:
|
206 | FIO4CLR |= 1<<16;
|
207 | break;
|
208 |
|
209 | case LED_TOGGLE:
|
210 | if (FIO4PIN & (1<<16) )
|
211 | {
|
212 | SetLed(led, LED_ON);
|
213 | }
|
214 | else
|
215 | {
|
216 | SetLed(led, LED_OFF);
|
217 | }
|
218 | break;
|
219 | default:
|
220 | break;
|
221 | }
|
222 | }
|
223 | }
|
224 |
|
225 | void InitLeds(void)
|
226 | {
|
227 | SetLed(STAT1, LED_OFF);
|
228 | SetLed(STAT2, LED_OFF);
|
229 |
|
230 | FIO4DIR |= 1<<17; /* STAT1&2 as out */
|
231 | FIO4DIR |= 1<<16;
|
232 | }
|
233 |
|
234 | /* Stubs for various interrupts (may be replaced later) */
|
235 | /* ---------------------------------------------------- */
|
236 |
|
237 | void IRQ_Routine (void) {
|
238 | while (1) ;
|
239 | }
|
240 |
|
241 | void FIQ_Routine (void) {
|
242 | while (1) ;
|
243 | }
|
244 |
|
245 |
|
246 | void SWI_Routine (void) {
|
247 | while (1) ;
|
248 | }
|
249 |
|
250 |
|
251 | void UNDEF_Routine (void) {
|
252 | while (1) ;
|
253 | }
|