forked from wongwh2002/2271Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
378 lines (341 loc) · 8.62 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
#include "constants.c"
#include "RTE_Components.h"
#include CMSIS_device_header
#include "cmsis_os2.h"
#define RLED 29 // portE pin 29
#define CLOCK_SETUP 1
#define BUZZ 12
#define c 523
#define d 587
#define e 659
#define f 698
#define g 784
#define a 880
#define b 988
#define C 1047
uint8_t greenPins[] = {8, 9, 10, 11, 2, 3, 4, 5, 20, 21};
osSemaphoreId_t brainSem;
osSemaphoreId_t motorSem;
volatile uint8_t uartData = 0x77;
volatile static float leftDc = 0.0;
volatile static float rightDc = 0.0;
volatile static uint8_t isMoving = 0;
volatile static int counter = 0;
volatile static uint8_t isDone = 0;
uint32_t frequencies_mod[] = {1000};
// Set up the sequence of notes and their durations in milliseconds
const int melody[] = {c, c, g, g, a, a, g, f, f, e, e, d, d, c, g, g, f, f, e, e, d, g, g, f, f, e, e, d, c, c, g, g, a, a, g, f, f, e, e, d, d, c};
int noteDurations[] = {500, 500, 500, 500, 500, 500, 1000, 500, 500, 500, 500, 500, 500, 1000, 500, 500, 500, 500, 500, 500, 1000, 500, 500, 500, 500, 500, 500, 1000, 500, 500, 500, 500, 500, 500, 1000, 500, 500, 500, 500, 500, 500, 1000};
int melodySize = sizeof(melody) / sizeof(melody[0]);
// uint8_t pinsC[] = {9};
// uint32_t frequencies_mod[] = {1000};
const osThreadAttr_t priorityHigh = {
.priority = osPriorityHigh};
const osThreadAttr_t priorityMax = {
.priority = osPriorityRealtime};
void initLedGpio()
{
// Enable Clock to PORTB and PORTD C
SIM->SCGC5 |= ((SIM_SCGC5_PORTB_MASK) | (SIM_SCGC5_PORTE_MASK));
// Configure MUX settings to make all 3 pins GPIO
uint8_t i = 0;
for (; i < 4; i++)
{
int pinNo = greenPins[i];
PORTB->PCR[pinNo] &= ~PORT_PCR_MUX_MASK;
PORTB->PCR[pinNo] |= PORT_PCR_MUX(1);
}
for (; i < 10; i++)
{
int pinNo = greenPins[i];
PORTE->PCR[pinNo] &= ~PORT_PCR_MUX_MASK;
PORTE->PCR[pinNo] |= PORT_PCR_MUX(1);
}
PORTE->PCR[RLED] &= ~PORT_PCR_MUX_MASK;
PORTE->PCR[RLED] |= PORT_PCR_MUX(1);
// Set Data Direction Registers for PortB and PortE
i = 0;
for (; i < 4; i++)
{
int pinNo = greenPins[i];
PTB->PDDR |= MASK(pinNo);
}
for (; i < 10; i++)
{
int pinNo = greenPins[i];
PTE->PDDR |= MASK(pinNo);
}
PTE->PDDR |= MASK(RLED);
// Clear all pins
for (int j = 0; j < 10; j++)
{
int pinNo = greenPins[i];
if (0 <= j && j <= 3)
{
PTB->PCOR |= MASK(pinNo);
}
else
{
PTE->PCOR |= MASK(pinNo);
}
}
}
// UART2 Initialisation
void initUART2(void)
{
SIM->SCGC4 |= SIM_SCGC4_UART2_MASK;
SIM->SCGC5 |= SIM_SCGC5_PORTE_MASK;
PORTE->PCR[UART_RX_PIN] &= ~PORT_PCR_MUX_MASK;
PORTE->PCR[UART_RX_PIN] |= PORT_PCR_MUX(4);
// PORTE->PCR[22] &= ~PORT_PCR_MUX_MASK;
// PORTE->PCR[22] |= PORT_PCR_MUX(4);
UART2->C2 &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK | UART_C2_RIE_MASK);
uint32_t bus_clock = (DEFAULT_SYSTEM_CLOCK) / 2;
uint32_t 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 | UART_C2_RIE_MASK);
NVIC_SetPriority(UART2_IRQn, 128);
NVIC_ClearPendingIRQ(UART2_IRQn);
NVIC_EnableIRQ(UART2_IRQn);
}
void UART2_IRQHandler()
{
NVIC_ClearPendingIRQ(UART2_IRQn);
if (UART2_S1 & UART_S1_RDRF_MASK)
{
uartData = UART2->D;
osSemaphoreRelease(brainSem);
}
// UART2->D = uartData;
}
void initBuzzerPWM(void)
{
SIM_SCGC5 |= SIM_SCGC5_PORTA_MASK;
PORTA->PCR [BUZZ] &= ~PORT_PCR_MUX_MASK;
PORTA->PCR [BUZZ] |= PORT_PCR_MUX (3);
SIM->SCGC6 |= SIM_SCGC6_TPM1_MASK;
SIM->SOPT2 &= ~SIM_SOPT2_TPMSRC_MASK;
SIM->SOPT2 |= SIM_SOPT2_TPMSRC(1);
TPM1->MOD = 7499;
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);
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));
TPM1_C0V = 7499/16;
}
void initMotorPWM(void)
{
SIM->SCGC5 |= SIM_SCGC5_PORTC_MASK;
PORTB->PCR[MOTOR_DRIVER_AIN1] &= ~PORT_PCR_MUX_MASK;
PORTB->PCR[MOTOR_DRIVER_AIN1] |= PORT_PCR_MUX(3);
PORTB->PCR[MOTOR_DRIVER_AIN2] &= ~PORT_PCR_MUX_MASK;
PORTB->PCR[MOTOR_DRIVER_AIN2] |= PORT_PCR_MUX(3);
PORTC->PCR[MOTOR_DRIVER_BIN1] &= ~PORT_PCR_MUX_MASK;
PORTC->PCR[MOTOR_DRIVER_BIN1] |= PORT_PCR_MUX(4);
PORTC->PCR[MOTOR_DRIVER_BIN2] &= ~PORT_PCR_MUX_MASK;
PORTC->PCR[MOTOR_DRIVER_BIN2] |= PORT_PCR_MUX(4);
SIM->SCGC6 |= (SIM_SCGC6_TPM2_MASK | SIM_SCGC6_TPM0_MASK);
// SIM->SOPT2 &= ~SIM_SOPT2_TPMSRC_MASK;
// SIM->SOPT2 |= SIM_SOPT2_TPMSRC(1);
TPM2->MOD = TIMER_THRESHOLD;
TPM2->SC &= ~((TPM_SC_CMOD_MASK) | (TPM_SC_PS_MASK));
TPM2->SC |= (TPM_SC_CMOD(1) | TPM_SC_PS(7));
TPM2->SC &= ~(TPM_SC_CPWMS_MASK);
TPM0->MOD = TIMER_THRESHOLD;
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);
TPM2_C0SC &= ~((TPM_CnSC_ELSB_MASK) | (TPM_CnSC_ELSA_MASK) | (TPM_CnSC_MSB_MASK) | (TPM_CnSC_MSA_MASK));
TPM2_C0SC |= (TPM_CnSC_ELSA(1) | TPM_CnSC_MSB(1));
TPM2_C1SC &= ~((TPM_CnSC_ELSB_MASK) | (TPM_CnSC_ELSA_MASK) | (TPM_CnSC_MSB_MASK) | (TPM_CnSC_MSA_MASK));
TPM2_C1SC |= (TPM_CnSC_ELSA(1) | TPM_CnSC_MSB(1));
TPM2_C0V = 0;
TPM2_C1V = 0;
TPM0_C0SC &= ~((TPM_CnSC_ELSB_MASK) | (TPM_CnSC_ELSA_MASK) | (TPM_CnSC_MSB_MASK) | (TPM_CnSC_MSA_MASK));
TPM0_C0SC |= (TPM_CnSC_ELSA(1) | TPM_CnSC_MSB(1));
TPM0_C1SC &= ~((TPM_CnSC_ELSB_MASK) | (TPM_CnSC_ELSA_MASK) | (TPM_CnSC_MSB_MASK) | (TPM_CnSC_MSA_MASK));
TPM0_C1SC |= (TPM_CnSC_ELSA(1) | TPM_CnSC_MSB(1));
TPM0_C0V = 0;
TPM0_C1V = 0;
}
void change_frequency(int frequency)
{
int mod_value = 375000 / frequency - 1;
TPM1->MOD = mod_value;
TPM1_C0V = mod_value / 16;
}
void red_blinky_main(void *argument)
{
for (;;)
{
PTE->PTOR |= MASK(RLED);
if (isMoving)
{
osDelay(500U);
}
else
{
osDelay(250U);
}
}
}
void green_blinky_main(void *argument)
{
for (;;)
{
if (isMoving)
{
for (int i = 0; i < 10; i++) {
if (0 <= i && i <= 3) {
PTB->PCOR |= MASK(greenPins[i]);
} else {
PTE->PCOR |= MASK(greenPins[i]);
}
}
// Running Mode
for (int i = 0; i < 10; i++) {
if (!isMoving) {
break;
}
if (0 <= i && i <= 3) {
PTB->PTOR |= MASK(greenPins[i]);
}
else
{
PTE->PTOR |= MASK(greenPins[i]);
}
osDelay(250U);
if (0 <= i && i <= 3)
{
PTB->PTOR |= MASK(greenPins[i]);
}
else
{
PTE->PTOR |= MASK(greenPins[i]);
}
}
}
else
{
// Light up all
for (int i = 0; i < 10; i++)
{
if (0 <= i && i <= 3)
{
PTB->PSOR |= MASK(greenPins[i]);
}
else
{
PTE->PSOR |= MASK(greenPins[i]);
}
}
}
}
}
void buzz_main(void *argument)
{
int i = 0;
for (;;)
{
// change_frequency(0);
// osDelay(5);
int length = sizeof(melody) / sizeof(melody[0]);
if (isDone == 0) {
change_frequency(melody[i]);
osDelay(noteDurations[i]);
i = (i + 1) % length;
} else {
change_frequency(melody[i]*1.5);
osDelay(noteDurations[i]/3);
i = (i == 0) ? length - 1 : i - 1;
}
}
}
void brain_main(void *argument)
{
for (;;)
{
osSemaphoreAcquire(brainSem, osWaitForever);
if (uartData == 0b00000000)
{
leftDc = 0;
rightDc = 0;
}
else if (uartData == 0b00000001)
{
leftDc = -0.5;
rightDc = -0.5;
isDone = 0;
}
else if (uartData == 0b00000010)
{
isDone = 1;
}
else
{
leftDc = ((uartData >> 4)) / 15.0;
rightDc = ((uartData & 0b00001111)) / 15.0;
}
if (leftDc == 0 && rightDc == 0)
{
isMoving = 0;
}
else
{
isMoving = 1;
}
osSemaphoreRelease(motorSem);
}
}
void motor_main(void *argument)
{
for (;;)
{
osSemaphoreAcquire(motorSem, osWaitForever);
if (leftDc > 0)
{
TPM2_C0V = TIMER_THRESHOLD * leftDc;
TPM2_C1V = 0;
}
else
{
TPM2_C0V = 0;
TPM2_C1V = TIMER_THRESHOLD * -leftDc;
}
if (rightDc > 0)
{
TPM0_C0V = TIMER_THRESHOLD * rightDc;
TPM0_C1V = 0;
}
else
{
TPM0_C0V = 0;
TPM0_C1V = TIMER_THRESHOLD * -rightDc;
}
}
}
int main(void)
{
SystemCoreClockUpdate();
initUART2();
initLedGpio();
initBuzzerPWM();
initMotorPWM();
brainSem = osSemaphoreNew(1, 0, NULL);
motorSem = osSemaphoreNew(1, 0, NULL);
osKernelInitialize(); // Initialize CMSIS-RTOS
osThreadNew(red_blinky_main, NULL, NULL);
osThreadNew(green_blinky_main, NULL, NULL);
osThreadNew(buzz_main, NULL, NULL);
osThreadNew(brain_main, NULL, NULL);
osThreadNew(motor_main, NULL, NULL);
osKernelStart(); // Start thread execution
for (;;)
{
}
}