adc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /****************************************************************************
  2. * drivers/analog/adc.c
  3. *
  4. * Copyright (C) 2008-2009, 2016-2017 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. * Derived from drivers/can.c
  10. *
  11. * Copyright (C) 2008-2009 Gregory Nutt. All rights reserved.
  12. * Author: Gregory Nutt <gnutt@nuttx.org>
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in
  22. * the documentation and/or other materials provided with the
  23. * distribution.
  24. * 3. Neither the name NuttX nor the names of its contributors may be
  25. * used to endorse or promote products derived from this software
  26. * without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  29. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  30. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  31. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  32. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  33. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  34. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  35. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  36. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  37. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  38. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  39. * POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. ****************************************************************************/
  42. /****************************************************************************
  43. * Included Files
  44. ****************************************************************************/
  45. #include <nuttx/config.h>
  46. #include <sys/types.h>
  47. #include <stdint.h>
  48. #include <stdbool.h>
  49. #include <unistd.h>
  50. #include <string.h>
  51. #include <semaphore.h>
  52. #include <fcntl.h>
  53. #include <errno.h>
  54. #include <debug.h>
  55. #include <nuttx/fs/fs.h>
  56. #include <nuttx/arch.h>
  57. #include <nuttx/semaphore.h>
  58. #include <nuttx/analog/adc.h>
  59. #include <nuttx/random.h>
  60. #include <nuttx/irq.h>
  61. /****************************************************************************
  62. * Private Function Prototypes
  63. ****************************************************************************/
  64. static int adc_open(FAR struct file *filep);
  65. static int adc_close(FAR struct file *filep);
  66. static ssize_t adc_read(FAR struct file *fielp, FAR char *buffer,
  67. size_t buflen);
  68. static int adc_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
  69. static int adc_receive(FAR struct adc_dev_s *dev, uint8_t ch,
  70. int32_t data);
  71. static void adc_notify(FAR struct adc_dev_s *dev);
  72. static int adc_poll(FAR struct file *filep, struct pollfd *fds, bool setup);
  73. /****************************************************************************
  74. * Private Data
  75. ****************************************************************************/
  76. static const struct file_operations g_adc_fops =
  77. {
  78. adc_open, /* open */
  79. adc_close, /* close */
  80. adc_read, /* read */
  81. 0, /* write */
  82. 0, /* seek */
  83. adc_ioctl, /* ioctl */
  84. adc_poll /* poll */
  85. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  86. , NULL /* unlink */
  87. #endif
  88. };
  89. static const struct adc_callback_s g_adc_callback =
  90. {
  91. adc_receive /* au_receive */
  92. };
  93. /****************************************************************************
  94. * Private Functions
  95. ****************************************************************************/
  96. /************************************************************************************
  97. * Name: adc_open
  98. *
  99. * Description:
  100. * This function is called whenever the ADC device is opened.
  101. *
  102. ************************************************************************************/
  103. static int adc_open(FAR struct file *filep)
  104. {
  105. FAR struct inode *inode = filep->f_inode;
  106. FAR struct adc_dev_s *dev = inode->i_private;
  107. uint8_t tmp;
  108. int ret;
  109. /* If the port is the middle of closing, wait until the close is finished */
  110. ret = nxsem_wait(&dev->ad_closesem);
  111. if (ret >= 0)
  112. {
  113. /* Increment the count of references to the device. If this the first
  114. * time that the driver has been opened for this device, then initialize
  115. * the device.
  116. */
  117. tmp = dev->ad_ocount + 1;
  118. if (tmp == 0)
  119. {
  120. /* More than 255 opens; uint8_t overflows to zero */
  121. ret = -EMFILE;
  122. }
  123. else
  124. {
  125. /* Check if this is the first time that the driver has been opened. */
  126. if (tmp == 1)
  127. {
  128. /* Yes.. perform one time hardware initialization. */
  129. irqstate_t flags = enter_critical_section();
  130. ret = dev->ad_ops->ao_setup(dev);
  131. if (ret == OK)
  132. {
  133. /* Mark the FIFOs empty */
  134. dev->ad_recv.af_head = 0;
  135. dev->ad_recv.af_tail = 0;
  136. /* Finally, Enable the ADC RX interrupt */
  137. dev->ad_ops->ao_rxint(dev, true);
  138. /* Save the new open count on success */
  139. dev->ad_ocount = tmp;
  140. }
  141. leave_critical_section(flags);
  142. }
  143. }
  144. nxsem_post(&dev->ad_closesem);
  145. }
  146. return ret;
  147. }
  148. /************************************************************************************
  149. * Name: adc_close
  150. *
  151. * Description:
  152. * This routine is called when the ADC device is closed.
  153. * It waits for the last remaining data to be sent.
  154. *
  155. ************************************************************************************/
  156. static int adc_close(FAR struct file *filep)
  157. {
  158. FAR struct inode *inode = filep->f_inode;
  159. FAR struct adc_dev_s *dev = inode->i_private;
  160. irqstate_t flags;
  161. int ret;
  162. ret = nxsem_wait(&dev->ad_closesem);
  163. if (ret >= 0)
  164. {
  165. /* Decrement the references to the driver. If the reference count will
  166. * decrement to 0, then uninitialize the driver.
  167. */
  168. if (dev->ad_ocount > 1)
  169. {
  170. dev->ad_ocount--;
  171. nxsem_post(&dev->ad_closesem);
  172. }
  173. else
  174. {
  175. /* There are no more references to the port */
  176. dev->ad_ocount = 0;
  177. /* Free the IRQ and disable the ADC device */
  178. flags = enter_critical_section(); /* Disable interrupts */
  179. dev->ad_ops->ao_shutdown(dev); /* Disable the ADC */
  180. leave_critical_section(flags);
  181. nxsem_post(&dev->ad_closesem);
  182. }
  183. }
  184. return ret;
  185. }
  186. /****************************************************************************
  187. * Name: adc_read
  188. ****************************************************************************/
  189. static ssize_t adc_read(FAR struct file *filep, FAR char *buffer, size_t buflen)
  190. {
  191. FAR struct inode *inode = filep->f_inode;
  192. FAR struct adc_dev_s *dev = inode->i_private;
  193. size_t nread;
  194. irqstate_t flags;
  195. int ret = 0;
  196. int msglen;
  197. ainfo("buflen: %d\n", (int)buflen);
  198. /* Determine size of the messages to return.
  199. *
  200. * REVISIT: What if buflen is 8 does that mean 4 messages of size 2? Or
  201. * 2 messages of size 4? What if buflen is 12. Does that mean 3 at size
  202. * 4? Or 4 at size 3? The form of the return data should probably really
  203. * be specified via IOCTL.
  204. */
  205. if (buflen % 5 == 0)
  206. {
  207. msglen = 5;
  208. }
  209. else if (buflen % 4 == 0)
  210. {
  211. msglen = 4;
  212. }
  213. else if (buflen % 3 == 0)
  214. {
  215. msglen = 3;
  216. }
  217. else if (buflen % 2 == 0)
  218. {
  219. msglen = 2;
  220. }
  221. else if (buflen == 1)
  222. {
  223. msglen = 1;
  224. }
  225. else
  226. {
  227. msglen = 5;
  228. }
  229. if (buflen >= msglen)
  230. {
  231. /* Interrupts must be disabled while accessing the ad_recv FIFO */
  232. flags = enter_critical_section();
  233. while (dev->ad_recv.af_head == dev->ad_recv.af_tail)
  234. {
  235. /* The receive FIFO is empty -- was non-blocking mode selected? */
  236. if (filep->f_oflags & O_NONBLOCK)
  237. {
  238. ret = -EAGAIN;
  239. goto return_with_irqdisabled;
  240. }
  241. /* Wait for a message to be received */
  242. dev->ad_nrxwaiters++;
  243. ret = nxsem_wait(&dev->ad_recv.af_sem);
  244. dev->ad_nrxwaiters--;
  245. if (ret < 0)
  246. {
  247. goto return_with_irqdisabled;
  248. }
  249. }
  250. /* The ad_recv FIFO is not empty. Copy all buffered data that will fit
  251. * in the user buffer.
  252. */
  253. nread = 0;
  254. do
  255. {
  256. FAR struct adc_msg_s *msg = &dev->ad_recv.af_buffer[dev->ad_recv.af_head];
  257. /* Will the next message in the FIFO fit into the user buffer? */
  258. if (nread + msglen > buflen)
  259. {
  260. /* No.. break out of the loop now with nread equal to the actual
  261. * number of bytes transferred.
  262. */
  263. break;
  264. }
  265. /* Feed ADC data to entropy pool */
  266. add_sensor_randomness(msg->am_data);
  267. /* Copy the message to the user buffer */
  268. if (msglen == 1)
  269. {
  270. /* Only one channel, return MS 8-bits of the sample*/
  271. buffer[nread] = msg->am_data >> 24;
  272. }
  273. else if (msglen == 2)
  274. {
  275. /* Only one channel, return only the MS 16-bits of the sample.*/
  276. int16_t data16 = msg->am_data >> 16;
  277. memcpy(&buffer[nread], &data16, 2);
  278. }
  279. else if (msglen == 3)
  280. {
  281. int16_t data16;
  282. /* Return the channel and the MS 16-bits of the sample. */
  283. buffer[nread] = msg->am_channel;
  284. data16 = msg->am_data >> 16;
  285. memcpy(&buffer[nread + 1], &data16, 2);
  286. }
  287. else if (msglen == 4)
  288. {
  289. int32_t data24;
  290. #ifdef CONFIG_ENDIAN_BIG
  291. /* In the big endian case, we simply copy the MS three bytes
  292. * which are indices: 0-2.
  293. */
  294. data24 = msg->am_data;
  295. #else
  296. /* In the little endian case, indices 0-2 correspond to the
  297. * the three LS bytes.
  298. */
  299. data24 = msg->am_data >> 8;
  300. #endif
  301. /* Return the channel and the most significant 24-bits */
  302. buffer[nread] = msg->am_channel;
  303. memcpy(&buffer[nread + 1], &data24, 3);
  304. }
  305. else
  306. {
  307. /* Return the channel and all four bytes of the sample */
  308. buffer[nread] = msg->am_channel;
  309. memcpy(&buffer[nread + 1], &msg->am_data, 4);
  310. }
  311. nread += msglen;
  312. /* Increment the head of the circular message buffer */
  313. if (++dev->ad_recv.af_head >= CONFIG_ADC_FIFOSIZE)
  314. {
  315. dev->ad_recv.af_head = 0;
  316. }
  317. }
  318. while (dev->ad_recv.af_head != dev->ad_recv.af_tail);
  319. /* All on the messages have bee transferred. Return the number of bytes
  320. * that were read.
  321. */
  322. ret = nread;
  323. return_with_irqdisabled:
  324. leave_critical_section(flags);
  325. }
  326. ainfo("Returning: %d\n", ret);
  327. return ret;
  328. }
  329. /************************************************************************************
  330. * Name: adc_ioctl
  331. ************************************************************************************/
  332. static int adc_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
  333. {
  334. FAR struct inode *inode = filep->f_inode;
  335. FAR struct adc_dev_s *dev = inode->i_private;
  336. int ret;
  337. ret = dev->ad_ops->ao_ioctl(dev, cmd, arg);
  338. return ret;
  339. }
  340. /****************************************************************************
  341. * Name: adc_receive
  342. ****************************************************************************/
  343. static int adc_receive(FAR struct adc_dev_s *dev, uint8_t ch, int32_t data)
  344. {
  345. FAR struct adc_fifo_s *fifo = &dev->ad_recv;
  346. int nexttail;
  347. int errcode = -ENOMEM;
  348. /* Check if adding this new message would over-run the drivers ability to enqueue
  349. * read data.
  350. */
  351. nexttail = fifo->af_tail + 1;
  352. if (nexttail >= CONFIG_ADC_FIFOSIZE)
  353. {
  354. nexttail = 0;
  355. }
  356. /* Refuse the new data if the FIFO is full */
  357. if (nexttail != fifo->af_head)
  358. {
  359. /* Add the new, decoded ADC sample at the tail of the FIFO */
  360. fifo->af_buffer[fifo->af_tail].am_channel = ch;
  361. fifo->af_buffer[fifo->af_tail].am_data = data;
  362. /* Increment the tail of the circular buffer */
  363. fifo->af_tail = nexttail;
  364. adc_notify(dev);
  365. errcode = OK;
  366. }
  367. return errcode;
  368. }
  369. /****************************************************************************
  370. * Name: adc_pollnotify
  371. ****************************************************************************/
  372. static void adc_pollnotify(FAR struct adc_dev_s *dev, uint32_t type)
  373. {
  374. int i;
  375. for (i = 0; i < CONFIG_ADC_NPOLLWAITERS; i++)
  376. {
  377. struct pollfd *fds = dev->fds[i];
  378. if (fds)
  379. {
  380. fds->revents |= type;
  381. nxsem_post(fds->sem);
  382. }
  383. }
  384. }
  385. /****************************************************************************
  386. * Name: adc_notify
  387. ****************************************************************************/
  388. static void adc_notify(FAR struct adc_dev_s *dev)
  389. {
  390. FAR struct adc_fifo_s *fifo = &dev->ad_recv;
  391. /* If there are threads waiting for read data, then signal one of them
  392. * that the read data is available.
  393. */
  394. if (dev->ad_nrxwaiters > 0)
  395. {
  396. nxsem_post(&fifo->af_sem);
  397. }
  398. /* If there are threads waiting on poll() for data to become available,
  399. * then wake them up now.
  400. */
  401. adc_pollnotify(dev, POLLIN);
  402. }
  403. /************************************************************************************
  404. * Name: adc_poll
  405. ************************************************************************************/
  406. static int adc_poll(FAR struct file *filep, struct pollfd *fds, bool setup)
  407. {
  408. FAR struct inode *inode = filep->f_inode;
  409. FAR struct adc_dev_s *dev = inode->i_private;
  410. irqstate_t flags;
  411. int ret = 0;
  412. int i;
  413. /* Interrupts must be disabled while accessing the list of poll structures
  414. * and ad_recv FIFO.
  415. */
  416. flags = enter_critical_section();
  417. if (setup)
  418. {
  419. /* Ignore waits that do not include POLLIN */
  420. if ((fds->events & POLLIN) == 0)
  421. {
  422. ret = -EDEADLK;
  423. goto return_with_irqdisabled;
  424. }
  425. /* This is a request to set up the poll. Find an available
  426. * slot for the poll structure reference
  427. */
  428. for (i = 0; i < CONFIG_ADC_NPOLLWAITERS; i++)
  429. {
  430. /* Find an available slot */
  431. if (!dev->fds[i])
  432. {
  433. /* Bind the poll structure and this slot */
  434. dev->fds[i] = fds;
  435. fds->priv = &dev->fds[i];
  436. break;
  437. }
  438. }
  439. if (i >= CONFIG_ADC_NPOLLWAITERS)
  440. {
  441. fds->priv = NULL;
  442. ret = -EBUSY;
  443. goto return_with_irqdisabled;
  444. }
  445. /* Should we immediately notify on any of the requested events? */
  446. if (dev->ad_recv.af_head != dev->ad_recv.af_tail)
  447. {
  448. adc_pollnotify(dev, POLLIN);
  449. }
  450. }
  451. else if (fds->priv)
  452. {
  453. /* This is a request to tear down the poll. */
  454. struct pollfd **slot = (struct pollfd **)fds->priv;
  455. /* Remove all memory of the poll setup */
  456. *slot = NULL;
  457. fds->priv = NULL;
  458. }
  459. return_with_irqdisabled:
  460. leave_critical_section(flags);
  461. return ret;
  462. }
  463. /****************************************************************************
  464. * Public Functions
  465. ****************************************************************************/
  466. /****************************************************************************
  467. * Name: adc_register
  468. ****************************************************************************/
  469. int adc_register(FAR const char *path, FAR struct adc_dev_s *dev)
  470. {
  471. int ret;
  472. DEBUGASSERT(path != NULL && dev != NULL);
  473. /* Bind the upper-half callbacks to the lower half ADC driver */
  474. DEBUGASSERT(dev->ad_ops != NULL && dev->ad_ops->ao_bind != NULL);
  475. ret = dev->ad_ops->ao_bind(dev, &g_adc_callback);
  476. if (ret < 0)
  477. {
  478. aerr("ERROR: Failed to bind callbacks: %d\n", ret);
  479. return ret;
  480. }
  481. /* Initialize the ADC device structure */
  482. dev->ad_ocount = 0;
  483. /* Initialize semaphores */
  484. nxsem_init(&dev->ad_recv.af_sem, 0, 0);
  485. nxsem_init(&dev->ad_closesem, 0, 1);
  486. /* The receive semaphore is used for signaling and, hence, should not have
  487. * priority inheritance enabled.
  488. */
  489. nxsem_setprotocol(&dev->ad_recv.af_sem, SEM_PRIO_NONE);
  490. /* Reset the ADC hardware */
  491. DEBUGASSERT(dev->ad_ops->ao_reset != NULL);
  492. dev->ad_ops->ao_reset(dev);
  493. /* Register the ADC character driver */
  494. ret = register_driver(path, &g_adc_fops, 0444, dev);
  495. if (ret < 0)
  496. {
  497. nxsem_destroy(&dev->ad_recv.af_sem);
  498. nxsem_destroy(&dev->ad_closesem);
  499. }
  500. return ret;
  501. }