oneshot.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /****************************************************************************
  2. * include/nuttx/timers/oneshot.h
  3. *
  4. * Copyright (C) 2016 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  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. #ifndef __INCLUDE_NUTTX_TIMERS_ONESHOT_H
  36. #define __INCLUDE_NUTTX_TIMERS_ONESHOT_H
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/config.h>
  41. #include <errno.h>
  42. #include <signal.h>
  43. #include <stdint.h>
  44. #include <time.h>
  45. #include <nuttx/fs/ioctl.h>
  46. /****************************************************************************
  47. * Pre-processor Definitions
  48. ****************************************************************************/
  49. /* IOCTL commands ***********************************************************/
  50. /* These commands are used by applications to access the oneshot lower-half
  51. * logic via the oneshot character driver IOCTL command. Since the oneshot
  52. * driver is a device control interface and not a data transfer interface,
  53. * the majority of the functionality is implemented in driver IOCTL calls.
  54. * The oneshot IOCTL commands are listed below:
  55. *
  56. * These are detected and handled by the "upper half" timer driver.
  57. *
  58. * OSIOC_MAXDELAY - Return the maximum delay that can be supported by
  59. * this timer.
  60. * Argument: A reference to a struct timespec in which
  61. * the maximum time will be returned.
  62. * OSIOC_START - Start the oneshot timer
  63. * Argument: A reference to struct oneshot_start_s
  64. * OSIOC_CANCEL - Stop the timer
  65. * Argument: A reference to a struct timespec in which
  66. * the time remaining will be returned.
  67. * OSIOC_CURRENT - Get the current time
  68. * Argument: A reference to a struct timespec in which
  69. * the current time will be returned.
  70. *
  71. * NOTE: _TCIOC(0x0020) through _TCIOC(0x003f) are reserved for use by the
  72. * oneshot driver to assure that the values are unique. Other timer drivers
  73. * must not use IOCTL commands in this numeric range.
  74. */
  75. #define OSIOC_MAXDELAY _TCIOC(0x0020)
  76. #define OSIOC_START _TCIOC(0x0021)
  77. #define OSIOC_CANCEL _TCIOC(0x0022)
  78. #define OSIOC_CURRENT _TCIOC(0x0023)
  79. /* Method access helper macros **********************************************/
  80. /****************************************************************************
  81. * Name: ONESHOT_MAX_DELAY
  82. *
  83. * Description:
  84. * Determine the maximum delay of the one-shot timer (in microseconds)
  85. *
  86. * Input Parameters:
  87. * lower An instance of the lower-half oneshot state structure. This
  88. * structure must have been previously initialized via a call to
  89. * oneshot_initialize();
  90. * ts The location in which to return the maximum delay.
  91. *
  92. * Returned Value:
  93. * Zero (OK) is returned on success; a negated errno value is returned
  94. * on failure.
  95. *
  96. ****************************************************************************/
  97. #define ONESHOT_MAX_DELAY(l,t) ((l)->ops->max_delay(l,t))
  98. /****************************************************************************
  99. * Name: ONESHOT_START
  100. *
  101. * Description:
  102. * Start the oneshot timer
  103. *
  104. * Input Parameters:
  105. * lower An instance of the lower-half oneshot state structure. This
  106. * structure must have been previously initialized via a call to
  107. * oneshot_initialize();
  108. * handler The function to call when when the oneshot timer expires.
  109. * arg An opaque argument that will accompany the callback.
  110. * ts Provides the duration of the one shot timer.
  111. *
  112. * Returned Value:
  113. * Zero (OK) is returned on success; a negated errno value is returned
  114. * on failure.
  115. *
  116. ****************************************************************************/
  117. #define ONESHOT_START(l,h,a,t) ((l)->ops->start(l,h,a,t))
  118. /****************************************************************************
  119. * Name: ONESHOT_CANCEL
  120. *
  121. * Description:
  122. * Cancel the oneshot timer and return the time remaining on the timer.
  123. *
  124. * NOTE: This function may execute at a high rate with no timer running (as
  125. * when pre-emption is enabled and disabled).
  126. *
  127. * Input Parameters:
  128. * lower Caller allocated instance of the oneshot state structure. This
  129. * structure must have been previously initialized via a call to
  130. * oneshot_initialize();
  131. * ts The location in which to return the time remaining on the
  132. * oneshot timer. A time of zero is returned if the timer is
  133. * not running.
  134. *
  135. * Returned Value:
  136. * Zero (OK) is returned on success. A call to up_timer_cancel() when
  137. * the timer is not active should also return success; a negated errno
  138. * value is returned on any failure.
  139. *
  140. ****************************************************************************/
  141. #define ONESHOT_CANCEL(l,t) ((l)->ops->cancel(l,t))
  142. /****************************************************************************
  143. * Name: ONESHOT_CURRENT
  144. *
  145. * Description:
  146. * Get the current time.
  147. *
  148. * Input Parameters:
  149. * lower Caller allocated instance of the oneshot state structure. This
  150. * structure must have been previously initialized via a call to
  151. * oneshot_initialize();
  152. * ts The location in which to return the current time. A time of zero
  153. * is returned for the initialization moment.
  154. *
  155. * Returned Value:
  156. * Zero (OK) is returned on success, a negated errno value is returned on
  157. * any failure.
  158. *
  159. ****************************************************************************/
  160. #define ONESHOT_CURRENT(l,t) ((l)->ops->current ? (l)->ops->current(l,t) : -ENOSYS)
  161. /****************************************************************************
  162. * Public Types
  163. ****************************************************************************/
  164. /* This describes the callback function that will be invoked when the oneshot
  165. * timer expires. The oneshot fires, the client will receive:
  166. *
  167. * lower - An instance of the lower half driver
  168. * arg - The opaque argument provided when the interrupt was registered
  169. */
  170. struct oneshot_lowerhalf_s;
  171. typedef CODE void (*oneshot_callback_t)(FAR struct oneshot_lowerhalf_s *lower,
  172. FAR void *arg);
  173. /* The one short operations supported by the lower half driver */
  174. struct timespec;
  175. struct oneshot_operations_s
  176. {
  177. CODE int (*max_delay)(FAR struct oneshot_lowerhalf_s *lower,
  178. FAR struct timespec *ts);
  179. CODE int (*start)(FAR struct oneshot_lowerhalf_s *lower,
  180. oneshot_callback_t callback, FAR void *arg,
  181. FAR const struct timespec *ts);
  182. CODE int (*cancel)(struct oneshot_lowerhalf_s *lower,
  183. FAR struct timespec *ts);
  184. CODE int (*current)(struct oneshot_lowerhalf_s *lower,
  185. FAR struct timespec *ts);
  186. };
  187. /* This structure describes the state of the oneshot timer lower-half driver */
  188. struct oneshot_lowerhalf_s
  189. {
  190. /* This is the part of the lower half driver that is visible to the upper-
  191. * half client of the driver.
  192. */
  193. FAR const struct oneshot_operations_s *ops;
  194. /* Private lower half data may follow */
  195. };
  196. #ifdef CONFIG_ONESHOT
  197. /* Argument to OSIOC_START IOCTL command */
  198. struct oneshot_start_s
  199. {
  200. pid_t pid; /* PID of task to be signalled (0 means calling task) */
  201. struct sigevent event; /* Describe the way a task is to be notified */
  202. struct timespec ts; /* Delay until time expiration */
  203. };
  204. #endif
  205. /****************************************************************************
  206. * Public Data
  207. ****************************************************************************/
  208. #undef EXTERN
  209. #if defined(__cplusplus)
  210. #define EXTERN extern "C"
  211. extern "C"
  212. {
  213. #else
  214. #define EXTERN extern
  215. #endif
  216. /****************************************************************************
  217. * Public Function Prototypes
  218. ****************************************************************************/
  219. /****************************************************************************
  220. * Name: oneshot_initialize
  221. *
  222. * Description:
  223. * Initialize the oneshot timer and return a oneshot lower half driver
  224. * instance.
  225. *
  226. * Input Parameters:
  227. * chan Timer counter channel to be used.
  228. * resolution The required resolution of the timer in units of
  229. * microseconds. NOTE that the range is restricted to the
  230. * range of uint16_t (excluding zero).
  231. *
  232. * Returned Value:
  233. * On success, a non-NULL instance of the oneshot lower-half driver is
  234. * returned. NULL is return on any failure.
  235. *
  236. ****************************************************************************/
  237. FAR struct oneshot_lowerhalf_s *oneshot_initialize(int chan,
  238. uint16_t resolution);
  239. /****************************************************************************
  240. * Name: oneshot_register
  241. *
  242. * Description:
  243. * Register the oneshot device as 'devpath'
  244. *
  245. * Input Parameters:
  246. * devpath - The full path to the driver to register. E.g., "/dev/oneshot0"
  247. * lower - An instance of the lower half interface
  248. *
  249. * Returned Value:
  250. * Zero (OK) on success; a negated errno value on failure. The following
  251. * possible error values may be returned (most are returned by
  252. * register_driver()):
  253. *
  254. * EINVAL - 'path' is invalid for this operation
  255. * EEXIST - An inode already exists at 'path'
  256. * ENOMEM - Failed to allocate in-memory resources for the operation
  257. *
  258. ****************************************************************************/
  259. #ifdef CONFIG_ONESHOT
  260. int oneshot_register(FAR const char *devname,
  261. FAR struct oneshot_lowerhalf_s *lower);
  262. #endif
  263. #undef EXTERN
  264. #ifdef __cplusplus
  265. }
  266. #endif
  267. #endif /* __INCLUDE_NUTTX_TIMERS_ONESHOT_H */