-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSensorTask.c
499 lines (316 loc) · 9.88 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
#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/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 "board_define.h"
#include "uart_usb_in_out.h"
#define SENSOR_TASK_STACK_SIZE 1024
#define SENSOR_TASK_TASK_PRIORITY 3
#define SENSOR_TASK_EVENT_ALL 0xFFFFFFFF
#define SENSOR_TASK_RADIO_PACKET_SEND (uint32_t)(1 << 1)
#define SENSOR_TASK_RADIO_PACKET_RECEIVE (uint32_t)(1 << 2)
#define TIMER_TIMEOUT 100
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_LED_2 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL
| PIN_DRVSTR_MAX,
GPIO_0 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL
| PIN_DRVSTR_MAX,
GPIO_1 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL
| PIN_DRVSTR_MAX,
PIN_TERMINATE
};
PIN_Config buttonTable[] = {
GPIO_BUTTON_0 | PIN_INPUT_EN | PIN_PULLDOWN | PIN_IRQ_NEGEDGE,
GPIO_BUTTON_1 | PIN_INPUT_EN | PIN_PULLDOWN
| 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;
static PacketSendRequestCallback packetSendRequestCallback;
static PIN_State pinState;
static PIN_Handle pinHandle;
static PIN_State buttonState;
static PIN_Handle buttonHandle;
static I2C_Handle i2c;
static char uart2_read_buf = 0x00;
static UART2_Handle uart2;
static uint8_t init_state = 0x00;
#ifdef EXAMPLE_TX
static uint16_t tx_timer_count = 0;
static uint8_t payload_index;
static uint8_t payload_size;
static uint8_t payload[PAYLOAD_SIZE];
#endif
extern uint8_t radio_init;
extern uint8_t mac_address[MAC_ADDRESS_SIZE];
extern uint8_t packet_receive;
extern int8_t rssi;
extern radio_packet_protocol_t receive_radio_packet;
#ifdef EXAMPLE_RX
extern uint8_t payload_index;
extern uint8_t payload_size;
extern uint8_t payload[PAYLOAD_SIZE];
#endif
void uart2ReadCallback(UART2_Handle handle, void *rxBuf, size_t size,
void *userArg, int_fast16_t status);
static void wait_ms(uint32_t wait)
{
Task_sleep(wait * 1000 / Clock_tickPeriod);
}
static void buttonCallback(PIN_Handle handle, PIN_Id pinId)
{
if (init_state)
{
if (pinId == GPIO_BUTTON_0)
{
PIN_setOutputValue(pinHandle, GPIO_LED_0,
!PIN_getOutputValue(GPIO_LED_0));
}
else if (pinId == GPIO_BUTTON_1)
{
PIN_setOutputValue(pinHandle, GPIO_LED_1,
!PIN_getOutputValue(GPIO_LED_1));
}
}
}
static void sensorTimerClockCallBack(UArg arg0)
{
if (init_state)
{
#ifdef EXAMPLE_TX
tx_timer_count += 1;
if (tx_timer_count > TX_TIMEOUT_COUNT)
{
Event_post(sensorEventHandle, SENSOR_TASK_RADIO_PACKET_SEND);
tx_timer_count = 0;
}
#endif
if (packet_receive)
{
Event_post(sensorEventHandle, SENSOR_TASK_RADIO_PACKET_RECEIVE);
packet_receive = 0x00;
}
}
}
static void readVoltage()
{
ADC_Handle adc;
ADC_Params ADCParams;
ADC_Params_init(&ADCParams);
adc = ADC_open(CONFIG_I2C_0, &ADCParams);
if (adc != NULL)
{
uint16_t adc_value;
int_fast16_t result = ADC_convert(adc, &adc_value);
ADC_close(adc);
if (result == ADC_STATUS_SUCCESS)
{
uint32_t microVolt = ADC_convertRawToMicroVolts(adc, adc_value);
microVolt *= 2;
microVolt /= 100000;
int16_t voltage = microVolt / 10;
set_uart_usb_in_out_voltage(voltage);
}
}
}
static void i2cScan()
{
I2C_Params i2cParams;
I2C_Transaction i2cTransaction;
uint8_t txBuffer[2] = { 0x00 };
uint8_t rxBuffer[2] = { 0x00 };
I2C_Params_init(&i2cParams);
i2cParams.bitRate = I2C_400kHz;
i2c = I2C_open(CONFIG_I2C_0, &i2cParams);
if (i2c == NULL)
{
SysCtrlSystemReset();
}
set_uart_usb_in_out_write_string("Start I2C Scan");
wait_ms(10);
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(10);
}
}
I2C_close(i2c);
}
static void initUartUSB()
{
UART2_Params uart2Params;
UART2_Params_init(&uart2Params);
uart2Params.readMode = UART2_Mode_CALLBACK;
uart2Params.readCallback = uart2ReadCallback;
uart2Params.baudRate = 9600;
uart2 = UART2_open(CONFIG_UART2_0, &uart2Params);
set_uart_usb_in_out_uart2_instance(uart2);
}
static void sensorTaskFunction(UArg arg0, UArg arg1)
{
ADC_init();
I2C_init();
pinHandle = PIN_open(&pinState, pinTable);
if (!pinHandle)
{
SysCtrlSystemReset();
}
PIN_setOutputValue(pinHandle, GPIO_LED_0, 0);
PIN_setOutputValue(pinHandle, GPIO_LED_1, 0);
PIN_setOutputValue(pinHandle, GPIO_LED_2, 0);
PIN_setOutputValue(pinHandle, GPIO_0, 0);
PIN_setOutputValue(pinHandle, GPIO_1, 0);
buttonHandle = PIN_open(&buttonState, buttonTable);
if (!buttonHandle)
{
SysCtrlSystemReset();
}
PIN_registerIntCb(buttonHandle, &buttonCallback);
initUartUSB();
set_uart_usb_in_out_write_string("\r\n\r\n");
wait_ms(10);
#ifdef EXAMPLE_TX
set_uart_usb_in_out_write_string("Start AxDen TX Example");
wait_ms(10);
#endif
#ifdef EXAMPLE_RX
set_uart_usb_in_out_write_string("Start AxDen RX Example");
wait_ms(10);
#endif
i2cScan();
readVoltage();
PIN_setOutputValue(pinHandle, GPIO_LED_0, 1);
PIN_setOutputValue(pinHandle, GPIO_LED_1, 1);
PIN_setOutputValue(pinHandle, GPIO_LED_2, 1);
while (radio_init == RADIO_INIT_WAIT)
{
wait_ms(100);
}
set_uart_usb_in_out_mac_address(mac_address);
UART2_read(uart2, &uart2_read_buf, 1, NULL);
#ifdef EXAMPLE_RX
set_uart_usb_in_out_write_string("Start RX");
wait_ms(10);
#endif
init_state = 0x01;
while (1)
{
uint32_t events = Event_pend(sensorEventHandle, 0,
SENSOR_TASK_EVENT_ALL,
BIOS_WAIT_FOREVER);
#ifdef EXAMPLE_TX
if (events == SENSOR_TASK_RADIO_PACKET_SEND)
{
set_uart_usb_in_out_write_string("\r\nPacket Send");
wait_ms(10);
if (payload_size == 0)
{
payload_size = sprintf((char*) payload, "%s", "Ping");
}
if (packetSendRequestCallback)
{
packetSendRequestCallback(payload, payload_size);
payload_index = 0;
payload_size = 0;
memset(payload, 0x00, PAYLOAD_SIZE);
}
}
else if (events == SENSOR_TASK_RADIO_PACKET_RECEIVE)
{
set_uart_usb_in_out_payload(receive_radio_packet, rssi);
}
#endif
#ifdef EXAMPLE_RX
if (events == SENSOR_TASK_RADIO_PACKET_RECEIVE)
{
set_uart_usb_in_out_payload(receive_radio_packet, rssi);
}
#endif
PIN_setOutputValue(pinHandle, GPIO_LED_2,
!PIN_getOutputValue(GPIO_LED_2));
}
}
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);
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;
}
void uart2ReadCallback(UART2_Handle handle, void *rxBuf, size_t size,
void *userArg, int_fast16_t status)
{
if (status == UART2_STATUS_SUCCESS)
{
UART2_read(handle, &uart2_read_buf, 1, NULL);
payload[payload_index++] = uart2_read_buf;
if (payload_size + 1 > PAYLOAD_SIZE)
{
payload_size = PAYLOAD_SIZE;
}
else
{
payload_size += 1;
}
if (payload_index >= PAYLOAD_SIZE)
{
payload_index = 0;
}
}
}