/nuttx/drivers/wireless/cc1101/cc1101.c

https://github.com/luozhongchao/Firmware · C · 812 lines · 405 code · 192 blank · 215 comment · 54 complexity · 906fd59cdcb748fd647f6a0bb8ede95d MD5 · raw file

  1. /****************************************************************************
  2. * drivers/wireless/cc1101/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. /** \file
  37. * \author Uros Platise
  38. * \brief Chipcon CC1101 Device Driver
  39. *
  40. * Features:
  41. * - Maximum data length: 61 bytes CC1101_PACKET_MAXDATALEN
  42. * - Packet length includes two additional bytes: CC1101_PACKET_MAXTOTALLEN
  43. * - Requires one GDO to trigger end-of-packets in RX and TX modes.
  44. * - Variable packet length with data payload between 1..61 bytes
  45. * (three bytes are reserved for packet length, and RSSI and LQI
  46. * appended at the end of RXFIFO after each reception)
  47. * - Support for General Digital Outputs with overload protection
  48. * (single XOSC pin is allowed, otherwise error is returned)
  49. * - Loadable RF settings, one for ISM Region 1 (Europe) and one for
  50. * ISM Region 2 (Complete America)
  51. *
  52. * Todo:
  53. * - Extend max packet length up to 255 bytes or rather infinite < 4096 bytes
  54. * - Power up/down modes
  55. * - Sequencing between states or add protection for correct termination of
  56. * various different state (so that CC1101 does not block in case of improper use)
  57. *
  58. * \par RSSI and LQI value interpretation
  59. *
  60. * The LQI can be read from the LQI status register or it can be appended
  61. * to the received packet in the RX FIFO. LQI is a metric of the current
  62. * quality of the received signal. The LQI gives an estimate of how easily
  63. * a received signal can be demodulated by accumulating the magnitude of
  64. * the error between ideal constellations and the received signal over
  65. * the 64 symbols immediately following the sync word. LQI is best used
  66. * as a relative measurement of the link quality (a high value indicates
  67. * a better link than what a low value does), since the value is dependent
  68. * on the modulation format.
  69. *
  70. * To simplify: If the received modulation is FSK or GFSK, the receiver
  71. * will measure the frequency of each "bit" and compare it with the
  72. * expected frequency based on the channel frequency and the deviation
  73. * and the measured frequency offset. If other modulations are used, the
  74. * error of the modulated parameter (frequency for FSK/GFSK, phase for
  75. * MSK, amplitude for ASK etc) will be measured against the expected
  76. * ideal value
  77. *
  78. * RSSI (Received Signal Strength Indicator) is a signal strength
  79. * indication. It does not care about the "quality" or "correctness" of
  80. * the signal. LQI does not care about the actual signal strength, but
  81. * the signal quality often is linked to signal strength. This is because
  82. * a strong signal is likely to be less affected by noise and thus will
  83. * be seen as "cleaner" or more "correct" by the receiver.
  84. *
  85. * There are four to five "extreme cases" that can be used to illustrate
  86. * how RSSI and LQI work:
  87. * 1. A weak signal in the presence of noise may give low RSSI and low LQI.
  88. * 2. A weak signal in "total" absence of noise may give low RSSI and high LQI.
  89. * 3. Strong noise (usually coming from an interferer) may give high RSSI and low LQI.
  90. * 4. A strong signal without much noise may give high RSSI and high LQI.
  91. * 5. A very strong signal that causes the receiver to saturate may give
  92. * high RSSI and low LQI.
  93. *
  94. * Note that both RSSI and LQI are best used as relative measurements since
  95. * the values are dependent on the modulation format.
  96. **/
  97. #include <nuttx/config.h>
  98. #include <assert.h>
  99. #include <stdlib.h>
  100. #include <string.h>
  101. #include <errno.h>
  102. #include <stdio.h>
  103. #include <nuttx/kmalloc.h>
  104. #include <nuttx/wireless/cc1101.h>
  105. /****************************************************************************
  106. * Declarations
  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. /*
  204. * Chip Status Byte
  205. */
  206. /* Bit fields in the chip status byte */
  207. #define CC1101_STATUS_CHIP_RDYn_BM 0x80
  208. #define CC1101_STATUS_STATE_BM 0x70
  209. #define CC1101_STATUS_FIFO_BYTES_AVAILABLE_BM 0x0F
  210. /* Chip states */
  211. #define CC1101_STATE_MASK 0x70
  212. #define CC1101_STATE_IDLE 0x00
  213. #define CC1101_STATE_RX 0x10
  214. #define CC1101_STATE_TX 0x20
  215. #define CC1101_STATE_FSTXON 0x30
  216. #define CC1101_STATE_CALIBRATE 0x40
  217. #define CC1101_STATE_SETTLING 0x50
  218. #define CC1101_STATE_RX_OVERFLOW 0x60
  219. #define CC1101_STATE_TX_UNDERFLOW 0x70
  220. /* Values of the MACRSTATE register */
  221. #define CC1101_MARCSTATE_SLEEP 0x00
  222. #define CC1101_MARCSTATE_IDLE 0x01
  223. #define CC1101_MARCSTATE_XOFF 0x02
  224. #define CC1101_MARCSTATE_VCOON_MC 0x03
  225. #define CC1101_MARCSTATE_REGON_MC 0x04
  226. #define CC1101_MARCSTATE_MANCAL 0x05
  227. #define CC1101_MARCSTATE_VCOON 0x06
  228. #define CC1101_MARCSTATE_REGON 0x07
  229. #define CC1101_MARCSTATE_STARTCAL 0x08
  230. #define CC1101_MARCSTATE_BWBOOST 0x09
  231. #define CC1101_MARCSTATE_FS_LOCK 0x0A
  232. #define CC1101_MARCSTATE_IFADCON 0x0B
  233. #define CC1101_MARCSTATE_ENDCAL 0x0C
  234. #define CC1101_MARCSTATE_RX 0x0D
  235. #define CC1101_MARCSTATE_RX_END 0x0E
  236. #define CC1101_MARCSTATE_RX_RST 0x0F
  237. #define CC1101_MARCSTATE_TXRX_SWITCH 0x10
  238. #define CC1101_MARCSTATE_RXFIFO_OVERFLOW 0x11
  239. #define CC1101_MARCSTATE_FSTXON 0x12
  240. #define CC1101_MARCSTATE_TX 0x13
  241. #define CC1101_MARCSTATE_TX_END 0x14
  242. #define CC1101_MARCSTATE_RXTX_SWITCH 0x15
  243. #define CC1101_MARCSTATE_TXFIFO_UNDERFLOW 0x16
  244. /* Part number and version */
  245. #define CC1101_PARTNUM_VALUE 0x00
  246. #define CC1101_VERSION_VALUE 0x04
  247. /*
  248. * Others ...
  249. */
  250. #define CC1101_LQI_CRC_OK_BM 0x80
  251. #define CC1101_LQI_EST_BM 0x7F
  252. /****************************************************************************
  253. * Private Data Types
  254. ****************************************************************************/
  255. #define FLAGS_RXONLY 1 /* Indicates receive operation only */
  256. #define FLAGS_XOSCENABLED 2 /* Indicates that one pin is configured as XOSC/n */
  257. struct cc1101_dev_s {
  258. const struct c1101_rfsettings_s *rfsettings;
  259. struct spi_dev_s * spi;
  260. uint8_t isrpin; /* CC1101 pin used to trigger interrupts */
  261. uint32_t pinset; /* GPIO of the MCU */
  262. uint8_t flags;
  263. uint8_t channel;
  264. uint8_t power;
  265. };
  266. /****************************************************************************
  267. * Private Functions
  268. ****************************************************************************/
  269. void cc1101_access_begin(struct cc1101_dev_s * dev)
  270. {
  271. (void)SPI_LOCK(dev->spi, true);
  272. SPI_SELECT(dev->spi, SPIDEV_WIRELESS, true);
  273. SPI_SETMODE(dev->spi, SPIDEV_MODE0); /* CPOL=0, CPHA=0 */
  274. SPI_SETBITS(dev->spi, 8);
  275. }
  276. void cc1101_access_end(struct cc1101_dev_s * dev)
  277. {
  278. SPI_SELECT(dev->spi, SPIDEV_WIRELESS, false);
  279. (void)SPI_LOCK(dev->spi, false);
  280. }
  281. /** CC1101 Access with Range Check
  282. *
  283. * \param dev CC1101 Private Structure
  284. * \param addr CC1101 Address
  285. * \param buf Pointer to buffer, either for read or write access
  286. * \param length when >0 it denotes read access, when <0 it denotes write
  287. * access of -length. abs(length) greater of 1 implies burst mode,
  288. * however
  289. * \return OK on success or errno is set.
  290. */
  291. int cc1101_access(struct cc1101_dev_s * dev, uint8_t addr, uint8_t *buf, int length)
  292. {
  293. int stabyte;
  294. /* Address cannot explicitly define READ command while length WRITE.
  295. * Also access to these cells is only permitted as one byte, eventhough
  296. * transfer is marked as BURST!
  297. */
  298. if ( (addr & CC1101_READ_SINGLE) && length != 1 )
  299. return ERROR;
  300. /* Prepare SPI */
  301. cc1101_access_begin(dev);
  302. if (length>1 || length < -1)
  303. SPI_SETFREQUENCY(dev->spi, CC1101_SPIFREQ_BURST);
  304. else SPI_SETFREQUENCY(dev->spi, CC1101_SPIFREQ_SINGLE);
  305. /* Transfer */
  306. if (length <= 0) { /* 0 length are command strobes */
  307. if (length < -1)
  308. addr |= CC1101_WRITE_BURST;
  309. stabyte = SPI_SEND(dev->spi, addr);
  310. if (length) {
  311. SPI_SNDBLOCK(dev->spi, buf, -length);
  312. }
  313. }
  314. else {
  315. addr |= CC1101_READ_SINGLE;
  316. if (length > 1)
  317. addr |= CC1101_READ_BURST;
  318. stabyte = SPI_SEND(dev->spi, addr);
  319. SPI_RECVBLOCK(dev->spi, buf, length);
  320. }
  321. cc1101_access_end(dev);
  322. return stabyte;
  323. }
  324. /** Strobes command and returns chip status byte
  325. *
  326. * By default commands are send as Write. To a command,
  327. * CC1101_READ_SINGLE may be OR'ed to obtain the number of RX bytes
  328. * pending in RX FIFO.
  329. */
  330. inline uint8_t cc1101_strobe(struct cc1101_dev_s * dev, uint8_t command)
  331. {
  332. uint8_t status;
  333. cc1101_access_begin(dev);
  334. SPI_SETFREQUENCY(dev->spi, CC1101_SPIFREQ_SINGLE);
  335. status = SPI_SEND(dev->spi, command);
  336. cc1101_access_end(dev);
  337. return status;
  338. }
  339. int cc1101_reset(struct cc1101_dev_s * dev)
  340. {
  341. cc1101_strobe(dev, CC1101_SRES);
  342. return OK;
  343. }
  344. int cc1101_checkpart(struct cc1101_dev_s * dev)
  345. {
  346. uint8_t partnum, version;
  347. if (cc1101_access(dev, CC1101_PARTNUM, &partnum, 1) < 0 ||
  348. cc1101_access(dev, CC1101_VERSION, &version, 1) < 0)
  349. return ERROR;
  350. if (partnum == CC1101_PARTNUM_VALUE && version == CC1101_VERSION_VALUE)
  351. return OK;
  352. return ERROR;
  353. }
  354. void cc1101_dumpregs(struct cc1101_dev_s * dev, uint8_t addr, uint8_t length)
  355. {
  356. uint8_t buf[0x30], i;
  357. cc1101_access(dev, addr, buf, length);
  358. printf("CC1101[%2x]: ", addr);
  359. for (i=0; i<length; i++) printf(" %2x,", buf[i]);
  360. printf("\n");
  361. }
  362. void cc1101_setpacketctrl(struct cc1101_dev_s * dev)
  363. {
  364. uint8_t values[3];
  365. values[0] = 0; /* Rx FIFO threshold = 32, Tx FIFO threshold = 33 */
  366. cc1101_access(dev, CC1101_FIFOTHR, values, -1);
  367. /* Packet length
  368. * Limit it to 61 bytes in total: pktlen, data[61], rssi, lqi
  369. */
  370. values[0] = CC1101_PACKET_MAXDATALEN;
  371. cc1101_access(dev, CC1101_PKTLEN, values, -1);
  372. /* Packet Control */
  373. values[0] = 0x04; /* Append status: RSSI and LQI at the end of received packet */
  374. /* TODO: CRC Auto Flash bit 0x08 ??? */
  375. values[1] = 0x05; /* CRC in Rx and Tx Enabled: Variable Packet mode, defined by first byte */
  376. /* TODO: Enable data whitening ... */
  377. cc1101_access(dev, CC1101_PKTCTRL1, values, -2);
  378. /* Main Radio Control State Machine */
  379. values[0] = 0x07; /* No time-out */
  380. values[1] = 0x00; /* Clear channel if RSSI < thr && !receiving;
  381. * TX -> RX, RX -> RX: 0x3F */
  382. values[2] = CC1101_MCSM0_VALUE; /* Calibrate on IDLE -> RX/TX, OSC Timeout = ~500 us
  383. TODO: has XOSC_FORCE_ON */
  384. cc1101_access(dev, CC1101_MCSM2, values, -3);
  385. /* Wake-On Radio Control */
  386. // Not used yet.
  387. // WOREVT1:WOREVT0 - 16-bit timeout register
  388. }
  389. /****************************************************************************
  390. * Callbacks
  391. ****************************************************************************/
  392. volatile int cc1101_interrupt = 0;
  393. /** External line triggers this callback
  394. *
  395. * The concept todo is:
  396. * - GPIO provides EXTI Interrupt
  397. * - It should handle EXTI Interrupts in ISR, to which chipcon can
  398. * register a callback (and others). The ISR then foreach() calls a
  399. * its callback, and it is up to peripheral to find, whether the cause
  400. * of EXTI ISR was itself.
  401. **/
  402. int cc1101_eventcb(int irq, FAR void *context)
  403. {
  404. cc1101_interrupt++;
  405. return OK;
  406. }
  407. /****************************************************************************
  408. * Public Functions
  409. ****************************************************************************/
  410. struct cc1101_dev_s * cc1101_init(struct spi_dev_s * spi, uint8_t isrpin,
  411. uint32_t pinset, const struct c1101_rfsettings_s * rfsettings)
  412. {
  413. struct cc1101_dev_s * dev;
  414. ASSERT(spi);
  415. if ( (dev = kmalloc( sizeof(struct cc1101_dev_s) )) == NULL) {
  416. errno = ENOMEM;
  417. return NULL;
  418. }
  419. dev->rfsettings = rfsettings;
  420. dev->spi = spi;
  421. dev->isrpin = isrpin;
  422. dev->pinset = pinset;
  423. dev->flags = 0;
  424. dev->channel = rfsettings->CHMIN;
  425. dev->power = rfsettings->PAMAX;
  426. /* Reset chip, check status bytes */
  427. if ( cc1101_reset(dev) < 0 ) {
  428. kfree(dev);
  429. errno = EFAULT;
  430. return NULL;
  431. }
  432. /* Check part compatibility */
  433. if ( cc1101_checkpart(dev) < 0 ) {
  434. kfree(dev);
  435. errno = ENODEV;
  436. return NULL;
  437. }
  438. /* Configure CC1101:
  439. * - disable GDOx for best performance
  440. * - load RF
  441. * - and packet control
  442. */
  443. cc1101_setgdo(dev, CC1101_PIN_GDO0, CC1101_GDO_HIZ);
  444. cc1101_setgdo(dev, CC1101_PIN_GDO1, CC1101_GDO_HIZ);
  445. cc1101_setgdo(dev, CC1101_PIN_GDO2, CC1101_GDO_HIZ);
  446. cc1101_setrf(dev, rfsettings);
  447. cc1101_setpacketctrl(dev);
  448. /* Set the ISR to be triggerred on falling edge of the:
  449. *
  450. * 6 (0x06) Asserts when sync word has been sent / received, and
  451. * de-asserts at the end of the packet. In RX, the pin will de-assert
  452. * when the optional address check fails or the RX FIFO overflows.
  453. * In TX the pin will de-assert if the TX FIFO underflows.
  454. */
  455. cc1101_setgdo(dev, dev->isrpin, CC1101_GDO_SYNC);
  456. /* Bind to external interrupt line */
  457. // depends on STM32: TODO: Make that config within pinset and
  458. // provide general gpio interface
  459. //stm32_gpiosetevent(pinset, false, true, true, cc1101_eventcb);
  460. return dev;
  461. }
  462. int cc1101_deinit(struct cc1101_dev_s * dev)
  463. {
  464. ASSERT(dev);
  465. /* Release interrupt */
  466. //stm32_gpiosetevent(pinset, false, false, false, NULL);
  467. /* Power down chip */
  468. cc1101_powerdown(dev);
  469. /* Release external interrupt line */
  470. kfree(dev);
  471. return 0;
  472. }
  473. int cc1101_powerup(struct cc1101_dev_s * dev)
  474. {
  475. ASSERT(dev);
  476. return 0;
  477. }
  478. int cc1101_powerdown(struct cc1101_dev_s * dev)
  479. {
  480. ASSERT(dev);
  481. return 0;
  482. }
  483. int cc1101_setgdo(struct cc1101_dev_s * dev, uint8_t pin, uint8_t function)
  484. {
  485. ASSERT(dev);
  486. ASSERT(pin <= CC1101_IOCFG0);
  487. if (function >= CC1101_GDO_CLK_XOSC1) {
  488. /* Only one pin can be enabled at a time as XOSC/n */
  489. if (dev->flags & FLAGS_XOSCENABLED) return -EPERM;
  490. /* Force XOSC to stay active even in sleep mode */
  491. int value = CC1101_MCSM0_VALUE | CC1101_MCSM0_XOSC_FORCE_ON;
  492. cc1101_access(dev, CC1101_MCSM0, &value, -1);
  493. dev->flags |= FLAGS_XOSCENABLED;
  494. }
  495. else if (dev->flags & FLAGS_XOSCENABLED) {
  496. /* Disable XOSC in sleep mode */
  497. int value = CC1101_MCSM0_VALUE;
  498. cc1101_access(dev, CC1101_MCSM0, &value, -1);
  499. dev->flags &= ~FLAGS_XOSCENABLED;
  500. }
  501. return cc1101_access(dev, pin, &function, -1);
  502. }
  503. int cc1101_setrf(struct cc1101_dev_s * dev, const struct c1101_rfsettings_s *settings)
  504. {
  505. ASSERT(dev);
  506. ASSERT(settings);
  507. if (cc1101_access(dev, CC1101_FSCTRL1, &settings->FSCTRL1, -11) < 0) return ERROR;
  508. if (cc1101_access(dev, CC1101_FOCCFG, &settings->FOCCFG, -5) < 0) return ERROR;
  509. if (cc1101_access(dev, CC1101_FREND1, &settings->FREND1, -6) < 0) return ERROR;
  510. /* Load Power Table */
  511. if (cc1101_access(dev, CC1101_PATABLE, settings->PA, -8) < 0) return ERROR;
  512. /* If channel is out of valid range, mark that. Limit power.
  513. * We are not allowed to send any data, but are allowed to listen
  514. * and receive.
  515. */
  516. cc1101_setchannel(dev, dev->channel);
  517. cc1101_setpower(dev, dev->power);
  518. return OK;
  519. }
  520. int cc1101_setchannel(struct cc1101_dev_s * dev, uint8_t channel)
  521. {
  522. ASSERT(dev);
  523. /* Store localy in further checks */
  524. dev->channel = channel;
  525. /* If channel is out of valid, we are allowed to listen and receive only */
  526. if (channel < dev->rfsettings->CHMIN || channel > dev->rfsettings->CHMAX)
  527. dev->flags |= FLAGS_RXONLY;
  528. else dev->flags &= ~FLAGS_RXONLY;
  529. cc1101_access(dev, CC1101_CHANNR, &dev->channel, -1);
  530. return dev->flags & FLAGS_RXONLY;
  531. }
  532. uint8_t cc1101_setpower(struct cc1101_dev_s * dev, uint8_t power)
  533. {
  534. ASSERT(dev);
  535. if (power > dev->rfsettings->PAMAX)
  536. power = dev->rfsettings->PAMAX;
  537. dev->power = power;
  538. if (power == 0) {
  539. dev->flags |= FLAGS_RXONLY;
  540. return 0;
  541. }
  542. else dev->flags &= ~FLAGS_RXONLY;
  543. /* Add remaining part from RF table (to get rid of readback) */
  544. power--;
  545. power |= dev->rfsettings->FREND0;
  546. /* On error, report that as zero power */
  547. if (cc1101_access(dev, CC1101_FREND0, &power, -1) < 0)
  548. dev->power = 0;
  549. return dev->power;
  550. }
  551. int cc1101_calcRSSIdBm(int rssi)
  552. {
  553. if (rssi >= 128) rssi -= 256;
  554. return (rssi >> 1) - 74;
  555. }
  556. int cc1101_receive(struct cc1101_dev_s * dev)
  557. {
  558. ASSERT(dev);
  559. /* \todo Wait for IDLE before going into another state? */
  560. cc1101_interrupt = 0;
  561. cc1101_strobe(dev, CC1101_SRX | CC1101_READ_SINGLE);
  562. return 0;
  563. }
  564. int cc1101_read(struct cc1101_dev_s * dev, uint8_t * buf, size_t size)
  565. {
  566. ASSERT(dev);
  567. if (buf==NULL) {
  568. if (size==0) return 64;
  569. // else received packet size
  570. return 0;
  571. }
  572. if (cc1101_interrupt == 0) return 0;
  573. int status = cc1101_strobe(dev, CC1101_SNOP | CC1101_READ_SINGLE);
  574. if (status & CC1101_STATUS_FIFO_BYTES_AVAILABLE_BM &&
  575. (status & CC1101_STATE_MASK) == CC1101_STATE_IDLE) {
  576. uint8_t nbytes;
  577. cc1101_access(dev, CC1101_RXFIFO, &nbytes, 1);
  578. nbytes += 2; /* RSSI and LQI */
  579. cc1101_access(dev, CC1101_RXFIFO, buf, (nbytes > size) ? size : nbytes);
  580. /* Flush remaining bytes, if there is no room to receive
  581. * or if there is a BAD CRC
  582. */
  583. if (nbytes > size || (nbytes <= size && !(buf[nbytes-1]&0x80)) ) {
  584. printf("Flushing RX FIFO\n");
  585. cc1101_strobe(dev, CC1101_SFRX);
  586. }
  587. return nbytes;
  588. }
  589. return 0;
  590. }
  591. int cc1101_write(struct cc1101_dev_s * dev, const uint8_t * buf, size_t size)
  592. {
  593. uint8_t packetlen;
  594. ASSERT(dev);
  595. ASSERT(buf);
  596. if (dev->flags & FLAGS_RXONLY) return -EPERM;
  597. /* Present limit */
  598. if (size > CC1101_PACKET_MAXDATALEN)
  599. packetlen = CC1101_PACKET_MAXDATALEN;
  600. else packetlen = size;
  601. cc1101_access(dev, CC1101_TXFIFO, &packetlen, -1);
  602. cc1101_access(dev, CC1101_TXFIFO, buf, -size);
  603. return 0;
  604. }
  605. int cc1101_send(struct cc1101_dev_s * dev)
  606. {
  607. ASSERT(dev);
  608. if (dev->flags & FLAGS_RXONLY) return -EPERM;
  609. cc1101_interrupt = 0;
  610. cc1101_strobe(dev, CC1101_STX);
  611. /* wait until send, going to IDLE */
  612. while( cc1101_interrupt == 0 );
  613. return 0;
  614. }
  615. int cc1101_idle(struct cc1101_dev_s * dev)
  616. {
  617. ASSERT(dev);
  618. cc1101_strobe(dev, CC1101_SIDLE);
  619. return 0;
  620. }