oneshot.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /****************************************************************************
  2. * drivers/timers/oneshot.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <sys/types.h>
  25. #include <stdint.h>
  26. #include <stdbool.h>
  27. #include <fcntl.h>
  28. #include <assert.h>
  29. #include <debug.h>
  30. #include <nuttx/kmalloc.h>
  31. #include <nuttx/signal.h>
  32. #include <nuttx/fs/fs.h>
  33. #include <nuttx/semaphore.h>
  34. #include <nuttx/timers/oneshot.h>
  35. #ifdef CONFIG_ONESHOT
  36. /****************************************************************************
  37. * Private Type Definitions
  38. ****************************************************************************/
  39. /* This structure describes the state of the upper half driver */
  40. struct oneshot_dev_s
  41. {
  42. FAR struct oneshot_lowerhalf_s *od_lower; /* Lower-half driver state */
  43. sem_t od_exclsem; /* Supports mutual exclusion */
  44. /* Oneshot timer expiration notification information */
  45. struct sigevent od_event; /* Signal info */
  46. struct sigwork_s od_work; /* Signal work */
  47. pid_t od_pid; /* PID to be notified */
  48. };
  49. /****************************************************************************
  50. * Private Function Prototypes
  51. ****************************************************************************/
  52. static int oneshot_open(FAR struct file *filep);
  53. static int oneshot_close(FAR struct file *filep);
  54. static ssize_t oneshot_read(FAR struct file *filep, FAR char *buffer,
  55. size_t buflen);
  56. static ssize_t oneshot_write(FAR struct file *filep, FAR const char *buffer,
  57. size_t buflen);
  58. static int oneshot_ioctl(FAR struct file *filep, int cmd,
  59. unsigned long arg);
  60. static void oneshot_callback(FAR struct oneshot_lowerhalf_s *lower,
  61. FAR void *arg);
  62. /****************************************************************************
  63. * Private Data
  64. ****************************************************************************/
  65. static const struct file_operations g_oneshot_ops =
  66. {
  67. oneshot_open, /* open */
  68. oneshot_close, /* close */
  69. oneshot_read, /* read */
  70. oneshot_write, /* write */
  71. NULL, /* seek */
  72. oneshot_ioctl, /* ioctl */
  73. NULL /* poll */
  74. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  75. , NULL /* unlink */
  76. #endif
  77. };
  78. /****************************************************************************
  79. * Private Functions
  80. ****************************************************************************/
  81. /****************************************************************************
  82. * Name: oneshot_callback
  83. ****************************************************************************/
  84. static void oneshot_callback(FAR struct oneshot_lowerhalf_s *lower,
  85. FAR void *arg)
  86. {
  87. FAR struct oneshot_dev_s *priv = (FAR struct oneshot_dev_s *)arg;
  88. DEBUGASSERT(priv != NULL);
  89. /* Signal the waiter.. if there is one */
  90. nxsig_notification(priv->od_pid, &priv->od_event,
  91. SI_QUEUE, &priv->od_work);
  92. }
  93. /****************************************************************************
  94. * Name: oneshot_open
  95. *
  96. * Description:
  97. * This function is called whenever the PWM device is opened.
  98. *
  99. ****************************************************************************/
  100. static int oneshot_open(FAR struct file *filep)
  101. {
  102. tmrinfo("Opening...\n");
  103. DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
  104. return OK;
  105. }
  106. /****************************************************************************
  107. * Name: oneshot_close
  108. *
  109. * Description:
  110. * This function is called when the PWM device is closed.
  111. *
  112. ****************************************************************************/
  113. static int oneshot_close(FAR struct file *filep)
  114. {
  115. tmrinfo("Closing...\n");
  116. DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
  117. return OK;
  118. }
  119. /****************************************************************************
  120. * Name: oneshot_read
  121. *
  122. * Description:
  123. * A dummy read method. This is provided only to satsify the VFS layer.
  124. *
  125. ****************************************************************************/
  126. static ssize_t oneshot_read(FAR struct file *filep, FAR char *buffer,
  127. size_t buflen)
  128. {
  129. /* Return zero -- usually meaning end-of-file */
  130. tmrinfo("buflen=%ld\n", (unsigned long)buflen);
  131. DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
  132. return 0;
  133. }
  134. /****************************************************************************
  135. * Name: oneshot_write
  136. *
  137. * Description:
  138. * A dummy write method. This is provided only to satsify the VFS layer.
  139. *
  140. ****************************************************************************/
  141. static ssize_t oneshot_write(FAR struct file *filep, FAR const char *buffer,
  142. size_t buflen)
  143. {
  144. /* Return a failure */
  145. tmrinfo("buflen=%ld\n", (unsigned long)buflen);
  146. DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
  147. return -EPERM;
  148. }
  149. /****************************************************************************
  150. * Name: oneshot_ioctl
  151. *
  152. * Description:
  153. * The standard ioctl method. This is where ALL of the PWM work is done.
  154. *
  155. ****************************************************************************/
  156. static int oneshot_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
  157. {
  158. FAR struct inode *inode;
  159. FAR struct oneshot_dev_s *priv;
  160. int ret;
  161. tmrinfo("cmd=%d arg=%08lx\n", cmd, (unsigned long)arg);
  162. DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
  163. inode = filep->f_inode;
  164. priv = (FAR struct oneshot_dev_s *)inode->i_private;
  165. DEBUGASSERT(priv != NULL);
  166. /* Get exclusive access to the device structures */
  167. ret = nxsem_wait(&priv->od_exclsem);
  168. if (ret < 0)
  169. {
  170. return ret;
  171. }
  172. /* Handle oneshot timer ioctl commands */
  173. switch (cmd)
  174. {
  175. /* OSIOC_MAXDELAY - Return the maximum delay that can be supported
  176. * by this timer.
  177. * Argument: A reference to a struct timespec in
  178. * which the maximum time will be returned.
  179. */
  180. case OSIOC_MAXDELAY:
  181. {
  182. FAR struct timespec *ts = (FAR struct timespec *)((uintptr_t)arg);
  183. DEBUGASSERT(ts != NULL);
  184. ret = ONESHOT_MAX_DELAY(priv->od_lower, ts);
  185. }
  186. break;
  187. /* OSIOC_START - Start the oneshot timer
  188. * Argument: A reference to struct oneshot_start_s
  189. */
  190. case OSIOC_START:
  191. {
  192. FAR struct oneshot_start_s *start;
  193. pid_t pid;
  194. start = (FAR struct oneshot_start_s *)((uintptr_t)arg);
  195. DEBUGASSERT(start != NULL);
  196. /* Save signaling information */
  197. priv->od_event = start->event;
  198. pid = start->pid;
  199. if (pid == 0)
  200. {
  201. pid = getpid();
  202. }
  203. priv->od_pid = pid;
  204. /* Start the oneshot timer */
  205. ret = ONESHOT_START(priv->od_lower, oneshot_callback, priv,
  206. &start->ts);
  207. }
  208. break;
  209. /* OSIOC_CANCEL - Stop the timer
  210. * Argument: A reference to a struct timespec in
  211. * which the time remaining will be returned.
  212. */
  213. case OSIOC_CANCEL:
  214. {
  215. FAR struct timespec *ts = (FAR struct timespec *)((uintptr_t)arg);
  216. /* Cancel the oneshot timer */
  217. ret = ONESHOT_CANCEL(priv->od_lower, ts);
  218. nxsig_cancel_notification(&priv->od_work);
  219. }
  220. break;
  221. /* OSIOC_CURRENT - Get the current time
  222. * Argument: A reference to a struct timespec in
  223. * which the current time will be returned.
  224. */
  225. case OSIOC_CURRENT:
  226. {
  227. FAR struct timespec *ts = (FAR struct timespec *)((uintptr_t)arg);
  228. /* Get the current time */
  229. ret = ONESHOT_CURRENT(priv->od_lower, ts);
  230. }
  231. break;
  232. default:
  233. {
  234. tmrerr("ERROR: Unrecognized cmd: %d arg: %ld\n", cmd, arg);
  235. ret = -ENOTTY;
  236. }
  237. break;
  238. }
  239. nxsem_post(&priv->od_exclsem);
  240. return ret;
  241. }
  242. /****************************************************************************
  243. * Public Functions
  244. ****************************************************************************/
  245. /****************************************************************************
  246. * Name: oneshot_register
  247. *
  248. * Description:
  249. * Register the oneshot device as 'devpath'
  250. *
  251. * Input Parameters:
  252. * devpath - The full path to the driver to register. E.g., "/dev/oneshot0"
  253. * lower - An instance of the lower half interface
  254. *
  255. * Returned Value:
  256. * Zero (OK) on success; a negated errno value on failure. The following
  257. * possible error values may be returned (most are returned by
  258. * register_driver()):
  259. *
  260. * EINVAL - 'path' is invalid for this operation
  261. * EEXIST - An inode already exists at 'path'
  262. * ENOMEM - Failed to allocate in-memory resources for the operation
  263. *
  264. ****************************************************************************/
  265. int oneshot_register(FAR const char *devname,
  266. FAR struct oneshot_lowerhalf_s *lower)
  267. {
  268. FAR struct oneshot_dev_s *priv;
  269. int ret;
  270. sninfo("devname=%s lower=%p\n", devname, lower);
  271. DEBUGASSERT(devname != NULL && lower != NULL);
  272. /* Allocate a new oneshot timer driver instance */
  273. priv = (FAR struct oneshot_dev_s *)
  274. kmm_zalloc(sizeof(struct oneshot_dev_s));
  275. if (!priv)
  276. {
  277. snerr("ERROR: Failed to allocate device structure\n");
  278. return -ENOMEM;
  279. }
  280. /* Initialize the new oneshot timer driver instance */
  281. priv->od_lower = lower;
  282. nxsem_init(&priv->od_exclsem, 0, 1);
  283. /* And register the oneshot timer driver */
  284. ret = register_driver(devname, &g_oneshot_ops, 0666, priv);
  285. if (ret < 0)
  286. {
  287. snerr("ERROR: register_driver failed: %d\n", ret);
  288. nxsem_destroy(&priv->od_exclsem);
  289. kmm_free(priv);
  290. }
  291. return ret;
  292. }
  293. #endif /* CONFIG_ONESHOT */