-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgreenStripLED.c
364 lines (285 loc) · 9.1 KB
/
greenStripLED.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
/*----------------------------------------------------------------------------
* CMSIS-RTOS 'main' function template
*---------------------------------------------------------------------------*/
#include "MKL25Z4.h"
#include "RTE_Components.h"
#include CMSIS_device_header
#include "cmsis_os2.h"
#define MASK(x) (1 << (x))
#define RED_LED 18 // PortB Pin 18
#define GREEN_LED 19 // PortB Pin 19
#define BLUE_LED 1 // PortD Pin 1
#define TOP_LEFT_WHEEL_1 30 // PORT E pin 30 CH2
#define TOP_LEFT_WHEEL_2 29 // PORT E pin 29 CH3
#define IR_SENSOR 13 // PORT A pin 1
#define GREEN_1 7// port C pin 7
#define GREEN_2 0// port C pin 0
#define GREEN_3 3// port C pin 3
#define GREEN_4 4// port C pin 4
#define GREEN_5 5// port C pin 5
#define GREEN_6 6// port C pin 6
#define GREEN_7 10// port C pin 10
#define GREEN_8 11// port C pin 11
#define GREEN_9 12// port C pin 12
#define GREEN_10 13// port C pin 13
#define delayGreen 0x100 // for green strip how fast u wan
volatile unsigned int counter = 0;
/*----------------------------------------------------------------------------
* Application main thread
*---------------------------------------------------------------------------*/
void app_main (void *argument) {
// ...
for (;;) {}
}
void initIRSensor(void)
{
// enabile clock for port A
SIM->SCGC5 |= (SIM_SCGC5_PORTA_MASK);
// configure mux to GPIO
PORTA->PCR[IR_SENSOR] &= ~PORT_PCR_MUX_MASK;
// configure at falling edge
PORTA->PCR[IR_SENSOR] |= (PORT_PCR_MUX(1) | PORT_PCR_IRQC(0B1000));
//PORTE->PCR[IR_SENSOR] |= PORT_PCR_MUX(1);
// Set data direction registor for port A as input
PTA->PDDR &= ~MASK(IR_SENSOR);
// enable interupts
NVIC_SetPriority(PORTA_IRQn, 1);
NVIC_ClearPendingIRQ(PORTA_IRQn);
NVIC_EnableIRQ(PORTA_IRQn);
}
/* Delay Function */
static void delay(volatile uint32_t nof) {
while(nof!=0) {
__asm("NOP");
nof--;
}
}
void PORTA_IRQHandler() {
NVIC_ClearPendingIRQ(PORTA_IRQn);
counter = 1;
if(PORTA->ISFR & MASK(IR_SENSOR)) {
// ON LED on falling edge
PTB -> PCOR = MASK(RED_LED);
PTB ->PSOR = (MASK(GREEN_LED));
PTD ->PSOR = (MASK(BLUE_LED));
delay(0x80000);
}
PORTA->ISFR |= MASK(IR_SENSOR);
}
void initGreenStrip(void ) {
SIM->SCGC5 |= (SIM_SCGC5_PORTC_MASK);
PORTC->PCR[GREEN_1] &= ~PORT_PCR_MUX_MASK;
PORTC->PCR[GREEN_2] &= ~PORT_PCR_MUX_MASK;
PORTC->PCR[GREEN_3] &= ~PORT_PCR_MUX_MASK;
PORTC->PCR[GREEN_4] &= ~PORT_PCR_MUX_MASK;
PORTC->PCR[GREEN_5] &= ~PORT_PCR_MUX_MASK;
PORTC->PCR[GREEN_6] &= ~PORT_PCR_MUX_MASK;
PORTC->PCR[GREEN_7] &= ~PORT_PCR_MUX_MASK;
PORTC->PCR[GREEN_8] &= ~PORT_PCR_MUX_MASK;
PORTC->PCR[GREEN_9] &= ~PORT_PCR_MUX_MASK;
PORTC->PCR[GREEN_10] &= ~PORT_PCR_MUX_MASK;
PORTC->PCR[GREEN_1] |= PORT_PCR_MUX(1);
PORTC->PCR[GREEN_2] |= PORT_PCR_MUX(1);
PORTC->PCR[GREEN_3] |= PORT_PCR_MUX(1);
PORTC->PCR[GREEN_4] |= PORT_PCR_MUX(1);
PORTC->PCR[GREEN_5] |= PORT_PCR_MUX(1);
PORTC->PCR[GREEN_6] |= PORT_PCR_MUX(1);
PORTC->PCR[GREEN_7] |= PORT_PCR_MUX(1);
PORTC->PCR[GREEN_8] |= PORT_PCR_MUX(1);
PORTC->PCR[GREEN_9] |= PORT_PCR_MUX(1);
PORTC->PCR[GREEN_10] |= PORT_PCR_MUX(1);
// set as output
PTC->PDDR |= MASK(GREEN_1) | MASK(GREEN_2) | MASK(GREEN_3) | MASK(GREEN_4)
| MASK(GREEN_5) | MASK(GREEN_6) | MASK(GREEN_7) | MASK(GREEN_8)
| MASK(GREEN_9) | MASK(GREEN_10) ;
}
void initLED (void) {
// Enable Clock to PORTB and PORTD
SIM->SCGC5 |= ((SIM_SCGC5_PORTB_MASK) | (SIM_SCGC5_PORTD_MASK));
// Configure MUX settings to make all 3 pins GPIO
PORTB->PCR[RED_LED] &= ~PORT_PCR_MUX_MASK;
PORTB->PCR[RED_LED] |= PORT_PCR_MUX(1);
PORTB->PCR[GREEN_LED] &= ~PORT_PCR_MUX_MASK;
PORTB->PCR[GREEN_LED] |= PORT_PCR_MUX(1);
PORTD->PCR[BLUE_LED] &= ~PORT_PCR_MUX_MASK;
PORTD->PCR[BLUE_LED] |= PORT_PCR_MUX(1);
// Set Data Direction Registers for PortB and PortD
PTB->PDDR |= (MASK(RED_LED) | MASK(GREEN_LED));
PTD->PDDR |= MASK(BLUE_LED);
}
void offlled(void){
PTB ->PSOR = (MASK(RED_LED) | MASK(GREEN_LED));
PTD ->PSOR = (MASK(BLUE_LED));
}
void stationaryMode(void *argument) {
for(;;) {
PTC ->PSOR = (MASK(GREEN_1));
PTC ->PSOR = (MASK(GREEN_2));
PTC ->PSOR = (MASK(GREEN_3));
PTC ->PSOR = (MASK(GREEN_4));
PTC ->PSOR = (MASK(GREEN_5));
PTC ->PSOR = (MASK(GREEN_6));
PTC ->PSOR = (MASK(GREEN_7));
PTC ->PSOR = (MASK(GREEN_8));
PTC ->PSOR = (MASK(GREEN_9));
PTC ->PSOR = (MASK(GREEN_10));
osDelay(delayGreen);
}
}
void runMode(void *argument){
for(;;){
// on 1
PTC ->PSOR = (MASK(GREEN_1));
PTC ->PCOR = (MASK(GREEN_2));
PTC ->PCOR = (MASK(GREEN_3));
PTC ->PCOR = (MASK(GREEN_4));
PTC ->PCOR = (MASK(GREEN_5));
PTC ->PCOR = (MASK(GREEN_6));
PTC ->PCOR = (MASK(GREEN_7));
PTC ->PCOR = (MASK(GREEN_8));
PTC ->PCOR = (MASK(GREEN_9));
PTC ->PCOR = (MASK(GREEN_10));
osDelay(delayGreen);
// on 2
PTC ->PCOR = (MASK(GREEN_1));
PTC ->PSOR = (MASK(GREEN_2));
PTC ->PCOR = (MASK(GREEN_3));
PTC ->PCOR = (MASK(GREEN_4));
PTC ->PCOR = (MASK(GREEN_5));
PTC ->PCOR = (MASK(GREEN_6));
PTC ->PCOR = (MASK(GREEN_7));
PTC ->PCOR = (MASK(GREEN_8));
PTC ->PCOR = (MASK(GREEN_9));
PTC ->PCOR = (MASK(GREEN_10));
osDelay(delayGreen);
PTC ->PCOR = (MASK(GREEN_1));
PTC ->PCOR = (MASK(GREEN_2));
PTC ->PSOR = (MASK(GREEN_3));
PTC ->PCOR = (MASK(GREEN_4));
PTC ->PCOR = (MASK(GREEN_5));
PTC ->PCOR = (MASK(GREEN_6));
PTC ->PCOR = (MASK(GREEN_7));
PTC ->PCOR = (MASK(GREEN_8));
PTC ->PCOR = (MASK(GREEN_9));
PTC ->PCOR = (MASK(GREEN_10));
osDelay(delayGreen);
PTC ->PCOR = (MASK(GREEN_1));
PTC ->PCOR = (MASK(GREEN_2));
PTC ->PCOR = (MASK(GREEN_3));
PTC ->PSOR = (MASK(GREEN_4));
PTC ->PCOR = (MASK(GREEN_5));
PTC ->PCOR = (MASK(GREEN_6));
PTC ->PCOR = (MASK(GREEN_7));
PTC ->PCOR = (MASK(GREEN_8));
PTC ->PCOR = (MASK(GREEN_9));
PTC ->PCOR = (MASK(GREEN_10));
osDelay(delayGreen);
PTC ->PCOR = (MASK(GREEN_1));
PTC ->PCOR = (MASK(GREEN_2));
PTC ->PCOR = (MASK(GREEN_3));
PTC ->PCOR = (MASK(GREEN_4));
PTC ->PSOR = (MASK(GREEN_5));
PTC ->PCOR = (MASK(GREEN_6));
PTC ->PCOR = (MASK(GREEN_7));
PTC ->PCOR = (MASK(GREEN_8));
PTC ->PCOR = (MASK(GREEN_9));
PTC ->PCOR = (MASK(GREEN_10));
osDelay(delayGreen);
PTC ->PCOR = (MASK(GREEN_1));
PTC ->PCOR = (MASK(GREEN_2));
PTC ->PCOR = (MASK(GREEN_3));
PTC ->PCOR = (MASK(GREEN_4));
PTC ->PCOR = (MASK(GREEN_5));
PTC ->PSOR = (MASK(GREEN_6));
PTC ->PCOR = (MASK(GREEN_7));
PTC ->PCOR = (MASK(GREEN_8));
PTC ->PCOR = (MASK(GREEN_9));
PTC ->PCOR = (MASK(GREEN_10));
osDelay(delayGreen);
PTC ->PCOR = (MASK(GREEN_1));
PTC ->PCOR = (MASK(GREEN_2));
PTC ->PCOR = (MASK(GREEN_3));
PTC ->PCOR = (MASK(GREEN_4));
PTC ->PCOR = (MASK(GREEN_5));
PTC ->PCOR = (MASK(GREEN_6));
PTC ->PSOR = (MASK(GREEN_7));
PTC ->PCOR = (MASK(GREEN_8));
PTC ->PCOR = (MASK(GREEN_9));
PTC ->PCOR = (MASK(GREEN_10));
osDelay(delayGreen);
PTC ->PCOR = (MASK(GREEN_1));
PTC ->PCOR = (MASK(GREEN_2));
PTC ->PCOR = (MASK(GREEN_3));
PTC ->PCOR = (MASK(GREEN_4));
PTC ->PCOR = (MASK(GREEN_5));
PTC ->PCOR = (MASK(GREEN_6));
PTC ->PCOR = (MASK(GREEN_7));
PTC ->PSOR = (MASK(GREEN_8));
PTC ->PCOR = (MASK(GREEN_9));
PTC ->PCOR = (MASK(GREEN_10));
osDelay(delayGreen);
PTC ->PCOR = (MASK(GREEN_1));
PTC ->PCOR = (MASK(GREEN_2));
PTC ->PCOR = (MASK(GREEN_3));
PTC ->PCOR = (MASK(GREEN_4));
PTC ->PCOR = (MASK(GREEN_5));
PTC ->PCOR = (MASK(GREEN_6));
PTC ->PCOR = (MASK(GREEN_7));
PTC ->PCOR = (MASK(GREEN_8));
PTC ->PSOR = (MASK(GREEN_9));
PTC ->PCOR = (MASK(GREEN_10));
osDelay(delayGreen);
PTC ->PCOR = (MASK(GREEN_1));
PTC ->PCOR = (MASK(GREEN_2));
PTC ->PCOR = (MASK(GREEN_3));
PTC ->PCOR = (MASK(GREEN_4));
PTC ->PCOR = (MASK(GREEN_5));
PTC ->PCOR = (MASK(GREEN_6));
PTC ->PCOR = (MASK(GREEN_7));
PTC ->PCOR = (MASK(GREEN_8));
PTC ->PCOR = (MASK(GREEN_9));
PTC ->PSOR = (MASK(GREEN_10));
osDelay(delayGreen);
}
}
int main (void) {
// System Initialization
SystemCoreClockUpdate();
initGreenStrip();
// on all when stationary
//stationaryMode();
//delay(0x800000);
osKernelInitialize(); // Initialize CMSIS-RTOS
osThreadNew(runMode, NULL, NULL); // Create application main thread
osThreadNew(stationaryMode, NULL, NULL);
osKernelStart(); // Start thread execution
for (;;) {
}
//while (1)
//{
/*if(PTE -> PDIR & MASK(IR_SENSOR)) {
PTB -> PCOR = MASK(RED_LED);
PTB ->PSOR = (MASK(GREEN_LED));
PTD ->PSOR = (MASK(BLUE_LED));
} else {
PTD ->PSOR = (MASK(BLUE_LED));
PTB ->PSOR = (MASK(RED_LED));
PTB ->PSOR = (MASK(GREEN_LED));
}*/
//}
//TPM0_C2V = 0x0EA6;
//TPM0_C2V = 0x0EA6;
//delay(0x40000);
/**
// Configure MUX settings to make all 3 pins GPIO
PORTE->PCR[TOP_LEFT_WHEEL_1] &= ~PORT_PCR_MUX_MASK;
PORTE->PCR[TOP_LEFT_WHEEL_1] |= PORT_PCR_MUX(1);
// Set Data Direction Registers for PortB and PortD
PTE->PDDR |= (MASK(TOP_LEFT_WHEEL_1) );
PTE ->PCOR = MASK(TOP_LEFT_WHEEL_1);
delay(0x800000);
//enable pin output and set mux TO 3 to use alt 3 for time in port b 01 n 1
PORTE->PCR[TOP_LEFT_WHEEL_1] &= ~PORT_PCR_MUX_MASK;
PORTE->PCR[TOP_LEFT_WHEEL_1] |= PORT_PCR_MUX(3);
**/
}