1 #ifndef DeviceFamily_CC13X0
16 const uint8_t RfPhysicalLayerCC1101::manchEncodeTab[16] = {0xAA,
36 const uint8_t RfPhysicalLayerCC1101::manchDecodeTab[16] = {0xFF,
81 const uint8_t RfPhysicalLayerCC1101::cc1101_2FSK_32_7_kb[CFG_REGISTER] =
133 const uint8_t RfPhysicalLayerCC1101::paTablePower868[8] = {0x03, 0x17, 0x1D, 0x26, 0x50, 0x86, 0xCD, 0xC0};
140 void RfPhysicalLayerCC1101::manchEncode(uint8_t* uncodedData, uint8_t* encodedData)
142 uint8_t data0, data1;
145 data1 = (((*uncodedData) >> 4) & 0x0F);
146 data0 = ((*uncodedData) & 0x0F);
149 *encodedData = (manchEncodeTab[data1]);
150 *(encodedData + 1) = manchEncodeTab[data0];
153 bool RfPhysicalLayerCC1101::manchDecode(uint8_t* encodedData, uint8_t* decodedData)
155 uint8_t data0, data1, data2, data3;
158 data3 = ((*encodedData >> 4) & 0x0F);
159 data2 = ( *encodedData & 0x0F);
160 data1 = ((*(encodedData + 1) >> 4) & 0x0F);
161 data0 = ((*(encodedData + 1)) & 0x0F);
164 if ( (manchDecodeTab[data3] == 0xFF ) | (manchDecodeTab[data2] == 0xFF ) |
165 (manchDecodeTab[data1] == 0xFF ) | (manchDecodeTab[data0] == 0xFF ) )
171 *decodedData = (manchDecodeTab[data3] << 6) | (manchDecodeTab[data2] << 4) |
172 (manchDecodeTab[data1] << 2) | manchDecodeTab[data0];
177 uint8_t RfPhysicalLayerCC1101::sIdle()
182 spiWriteStrobe(SIDLE);
187 while ((marcState != MARCSTATE_IDLE) && ((
millis() - timeStart) < CC1101_TIMEOUT))
189 marcState = (spiReadRegister(MARCSTATE) & MARCSTATE_BITMASK);
195 if (marcState != MARCSTATE_IDLE)
197 println(
"Timeout when trying to set idle state.");
204 uint8_t RfPhysicalLayerCC1101::sReceive()
214 while ((marcState != MARCSTATE_RX) && ((
millis() - timeStart) < CC1101_TIMEOUT))
216 marcState = (spiReadRegister(MARCSTATE) & MARCSTATE_BITMASK);
222 if (marcState != MARCSTATE_RX)
224 println(
"Timeout when trying to set receive state.");
231 void RfPhysicalLayerCC1101::spiWriteRegister(uint8_t spi_instr, uint8_t value)
233 uint8_t tbuf[2] = {0};
234 tbuf[0] = spi_instr | WRITE_SINGLE_BYTE;
242 uint8_t RfPhysicalLayerCC1101::spiReadRegister(uint8_t spi_instr)
245 uint8_t rbuf[2] = {0};
246 rbuf[0] = spi_instr | READ_SINGLE_BYTE;
257 uint8_t RfPhysicalLayerCC1101::spiWriteStrobe(uint8_t spi_instr)
259 uint8_t tbuf[1] = {0};
268 void RfPhysicalLayerCC1101::spiReadBurst(uint8_t spi_instr, uint8_t* pArr, uint8_t len)
270 uint8_t rbuf[len + 1];
271 rbuf[0] = spi_instr | READ_BURST;
276 for (uint8_t i = 0; i < len ; i++ )
278 pArr[i] = rbuf[i + 1];
283 void RfPhysicalLayerCC1101::spiWriteBurst(uint8_t spi_instr,
const uint8_t* pArr, uint8_t len)
285 uint8_t tbuf[len + 1];
286 tbuf[0] = spi_instr | WRITE_BURST;
288 for (uint8_t i = 0; i < len ; i++ )
290 tbuf[i + 1] = pArr[i];
299 void RfPhysicalLayerCC1101::powerDownCC1101()
305 spiWriteStrobe(SPWD);
308 void RfPhysicalLayerCC1101::setOutputPowerLevel(int8_t dBm)
329 spiWriteRegister(FREND0, pa);
351 spiWriteStrobe(SRES);
357 spiWriteStrobe(SFTX);
359 spiWriteStrobe(SFRX);
362 uint8_t partnum = spiReadRegister(PARTNUM);
363 uint8_t version = spiReadRegister(VERSION);
366 if (version == 0x00 || version == 0xFF)
373 print(
"Partnumber: 0x");
375 print(
"Version : 0x");
379 spiWriteBurst(WRITE_BURST, cc1101_2FSK_32_7_kb, CFG_REGISTER);
382 spiWriteBurst(PATABLE_BURST, paTablePower868, 8);
385 spiWriteRegister(FREQ2, 0x21);
386 spiWriteRegister(FREQ1, 0x65);
387 spiWriteRegister(FREQ0, 0x6A);
390 spiWriteRegister(CHANNR, 0);
393 setOutputPowerLevel(0);
407 uint8_t config_reg_verify[CFG_REGISTER];
408 uint8_t Patable_verify[CFG_REGISTER];
410 spiReadBurst(READ_BURST, config_reg_verify, CFG_REGISTER);
411 spiReadBurst(PATABLE_BURST, Patable_verify, 8);
414 printHex(
"", config_reg_verify, CFG_REGISTER);
432 spiWriteRegister(SYNC1, 0x54);
433 spiWriteRegister(SYNC0, 0x76);
435 spiWriteRegister(FIFOTHR, 0x47);
437 spiWriteRegister(IOCFG2, 0x02);
439 spiWriteRegister(IOCFG0, 0x06);
441 spiWriteStrobe(SFTX);
446 pktLen = PACKET_SIZE(sendBuffer[0]);
449 if ((pktLen == 0) || (pktLen > 290))
451 println(
"TX packet length error!");
457 bytesLeft = (2 * pktLen) + 2;
462 for (
int i = 0; i < pktLen; i++)
464 manchEncode(&sendBuffer[i], &buffer[1 + i * 2]);
468 buffer[1 + bytesLeft - 1] = 0x55;
471 pByteIndex = &buffer[0];
476 spiWriteRegister(PKTLEN, bytesLeft);
477 spiWriteRegister(PKTCTRL0, 0x00);
478 fixedLengthMode =
true;
482 uint8_t fixedLength = bytesLeft % 256;
483 spiWriteRegister(PKTLEN, fixedLength);
484 spiWriteRegister(PKTCTRL0, 0x02);
485 fixedLengthMode =
false;
488 uint8_t bytesToWrite = MIN(64, bytesLeft);
489 spiWriteBurst(TXFIFO_BURST, pByteIndex, bytesToWrite);
490 pByteIndex += bytesToWrite;
491 bytesLeft -= bytesToWrite;
496 _loopState = TX_ACTIVE;
504 if (syncStart && (
millis() - packetStartTime > TX_PACKET_TIMEOUT))
516 if (prevStatusGDO2 != statusGDO2)
518 prevStatusGDO2 = statusGDO2;
525 uint8_t bytesToWrite = MIN(64, bytesLeft);
526 spiWriteBurst(TXFIFO_BURST, pByteIndex, bytesToWrite);
527 pByteIndex += bytesToWrite;
528 bytesLeft -= bytesToWrite;
531 if ( (bytesLeft < (256 - 64)) && !fixedLengthMode )
533 spiWriteRegister(PKTCTRL0, 0x00);
534 fixedLengthMode =
true;
542 if (prevStatusGDO0 != statusGDO0)
544 prevStatusGDO0 = statusGDO0;
547 if (statusGDO0 == 0x00)
550 uint8_t chipStatusBytes = spiWriteStrobe(SNOP);
552 if ((chipStatusBytes & CHIPSTATUS_STATE_BITMASK) == CHIPSTATUS_STATE_TX_UNDERFLOW)
567 packetStartTime =
millis();
579 _loopState = RX_START;
589 fixedLengthMode =
false;
595 spiWriteRegister(SYNC1, 0x76);
596 spiWriteRegister(SYNC0, 0x96);
598 spiWriteRegister(IOCFG2, 0x00);
600 spiWriteRegister(IOCFG0, 0x06);
602 spiWriteRegister(FIFOTHR, 0x40);
604 spiWriteRegister(PKTCTRL0, 0x02);
606 spiWriteStrobe(SFRX);
609 _loopState = RX_ACTIVE;
618 _loopState = TX_START;
624 if (syncStart && (
millis() - packetStartTime > RX_PACKET_TIMEOUT))
631 _loopState = RX_START;
638 if (prevStatusGDO2 != statusGDO2)
640 prevStatusGDO2 = statusGDO2;
652 spiReadBurst(RXFIFO_BURST, pByteIndex, 2);
655 if (!manchDecode(&buffer[0], &packet[0]))
658 _loopState = RX_START;
663 pktLen = 2 * PACKET_SIZE(packet[0]);
669 spiWriteRegister(PKTLEN, pktLen);
670 spiWriteRegister(PKTCTRL0, 0x00);
671 fixedLengthMode =
true;
677 uint8_t fixedLength = pktLen % 256;
678 spiWriteRegister(PKTLEN, fixedLength);
682 bytesLeft = pktLen - 2;
686 spiWriteRegister(FIFOTHR, 0x47);
696 if ((bytesLeft < 256 ) && !fixedLengthMode)
698 spiWriteRegister(PKTCTRL0, 0x00);
699 fixedLengthMode =
true;
704 spiReadBurst(RXFIFO_BURST, pByteIndex, 32 - 1);
706 bytesLeft -= (32 - 1);
707 pByteIndex += (32 - 1);
715 if (prevStatusGDO0 != statusGDO0)
717 prevStatusGDO0 = statusGDO0;
720 if (statusGDO0 == 0x00)
723 uint8_t chipStatusBytes = spiWriteStrobe(SNOP);
725 if ((chipStatusBytes & CHIPSTATUS_STATE_BITMASK) == CHIPSTATUS_STATE_RX_OVERFLOW)
728 _loopState = RX_START;
737 spiReadBurst(RXFIFO_BURST, pByteIndex, bytesLeft);
747 packetStartTime =
millis();
756 uint16_t pLen = PACKET_SIZE(packet[0]);
758 bool decodeOk =
true;
760 for (uint16_t i = 1; i < pLen; i++)
763 if (!manchDecode(&buffer[i * 2], &packet[i]))
765 println(
"Could not decode packet: manchester code violation");
776 _loopState = RX_START;
void printHex(const char *suffix, const uint8_t *data, size_t length, bool newline)
RfPhysicalLayerCC1101(RfDataLinkLayer &rfDataLinkLayer, Platform &platform)
void showRegisterSettings()
RfDataLinkLayer & _rfDataLinkLayer