1 #ifdef DeviceFamily_CC13X0
13 #include <ti/devices/DeviceFamily.h>
14 #include DeviceFamily_constructPath(driverlib/rf_data_entry.h)
15 #include DeviceFamily_constructPath(driverlib/rf_prop_mailbox.h)
16 #include <ti/drivers/rf/RF.h>
17 #include "smartrf_settings/smartrf_settings.h"
25 #define RX_MAX_BUFFER_LENGTH 256
26 #define RF_TERMINATION_EVENT_MASK (RF_EventLastCmdDone | RF_EventLastFGCmdDone | RF_EventCmdAborted | RF_EventCmdStopped | RF_EventCmdCancelled)
28 #define DEBUG_DUMP_PACKETS
30 static RF_Object rfObject;
31 static RF_Handle rfHandle;
32 static RF_CmdHandle rxCommandHandle;
34 static uint8_t rxBuffer[
sizeof(rfc_dataEntryPartial_t) + RX_MAX_BUFFER_LENGTH]
__attribute__((aligned(4)));
35 static rfc_dataEntryPartial_t* pDataEntry = (rfc_dataEntryPartial_t*)& rxBuffer;
36 static dataQueue_t dataQueue;
38 static uint8_t addrFilterTable[2] = {0x44, 0xFF};
40 static rfc_propRxOutput_t rxStatistics;
42 static uint8_t packetLength;
43 static uint8_t* packetDataPointer;
44 static int32_t packetStartTime = 0;
46 static volatile bool rfDone =
false;
47 static volatile int rfErr = 0;
48 static volatile int err;
50 static void RxCallback(RF_Handle h, RF_CmdHandle ch, RF_EventMask e)
63 if (e & RF_EventNDataWritten)
66 if (packetStartTime == 0)
68 packetStartTime =
millis();
72 uint8_t* pData = &pDataEntry->rxData;
75 if (((pData[1] != addrFilterTable[0]) || (pData[2] != addrFilterTable[1])) ||
76 (
crc16Dnp(pData, 10) != ((pData[10] << 8) | pData[11])))
79 RF_cancelCmd(rfHandle, rxCommandHandle, 0 );
84 uint8_t len = pDataEntry->rxData;
85 struct rfc_CMD_PROP_SET_LEN_s RF_cmdPropSetLen =
87 .commandNo = CMD_PROP_SET_LEN,
88 .rxLen = (uint16_t)PACKET_SIZE(len)
92 RF_Stat status = RF_runDirectCmd(rfHandle, (uint32_t)&RF_cmdPropSetLen);
94 if (status != RF_StatCmdDoneSuccess)
96 println(
"RF CMD_PROP_SET_LEN failed!");
100 else if (e & RF_TERMINATION_EVENT_MASK)
102 if (e & RF_EventCmdAborted)
108 rfErr = e & (RF_EventCmdStopped | RF_EventCmdAborted | RF_EventCmdCancelled);
112 pDataEntry->status = DATA_ENTRY_PENDING;
124 RF_TxPowerTable_Entry* rfPowerTable = NULL;
125 RF_TxPowerTable_Value newValue;
126 uint8_t rfPowerTableSize = 0;
129 newValue = RF_TxPowerTable_findValue((RF_TxPowerTable_Entry*)PROP_RF_txPowerTable, dBm);
131 if (newValue.rawValue != RF_TxPowerTable_INVALID_VALUE)
134 rfPowerTable = (RF_TxPowerTable_Entry*)PROP_RF_txPowerTable;
135 rfPowerTableSize = PROP_RF_txPowerTableSize;
140 #if (CCFG_FORCE_VDDR_HH != 0x1)
142 if ((newValue.paType == RF_TxPowerTable_DefaultPA) &&
143 (dBm == rfPowerTable[rfPowerTableSize - 2].power))
153 RF_Stat rfStatus = RF_setTxPower(rfHandle, newValue);
155 if (rfStatus == RF_StatSuccess)
157 print(
"Successfully set TX output power to: ");
162 print(
"Could not set TX output power to: ");
171 RF_Params_init(&rfParams);
173 pDataEntry->length = 255;
174 pDataEntry->config.type = DATA_ENTRY_TYPE_PARTIAL;
175 pDataEntry-> config.irqIntv = 12;
176 pDataEntry-> config.lenSz = 0;
177 pDataEntry->status = DATA_ENTRY_PENDING;
178 pDataEntry->pNextEntry = (uint8_t*)pDataEntry;
180 dataQueue.pCurrEntry = (uint8_t*)pDataEntry;
181 dataQueue.pLastEntry = NULL;
185 RF_cmdPropRxAdv.pAddr = (uint8_t*)&addrFilterTable;
187 RF_cmdPropRxAdv.pQueue = &dataQueue;
189 RF_cmdPropRxAdv.pOutput = (uint8_t*)&rxStatistics;
192 rfHandle = RF_open(&rfObject, &RF_prop, (RF_RadioSetup*)&RF_cmdPropRadioDivSetup, &rfParams);
195 RF_runCmd(rfHandle, (RF_Op*)&RF_cmdFs, RF_PriorityNormal, NULL, 0);
201 RF_cancelCmd(rfHandle, rxCommandHandle, 0 );
202 RF_pendCmd(rfHandle, rxCommandHandle, RF_TERMINATION_EVENT_MASK);
213 uint8_t* sendBuffer {
nullptr};
214 uint16_t sendBufferLength {0};
218 uint16_t pktLen = PACKET_SIZE(sendBuffer[0]);
220 if (pktLen != sendBufferLength)
222 print(
"Error TX: SendBuffer[0]=");
224 print(
"Error TX: SendBufferLength=");
226 print(
"Error TX: PACKET_SIZE=");
227 println(PACKET_SIZE(sendBuffer[0]));
232 if ((pktLen == 0) || (pktLen > 290))
234 println(
"TX packet length error!");
240 println(
"Unhandled: TX packet > 255");
244 RF_cmdPropTx.pktLen = pktLen;
245 RF_cmdPropTx.pPkt = sendBuffer;
246 RF_cmdPropTx.startTrigger.triggerType = TRIG_NOW;
247 RF_EventMask result = RF_runCmd(rfHandle, (RF_Op*)&RF_cmdPropTx, RF_PriorityNormal, NULL, RF_TERMINATION_EVENT_MASK);
250 #if defined(DEBUG_DUMP_PACKETS)
251 printHex(
"TX: ", sendBuffer, pktLen);
255 if (result != RF_EventLastCmdDone)
257 print(
"Unexpected result command: ");
262 _loopState = RX_START;
272 pDataEntry->status = DATA_ENTRY_PENDING;
274 rxCommandHandle = RF_postCmd(rfHandle, (RF_Op*)&RF_cmdPropRxAdv, RF_PriorityNormal, &RxCallback, RF_EventNDataWritten | RF_EventRxAborted);
276 if (rxCommandHandle == RF_ALLOC_ERROR)
278 println(
"Error: nRF_pendCmd() failed");
282 _loopState = RX_ACTIVE;
290 RF_cancelCmd(rfHandle, rxCommandHandle, RF_ABORT_GRACEFULLY);
291 RF_pendCmd(rfHandle, rxCommandHandle, RF_TERMINATION_EVENT_MASK);
293 if (RF_cmdPropTx.status != PROP_DONE_OK)
295 print(
"Unexpected RF_cmdPropTx.status after stopping RX: ");
296 println(RF_cmdPropTx.status, HEX);
299 _loopState = TX_START;
304 if (!rfDone && ((packetStartTime > 0) && (
millis() - packetStartTime > RX_PACKET_TIMEOUT)))
307 RF_cancelCmd(rfHandle, rxCommandHandle, RF_ABORT_GRACEFULLY);
308 RF_pendCmd(rfHandle, rxCommandHandle, RF_TERMINATION_EVENT_MASK);
316 _loopState = RX_START;
321 RF_EventMask result = RF_pendCmd(rfHandle, rxCommandHandle, RF_TERMINATION_EVENT_MASK);
323 if ((result & RF_EventCmdCancelled) || (result & RF_EventCmdStopped) || (result & RF_EventCmdAborted))
325 println(
"RF terminated because of RF_flushCmd() or RF_cancelCmd()");
327 else if ((result & RF_EventLastCmdDone) != RF_EventLastCmdDone)
329 print(
"Unexpected Rx result command: ");
336 else if ((result & RF_EventLastCmdDone) == RF_EventLastCmdDone)
339 packetLength = PACKET_SIZE(pDataEntry->rxData);
340 packetDataPointer = (uint8_t*) &pDataEntry->rxData;
343 if (packetLength != (pDataEntry->nextIndex - 1))
345 println(
"Mismatch between packetLength and pDataEntry->nextIndex: ");
346 print(
"packetLength = ");
348 print(
", pDataEntry->nextIndex = ");
349 println(pDataEntry->nextIndex);
360 #if defined(DEBUG_DUMP_PACKETS)
361 printHex(
"RX: ", packetDataPointer, packetLength,
false);
363 println(rxStatistics.lastRssi);
368 _loopState = RX_START;
uint16_t crc16Dnp(uint8_t *input, uint16_t length)
void printHex(const char *suffix, const uint8_t *data, size_t length, bool newline)
RfPhysicalLayerCC1310(RfDataLinkLayer &rfDataLinkLayer, Platform &platform)
void setOutputPowerLevel(int8_t dBm)
RfDataLinkLayer & _rfDataLinkLayer