rtc.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /****************************************************************************
  2. * include/nuttx/timers/rtc.h
  3. *
  4. * Copyright(C) 2011 Uros Platise. All rights reserved.
  5. * Author: Uros Platise <uros.platise@isotel.eu>
  6. *
  7. * With extensions, modifications by:
  8. *
  9. * Copyright (C) 2011-2012, 2015-2016 Gregory Nutt. All rights reserved.
  10. * Author: Gregory Nutt <gnutt@nuttx.org>
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in
  20. * the documentation and/or other materials provided with the
  21. * distribution.
  22. * 3. Neither the name NuttX nor the names of its contributors may be
  23. * used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  29. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  30. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  31. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING,
  32. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  33. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  34. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  36. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. ****************************************************************************/
  40. #ifndef __INCLUDE_NUTTX_TIMERS_RTC_H
  41. #define __INCLUDE_NUTTX_TIMERS_RTC_H
  42. /****************************************************************************
  43. * Included Files
  44. ****************************************************************************/
  45. #include <nuttx/config.h>
  46. #include <nuttx/compiler.h>
  47. #include <stdbool.h>
  48. #include <signal.h>
  49. #include <time.h>
  50. #include <sys/time.h>
  51. #include <nuttx/fs/ioctl.h>
  52. #ifdef CONFIG_RTC
  53. /****************************************************************************
  54. * Pre-processor Definitions
  55. ****************************************************************************/
  56. /* Configuration ************************************************************/
  57. /* CONFIG_RTC - Enables general support for a hardware RTC. Specific
  58. * architectures may require other specific settings.
  59. *
  60. * CONFIG_RTC_DATETIME - There are two general types of RTC: (1) A simple
  61. * battery backed counter that keeps the time when power is down, and (2)
  62. * A full date / time RTC the provides the date and time information, often
  63. * in BCD format. If CONFIG_RTC_DATETIME is selected, it specifies this
  64. * second kind of RTC. In this case, the RTC is used to "seed" the normal
  65. * NuttX timer and the NuttX system timer provides for higher resolution
  66. * time.
  67. *
  68. * CONFIG_RTC_HIRES - If CONFIG_RTC_DATETIME not selected, then the simple,
  69. * battery backed counter is used. There are two different implementations
  70. * of such simple counters based on the time resolution of the counter:
  71. * The typical RTC keeps time to resolution of 1 second, usually
  72. * supporting a 32-bit time_t value. In this case, the RTC is used to
  73. * "seed" the normal NuttX timer and the NuttX timer provides for higher
  74. * resolution time.
  75. *
  76. * If CONFIG_RTC_HIRES is enabled in the NuttX configuration, then the
  77. * RTC provides higher resolution time and completely replaces the system
  78. * timer for purpose of date and time.
  79. *
  80. * CONFIG_RTC_FREQUENCY - If CONFIG_RTC_HIRES is defined, then the frequency
  81. * of the high resolution RTC must be provided. If CONFIG_RTC_HIRES is
  82. * not defined, CONFIG_RTC_FREQUENCY is assumed to be one.
  83. *
  84. * CONFIG_RTC_ALARM - Enable if the RTC hardware supports setting of an
  85. * alarm. A callback function will be executed when the alarm goes off
  86. *
  87. * CONFIG_RTC_PERIODIC - Enable if the RTC hardware supports setting a
  88. * periodic wakeup. A callback function will be executed when the wakeup happens.
  89. * This is an experimental feature.
  90. *
  91. * CONFIG_RTC_DRIVER - Enable building the upper-half RTC driver
  92. */
  93. #ifdef CONFIG_RTC_HIRES
  94. # ifdef CONFIG_RTC_DATETIME
  95. # error "CONFIG_RTC_HIRES and CONFIG_RTC_DATETIME are both defined"
  96. # endif
  97. # ifndef CONFIG_RTC_FREQUENCY
  98. # error "CONFIG_RTC_FREQUENCY is required for CONFIG_RTC_HIRES"
  99. # endif
  100. #else
  101. # ifndef CONFIG_RTC_FREQUENCY
  102. # define CONFIG_RTC_FREQUENCY 1
  103. # endif
  104. # if CONFIG_RTC_FREQUENCY != 1
  105. # error "The low resolution RTC must have frequency 1Hz"
  106. # endif
  107. #endif
  108. #if defined(CONFIG_RTC_ALARM) && !defined(CONFIG_RTC_NALARMS)
  109. # define CONFIG_RTC_NALARMS 1
  110. #endif
  111. /* The remainder of the contain of this header file is only valid if the
  112. * RTC upper half driver is built.
  113. */
  114. #ifdef CONFIG_RTC_DRIVER
  115. /* IOCTL Commands ***********************************************************/
  116. /* RTC driver IOCTL commands. These are Linux compatible command names, not
  117. * all of these commands are supported by all RTC drivers, however.
  118. */
  119. /* RTC_RD_TIME returns the current RTC time.
  120. *
  121. * Argument: A writeable reference to a struct rtc_time to receive the RTC's
  122. * time.
  123. */
  124. #define RTC_RD_TIME _RTCIOC(0x0001)
  125. /* RTC_SET_TIME sets the RTC's time
  126. *
  127. * Argument: A read-only reference to a struct rtc_time containing the
  128. * new time to be set.
  129. */
  130. #define RTC_SET_TIME _RTCIOC(0x0002)
  131. /* RTC_HAVE_SET_TIME checks if RTC's time had been set
  132. *
  133. * Argument: A writable reference to a bool to receive true/false return value
  134. * of the check.
  135. */
  136. #define RTC_HAVE_SET_TIME _RTCIOC(0x0003)
  137. /* RTC_SET_ALARM sets the alarm time (for RTCs that support alarms).
  138. *
  139. * Argument: A read-only reference to a struct rtc_setalarm_s containing the
  140. * new alarm time to be set.
  141. */
  142. #define RTC_SET_ALARM _RTCIOC(0x0004)
  143. /* RTC_SET_RELATIVE sets the alarm time relative to the current time.
  144. *
  145. * Argument: A read-only reference to a struct rtc_setrelative_s containing the
  146. * new relative alarm time to be set.
  147. */
  148. #define RTC_SET_RELATIVE _RTCIOC(0x0005)
  149. /* RTC_CANCEL_ALARM cancel the alarm.
  150. *
  151. * Argument: An ALARM ID value that indicates which alarm should be canceled.
  152. */
  153. #define RTC_CANCEL_ALARM _RTCIOC(0x0006)
  154. /* RTC_RD_ALARM query the alarm.
  155. *
  156. * Argument: An ALARM ID value that indicates which alarm should be queried.
  157. */
  158. #define RTC_RD_ALARM _RTCIOC(0x0007)
  159. /* RTC_SET_PERIODIC set a periodic wakeup.
  160. *
  161. * Argument: A read-only reference to a struct rtc_setperiodic_s containing the
  162. * new wakeup period to be set.
  163. */
  164. #define RTC_SET_PERIODIC _RTCIOC(0x0008)
  165. /* RTC_CANCEL_PERIODIC cancel the periodic wakeup.
  166. *
  167. * Argument: An ID value that indicates which wakeup should be canceled.
  168. */
  169. #define RTC_CANCEL_PERIODIC _RTCIOC(0x0009)
  170. /* Architecture-specific RTC IOCTLS should begin at RTC_USER_IOCBASE. For
  171. * example:
  172. *
  173. * #define MY_RTC_IOCTL1 _RTCIOC(RTC_USER_IOCBASE);
  174. * #define MY_RTC_IOCTL2 _RTCIOC(RTC_USER_IOCBASE + 1);
  175. * etc.
  176. */
  177. #define RTC_USER_IOCBASE 0x000a
  178. /****************************************************************************
  179. * Public Types
  180. ****************************************************************************/
  181. /* IOCTL data structures */
  182. /* Broken-out time representation used with RTC IOCTL commands:
  183. *
  184. * The fields in this structure have the same meaning and ranges as for the
  185. * tm structure described in gmtime(). Further, it is REQUIRED that the
  186. * structure be cast compatible with struct tm! They must be interchangeable.
  187. */
  188. struct rtc_time
  189. {
  190. int tm_sec; /* Seconds (0-61, allows for leap seconds) */
  191. int tm_min; /* Minutes (0-59) */
  192. int tm_hour; /* Hours (0-23) */
  193. int tm_mday; /* Day of the month (1-31) */
  194. int tm_mon; /* Month (0-11) */
  195. int tm_year; /* Years since 1900 */
  196. int tm_wday; /* Day of the week (0-6) (unused) */
  197. int tm_yday; /* Day of the year (0-365) (unused) */
  198. int tm_isdst; /* Non-0 if daylight savings time is in effect (unused) */
  199. #if defined(CONFIG_RTC_HIRES) || defined(CONFIG_ARCH_HAVE_RTC_SUBSECONDS)
  200. long tm_nsec; /* Nanosecond (0-999999999) */
  201. #endif
  202. };
  203. #ifdef CONFIG_RTC_ALARM
  204. /* Structure used with the RTC_RD_ALARM IOCTL command and with
  205. * rdalarm() method.
  206. */
  207. struct rtc_rdalarm_s
  208. {
  209. uint8_t id; /* Indicates the alarm being queried */
  210. bool active; /* Alarm actively timing or disabled */
  211. struct rtc_time time; /* Current RTC time (if enabled) */
  212. };
  213. /* Structure used with the RTC_SET_ALARM IOCTL command. */
  214. struct rtc_setalarm_s
  215. {
  216. uint8_t id; /* Indicates the alarm to be set */
  217. pid_t pid; /* Identifies task to be notified (0=caller) */
  218. struct sigevent event; /* Describe the way a task is to be notified */
  219. struct rtc_time time; /* Alarm time */
  220. };
  221. /* Structure used with the RTC_SET_RELATIVE IOCTL command. */
  222. struct rtc_setrelative_s
  223. {
  224. uint8_t id; /* Indicates the alarm to be set */
  225. pid_t pid; /* Identifies task to be notified (0=caller) */
  226. struct sigevent event; /* Describe the way a task is to be notified */
  227. time_t reltime; /* Relative time in seconds */
  228. };
  229. /* Callback type used by the RTC hardware to notify the RTC driver when the
  230. * alarm expires.
  231. */
  232. typedef CODE void (*rtc_alarm_callback_t)(FAR void *priv, int alarmid);
  233. /* Structure used with the setalarm method */
  234. struct lower_setalarm_s
  235. {
  236. uint8_t id; /* Indicates the alarm to be set */
  237. rtc_alarm_callback_t cb; /* Callback when the alarm expires */
  238. FAR void *priv; /* Private argument to accompany callback */
  239. struct rtc_time time; /* Alarm time */
  240. };
  241. /* Structure used with the setrelative method */
  242. struct lower_setrelative_s
  243. {
  244. uint8_t id; /* Indicates the alarm to be set */
  245. rtc_alarm_callback_t cb; /* Callback when the alarm expires */
  246. FAR void *priv; /* Private argument to accompany callback */
  247. time_t reltime; /* Relative time in seconds */
  248. };
  249. /* Structure used with the rdalarm method */
  250. struct lower_rdalarm_s
  251. {
  252. uint8_t id; /* Indicates the alarm to be set */
  253. FAR void *priv; /* Private argument to accompany callback */
  254. FAR struct rtc_time *time; /* Queried RTC time pointer */
  255. };
  256. #endif
  257. #ifdef CONFIG_RTC_PERIODIC
  258. /* Structure used with the RTC_SET_PERIODIC IOCTL command. */
  259. struct rtc_setperiodic_s
  260. {
  261. uint8_t id; /* Indicates the alarm to be set */
  262. pid_t pid; /* Identifies task to be notified (0=caller) */
  263. struct sigevent event; /* Describe the way a task is to be notified */
  264. struct timespec period; /* Period between wakeups */
  265. };
  266. /* Callback type used by the RTC hardware to notify the RTC driver when the
  267. * wakeup period expires.
  268. */
  269. typedef CODE void (*rtc_wakeup_callback_t)(FAR void *priv, int alarmid);
  270. /* Structure used with the setperiodic method */
  271. struct lower_setperiodic_s
  272. {
  273. uint8_t id; /* Indicates the wakeup to be set */
  274. rtc_wakeup_callback_t cb; /* Callback when the wakeup expires */
  275. FAR void *priv; /* Private argument to accompany callback */
  276. struct timespec period; /* Period between wakeups */
  277. };
  278. #endif
  279. /* The RTC driver is implemented as a common, upper-half character driver
  280. * that provides the RTC driver structure and a lower-level, hardware
  281. * specific implementation that performs the actual RTC operations.
  282. *
  283. * struct rtc_ops_s defines the callable interface from the common upper-
  284. * half driver into lower half implementation. Each method in this
  285. * structure corresponds to one of the RTC IOCTL commands. All IOCTL
  286. * command logic is implemented in the lower-half driver.
  287. *
  288. * A NULL value should be provided for any unsupported methods.
  289. */
  290. struct rtc_lowerhalf_s;
  291. struct rtc_ops_s
  292. {
  293. /* rdtime() returns the current RTC time. */
  294. CODE int (*rdtime)(FAR struct rtc_lowerhalf_s *lower,
  295. FAR struct rtc_time *rtctime);
  296. /* settime sets the RTC's time */
  297. CODE int (*settime)(FAR struct rtc_lowerhalf_s *lower,
  298. FAR const struct rtc_time *rtctime);
  299. /* havesettime checks if RTC time have been set */
  300. CODE bool (*havesettime)(FAR struct rtc_lowerhalf_s *lower);
  301. #ifdef CONFIG_RTC_ALARM
  302. /* setalarm sets up a new alarm. */
  303. CODE int (*setalarm)(FAR struct rtc_lowerhalf_s *lower,
  304. FAR const struct lower_setalarm_s *alarminfo);
  305. /* setalarm sets up a new alarm relative to the current time. */
  306. CODE int (*setrelative)(FAR struct rtc_lowerhalf_s *lower,
  307. FAR const struct lower_setrelative_s *alarminfo);
  308. /* cancelalarm cancels the current alarm. */
  309. CODE int (*cancelalarm)(FAR struct rtc_lowerhalf_s *lower, int alarmid);
  310. /* rdalarm query the current alarm. */
  311. CODE int (*rdalarm)(FAR struct rtc_lowerhalf_s *lower,
  312. FAR struct lower_rdalarm_s *alarminfo);
  313. #endif
  314. #ifdef CONFIG_RTC_PERIODIC
  315. /* setperiodic sets up a new periodic wakeup. */
  316. CODE int (*setperiodic)(FAR struct rtc_lowerhalf_s *lower,
  317. FAR const struct lower_setperiodic_s *alarminfo);
  318. /* cancelperiodic cancels the current periodic wakeup. */
  319. CODE int (*cancelperiodic)(FAR struct rtc_lowerhalf_s *lower, int alarmid);
  320. #endif
  321. #ifdef CONFIG_RTC_IOCTL
  322. /* Support for architecture-specific RTC operations */
  323. CODE int (*ioctl)(FAR struct rtc_lowerhalf_s *lower, int cmd,
  324. unsigned long arg);
  325. #endif
  326. #ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
  327. /* The driver has been unlinked and there are no further open references
  328. * to the driver.
  329. */
  330. CODE int (*destroy)(FAR struct rtc_lowerhalf_s *lower);
  331. #endif
  332. };
  333. /* When the RTC driver is instantiated, a reference to struct
  334. * rtc_lowerhalf_s is passed to the initialization function and bound to
  335. * the driver. The actual content of the state structure used by different
  336. * lower half drivers will vary from implementation to implementation. But
  337. * each implementation must be cast compatible with this definition of
  338. * struct rtc_lowerhalf_s that is understood by the upper half driver.
  339. */
  340. struct rtc_lowerhalf_s
  341. {
  342. /* This is the contained reference to the read-only, lower-half
  343. * operations vtable (which may lie in FLASH or ROM)
  344. */
  345. FAR const struct rtc_ops_s *ops;
  346. /* Data following this can vary from RTC driver-to-driver */
  347. };
  348. /****************************************************************************
  349. * Public Data
  350. ****************************************************************************/
  351. #undef EXTERN
  352. #if defined(__cplusplus)
  353. #define EXTERN extern "C"
  354. extern "C"
  355. {
  356. #else
  357. #define EXTERN extern
  358. #endif
  359. /****************************************************************************
  360. * Public Function Prototypes
  361. ****************************************************************************/
  362. /****************************************************************************
  363. * Name: rtc_initialize
  364. *
  365. * Description:
  366. * Create an RTC driver by binding to the lower half RTC driver instance
  367. * provided to this function. The resulting RTC driver will be registered
  368. * at /dev/rtcN where N is the minor number provided to this function.
  369. *
  370. ****************************************************************************/
  371. #ifdef __KERNEL__
  372. int rtc_initialize(int minor, FAR struct rtc_lowerhalf_s *lower);
  373. #endif
  374. #undef EXTERN
  375. #if defined(__cplusplus)
  376. }
  377. #endif
  378. #endif /* CONFIG_RTC_DRIVER */
  379. #endif /* CONFIG_RTC */
  380. #endif /* __INCLUDE_NUTTX_TIMERS_RTC_H */