pycc1101.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. import spidev
  2. import time
  3. class TICC1101(object):
  4. WRITE_SINGLE_BYTE = 0x00
  5. WRITE_BURST = 0x40
  6. READ_SINGLE_BYTE = 0x80
  7. READ_BURST = 0xC0
  8. # Configuration Register Details - Registers with preserved values in SLEEP state
  9. # TI-CC1101 Datasheet
  10. IOCFG2 = 0x00 # GDO2 Output Pin Configuration
  11. IOCFG1 = 0x01 # GDO1 Output Pin Configuration
  12. IOCFG0 = 0x02 # GDO0 Output Pin Configuration
  13. FIFOTHR = 0x03 # RX FIFO and TX FIFO Thresholds
  14. SYNC1 = 0x04 # Sync Word, High Byte
  15. SYNC0 = 0x05 # Sync Word, Low Byte
  16. PKTLEN = 0x06 # Packet Length
  17. PKTCTRL1 = 0x07 # Packet Automation Control
  18. PKTCTRL0 = 0x08 # Packet Automation Control
  19. ADDR = 0x09 # Device Address
  20. CHANNR = 0x0A # Channel Number
  21. FSCTRL1 = 0x0B # Frequency Synthesizer Control
  22. FSCTRL0 = 0x0C # Frequency Synthesizer Control
  23. FREQ2 = 0x0D # Frequency Control Word, High Byte
  24. FREQ1 = 0x0E # Frequency Control Word, Middle Byte
  25. FREQ0 = 0x0F # Frequency Control Word, Low Byte
  26. MDMCFG4 = 0x10 # Modem Configuration
  27. MDMCFG3 = 0x11 # Modem Configuration
  28. MDMCFG2 = 0x12 # Modem Configuration
  29. MDMCFG1 = 0x13 # Modem Configuration
  30. MDMCFG0 = 0x14 # Modem Configuration
  31. DEVIATN = 0x15 # Modem Deviation Setting
  32. MCSM2 = 0x16 # Main Radio Control State Machine Configuration
  33. MCSM1 = 0x17 # Main Radio Control State Machine Configuration
  34. MCSM0 = 0x18 # Main Radio Control State Machine Configuration
  35. FOCCFG = 0x19 # Frequency Offset Compensation Configuration
  36. BSCFG = 0x1A # Bit Synchronization Configuration
  37. AGCCTRL2 = 0x1B # AGC Control
  38. AGCCTRL1 = 0x1C # AGC Control
  39. AGCCTRL0 = 0x1D # AGC Control
  40. WOREVT1 = 0x1E # High Byte Event0 Timeout
  41. WOREVT0 = 0x1F # Low Byte Event0 Timeout
  42. WORCTRL = 0x20 # Wake On Radio Control
  43. FREND1 = 0x21 # Front End RX Configuration
  44. FREND0 = 0x22 # Front End TX Configuration
  45. FSCAL3 = 0x23 # Frequency Synthesizer Calibration
  46. FSCAL2 = 0x24 # Frequency Synthesizer Calibration
  47. FSCAL1 = 0x25 # Frequency Synthesizer Calibration
  48. FSCAL0 = 0x26 # Frequency Synthesizer Calibration
  49. RCCTRL1 = 0x27 # RC Oscillator Configuration
  50. RCCTRL0 = 0x28 # RC Oscillator Configuration
  51. # Configuration Register Details - Registers that Loose Programming in SLEEP State
  52. FSTEST = 0x29 # Frequency Synthesizer Calibration Control
  53. PTEST = 0x2A # Production Test
  54. AGCTEST = 0x2B # AGC Test
  55. TEST2 = 0x2C # Various Test Settings
  56. TEST1 = 0x2D # Various Test Settings
  57. TEST0 = 0x2E # Various Test Settings
  58. # Command Strobe Registers
  59. SRES = 0x30 # Reset chip
  60. SFSTXON = 0x31 # Enable and calibrate frequency synthesizer (if MCSM0.FS_AUTOCAL=1).
  61. # If in RX (with CCA): Go to a wait state where only the synthesizer
  62. # is running (for quick RX / TX turnaround).
  63. SXOFF = 0x32 # Turn off crystal oscillator.
  64. SCAL = 0x33 # Calibrate frequency synthesizer and turn it off.
  65. # SCAL can be strobed from IDLE mode without setting manual calibration mode.
  66. SRX = 0x34 # Enable RX. Perform calibration first if coming from IDLE and MCSM0.FS_AUTOCAL=1.
  67. STX = 0x35 # In IDLE state: Enable TX. Perform calibration first
  68. # if MCSM0.FS_AUTOCAL=1.
  69. # If in RX state and CCA is enabled: Only go to TX if channel is clear.
  70. SIDLE = 0x36 # Exit RX / TX, turn off frequency synthesizer and exit Wake-On-Radio mode if applicable.
  71. SWOR = 0x38 # Start automatic RX polling sequence (Wake-on-Radio)
  72. # as described in Section 19.5 if WORCTRL.RC_PD=0.
  73. SPWD = 0x39 # Enter power down mode when CSn goes high.
  74. SFRX = 0x3A # Flush the RX FIFO buffer. Only issue SFRX in IDLE or RXFIFO_OVERFLOW states.
  75. SFTX = 0x3B # Flush the TX FIFO buffer. Only issue SFTX in IDLE or TXFIFO_UNDERFLOW states.
  76. SWORRST = 0x3C # Reset real time clock to Event1 value.
  77. SNOP = 0x3D # No operation. May be used to get access to the chip status byte.
  78. PATABLE = 0x3E # PATABLE
  79. TXFIFO = 0x3F # TXFIFO
  80. RXFIFO = 0x3F # RXFIFO
  81. # Status Register Details
  82. PARTNUM = 0xF0 # Chip ID
  83. VERSION = 0xF1 # Chip ID
  84. FREQEST = 0xF2 # Frequency Offset Estimate from Demodulator
  85. LQI = 0xF3 # Demodulator Estimate for Link Quality
  86. RSSI = 0xF4 # Received Signal Strength Indication
  87. MARCSTATE = 0xF5 # Main Radio Control State Machine State
  88. WORTIME1 = 0xF6 # High Byte of WOR Time
  89. WORTIME0 = 0xF7 # Low Byte of WOR Time
  90. PKTSTATUS = 0xF8 # Current GDOx Status and Packet Status
  91. VCO_VC_DAC = 0xF9 # Current Setting from PLL Calibration Module
  92. TXBYTES = 0xFA # Underflow and Number of Bytes
  93. RXBYTES = 0xFB # Overflow and Number of Bytes
  94. RCCTRL1_STATUS = 0xFC # Last RC Oscillator Calibration Result
  95. RCCTRL0_STATUS = 0xFD # Last RC Oscillator Calibration Result
  96. def __init__(self, bus=0, device=0, speed=50000, debug=True):
  97. try:
  98. self.debug = debug
  99. self._spi = spidev.SpiDev()
  100. self._spi.open(bus, device)
  101. self._spi.max_speed_hz = speed
  102. except Exception as e:
  103. print(e)
  104. def _usDelay(self, useconds):
  105. time.sleep(useconds / 1000000.0)
  106. def _writeSingleByte(self, address, byte_data):
  107. return self._spi.xfer([self.WRITE_SINGLE_BYTE | address, byte_data])
  108. def _readSingleByte(self, address):
  109. return self._spi.xfer([self.READ_SINGLE_BYTE | address, 0x00])[1]
  110. def _readBurst(self, start_address, length):
  111. buff = []
  112. ret = []
  113. for x in range(length + 1):
  114. addr = (start_address + (x * 8)) | self.READ_BURST
  115. buff.append(addr)
  116. ret = self._spi.xfer(buff)[1:]
  117. if self.debug:
  118. print("_readBurst | start_address = {:x}, length = {:x}".format(start_address, length))
  119. return ret
  120. def _writeBurst(self, address, data):
  121. data.insert(0, (self.WRITE_BURST | address))
  122. return self._spi.xfer(data)
  123. def reset(self):
  124. return self._strobe(self.SRES)
  125. def _strobe(self, address):
  126. return self._spi.xfer([address, 0x00])
  127. def selfTest(self):
  128. part_number = self._readSingleByte(self.PARTNUM)
  129. component_version = self._readSingleByte(self.VERSION)
  130. # These asserts are based on the documentation
  131. # Section 29.3 "Status Register Details"
  132. # On reset PARTNUM == 0x00
  133. # On reset VERSION == 0x14
  134. assert part_number == 0x00
  135. assert component_version == 0x14
  136. if self.debug:
  137. print("Part Number: {:x}".format(part_number))
  138. print("Component Version: {:x}".format(component_version))
  139. print("Self test OK")
  140. def sidle(self):
  141. self._strobe(self.SIDLE)
  142. while (self._readSingleByte(self.MARCSTATE) != 0x01):
  143. self._usDelay(100)
  144. self._strobe(self.SFTX)
  145. self._usDelay(100)
  146. def powerDown(self):
  147. self.sidle()
  148. self._strobe(self.SPWD)
  149. def setCarrierFrequency(self, freq=433):
  150. # Register values extracted from SmartRF Studio 7
  151. if freq == 433:
  152. self._writeSingleByte(self.FREQ2, 0x10)
  153. self._writeSingleByte(self.FREQ1, 0xA7)
  154. self._writeSingleByte(self.FREQ0, 0x62)
  155. elif freq == 868:
  156. self._writeSingleByte(self.FREQ2, 0x21)
  157. self._writeSingleByte(self.FREQ1, 0x62)
  158. self._writeSingleByte(self.FREQ0, 0x76)
  159. else:
  160. raise Exception("Only 433MHz and 868MHz are currently supported")
  161. def setChannel(self, channel=0x00):
  162. self._writeSingleByte(self.CHANNR, channel)
  163. def setSyncWord(self, sync_word="FAFA"):
  164. assert len(sync_word) == 4
  165. self._writeSingleByte(self.SYNC1, int(sync_word[:2], 16))
  166. self._writeSingleByte(self.SYNC0, int(sync_word[2:], 16))
  167. def getRegisterConfiguration(self, register, showConfig=True):
  168. def toBits(byte):
  169. return bin(byte)[2:].zfill(8)
  170. if register == "PKTCTRL1":
  171. bits = toBits(self._readSingleByte(self.PKTCTRL1))
  172. if showConfig:
  173. print("PKTCTRL1")
  174. print("PQT[7:5] = {}".format(bits[:3]))
  175. print("CRC_AUTOFLUSH = {}".format(bits[4]))
  176. print("APPEND_STATUS = {}".format(bits[5]))
  177. print("ADR_CHK[1:0] = {}".format(bits[6:]))
  178. elif register == "PKTCTRL0":
  179. bits = toBits(self._readSingleByte(self.PKTCTRL0))
  180. if showConfig:
  181. print("PKTCTRL0")
  182. print("WHITE_DATA = {}".format(bits[1]))
  183. print("PKT_FORMAT[1:0] = {}".format(bits[2:4]))
  184. print("CRC_EN = {}".format(bits[5]))
  185. print("LENGTH_CONFIG[1:0] = {}".format(bits[6:]))
  186. elif register == "ADDR":
  187. bits = toBits(self._readSingleByte(self.ADDR))
  188. if showConfig:
  189. print("ADDR")
  190. print("DEVICE_ADDR = {}".format(bits))
  191. elif register == "CHANNR":
  192. bits = toBits(self._readSingleByte(self.CHANNR))
  193. if showConfig:
  194. print("CAHNNR")
  195. print("CHAN = {}".format(bits))
  196. elif register == "PKTSTATUS":
  197. bits = toBits(self._readSingleByte(self.CHANNR))
  198. if showConfig:
  199. print("PKTSTATUS")
  200. print("CRC_OK = {}".format(bits[0]))
  201. print("CS = {}".format(bits[1]))
  202. print("PQT_REACHED = {}".format(bits[2]))
  203. print("CCA = {}".format(bits[3]))
  204. print("SFD = {}".format(bits[4]))
  205. print("GDO2 = {}".format(bits[5]))
  206. print("GDO0 = {}".format(bits[7]))
  207. elif register == "MDMCFG2":
  208. bits = toBits(self._readSingleByte(self.MDMCFG2))
  209. if showConfig:
  210. print("MDMCFG2")
  211. print("DEM_DCFILT_OFF = {}".format(bits[0]))
  212. print("MOD_FORMAT = {}".format(bits[1:4]))
  213. print("MANCHESTER_EN = {}".format(bits[4]))
  214. print("SYNC_MODE = {}".format(bits[5:]))
  215. elif register == "MDMCFG1":
  216. bits = toBits(self._readSingleByte(self.MDMCFG1))
  217. if showConfig:
  218. print("MDMCFG1")
  219. print("FEC_EN = {}".format(bits[0]))
  220. print("NUM_PREAMBLE = {}".format(bits[1:4]))
  221. print("CHANSPC_E = {}".format(bits[6:]))
  222. return bits
  223. def setDefaultValues(self, version=1):
  224. # Default values extracted from Smart RF Studio 7
  225. self._writeSingleByte(self.IOCFG2, 0x2E) # Panstamp
  226. self._writeSingleByte(self.IOCFG1, 0x2E) # Panstamp
  227. self._writeSingleByte(self.IOCFG0, 0x06) # Panstamp
  228. self._writeSingleByte(self.FIFOTHR, 0x07) # Panstamp
  229. self._writeSingleByte(self.PKTLEN, 20)
  230. self._writeSingleByte(self.PKTCTRL1, 0x06) # Panstamp
  231. self._writeSingleByte(self.PKTCTRL0, 0x04) # Panstamp
  232. self.setSyncWord()
  233. self.setChannel()
  234. self.configureAddressFiltering()
  235. self._writeSingleByte(self.FSCTRL1, 0x08) # Panstamp
  236. self._writeSingleByte(self.FSCTRL0, 0x00) # Panstamp
  237. self.setCarrierFrequency()
  238. self._writeSingleByte(self.MDMCFG4, 0xCA) # Panstamp
  239. self._writeSingleByte(self.MDMCFG3, 0x83) # Panstamp
  240. self._writeSingleByte(self.MDMCFG2, 0x93) # Panstamp
  241. self._writeSingleByte(self.MDMCFG1, 0x22)
  242. self._writeSingleByte(self.MDMCFG0, 0xF8)
  243. self._writeSingleByte(self.DEVIATN, 0x35) # Panstamp
  244. self._writeSingleByte(self.MCSM2, 0x07)
  245. self._writeSingleByte(self.MCSM1, 0x20) # Panstamp
  246. self._writeSingleByte(self.MCSM0, 0x18)
  247. self._writeSingleByte(self.FOCCFG, 0x16)
  248. self._writeSingleByte(self.BSCFG, 0x6C)
  249. self._writeSingleByte(self.AGCCTRL2, 0x43) # Panstamp
  250. self._writeSingleByte(self.AGCCTRL1, 0x40)
  251. self._writeSingleByte(self.AGCCTRL0, 0x91)
  252. self._writeSingleByte(self.WOREVT1, 0x87)
  253. self._writeSingleByte(self.WOREVT0, 0x6B)
  254. self._writeSingleByte(self.WORCTRL, 0xFB)
  255. self._writeSingleByte(self.FREND1, 0x56)
  256. self._writeSingleByte(self.FREND0, 0x10)
  257. self._writeSingleByte(self.FSCAL3, 0xE9)
  258. self._writeSingleByte(self.FSCAL2, 0x2A)
  259. self._writeSingleByte(self.FSCAL1, 0x00)
  260. self._writeSingleByte(self.FSCAL0, 0x1F)
  261. self._writeSingleByte(self.RCCTRL1, 0x41)
  262. self._writeSingleByte(self.RCCTRL0, 0x00)
  263. self._writeSingleByte(self.FSTEST, 0x59)
  264. self._writeSingleByte(self.PTEST, 0x7F)
  265. self._writeSingleByte(self.AGCTEST, 0x3F)
  266. self._writeSingleByte(self.TEST2, 0x81)
  267. self._writeSingleByte(self.TEST1, 0x35)
  268. self._writeSingleByte(self.TEST0, 0x09)
  269. self._writeSingleByte(0x3E, 0xC0) # Power 10dBm
  270. def setSyncMode(self, syncmode):
  271. regVal = list(self.getRegisterConfiguration("MDMCFG2"))
  272. if syncmode > 7:
  273. raise Exception("Invalid SYNC mode")
  274. regVal[5:] = bin(syncmode)[2:].zfill(3)
  275. regVal = int("".join(regVal), 2)
  276. self._writeSingleByte(self.MDMCFG2, regVal)
  277. def setModulation(self, modulation):
  278. regVal = list(self.getRegisterConfiguration("MDMCFG2"))
  279. if modulation == "2-FSK":
  280. modVal = "000"
  281. elif modulation == "GFSK":
  282. modVal = "001"
  283. elif modulation == "ASK" or modulation == "OOK":
  284. modVal = "011"
  285. elif modulation == "4-FSK":
  286. modVal = "100"
  287. elif modulation == "MSK":
  288. modVal = "111"
  289. else:
  290. raise Exception("Modulation type NOT SUPPORTED!")
  291. regVal[1:4] = modVal
  292. regVal = int("".join(regVal), 2)
  293. self._writeSingleByte(self.MDMCFG2, regVal)
  294. def _flushRXFifo(self):
  295. self._strobe(self.SFRX)
  296. self._usDelay(2)
  297. def _flushTXFifo(self):
  298. self._strobe(self.SFTX)
  299. self._usDelay(2)
  300. def _setTXState(self):
  301. self._strobe(self.STX)
  302. self._usDelay(2)
  303. def _setRXState(self):
  304. self._strobe(self.SRX)
  305. self._usDelay(2)
  306. def getRSSI(self):
  307. return self._readSingleByte(self.RSSI)
  308. def _getMRStateMachineState(self):
  309. # The &0x1F works as a mask due to the fact
  310. # that the MARCSTATE register only uses the
  311. # first 5 bits
  312. return (self._readSingleByte(self.MARCSTATE) & 0x1F)
  313. def getPacketConfigurationMode(self):
  314. pktCtrlVal = self.getRegisterConfiguration("PKTCTRL0", False)
  315. if pktCtrlVal[6:] == "00": # Packet len is fixed
  316. return "PKT_LEN_FIXED"
  317. elif pktCtrlVal[6:] == "01": # Packet len is variable
  318. return "PKT_LEN_VARIABLE"
  319. elif pktCtrlVal[6:] == "10": # Infinite packet len mode
  320. return "PKT_LEN_INFINITE"
  321. def setPacketMode(self, mode="PKT_LEN_VARIABLE"):
  322. regVal = list(self.getRegisterConfiguration("PKTCTRL0", False))
  323. if mode == "PKT_LEN_FIXED":
  324. val = "00"
  325. elif mode == "PKT_LEN_VARIABLE":
  326. val = "01"
  327. elif mode == "PKT_LEN_INFINITE":
  328. val = "10"
  329. else:
  330. raise Exception("Packet mode NOT SUPPORTED!")
  331. regVal[6:] = val
  332. regVal = int("".join(regVal), 2)
  333. self._writeSingleByte(self.PKTCTRL0, regVal)
  334. def setFilteringAddress(self, address=0x0E):
  335. self._writeSingleByte(self.ADDR, address)
  336. def configureAddressFiltering(self, value="DISABLED"):
  337. regVal = list(self.getRegisterConfiguration("PKTCTRL1", False))
  338. if value == "DISABLED":
  339. val = "00"
  340. elif value == "ENABLED_NO_BROADCAST":
  341. val = "01"
  342. elif value == "ENABLED_00_BROADCAST":
  343. val = "10"
  344. elif value == "ENABLED_00_255_BROADCAST":
  345. val = "11"
  346. else:
  347. raise Exception("Address filtering configuration NOT SUPPORTED!")
  348. regVal[6:] = val
  349. regVal = int("".join(regVal), 2)
  350. self._writeSingleByte(self.PKTCTRL1, regVal)
  351. def sendData(self, dataBytes):
  352. self._setRXState()
  353. marcstate = self._getMRStateMachineState()
  354. dataToSend = []
  355. while ((marcstate & 0x1F) != 0x0D):
  356. if self.debug:
  357. print("marcstate = %x".format(marcstate))
  358. print("waiting for marcstate == 0x0D")
  359. if marcstate == 0x11:
  360. self._flushRXFifo()
  361. marcstate = self._getMRStateMachineState()
  362. if len(dataBytes) == 0:
  363. if self.debug:
  364. print("sendData | No data to send")
  365. return False
  366. sending_mode = self.getPacketConfigurationMode()
  367. data_len = len(dataBytes)
  368. if sending_mode == "PKT_LEN_FIXED":
  369. if data_len > self._readSingleByte(self.PKTLEN):
  370. if self.debug:
  371. print("Len of data exceeds the configured packet len")
  372. return False
  373. if self.getRegisterConfiguration("PKTCTRL1", False)[6:] != "00":
  374. dataToSend.append(self._readSingleByte(self.ADDR))
  375. dataToSend.extend(dataBytes)
  376. dataToSend.extend([0] * (self._readSingleByte(self.PKTLEN) - len(dataToSend)))
  377. if self.debug:
  378. print("Sending a fixed len packet")
  379. print("data len = %d".format((data_len)))
  380. elif sending_mode == "PKT_LEN_VARIABLE":
  381. dataToSend.append(data_len)
  382. if self.getRegisterConfiguration("PKTCTRL1", False)[6:] != "00":
  383. dataToSend.append(self._readSingleByte(self.ADDR))
  384. dataToSend[0] += 1
  385. dataToSend.extend(dataBytes)
  386. if self.debug:
  387. print("Sending a variable len packet")
  388. print("Length of the packet is: %d".format(data_len))
  389. elif sending_mode == "PKT_LEN_INFINITE":
  390. # ToDo
  391. raise Exception("MODE NOT IMPLEMENTED")
  392. print("{}".format(dataToSend))
  393. self._writeBurst(self.TXFIFO, dataToSend)
  394. self._usDelay(2000)
  395. self._setTXState()
  396. marcstate = self._getMRStateMachineState()
  397. if marcstate not in [0x13, 0x14, 0x15]: # RX, RX_END, RX_RST
  398. self.sidle()
  399. self._flushTXFifo()
  400. self._setRXState()
  401. if self.debug:
  402. print("senData | FAIL")
  403. print("sendData | MARCSTATE: %x".format(self._readSingleByte(self.MARCSTATE)))
  404. return False
  405. remaining_bytes = self._readSingleByte(self.TXBYTES) & 0x7F
  406. while remaining_bytes != 0:
  407. self._usDelay(1000)
  408. remaining_bytes = self._readSingleByte(self.TXBYTES) & 0x7F
  409. if self.debug:
  410. print("Waiting until all bytes are transmited, remaining bytes: %d".format(remaining_bytes))
  411. if (self._readSingleByte(self.TXBYTES) & 0x7F) == 0:
  412. if self.debug:
  413. print("Packet sent!")
  414. return True
  415. else:
  416. if self.debug:
  417. print("{}".format(self._readSingleByte(self.TXBYTES) & 0x7F))
  418. print("sendData | MARCSTATE: %x".format(self._getMRStateMachineState()))
  419. self.sidle()
  420. self._flushTXFifo()
  421. time.sleep(5)
  422. self._setRXState()
  423. return False
  424. def recvData(self):
  425. rx_bytes_val = self._readSingleByte(self.RXBYTES)
  426. #if rx_bytes_val has something and Underflow bit is not 1
  427. if (rx_bytes_val & 0x7F and not (rx_bytes_val & 0x80)):
  428. sending_mode = self.getPacketConfigurationMode()
  429. if sending_mode == "PKT_LEN_FIXED":
  430. data_len = self._readSingleByte(self.PKTLEN)
  431. elif sending_mode == "PKT_LEN_VARIABLE":
  432. max_len = self._readSingleByte(self.PKTLEN)
  433. data_len = self._readSingleByte(self.RXFIFO)
  434. if data_len > max_len:
  435. if self.debug:
  436. print("Len of data exceeds the configured maximum packet len")
  437. return False
  438. if self.debug:
  439. print("Receiving a variable len packet")
  440. print("max len: %d".format(max_len))
  441. print("Packet length: %d".format(data_len))
  442. elif sending_mode == "PKT_LEN_INFINITE":
  443. # ToDo
  444. raise Exception("MODE NOT IMPLEMENTED")
  445. data = self._readBurst(self.RXFIFO, data_len)
  446. valPktCtrl1 = self.getRegisterConfiguration("PKTCTRL1", False)
  447. if valPktCtrl1[5] == "1": # PKTCTRL1[5] == APPEND_STATUS
  448. # When enabled, two status bytes will be appended to the payload of the
  449. # packet. The status bytes contain RSSI and LQI values, as well as CRC OK.
  450. rssi = self._readSingleByte(self.RXFIFO)
  451. val = self._readSingleByte(self.RXFIFO)
  452. lqi = val & 0x7f
  453. if self.debug and valPktCtrl1[5] == "1":
  454. print("Packet information is enabled")
  455. print("RSSI: %d".format((rssi)))
  456. print("VAL: %d".format((val)))
  457. print("LQI: %d".format((lqi)))
  458. print("Data: ") + str(data)
  459. self._flushRXFifo()
  460. return data