timer.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /****************************************************************************
  2. * drivers/timers/timer.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 <string.h>
  28. #include <fcntl.h>
  29. #include <assert.h>
  30. #include <errno.h>
  31. #include <debug.h>
  32. #include <nuttx/irq.h>
  33. #include <nuttx/kmalloc.h>
  34. #include <nuttx/signal.h>
  35. #include <nuttx/fs/fs.h>
  36. #include <nuttx/semaphore.h>
  37. #include <nuttx/timers/timer.h>
  38. #ifdef CONFIG_TIMER
  39. /****************************************************************************
  40. * Private Type Definitions
  41. ****************************************************************************/
  42. /* This structure describes the state of the upper half driver */
  43. struct timer_upperhalf_s
  44. {
  45. sem_t exclsem; /* Supports mutual exclusion */
  46. uint8_t crefs; /* The number of times the device has been opened */
  47. FAR char *path; /* Registration path */
  48. /* The contained signal info */
  49. struct timer_notify_s notify;
  50. struct sigwork_s work;
  51. /* The contained lower-half driver */
  52. FAR struct timer_lowerhalf_s *lower;
  53. };
  54. /****************************************************************************
  55. * Private Function Prototypes
  56. ****************************************************************************/
  57. static bool timer_notifier(FAR uint32_t *next_interval_us, FAR void *arg);
  58. static int timer_open(FAR struct file *filep);
  59. static int timer_close(FAR struct file *filep);
  60. static ssize_t timer_read(FAR struct file *filep, FAR char *buffer,
  61. size_t buflen);
  62. static ssize_t timer_write(FAR struct file *filep, FAR const char *buffer,
  63. size_t buflen);
  64. static int timer_ioctl(FAR struct file *filep, int cmd,
  65. unsigned long arg);
  66. /****************************************************************************
  67. * Private Data
  68. ****************************************************************************/
  69. static const struct file_operations g_timerops =
  70. {
  71. timer_open, /* open */
  72. timer_close, /* close */
  73. timer_read, /* read */
  74. timer_write, /* write */
  75. NULL, /* seek */
  76. timer_ioctl, /* ioctl */
  77. NULL /* poll */
  78. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  79. , NULL /* unlink */
  80. #endif
  81. };
  82. /****************************************************************************
  83. * Private Functions
  84. ****************************************************************************/
  85. /****************************************************************************
  86. * Name: timer_notifier
  87. *
  88. * Description:
  89. * Notify the application via a signal when the timer interrupt occurs
  90. *
  91. * REVISIT: This function prototype is insufficient to support signaling
  92. *
  93. ****************************************************************************/
  94. static bool timer_notifier(FAR uint32_t *next_interval_us, FAR void *arg)
  95. {
  96. FAR struct timer_upperhalf_s *upper = (FAR struct timer_upperhalf_s *)arg;
  97. FAR struct timer_notify_s *notify = &upper->notify;
  98. DEBUGASSERT(upper != NULL);
  99. /* Signal the waiter.. if there is one */
  100. nxsig_notification(notify->pid, &notify->event,
  101. SI_QUEUE, &upper->work);
  102. return true;
  103. }
  104. /****************************************************************************
  105. * Name: timer_open
  106. *
  107. * Description:
  108. * This function is called whenever the timer device is opened.
  109. *
  110. ****************************************************************************/
  111. static int timer_open(FAR struct file *filep)
  112. {
  113. FAR struct inode *inode = filep->f_inode;
  114. FAR struct timer_upperhalf_s *upper = inode->i_private;
  115. uint8_t tmp;
  116. int ret;
  117. tmrinfo("crefs: %d\n", upper->crefs);
  118. /* Get exclusive access to the device structures */
  119. ret = nxsem_wait(&upper->exclsem);
  120. if (ret < 0)
  121. {
  122. goto errout;
  123. }
  124. /* Increment the count of references to the device. If this the first
  125. * time that the driver has been opened for this device, then initialize
  126. * the device.
  127. */
  128. tmp = upper->crefs + 1;
  129. if (tmp == 0)
  130. {
  131. /* More than 255 opens; uint8_t overflows to zero */
  132. ret = -EMFILE;
  133. goto errout_with_sem;
  134. }
  135. /* Save the new open count */
  136. upper->crefs = tmp;
  137. ret = OK;
  138. errout_with_sem:
  139. nxsem_post(&upper->exclsem);
  140. errout:
  141. return ret;
  142. }
  143. /****************************************************************************
  144. * Name: timer_close
  145. *
  146. * Description:
  147. * This function is called when the timer device is closed.
  148. *
  149. ****************************************************************************/
  150. static int timer_close(FAR struct file *filep)
  151. {
  152. FAR struct inode *inode = filep->f_inode;
  153. FAR struct timer_upperhalf_s *upper = inode->i_private;
  154. int ret;
  155. tmrinfo("crefs: %d\n", upper->crefs);
  156. /* Get exclusive access to the device structures */
  157. ret = nxsem_wait(&upper->exclsem);
  158. if (ret < 0)
  159. {
  160. return ret;
  161. }
  162. /* Decrement the references to the driver. If the reference count will
  163. * decrement to 0, then uninitialize the driver.
  164. */
  165. if (upper->crefs > 0)
  166. {
  167. upper->crefs--;
  168. }
  169. nxsem_post(&upper->exclsem);
  170. return OK;
  171. }
  172. /****************************************************************************
  173. * Name: timer_read
  174. *
  175. * Description:
  176. * A dummy read method. This is provided only to satisfy the VFS layer.
  177. *
  178. ****************************************************************************/
  179. static ssize_t timer_read(FAR struct file *filep, FAR char *buffer,
  180. size_t buflen)
  181. {
  182. /* Return zero -- usually meaning end-of-file */
  183. return 0;
  184. }
  185. /****************************************************************************
  186. * Name: timer_write
  187. *
  188. * Description:
  189. * A dummy write method. This is provided only to satisfy the VFS layer.
  190. *
  191. ****************************************************************************/
  192. static ssize_t timer_write(FAR struct file *filep, FAR const char *buffer,
  193. size_t buflen)
  194. {
  195. return 0;
  196. }
  197. /****************************************************************************
  198. * Name: timer_ioctl
  199. *
  200. * Description:
  201. * The standard ioctl method. This is where ALL of the timer work is
  202. * done.
  203. *
  204. ****************************************************************************/
  205. static int timer_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
  206. {
  207. FAR struct inode *inode = filep->f_inode;
  208. FAR struct timer_upperhalf_s *upper;
  209. FAR struct timer_lowerhalf_s *lower;
  210. int ret;
  211. tmrinfo("cmd: %d arg: %ld\n", cmd, arg);
  212. upper = inode->i_private;
  213. DEBUGASSERT(upper != NULL);
  214. lower = upper->lower;
  215. DEBUGASSERT(lower != NULL);
  216. /* Get exclusive access to the device structures */
  217. ret = nxsem_wait(&upper->exclsem);
  218. if (ret < 0)
  219. {
  220. return ret;
  221. }
  222. /* Handle built-in ioctl commands */
  223. switch (cmd)
  224. {
  225. /* cmd: TCIOC_START
  226. * Description: Start the timer
  227. * Argument: Ignored
  228. */
  229. case TCIOC_START:
  230. {
  231. /* Start the timer, resetting the time to the current timeout */
  232. if (lower->ops->start)
  233. {
  234. ret = lower->ops->start(lower);
  235. }
  236. else
  237. {
  238. ret = -ENOSYS;
  239. }
  240. }
  241. break;
  242. /* cmd: TCIOC_STOP
  243. * Description: Stop the timer
  244. * Argument: Ignored
  245. */
  246. case TCIOC_STOP:
  247. {
  248. /* Stop the timer */
  249. DEBUGASSERT(lower->ops->stop != NULL); /* Required */
  250. ret = lower->ops->stop(lower);
  251. nxsig_cancel_notification(&upper->work);
  252. }
  253. break;
  254. /* cmd: TCIOC_GETSTATUS
  255. * Description: Get the status of the timer.
  256. * Argument: A writeable pointer to struct timer_status_s.
  257. */
  258. case TCIOC_GETSTATUS:
  259. {
  260. FAR struct timer_status_s *status;
  261. /* Get the current timer status */
  262. if (lower->ops->getstatus) /* Optional */
  263. {
  264. status = (FAR struct timer_status_s *)((uintptr_t)arg);
  265. if (status)
  266. {
  267. ret = lower->ops->getstatus(lower, status);
  268. }
  269. else
  270. {
  271. ret = -EINVAL;
  272. }
  273. }
  274. else
  275. {
  276. ret = -ENOSYS;
  277. }
  278. }
  279. break;
  280. /* cmd: TCIOC_SETTIMEOUT
  281. * Description: Reset the timeout to this value
  282. * Argument: A 32-bit timeout value in microseconds.
  283. *
  284. * TODO: pass pointer to uint64 ns? Need to determine if these timers
  285. * are 16 or 32 bit...
  286. */
  287. case TCIOC_SETTIMEOUT:
  288. {
  289. /* Set a new timeout value (and reset the timer) */
  290. if (lower->ops->settimeout) /* Optional */
  291. {
  292. ret = lower->ops->settimeout(lower, (uint32_t)arg);
  293. }
  294. else
  295. {
  296. ret = -ENOSYS;
  297. }
  298. }
  299. break;
  300. /* cmd: TCIOC_NOTIFICATION
  301. * Description: Notify application via a signal when the timer expires.
  302. * Argument: signal information
  303. */
  304. case TCIOC_NOTIFICATION:
  305. {
  306. FAR struct timer_notify_s *notify =
  307. (FAR struct timer_notify_s *)((uintptr_t)arg);
  308. if (notify != NULL)
  309. {
  310. memcpy(&upper->notify, notify, sizeof(*notify));
  311. ret = timer_setcallback((FAR void *)upper,
  312. timer_notifier, upper);
  313. }
  314. else
  315. {
  316. ret = -EINVAL;
  317. }
  318. }
  319. break;
  320. /* cmd: TCIOC_MAXTIMEOUT
  321. * Description: Get the maximum supported timeout value
  322. * Argument: A 32-bit timeout value in microseconds.
  323. */
  324. case TCIOC_MAXTIMEOUT:
  325. {
  326. /* Get the maximum supported timeout value */
  327. if (lower->ops->maxtimeout) /* Optional */
  328. {
  329. ret = lower->ops->maxtimeout(lower, (uint32_t *)arg);
  330. }
  331. else
  332. {
  333. ret = -ENOSYS;
  334. }
  335. }
  336. break;
  337. /* Any unrecognized IOCTL commands might be platform-specific ioctl
  338. * commands
  339. */
  340. default:
  341. {
  342. tmrinfo("Forwarding unrecognized cmd: %d arg: %ld\n", cmd, arg);
  343. /* An ioctl commands that are not recognized by the "upper-half"
  344. * driver are forwarded to the lower half driver through this
  345. * method.
  346. */
  347. if (lower->ops->ioctl) /* Optional */
  348. {
  349. ret = lower->ops->ioctl(lower, cmd, arg);
  350. }
  351. else
  352. {
  353. ret = -ENOTTY;
  354. }
  355. }
  356. break;
  357. }
  358. nxsem_post(&upper->exclsem);
  359. return ret;
  360. }
  361. /****************************************************************************
  362. * Public Functions
  363. ****************************************************************************/
  364. /****************************************************************************
  365. * Name: timer_register
  366. *
  367. * Description:
  368. * This function binds an instance of a "lower half" timer driver with the
  369. * "upper half" timer device and registers that device so that can be used
  370. * by application code.
  371. *
  372. * When this function is called, the "lower half" driver should be in the
  373. * disabled state (as if the stop() method had already been called).
  374. *
  375. * Input Parameters:
  376. * dev path - The full path to the driver to be registers in the NuttX
  377. * pseudo-filesystem. The recommended convention is to name all timer
  378. * drivers as "/dev/tc0", "/dev/tc1", etc. where the driver
  379. * path differs only in the "minor" number at the end of the device name.
  380. * lower - A pointer to an instance of lower half timer driver. This
  381. * instance is bound to the timer driver and must persists as long as
  382. * the driver persists.
  383. *
  384. * Returned Value:
  385. * On success, a non-NULL handle is returned to the caller. In the event
  386. * of any failure, a NULL value is returned.
  387. *
  388. ****************************************************************************/
  389. FAR void *timer_register(FAR const char *path,
  390. FAR struct timer_lowerhalf_s *lower)
  391. {
  392. FAR struct timer_upperhalf_s *upper;
  393. int ret;
  394. DEBUGASSERT(path && lower);
  395. tmrinfo("Entry: path=%s\n", path);
  396. /* Allocate the upper-half data structure */
  397. upper = (FAR struct timer_upperhalf_s *)
  398. kmm_zalloc(sizeof(struct timer_upperhalf_s));
  399. if (!upper)
  400. {
  401. tmrerr("ERROR: Upper half allocation failed\n");
  402. goto errout;
  403. }
  404. /* Initialize the timer device structure (it was already zeroed
  405. * by kmm_zalloc()).
  406. */
  407. upper->lower = lower;
  408. nxsem_init(&upper->exclsem, 0, 1);
  409. /* Copy the registration path */
  410. upper->path = strdup(path);
  411. if (!upper->path)
  412. {
  413. tmrerr("ERROR: Path allocation failed\n");
  414. goto errout_with_upper;
  415. }
  416. /* Register the timer device */
  417. ret = register_driver(path, &g_timerops, 0666, upper);
  418. if (ret < 0)
  419. {
  420. tmrerr("ERROR: register_driver failed: %d\n", ret);
  421. goto errout_with_path;
  422. }
  423. return (FAR void *)upper;
  424. errout_with_path:
  425. kmm_free(upper->path);
  426. errout_with_upper:
  427. nxsem_destroy(&upper->exclsem);
  428. kmm_free(upper);
  429. errout:
  430. return NULL;
  431. }
  432. /****************************************************************************
  433. * Name: timer_unregister
  434. *
  435. * Description:
  436. * This function can be called to disable and unregister the timer
  437. * device driver.
  438. *
  439. * Input Parameters:
  440. * handle - This is the handle that was returned by timer_register()
  441. *
  442. * Returned Value:
  443. * None
  444. *
  445. ****************************************************************************/
  446. void timer_unregister(FAR void *handle)
  447. {
  448. FAR struct timer_upperhalf_s *upper;
  449. FAR struct timer_lowerhalf_s *lower;
  450. /* Recover the pointer to the upper-half driver state */
  451. upper = (FAR struct timer_upperhalf_s *)handle;
  452. DEBUGASSERT(upper != NULL && upper->lower != NULL);
  453. lower = upper->lower;
  454. tmrinfo("Unregistering: %s\n", upper->path);
  455. /* Disable the timer */
  456. DEBUGASSERT(lower->ops->stop); /* Required */
  457. lower->ops->stop(lower);
  458. nxsig_cancel_notification(&upper->work);
  459. /* Unregister the timer device */
  460. unregister_driver(upper->path);
  461. /* Then free all of the driver resources */
  462. nxsem_destroy(&upper->exclsem);
  463. kmm_free(upper->path);
  464. kmm_free(upper);
  465. }
  466. /****************************************************************************
  467. * Name: timer_setcallback
  468. *
  469. * Description:
  470. * This function can be called to add a callback into driver-related code
  471. * to handle timer expirations. This is a strictly OS internal interface
  472. * and may NOT be used by application code.
  473. *
  474. * Input Parameters:
  475. * handle - This is the handle that was returned by timer_register()
  476. * callback - The new timer interrupt callback
  477. * arg - Argument to be provided with the callback
  478. *
  479. * Returned Value:
  480. * Zero (OK), if the callback was successfully set, or -ENOSYS if the lower
  481. * half driver does not support the operation.
  482. *
  483. ****************************************************************************/
  484. int timer_setcallback(FAR void *handle, tccb_t callback, FAR void *arg)
  485. {
  486. FAR struct timer_upperhalf_s *upper;
  487. FAR struct timer_lowerhalf_s *lower;
  488. /* Recover the pointer to the upper-half driver state */
  489. upper = (FAR struct timer_upperhalf_s *)handle;
  490. DEBUGASSERT(upper != NULL && upper->lower != NULL);
  491. lower = upper->lower;
  492. DEBUGASSERT(lower->ops != NULL);
  493. /* Check if the lower half driver supports the setcallback method */
  494. if (lower->ops->setcallback != NULL) /* Optional */
  495. {
  496. /* Yes.. Defer the handler attachment to the lower half driver */
  497. lower->ops->setcallback(lower, callback, arg);
  498. return OK;
  499. }
  500. return -ENOSYS;
  501. }
  502. #endif /* CONFIG_TIMER */