comp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /****************************************************************************
  2. * drivers/analog/comp.c
  3. *
  4. * Copyright (C) 2017 Gregory Nutt. All rights reserved.
  5. * Author: Mateusz Szafoni <raiden00@railab.me>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <stdint.h>
  41. #include <unistd.h>
  42. #include <semaphore.h>
  43. #include <fcntl.h>
  44. #include <errno.h>
  45. #include <debug.h>
  46. #include <poll.h>
  47. #include <nuttx/arch.h>
  48. #include <nuttx/semaphore.h>
  49. #include <nuttx/fs/fs.h>
  50. #include <nuttx/analog/comp.h>
  51. #include <nuttx/irq.h>
  52. /****************************************************************************
  53. * Private Function Prototypes
  54. ****************************************************************************/
  55. static int comp_open(FAR struct file *filep);
  56. static int comp_close(FAR struct file *filep);
  57. static ssize_t comp_read(FAR struct file *filep, FAR char *buffer,
  58. size_t buflen);
  59. static int comp_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
  60. static int comp_poll(FAR struct file *filep, FAR struct pollfd *fds,
  61. bool setup);
  62. static int comp_notify(FAR struct comp_dev_s *dev, uint8_t val);
  63. /****************************************************************************
  64. * Private Data
  65. ****************************************************************************/
  66. static const struct file_operations comp_fops =
  67. {
  68. comp_open, /* open */
  69. comp_close, /* close */
  70. comp_read, /* read */
  71. NULL, /* write */
  72. NULL, /* seek */
  73. comp_ioctl, /* ioctl */
  74. comp_poll /* poll */
  75. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  76. , NULL /* unlink */
  77. #endif
  78. };
  79. static const struct comp_callback_s g_comp_callback =
  80. {
  81. comp_notify /* au_notify */
  82. };
  83. /****************************************************************************
  84. * Private Functions
  85. ****************************************************************************/
  86. /****************************************************************************
  87. * Name: comp_pollnotify
  88. *
  89. * Description:
  90. * This function is called to notificy any waiters of poll-reated events.
  91. *
  92. ****************************************************************************/
  93. static void comp_pollnotify(FAR struct comp_dev_s *dev,
  94. pollevent_t eventset)
  95. {
  96. int i;
  97. if (eventset & POLLERR)
  98. {
  99. eventset &= ~(POLLOUT | POLLIN);
  100. }
  101. for (i = 0; i < CONFIG_DEV_COMP_NPOLLWAITERS; i++)
  102. {
  103. FAR struct pollfd *fds = dev->d_fds[i];
  104. if (fds)
  105. {
  106. fds->revents |= eventset & (fds->events | POLLERR | POLLHUP);
  107. if ((fds->revents & (POLLOUT | POLLHUP)) == (POLLOUT | POLLHUP))
  108. {
  109. /* POLLOUT and POLLHUP are mutually exclusive. */
  110. fds->revents &= ~POLLOUT;
  111. }
  112. if (fds->revents != 0)
  113. {
  114. ainfo("Report events: %02x\n", fds->revents);
  115. nxsem_post(fds->sem);
  116. }
  117. }
  118. }
  119. }
  120. /****************************************************************************
  121. * Name: comp_semtake
  122. ****************************************************************************/
  123. static void comp_semtake(FAR sem_t *sem)
  124. {
  125. int ret;
  126. do
  127. {
  128. /* Take the semaphore (perhaps waiting) */
  129. ret = nxsem_wait(sem);
  130. /* The only case that an error should occur here is if the wait was
  131. * awakened by a signal.
  132. */
  133. DEBUGASSERT(ret == OK || ret == -EINTR);
  134. }
  135. while (ret == -EINTR);
  136. }
  137. /****************************************************************************
  138. * Name: comp_poll
  139. ****************************************************************************/
  140. static int comp_poll(FAR struct file *filep, FAR struct pollfd *fds,
  141. bool setup)
  142. {
  143. FAR struct inode *inode = filep->f_inode;
  144. FAR struct comp_dev_s *dev = inode->i_private;
  145. int ret = OK;
  146. int i;
  147. DEBUGASSERT(dev && fds);
  148. /* Are we setting up the poll? Or tearing it down? */
  149. comp_semtake(&dev->ad_sem);
  150. if (setup)
  151. {
  152. /* This is a request to set up the poll. Find an available
  153. * slot for the poll structure reference
  154. */
  155. for (i = 0; i < CONFIG_DEV_COMP_NPOLLWAITERS; i++)
  156. {
  157. /* Find an available slot */
  158. if (!dev->d_fds[i])
  159. {
  160. /* Bind the poll structure and this slot */
  161. dev->d_fds[i] = fds;
  162. fds->priv = &dev->d_fds[i];
  163. break;
  164. }
  165. }
  166. if (i >= CONFIG_DEV_COMP_NPOLLWAITERS)
  167. {
  168. fds->priv = NULL;
  169. ret = -EBUSY;
  170. goto errout;
  171. }
  172. }
  173. else
  174. {
  175. /* This is a request to tear down the poll. */
  176. FAR struct pollfd **slot = (FAR struct pollfd **)fds->priv;
  177. #ifdef CONFIG_DEBUG_FEATURES
  178. if (!slot)
  179. {
  180. ret = -EIO;
  181. goto errout;
  182. }
  183. #endif
  184. /* Remove all memory of the poll setup */
  185. *slot = NULL;
  186. fds->priv = NULL;
  187. }
  188. errout:
  189. nxsem_post(&dev->ad_sem);
  190. return ret;
  191. }
  192. /****************************************************************************
  193. * Name: comp_notify
  194. *
  195. * Description:
  196. * This function is called from the lower half driver to notify
  197. * the change of the comparator output.
  198. *
  199. ****************************************************************************/
  200. static int comp_notify(FAR struct comp_dev_s *dev, uint8_t val)
  201. {
  202. /* TODO: store values in FIFO? */
  203. dev->val = val;
  204. comp_pollnotify(dev, POLLIN);
  205. nxsem_post(&dev->ad_readsem);
  206. return 0;
  207. }
  208. /****************************************************************************
  209. * Name: comp_open
  210. *
  211. * Description:
  212. * This function is called whenever the COMP device is opened.
  213. *
  214. ****************************************************************************/
  215. static int comp_open(FAR struct file *filep)
  216. {
  217. FAR struct inode *inode = filep->f_inode;
  218. FAR struct comp_dev_s *dev = inode->i_private;
  219. uint8_t tmp;
  220. int ret;
  221. /* If the port is the middle of closing, wait until the close is finished */
  222. ret = nxsem_wait(&dev->ad_sem);
  223. if (ret >= 0)
  224. {
  225. /* Increment the count of references to the device. If this the first
  226. * time that the driver has been opened for this device, then initialize
  227. * the device.
  228. */
  229. tmp = dev->ad_ocount + 1;
  230. if (tmp == 0)
  231. {
  232. /* More than 255 opens; uint8_t overflows to zero */
  233. ret = -EMFILE;
  234. }
  235. else
  236. {
  237. /* Check if this is the first time that the driver has been opened. */
  238. if (tmp == 1)
  239. {
  240. /* Yes.. perform one time hardware initialization. */
  241. irqstate_t flags = enter_critical_section();
  242. ret = dev->ad_ops->ao_setup(dev);
  243. if (ret == OK)
  244. {
  245. /* Save the new open count on success */
  246. dev->ad_ocount = tmp;
  247. }
  248. leave_critical_section(flags);
  249. }
  250. }
  251. nxsem_post(&dev->ad_sem);
  252. }
  253. return ret;
  254. }
  255. /****************************************************************************
  256. * Name: comp_close
  257. *
  258. * Description:
  259. * This routine is called when the COMP device is closed.
  260. * It waits for the last remaining data to be sent.
  261. *
  262. ****************************************************************************/
  263. static int comp_close(FAR struct file *filep)
  264. {
  265. FAR struct inode *inode = filep->f_inode;
  266. FAR struct comp_dev_s *dev = inode->i_private;
  267. irqstate_t flags;
  268. int ret;
  269. ret = nxsem_wait(&dev->ad_sem);
  270. if (ret >= 0)
  271. {
  272. /* Decrement the references to the driver. If the reference count will
  273. * decrement to 0, then uninitialize the driver.
  274. */
  275. if (dev->ad_ocount > 1)
  276. {
  277. dev->ad_ocount--;
  278. nxsem_post(&dev->ad_sem);
  279. }
  280. else
  281. {
  282. /* There are no more references to the port */
  283. dev->ad_ocount = 0;
  284. /* Free the IRQ and disable the COMP device */
  285. flags = enter_critical_section(); /* Disable interrupts */
  286. dev->ad_ops->ao_shutdown(dev); /* Disable the COMP */
  287. leave_critical_section(flags);
  288. nxsem_post(&dev->ad_sem);
  289. }
  290. }
  291. return ret;
  292. }
  293. /****************************************************************************
  294. * Name: comp_read
  295. ****************************************************************************/
  296. static ssize_t comp_read(FAR struct file *filep, FAR char *buffer, size_t buflen)
  297. {
  298. FAR struct inode *inode = filep->f_inode;
  299. FAR struct comp_dev_s *dev = inode->i_private;
  300. int ret;
  301. /* If non-blocking read, read the value immediately and return. */
  302. if (filep->f_oflags & O_NONBLOCK)
  303. {
  304. ret = dev->ad_ops->ao_read(dev);
  305. buffer[0] = (uint8_t)ret;
  306. return 1;
  307. }
  308. ret = nxsem_wait(&dev->ad_readsem);
  309. if (ret < 0)
  310. {
  311. aerr("nxsem_wait() failed: %d\n", ret);
  312. return ret;
  313. }
  314. buffer[0] = dev->val;
  315. return 1;
  316. }
  317. /****************************************************************************
  318. * Name: comp_ioctl
  319. ****************************************************************************/
  320. static int comp_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
  321. {
  322. FAR struct inode *inode = filep->f_inode;
  323. FAR struct comp_dev_s *dev = inode->i_private;
  324. int ret;
  325. ret = dev->ad_ops->ao_ioctl(dev, cmd, arg);
  326. return ret;
  327. }
  328. /****************************************************************************
  329. * Public Functions
  330. ****************************************************************************/
  331. /****************************************************************************
  332. * Name: comp_register
  333. ****************************************************************************/
  334. int comp_register(FAR const char *path, FAR struct comp_dev_s *dev)
  335. {
  336. int ret;
  337. /* Initialize the COMP device structure */
  338. dev->ad_ocount = 0;
  339. /* Initialize semaphores */
  340. nxsem_init(&dev->ad_sem, 0, 1);
  341. (void)nxsem_setprotocol(&dev->ad_sem, SEM_PRIO_NONE);
  342. nxsem_init(&dev->ad_readsem, 0, 0);
  343. (void)nxsem_setprotocol(&dev->ad_readsem, SEM_PRIO_NONE);
  344. /* Bind the upper-half callbacks to the lower half COMP driver */
  345. DEBUGASSERT(dev->ad_ops != NULL);
  346. if (dev->ad_ops->ao_bind != NULL)
  347. {
  348. ret = dev->ad_ops->ao_bind(dev, &g_comp_callback);
  349. if (ret < 0)
  350. {
  351. aerr("ERROR: Failed to bind callbacks: %d\n", ret);
  352. return ret;
  353. }
  354. }
  355. /* Register the COMP character driver */
  356. ret = register_driver(path, &comp_fops, 0444, dev);
  357. if (ret < 0)
  358. {
  359. nxsem_destroy(&dev->ad_sem);
  360. }
  361. return ret;
  362. }