-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
289 lines (236 loc) · 7.78 KB
/
main.cpp
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
/***** Includes *****/
#include <assert.h>
#include <Ash500Parser.h>
#include <stdlib.h>
#include <Board.h>
#include <xdc/runtime/System.h>
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Event.h>
#include <ti/sysbios/knl/Task.h>
/* Drivers */
#include <ti/drivers/rf/RF.h>
#include <ti/drivers/PIN.h>
#include <ti/display/Display.h>
#include <ti/devices/cc13x0/driverlib/rf_data_entry.h>
#include <ti/devices/cc13x0/driverlib/rf_prop_cmd.h>
#include <ti/devices/cc13x0/driverlib/rf_prop_mailbox.h>
#include <ti/devices/cc13x0/driverlib/rf_mailbox.h>
#include "smartrf_settings/smartrf_settings.h"
#include "arraylist.h"
#define RAW_PAYLOAD_LENGTH 24 /* Max length byte the radio will accept */
Display_Handle lcd;
Display_Handle uart;
PIN_Handle pinHandle;
PIN_State pinState;
RF_Object rfObject;
RF_Handle rfHandle;
PIN_Config pinTable[] =
{
Board_PIN_LED1 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
Board_BUTTON0 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
Board_BUTTON1 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
PIN_TERMINATE
};
extern const Event_Handle applicationEvent;
extern const Task_Handle mainTask;
struct RxQueueItem : public rfc_dataEntry_t
{
uint8_t data[RAW_PAYLOAD_LENGTH + 4];
RxQueueItem();
};
struct SensorListEntry
{
uint8_t serial;
ratmr_t timestamp;
bool operator==(const SensorListEntry& other) const;
};
ArrayList<SensorListEntry, 4> sensors;
RxQueueItem rxItem;
dataQueue_t dataQueue =
{
reinterpret_cast<uint8_t*>(&rxItem),
NULL
};
rfc_propRxOutput_t rxMetaData;
Ash500Parser sensor;
rfc_CMD_PROP_RX_ADV_t propRxAdvCommand = {
.commandNo = CMD_PROP_RX_ADV, //!< The command ID number 0x3804
.pNextOp = NULL, //!< Pointer to the next operation to run after this operation is done
.startTrigger.triggerType = TRIG_NOW,//!< The type of trigger
.condition.rule = COND_NEVER,
.pktConf.bFsOff = 0x0,
.pktConf.bRepeatOk = 0x0,
.pktConf.bRepeatNok = 0x0,
.pktConf.bUseCrc = 0x0,
.pktConf.bCrcIncSw = 0x0,
.pktConf.bCrcIncHdr = 0x0,
.pktConf.endType = 0x0,
.pktConf.filterOp = 0x0,
.rxConf.bAutoFlushIgnored = 0x1,
.rxConf.bAutoFlushCrcErr = 0x0,
.rxConf.bIncludeHdr = 0x0,
.rxConf.bIncludeCrc = 0x0,
.rxConf.bAppendRssi = 0x0,
.rxConf.bAppendTimestamp = 0x0,
.rxConf.bAppendStatus = 0x1,
.syncWord0 = 0x159,
.syncWord1 = 0x15a,
.maxPktLen = RAW_PAYLOAD_LENGTH,
.hdrConf.numHdrBits = 0,
.hdrConf.lenPos = 0,
.hdrConf.numLenBits = 0,
.addrConf.addrType = 0,
.addrConf.addrSize = 0,
.addrConf.addrPos = 0,
.addrConf.numAddr = 0,
.lenOffset = 0,
.endTrigger.triggerType = TRIG_NEVER,
.pAddr = NULL,
.pQueue = &dataQueue,
.pOutput = reinterpret_cast<uint8_t*>(&rxMetaData)
};
void dumpMemory(const void* data, uint32_t length);
extern "C" void mainTaskFunction(UArg arg0, UArg arg1)
{
Display_Params lcdParams;
Display_Params_init(&lcdParams);
lcdParams.lineClearMode = DISPLAY_CLEAR_NONE;
lcd = Display_open(Display_Type_LCD, &lcdParams);
uart = Display_open(Display_Type_UART, NULL);
Display_clear(lcd);
Display_printf(lcd, 0, 0, " %u.%2uMhz scan..",
RF_cmdFs.frequency,
(((uint32_t)RF_cmdFs.fractFreq) * 100) / 65536);
Display_printf(uart, 0, 0, "Serial, Temperature, Humidity, RSSI, Timestamp\n");
RF_Params rfParams;
RF_Params_init(&rfParams);
/* Request access to the radio */
rfHandle = RF_open(&rfObject, &RF_prop, (RF_RadioSetup*)&RF_cmdPropRadioDivSetup, &rfParams);
assert(rfHandle != NULL);
/* Set the frequency */
RF_runCmd(rfHandle, (RF_Op*)&RF_cmdFs, RF_PriorityNormal, NULL, 0);
assert(RF_cmdFs.status == DONE_OK);
for (;;)
{
rxItem.status = DATA_ENTRY_PENDING;
RF_EventMask events = RF_runCmd(rfHandle, (RF_Op*)&propRxAdvCommand, RF_PriorityNormal, NULL, 0);
switch (reinterpret_cast<volatile RF_Op*>(&propRxAdvCommand)->status)
{
case PROP_DONE_OK:
// Wanted case
break;
case ERROR_SYNTH_PROG:
continue;
default:
assert(false);
}
rfc_propRxStatus_t status;
memcpy(&status, rxItem.data + RAW_PAYLOAD_LENGTH, sizeof(status));
System_printf("Sync: %x, RSSI: %i, RAT: %u ", status.status.syncWordId, rxMetaData.lastRssi, rxMetaData.timeStamp);
uint8_t decodedBytes = Ash500Parser::decodeManchester(rxItem.data, rxItem.data, 20);
if (decodedBytes != 20)
{
System_printf("Manchester violation after %u bytes.\n", decodedBytes);
System_flush();
continue;
}
/* 1 bit has been used for the sync word. Restore
* the message before decoding.
*/
for (int32_t i = 11; i > 0; i--)
{
rxItem.data[i] = rxItem.data[i] >> 1;
rxItem.data[i] |= (rxItem.data[i-1] & 0x01) << 7;
}
rxItem.data[0] = rxItem.data[0] >> 1;
if (status.status.syncWordId == 0)
{
rxItem.data[0] |= ((uint8_t)0x01 << 7);
}
sensor.parseData(rxItem.data);
if (sensor.hasErrors() && (sensor.error() == Ash500Parser::ParityError))
{
System_printf("Parity error\n");
System_flush();
continue;
}
System_printf("Id %u, Temp: %u.%u C, Hum: %u\n",
sensor.serial(),
sensor.temperature() / 10,
sensor.temperature() % 10,
sensor.humidity());
System_flush();
SensorListEntry entry;
entry.serial = sensor.serial();
entry.timestamp = rxMetaData.timeStamp;
int32_t sensorIndex = sensors.indexOf(entry);
if (sensorIndex == sensors.InvalidIndex)
{
sensorIndex = sensors.length();
sensors.append(entry);
}
else
{
sensors[sensorIndex] = entry;
}
Display_printf(lcd, (sensorIndex * 3) + 3, 0, "%02x: %u.%u C %u%%",
sensor.serial(),
sensor.temperature() / 10,
sensor.temperature() % 10,
sensor.humidity());
Display_printf(lcd, (sensorIndex * 3) + 4, 4, "%idBm",
rxMetaData.lastRssi);
Display_printf(uart, 0, 0, "%u, %u.%u, %u, %i, %u\n",
sensor.serial(),
sensor.temperature() / 10,
sensor.temperature() % 10,
sensor.humidity(),
rxMetaData.lastRssi,
rxMetaData.timeStamp);
}
}
void handleButtonEvent(PIN_Handle handle, PIN_Id pin)
{
switch (pin)
{
case Board_BUTTON0:
break;
case Board_BUTTON1:
break;
}
}
void dumpMemory(const void* data, uint32_t length)
{
System_printf("Raw: ");
for (uint32_t i = 0; i < length; i++)
{
System_printf(BYTE_TO_BINARY_PATTERN" ", BYTE_TO_BINARY(reinterpret_cast<const uint8_t*>(data)[i]));
}
System_printf("\n");
}
RxQueueItem::RxQueueItem()
{
config.lenSz = 0;
config.type = DATA_ENTRY_TYPE_GEN;
status = DATA_ENTRY_PENDING;
pNextEntry = reinterpret_cast<uint8_t*>(this);
length = sizeof(data);
}
bool SensorListEntry::operator==(const SensorListEntry& other) const
{
return this->serial == other.serial;
}
int main(void)
{
/* Call driver init functions. */
Board_initGeneral();
Display_init();
/* Open LED pins */
pinHandle = PIN_open(&pinState, pinTable);
assert(pinHandle != NULL);
PIN_Status status = PIN_registerIntCb(pinHandle, &handleButtonEvent);
assert(status == PIN_SUCCESS);
/* Start BIOS */
BIOS_start();
return (0);
}