stm32f10x_rtc.c 8.4 KB

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