-
Notifications
You must be signed in to change notification settings - Fork 0
/
RadioTask.c
384 lines (242 loc) · 8.56 KB
/
RadioTask.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
#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>
/* Drivers */
#include <ti/drivers/pin/PINCC26XX.h>
#include <ti/drivers/rf/RF.h>
#include <ti/drivers/PIN.h>
#include <ti/drivers/power/PowerCC26XX.h>
/* Board Header files */
#include "ti_drivers_config.h"
#include <ti/devices/DeviceFamily.h>
#include DeviceFamily_constructPath(driverlib/sys_ctrl.h)
/* EasyLink API Header files */
#include "easylink/EasyLink.h"
#include "RadioTask.h"
#include "SensorTask.h"
#include "Protocol.h"
#include "board_define.h"
#define RADIO_TASK_STACK_SIZE 1024
#define RADIO_TASK_TASK_PRIORITY 3
#define RADIO_TASK_EVENT_ALL 0xFFFFFFFF
#define RADIO_TASK_EVENT_ACK (uint32_t)(1 << 1)
#define RADIO_TASK_EVENT_ACK_TIMEOUT (uint32_t)(1 << 2)
#define ACK_TIME_OUT 160
const PIN_Config RADIO_TASK_GPIO_CONFIG[] = {
CC1190_PA_EN | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
CC1190_LNA_EN | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL
| PIN_DRVSTR_MAX,
CC1190_HGM | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL
| PIN_DRVSTR_MIN,
PIN_TERMINATE};
const uint8_t addressFilterValue[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0F };
const int8_t TX_POWER = 0;
const uint16_t COMPANY_ID = 0x0000;
const uint16_t DEVICE_ID = 0x0000;
static Task_Params radioTaskParams;
Task_Struct radioTask; /* not static so you can see in ROV */
static uint8_t radioTaskStack[RADIO_TASK_STACK_SIZE];
Event_Struct radioEvent; /* not static so you can see in ROV */
static Event_Handle radioEventHandle;
Semaphore_Struct radioAccessSem; /* not static so you can see in ROV */
static Semaphore_Handle radioAccessSemHandle;
static PIN_Handle radioTaskPinHandle;
static PIN_State radioTaskPinState;
static EasyLink_TxPacket txPacket;
static radio_packet_protocol_t radio_packet_protocol;
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
#ifdef EXAMPLE_RX
static void wait_ms(uint32_t wait)
{
Task_sleep(wait * 1000 / Clock_tickPeriod);
}
#endif
static void sendPacketCallback(uint8_t *payload, uint8_t payload_size)
{
#ifdef EXAMPLE_TX
memset(radio_packet_protocol.Packet.paylaod, 0x00,
sizeof(radio_packet_protocol.Packet.paylaod));
memcpy(radio_packet_protocol.Packet.paylaod, payload, payload_size);
Semaphore_post(radioAccessSemHandle);
#endif
}
static void rxDoneCallback(EasyLink_RxPacket *rxPacket, EasyLink_Status status)
{
if (status == EasyLink_Status_Success)
{
if (rxPacket->len <= sizeof(receive_radio_packet.buffer))
{
rssi = rxPacket->rssi;
memcpy(receive_radio_packet.buffer, rxPacket->payload,
rxPacket->len);
}
Event_post(radioEventHandle, RADIO_TASK_EVENT_ACK);
}
else
{
Event_post(radioEventHandle, RADIO_TASK_EVENT_ACK_TIMEOUT);
}
}
#ifdef EXAMPLE_TX
static void radio_sensor_data_packet_send(radio_packet_protocol_t radio_packet)
{
txPacket.len = 0;
txPacket.absTime = 0;
memset(txPacket.dstAddr, 0x00, sizeof(txPacket.dstAddr));
memset(txPacket.payload, 0x00, sizeof(txPacket.payload));
memcpy(txPacket.dstAddr, addressFilterValue, 8);
memcpy(txPacket.payload, radio_packet.buffer, sizeof(radio_packet.buffer));
txPacket.len = sizeof(radio_packet.buffer);
EasyLink_setCtrl(EasyLink_Ctrl_AsyncRx_TimeOut,
EasyLink_ms_To_RadioTime(ACK_TIME_OUT));
if (EasyLink_transmit(&txPacket) != EasyLink_Status_Success)
{
SysCtrlSystemReset();
}
if (EasyLink_receiveAsync(rxDoneCallback, 0) != EasyLink_Status_Success)
{
SysCtrlSystemReset();
}
}
#endif
#ifdef EXAMPLE_RX
static void radio_sensor_data_packet_send(radio_packet_protocol_t radio_packet)
{
txPacket.len = 0;
txPacket.absTime = 0;
memset(txPacket.dstAddr, 0x00, sizeof(txPacket.dstAddr));
memset(txPacket.payload, 0x00, sizeof(txPacket.payload));
memcpy(txPacket.dstAddr, receive_radio_packet.Packet.mac_address, 8);
memcpy(txPacket.payload, radio_packet.buffer, sizeof(radio_packet.buffer));
txPacket.len = sizeof(radio_packet.buffer);
if (EasyLink_transmit(&txPacket) != EasyLink_Status_Success)
{
SysCtrlSystemReset();
}
}
#endif
static void radioTaskFunction(UArg arg0, UArg arg1)
{
//CC1190 Setup
radioTaskPinHandle = PIN_open(&radioTaskPinState, RADIO_TASK_GPIO_CONFIG);
if (radioTaskPinHandle == NULL)
{
SysCtrlSystemReset();
}
//PA LNA Enable Or Bypass Setup
PIN_setOutputValue(radioTaskPinHandle, CC1190_HGM, 1);
//LNA Pin
PINCC26XX_setMux(radioTaskPinHandle, CC1190_LNA_EN, PINCC26XX_MUX_RFC_GPO0);
//PA Pin
PINCC26XX_setMux(radioTaskPinHandle, CC1190_PA_EN, PINCC26XX_MUX_RFC_GPO1);
EasyLink_Params easyLink_params;
EasyLink_Params_init(&easyLink_params);
easyLink_params.ui32ModType = EasyLink_Phy_5kbpsSlLr;
if (EasyLink_init(&easyLink_params) != EasyLink_Status_Success)
{
SysCtrlSystemReset();
}
EasyLink_getIeeeAddr(mac_address);
EasyLink_setFrequency(920000000);
#ifdef EXAMPLE_TX
EasyLink_enableRxAddrFilter(mac_address, 8, 1);
#endif
#ifdef EXAMPLE_RX
EasyLink_enableRxAddrFilter((uint8_t*) addressFilterValue, 8, 1);
#endif
SensorTask_registerPacketSendRequestCallback(sendPacketCallback);
radio_packet_protocol.Packet.company_id[0] = COMPANY_ID >> 8;
radio_packet_protocol.Packet.company_id[1] = COMPANY_ID;
radio_packet_protocol.Packet.device_id[0] = DEVICE_ID >> 8;
radio_packet_protocol.Packet.device_id[1] = DEVICE_ID;
memcpy(radio_packet_protocol.Packet.mac_address, mac_address, 8);
radio_packet_protocol.Packet.control_number = 0;
radio_init = RADIO_INIT_SUCCESS;
#ifdef EXAMPLE_RX
if (EasyLink_receiveAsync(rxDoneCallback, 0) != EasyLink_Status_Success)
{
SysCtrlSystemReset();
}
#endif
while (1)
{
#ifdef EXAMPLE_TX
Semaphore_pend(radioAccessSemHandle, BIOS_WAIT_FOREVER);
EasyLink_setRfPower(TX_POWER);
radio_sensor_data_packet_send(radio_packet_protocol);
uint32_t events = Event_pend(radioEventHandle, 0,
RADIO_TASK_EVENT_ALL,
BIOS_WAIT_FOREVER);
if (events == RADIO_TASK_EVENT_ACK)
{
packet_receive = 0x01;
}
#endif
#ifdef EXAMPLE_RX
uint32_t events = Event_pend(radioEventHandle, 0,
RADIO_TASK_EVENT_ALL,
BIOS_WAIT_FOREVER);
if (events == RADIO_TASK_EVENT_ACK)
{
EasyLink_setRfPower(TX_POWER);
memset(radio_packet_protocol.Packet.paylaod, 0x00,
sizeof(radio_packet_protocol.Packet.paylaod));
if (payload_size == 0)
{
payload_size = sprintf(
(char*) radio_packet_protocol.Packet.paylaod, "%s",
"Pong");
}
else
{
memcpy(radio_packet_protocol.Packet.paylaod, payload,
payload_size);
}
payload_index = 0;
payload_size = 0;
memset(payload, 0x00, PAYLOAD_SIZE);
radio_sensor_data_packet_send(radio_packet_protocol);
packet_receive = 0x01;
}
wait_ms(500);
if (EasyLink_receiveAsync(rxDoneCallback, 0) != EasyLink_Status_Success)
{
SysCtrlSystemReset();
}
#endif
}
}
void RadioTask_init(void)
{
Semaphore_Params semParam;
Semaphore_Params_init(&semParam);
Semaphore_construct(&radioAccessSem, 0, &semParam);
radioAccessSemHandle = Semaphore_handle(&radioAccessSem);
Event_Params eventParam;
Event_Params_init(&eventParam);
Event_construct(&radioEvent, &eventParam);
radioEventHandle = Event_handle(&radioEvent);
Task_Params_init(&radioTaskParams);
radioTaskParams.stackSize = RADIO_TASK_STACK_SIZE;
radioTaskParams.priority = RADIO_TASK_TASK_PRIORITY;
radioTaskParams.stack = &radioTaskStack;
Task_construct(&radioTask, radioTaskFunction, &radioTaskParams, NULL);
}