hal_rtc.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. ////////////////////////////////////////////////////////////////////////////////
  2. /// @file hal_rtc.c
  3. /// @author AE TEAM
  4. /// @brief THIS FILE PROVIDES ALL THE RTC FIRMWARE FUNCTIONS.
  5. ////////////////////////////////////////////////////////////////////////////////
  6. /// @attention
  7. ///
  8. /// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
  9. /// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
  10. /// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
  11. /// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
  12. /// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
  13. /// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
  14. ///
  15. /// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
  16. ////////////////////////////////////////////////////////////////////////////////
  17. // Define to prevent recursive inclusion
  18. #define _HAL_RTC_C_
  19. // Files includes
  20. #include "hal_rtc.h"
  21. ////////////////////////////////////////////////////////////////////////////////
  22. /// @addtogroup MM32_Hardware_Abstract_Layer
  23. /// @{
  24. ////////////////////////////////////////////////////////////////////////////////
  25. /// @addtogroup RTC_HAL
  26. /// @{
  27. ////////////////////////////////////////////////////////////////////////////////
  28. /// @addtogroup RTC_Exported_Functions
  29. /// @{
  30. ////////////////////////////////////////////////////////////////////////////////
  31. /// @brief Enables or disables the specified RTC interrupts.
  32. /// @param it: specifies the RTC interrupts sources to be enabled or
  33. /// disabled.
  34. /// This parameter can be any combination of the following values:
  35. /// @arg RTC_IT_OW: Overflow interrupt
  36. /// @arg RTC_IT_ALR: Alarm interrupt
  37. /// @arg RTC_IT_SEC: Second interrupt
  38. /// @param state: new state of the specified RTC interrupts.
  39. /// This parameter can be: ENABLE or DISABLE.
  40. /// @retval None.
  41. ////////////////////////////////////////////////////////////////////////////////
  42. void RTC_ITConfig(RTC_IT_TypeDef it, FunctionalState state)
  43. {
  44. (state == ENABLE) ? (RTC->CR |= it) : (RTC->CR &= (u16)~it);
  45. // RTC_WaitForLastTask();
  46. }
  47. ////////////////////////////////////////////////////////////////////////////////
  48. /// @brief Enters the RTC configuration mode.
  49. /// @param None.
  50. /// @retval None.
  51. ////////////////////////////////////////////////////////////////////////////////
  52. void RTC_EnterConfigMode(void)
  53. {
  54. // PWR->CR |= PWR_CR_DBP;
  55. RTC->CSR |= RTC_CSR_CNF;
  56. }
  57. ////////////////////////////////////////////////////////////////////////////////
  58. /// @brief Exits from the RTC configuration mode.
  59. /// @param None.
  60. /// @retval None.
  61. ////////////////////////////////////////////////////////////////////////////////
  62. void RTC_ExitConfigMode(void)
  63. {
  64. RTC->CSR &= ~RTC_CSR_CNF;
  65. while (!(RTC->CSR & RTC_CSR_RTOFF));
  66. // PWR->CR &= ~PWR_CR_DBP;
  67. }
  68. ////////////////////////////////////////////////////////////////////////////////
  69. /// @brief Gets the RTC counter value.
  70. /// @param None.
  71. /// @retval RTC counter value.
  72. ////////////////////////////////////////////////////////////////////////////////
  73. u32 RTC_GetCounter(void)
  74. {
  75. u32 dat = RTC->CNTH << 16;
  76. return (RTC->CNTL | dat);
  77. }
  78. ////////////////////////////////////////////////////////////////////////////////
  79. /// @brief Sets the RTC counter value.
  80. /// @param count: RTC counter new value.
  81. /// @retval None.
  82. ////////////////////////////////////////////////////////////////////////////////
  83. void RTC_SetCounter(u32 count)
  84. {
  85. RTC_EnterConfigMode();//RTC->CSR |= RTC_CSR_CNF;
  86. RTC->CNTH = count >> 16;
  87. RTC->CNTL = count;
  88. RTC_ExitConfigMode();//RTC->CSR &= ~RTC_CSR_CNF;
  89. // while (!(RTC->CSR & RTC_CSR_RTOFF));
  90. }
  91. ////////////////////////////////////////////////////////////////////////////////
  92. /// @brief Sets the RTC prescaler value.
  93. /// @param prescaler: RTC prescaler new value.
  94. /// @retval None.
  95. ////////////////////////////////////////////////////////////////////////////////
  96. void RTC_SetPrescaler(u32 prescaler)
  97. {
  98. RTC_EnterConfigMode();//RTC->CSR |= RTC_CSR_CNF;
  99. RTC->PRLH = prescaler >> 16;
  100. RTC->PRLL = prescaler;
  101. RTC_ExitConfigMode();//RTC->CSR &= ~RTC_CSR_CNF;
  102. // while (!(RTC->CSR & RTC_CSR_RTOFF));
  103. }
  104. ////////////////////////////////////////////////////////////////////////////////
  105. /// @brief Sets the RTC alarm value.
  106. /// @param alarm: RTC alarm new value.
  107. /// @retval None.
  108. ////////////////////////////////////////////////////////////////////////////////
  109. void RTC_SetAlarm(u32 alarm)
  110. {
  111. RTC_EnterConfigMode();//RTC->CSR |= RTC_CSR_CNF;
  112. RTC->ALRH = alarm >> 16;
  113. RTC->ALRL = alarm;
  114. RTC_ExitConfigMode();//RTC->CSR &= ~RTC_CSR_CNF;
  115. // while (!(RTC->CSR & RTC_CSR_RTOFF));
  116. }
  117. ////////////////////////////////////////////////////////////////////////////////
  118. /// @brief Gets the RTC divider value.
  119. /// @param None.
  120. /// @retval RTC Divider value.
  121. ////////////////////////////////////////////////////////////////////////////////
  122. u32 RTC_GetDivider(void)
  123. {
  124. u32 dat = ((u32)(RTC->DIVH & RTC_DIVH_DIV) << 16);
  125. return (RTC->DIVL | dat);
  126. }
  127. ////////////////////////////////////////////////////////////////////////////////
  128. /// @brief Waits until last write operation on RTC registers has finished.
  129. /// @note This function must be called before any write to RTC registers.
  130. /// @param None.
  131. /// @retval None.
  132. ////////////////////////////////////////////////////////////////////////////////
  133. void RTC_WaitForLastTask(void)
  134. {
  135. while (!(RTC->CSR & RTC_CSR_RTOFF));
  136. }
  137. ////////////////////////////////////////////////////////////////////////////////
  138. /// @brief Waits until the RTC registers (RTC_CNT, RTC_ALR and RTC_PRL)
  139. /// are synchronized with RTC APB clock.
  140. /// @note This function must be called before any read operation after an APB
  141. /// reset or an APB clock stop.
  142. /// @param None.
  143. /// @retval None.
  144. ////////////////////////////////////////////////////////////////////////////////
  145. void RTC_WaitForSynchro(void)
  146. {
  147. RTC->CSR &= ~RTC_CSR_RSF;
  148. while (!(RTC->CSR & RTC_CSR_RSF));
  149. }
  150. ////////////////////////////////////////////////////////////////////////////////
  151. /// @brief Checks whether the specified RTC flag is set or not.
  152. /// @param flag: specifies the flag to check.
  153. /// This parameter can be one the following values:
  154. /// @arg RTC_FLAG_RTOFF: RTC Operation OFF flag
  155. /// @arg RTC_FLAG_RSF: Registers Synchronized flag
  156. /// @arg RTC_FLAG_OW: Overflow flag
  157. /// @arg RTC_FLAG_ALR: Alarm flag
  158. /// @arg RTC_FLAG_SEC: Second flag
  159. /// @retval The state of RTC_FLAG (SET or RESET).
  160. /////////////////////////////////////////////////////////////////////////////////
  161. FlagStatus RTC_GetFlagStatus(RTC_FLAG_TypeDef flag)
  162. {
  163. return (FlagStatus)(RTC->CSR & flag);
  164. }
  165. ////////////////////////////////////////////////////////////////////////////////
  166. /// @brief Clears the RTC's pending flags.
  167. /// @param flag: specifies the flag to clear.
  168. /// This parameter can be any combination of the following values:
  169. /// @arg RTC_FLAG_RSF: Registers Synchronized flag. This flag is cleared only
  170. /// after an APB reset or an APB Clock stop.
  171. /// @arg RTC_FLAG_OW: Overflow flag
  172. /// @arg RTC_FLAG_ALR: Alarm flag
  173. /// @arg RTC_FLAG_SEC: Second flag
  174. /// @retval None.
  175. ////////////////////////////////////////////////////////////////////////////////
  176. void RTC_ClearFlag(RTC_FLAG_TypeDef flag)
  177. {
  178. RTC->CSR &= ~flag;
  179. }
  180. ////////////////////////////////////////////////////////////////////////////////
  181. /// @brief Checks whether the specified RTC interrupt has occurred or not.
  182. /// @param it: specifies the RTC interrupts sources to check.
  183. /// This parameter can be one of the following values:
  184. /// @arg RTC_IT_OW: Overflow interrupt
  185. /// @arg RTC_IT_ALR: Alarm interrupt
  186. /// @arg RTC_IT_SEC: Second interrupt
  187. /// @retval The state of the RTC_IT (SET or RESET).
  188. ////////////////////////////////////////////////////////////////////////////////
  189. ITStatus RTC_GetITStatus(RTC_IT_TypeDef it)
  190. {
  191. return (ITStatus)(RTC->CSR & it);
  192. }
  193. ////////////////////////////////////////////////////////////////////////////////
  194. /// @brief Clears the RTC's interrupt pending bits.
  195. /// @param it: specifies the interrupt pending bit to clear.
  196. /// This parameter can be any combination of the following values:
  197. /// @arg RTC_IT_OW: Overflow interrupt
  198. /// @arg RTC_IT_ALR: Alarm interrupt
  199. /// @arg RTC_IT_SEC: Second interrupt
  200. /// @retval None.
  201. ////////////////////////////////////////////////////////////////////////////////
  202. void RTC_ClearITPendingBit(RTC_IT_TypeDef it)
  203. {
  204. // RTC_EnterConfigMode();//RTC->CSR |= RTC_CSR_CNF;
  205. RTC->CSR &= ~it;
  206. // RTC_ExitConfigMode();//RTC->CSR &= ~RTC_CSR_CNF;
  207. }
  208. /// @}
  209. /// @}
  210. /// @}