cc1101.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. /****************************************************************************
  2. * drivers/wireless/cc1101.c
  3. *
  4. * Copyright (C) 2011 Uros Platise. All rights reserved.
  5. *
  6. * Authors: Uros Platise <uros.platise@isotel.eu>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * 3. Neither the name NuttX nor the names of its contributors may be
  19. * used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ****************************************************************************/
  36. /* Features:
  37. * - Maximum data length: 61 bytes CC1101_PACKET_MAXDATALEN
  38. * - Packet length includes two additional bytes: CC1101_PACKET_MAXTOTALLEN
  39. * - Requires one GDO to trigger end-of-packets in RX and TX modes.
  40. * - Variable packet length with data payload between 1..61 bytes
  41. * (three bytes are reserved for packet length, and RSSI and LQI
  42. * appended at the end of RXFIFO after each reception)
  43. * - Support for General Digital Outputs with overload protection
  44. * (single XOSC pin is allowed, otherwise error is returned)
  45. * - Loadable RF settings, one for ISM Region 1 (Europe) and one for
  46. * ISM Region 2 (Complete America)
  47. *
  48. * Todo:
  49. * - Extend max packet length up to 255 bytes or rather infinite < 4096 bytes
  50. * - Power up/down modes
  51. * - Sequencing between states or add protection for correct termination of
  52. * various different state (so that CC1101 does not block in case of improper use)
  53. *
  54. * RSSI and LQI value interpretation
  55. *
  56. * The LQI can be read from the LQI status register or it can be appended
  57. * to the received packet in the RX FIFO. LQI is a metric of the current
  58. * quality of the received signal. The LQI gives an estimate of how easily
  59. * a received signal can be demodulated by accumulating the magnitude of
  60. * the error between ideal constellations and the received signal over
  61. * the 64 symbols immediately following the sync word. LQI is best used
  62. * as a relative measurement of the link quality (a high value indicates
  63. * a better link than what a low value does), since the value is dependent
  64. * on the modulation format.
  65. *
  66. * To simplify: If the received modulation is FSK or GFSK, the receiver
  67. * will measure the frequency of each "bit" and compare it with the
  68. * expected frequency based on the channel frequency and the deviation
  69. * and the measured frequency offset. If other modulations are used, the
  70. * error of the modulated parameter (frequency for FSK/GFSK, phase for
  71. * MSK, amplitude for ASK etc) will be measured against the expected
  72. * ideal value
  73. *
  74. * RSSI (Received Signal Strength Indicator) is a signal strength
  75. * indication. It does not care about the "quality" or "correctness" of
  76. * the signal. LQI does not care about the actual signal strength, but
  77. * the signal quality often is linked to signal strength. This is because
  78. * a strong signal is likely to be less affected by noise and thus will
  79. * be seen as "cleaner" or more "correct" by the receiver.
  80. *
  81. * There are four to five "extreme cases" that can be used to illustrate
  82. * how RSSI and LQI work:
  83. * 1. A weak signal in the presence of noise may give low RSSI and low LQI.
  84. * 2. A weak signal in "total" absence of noise may give low RSSI and high LQI.
  85. * 3. Strong noise (usually coming from an interferer) may give high RSSI and low LQI.
  86. * 4. A strong signal without much noise may give high RSSI and high LQI.
  87. * 5. A very strong signal that causes the receiver to saturate may give
  88. * high RSSI and low LQI.
  89. *
  90. * Note that both RSSI and LQI are best used as relative measurements since
  91. * the values are dependent on the modulation format.
  92. */
  93. /****************************************************************************
  94. * Included Files
  95. ****************************************************************************/
  96. #include <nuttx/config.h>
  97. #include <stdlib.h>
  98. #include <string.h>
  99. #include <stdio.h>
  100. #include <assert.h>
  101. #include <errno.h>
  102. #include <debug.h>
  103. #include <nuttx/kmalloc.h>
  104. #include <nuttx/wireless/cc1101.h>
  105. /****************************************************************************
  106. * Pre-processor Definitions
  107. ****************************************************************************/
  108. #define CC1101_SPIFREQ_BURST 6500000 /* Hz, no delay */
  109. #define CC1101_SPIFREQ_SINGLE 9000000 /* Hz, single access only - no delay */
  110. #define CC1101_MCSM0_VALUE 0x1C
  111. /****************************************************************************
  112. * Chipcon CC1101 Internal Registers
  113. ****************************************************************************/
  114. /* Configuration Registers */
  115. #define CC1101_IOCFG2 0x00 /* GDO2 output pin configuration */
  116. #define CC1101_IOCFG1 0x01 /* GDO1 output pin configuration */
  117. #define CC1101_IOCFG0 0x02 /* GDO0 output pin configuration */
  118. #define CC1101_FIFOTHR 0x03 /* RX FIFO and TX FIFO thresholds */
  119. #define CC1101_SYNC1 0x04 /* Sync word, high byte */
  120. #define CC1101_SYNC0 0x05 /* Sync word, low byte */
  121. #define CC1101_PKTLEN 0x06 /* Packet length */
  122. #define CC1101_PKTCTRL1 0x07 /* Packet automation control */
  123. #define CC1101_PKTCTRL0 0x08 /* Packet automation control */
  124. #define CC1101_ADDR 0x09 /* Device address */
  125. #define CC1101_CHANNR 0x0A /* Channel number */
  126. #define CC1101_FSCTRL1 0x0B /* Frequency synthesizer control */
  127. #define CC1101_FSCTRL0 0x0C /* Frequency synthesizer control */
  128. #define CC1101_FREQ2 0x0D /* Frequency control word, high byte */
  129. #define CC1101_FREQ1 0x0E /* Frequency control word, middle byte */
  130. #define CC1101_FREQ0 0x0F /* Frequency control word, low byte */
  131. #define CC1101_MDMCFG4 0x10 /* Modem configuration */
  132. #define CC1101_MDMCFG3 0x11 /* Modem configuration */
  133. #define CC1101_MDMCFG2 0x12 /* Modem configuration */
  134. #define CC1101_MDMCFG1 0x13 /* Modem configuration */
  135. #define CC1101_MDMCFG0 0x14 /* Modem configuration */
  136. #define CC1101_DEVIATN 0x15 /* Modem deviation setting */
  137. #define CC1101_MCSM2 0x16 /* Main Radio Cntrl State Machine config */
  138. #define CC1101_MCSM1 0x17 /* Main Radio Cntrl State Machine config */
  139. #define CC1101_MCSM0 0x18 /* Main Radio Cntrl State Machine config */
  140. #define CC1101_FOCCFG 0x19 /* Frequency Offset Compensation config */
  141. #define CC1101_BSCFG 0x1A /* Bit Synchronization configuration */
  142. #define CC1101_AGCCTRL2 0x1B /* AGC control */
  143. #define CC1101_AGCCTRL1 0x1C /* AGC control */
  144. #define CC1101_AGCCTRL0 0x1D /* AGC control */
  145. #define CC1101_WOREVT1 0x1E /* High byte Event 0 timeout */
  146. #define CC1101_WOREVT0 0x1F /* Low byte Event 0 timeout */
  147. #define CC1101_WORCTRL 0x20 /* Wake On Radio control */
  148. #define CC1101_FREND1 0x21 /* Front end RX configuration */
  149. #define CC1101_FREND0 0x22 /* Front end TX configuration */
  150. #define CC1101_FSCAL3 0x23 /* Frequency synthesizer calibration */
  151. #define CC1101_FSCAL2 0x24 /* Frequency synthesizer calibration */
  152. #define CC1101_FSCAL1 0x25 /* Frequency synthesizer calibration */
  153. #define CC1101_FSCAL0 0x26 /* Frequency synthesizer calibration */
  154. #define CC1101_RCCTRL1 0x27 /* RC oscillator configuration */
  155. #define CC1101_RCCTRL0 0x28 /* RC oscillator configuration */
  156. #define CC1101_FSTEST 0x29 /* Frequency synthesizer cal control */
  157. #define CC1101_PTEST 0x2A /* Production test */
  158. #define CC1101_AGCTEST 0x2B /* AGC test */
  159. #define CC1101_TEST2 0x2C /* Various test settings */
  160. #define CC1101_TEST1 0x2D /* Various test settings */
  161. #define CC1101_TEST0 0x2E /* Various test settings */
  162. /* Status registers */
  163. #define CC1101_PARTNUM (0x30 | 0xc0) /* Part number */
  164. #define CC1101_VERSION (0x31 | 0xc0) /* Current version number */
  165. #define CC1101_FREQEST (0x32 | 0xc0) /* Frequency offset estimate */
  166. #define CC1101_LQI (0x33 | 0xc0) /* Demodulator estimate for link quality */
  167. #define CC1101_RSSI (0x34 | 0xc0) /* Received signal strength indication */
  168. #define CC1101_MARCSTATE (0x35 | 0xc0) /* Control state machine state */
  169. #define CC1101_WORTIME1 (0x36 | 0xc0) /* High byte of WOR timer */
  170. #define CC1101_WORTIME0 (0x37 | 0xc0) /* Low byte of WOR timer */
  171. #define CC1101_PKTSTATUS (0x38 | 0xc0) /* Current GDOx status and packet status */
  172. #define CC1101_VCO_VC_DAC (0x39 | 0xc0) /* Current setting from PLL cal module */
  173. #define CC1101_TXBYTES (0x3A | 0xc0) /* Underflow and # of bytes in TXFIFO */
  174. #define CC1101_RXBYTES (0x3B | 0xc0) /* Overflow and # of bytes in RXFIFO */
  175. #define CC1101_RCCTRL1_STATUS (0x3C | 0xc0) /* Last RC oscilator calibration results */
  176. #define CC1101_RCCTRL0_STATUS (0x3D | 0xc0) /* Last RC oscilator calibration results */
  177. /* Multi byte memory locations */
  178. #define CC1101_PATABLE 0x3E
  179. #define CC1101_TXFIFO 0x3F
  180. #define CC1101_RXFIFO 0x3F
  181. /* Definitions for burst/single access to registers */
  182. #define CC1101_WRITE_BURST 0x40
  183. #define CC1101_READ_SINGLE 0x80
  184. #define CC1101_READ_BURST 0xC0
  185. /* Strobe commands */
  186. #define CC1101_SRES 0x30 /* Reset chip. */
  187. #define CC1101_SFSTXON 0x31 /* Enable and calibrate frequency synthesizer (if MCSM0.FS_AUTOCAL=1). */
  188. #define CC1101_SXOFF 0x32 /* Turn off crystal oscillator. */
  189. #define CC1101_SCAL 0x33 /* Calibrate frequency synthesizer and turn it off */
  190. #define CC1101_SRX 0x34 /* Enable RX. Perform calibration first if switching from IDLE and MCSM0.FS_AUTOCAL=1. */
  191. #define CC1101_STX 0x35 /* Enable TX. Perform calibration first if IDLE and MCSM0.FS_AUTOCAL=1. */
  192. /* If switching from RX state and CCA is enabled then go directly to TX if channel is clear. */
  193. #define CC1101_SIDLE 0x36 /* Exit RX / TX, turn off frequency synthesizer and exit Wake-On-Radio mode if applicable. */
  194. #define CC1101_SAFC 0x37 /* Perform AFC adjustment of the frequency synthesizer */
  195. #define CC1101_SWOR 0x38 /* Start automatic RX polling sequence (Wake-on-Radio) */
  196. #define CC1101_SPWD 0x39 /* Enter power down mode when CSn goes high. */
  197. #define CC1101_SFRX 0x3A /* Flush the RX FIFO buffer. */
  198. #define CC1101_SFTX 0x3B /* Flush the TX FIFO buffer. */
  199. #define CC1101_SWORRST 0x3C /* Reset real time clock. */
  200. #define CC1101_SNOP 0x3D /* No operation. */
  201. /* Modem Control */
  202. #define CC1101_MCSM0_XOSC_FORCE_ON 0x01
  203. /* Chip Status Byte
  204. */
  205. /* Bit fields in the chip status byte */
  206. #define CC1101_STATUS_CHIP_RDYn_BM 0x80
  207. #define CC1101_STATUS_STATE_BM 0x70
  208. #define CC1101_STATUS_FIFO_BYTES_AVAILABLE_BM 0x0F
  209. /* Chip states */
  210. #define CC1101_STATE_MASK 0x70
  211. #define CC1101_STATE_IDLE 0x00
  212. #define CC1101_STATE_RX 0x10
  213. #define CC1101_STATE_TX 0x20
  214. #define CC1101_STATE_FSTXON 0x30
  215. #define CC1101_STATE_CALIBRATE 0x40
  216. #define CC1101_STATE_SETTLING 0x50
  217. #define CC1101_STATE_RX_OVERFLOW 0x60
  218. #define CC1101_STATE_TX_UNDERFLOW 0x70
  219. /* Values of the MACRSTATE register */
  220. #define CC1101_MARCSTATE_SLEEP 0x00
  221. #define CC1101_MARCSTATE_IDLE 0x01
  222. #define CC1101_MARCSTATE_XOFF 0x02
  223. #define CC1101_MARCSTATE_VCOON_MC 0x03
  224. #define CC1101_MARCSTATE_REGON_MC 0x04
  225. #define CC1101_MARCSTATE_MANCAL 0x05
  226. #define CC1101_MARCSTATE_VCOON 0x06
  227. #define CC1101_MARCSTATE_REGON 0x07
  228. #define CC1101_MARCSTATE_STARTCAL 0x08
  229. #define CC1101_MARCSTATE_BWBOOST 0x09
  230. #define CC1101_MARCSTATE_FS_LOCK 0x0A
  231. #define CC1101_MARCSTATE_IFADCON 0x0B
  232. #define CC1101_MARCSTATE_ENDCAL 0x0C
  233. #define CC1101_MARCSTATE_RX 0x0D
  234. #define CC1101_MARCSTATE_RX_END 0x0E
  235. #define CC1101_MARCSTATE_RX_RST 0x0F
  236. #define CC1101_MARCSTATE_TXRX_SWITCH 0x10
  237. #define CC1101_MARCSTATE_RXFIFO_OVERFLOW 0x11
  238. #define CC1101_MARCSTATE_FSTXON 0x12
  239. #define CC1101_MARCSTATE_TX 0x13
  240. #define CC1101_MARCSTATE_TX_END 0x14
  241. #define CC1101_MARCSTATE_RXTX_SWITCH 0x15
  242. #define CC1101_MARCSTATE_TXFIFO_UNDERFLOW 0x16
  243. /* Part number and version */
  244. #define CC1101_PARTNUM_VALUE 0x00
  245. #define CC1101_VERSION_VALUE 0x04
  246. /* Others ... */
  247. #define CC1101_LQI_CRC_OK_BM 0x80
  248. #define CC1101_LQI_EST_BM 0x7F
  249. /****************************************************************************
  250. * Private Data Types
  251. ****************************************************************************/
  252. #define FLAGS_RXONLY 1 /* Indicates receive operation only */
  253. #define FLAGS_XOSCENABLED 2 /* Indicates that one pin is configured as XOSC/n */
  254. struct cc1101_dev_s
  255. {
  256. const struct c1101_rfsettings_s *rfsettings;
  257. struct spi_dev_s *spi;
  258. uint8_t isrpin; /* CC1101 pin used to trigger interrupts */
  259. uint32_t pinset; /* GPIO of the MCU */
  260. uint8_t flags;
  261. uint8_t channel;
  262. uint8_t power;
  263. };
  264. /****************************************************************************
  265. * Private Data
  266. ****************************************************************************/
  267. static volatile int cc1101_interrupt = 0;
  268. /****************************************************************************
  269. * Private Functions
  270. ****************************************************************************/
  271. void cc1101_access_begin(FAR struct cc1101_dev_s *dev)
  272. {
  273. (void)SPI_LOCK(dev->spi, true);
  274. SPI_SELECT(dev->spi, SPIDEV_WIRELESS(0), true);
  275. SPI_SETMODE(dev->spi, SPIDEV_MODE0); /* CPOL=0, CPHA=0 */
  276. SPI_SETBITS(dev->spi, 8);
  277. (void)SPI_HWFEATURES(dev->spi, 0);
  278. }
  279. void cc1101_access_end(FAR struct cc1101_dev_s *dev)
  280. {
  281. SPI_SELECT(dev->spi, SPIDEV_WIRELESS(0), false);
  282. (void)SPI_LOCK(dev->spi, false);
  283. }
  284. /* CC1101 Access with Range Check
  285. *
  286. * Input Paramters:
  287. * dev CC1101 Private Structure
  288. * addr CC1101 Address
  289. * buf Pointer to buffer, either for read or write access
  290. * length when >0 it denotes read access, when <0 it denotes write
  291. * access of -length. abs(length) greater of 1 implies burst mode,
  292. * however
  293. *
  294. * Returned Value:
  295. * OK on success or errno is set.
  296. */
  297. int cc1101_access(FAR struct cc1101_dev_s *dev, uint8_t addr,
  298. FAR uint8_t *buf, int length)
  299. {
  300. int stabyte;
  301. /* Address cannot explicitly define READ command while length WRITE.
  302. * Also access to these cells is only permitted as one byte, eventhough
  303. * transfer is marked as BURST!
  304. */
  305. if ((addr & CC1101_READ_SINGLE) && length != 1)
  306. {
  307. return ERROR;
  308. }
  309. /* Prepare SPI */
  310. cc1101_access_begin(dev);
  311. if (length > 1 || length < -1)
  312. {
  313. SPI_SETFREQUENCY(dev->spi, CC1101_SPIFREQ_BURST);
  314. }
  315. else
  316. {
  317. SPI_SETFREQUENCY(dev->spi, CC1101_SPIFREQ_SINGLE);
  318. }
  319. /* Transfer */
  320. if (length <= 0)
  321. {
  322. /* 0 length are command strobes */
  323. if (length < -1)
  324. {
  325. addr |= CC1101_WRITE_BURST;
  326. }
  327. stabyte = SPI_SEND(dev->spi, addr);
  328. if (length)
  329. {
  330. SPI_SNDBLOCK(dev->spi, buf, -length);
  331. }
  332. }
  333. else
  334. {
  335. addr |= CC1101_READ_SINGLE;
  336. if (length > 1)
  337. {
  338. addr |= CC1101_READ_BURST;
  339. }
  340. stabyte = SPI_SEND(dev->spi, addr);
  341. SPI_RECVBLOCK(dev->spi, buf, length);
  342. }
  343. cc1101_access_end(dev);
  344. return stabyte;
  345. }
  346. /* Strobes command and returns chip status byte
  347. *
  348. * By default commands are send as Write. To a command,
  349. * CC1101_READ_SINGLE may be OR'ed to obtain the number of RX bytes
  350. * pending in RX FIFO.
  351. */
  352. inline uint8_t cc1101_strobe(struct cc1101_dev_s *dev, uint8_t command)
  353. {
  354. uint8_t status;
  355. cc1101_access_begin(dev);
  356. SPI_SETFREQUENCY(dev->spi, CC1101_SPIFREQ_SINGLE);
  357. status = SPI_SEND(dev->spi, command);
  358. cc1101_access_end(dev);
  359. return status;
  360. }
  361. int cc1101_reset(struct cc1101_dev_s *dev)
  362. {
  363. cc1101_strobe(dev, CC1101_SRES);
  364. return OK;
  365. }
  366. int cc1101_checkpart(struct cc1101_dev_s *dev)
  367. {
  368. uint8_t partnum;
  369. uint8_t version;
  370. if (cc1101_access(dev, CC1101_PARTNUM, &partnum, 1) < 0 ||
  371. cc1101_access(dev, CC1101_VERSION, &version, 1) < 0)
  372. {
  373. return ERROR;
  374. }
  375. if (partnum == CC1101_PARTNUM_VALUE && version == CC1101_VERSION_VALUE)
  376. {
  377. return OK;
  378. }
  379. return ERROR;
  380. }
  381. void cc1101_dumpregs(struct cc1101_dev_s *dev, uint8_t addr, uint8_t length)
  382. {
  383. uint8_t buf[0x30], i;
  384. cc1101_access(dev, addr, (FAR uint8_t *)buf, length);
  385. /* REVISIT: printf() should not be used from within the OS */
  386. printf("CC1101[%2x]: ", addr);
  387. for (i = 0; i < length; i++)
  388. {
  389. printf(" %2x,", buf[i]);
  390. }
  391. printf("\n");
  392. }
  393. void cc1101_setpacketctrl(struct cc1101_dev_s *dev)
  394. {
  395. uint8_t values[3];
  396. values[0] = 0; /* Rx FIFO threshold = 32, Tx FIFO threshold = 33 */
  397. cc1101_access(dev, CC1101_FIFOTHR, values, -1);
  398. /* Packet length
  399. * Limit it to 61 bytes in total: pktlen, data[61], rssi, lqi
  400. */
  401. values[0] = CC1101_PACKET_MAXDATALEN;
  402. cc1101_access(dev, CC1101_PKTLEN, values, -1);
  403. /* Packet Control */
  404. values[0] = 0x04; /* Append status: RSSI and LQI at the end of received packet */
  405. /* TODO: CRC Auto Flash bit 0x08 ??? */
  406. values[1] = 0x05; /* CRC in Rx and Tx Enabled: Variable Packet mode, defined by first byte */
  407. /* TODO: Enable data whitening ... */
  408. cc1101_access(dev, CC1101_PKTCTRL1, values, -2);
  409. /* Main Radio Control State Machine */
  410. values[0] = 0x07; /* No time-out */
  411. values[1] = 0x00; /* Clear channel if RSSI < thr && !receiving;
  412. * TX -> RX, RX -> RX: 0x3F */
  413. values[2] = CC1101_MCSM0_VALUE; /* Calibrate on IDLE -> RX/TX, OSC Timeout = ~500 us
  414. * TODO: has XOSC_FORCE_ON */
  415. cc1101_access(dev, CC1101_MCSM2, values, -3);
  416. /* Wake-On Radio Control */
  417. /* Not used yet. */
  418. /* WOREVT1:WOREVT0 - 16-bit timeout register */
  419. }
  420. /****************************************************************************
  421. * Callbacks
  422. ****************************************************************************/
  423. /* External line triggers this callback
  424. *
  425. * The concept todo is:
  426. * - GPIO provides EXTI Interrupt
  427. * - It should handle EXTI Interrupts in ISR, to which chipcon can
  428. * register a callback (and others). The ISR then foreach() calls a
  429. * its callback, and it is up to peripheral to find, whether the cause
  430. * of EXTI ISR was itself.
  431. **/
  432. int cc1101_eventcb(int irq, FAR void *context)
  433. {
  434. cc1101_interrupt++;
  435. return OK;
  436. }
  437. /****************************************************************************
  438. * Public Functions
  439. ****************************************************************************/
  440. struct cc1101_dev_s *cc1101_init(struct spi_dev_s *spi, uint8_t isrpin,
  441. uint32_t pinset, const struct c1101_rfsettings_s *rfsettings)
  442. {
  443. struct cc1101_dev_s *dev;
  444. ASSERT(spi);
  445. if ((dev = kmm_malloc(sizeof(struct cc1101_dev_s))) == NULL)
  446. {
  447. errno = ENOMEM;
  448. return NULL;
  449. }
  450. dev->rfsettings = rfsettings;
  451. dev->spi = spi;
  452. dev->isrpin = isrpin;
  453. dev->pinset = pinset;
  454. dev->flags = 0;
  455. dev->channel = rfsettings->CHMIN;
  456. dev->power = rfsettings->PAMAX;
  457. /* Reset chip, check status bytes */
  458. if (cc1101_reset(dev) < 0)
  459. {
  460. kmm_free(dev);
  461. errno = EFAULT;
  462. return NULL;
  463. }
  464. /* Check part compatibility */
  465. if (cc1101_checkpart(dev) < 0)
  466. {
  467. kmm_free(dev);
  468. errno = ENODEV;
  469. return NULL;
  470. }
  471. /* Configure CC1101:
  472. * - disable GDOx for best performance
  473. * - load RF
  474. * - and packet control
  475. */
  476. cc1101_setgdo(dev, CC1101_PIN_GDO0, CC1101_GDO_HIZ);
  477. cc1101_setgdo(dev, CC1101_PIN_GDO1, CC1101_GDO_HIZ);
  478. cc1101_setgdo(dev, CC1101_PIN_GDO2, CC1101_GDO_HIZ);
  479. cc1101_setrf(dev, rfsettings);
  480. cc1101_setpacketctrl(dev);
  481. /* Set the ISR to be triggerred on falling edge of the:
  482. *
  483. * 6 (0x06) Asserts when sync word has been sent / received, and
  484. * de-asserts at the end of the packet. In RX, the pin will de-assert
  485. * when the optional address check fails or the RX FIFO overflows.
  486. * In TX the pin will de-assert if the TX FIFO underflows.
  487. */
  488. cc1101_setgdo(dev, dev->isrpin, CC1101_GDO_SYNC);
  489. /* Configure to receive interrupts on the external GPIO interrupt line.
  490. *
  491. * REVISIT: There is no MCU-independent way to do this in this
  492. * context.
  493. */
  494. return dev;
  495. }
  496. int cc1101_deinit(struct cc1101_dev_s *dev)
  497. {
  498. ASSERT(dev);
  499. /* Release the external GPIO interrupt
  500. *
  501. * REVISIT: There is no MCU-independent way to do this in this
  502. * context.
  503. */
  504. /* Power down chip */
  505. cc1101_powerdown(dev);
  506. /* Release external interrupt line */
  507. kmm_free(dev);
  508. return 0;
  509. }
  510. int cc1101_powerup(struct cc1101_dev_s *dev)
  511. {
  512. ASSERT(dev);
  513. return 0;
  514. }
  515. int cc1101_powerdown(struct cc1101_dev_s *dev)
  516. {
  517. ASSERT(dev);
  518. return 0;
  519. }
  520. int cc1101_setgdo(struct cc1101_dev_s *dev, uint8_t pin, uint8_t function)
  521. {
  522. ASSERT(dev);
  523. ASSERT(pin <= CC1101_IOCFG0);
  524. if (function >= CC1101_GDO_CLK_XOSC1)
  525. {
  526. /* Only one pin can be enabled at a time as XOSC/n */
  527. if (dev->flags & FLAGS_XOSCENABLED)
  528. {
  529. return -EPERM;
  530. }
  531. /* Force XOSC to stay active even in sleep mode */
  532. int value = CC1101_MCSM0_VALUE | CC1101_MCSM0_XOSC_FORCE_ON;
  533. cc1101_access(dev, CC1101_MCSM0, (FAR uint8_t *)&value, -1);
  534. dev->flags |= FLAGS_XOSCENABLED;
  535. }
  536. else if (dev->flags & FLAGS_XOSCENABLED)
  537. {
  538. /* Disable XOSC in sleep mode */
  539. int value = CC1101_MCSM0_VALUE;
  540. cc1101_access(dev, CC1101_MCSM0, (FAR uint8_t *)&value, -1);
  541. dev->flags &= ~FLAGS_XOSCENABLED;
  542. }
  543. return cc1101_access(dev, pin, &function, -1);
  544. }
  545. int cc1101_setrf(struct cc1101_dev_s *dev, const struct c1101_rfsettings_s *settings)
  546. {
  547. ASSERT(dev);
  548. ASSERT(settings);
  549. if (cc1101_access(dev, CC1101_FSCTRL1, (FAR uint8_t *)&settings->FSCTRL1, -11) < 0)
  550. {
  551. return ERROR;
  552. }
  553. if (cc1101_access(dev, CC1101_FOCCFG, (FAR uint8_t *)&settings->FOCCFG, -5) < 0)
  554. {
  555. return ERROR;
  556. }
  557. if (cc1101_access(dev, CC1101_FREND1, (FAR uint8_t *)&settings->FREND1, -6) < 0)
  558. {
  559. return ERROR;
  560. }
  561. /* Load Power Table */
  562. if (cc1101_access(dev, CC1101_PATABLE, (FAR uint8_t *)settings->PA, -8) < 0)
  563. {
  564. return ERROR;
  565. }
  566. /* If channel is out of valid range, mark that. Limit power.
  567. * We are not allowed to send any data, but are allowed to listen
  568. * and receive.
  569. */
  570. cc1101_setchannel(dev, dev->channel);
  571. cc1101_setpower(dev, dev->power);
  572. return OK;
  573. }
  574. int cc1101_setchannel(struct cc1101_dev_s *dev, uint8_t channel)
  575. {
  576. ASSERT(dev);
  577. /* Store localy in further checks */
  578. dev->channel = channel;
  579. /* If channel is out of valid, we are allowed to listen and receive only */
  580. if (channel < dev->rfsettings->CHMIN || channel > dev->rfsettings->CHMAX)
  581. {
  582. dev->flags |= FLAGS_RXONLY;
  583. }
  584. else
  585. {
  586. dev->flags &= ~FLAGS_RXONLY;
  587. }
  588. cc1101_access(dev, CC1101_CHANNR, &dev->channel, -1);
  589. return dev->flags & FLAGS_RXONLY;
  590. }
  591. uint8_t cc1101_setpower(struct cc1101_dev_s *dev, uint8_t power)
  592. {
  593. ASSERT(dev);
  594. if (power > dev->rfsettings->PAMAX)
  595. {
  596. power = dev->rfsettings->PAMAX;
  597. }
  598. dev->power = power;
  599. if (power == 0)
  600. {
  601. dev->flags |= FLAGS_RXONLY;
  602. return 0;
  603. }
  604. else
  605. {
  606. dev->flags &= ~FLAGS_RXONLY;
  607. }
  608. /* Add remaining part from RF table (to get rid of readback) */
  609. power--;
  610. power |= dev->rfsettings->FREND0;
  611. /* On error, report that as zero power */
  612. if (cc1101_access(dev, CC1101_FREND0, &power, -1) < 0)
  613. {
  614. dev->power = 0;
  615. }
  616. return dev->power;
  617. }
  618. int cc1101_calcRSSIdBm(int rssi)
  619. {
  620. if (rssi >= 128)
  621. {
  622. rssi -= 256;
  623. }
  624. return (rssi >> 1) - 74;
  625. }
  626. int cc1101_receive(struct cc1101_dev_s *dev)
  627. {
  628. ASSERT(dev);
  629. /* \todo Wait for IDLE before going into another state? */
  630. cc1101_interrupt = 0;
  631. cc1101_strobe(dev, CC1101_SRX | CC1101_READ_SINGLE);
  632. return 0;
  633. }
  634. int cc1101_read(struct cc1101_dev_s *dev, uint8_t * buf, size_t size)
  635. {
  636. ASSERT(dev);
  637. if (buf == NULL)
  638. {
  639. if (size == 0)
  640. {
  641. return 64;
  642. }
  643. /* else received packet size */
  644. return 0;
  645. }
  646. if (cc1101_interrupt == 0)
  647. {
  648. return 0;
  649. }
  650. int status = cc1101_strobe(dev, CC1101_SNOP | CC1101_READ_SINGLE);
  651. if (status & CC1101_STATUS_FIFO_BYTES_AVAILABLE_BM &&
  652. (status & CC1101_STATE_MASK) == CC1101_STATE_IDLE)
  653. {
  654. uint8_t nbytes;
  655. cc1101_access(dev, CC1101_RXFIFO, &nbytes, 1);
  656. nbytes += 2; /* RSSI and LQI */
  657. cc1101_access(dev, CC1101_RXFIFO, buf, (nbytes > size) ? size : nbytes);
  658. /* Flush remaining bytes, if there is no room to receive
  659. * or if there is a BAD CRC
  660. */
  661. if (nbytes > size || (nbytes <= size && !(buf[nbytes-1]&0x80)))
  662. {
  663. ninfo("Flushing RX FIFO\n");
  664. cc1101_strobe(dev, CC1101_SFRX);
  665. }
  666. return nbytes;
  667. }
  668. return 0;
  669. }
  670. int cc1101_write(struct cc1101_dev_s *dev, const uint8_t *buf, size_t size)
  671. {
  672. uint8_t packetlen;
  673. ASSERT(dev);
  674. ASSERT(buf);
  675. if (dev->flags & FLAGS_RXONLY)
  676. {
  677. return -EPERM;
  678. }
  679. /* Present limit */
  680. if (size > CC1101_PACKET_MAXDATALEN)
  681. {
  682. packetlen = CC1101_PACKET_MAXDATALEN;
  683. }
  684. else
  685. {
  686. packetlen = size;
  687. }
  688. cc1101_access(dev, CC1101_TXFIFO, &packetlen, -1);
  689. cc1101_access(dev, CC1101_TXFIFO, (FAR uint8_t *)buf, -size);
  690. return 0;
  691. }
  692. int cc1101_send(struct cc1101_dev_s *dev)
  693. {
  694. ASSERT(dev);
  695. if (dev->flags & FLAGS_RXONLY)
  696. {
  697. return -EPERM;
  698. }
  699. cc1101_interrupt = 0;
  700. cc1101_strobe(dev, CC1101_STX);
  701. /* wait until send, going to IDLE */
  702. while (cc1101_interrupt == 0);
  703. return 0;
  704. }
  705. int cc1101_idle(struct cc1101_dev_s *dev)
  706. {
  707. ASSERT(dev);
  708. cc1101_strobe(dev, CC1101_SIDLE);
  709. return 0;
  710. }