-
Notifications
You must be signed in to change notification settings - Fork 0
/
SensorTask.c
770 lines (480 loc) · 15.8 KB
/
SensorTask.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
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <xdc/std.h>
#include <xdc/runtime/System.h>
/* BIOS Header files */
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/knl/Semaphore.h>
#include <ti/sysbios/knl/Event.h>
#include <ti/sysbios/knl/Clock.h>
/* TI-RTOS Header files */
#include <ti/drivers/I2C.h>
#include <ti/drivers/UART.h>
#include <ti/drivers/UART2.h>
#include <ti/drivers/NVS.h>
#include <ti/drivers/ADC.h>
#include <ti/drivers/SPI.h>
#include <ti/drivers/Watchdog.h>
#include <ti/devices/DeviceFamily.h>
#include DeviceFamily_constructPath(driverlib/cpu.h)
#include DeviceFamily_constructPath(driverlib/sys_ctrl.h)
#include DeviceFamily_constructPath(driverlib/aon_batmon.h)
#include <ti/devices/cc13x2_cc26x2/driverlib/aux_adc.h>
/* Board Header files */
#include "ti_drivers_config.h"
/* Application Header files */
#include "SensorTask.h"
#include "Protocol.h"
#include "uart_usb_in_out.h"
#include "board_define.h"
#include "si7051.h"
#include "ecg_algorithm.h"
#include "max30003.h"
#include "heartrate_spo2_algorithm.h"
#include "max30101.h"
#define SENSOR_TASK_STACK_SIZE 1024
#define SENSOR_TASK_TASK_PRIORITY 3
#define SENSOR_TASK_EVENT_ALL 0xFFFFFFFF
#define SENSOR_TASK_SENSOR_UPDATE (uint32_t)(1 << 0)
#define TIMER_TIMEOUT 1000
#define WATCHDOG_TIMEOUT_MS 3000
#define BUTTON_PRESS_TIMER_COUNT 5
#define BUTTON_RPESS_COUNT 3
PIN_Config pinTable[] = {
GPIO_LED_0 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
GPIO_LED_1 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL
| PIN_DRVSTR_MAX,
GPIO_BAT_EN | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL
| PIN_DRVSTR_MAX,
GPIO_SENSOR_POWER_EN | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL
| PIN_DRVSTR_MAX,
SPI_CS_GPIO | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL
| PIN_DRVSTR_MAX,
PIN_TERMINATE
};
PIN_Config buttonTable[] = {
GPIO_BTN | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
PIN_TERMINATE
};
static Task_Params sensorTaskParams;
Task_Struct sensorTask; /* not static so you can see in ROV */
static uint8_t sensorTaskStack[SENSOR_TASK_STACK_SIZE];
Event_Struct sensorEvent; /* not static so you can see in ROV */
static Event_Handle sensorEventHandle;
Clock_Struct sensorTimerClock;
Watchdog_Handle watchdogHandle;
static PacketSendRequestCallback packetSendRequestCallback;
static PIN_State pinState;
static PIN_Handle pinHandle;
static PIN_State buttonState;
static PIN_Handle buttonHandle;
static I2C_Handle i2c;
static I2C_Params i2cParams;
static SPI_Params spiParams;
static SPI_Handle masterSpi;
static uint8_t uart_read_buf = 0x00;
static UART_Handle uart;
static uint8_t init_state = 0x00;
static int16_t temperature = 0;
static uint8_t battery_voltage = 0;
static uint8_t ir_heart_rate = 0;
static uint8_t ir_heart_rate_count = 0;
static uint8_t green_heart_rate = 0;
static uint8_t green_heart_rate_count = 0;
static uint8_t spo2 = 0;
static uint8_t spo2_count = 0;
static uint8_t ecg_heart_rate = 0;
static uint8_t ecg_heart_rate_count = 0;
static uint8_t on_off_mode = 0x01;
static uint8_t button_press_state = 0x00;
static uint8_t button_press_count = 0;
static int8_t button_press_timer_count = 0;
static uint8_t payload_size = 0;
static uint8_t payload[115];
static uint8_t sensor_task_release_state = 0x00;
extern uint8_t radio_init;
extern uint8_t mac_address[8];
extern uint32_t collection_cycle_timeout_count;
extern uint32_t collection_cycle_timer_count;
static void uartReadCallback(UART_Handle handle, void *rxBuf, size_t size);
void scCtrlReadyCallback(void)
{
}
void scTaskAlertCallback(void)
{
}
void watchdogCallback(uintptr_t watchdogHandle)
{
while (1)
{
}
}
static void buttonCallback(PIN_Handle handle, PIN_Id pinId)
{
if (init_state)
{
button_press_timer_count = BUTTON_PRESS_TIMER_COUNT;
PIN_setOutputValue(pinHandle, GPIO_LED_1, 1);
button_press_state = 0x01;
}
}
static void sensorTimerClockCallBack(UArg arg0)
{
if (init_state)
{
collection_cycle_timeout_count++;
if (on_off_mode == 0x01)
{
if (collection_cycle_timeout_count >= collection_cycle_timer_count)
{
if (sensor_task_release_state == 0x00)
{
sensor_task_release_state = 0x01;
Event_post(sensorEventHandle, SENSOR_TASK_SENSOR_UPDATE);
}
collection_cycle_timeout_count = 0;
}
}
else
{
collection_cycle_timeout_count = 0;
}
if (button_press_state)
{
PIN_setOutputValue(pinHandle, GPIO_LED_1, 0);
button_press_state = 0x00;
button_press_count++;
}
button_press_timer_count -= 1;
if (button_press_timer_count > 0)
{
if (button_press_count >= BUTTON_RPESS_COUNT)
{
if (on_off_mode == 0x01)
{
on_off_mode = 0x00;
}
else
{
on_off_mode = 0x01;
}
collection_cycle_timer_count = 0;
PIN_setOutputValue(pinHandle, GPIO_LED_0, 1);
button_press_timer_count = 0;
button_press_count = 0;
}
}
else
{
PIN_setOutputValue(pinHandle, GPIO_LED_0, 0);
button_press_count = 0;
}
}
Watchdog_clear(watchdogHandle);
}
static void wait_ms(uint32_t wait)
{
Task_sleep(wait * 1000 / Clock_tickPeriod);
}
static void initErrorUpdate(void)
{
for (uint8_t i = 0; i < 4; i++)
{
PIN_setOutputValue(pinHandle, GPIO_LED_0,
!PIN_getOutputValue(GPIO_LED_0));
PIN_setOutputValue(pinHandle, GPIO_LED_1,
!PIN_getOutputValue(GPIO_LED_1));
wait_ms(500);
}
PIN_setOutputValue(pinHandle, GPIO_LED_0, 1);
PIN_setOutputValue(pinHandle, GPIO_LED_1, 1);
}
static uint8_t update_Heartrate_SPO2()
{
uint8_t i2c_state = 0x01;
uint16_t printCount = 0;
uint32_t red = 0x00;
uint32_t ir = 0x00;
uint32_t green = 0x00;
uint8_t ppgErrorCount = 0;
i2c_state = i2c_state & set_max30101_normal_mode();
setHeartrateMinMax(40, 180);
if (i2c_state)
{
for (uint32_t i = 0; i < 3000; i++)
{
get_max30101_ir_red_green(&ir, &red, &green);
if (green > MAX30101_GREEN_PROXY_THR)
{
updateHeartateGreen(green);
if (updateHeartateIR(ir, red) == 0x01)
{
updateSpo2RedIrAcc();
}
ppgErrorCount = 0;
}
else
{
ppgErrorCount++;
if (ppgErrorCount > MAX30101_GREEN_PROXY_ERROR_COUNT)
{
initHeartrate();
ppgErrorCount = 0;
}
}
printCount++;
if (printCount > 500)
{
getHeartrateValue(&ir_heart_rate, &ir_heart_rate_count,
&green_heart_rate, &green_heart_rate_count);
getSpo2Value(&spo2, &spo2_count);
set_uart_usb_in_out_green_heartrate(green_heart_rate,
green_heart_rate_count);
set_uart_usb_in_out_ir_heartrate(ir_heart_rate,
ir_heart_rate_count);
set_uart_usb_in_out_spo2(spo2, spo2_count);
printCount = 0;
}
wait_ms(20);
}
}
initHeartrate();
i2c_state = i2c_state & set_max30101_sleep_mode();
return i2c_state;
}
static void update_ECG()
{
uint16_t printCount = 0;
init_ecg_algorithm();
for (uint32_t i = 0; i < 7500; i++)
{
int32_t sample = get_max30003_ecg_voltage_sample();
update_ecg_algorithm(sample);
get_ecg_heartrate(&ecg_heart_rate, &ecg_heart_rate_count);
printCount++;
if (printCount > 1250)
{
set_uart_usb_in_out_ecg_heartrate(ecg_heart_rate,
ecg_heart_rate_count);
printCount = 0;
}
wait_ms(8);
}
}
static void battery_value_read()
{
ADC_Handle adc;
ADC_Params ADCParams;
ADC_Params_init(&ADCParams);
adc = ADC_open(CONFIG_I2C_0, &ADCParams);
if (adc != NULL)
{
uint16_t adc_value;
PIN_setOutputValue(pinHandle, GPIO_BAT_EN, 1);
wait_ms(20);
int_fast16_t result = ADC_convert(adc, &adc_value);
ADC_close(adc);
PIN_setOutputValue(pinHandle, GPIO_BAT_EN, 0);
if (result == ADC_STATUS_SUCCESS)
{
uint32_t microVolt = ADC_convertRawToMicroVolts(adc, adc_value);
microVolt *= 3;
microVolt /= 100000;
battery_voltage = microVolt;
}
}
}
static void i2cScan()
{
uint8_t txBuffer[2] = { 0x00 };
uint8_t rxBuffer[2] = { 0x00 };
I2C_Transaction i2cTransaction;
i2cTransaction.writeBuf = txBuffer;
i2cTransaction.readBuf = rxBuffer;
i2cTransaction.writeCount = 1;
i2cTransaction.readCount = 0;
for (uint16_t i = 0; i < 128; i++)
{
i2cTransaction.slaveAddress = i;
if (I2C_transfer(i2c, &i2cTransaction))
{
set_uart_usb_in_out_find_i2c_address(i);
wait_ms(20);
}
}
}
static void initSensor(void)
{
uint8_t i2c_state = 0x01;
PIN_setOutputValue(pinHandle, GPIO_SENSOR_POWER_EN, 1);
wait_ms(1000);
I2C_Params_init(&i2cParams);
i2cParams.bitRate = I2C_400kHz;
i2c = I2C_open(CONFIG_I2C_0, &i2cParams);
if (i2c == NULL)
{
initErrorUpdate();
SysCtrlSystemReset();
}
SPI_Params_init(&spiParams);
spiParams.mode = SPI_MASTER;
spiParams.transferMode = SPI_MODE_BLOCKING;
masterSpi = SPI_open(CONFIG_SPI_0, &spiParams);
if (masterSpi == NULL)
{
initErrorUpdate();
SysCtrlSystemReset();
}
i2cScan();
wait_ms(100);
set_si7051_i2c_instance(i2c);
i2c_state = i2c_state & init_si7051();
i2c_state = i2c_state & get_si7051_temperature(&temperature);
wait_ms(100);
set_max30101_i2c_instance(i2c);
i2c_state = i2c_state & init_max30101();
if (!i2c_state)
{
initErrorUpdate();
}
set_max30003_gpio_instance(pinHandle);
set_max30003_spi_instance(masterSpi);
init_max30003_mode_0();
}
static void initUartUSB()
{
UART_Params uartParams;
UART_Params_init(&uartParams);
uartParams.writeDataMode = UART_DATA_BINARY;
uartParams.readDataMode = UART_DATA_BINARY;
uartParams.readMode = UART_MODE_CALLBACK;
uartParams.readCallback = uartReadCallback;
uartParams.readReturnMode = UART_RETURN_FULL;
uartParams.readEcho = UART_ECHO_OFF;
uartParams.baudRate = 115200;
uart = UART_open(CONFIG_UART_0, &uartParams);
set_uart_usb_in_out_uart_instance(uart);
}
static void sensorTaskFunction(UArg arg0, UArg arg1)
{
UART_init();
ADC_init();
I2C_init();
SPI_init();
pinHandle = PIN_open(&pinState, pinTable);
if (!pinHandle)
{
SysCtrlSystemReset();
}
PIN_setOutputValue(pinHandle, GPIO_LED_0, 1);
PIN_setOutputValue(pinHandle, GPIO_LED_1, 1);
PIN_setOutputValue(pinHandle, GPIO_BAT_EN, 0);
PIN_setOutputValue(pinHandle, GPIO_SENSOR_POWER_EN, 0);
buttonHandle = PIN_open(&buttonState, buttonTable);
if (!buttonHandle)
{
SysCtrlSystemReset();
}
PIN_registerIntCb(buttonHandle, &buttonCallback);
initUartUSB();
while (radio_init == 0x00)
{
wait_ms(100);
}
set_uart_usb_in_out_write_string("\n\n");
set_uart_usb_in_out_write_string("Start AxDen PPG ECG Example");
set_uart_usb_in_out_mac_address(mac_address);
battery_value_read();
initSensor();
set_uart_usb_in_out_battery(battery_voltage);
set_uart_usb_in_out_temperature(temperature);
PIN_setOutputValue(pinHandle, GPIO_LED_0, 0);
PIN_setOutputValue(pinHandle, GPIO_LED_1, 0);
on_off_mode = 0x01;
init_state = 0x01;
while (1)
{
uint32_t events = Event_pend(sensorEventHandle, 0,
SENSOR_TASK_EVENT_ALL,
BIOS_WAIT_FOREVER);
if (events == SENSOR_TASK_SENSOR_UPDATE)
{
battery_value_read();
get_si7051_temperature(&temperature);
update_Heartrate_SPO2();
update_ECG();
if (packetSendRequestCallback)
{
payload_size = 0;
memset(payload, 0x00, sizeof(payload));
payload_size = sprintf((char*) payload, "%d.%d,",
(battery_voltage / 10),
(battery_voltage % 10));
payload_size += sprintf((char*) payload + payload_size,
"%d.%d,", (temperature / 10),
abs(temperature % 10));
payload_size += sprintf((char*) payload + payload_size, "%d,",
green_heart_rate);
payload_size += sprintf((char*) payload + payload_size, "%d,",
green_heart_rate_count);
payload_size += sprintf((char*) payload + payload_size, "%d,",
ir_heart_rate);
payload_size += sprintf((char*) payload + payload_size, "%d,",
ir_heart_rate_count);
payload_size += sprintf((char*) payload + payload_size, "%d,",
ecg_heart_rate);
payload_size += sprintf((char*) payload + payload_size, "%d,",
ecg_heart_rate_count);
packetSendRequestCallback(payload, payload_size);
}
}
sensor_task_release_state = 0x00;
}
}
void SensorTask_init(void)
{
Event_Params eventParam;
Event_Params_init(&eventParam);
Event_construct(&sensorEvent, &eventParam);
sensorEventHandle = Event_handle(&sensorEvent);
Task_Params_init(&sensorTaskParams);
sensorTaskParams.stackSize = SENSOR_TASK_STACK_SIZE;
sensorTaskParams.priority = SENSOR_TASK_TASK_PRIORITY;
sensorTaskParams.stack = &sensorTaskStack;
Task_construct(&sensorTask, sensorTaskFunction, &sensorTaskParams, NULL);
Watchdog_init();
Watchdog_Params watchdogParams;
Watchdog_Params_init(&watchdogParams);
watchdogParams.callbackFxn = (Watchdog_Callback) watchdogCallback;
watchdogParams.debugStallMode = Watchdog_DEBUG_STALL_ON;
watchdogParams.resetMode = Watchdog_RESET_ON;
watchdogHandle = Watchdog_open(CONFIG_WATCHDOG_0, &watchdogParams);
if (watchdogHandle == NULL)
{
SysCtrlSystemReset();
}
uint32_t reloadValue = Watchdog_convertMsToTicks(watchdogHandle,
WATCHDOG_TIMEOUT_MS);
if (reloadValue != 0)
{
Watchdog_setReload(watchdogHandle, reloadValue);
}
Clock_Params clockParams;
Clock_Params_init(&clockParams);
clockParams.period = TIMER_TIMEOUT * 1000 / Clock_tickPeriod;
clockParams.startFlag = TRUE;
Clock_construct(&sensorTimerClock, sensorTimerClockCallBack,
TIMER_TIMEOUT * 1000 / Clock_tickPeriod,
&clockParams);
}
void SensorTask_registerPacketSendRequestCallback(
PacketSendRequestCallback callback)
{
packetSendRequestCallback = callback;
}
static void uartReadCallback(UART_Handle handle, void *rxBuf, size_t size)
{
UART_read(handle, &uart_read_buf, 1);
}