-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
468 lines (415 loc) · 11.7 KB
/
main.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/******************************************************************************
*
* Copyright:
* (C) 2000 - 2005 Embedded Artists AB
*
* Description:
* Sample application that demonstrates how to create processes.
*
*****************************************************************************/
#include "pre_emptive_os/api/osapi.h"
#include "pre_emptive_os/api/general.h"
#include <printf_P.h>
#include <ea_init.h>
#include <lpc2xxx.h>
#include <consol.h>
#define PROC1_STACK_SIZE 2048
#define PROC2_STACK_SIZE 2048
#define PROC3_STACK_SIZE 2048
#define PROC4_STACK_SIZE 2048
#define PROC5_STACK_SIZE 2048
#define INIT_STACK_SIZE 400
#define BUZZ_PIN (1<<21)
#define CRYSTAL_FREQUENCY FOSC
#define PLL_FACTOR PLL_MUL
#define VPBDIV_FACTOR PBSD
#define SPI_CS 0x00008000 //<= new board, old board = 0x00800000
const tU8 matrixSymbol[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, // caly zapelniony
0x00,0x00,0x00,0x00,0x00,0x00,0x00};
static tU8 proc1Stack[PROC1_STACK_SIZE];
static tU8 proc2Stack[PROC2_STACK_SIZE];
static tU8 proc3Stack[PROC3_STACK_SIZE];
static tU8 proc4Stack[PROC4_STACK_SIZE];
static tU8 proc5Stack[PROC5_STACK_SIZE];
static tU8 initStack[INIT_STACK_SIZE];
static tU8 pid1;
static tU8 pid2;
static tU8 pid3;
static tU8 pid4;
static tU8 pid5;
static void proc1(void* arg);
static void proc2(void* arg);
static void proc3(void* arg);
static void proc4(void* arg);
static void proc5(void* arg);
static void initProc(void* arg);
//void testLedMatrix(void);
void testLcd(void);
//void testMotor(void);
void testMotor(int);
void testRGB(void);
void testI2C(void);
void testAdc(void);
void testJoy(void);
/*****************************************************************************
*
* Description:
* The first function to execute
*
****************************************************************************/
int main(void) {
tU8 error;
tU8 pid;
//immediately turn off buzzer (if connected)
IODIR0 |= 0x00000080;
IOSET0 = 0x00000080;
osInit();
osCreateProcess(initProc, initStack, INIT_STACK_SIZE, &pid, 1, NULL, &error);
osStartProcess(pid, &error);
osStart();
return 0;
}
/*****************************************************************************
*
* Description:
* A process entry function
*
* Params:
* [in] arg - This parameter is not used in this application.
*
****************************************************************************/
static void proc1(void* arg) {
tU8 centerReleased = TRUE;
tU8 i;
tU32 pattern;
tU8 error;
for (;;) {
printf(
"\n\n\n\n\n*******************************************************\n");
printf("* LPC2148 Education Board... *\n");
printf("*******************************************************\n");
//
//Test LEDs
//
// PINSEL0 &= 0x0000ffff;
// IODIR0 |= 0x0000ff00;
// IOSET0 = 0x0000fb00;
// IOCLR0 = 0x00000400;
// for(i=0; i<4; i++)
// {
// pattern = 0x00000100;
// while(pattern <= 0x00008000)
// {
// IOCLR = pattern;
// osSleep(20);
// IOSET0 = pattern;
// IOCLR0 = 0x00000400; //P0.10 must always be low = LED on since motor control else will be enables
//
// pattern <<= 1;
// }
// }
// //flash with P0.10 (with P0.8 and P0.9 low)
// IOCLR0 = 0x00000700;
// osSleep(20);
// IOSET0 = 0x00000400;
// osSleep(20);
// IOCLR0 = 0x00000400;
// osSleep(20);
// IOSET0 = 0x00000400;
// osSleep(20);
// IOCLR0 = 0x00000400;
// osSleep(20);
// IOSET0 = 0x00000400;
//
//
// //
// //Test EEPROM via I2C
// //
// testI2C();
//
//Start the rest of the processes
//
// osCreateProcess(proc2, proc2Stack, PROC2_STACK_SIZE, &pid2, 3, NULL, &error); // led matrix
// osStartProcess(pid2, &error);
osCreateProcess(proc2, proc2Stack, PROC2_STACK_SIZE, &pid2, 3, NULL,
&error); // led matrix
osStartProcess(pid2, &error);
osCreateProcess(proc3, proc3Stack, PROC3_STACK_SIZE, &pid3, 3, NULL,
&error); // lcd
osStartProcess(pid3, &error);
// osCreateProcess(proc4, proc4Stack, PROC4_STACK_SIZE, &pid4, 3, NULL, &error); // silnik i rgb
// osStartProcess(pid4, &error);
// osCreateProcess(proc5, proc5Stack, PROC5_STACK_SIZE, &pid5, 3, NULL, &error); // adc ?
// osStartProcess(pid5, &error);
//wait for a short while
osSleep(100);
IOPIN &= ~0x001f0000;
for (;;) {
/*
if ((IOPIN & 0x00100000) == 0)
HID_SendReport(0,0,10);
else if ((IOPIN & 0x00080000) == 0)
HID_SendReport(0,-10,0);
else if ((IOPIN & 0x00040000) == 0)
HID_SendReport(0,10,0);
else if ((IOPIN & 0x00020000) == 0)
HID_SendReport(0,0,-10);
*/
if ((IOPIN & 0x00010000) == 0) {
if (centerReleased == TRUE) {
// HID_SendReport(1,0,0);
centerReleased = FALSE;
}
} else {
if (centerReleased == FALSE)
// HID_SendReport(0,0,0);
centerReleased = TRUE;
}
osSleep(2);
}
}
}
static void led(int i) {
tU32 pattern;
//
//Test LEDs
//
printf("h = %d", i);
if (i > 8 || i < 0)
i = 0;
PINSEL0 &= 0x0000ffff;
IODIR0 |= 0x0000ff00;
IOSET0 = 0x0000fb00;
IOCLR0 = 0x00000400;
pattern = 256;
int j;
for (j = 0; j < 8; j++) {
printf("pattern = %d", pattern);
IOCLR = pattern;
// osSleep(100);
// IOSET0 = pattern;
// IOCLR0 = 0x00000400; //P0.10 must always be low = LED on since motor control else will be enables
pattern <<= 1;
}
pattern = 256;
for (j = 0; j < i; j++) {
// IOCLR = pattern;
// osSleep(100);
IOSET0 = pattern;
// IOCLR0 = 0x00000400; //P0.10 must always be low = LED on since motor control else will be enables
pattern <<= 1;
}
// //flash with P0.10 (with P0.8 and P0.9 low)
// IOCLR0 = 0x00000700;
// osSleep(20);
// IOSET0 = 0x00000400;
// osSleep(20);
// IOCLR0 = 0x00000400;
// osSleep(20);
// IOSET0 = 0x00000400;
// osSleep(20);
// IOCLR0 = 0x00000400;
// osSleep(20);
// IOSET0 = 0x00000400;
}
static void
startTimer1(tU16 delayInMs)
{
//initialize VIC for Timer1 interrupts
VICIntSelect &= ~0x20; //Timer1 interrupt is assigned to IRQ (not FIQ)
VICVectAddr5 = (tU32)ledMatrix; //register ISR address
VICVectCntl5 = 0x25; //enable vector interrupt for timer1
VICIntEnable = 0x20; //enable timer1 interrupt
//initialize and start Timer #0
T1TCR = 0x00000002; //disable and reset Timer1
T1PC = 0x00000000; //no prescale of clock
T1MR0 = delayInMs * //calculate no of timer ticks
((CRYSTAL_FREQUENCY * PLL_FACTOR) / (1000 * VPBDIV_FACTOR));
T1IR = 0x000000ff; //reset all flags before enable IRQs
T1MCR = 0x00000003; //reset counter and generate IRQ on MR0 match
T1TCR = 0x00000001; //start Timer1
}
void
testLedMatrix(int speed)
{
tU8 cntA = 0;
PINSEL0 |= 0x00001500 ; //Initiering av SPI
SPI_SPCCR = 0x08;
SPI_SPCR = 0x60;
IODIR0 |= SPI_CS;
startTimer1(2);
for(;;)
{
cntA++;
if (cntA > sizeof(eaText)-speed)
break;
#if 0
pattern[0] = eaText[cntA+0];
pattern[1] = eaText[cntA+1];
pattern[2] = eaText[cntA+2];
pattern[3] = eaText[cntA+3];
pattern[4] = eaText[cntA+4];
pattern[5] = eaText[cntA+5];
pattern[6] = eaText[cntA+6];
pattern[7] = eaText[cntA+7];
#endif
}
}
/*****************************************************************************
*
* Description:
* A process entry function
*
* Params:
* [in] arg - This parameter is not used in this application.
*
****************************************************************************/
static void proc2(void* arg) {
// testJoy();
tU32 oldIOPIN, tmp;
/* read input from joystick */
IODIR &= ~0x001f0000;
messageOnLCD("Joystick works!", TRUE);
/* joystick support */
tU32 oldMove;
int i = 0, j = 0, h = 0;
for (;;) {
// printf("IOPIN = %d",(IOPIN));
oldIOPIN = IOPIN;
osSleep(10);
if (IOPIN == 1589624063) {
printf("work");
}
tmp = (oldIOPIN & ~IOPIN);// & 0x001f0000;
printf("tmp = %d", tmp);
printf("\n");
// if( oldMove != tmp)
// oldMove = tmp;`
// if(tmp != 0x00000000) {
// if(tmp == 0x00020000) {
// messageOnLCD("Gora",TRUE);
// }
// else if(tmp == 0x00040000) messageOnLCD("Prawo",TRUE);
// else if(tmp == 0x00100000)
// {
// messageOnLCD("Dol",TRUE);
// }
// else if(tmp == 0x00080000) messageOnLCD("Lewo",TRUE);
// else if(tmp == 0x00010000) messageOnLCD("Center",TRUE);
// }
// if(tmp != 0) {
if (tmp == 131072)
{
messageOnLCD("up", TRUE);
if(h<8)
h++;
testLedMatrix(h);
led(h);
// IODIR0 |= BUZZ_PIN; //Select the Buzzer pin as Output
// IOPIN0 &= ~(BUZZ_PIN); //Clear the Buzzer pin
//
// while(1)
// {
// IOPIN0 |= BUZZ_PIN; //Set the Buzzer pin High
// osSleep(25);
// //_DelayMs(250); //A Delay
// IOPIN0 &= ~BUZZ_PIN; //Set the Buzzer pin Low
// //_DelayMs(250); //A Delay
// osSleep(25);
// }
}
else if (tmp == 262144) {
messageOnLCD("right", TRUE);
} else if (tmp == 16384) {
messageOnLCD("button", TRUE);
} else if (tmp == 524288) {
messageOnLCD("left", TRUE);
} else if (tmp == 65536) {
messageOnLCD("Center", TRUE);
testMotor(0);
} else if (tmp == 1048576)
messageOnLCD("down", TRUE);
if(h>0)
h--;
testLedMatrix(h);
led(h);
// }
// else
// {
// messageOnLCD("NIC",TRUE);
// }
}
osDeleteProcess();
}
/*****************************************************************************
*
* Description:
* A process entry function
*
* Params:
* [in] arg - This parameter is not used in this application.
*
****************************************************************************/
static void proc3(void* arg) {
//testLcd();
// testLedMatrix();
}
/*****************************************************************************
*
* Description:
* A process entry function
*
* Params:
* [in] arg - This parameter is not used in this application.
*
****************************************************************************/
static void proc4(void* arg) {
for (;;) {
testRGB();
// testMotor();
}
}
/*****************************************************************************
*
* Description:
* A process entry function
*
* Params:
* [in] arg - This parameter is not used in this application.
*
****************************************************************************/
static void proc5(void* arg) {
testAdc();
}
/*****************************************************************************
*
* Description:
* The entry function for the initialization process.
*
* Params:
* [in] arg - This parameter is not used in this application.
*
****************************************************************************/
static void initProc(void* arg) {
tU8 error;
eaInit();
osCreateProcess(proc1, proc1Stack, PROC1_STACK_SIZE, &pid1, 3, NULL, &error);
osStartProcess(pid1, &error);
osDeleteProcess();
}
/*****************************************************************************
*
* Description:
* The timer tick entry function that is called once every timer tick
* interrupt in the RTOS. Observe that any processing in this
* function must be kept as short as possible since this function
* execute in interrupt context.
*
* Params:
* [in] elapsedTime - The number of elapsed milliseconds since last call.
*
****************************************************************************/
void appTick(tU32 elapsedTime) {
}