drv_rtc.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _DRV_RTC_H_
  13. #define _DRV_RTC_H_
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include "osi_compiler.h"
  17. OSI_EXTERN_C_BEGIN
  18. /**
  19. * alarm opaque information size
  20. */
  21. #define DRV_RTC_ALARM_INFO_SIZE (40)
  22. /**
  23. * forward decalre of alarm data structure
  24. */
  25. struct drvRtcAlarm;
  26. /**
  27. * alarm type
  28. */
  29. typedef enum
  30. {
  31. DRV_RTC_ALARM_ONE_TIME, ///< one time alarm
  32. DRV_RTC_ALARM_WDAY_REPEATED ///< repeated alarm in selected day of the week.
  33. } drvRtcAlarmType_t;
  34. /**
  35. * Callback function type to be called on alarm expiration.
  36. *
  37. * In callback, it is prohibited to call alarm related APIs.
  38. * Ecah owner should set there own callback first.
  39. *
  40. * The callback will be executed in RTC thread.
  41. */
  42. typedef void (*drvRtcAlarmCB_t)(struct drvRtcAlarm *alarm, void *ctx);
  43. /**
  44. * Parameters for day of the week repeated alarm
  45. */
  46. typedef struct
  47. {
  48. uint8_t wday_mask; ///< selected day of the week
  49. uint32_t sec_in_day; ///< time in day, in seconds
  50. int32_t timezone; ///< timezone of the alarm
  51. } drvRtcAlarmWdayRepeated_t;
  52. /**
  53. * alarm information
  54. */
  55. typedef struct drvRtcAlarm
  56. {
  57. uint32_t owner; ///< alarm owner
  58. uint32_t name; ///< name of the alarm
  59. drvRtcAlarmType_t type; ///< type of the alarm
  60. drvRtcAlarmWdayRepeated_t wday_repeated; ///< day of the week repeated parameters
  61. int64_t expire_sec; ///< next expiration time
  62. uint8_t info[DRV_RTC_ALARM_INFO_SIZE]; ///< opaque information
  63. } drvRtcAlarm_t;
  64. /**
  65. * @brief initialize RTC driver
  66. *
  67. * At initialization, NVM file may be read. And then it MUST be called
  68. * after file system is initialized. Also, it will use ADI bus, and then
  69. * it should be called after ADI bus is initialized.
  70. */
  71. void drvRtcInit(void);
  72. /**
  73. * @brief set one time alarm
  74. *
  75. * Set a one time alarm with specified time. The time is offset in
  76. * second from 1970-01-01 UTC.
  77. *
  78. * When the alarm already exists, it will be replaced with the new
  79. * parameters.
  80. *
  81. * @param owner alarm owner
  82. * @param name alarm name
  83. * @param info alarm opaque information
  84. * @param info_size alarm opaque information size
  85. * @param sec time second from 1970-01-01 UTC
  86. * @param replace true: replace existed alarm with the same owner and name
  87. * @return
  88. * - true on success
  89. * - false on failure
  90. * - invalid parameter
  91. * - alarm exists, and replace is false
  92. * - out of memory
  93. */
  94. bool drvRtcSetAlarm(uint32_t owner, uint32_t name, const void *info,
  95. uint32_t info_size, int64_t sec, bool replace);
  96. /**
  97. * @brief set repeated alarm
  98. *
  99. * Set a repeated alarm with specified time.
  100. *
  101. * @param owner alarm owner
  102. * @param name alarm name
  103. * @param info alarm opaque information
  104. * @param info_size alarm opaque information size
  105. * @param wday_mask day of the week for the alarm. bit 0 for Sunday, bit 6 for Saturday.
  106. * @param sec_in_day alarm time in the day
  107. * @param timezone timezone (second offset from UTC) of the alarm
  108. * @param replace true: replace existed alarm with the same owner and name
  109. * @return
  110. * - true on success
  111. * - false on failure
  112. * - invalid parameter
  113. * - alarm exists, and replace is false
  114. * - out of memory
  115. */
  116. bool drvRtcSetRepeatAlarm(uint32_t owner, uint32_t name, const void *info,
  117. uint32_t info_size, uint8_t wday_mask,
  118. uint32_t sec_in_day, int timezone, bool replace);
  119. /**
  120. * @brief remove an alarm
  121. *
  122. * @param owner alarm owner
  123. * @param name alarm name
  124. * @return
  125. * - true on success
  126. * - false on failure
  127. */
  128. bool drvRtcRemoveAlarm(uint32_t owner, uint32_t name);
  129. /**
  130. * @brief get alarm count for specific owner
  131. *
  132. * @param owner alarm owner
  133. * @return alarm count
  134. */
  135. int drvRtcGetAlarmCount(uint32_t owner);
  136. /**
  137. * @brief get alarm information for specific owner
  138. *
  139. * The returned alarms will be ordered by expiration time.
  140. *
  141. * @param owner alarm owner
  142. * @param alarms pointer to alarm array for output (caller allocated)
  143. * @param count max count of alarms
  144. * @return count of alarm
  145. */
  146. int drvRtcGetAlarms(uint32_t owner, drvRtcAlarm_t *alarms, uint32_t count);
  147. /**
  148. * @brief remove all alarms
  149. *
  150. * Maybe the only purpose is "factory reset".
  151. */
  152. void drvRtcRemoveAllAlarms(void);
  153. /**
  154. * @brief set alarm process callback for specific owner
  155. *
  156. * Caller must set a alarm owner callback before set an alarm, or the
  157. * alarm won't response.
  158. *
  159. * @param owner alarm owner
  160. * @param context caller context
  161. * @param cb callback (NULL is allowed)
  162. * @return
  163. * - true on success
  164. * - false on fail
  165. */
  166. bool drvRtcAlarmOwnerSetCB(uint32_t owner, void *context, drvRtcAlarmCB_t cb);
  167. /**
  168. * @brief update RTC time from system epoch time
  169. */
  170. void drvRtcUpdateTime(void);
  171. /**
  172. * @brief get wakeup alarm in epoch time
  173. *
  174. * It may be called in sleep procedure, so thread synchronization (such as
  175. * smaphore and mutex) shouldn't be called.
  176. *
  177. * @return
  178. * - wakeup alarm in epoch time in millisecond
  179. * - INT64_MAX if there are no alarms
  180. */
  181. int64_t drvRtcGetWakeupTime(void);
  182. OSI_EXTERN_C_END
  183. #endif