-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
170 lines (146 loc) · 3.6 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
#include "constants.c"
/*----------------------------------------------------------------------------
* CMSIS-RTOS 'main' function template
*---------------------------------------------------------------------------*/
#include "RTE_Components.h"
#include CMSIS_device_header
#include "cmsis_os2.h"
#include "led.h"
#include "uart.h"
#define RLED 29 //portE pin 29
#define CLOCK_SETUP 1
#define PTB0_Pin 0
#define isMoving 1
uint8_t greenPins[] = {8, 9, 10, 11, 2, 3, 4, 5, 20, 21};
uint8_t pinsB[] = {8, 9, 10, 11};
uint8_t pinsE[] = {2, 3, 4, 5, 20, 21};
uint32_t frequencies_mod[] = {1000};
void initGPIO() {
// 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);
}
}
PTE->PCOR |= MASK(RLED);
}
void initPWM(void)
{
SIM_SCGC5 |= SIM_SCGC5_PORTB_MASK;
PORTB->PCR [PTB0_Pin] &= ~PORT_PCR_MUX_MASK;
PORTB->PCR [PTB0_Pin] |= 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/2;
}
void change_frequency(int frequency)
{
int mod_value = 375000/frequency - 1;
TPM1->MOD = mod_value;
TPM1_C0V = mod_value / 2;
}
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)
{
// Running Mode
for (int i = 0; i < 10; i++) {
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)
{
for (;;) {
change_frequency(442);
osDelay(1000U);
change_frequency(800);
osDelay(1000U);
}
}
int main(void)
{
SystemCoreClockUpdate();
initGPIO();
initPWM();
osKernelInitialize(); // Initialize CMSIS-RTOS
osThreadNew(red_blinky_main, NULL, NULL); // Create application main thread
osThreadNew(green_blinky_main, NULL, NULL); // Create application main thread
osThreadNew(buzz_main, NULL, NULL);
osKernelStart(); // Start thread execution
for (;;) {}
}