rtc.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /******************************************************************************
  2. *
  3. * @brief Real-ETMe counter (RTC) driver head file.
  4. *
  5. ******************************************************************************/
  6. #ifndef RTC_H_
  7. #define RTC_H_
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. /******************************************************************************
  12. * Includes
  13. ******************************************************************************/
  14. /******************************************************************************
  15. * Constants
  16. ******************************************************************************/
  17. /******************************************************************************
  18. * Macros
  19. ******************************************************************************/
  20. /******************************************************************************
  21. * RTC control bit definition
  22. *
  23. *//*! @addtogroup rtc_controlbit
  24. * @{
  25. *******************************************************************************/
  26. #define RTC_OUTPUT_ENABLE 1 /*!< enable RTCO pin */
  27. #define RTC_INTERRUPT_ENABLE 1 /*!< enable RTC interrupt */
  28. #define RTC_CLKSRC_EXTERNAL 0 /*!< select external clock as RTC clock source */
  29. #define RTC_CLKSRC_1KHZ 1 /*!< select LPO as RTC clock source */
  30. #define RTC_CLKSRC_IREF 2 /*!< select internal reference clock as RTC clock source */
  31. #define RTC_CLKSRC_BUS 3 /*!< select bus clock as RTC clock source */
  32. #define RTC_CLK_PRESCALER_128 1 /*!< presalcer is 1 or 128 according to RTCLKS bits */
  33. #define RTC_CLK_PRESCALER_256 2 /*!< presalcer is 2 or 256 according to RTCLKS bits */
  34. #define RTC_CLK_PRESCALER_512 3 /*!< presalcer is 4 or 512 according to RTCLKS bits */
  35. #define RTC_CLK_PRESCALER_1024 4 /*!< presalcer is 8 or 1024 according to RTCLKS bits */
  36. #define RTC_CLK_PRESCALER_2048 5 /*!< presalcer is 16 or 2048 according to RTCLKS bits */
  37. #define RTC_CLK_PRESCALER_100 6 /*!< presalcer is 32 or 100 according to RTCLKS bits */
  38. #define RTC_CLK_PRESCALER_1000 7 /*!< presalcer is 64 or 1000 according to RTCLKS bits */
  39. /*! @} End of rtc_controlbit */
  40. /******************************************************************************
  41. * Types
  42. ******************************************************************************/
  43. /*
  44. * Callback type
  45. */
  46. /******************************************************************************
  47. * RTC callback function declaration
  48. *
  49. *//*! @addtogroup rtc_callback
  50. * @{
  51. *******************************************************************************/
  52. /*!
  53. * @brief RTC Callback type.
  54. *
  55. */
  56. typedef void (*RTC_CallbackType)(void);
  57. /*! @} End of rtc_callback */
  58. /* RTC configuration structure
  59. */
  60. /*!
  61. * @brief RTC configuration type.
  62. *
  63. */
  64. typedef struct
  65. {
  66. uint16_t bReserved : 4; /*!< reserved */
  67. uint16_t bRTCOut : 1; /*!< 1: RTCO pin is enable, 0: RTCO pin is disable */
  68. uint16_t bReserved1 : 1; /*!< reserved */
  69. uint16_t bInterruptEn : 1; /*!< 1: RTC interrupt is enable, 0: RTC interrupt is disable */
  70. uint16_t bFlag : 1; /*!< 1: RTC flag is set, 0: RTC flag is not set */
  71. uint16_t bClockPresaler : 3; /*!< 1: RTC presclaer, from 0x0 to 0x7 */
  72. uint16_t bReserved2 : 3; /*!< reserved */
  73. uint16_t bClockSource : 2; /*!< RTC clock source selection from 0x0 to 0x3 */
  74. uint16_t u16ModuloValue ; /*!< 16-bit rtc modulo value */
  75. } RTC_ConfigType, *RTC_ConfigPtr;
  76. /******************************************************************************
  77. * Global variables
  78. ******************************************************************************/
  79. /*!
  80. * inline functions
  81. */
  82. /******************************************************************************
  83. * RTC API list
  84. *
  85. *//*! @addtogroup rtc_api_list
  86. * @{
  87. *******************************************************************************/
  88. /*****************************************************************************//*!
  89. *
  90. * @brief enable rtc interrupt.
  91. *
  92. * @param none
  93. *
  94. * @return none
  95. *
  96. * @ Pass/ Fail criteria: none
  97. *****************************************************************************/
  98. __STATIC_INLINE void RTC_EnableInt(void)
  99. {
  100. RTC->SC |= RTC_SC_RTIE_MASK;
  101. }
  102. /*****************************************************************************//*!
  103. *
  104. * @brief disable rtc interrupt.
  105. *
  106. * @param none
  107. *
  108. * @return non
  109. *
  110. * @ Pass/ Fail criteria: none
  111. *****************************************************************************/
  112. __STATIC_INLINE void RTC_DisableInt(void)
  113. {
  114. RTC->SC &= ~RTC_SC_RTIE_MASK;
  115. }
  116. /*****************************************************************************//*!
  117. *
  118. * @brief set rtc modulo value.
  119. *
  120. * @param[in] u16Mod_Value
  121. *
  122. * @return none
  123. *
  124. * @ Pass/ Fail criteria: none
  125. *****************************************************************************/
  126. __STATIC_INLINE void RTC_SetModulo(uint16_t u16Mod_Value)
  127. {
  128. RTC->MOD = u16Mod_Value;
  129. }
  130. /*****************************************************************************//*!
  131. *
  132. * @brief set rtc clock source and presalcer.
  133. *
  134. * @param[in] u16Clock_Number clock source number
  135. * @param[in] u16Presalcer prescaler value
  136. *
  137. * @return none
  138. *
  139. * @ Pass/ Fail criteria: none
  140. *****************************************************************************/
  141. __STATIC_INLINE void RTC_SetClock(uint16_t u16Clock_Number, uint16_t u16Presalcer)
  142. {
  143. uint32_t u32rtc_sc;
  144. u32rtc_sc = RTC->SC;
  145. u32rtc_sc &= ~(RTC_SC_RTCLKS_MASK | RTC_SC_RTCPS_MASK);
  146. u32rtc_sc |= RTC_SC_RTCLKS(u16Clock_Number) | RTC_SC_RTCPS(u16Presalcer);
  147. RTC->SC = u32rtc_sc;
  148. }
  149. /*****************************************************************************//*!
  150. *
  151. * @brief get rtc flag bit.
  152. *
  153. * @param none
  154. *
  155. * @return bflag.
  156. *
  157. * @ Pass/ Fail criteria: none
  158. *****************************************************************************/
  159. __STATIC_INLINE uint8_t RTC_GetFlags(void)
  160. {
  161. uint8_t bflag;
  162. bflag = RTC->SC & RTC_SC_RTIF_MASK;
  163. return bflag;
  164. }
  165. /*****************************************************************************//*!
  166. *
  167. * @brief clear rtc flag bit.
  168. *
  169. * @param none
  170. *
  171. * @return none
  172. *
  173. * @ Pass/ Fail criteria: none
  174. *****************************************************************************/
  175. __STATIC_INLINE void RTC_ClrFlags(void)
  176. {
  177. RTC->SC |= RTC_SC_RTIF_MASK;
  178. }
  179. /******************************************************************************
  180. * Global functions
  181. ******************************************************************************/
  182. void RTC_Init(RTC_ConfigType *pConfig);
  183. void RTC_SetCallback(RTC_CallbackType pfnCallback);
  184. void RTC_DeInit(void);
  185. /*! @} End of rtc_api_list */
  186. #ifdef __cplusplus
  187. }
  188. #endif
  189. #endif /* RTC_H_ */