rtc.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. * @file
  3. * @brief Real Time Clock (RTC) functions and prototypes.
  4. */
  5. /* ****************************************************************************
  6. * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included
  16. * in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
  22. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  23. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. * OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. * Except as contained in this notice, the name of Maxim Integrated
  27. * Products, Inc. shall not be used except as stated in the Maxim Integrated
  28. * Products, Inc. Branding Policy.
  29. *
  30. * The mere transfer of this software does not imply any licenses
  31. * of trade secrets, proprietary technology, copyrights, patents,
  32. * trademarks, maskwork rights, or any other form of intellectual
  33. * property whatsoever. Maxim Integrated Products, Inc. retains all
  34. * ownership rights.
  35. *
  36. *
  37. * $Date: 2019-10-07 11:05:30 -0500 (Mon, 07 Oct 2019) $
  38. * $Revision: 47429 $
  39. *************************************************************************** */
  40. /* Define to prevent redundant inclusion */
  41. #ifndef _RTC_H_
  42. #define _RTC_H_
  43. /* **** Includes **** */
  44. #include <stdint.h>
  45. #include "mxc_config.h"
  46. #include "rtc_regs.h"
  47. #include "mxc_sys.h"
  48. #ifdef __cplusplus
  49. extern "C" {
  50. #endif
  51. /**
  52. * @defgroup rtc RTC
  53. * @ingroup periphlibs
  54. * @{
  55. */
  56. /* **** Definitions **** */
  57. typedef enum {
  58. SQUARE_WAVE_DISABLED, /**< Sq. wave output disabled */
  59. SQUARE_WAVE_ENABLED, /**< Sq. wave output enabled */
  60. } rtc_sqwave_en_t;
  61. typedef enum {
  62. F_1HZ = MXC_S_RTC_CTRL_FT_FREQ1HZ, /**< 1Hz (Compensated) */
  63. F_512HZ = MXC_S_RTC_CTRL_FT_FREQ512HZ, /**< 512Hz (Compensated) */
  64. F_4KHZ = MXC_S_RTC_CTRL_FT_FREQ4KHZ, /**< 4Khz */
  65. F_32KHZ = 32, /**< 32Khz */
  66. } rtc_freq_sel_t;
  67. typedef enum {
  68. NOISE_IMMUNE_MODE = MXC_S_RTC_CTRL_X32KMD_NOISEIMMUNEMODE,
  69. QUIET_MODE = MXC_S_RTC_CTRL_X32KMD_QUIETMODE,
  70. QUIET_STOP_WARMUP_MODE = MXC_S_RTC_CTRL_X32KMD_QUIETINSTOPWITHWARMUP,
  71. QUIET_STOP_NOWARMUP_MODE = MXC_S_RTC_CTRL_X32KMD_QUIETINSTOPNOWARMUP,
  72. } rtc_osc_mode_t;
  73. /**
  74. *@brief Enables Time-of-Day's Alarm Interrupt
  75. *@param rtc pointer to the rtc register structure
  76. *@return #E_SUCCESS=pass
  77. *@return #E_BAD_STATE=fail
  78. *@return #E_BUSY=Fail
  79. */
  80. int RTC_EnableTimeofdayInterrupt(mxc_rtc_regs_t *rtc);
  81. /**
  82. *@brief Disable Time-of-Day's Alarm Interrupt
  83. *@param rtc pointer to the rtc register structure
  84. *@return #E_SUCCESS=pass
  85. *@return #E_BAD_STATE=fail
  86. *@return #E_BUSY=Fail
  87. */
  88. int RTC_DisableTimeofdayInterrupt(mxc_rtc_regs_t *rtc);
  89. /**
  90. *@brief Enables Sub-Second's Alarm Interrupt
  91. *@param rtc pointer to the rtc register structure
  92. *@return #E_SUCCESS=pass
  93. *@return #E_BAD_STATE=fail
  94. *@return #E_BUSY=Fail
  95. */
  96. int RTC_EnableSubsecondInterrupt(mxc_rtc_regs_t *rtc);
  97. /**
  98. *@brief Disable Sub-Second's Alarm Interrupt
  99. *@param rtc pointer to the rtc register structure
  100. *@return #E_SUCCESS=pass
  101. *@return #E_BAD_STATE=fail
  102. *@return #E_BUSY=Fail
  103. */
  104. int RTC_DisableSubsecondInterrupt(mxc_rtc_regs_t *rtc);
  105. /**
  106. *@brief Set Time-of-Day alarm value and enable Interrupt
  107. *@param rtc pointer to the rtc register structure
  108. *@param ras 20-bit value 0-0xFFFFF
  109. *@return #E_SUCCESS=pass
  110. *@return #E_BAD_STATE=fail
  111. *@return #E_BUSY=Fail
  112. */
  113. int RTC_SetTimeofdayAlarm(mxc_rtc_regs_t *rtc, uint32_t ras);
  114. /**
  115. *@brief Set Sub-Second alarm value and enable interrupt,
  116. *@brief this is to be called after the init_rtc() function
  117. *@param rtc pointer to the rtc register structure
  118. *@param rssa 32-bit value 0-0xFFFFFFFF
  119. *@return #E_SUCCESS=pass
  120. *@return #E_BAD_STATE=fail
  121. *@return #E_BUSY=Fail
  122. */
  123. int RTC_SetSubsecondAlarm(mxc_rtc_regs_t *rtc, uint32_t rssa);
  124. /**
  125. *@brief Enable/Start the Real Time Clock
  126. *@param rtc pointer to the rtc register structure
  127. *@return #E_SUCCESS=Pass
  128. *@return #E_BUSY=Fail
  129. */
  130. int RTC_EnableRTCE(mxc_rtc_regs_t *rtc);
  131. /**
  132. *@brief Disable/Stop the Real Time Clock
  133. *@param rtc pointer to the rtc register structure
  134. *@return #E_SUCCESS=Pass
  135. *@return #E_BUSY=Fail
  136. */
  137. int RTC_DisableRTCE(mxc_rtc_regs_t *rtc);
  138. /**
  139. * @brief Initialize the sec and ssec registers and enable RTC
  140. * @param rtc pointer to the rtc register structure
  141. * @param sec set the RTC Sec counter (32-bit)
  142. * @param ssec set the RTC Sub-second counter (8-bit)
  143. * @param sys_cfg The system configuration
  144. * @return #E_SUCCESS=pass
  145. * @return #E_BAD_STATE=fail
  146. */
  147. int RTC_Init(mxc_rtc_regs_t *rtc, uint32_t sec, uint8_t ssec, sys_cfg_rtc_t *sys_cfg);
  148. /**
  149. * @brief Allow generation of Square Wave on the SQW pin
  150. * @param rtc pointer to the rtc register structure
  151. * @param sqe Enable/Disable square wave output
  152. * @param ft Frequency output selection
  153. * @param x32kmd 32KHz Oscillator mode
  154. * @param sys_cfg The system configuration
  155. * @return #E_SUCCESS=Pass
  156. * @return #E_BUSY=Fail
  157. */
  158. int RTC_SquareWave(mxc_rtc_regs_t *rtc, rtc_sqwave_en_t sqe, rtc_freq_sel_t ft,
  159. rtc_osc_mode_t x32kmd, const sys_cfg_rtc_t* sys_cfg);
  160. /**
  161. *@brief Set Trim register value
  162. *@param rtc pointer to the rtc register structure
  163. *@param trm set the RTC Trim (8-bit, +/- 127)
  164. *@return #E_SUCCESS=Pass
  165. *@return #E_BUSY=Fail
  166. */
  167. int RTC_Trim(mxc_rtc_regs_t *rtc, int8_t trm);
  168. /**
  169. *@brief Check if BUSY bit is 0.
  170. *@return #E_SUCCESS=Pass
  171. *@return #E_BUSY=Fail
  172. */
  173. int RTC_CheckBusy(void);
  174. /**
  175. *@brief Gets Interrupt flags.
  176. *@return Interrupts flags that have not been cleared
  177. */
  178. int RTC_GetFlags(void);
  179. /**
  180. *@brief Clear Interrupt flag.
  181. *@param flags the flags that need to be cleared
  182. */
  183. int RTC_ClearFlags(int flags);
  184. /**
  185. *@brief Get SubSecond
  186. *@return Returns subsecond value
  187. */
  188. int RTC_GetSubSecond(void);
  189. /**
  190. * @brief Get Second
  191. * @return returns Second value
  192. */
  193. int RTC_GetSecond(void);
  194. /**
  195. * @brief Read seconds, then subseconds, and finally seconds. If RTC ready flag ever gets cleared during this sequence,
  196. the RTC is in the middle of updating the counts and the user should come back later and try again. If the first
  197. read of the seconds register doesn't match the next read, then a subsecond overflow condition has happened and
  198. another attempt to read the counts should be made.
  199. * @param sec variable that will be changed to hold second value
  200. * @param subsec variable that will be changed to hold Subsecond value
  201. * @return #E_NO_ERROR=Pass
  202. * @return #E_BUSY=Fail
  203. */
  204. int RTC_GetTime(uint32_t* sec, uint32_t* subsec);
  205. /**
  206. *@brief Check if RTC is already running
  207. */
  208. int RTC_IsEnabled(void);
  209. #ifdef __cplusplus
  210. }
  211. #endif
  212. /**@} end of group rtc */
  213. #endif /* _RTC_H_ */