This repository has been archived by the owner on Oct 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
387 lines (343 loc) · 10 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
#include "RTE_Components.h"
#include CMSIS_device_header
#include "cmsis_os2.h"
#define BAUD_RATE 9600
#define UART_RX_PORTE23 23
#define MASK(x) (1 << (x))
#define LEFT_PIN0 0
#define LEFT_PIN1 2
#define RIGHT_PIN0 3
#define RIGHT_PIN1 5
#define FREQ_TO_MOD(x) (375000/x) //48000000 / 128 = 375000
#define MUSIC_PIN 0
#define RED_LED 16
int GREEN_LED[] = {7, 0, 3, 4, 5, 6, 10, 11, 12, 13};
int musical_notes[] = {0, 262, 294, 330, 349, 392, 440, 494, 523, 587, 659, 698, 784, 880, 988, 1046, 1175, 1318, 1397, 1568, 1760, 1976, 554, 622, 740, 831, 932};
int begin_music[] = {8, 9, 10, 8, 8, 9, 10, 8, 10, 11, 12, 12, 10, 11, 12, 12};
int special_music[] = {10, 10, 10, 10, 10, 10, 10, 10,
9, 9, 9, 9, 9, 9, 9, 9,
8, 8, 8, 8, 8, 8, 8, 8,
7, 7, 7, 7, 7, 7, 7, 7,
6, 6, 6, 6, 6, 6, 6, 6,
5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6,
7, 7, 7, 7, 7, 7, 7, 7,
8, 8, 7, 7, 8, 8, 3, 3,
5, 5, 5, 5, 6, 6, 7, 7,
8, 8, 7, 7, 8, 8, 3, 3,
12, 12, 10, 10, 12, 12, 13, 13,
11, 11, 10, 10, 9, 9, 11, 11,
10, 10, 9, 9, 8, 8, 7, 7,
6, 6, 5, 5, 4, 4, 8, 8,
7, 7, 5, 5, 8, 8, 7, 7,
12, 12, 10, 11, 12, 12, 10, 11,
12, 5, 6, 7, 8, 9, 10, 11,
10, 10, 8, 9, 10, 10, 3, 4,
5, 6, 5, 3, 5, 8, 7, 8,
6, 6, 8, 7, 6, 6, 5, 4,
5, 4, 3, 4, 5, 6, 7, 8,
6, 6, 8, 7, 8, 8, 7, 6,
7, 6, 7, 8, 9, 10, 11, 12,
10, 10, 8, 9, 10, 10, 9, 8,
9, 7, 8, 9, 10, 9, 8, 7,
8, 8, 6, 7, 8, 8, 3, 4,
5, 6, 5, 4, 5, 8, 7, 8,
6, 6, 8, 7, 6, 6, 5, 4,
5, 4, 3, 4, 5, 6, 7, 8,
6, 6, 8, 7, 8, 8, 7, 6,
7, 8, 9, 8, 7, 8, 6, 7,
8, 8, 8, 8, 8, 8, 8, 8,
5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6,
3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4,
8, 8, 8, 8, 8, 8, 8, 8,
6, 6, 6, 6, 6, 6, 6, 6,
7, 7, 7, 7, 7, 7, 7, 7,
8, 8, 8, 8, 8, 8, 8, 8,
0, 0, 0, 0, 0, 0, 0, 0
};
int running_music[] = {10, 23,
10, 23, 10, 7, 9, 8,
6, 6, 0, 1, 3, 6,
7, 7, 0, 3, 25, 7,
8, 8, 0, 0, 10, 23,
10, 23, 10, 7, 9, 8,
6, 6, 0, 1, 3, 6,
7, 7, 0, 3, 8, 7,
6, 6, 0, 7, 8, 9,
10, 10, 0, 5, 11, 10,
9, 9, 0, 4, 10, 9,
8, 8, 0, 3, 9, 8,
7, 7, 7, 7, 7, 7
};
volatile uint8_t global_rx = 0x88;
osSemaphoreId_t mySem;
osEventFlagsId_t LED_Green, LED_Red;
osMessageQueueId_t motorMessage;
osMessageQueueId_t LEDMessage;
osMessageQueueId_t musicMessage;
void initGPIO(void)
{
// Enable Clock to PORTC
SIM->SCGC5 |= SIM_SCGC5_PORTC_MASK;
// Configure MUX
for (int i = 0; i < 10; i++) {
PORTC->PCR[GREEN_LED[i]] &= ~PORT_PCR_MUX_MASK;
PORTC->PCR[GREEN_LED[i]] |= PORT_PCR_MUX(1);
}
PORTC->PCR[RED_LED] &= ~PORT_PCR_MUX_MASK;
PORTC->PCR[RED_LED] |= PORT_PCR_MUX(1);
// Set Data Direction Registers for PortC
for (int i = 0; i < 10; i++) {
PTC->PDDR |= MASK(GREEN_LED[i]);
}
PTC->PDDR |= MASK(RED_LED);
}
void initPWM(void)
{
//Enable clock for portB and portD
SIM_SCGC5 |= SIM_SCGC5_PORTB_MASK;
SIM_SCGC5 |= SIM_SCGC5_PORTD_MASK;
//Configure pin mux to alt4
PORTB->PCR[MUSIC_PIN] &= ~PORT_PCR_MUX_MASK;
PORTB->PCR[MUSIC_PIN] |= PORT_PCR_MUX(3);
PORTD->PCR[LEFT_PIN0] &= ~PORT_PCR_MUX_MASK;
PORTD->PCR[LEFT_PIN0] |= PORT_PCR_MUX(4);
PORTD->PCR[LEFT_PIN1] &= ~PORT_PCR_MUX_MASK;
PORTD->PCR[LEFT_PIN1] |= PORT_PCR_MUX(4);
PORTD->PCR[RIGHT_PIN0] &= ~PORT_PCR_MUX_MASK;
PORTD->PCR[RIGHT_PIN0] |= PORT_PCR_MUX(4);
PORTD->PCR[RIGHT_PIN1] &= ~PORT_PCR_MUX_MASK;
PORTD->PCR[RIGHT_PIN1] |= PORT_PCR_MUX(4);
//Enable clock for timer0 and timer1
SIM->SCGC6 |= SIM_SCGC6_TPM0_MASK;
SIM->SCGC6 |= SIM_SCGC6_TPM1_MASK;
//Select clock source for TPM
SIM->SOPT2 &= ~SIM_SOPT2_TPMSRC_MASK;
SIM->SOPT2 |= SIM_SOPT2_TPMSRC(1);
TPM0->MOD = FREQ_TO_MOD(50);
TPM0->SC &= ~((TPM_SC_CMOD_MASK) | (TPM_SC_PS_MASK));
TPM0->SC |= (TPM_SC_CMOD(1) | TPM_SC_PS(7));
TPM0->SC &= ~(TPM_SC_CPWMS_MASK);
TPM1->SC &= ~((TPM_SC_CMOD_MASK) | (TPM_SC_PS_MASK));
TPM1->SC |= (TPM_SC_CMOD(1) | TPM_SC_PS(7));
TPM1->SC |= (TPM_SC_CPWMS_MASK);
//Motors
//Channel0
TPM0_C0SC &= ~((TPM_CnSC_ELSB_MASK) | (TPM_CnSC_ELSA_MASK) | (TPM_CnSC_MSB_MASK) | (TPM_CnSC_MSA_MASK));
TPM0_C0SC |= (TPM_CnSC_ELSB(1) | TPM_CnSC_MSB(1));
//Channel2
TPM0_C2SC &= ~((TPM_CnSC_ELSB_MASK) | (TPM_CnSC_ELSA_MASK) | (TPM_CnSC_MSB_MASK) | (TPM_CnSC_MSA_MASK));
TPM0_C2SC |= (TPM_CnSC_ELSB(1) | TPM_CnSC_MSB(1));
//Channel3
TPM0_C3SC &= ~((TPM_CnSC_ELSB_MASK) | (TPM_CnSC_ELSA_MASK) | (TPM_CnSC_MSB_MASK) | (TPM_CnSC_MSA_MASK));
TPM0_C3SC |= (TPM_CnSC_ELSB(1) | TPM_CnSC_MSB(1));
//Channel5
TPM0_C5SC &= ~((TPM_CnSC_ELSB_MASK) | (TPM_CnSC_ELSA_MASK) | (TPM_CnSC_MSB_MASK) | (TPM_CnSC_MSA_MASK));
TPM0_C5SC |= (TPM_CnSC_ELSB(1) | TPM_CnSC_MSB(1));
//Music
//Channel0
TPM1_C0SC &= ~((TPM_CnSC_ELSB_MASK) | (TPM_CnSC_ELSA_MASK) | (TPM_CnSC_MSB_MASK) | (TPM_CnSC_MSA_MASK));
TPM1_C0SC |= (TPM_CnSC_ELSB(1) | TPM_CnSC_MSB(1));
}
void initUART2(uint32_t baud_rate){
uint32_t divisor, bus_clock;
SIM->SCGC4 |= SIM_SCGC4_UART2_MASK;
SIM->SCGC5 |= SIM_SCGC5_PORTE_MASK;
PORTE->PCR[UART_RX_PORTE23] &= ~PORT_PCR_MUX_MASK;
PORTE->PCR[UART_RX_PORTE23] |= PORT_PCR_MUX(4);
UART2->C2 &= ~UART_C2_RE_MASK;
bus_clock = (DEFAULT_SYSTEM_CLOCK)/2;
divisor = bus_clock / (baud_rate * 16);
UART2->BDH = UART_BDH_SBR(divisor >> 8);
UART2->BDL = UART_BDL_SBR(divisor);
UART2->C1 = 0;
UART2->S2 = 0;
UART2->C3 = 0;
UART2->C2 |= (UART_C2_RE_MASK);
NVIC_SetPriority(UART2_IRQn, 128);
NVIC_ClearPendingIRQ(UART2_IRQn);
NVIC_EnableIRQ(UART2_IRQn);
UART2->C2 |= UART_C2_RIE_MASK;
}
void UART2_IRQHandler(void) {
NVIC_ClearPendingIRQ(UART2_IRQn);
if (UART2->S1 & UART_S1_RDRF_MASK) {
global_rx = UART2->D;
osSemaphoreRelease(mySem);
}
}
void tBTBegin(void *);
void tLED(void *);
void tAudio(void *);
//Decodes the data from the Serial Port and performs the necessary action
void tBrain (void *argument) {
_Bool isMoving;
_Bool music;
osSemaphoreAcquire(mySem, osWaitForever);
osThreadNew(tBTBegin, NULL, NULL);
for (;;) {
osSemaphoreAcquire(mySem, osWaitForever);
osMessageQueuePut(motorMessage, (uint8_t*)&global_rx, NULL, 0);
if (global_rx & 0x77) {
isMoving = 1;
} else {
isMoving = 0;
}
if (global_rx == 0) music = 1;
osMessageQueuePut(LEDMessage, &isMoving, NULL, 0);
osMessageQueuePut(musicMessage, &music, NULL, 0);
}
}
//Controls motors
void tMotorControl (void *argument) {
uint8_t myRxData;
for (;;) {
osMessageQueueGet(motorMessage, &myRxData, NULL, 0);
if (myRxData & 0x8) {
TPM0_C0V = 0;
TPM0_C2V = FREQ_TO_MOD(50) * (myRxData & 0x7) / (0x7);
} else {
TPM0_C0V = FREQ_TO_MOD(50) * (myRxData & 0x7) / (0x7);
TPM0_C2V = 0;
}
if ((myRxData >> 4) & 0x8) {
TPM0_C3V = 0;
TPM0_C5V = FREQ_TO_MOD(50) * ((myRxData >> 4) & 0x7) / (0x7);
} else {
TPM0_C3V = FREQ_TO_MOD(50) * ((myRxData >> 4) & 0x7) / (0x7);
TPM0_C5V = 0;
}
}
}
void LED_Off(int LEDPin) {
PTC->PDOR &= ~MASK(LEDPin);
}
void LED_On(int LEDPin) {
PTC->PDOR |= MASK(LEDPin);
}
//Controls LEDs
void tLED (void *argument) {
_Bool isMoving;
for (;;) {
osMessageQueueGet(LEDMessage, &isMoving, NULL, 0);
if (isMoving) {
osEventFlagsSet(LED_Green, 0x1);
osEventFlagsSet(LED_Red, 0x1);
} else {
osEventFlagsSet(LED_Green, 0x2);
osEventFlagsSet(LED_Red, 0x2);
}
}
}
void tLED_Green_Pattern1 (void *argument) {
int currentGreenLED = 0;
_Bool flag = 1;
for (;;) {
osEventFlagsWait(LED_Green, 0x1, osFlagsWaitAny, osWaitForever);
for (int i = 0; i < 10; i++) {
LED_Off(GREEN_LED[i]);
}
if (currentGreenLED == 0) {
flag = 1;
} else if (currentGreenLED == 9) {
flag = 0;
}
if (flag) {
currentGreenLED += 1;
} else currentGreenLED -= 1;
LED_On(GREEN_LED[currentGreenLED]);
osDelay(100);
}
}
void tLED_Green_Pattern2 (void *argument) {
for (;;) {
osEventFlagsWait(LED_Green, 0x2, osFlagsWaitAny, osWaitForever);
for (int i = 0; i < 10; i++) {
LED_On(GREEN_LED[i]);
}
}
}
void tLED_Red_Pattern1 (void *argument) {
for (;;) {
osEventFlagsWait(LED_Red, 0x1, osFlagsWaitAny, osWaitForever);
LED_On(RED_LED);
osDelay(500);
LED_Off(RED_LED);
osDelay(500);
}
}
void tLED_Red_Pattern2 (void *argument) {
for (;;) {
osEventFlagsWait(LED_Red, 0x2, osFlagsWaitAny, osWaitForever);
LED_On(RED_LED);
osDelay(250);
LED_Off(RED_LED);
osDelay(250);
}
}
//Provides audio output
void tSpecialAudio (void *argument) {
for (int i = 0; i < (sizeof(special_music) / sizeof(int)); i++) {
TPM1->MOD = FREQ_TO_MOD(musical_notes[special_music[i]]);
TPM1_C0V = (FREQ_TO_MOD(musical_notes[special_music[i]])) / 2;
osDelay(100);
}
}
void tAudio (void *argument) {
int currentNote = 0;
_Bool music = 0;
for (;;) {
osMessageQueueGet(musicMessage, &music, NULL, 0);
if (!music) {
if (currentNote == sizeof(running_music) / sizeof(int)) {
currentNote = 0;
} else currentNote++;
TPM1->MOD = FREQ_TO_MOD(musical_notes[running_music[currentNote]]);
TPM1_C0V = (FREQ_TO_MOD(musical_notes[running_music[currentNote]])) / 2;
osDelay(200);
} else {
osThreadNew(tSpecialAudio, NULL, NULL);
osThreadExit();
}
}
}
void tBTBegin (void *argument) {
for (int i = 0; i < (sizeof(begin_music) / sizeof(int)); i++) {
osDelay(200);
LED_On(GREEN_LED[0]);
LED_On(GREEN_LED[9]);
TPM1->MOD = FREQ_TO_MOD(musical_notes[begin_music[i]]);
TPM1_C0V = (FREQ_TO_MOD(musical_notes[begin_music[i]])) / 2;
osDelay(200);
LED_Off(GREEN_LED[0]);
LED_Off(GREEN_LED[9]);
}
osThreadNew(tLED, NULL, NULL);
osThreadNew(tAudio, NULL, NULL);
}
int main (void) {
// System Initialization
SystemCoreClockUpdate();
initUART2(BAUD_RATE);
initGPIO();
initPWM();
osKernelInitialize(); // Initialize CMSIS-RTOS
mySem = osSemaphoreNew(1, 0, NULL);
motorMessage = osMessageQueueNew(1, sizeof(uint8_t), NULL);
LEDMessage = osMessageQueueNew(1, sizeof(_Bool), NULL);
musicMessage = osMessageQueueNew(1, sizeof(_Bool), NULL);
LED_Green = osEventFlagsNew(NULL);
LED_Red = osEventFlagsNew(NULL);
osThreadNew(tBrain, NULL, NULL);
osThreadNew(tMotorControl, NULL, NULL);
//osThreadNew(tLED, NULL, NULL);
osThreadNew(tLED_Green_Pattern1, NULL, NULL);
osThreadNew(tLED_Green_Pattern2, NULL, NULL);
osThreadNew(tLED_Red_Pattern1, NULL, NULL);
osThreadNew(tLED_Red_Pattern2, NULL, NULL);
//osThreadNew(tAudio, NULL, NULL);
osKernelStart(); // Start thread execution
for (;;) {}
}