ads1255.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /****************************************************************************
  2. * arch/drivers/analog/ads1255.c
  3. *
  4. * Copyright (C) 2010, 2016 Gregory Nutt. All rights reserved.
  5. * Copyright (C) 2011 Li Zhuoyi. All rights reserved.
  6. * Author: Li Zhuoyi <lzyy.cn@gmail.com>
  7. * Gregory Nutt <gnutt@nuttx.org>
  8. *
  9. * This file is a part of NuttX:
  10. *
  11. * Copyright (C) 2010 Gregory Nutt. All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in
  21. * the documentation and/or other materials provided with the
  22. * distribution.
  23. * 3. Neither the name NuttX nor the names of its contributors may be
  24. * used to endorse or promote products derived from this software
  25. * without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  30. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  31. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  32. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  33. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  34. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  35. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. ****************************************************************************/
  41. /****************************************************************************
  42. * Included Files
  43. ****************************************************************************/
  44. #include <nuttx/config.h>
  45. #include <stdio.h>
  46. #include <sys/types.h>
  47. #include <stdint.h>
  48. #include <stdbool.h>
  49. #include <semaphore.h>
  50. #include <errno.h>
  51. #include <assert.h>
  52. #include <debug.h>
  53. #include <nuttx/arch.h>
  54. #include <nuttx/wqueue.h>
  55. #include <nuttx/signal.h>
  56. #include <nuttx/spi/spi.h>
  57. #include <nuttx/analog/adc.h>
  58. #if defined(CONFIG_ADC_ADS1255)
  59. /****************************************************************************
  60. * Pre-processor Definitions
  61. ****************************************************************************/
  62. #define ADS125X_BUFON 0x02
  63. #define ADS125X_BUFOFF 0x00
  64. #define ADS125X_PGA1 0x00
  65. #define ADS125X_PGA2 0x01
  66. #define ADS125X_PGA4 0x02
  67. #define ADS125X_PGA8 0x03
  68. #define ADS125X_PGA16 0x04
  69. #define ADS125X_PGA32 0x05
  70. #define ADS125X_PGA64 0x06
  71. #define ADS125X_RDATA 0x01 /* Read Data */
  72. #define ADS125X_RDATAC 0x03 /* Read Data Continuously */
  73. #define ADS125X_SDATAC 0x0F /* Stop Read Data Continuously */
  74. #define ADS125X_RREG 0x10 /* Read from REG */
  75. #define ADS125X_WREG 0x50 /* Write to REG */
  76. #define ADS125X_SELFCAL 0xF0 /* Offset and Gain Self-Calibration */
  77. #define ADS125X_SELFOCAL 0xF1 /* Offset Self-Calibration */
  78. #define ADS125X_SELFGCAL 0xF2 /* Gain Self-Calibration */
  79. #define ADS125X_SYSOCAL 0xF3 /* System Offset Calibration */
  80. #define ADS125X_SYSGCAL 0xF4 /* System Gain Calibration */
  81. #define ADS125X_SYNC 0xFC /* Synchronize the A/D Conversion */
  82. #define ADS125X_STANDBY 0xFD /* Begin Standby Mode */
  83. #define ADS125X_RESET 0xFE /* Reset to Power-Up Values */
  84. #define ADS125X_WAKEUP 0xFF /* Completes SYNC and Exits Standby Mode */
  85. #ifndef CONFIG_ADS1255_FREQUENCY
  86. # define CONFIG_ADS1255_FREQUENCY 1000000
  87. #endif
  88. #ifndef CONFIG_ADS1255_MUX
  89. # define CONFIG_ADS1255_MUX 0x01
  90. #endif
  91. #ifndef CONFIG_ADS1255_CHMODE
  92. # define CONFIG_ADS1255_CHMODE 0x00
  93. #endif
  94. #ifndef CONFIG_ADS1255_BUFON
  95. # define CONFIG_ADS1255_BUFON 1
  96. #endif
  97. #ifndef CONFIG_ADS1255_PGA
  98. # define CONFIG_ADS1255_PGA ADS125X_PGA2
  99. #endif
  100. #ifndef CONFIG_ADS1255_SPS
  101. # define CONFIG_ADS1255_SPS 50
  102. #endif
  103. /****************************************************************************
  104. * Private Types
  105. ****************************************************************************/
  106. struct ads1255_dev_s
  107. {
  108. FAR const struct adc_callback_s *cb;
  109. FAR struct spi_dev_s *spi; /* Cached SPI device reference */
  110. struct work_s work;
  111. uint8_t channel;
  112. uint32_t sps;
  113. uint8_t pga;
  114. uint8_t buf;
  115. const uint8_t *mux;
  116. int irq;
  117. int devno;
  118. };
  119. /****************************************************************************
  120. * Private Function Prototypes
  121. ****************************************************************************/
  122. static void adc_lock(FAR struct spi_dev_s *spi);
  123. static void adc_unlock(FAR struct spi_dev_s *spi);
  124. /* ADC methods */
  125. static int adc_bind(FAR struct adc_dev_s *dev,
  126. FAR const struct adc_callback_s *callback);
  127. static void adc_reset(FAR struct adc_dev_s *dev);
  128. static int adc_setup(FAR struct adc_dev_s *dev);
  129. static void adc_shutdown(FAR struct adc_dev_s *dev);
  130. static void adc_rxint(FAR struct adc_dev_s *dev, bool enable);
  131. static int adc_ioctl(FAR struct adc_dev_s *dev, int cmd, unsigned long arg);
  132. /* Interrupt handling */
  133. static void adc_worker(FAR void *arg);
  134. static int adc_interrupt(int irq, void *context, FAR void *arg);
  135. /****************************************************************************
  136. * Private Data
  137. ****************************************************************************/
  138. static const struct adc_ops_s g_adcops =
  139. {
  140. .ao_bind = adc_bind, /* ao_bind */
  141. .ao_reset = adc_reset, /* ao_reset */
  142. .ao_setup = adc_setup, /* ao_setup */
  143. .ao_shutdown = adc_shutdown, /* ao_shutdown */
  144. .ao_rxint = adc_rxint, /* ao_rxint */
  145. .ao_ioctl = adc_ioctl /* ao_read */
  146. };
  147. static struct ads1255_dev_s g_adcpriv =
  148. {
  149. .mux = (const uint8_t [])
  150. {
  151. CONFIG_ADS1255_MUX, 0
  152. },
  153. .sps = CONFIG_ADS1255_SPS,
  154. .channel = 0,
  155. .irq = CONFIG_ADS1255_IRQ,
  156. };
  157. static struct adc_dev_s g_adcdev =
  158. {
  159. .ad_ops = &g_adcops,
  160. .ad_priv = &g_adcpriv,
  161. };
  162. /****************************************************************************
  163. * Private Functions
  164. ****************************************************************************/
  165. static uint8_t getspsreg(uint16_t sps)
  166. {
  167. static const unsigned short sps_tab[] =
  168. {
  169. 3, 7, 12, 20, 27, 40, 55, 80,
  170. 300, 750, 1500, 3000, 5000, 10000, 20000, 65535,
  171. };
  172. static const unsigned char sps_reg[] =
  173. {
  174. 0x03, 0x13, 0x23, 0x33, 0x43, 0x53, 0x63, 0x72,
  175. 0x82, 0x92, 0xa1, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0,
  176. };
  177. int i;
  178. for (i = 0; i < 16; i++)
  179. {
  180. if (sps < sps_tab[i])
  181. {
  182. return sps_reg[i];
  183. }
  184. }
  185. return sps_reg[15];
  186. }
  187. /****************************************************************************
  188. * Private Functions
  189. ****************************************************************************/
  190. /****************************************************************************
  191. * Name: adc_lock
  192. *
  193. * Description:
  194. * Lock and configure the SPI bus.
  195. *
  196. ****************************************************************************/
  197. static void adc_lock(FAR struct spi_dev_s *spi)
  198. {
  199. (void)SPI_LOCK(spi, true);
  200. SPI_SETMODE(spi, SPIDEV_MODE1);
  201. SPI_SETBITS(spi, 8);
  202. (void)SPI_HWFEATURES(spi, 0);
  203. SPI_SETFREQUENCY(spi, CONFIG_ADS1255_FREQUENCY);
  204. }
  205. /****************************************************************************
  206. * Name: adc_unlock
  207. *
  208. * Description:
  209. * Unlock the SPI bus.
  210. *
  211. ****************************************************************************/
  212. static void adc_unlock(FAR struct spi_dev_s *spi)
  213. {
  214. (void)SPI_LOCK(spi, false);
  215. }
  216. /****************************************************************************
  217. * Name: adc_bind
  218. *
  219. * Description:
  220. * Bind the upper-half driver callbacks to the lower-half implementation. This
  221. * must be called early in order to receive ADC event notifications.
  222. *
  223. ****************************************************************************/
  224. static int adc_bind(FAR struct adc_dev_s *dev,
  225. FAR const struct adc_callback_s *callback)
  226. {
  227. FAR struct ads1255_dev_s *priv = (FAR struct ads1255_dev_s *)dev->ad_priv;
  228. DEBUGASSERT(priv != NULL);
  229. priv->cb = callback;
  230. return OK;
  231. }
  232. /****************************************************************************
  233. * Name: adc_reset
  234. *
  235. * Description:
  236. * Reset the ADC device. Called early to initialize the hardware. This
  237. * is called, before ao_setup() and on error conditions.
  238. *
  239. ****************************************************************************/
  240. static void adc_reset(FAR struct adc_dev_s *dev)
  241. {
  242. FAR struct ads1255_dev_s *priv = (FAR struct ads1255_dev_s *)dev->ad_priv;
  243. FAR struct spi_dev_s *spi;
  244. DEBUGASSERT(priv != NULL && priv->spi != NULL);
  245. spi = priv->spi;
  246. adc_lock(spi);
  247. nxsig_usleep(1000);
  248. SPI_SELECT(spi, priv->devno, true);
  249. SPI_SEND(spi, ADS125X_WREG + 0x03); /* WRITE SPS REG */
  250. SPI_SEND(spi, 0x00); /* count=1 */
  251. SPI_SEND(spi, 0x63);
  252. SPI_SELECT(spi, priv->devno, false);
  253. adc_unlock(spi);
  254. }
  255. /****************************************************************************
  256. * Name: adc_setup
  257. *
  258. * Description:
  259. * Configure the ADC. This method is called the first time that the ADC
  260. * device is opened. This will occur when the port is first opened.
  261. * This setup includes configuring and attaching ADC interrupts. Interrupts
  262. * are all disabled upon return.
  263. *
  264. ****************************************************************************/
  265. static int adc_setup(FAR struct adc_dev_s *dev)
  266. {
  267. FAR struct ads1255_dev_s *priv = (FAR struct ads1255_dev_s *)dev->ad_priv;
  268. FAR struct spi_dev_s *spi;
  269. int ret;
  270. DEBUGASSERT(priv != NULL && priv->spi != NULL);
  271. spi = priv->spi;
  272. ret = irq_attach(priv->irq, adc_interrupt, NULL);
  273. if (ret == OK)
  274. {
  275. adc_lock(spi);
  276. SPI_SELECT(spi, priv->devno, true);
  277. SPI_SEND(spi, ADS125X_WREG); /* WRITE REG from 0 */
  278. SPI_SEND(spi, 0x03); /* count=4+1 */
  279. if (priv->buf)
  280. {
  281. SPI_SEND(spi, ADS125X_BUFON); /* REG0 STATUS BUFFER ON */
  282. }
  283. else
  284. {
  285. SPI_SEND(spi, ADS125X_BUFOFF);
  286. }
  287. SPI_SEND(spi, priv->mux[0]);
  288. SPI_SEND(spi, priv->pga); /* REG2 ADCON PGA=2 */
  289. SPI_SEND(spi, getspsreg(priv->sps));
  290. nxsig_usleep(1000);
  291. SPI_SEND(spi, ADS125X_SELFCAL);
  292. SPI_SELECT(spi, priv->devno, false);
  293. adc_unlock(spi);
  294. up_enable_irq(priv->irq);
  295. }
  296. return ret;
  297. }
  298. /****************************************************************************
  299. * Name: adc_shutdown
  300. *
  301. * Description:
  302. * Disable the ADC. This method is called when the ADC device is closed.
  303. * This method reverses the operation the setup method.
  304. *
  305. ****************************************************************************/
  306. static void adc_shutdown(FAR struct adc_dev_s *dev)
  307. {
  308. FAR struct ads1255_dev_s *priv = (FAR struct ads1255_dev_s *)dev->ad_priv;
  309. DEBUGASSERT(priv != NULL);
  310. up_disable_irq(priv->irq);
  311. irq_detach(priv->irq);
  312. }
  313. /****************************************************************************
  314. * Name: adc_rxint
  315. *
  316. * Description:
  317. * Call to enable or disable RX interrupts
  318. *
  319. ****************************************************************************/
  320. static void adc_rxint(FAR struct adc_dev_s *dev, bool enable)
  321. {
  322. FAR struct ads1255_dev_s *priv = (FAR struct ads1255_dev_s *)dev->ad_priv;
  323. DEBUGASSERT(priv != NULL);
  324. if (enable)
  325. {
  326. up_enable_irq(priv->irq);
  327. }
  328. else
  329. {
  330. up_disable_irq(priv->irq);
  331. }
  332. }
  333. /****************************************************************************
  334. * Name: adc_ioctl
  335. *
  336. * Description:
  337. * All ioctl calls will be routed through this method
  338. *
  339. ****************************************************************************/
  340. static int adc_ioctl(FAR struct adc_dev_s *dev, int cmd, unsigned long arg)
  341. {
  342. _err("ERROR: Fix me; Not Implemented\n");
  343. return 0;
  344. }
  345. /****************************************************************************
  346. * Name: adc_worker
  347. *
  348. * Description:
  349. * ADC interrupt work
  350. *
  351. ****************************************************************************/
  352. static void adc_worker(FAR void *arg)
  353. {
  354. FAR struct ads1255_dev_s *priv = (FAR struct ads1255_dev_s *)arg;
  355. FAR struct spi_dev_s *spi;
  356. unsigned char buf[4];
  357. unsigned char ch;
  358. DEBUGASSERT(priv != NULL && priv->spi != NULL);
  359. spi = priv->spi;
  360. /* REVISIT: Cannot perform SPI operations from an interrupt handler!
  361. * Need to use the high priority work queue.
  362. */
  363. adc_lock(spi);
  364. SPI_SELECT(spi, priv->devno, true);
  365. SPI_SEND(spi, ADS125X_RDATA);
  366. up_udelay(10);
  367. buf[3] = SPI_SEND(spi, 0xff);
  368. buf[2] = SPI_SEND(spi, 0xff);
  369. buf[1] = SPI_SEND(spi, 0xff);
  370. buf[0] = 0;
  371. priv->channel++;
  372. ch = priv->mux[priv->channel];
  373. if (ch == 0)
  374. {
  375. priv->channel = 0;
  376. ch = priv->mux[0];
  377. }
  378. SPI_SEND(spi, ADS125X_WREG + 0x01);
  379. SPI_SEND(spi, 0x00);
  380. SPI_SEND(spi, ch);
  381. SPI_SEND(spi, ADS125X_SYNC);
  382. up_udelay(2);
  383. SPI_SEND(spi, ADS125X_WAKEUP);
  384. SPI_SELECT(spi, priv->devno, false);
  385. adc_unlock(spi);
  386. /* Verify that the upper-half driver has bound its callback functions */
  387. if (priv->cb != NULL)
  388. {
  389. /* Perform the data received callback */
  390. DEBUGASSERT(priv->cb->au_receive != NULL);
  391. priv->cb->au_receive(&g_adcdev, priv->channel, *(int32_t *)buf);
  392. }
  393. /* Re-enable ADC interrupts */
  394. up_enable_irq(priv->irq);
  395. }
  396. /****************************************************************************
  397. * Name: adc_interrupt
  398. *
  399. * Description:
  400. * ADC interrupt handler
  401. *
  402. ****************************************************************************/
  403. static int adc_interrupt(int irq, void *context, FAR void *arg)
  404. {
  405. FAR struct ads1255_dev_s *priv = (FAR struct ads1255_dev_s *)g_adcdev.ad_priv;
  406. DEBUGASSERT(priv != NULL);
  407. /* Disable further ADC interrupts until the worker thread has executed. */
  408. up_disable_irq(priv->irq);
  409. /* Schedule the ADC work for the worker thread. Whent he sample has been
  410. * processed, the ADC interrupt will be re-enabled.
  411. */
  412. DEBUGVERIFY(work_queue(HPWORK, &priv->work, adc_worker, priv, 0));
  413. return OK;
  414. }
  415. /****************************************************************************
  416. * Public Functions
  417. ****************************************************************************/
  418. /****************************************************************************
  419. * Name: up_ads1255initialize
  420. *
  421. * Description:
  422. * Initialize the selected adc port
  423. *
  424. * Input Parameters:
  425. * Port number (for hardware that has multiple adc interfaces)
  426. *
  427. * Returned Value:
  428. * Valid can device structure reference on success; a NULL on failure
  429. *
  430. ****************************************************************************/
  431. FAR struct adc_dev_s *up_ads1255initialize(FAR struct spi_dev_s *spi,
  432. unsigned int devno)
  433. {
  434. FAR struct ads1255_dev_s *priv = (FAR struct ads1255_dev_s *)g_adcdev.ad_priv;
  435. DEBUGASSERT(spi != NULL);
  436. /* Driver state data */
  437. priv->cb = NULL;
  438. priv->spi = spi;
  439. priv->devno = devno;
  440. return &g_adcdev;
  441. }
  442. #endif