stm32f10x_rtc.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /**
  2. ******************************************************************************
  3. * @file stm32f10x_rtc.c
  4. * @author MCD Application Team
  5. * @version V3.3.0
  6. * @date 04/16/2010
  7. * @brief This file provides all the RTC firmware functions.
  8. ******************************************************************************
  9. * @copy
  10. *
  11. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *
  18. * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
  19. */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32f10x_rtc.h"
  22. /** @addtogroup STM32F10x_StdPeriph_Driver
  23. * @{
  24. */
  25. /** @defgroup RTC
  26. * @brief RTC driver modules
  27. * @{
  28. */
  29. /** @defgroup RTC_Private_TypesDefinitions
  30. * @{
  31. */
  32. /**
  33. * @}
  34. */
  35. /** @defgroup RTC_Private_Defines
  36. * @{
  37. */
  38. #define CRL_CNF_Set ((uint16_t)0x0010) /*!< Configuration Flag Enable Mask */
  39. #define CRL_CNF_Reset ((uint16_t)0xFFEF) /*!< Configuration Flag Disable Mask */
  40. #define RTC_LSB_Mask ((uint32_t)0x0000FFFF) /*!< RTC LSB Mask */
  41. #define PRLH_MSB_Mask ((uint32_t)0x000F0000) /*!< RTC Prescaler MSB Mask */
  42. /**
  43. * @}
  44. */
  45. /** @defgroup RTC_Private_Macros
  46. * @{
  47. */
  48. /**
  49. * @}
  50. */
  51. /** @defgroup RTC_Private_Variables
  52. * @{
  53. */
  54. /**
  55. * @}
  56. */
  57. /** @defgroup RTC_Private_FunctionPrototypes
  58. * @{
  59. */
  60. /**
  61. * @}
  62. */
  63. /** @defgroup RTC_Private_Functions
  64. * @{
  65. */
  66. /**
  67. * @brief Enables or disables the specified RTC interrupts.
  68. * @param RTC_IT: specifies the RTC interrupts sources to be enabled or disabled.
  69. * This parameter can be any combination of the following values:
  70. * @arg RTC_IT_OW: Overflow interrupt
  71. * @arg RTC_IT_ALR: Alarm interrupt
  72. * @arg RTC_IT_SEC: Second interrupt
  73. * @param NewState: new state of the specified RTC interrupts.
  74. * This parameter can be: ENABLE or DISABLE.
  75. * @retval None
  76. */
  77. void RTC_ITConfig(uint16_t RTC_IT, FunctionalState NewState)
  78. {
  79. /* Check the parameters */
  80. assert_param(IS_RTC_IT(RTC_IT));
  81. assert_param(IS_FUNCTIONAL_STATE(NewState));
  82. if (NewState != DISABLE)
  83. {
  84. RTC->CRH |= RTC_IT;
  85. }
  86. else
  87. {
  88. RTC->CRH &= (uint16_t)~RTC_IT;
  89. }
  90. }
  91. /**
  92. * @brief Enters the RTC configuration mode.
  93. * @param None
  94. * @retval None
  95. */
  96. void RTC_EnterConfigMode(void)
  97. {
  98. /* Set the CNF flag to enter in the Configuration Mode */
  99. RTC->CRL |= CRL_CNF_Set;
  100. }
  101. /**
  102. * @brief Exits from the RTC configuration mode.
  103. * @param None
  104. * @retval None
  105. */
  106. void RTC_ExitConfigMode(void)
  107. {
  108. /* Reset the CNF flag to exit from the Configuration Mode */
  109. RTC->CRL &= CRL_CNF_Reset;
  110. }
  111. /**
  112. * @brief Gets the RTC counter value.
  113. * @param None
  114. * @retval RTC counter value.
  115. */
  116. uint32_t RTC_GetCounter(void)
  117. {
  118. uint16_t tmp = 0;
  119. tmp = RTC->CNTL;
  120. return (((uint32_t)RTC->CNTH << 16 ) | tmp) ;
  121. }
  122. /**
  123. * @brief Sets the RTC counter value.
  124. * @param CounterValue: RTC counter new value.
  125. * @retval None
  126. */
  127. void RTC_SetCounter(uint32_t CounterValue)
  128. {
  129. RTC_EnterConfigMode();
  130. /* Set RTC COUNTER MSB word */
  131. RTC->CNTH = CounterValue >> 16;
  132. /* Set RTC COUNTER LSB word */
  133. RTC->CNTL = (CounterValue & RTC_LSB_Mask);
  134. RTC_ExitConfigMode();
  135. }
  136. /**
  137. * @brief Sets the RTC prescaler value.
  138. * @param PrescalerValue: RTC prescaler new value.
  139. * @retval None
  140. */
  141. void RTC_SetPrescaler(uint32_t PrescalerValue)
  142. {
  143. /* Check the parameters */
  144. assert_param(IS_RTC_PRESCALER(PrescalerValue));
  145. RTC_EnterConfigMode();
  146. /* Set RTC PRESCALER MSB word */
  147. RTC->PRLH = (PrescalerValue & PRLH_MSB_Mask) >> 16;
  148. /* Set RTC PRESCALER LSB word */
  149. RTC->PRLL = (PrescalerValue & RTC_LSB_Mask);
  150. RTC_ExitConfigMode();
  151. }
  152. /**
  153. * @brief Sets the RTC alarm value.
  154. * @param AlarmValue: RTC alarm new value.
  155. * @retval None
  156. */
  157. void RTC_SetAlarm(uint32_t AlarmValue)
  158. {
  159. RTC_EnterConfigMode();
  160. /* Set the ALARM MSB word */
  161. RTC->ALRH = AlarmValue >> 16;
  162. /* Set the ALARM LSB word */
  163. RTC->ALRL = (AlarmValue & RTC_LSB_Mask);
  164. RTC_ExitConfigMode();
  165. }
  166. /**
  167. * @brief Gets the RTC divider value.
  168. * @param None
  169. * @retval RTC Divider value.
  170. */
  171. uint32_t RTC_GetDivider(void)
  172. {
  173. uint32_t tmp = 0x00;
  174. tmp = ((uint32_t)RTC->DIVH & (uint32_t)0x000F) << 16;
  175. tmp |= RTC->DIVL;
  176. return tmp;
  177. }
  178. /**
  179. * @brief Waits until last write operation on RTC registers has finished.
  180. * @note This function must be called before any write to RTC registers.
  181. * @param None
  182. * @retval None
  183. */
  184. void RTC_WaitForLastTask(void)
  185. {
  186. /* Loop until RTOFF flag is set */
  187. while ((RTC->CRL & RTC_FLAG_RTOFF) == (uint16_t)RESET)
  188. {
  189. }
  190. }
  191. /**
  192. * @brief Waits until the RTC registers (RTC_CNT, RTC_ALR and RTC_PRL)
  193. * are synchronized with RTC APB clock.
  194. * @note This function must be called before any read operation after an APB reset
  195. * or an APB clock stop.
  196. * @param None
  197. * @retval None
  198. */
  199. void RTC_WaitForSynchro(void)
  200. {
  201. /* Clear RSF flag */
  202. RTC->CRL &= (uint16_t)~RTC_FLAG_RSF;
  203. /* Loop until RSF flag is set */
  204. while ((RTC->CRL & RTC_FLAG_RSF) == (uint16_t)RESET)
  205. {
  206. }
  207. }
  208. /**
  209. * @brief Checks whether the specified RTC flag is set or not.
  210. * @param RTC_FLAG: specifies the flag to check.
  211. * This parameter can be one the following values:
  212. * @arg RTC_FLAG_RTOFF: RTC Operation OFF flag
  213. * @arg RTC_FLAG_RSF: Registers Synchronized flag
  214. * @arg RTC_FLAG_OW: Overflow flag
  215. * @arg RTC_FLAG_ALR: Alarm flag
  216. * @arg RTC_FLAG_SEC: Second flag
  217. * @retval The new state of RTC_FLAG (SET or RESET).
  218. */
  219. FlagStatus RTC_GetFlagStatus(uint16_t RTC_FLAG)
  220. {
  221. FlagStatus bitstatus = RESET;
  222. /* Check the parameters */
  223. assert_param(IS_RTC_GET_FLAG(RTC_FLAG));
  224. if ((RTC->CRL & RTC_FLAG) != (uint16_t)RESET)
  225. {
  226. bitstatus = SET;
  227. }
  228. else
  229. {
  230. bitstatus = RESET;
  231. }
  232. return bitstatus;
  233. }
  234. /**
  235. * @brief Clears the RTC’s pending flags.
  236. * @param RTC_FLAG: specifies the flag to clear.
  237. * This parameter can be any combination of the following values:
  238. * @arg RTC_FLAG_RSF: Registers Synchronized flag. This flag is cleared only after
  239. * an APB reset or an APB Clock stop.
  240. * @arg RTC_FLAG_OW: Overflow flag
  241. * @arg RTC_FLAG_ALR: Alarm flag
  242. * @arg RTC_FLAG_SEC: Second flag
  243. * @retval None
  244. */
  245. void RTC_ClearFlag(uint16_t RTC_FLAG)
  246. {
  247. /* Check the parameters */
  248. assert_param(IS_RTC_CLEAR_FLAG(RTC_FLAG));
  249. /* Clear the coressponding RTC flag */
  250. RTC->CRL &= (uint16_t)~RTC_FLAG;
  251. }
  252. /**
  253. * @brief Checks whether the specified RTC interrupt has occured or not.
  254. * @param RTC_IT: specifies the RTC interrupts sources to check.
  255. * This parameter can be one of the following values:
  256. * @arg RTC_IT_OW: Overflow interrupt
  257. * @arg RTC_IT_ALR: Alarm interrupt
  258. * @arg RTC_IT_SEC: Second interrupt
  259. * @retval The new state of the RTC_IT (SET or RESET).
  260. */
  261. ITStatus RTC_GetITStatus(uint16_t RTC_IT)
  262. {
  263. ITStatus bitstatus = RESET;
  264. /* Check the parameters */
  265. assert_param(IS_RTC_GET_IT(RTC_IT));
  266. bitstatus = (ITStatus)(RTC->CRL & RTC_IT);
  267. if (((RTC->CRH & RTC_IT) != (uint16_t)RESET) && (bitstatus != (uint16_t)RESET))
  268. {
  269. bitstatus = SET;
  270. }
  271. else
  272. {
  273. bitstatus = RESET;
  274. }
  275. return bitstatus;
  276. }
  277. /**
  278. * @brief Clears the RTC’s interrupt pending bits.
  279. * @param RTC_IT: specifies the interrupt pending bit to clear.
  280. * This parameter can be any combination of the following values:
  281. * @arg RTC_IT_OW: Overflow interrupt
  282. * @arg RTC_IT_ALR: Alarm interrupt
  283. * @arg RTC_IT_SEC: Second interrupt
  284. * @retval None
  285. */
  286. void RTC_ClearITPendingBit(uint16_t RTC_IT)
  287. {
  288. /* Check the parameters */
  289. assert_param(IS_RTC_IT(RTC_IT));
  290. /* Clear the coressponding RTC pending bit */
  291. RTC->CRL &= (uint16_t)~RTC_IT;
  292. }
  293. /**
  294. * @}
  295. */
  296. /**
  297. * @}
  298. */
  299. /**
  300. * @}
  301. */
  302. /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/