-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLPC11U00API.c
446 lines (351 loc) · 10.7 KB
/
LPC11U00API.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include <LPC11U00API.h>
#include <LPC11U00.h>
#include <string.h>
#include <stdlib.h>
#define HARDWARE_EXTERNAL_CRYSTAL_FREQUENCY (12000000UL)
static struct LPC11U00APIState
{
unsigned int t1Millisecond;
unsigned int t8Microseconds;
} gLPC11U00APIState;
static void System_strobeClockUpdateEnable(u32 volatile* const updateEnable)
{
*updateEnable = 1;
*updateEnable = 0;
*updateEnable = 1;
while(!(*updateEnable & 1)); //spinwait for the change to take effect before returning
}
static unsigned int System_getPLLInputFrequency(void)
{
switch(*PLLSource)
{
default:
case PLLSource_InternalCrystal:
return(12000000UL);
case PLLSource_ExternalClock:
return(HARDWARE_EXTERNAL_CRYSTAL_FREQUENCY);
}
}
static unsigned int System_getPLLOutputFrequency(void)
{
//the PLL on the LPC1xxx parts isn't very well designed, as the fixed relationship between
// FCLKOUT, the M-divider and FCLKIN means that the output is always an integer multiple
// of the input. A better design would use relationship: Fcco / M = Fin; Fout = Fcco / D.
// (The LPC1xxx uses relationship: Fcco / D / M = Fin; Fout = Fcco / D.)
return(System_getPLLInputFrequency() * ((*PLLControl & PLLControl_MultiplierBitsMask) + 1));
}
unsigned int SystemGetMainClockFrequency(void)
{
switch(*MainClockSource)
{
default:
case MainClockSource_InternalCrystal:
return(12000000UL);
case MainClockSource_PLLInput:
return(System_getPLLInputFrequency());
case MainClockSource_WDTOscillator:
return(3400000UL); //@@todo: confirm this is a reliable figure
case MainClockSource_PLLOutput:
return(System_getPLLOutputFrequency());
}
}
//this is also the AHB (main high-speed bus) frequency
unsigned int SystemGetCoreFrequency(void)
{
//note no /0 check, but I would be very alarmed if this code could excecute with a stopped core :-/
return(SystemGetMainClockFrequency() / *MainBusDivider);
}
void SystemSetupSystemPLL(void)
{
//Note LPC1*** PLLs have transfer function H(f) = f * ((M + 1) / 2^^(P + 1))
//ensure the internal crystal is on
*PowerDownControl &= ~(PowerDownControl_InternalCrystalOutput | PowerDownControl_InternalCrystal);
*MainClockSource = MainClockSource_InternalCrystal;
System_strobeClockUpdateEnable(MainClockSourceUpdate);
//ensure the external crystal is on
*PowerDownControl &= ~PowerDownControl_SystemOscillator;
//wait 200 clocks for it to stabilize
for(int i = 0; i < 200; i++)
__asm__ volatile ("nop"::);
//turn on the PLL
*PowerDownControl &= ~PowerDownControl_SystemPLL;
*PLLSource = PLLSource_ExternalClock;
System_strobeClockUpdateEnable(PLLSourceUpdate);
//no: M = 4, P = 2 (m = 3, p = 1)
//48MHz:
*PLLControl = ((4) - 1) | (((1) - 1) << PLLControl_DividerBitsShift);
//wait for lock
while(!(*PLLStatus & PLLStatus_Locked));
//run the core from the PLL with this divider:
*MainBusDivider = 1;
*MainClockSource = MainClockSource_PLLOutput;
System_strobeClockUpdateEnable(MainClockSourceUpdate);
gLPC11U00APIState.t1Millisecond = SystemGetCoreFrequency() / 18000UL;
gLPC11U00APIState.t8Microseconds = SystemGetCoreFrequency() / 6000000UL; // @@needs adjustment
}
void SystemSetupUSBPLL(void)
{
//ensure the necessary analog components are on
*PowerDownControl &= ~(PowerDownControl_SystemOscillator | PowerDownControl_USBPLL | PowerDownControl_USBPins);
//disconnect the clocks while we update
*ClockControl &= ~(ClockControl_USB | ClockControl_USBRAM);
*USBClockDivider = 0;
*USBPLLSource = USBPLLSource_ExternalClock;
System_strobeClockUpdateEnable(USBPLLSourceUpdate);
//generate the required 48MHz
*USBPLLControl = ((4) - 1) | (((1) - 1) << USBPLLControl_DividerBitsShift);
//wait for PLL lock
while(!(*USBPLLStatus & USBPLLStatus_Locked));
*USBClockSource = USBClockSource_USBPLL_Output;
System_strobeClockUpdateEnable(USBClockSourceUpdate);
//clock and power on the parts
*USBClockDivider = 1;
*ClockControl |= (ClockControl_USB | ClockControl_USBRAM);
}
int NumberFormatterFormat(char* output, unsigned int number, int fractionBits, NumberFormatterBase base)
{
(void)fractionBits; // not currently supported
static char const charTable[] = "0123456789ABCDEF";
char buf[11];
int len = 0;
int neg = 0;
if(base == NumberFormatter_DecimalSigned)
{
base = NumberFormatter_DecimalUnsigned;
if(((int)number) < 0)
{
neg = 1;
number = -number;
}
}
else if(base == NumberFormatter_Character)
{
*output++ = (unsigned char)number;
return(1);
}
do
{
LPCuidivResult r = (*LPC11U00ROMAPI)->divider->uidivmod(number, (unsigned int)base);
buf[len++] = charTable[r.remainder];
number = r.quotient;
}
while((len < 10) && (number != 0));
if(neg)
buf[len++] = '-';
for(int i = len; i > 0;)
*output++ = buf[--i];
return(len);
}
int CircularBuffer_L(CircularBuffer* cb)
{
return(cb->_p[0]);
}
unsigned char* CircularBuffer_T(CircularBuffer* cb)
{
return((((unsigned char*)cb->_p) + (3 * sizeof(unsigned short))) + cb->_p[1]);
}
int CircularBuffer_U(CircularBuffer* cb)
{
return(cb->_p[2]);
}
unsigned char* CircularBuffer_h(CircularBuffer* cb)
{
return(CircularBuffer_T(cb) + cb->_p[2] - (((cb->_p[1] + cb->_p[2]) >= cb->_p[0])? cb->_p[0] : 0));
}
int CircularBuffer_f(CircularBuffer* cb)
{
return(cb->_p[0] - cb->_p[2]);
}
int CircularBuffer_tb(CircularBuffer* cb)
{
return(((cb->_p[1] + cb->_p[2]) < cb->_p[0])? cb->_p[2] : (cb->_p[0] - cb->_p[1]));
}
int CircularBuffer_hb(CircularBuffer* cb)
{
return(((cb->_p[1] + cb->_p[2]) < cb->_p[0])? (cb->_p[0] - cb->_p[2] - cb->_p[1]) : (cb->_p[0] - cb->_p[2]));
}
void CircularBuffer_Tincrement(CircularBuffer* cb, int increment)
{
int v = cb->_p[1] + increment;
if(v >= cb->_p[0]) v -= cb->_p[0];
cb->_p[1] = v;
cb->_p[2] -= increment;
}
void CircularBuffer_hincrement(CircularBuffer* cb, int increment)
{
cb->_p[2] += increment;
}
int CircularBufferInit(CircularBuffer* cb, int bufferSize)
{
CircularBufferDeinit(cb);
// crop to size for this implementation. This is adjustable
bufferSize = (unsigned short)bufferSize;
// operator new may return 0 in this ABI
interruptsDisabled();
cb->_p = (unsigned short*)malloc((3 * sizeof(unsigned short)) + bufferSize);
if(cb->_p == 0)
return(0);
((unsigned short*)cb->_p)[0] = bufferSize;
((unsigned short*)cb->_p)[1] = 0;
((unsigned short*)cb->_p)[2] = 0;
interruptsEnabled();
return(1);
}
void CircularBufferDeinit(CircularBuffer* cb)
{
interruptsDisabled();
if(cb->_p != 0)
{
free(cb->_p);
cb->_p = 0;
}
interruptsEnabled();
}
void CircularBufferReset(CircularBuffer* cb)
{
interruptsDisabled();
cb->_p[2] = 0;
interruptsEnabled();
}
int CircularBufferRead(CircularBuffer* cb, unsigned char* out, int length)
{
interruptsDisabled();
if(length < 0) length = 0;
int count = 0, b2, b1 = CircularBuffer_tb(cb);
if(length < b1) b1 = length;
if(out != 0)
memcpy(out, CircularBuffer_T(cb), b1);
CircularBuffer_Tincrement(cb, b1);
out += b1;
length -= b1;
count += b1;
if((length > 0) && ((b2 = CircularBuffer_tb(cb)) > 0))
{
if(length < b2) b2 = length;
if(out != 0)
memcpy(out, CircularBuffer_T(cb), b2);
count += b2;
CircularBuffer_Tincrement(cb, b2);
}
interruptsEnabled();
return(count);
}
unsigned char CircularBufferReadByte(CircularBuffer* cb)
{
interruptsDisabled();
unsigned char b = 0;
if(CircularBuffer_tb(cb) > 0)
{
b = *CircularBuffer_T(cb);
CircularBuffer_Tincrement(cb, 1);
}
interruptsEnabled();
return(b);
}
int CircularBufferWrite(CircularBuffer* cb, unsigned char const* in, int length)
{
interruptsDisabled();
if(length < 0) length = 0;
int count = 0, b2, b1 = CircularBuffer_hb(cb);
if(length < b1) b1 = length;
memcpy(CircularBuffer_h(cb), in, b1);
CircularBuffer_hincrement(cb, b1);
in += b1;
length -= b1;
count += b1;
if((length > 0) && ((b2 = CircularBuffer_hb(cb)) > 0))
{
if(length < b2) b2 = length;
memcpy(CircularBuffer_h(cb), in, b2);
count += b2;
CircularBuffer_hincrement(cb, b2);
}
interruptsEnabled();
return(count);
}
int CircularBufferWriteByte(CircularBuffer* cb, unsigned char b)
{
int v = 0;
interruptsDisabled();
if(CircularBuffer_hb(cb) > 0)
{
*CircularBuffer_h(cb) = b;
CircularBuffer_hincrement(cb, 1);
v = 1;
}
interruptsEnabled();
return(v);
}
void SystemDelayMs(unsigned int ms)
{
while(ms--)
for(unsigned int i = gLPC11U00APIState.t1Millisecond; i; i--)
__asm__ volatile ("nop \n nop \n nop \n nop \n" ::);
}
void UARTStart(unsigned int baudRate, unsigned int mode)
{
if(baudRate > 0)
{
*ClockControl |= ClockControl_UART;
*UARTClockDivider = 1;
*UARTLineControl = (mode & 0xFF) | UARTLineControl_DivisorLatch; //apply mode and enter DLAB state
unsigned int divider = SystemGetCoreFrequency() / (16 * baudRate);
*UARTDivisorLow = divider & 0xFF; //write divisor bytes
*UARTDivisorHigh = (divider >> 8) & 0xFF;
*UARTLineControl = (mode & 0xFF); //exit DLAB state
*UARTModemControl = (mode >> 8) & 0xC7;
*UARTFIFOControl |= (UARTFIFOControl_Enable | UARTFIFOControl_RxReset | UARTFIFOControl_TxReset); //| UARTFIFOControl_RxInterrupt1Char);
(void)*UARTLineStatus; //clear status
//TXD in UART mode, no pullup/down
*IOConfigPIO0_18 = (*IOConfigPIO0_18 & ~IOConfigPIO_FunctionMask) | IOConfigPIO0_18_Function_RXD;
*IOConfigPIO0_19 = (*IOConfigPIO0_19 & ~IOConfigPIO_FunctionMask) | IOConfigPIO0_19_Function_TXD;
}
else
{
*IOConfigPIO0_18 = (*IOConfigPIO0_18 & ~IOConfigPIO_FunctionMask);
*IOConfigPIO0_19 = (*IOConfigPIO0_19 & ~IOConfigPIO_FunctionMask);
*UARTFIFOControl = (UARTFIFOControl_RxReset | UARTFIFOControl_TxReset); //disable and reset
*ClockControl &= ~ClockControl_UART;
}
}
void UARTWriteStringLengthSync(char const* msg, int length)
{
while(((length == -1) && *msg) || (length-- > 0))
{
while(!(*UARTLineStatus & UARTLineStatus_TxHoldingRegisterEmpty));
*UARTData = *msg++;
}
}
void UARTWriteStringSync(char const* msg)
{
UARTWriteStringLengthSync(msg, -1);
}
void UARTWriteIntSync(unsigned int value, NumberFormatterBase base)
{
char buffer[34];
int len = NumberFormatterFormat(buffer, value, 0, base);
char* b = buffer;
while(len--)
{
while(!(*UARTLineStatus & UARTLineStatus_TxHoldingRegisterEmpty));
*UARTData = *b++;
}
}
void UARTWriteHexIntSync(unsigned int value, int minDigits)
{
int length = (value >= 0x10000)? (value >= 0x1000000)? 4 : 3 : (value >= 0x100)? 2 : 1;
value = __builtin_bswap32(value);
UARTWriteHexDumpSync(((unsigned char*)&value) + (4 - length), (length > minDigits)? length : minDigits);
}
void UARTWriteHexDumpSync(unsigned char const* data, unsigned int length)
{
static char const* hex = "0123456789ABCDEF";
while(length--)
{
unsigned char b = *data++;
while(!(*UARTLineStatus & UARTLineStatus_TxHoldingRegisterEmpty));
*UARTData = hex[b >> 4];
while(!(*UARTLineStatus & UARTLineStatus_TxHoldingRegisterEmpty));
*UARTData = hex[b & 0xF];
}
}