-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.c
More file actions
704 lines (569 loc) · 18.5 KB
/
Copy pathapp.c
File metadata and controls
704 lines (569 loc) · 18.5 KB
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/*
This file is the main core of the code. All application code is here
*/
#include "app.h"
#include "main.h"
#include <stdio.h>
#include <stdlib.h>
#include "eeprom.h"
/*
@purpose: Setup the System Timer with High Interrupt Priority with 1ms overflow period
@parameters: void
@return: void
@version: 0.1
*/
void
InitTimerCount()
{
Timer0_Construct(&Timer);
TimeCountConstruct(&TimerCount);
Timer.ClkSrc = INTERNAL_CLOCK;
Timer.Mode = _8_BIT;
Timer.Opr = YES;
Timer.PSA = PRESCALER_ON;
Timer.PSAConfig = PRESCALER_16;
Timer.Preload = 6;
Timer.PreloadOn = YES;
Timer.IntConfig(INTERRUPT_HIGH_PRIORITY);
Timer.IntProc = TimerCount.Update;
Timer.Init(&Timer);
}
/*
@purpose: Setup the interrupts of CPU
@parameters: void
@return: void
@version: 0.1
*/
void
InitInterrupt()
{
RCONbits.IPEN = 1;
INTCONbits.GIEH = 1;
INTCONbits.PEIE = 1;
IPR1bits.RC1IP = 1;
PIE1bits.RC1IE = 1;
INTCON2bits.INTEDG0 = 0;
INTCONbits.INT0IE = 1;
INTCON2bits.INTEDG1 = 1;
INTCON3bits.INT1IP = 1;
}
/*
@purpose: Configure IO pins
@parameters: void
@return: void
@version: 0.1
*/
void
InitIO()
{
LED_BLUE_D = 0;
LED_GREEN_D = 0;
LED_RED_D = 0;
LED_BLUE = 1;
LED_GREEN = 1;
LED_RED = 1;
ANSELC = 0;
ANSELAbits.ANSA2 = 1;
TRISAbits.TRISA2 = 1;
}
/*
@purpose: Send the Battery Status Package
@parameters: void
@return: void
@version: 0.1
*/
void
Battery_Status()
{
/* buffer for debug */
char buffer[50];
/* the package */
char BatteryPackage[4];
unsigned short adcValue;
/* Get the ADC value */
adcValue = ADC_Read(2);
/* if the device isn't in the configuration mode */
if (!configModeStatus)
{
/* send the adc value for debug serial port */
sprintf(buffer, "Battery ADC %u\n", adcValue);
Debug.Send(buffer);
/* */
/* mount the package */
BatteryPackage[0] = 0x55;
BatteryPackage[1] = adcValue;
BatteryPackage[2] = adcValue >> 8;
BatteryPackage[3] = BatteryPackage[0] + BatteryPackage[1] + BatteryPackage[2];
/* */
/* send the package via wifi */
ESP_Send_Transparent_Data(BatteryPackage, 4);
}
}
/*
@purpose: Blink Led - unused
@parameters: void
@return: void
@version: 0.1
*/
void
Led_Blink2()
{
//PORTDbits.RD1 = !PORTDbits.RD1;
}
/*
@purpose: Configure the tasks for the Task Schedule
@parameters: void
@return: void
@verson: V0.1
*/
void
Task_Init()
{
BattteryStatus.Execute = Battery_Status;
BattteryStatus.interval = 5000;
BattteryStatus.active = YES;
TaskScheduler(BattteryStatus);
LedBlink_2.Execute = Led_Blink2;
LedBlink_2.interval = 500;
LedBlink_2.active = YES;
TaskScheduler(LedBlink_2);
DMP.Execute = Frame_Update;
DMP.active = YES;
DMP.interval = 10;
TaskScheduler(DMP);
ConfigMode.Execute = configMode;
ConfigMode.active = YES;
ConfigMode.interval = 1000;
TaskScheduler(ConfigMode);
}
/*
@purpose: Start Serial Port
@parameters: void
@return: void
@version: V0.1
*/
void
Comm_Init()
{
Debug.Send("Init Serial Port at 57600bps\r");
UART_Init(57600);
}
/*
@purpose: Send the Version to debug
@parameters: void
@return: void
@version: V0.1
*/
void
Send_Version()
{
Soft_UART_Construct(&Debug);
Debug.Send(_VERSION_);
Debug.Send("\n\r");
}
/*
@purpose: Update Frame of device
@parameters: void
@return: void
@version: V0.1
*/
void
Frame_Update()
{
/* buffer for debug */
char buffer[50];
/* fifo buffer count */
char fifocount = 0;
/* fifo buffer */
char fifobuffer[42];
/* the quartenion */
int quartenion[4];
/* if MPU6050 data is available */
if (MPU_Sensor.DataReady)
{
/* Get MPU6050 Interrupt Status */
MPU_Sensor.IntStatus = MPU_Get_Int_Status(mpuAddr);
/* if FIFO Overflow occurred reset the FIFO */
if (MPU_Sensor.IntStatus & 0x10)
{
MPU_Reset_FIFO(mpuAddr);
Debug.Send("Fifo Overflow\r");
}
/* else If data is available */
else if (MPU_Sensor.IntStatus & 0x02)
{
/* wait for a complete package in fifo buffer */
while (fifocount < MPU_Sensor.PacketSize)
{
fifocount = MPU_Get_FIFO_Count(mpuAddr);
}
/* adjust fifocount to the actual fifo size*/
fifocount = fifocount - MPU_Sensor.PacketSize;
/* read the fifo package */
MPU_Get_Fifo_Bytes(mpuAddr, fifobuffer, MPU_Sensor.PacketSize);
/* send the fifo package via WiFi*/
ESP_Send_Transparent_Data(fifobuffer, MPU_Sensor.PacketSize);
}
/* else print the Int status in the debug */
else
{
sprintf(buffer, "Int Status: 0x%2X\r", MPU_Sensor.IntStatus);
Debug.Send(buffer);
}
/* clear dataready flag */
MPU_Sensor.DataReady = _FALSE_;
}
}
/*
@purpose: Start ESP8266 in client mode
@parameters: void
@return: void
@version: V0.1
*/
void
Wifi_Normal_Init()
{
/* if isn't in the configuration mode, test ESP8266 Startup */
if (!configModeStatus)
while (!ESP_Test_Startup() && !configModeStatus) {
__delay_ms(2000);
}
/* if isn't in the configuration mode, put the ESP8266 in the Station mode */
if (!configModeStatus)
while (!ESP_Wifi_Setup(ESP8266_STATION_MODE) && !configModeStatus) {
__delay_ms(2000);
}
/* if isn't in the configuration mode, connect to the network seted in the EEPROM */
if (!configModeStatus)
while (!ESP_Connect_Ap(CONFIG_WIFI.WIFI_SSID, CONFIG_WIFI.WIFI_PASS) && !configModeStatus) {
__delay_ms(2000);
}
/* if isn't in the configuration mode, get TCP IP status */
if (!configModeStatus)
while (!ESP_TCPIP_Status() && !configModeStatus) {
__delay_ms(2000);
}
/* if isn't in the configuration mode, connect to the UDP server seted in the EEPROM */
if (!configModeStatus)
while (!ESP_TCPIP_Connect("UDP", CONFIG_IP.IP_ADDR, CONFIG_IP.PORT_NUMBER) && !configModeStatus) {
__delay_ms(2000);
}
/* if isn't in the configuration mode, put the ESP8266 in the transparent mode */
if (!configModeStatus)
while (!ESP_Transparent_Mode() && !configModeStatus) {
__delay_ms(2000);
}
/* if isn't in the configuration mode, start the transparent transmission */
if (!configModeStatus)
while (!ESP_Start_Transparent_Transmission() && !configModeStatus) {
__delay_ms(2000);
}
/* send "WiFi ready" to de debug */
Debug.Send("Wifi Ready\r");
}
/*
@purpose: Start MPU6050 Digital Motion Processor
@parameters: void
@return: void
@version: V0.1
*/
void
MPU_DMP_Init()
{
/* Buffer for debug */
char buffer[10];
/* Start I2C Port as Master */
I2C_Master_Init();
/* Turn on the MPU6050 */
MPU_CH_DIRECTION = 0;
MPU_CH = 1;
/* Start the MPU6050 class */
MPU_Construct(&MPU_Sensor);
/* Start the initial Configuration of MPU6050 */
MPU_Sensor.Init();
/* Start Digital Motion Processor */
MPU_Sensor.DMPStatus = DMP_Init(&MPU_Sensor);
/* If Digital Motion Processor Startup is ok */
if (MPU_Sensor.DMPStatus == _DMP_OK_)
{
/* Set the Offsets of Gyroscope */
MPU_Set_X_Gyro_Offset(BIAS_MPU.Gx, mpuAddr);
MPU_Set_Y_Gyro_Offset(BIAS_MPU.Gy, mpuAddr);
MPU_Set_Z_Gyro_Offset(BIAS_MPU.Gz, mpuAddr);
/* */
/* Enable the Digital Motion Processor */
MPU_Set_DMP_Enabled(mpuAddr, _TRUE_);
/* Enable the FIFO Buffer */
MPU_Set_FIFO_Enabled(mpuAddr, _TRUE_);
/* Get Interrupt Status */
MPU_Sensor.IntStatus = MPU_Get_Int_Status(mpuAddr);
/* Start the Tasks */
Task_Init();
/* Send "DMP Ready to the debug */
Debug.Send("DMP Ready\r");
}
/* if Digital Motion processor startup failed */
else
{
/* Send the status to the debug serial port */
Debug.Send("DMP Init failed\r");
sprintf(buffer, "STS: %d\r", MPU_Sensor.DMPStatus);
Debug.Send(buffer);
/* reset the cpu */
reset();
}
}
/*
@purpose: Configuration Mode: Input of WiFi network parameters as well as UDP server parameters
@parameters: void
@return: void
@version: V0.1
*/
void
configMode()
{
/* loop control variables */
char i, j;
/* configuration buffers */
char config[128];
char configFinal[128];
/* */
/* general purpose pointer */
char * ponteiro;
/* variable to address the ESP8266 connection */
char link;
/* if in the configuration mode */
if (configModeStatus)
{
/* turn off the MPU6050 */
MPU_CH = 0;
/* str to debug */
Debug.Send("Starting Configurator...\r");
/* Clear the Config buffer*/
ClearAnyBuffer(config, 128);
/* Setup the WiFi Acess point name */
strcpy(config, "HeadTracker ");
strcat(config, _VERSION_);
/* Clear the WiFi Buffer */
ClearAnyBuffer(Wifi_Buffer, SIZE_OF_WIFI_BUFFER);
/* turn off the system clock */
INTCONbits.TMR0IE = 0;
/* reset the ESP8266 */
__delay_ms(500);
ESP_Reset();
__delay_ms(500);
/* */
/* Test the startup of ESP8266 */
while (!ESP_Test_Startup()) {
__delay_ms(1000);
}
/* Set ESP8266 as Acess Point */
while (!ESP_Wifi_Setup(ESP8266_AP_MODE)) {
__delay_ms(1000);
}
/* Setup the network */
while (!ESP_AP_Setup(config, "headtracker", 5, ESP8266_ENC_WPA2_PSK)) {
__delay_ms(1000);
}
/* Accept multiple connections */
while (!ESP_TCPIP_Mux(ESP8266_MULTIPLE_CONNECTION)) {
__delay_ms(1000);
}
/* Start server at port 80 */
while (!ESP_Start_Server(80)) {
__delay_ms(1000);
}
/* If WiFi configuration is not valid, turn on the green led */
if (CONFIG_WIFI.ValidData != 0xAA)
{
LED_GREEN = 1;
}
/* if server configuration is not valid, turn on the red led */
if (CONFIG_IP.ValidData != 0xAA) {
LED_RED = 1;
}
/* clear the wifi buffer */
ClearAnyBuffer(Wifi_Buffer, SIZE_OF_WIFI_BUFFER);
/* send Configuration Ready */
Debug.Send("Configurator ready\r");
/* while device is in the configuration mode */
while (configModeStatus)
{
/* check if there is new data on WiFi Buffer */
if (strstr(Wifi_Buffer, "+IPD"))
{
/* get the address of the connection */
ponteiro = strstr(Wifi_Buffer, "+IPD");
link = ponteiro[5];
/* */
/* If link is valid */
if (link >= '0' && link <= '3')
{
/* if the page is ip.html */
if (strstr(Wifi_Buffer, "ip.html"))
{
/* debug IP */
Debug.Send("IP\r");
/* get ip address */
ponteiro = strstr(Wifi_Buffer, "?");
ponteiro = ponteiro + 1;
for (i = 0; *ponteiro != 'H'; i++)
{
config[i] = *ponteiro;
ponteiro++;
}
Debug.Send(config);
Debug.Send("\r");
char * ip_start, * ip_end, * port_start, * port_end;
char port[5];
ip_start = strstr(config, "=") + 1;
ip_end = strstr(config, "&") - 1;
port_start = strstr(config, "PORT=") + 5;
port_end = port_start;
while (*port_end != 0x20)
port_end++;
ClearAnyBuffer(CONFIG_IP.IP_ADDR, sizeof (CONFIG_IP.IP_ADDR));
memcpy(CONFIG_IP.IP_ADDR, ip_start, ip_end - ip_start + 1);
ClearAnyBuffer(port, 5);
memcpy(port, port_start, port_end - port_start);
CONFIG_IP.PORT_NUMBER = atoi(port);
CONFIG_IP.ValidData = 0xAA;
EEPROM_Put_Object(&CONFIG_IP, sizeof (IP_CONFIG), ADDR_IP);
ESP_TCPIP_Send(link, result);
ESP_TCPIP_Close(link - 48);
configModeStatus = _FALSE_;
Debug.Send("Gravou IP\r");
/* */
}
/* if the page is wifi.html */
else if (strstr(Wifi_Buffer, "wifi.html"))
{
/* debug wifi */
Debug.Send("Wifi\r");
/* get the wifi configuration */
ClearAnyBuffer(config, 128);
ClearAnyBuffer(configFinal, 128);
ClearAnyBuffer(CONFIG_WIFI.WIFI_PASS, sizeof (CONFIG_WIFI.WIFI_PASS));
ClearAnyBuffer(CONFIG_WIFI.WIFI_SSID, sizeof (CONFIG_WIFI.WIFI_SSID));
while (!strstr(Wifi_Buffer, "SSID"));
ponteiro = strstr(Wifi_Buffer, "?");
ponteiro = ponteiro + 1;
for (i = 0; *ponteiro != 'H'; i++)
{
config[i] = *ponteiro;
ponteiro++;
}
ponteiro = 0x00;
for (i = 0, j = 0; j < 128; j++)
{
if (config[j] != '%')
{
configFinal[i] = config[j];
i++;
}
else
{
ponteiro = &config[j];
ponteiro++;
char c[2];
c[0] = *ponteiro;
ponteiro++;
c[1] = *ponteiro;
long int caractere = strtol(c, NULL, 16);
configFinal[i] = caractere;
j = j + 2;
i++;
}
}
char * SSID_START = strstr(configFinal, "SSID=") + 5;
char * SSID_FINAL = strstr(configFinal, "&PASS=");
memcpy(CONFIG_WIFI.WIFI_SSID, SSID_START, SSID_FINAL - SSID_START);
char * PASS_START = strstr(configFinal, "PASS=") + 5;
char * PASS_END = PASS_START;
while (*PASS_END != 0x20)
PASS_END++;
memcpy(CONFIG_WIFI.WIFI_PASS, PASS_START, PASS_END - PASS_START);
CONFIG_WIFI.ValidData = 0xAA;
EEPROM_Put_Object(&CONFIG_WIFI, sizeof (WIFI_CONFIG), ADDR_WIFI);
Debug.Send(configFinal);
Debug.Send("\r");
ESP_TCPIP_Send(link, result);
ESP_TCPIP_Close(link - 48);
configModeStatus = _FALSE_;
Debug.Send("Gravou Wifi\r");
/* */
}
/* else send the configuration page */
else
{
/* send the data */
ESP_TCPIP_Send(link, page);
/* wait the transmission */
__delay_ms(500);
/* close the connection (browser load complete)*/
ESP_TCPIP_Close(link - 48);
}
}
/* invalid link */
else
{
Debug.Send("Invalid Link!");
}
/* close all connections */
for (i = 0; i < 4; i++)
{
ESP_TCPIP_Close(i);
}
}
}
/* reset the cpu */
reset();
}
}
/*
@purpose: Reset the CPU
@parameters: void
@return: void
@version: V0.1
*/
void
reset()
{
/* debug */
Debug.Send("Reseta\r");
/* asm instruction reset */
asm("RESET");
}
/*
@purpose: Get Last reset cause and put it to resetCause Global variable
@parameters: void
@return: void
@version: V0.1
*/
void
GetResetCause()
{
if (!RCONbits.RI)
resetCause = SOFTWARE_RESET;
else if (!RCONbits.TO)
resetCause = WDT_RESET;
else if (!RCONbits.PD)
resetCause = POWER_DOWN_RESET;
else if (!RCONbits.BOR)
resetCause = BROWN_OUT_RESET;
RCON = 0;
}
/*
@purpose: Print last reset cause
@parameters: void
@return: void
@version: V0.1
*/
void
printResetCause()
{
unsigned const char* strings[4] = {"SOFT", "WDT", "PWRD", "BROWN"};
char buffer[30];
sprintf(buffer, "Cause: %s\r", strings[resetCause]);
Debug.Send(buffer);
}