-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
347 lines (289 loc) · 12.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
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
/**
* @file main.c
* @author Prithvi Bhat
* @brief Main Driver code for the Ultrasound Pen
* All peripherals and I/O are handled by GPIOs on Ports A and D as tabulated below
* |--------------|--------------|-------|
* | Peripheral | Direction | Pin |
* |--------------|--------------|-------|
* | Buzzer | Output | PD01 |
* | IR Sensor | Input | PD06 |
* | Audio Ch 1 | Input | PC04 |
* | Audio Ch 2 | Input | PC05 |
* | Audio Ch 3 | Input | PC06 |
* |--------------|--------------|-------|
**/
#include "clock.h"
#include "gpio.h"
#include "tm4c123gh6pm.h"
#include "eeprom.h"
#include "timer.h"
#include "nvic.h"
#include "uart0.h"
#include "wait.h"
#include "strings.h"
#include <stdio.h>
#include "eeprom_memory_map.h"
#include "commands.h"
#include <string.h>
#include "i2c0_lcd.h"
#define IS_COMMAND(string, count) if(isCommand(&user_data, string, count))
#define RESET (NVIC_APINT_R = (NVIC_APINT_VECTKEY | NVIC_APINT_SYSRESETREQ))
#define ASSERT(value) if(value >= 0)
// Global Variables
uint32_t g_timer_A_accumulated = 0, g_timer_B_accumulated = 0, g_timer_C_accumulated = 0, count = 0;
uint32_t g_timer_A_FIFO[MAX_FIFO_SIZE], g_timer_B_FIFO[MAX_FIFO_SIZE], g_timer_C_FIFO[MAX_FIFO_SIZE];
bool ir_in, sA_in, sB_in, sC_in;
// Pin Macros
#define IR_IN PORTA,6 // Input pin for IR signal
#define US_A_IN PORTC,4 // Input pin for comparator output from Sensor 1
#define US_B_IN PORTC,5 // Input pin for comparator output from Sensor 2
#define US_C_IN PORTC,6 // Input pin for comparator output from Sensor 3
#define BUZZ_OUT PORTD,1 // Output pin for buzzer
/**
* @brief Function to initialize all necessary hardware on the device
**/
void init_TM4C_hardware(void)
{
initSystemClockTo40Mhz(); // Initialize system clock
enablePort(PORTC); // Initialize clocks on PORT C
enablePort(PORTD); // Initialize clocks on PORT D
enablePort(PORTF); // Initialize clocks on PORT F
selectPinPushPullOutput(LED_R);
selectPinPushPullOutput(LED_B);
selectPinPushPullOutput(LED_G);
selectPinPushPullOutput(BUZZ_OUT);
setPinAuxFunction(BUZZ_OUT, GPIO_PCTL_PD1_M1PWM1);
initLcd(); // Initialise I2C display device
initEeprom(); // Initialize MCU to use EEPROM
pwm_init(); // Initialise PWM
initUart0(); // Initialise UART0
setUart0BaudRate(115200, 40e6); // Set UART baud rate and clock
disableNvicInterrupt(INT_GPIOD); // Disable the interrupt using its vector
disableNvicInterrupt(INT_GPIOA); // Disable the interrupt using its vector
disablePinInterrupt(IR_IN); // Disable Interrupt to configure
selectPinDigitalInput(IR_IN); // Set Pin to input
selectPinInterruptFallingEdge(IR_IN); // Initialize interrupt to trigger on rising edge
enablePinPullup(IR_IN);
clearPinInterrupt(IR_IN); // Clear any older, stray interrupts
enablePinInterrupt(IR_IN); // Initialize Interrupt
enableNvicInterrupt(INT_GPIOD); // Enable interrupt after all configurations are complete
enableNvicInterrupt(INT_GPIOA); // Enable interrupt after all configurations are complete
timer_init(); // Initialise timers
}
/**
* @brief ISR for when the MCU receives input from the comparator for Ultrasound Sensor A
**/
void sA_interrupt_handler(void)
{
g_timer_A_FIFO[g_timer_A_accumulated++] = WTIMER0_TAV_R; // Read timer register
WTIMER0_CTL_R &= ~TIMER_CTL_TAEN; // Disable timer
WTIMER0_TAV_R = 0; // Reset Register
WTIMER0_ICR_R |= TIMER_ICR_CAECINT; // Reset Timer interrupt
sA_in = true; // Set flag for feedback
}
/**
* @brief ISR for when the MCU receives input from the comparator for Ultrasound Sensor B
**/
void sB_interrupt_handler(void)
{
g_timer_B_FIFO[g_timer_B_accumulated++] = WTIMER0_TBV_R; // Read timer register
WTIMER0_CTL_R &= ~TIMER_CTL_TBEN; // Disable timer
WTIMER0_TBV_R = 0; // Reset Register
WTIMER0_ICR_R |= TIMER_ICR_CBECINT; // Reset Timer interrupt
sB_in = true; // Set flag for feedback
}
/**
* @brief ISR for when the MCU receives input from the comparator for Ultrasound Sensor C
**/
void sC_interrupt_handler(void)
{
g_timer_C_FIFO[g_timer_C_accumulated++] = WTIMER1_TAV_R; // Read timer register
WTIMER1_CTL_R &= ~TIMER_CTL_TAEN; // Disable timer
WTIMER1_TAV_R = 0; // Reset Register
WTIMER1_ICR_R |= TIMER_ICR_CAECINT; // Reset Timer interrupt
sC_in = true; // Set flag for feedback
}
/**
* @brief Watchdog timer ISR
**/
void timeout_interrupt_handler(void)
{
WTIMER3_CTL_R &= ~TIMER_CTL_TAEN; // Disable timer
WTIMER3_ICR_R |= TIMER_ICR_TATOCINT; // Reset Timer interrupt
WTIMER0_CTL_R &= ~TIMER_CTL_TAEN; // Disable timer
WTIMER0_ICR_R |= TIMER_ICR_CAECINT; // Reset Timer interrupt
WTIMER0_TAV_R = 0;
WTIMER0_CTL_R &= ~TIMER_CTL_TBEN; // Disable timer
WTIMER0_ICR_R |= TIMER_ICR_CBECINT; // Reset Timer interrupt
WTIMER0_TBV_R = 0;
WTIMER1_CTL_R &= ~TIMER_CTL_TAEN; // Disable timer
WTIMER1_ICR_R |= TIMER_ICR_CAECINT; // Reset Timer interrupt
WTIMER1_TAV_R = 0;
timer_init(); // Re-initialise timer as failsafe
if ((ir_in && !(sA_in && sB_in && sC_in)))
{
LED_TIMEOUT;
beep_now(BEEP_ERROR);
}
if (ir_in)
{
LED_IR_SENSOR;
beep_now(BEEP_IR_INT);
ir_in = false;
}
if (sA_in)
{
LED_SENSOR_A;
beep_now(BEEP_US_A_INT);
sA_in = false;
}
if (sB_in)
{
LED_SENSOR_B;
beep_now(BEEP_US_B_INT);
sB_in = false;
}
if (sC_in)
{
LED_SENSOR_C;
beep_now(BEEP_US_C_INT);
sC_in = false;
}
enableNvicInterrupt(INT_GPIOD); // Enable interrupts on PORTD to capture IR
WTIMER3_CTL_R &= ~TIMER_CTL_TAEN; // Disable timer
WTIMER3_ICR_R |= TIMER_ICR_TATOCINT; // Reset Timer interrupt
}
/**
* @brief ISR for when MCU receives signal from the IR receiver
* * Starts three timers simultaneously corresponding to sensors
* * Starts one watchdog timers
**/
void ir_interrupt_handler(void)
{
LED_CLEAR;
clearPinInterrupt(IR_IN); // Clear interrupt to be able to exit ISR and capture next interrupt
timer_init(); // Re-initialise timer as failsafe
// Accept only a max of five values in FIFO to average
// Reset count and flush FIFO
if (g_timer_A_accumulated >= count||
g_timer_B_accumulated >= count||
g_timer_C_accumulated >= count
)
{
g_timer_A_accumulated = 0;
g_timer_B_accumulated = 0;
g_timer_C_accumulated = 0;
memset(g_timer_A_accumulated, 0, sizeof(g_timer_A_accumulated));
memset(g_timer_B_accumulated, 0, sizeof(g_timer_B_accumulated));
memset(g_timer_C_accumulated, 0, sizeof(g_timer_C_accumulated));
}
WTIMER0_CTL_R &= ~TIMER_CTL_TAEN; // Stop timer 0 - Sensor A
WTIMER0_CTL_R &= ~TIMER_CTL_TBEN; // Stop timer 0 - Sensor B
WTIMER1_CTL_R &= ~TIMER_CTL_TAEN; // Stop timer 1 - Sensor C
WTIMER3_CTL_R &= ~TIMER_CTL_TAEN; // Stop timer 3 - Watchdog
WTIMER0_TAV_R = 0; // Reset timer to 0 before starting
WTIMER0_TBV_R = 0; // Reset timer to 0 before starting
WTIMER1_TAV_R = 0; // Reset timer to 0 before starting
WTIMER0_CTL_R |= TIMER_CTL_TAEN; // Start timer 0 - Sensor A
WTIMER0_CTL_R |= TIMER_CTL_TBEN; // Start timer 0 - Sensor B
WTIMER1_CTL_R |= TIMER_CTL_TAEN; // Start timer 1 - Sensor C
WTIMER3_CTL_R |= TIMER_CTL_TAEN; // Start timer 3 - Watchdog
ir_in = true; // Set flag for feedback
}
/**
* @brief Main, driver function
**/
void main(void)
{
init_TM4C_hardware();
char string[100];
string_data_t *user_data;
uint16_t character_count;
uint8_t i;
count = readEeprom(TC_AVG);
if (count <= 0) count = 1; // Capture only one value if nothing specified by user
if (readEeprom(0x00) == 0xFF) // Coordinates have not previously been written into EEPROM
{
putsUart0("Sensor coordinates missing!\r\n\r\n");
}
while (1)
{
string_input_get(&user_data); // Read user input
string_parse(&user_data); // Parse user input
IS_COMMAND("sensor", 4) // Compare and act on user input
{
// Store sensor coordinates in EEPROM
update_sensor_coordinates (
getFieldString(&user_data, 1),
(int32_t)getFieldInteger(&user_data, 2),
(int32_t)getFieldInteger(&user_data, 3)
);
putsUart0("Assuming input coordinates are in mm\r\n\r\n");
continue;
}
IS_COMMAND("reset", 1)
{
for (i = 0; i < MAX_FIFO_SIZE; i++)
{
g_timer_A_FIFO[i] = 0;
g_timer_B_FIFO[i] = 0;
g_timer_C_FIFO[i] = 0;
}
g_timer_A_accumulated = 0; // Reset All values
g_timer_B_accumulated = 0; // Reset All values
g_timer_C_accumulated = 0; // Reset All values
RESET; // Reset System
continue;
}
IS_COMMAND("distance", 1)
{
calculate_distance(g_timer_A_FIFO, g_timer_B_FIFO, g_timer_C_FIFO, true); // Output distance
continue;
}
IS_COMMAND("average", 2)
{
uint32_t average = (uint32_t)getFieldInteger(&user_data, 1);
if (average > MAX_AVERAGES)
{
fprintf(string, "ERROR! Max average of %d\r\n\r\n", MAX_AVERAGES);
putsUart0(string);
}
else
{
writeEeprom(TC_AVG, (uint32_t)average); // Write the number of averages into eeprom
putsUart0("Averager updated\r\n\r\n");
count = average;
}
continue;
}
IS_COMMAND("beep", 4) // Update beep tones
{
int32_t type = (int32_t)getFieldInteger(&user_data, 1);
int32_t load = (int32_t)getFieldInteger(&user_data, 2);
int32_t per1 = (int32_t)getFieldInteger(&user_data, 3);
write_beep(type, load, per1);
// write_beep(0, 1, 1);
// write_beep(1, 2, 2);
// write_beep(2, 3, 2);
// write_beep(3, 4, 2);
// write_beep(4, 5, 3);
putsUart0("Beep tones updated\r\n\r\n");
}
IS_COMMAND("variance", 1)
{
calculate_variance(g_timer_A_FIFO, g_timer_B_FIFO, g_timer_C_FIFO); // Output Variance
}
IS_COMMAND("coord", 1)
{
calculate_coordinates();
}
IS_COMMAND("fix", 3)
{
int32_t x_fix = (int32_t)getFieldInteger(&user_data, 1);
int32_t y_fix = (int32_t)getFieldInteger(&user_data, 2);
update_fix(x_fix, y_fix);
putsUart0("Fix values updated\r\n");
}
}
}