stm32f0xx_hal_rtc_ex.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_hal_rtc_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended RTC HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Real-Time Clock (RTC) Extended peripheral:
  8. * + RTC Timestamp functions
  9. * + RTC Tamper functions
  10. * + RTC Wakeup functions
  11. * + Extended Control functions
  12. * + Extended RTC features functions
  13. *
  14. ******************************************************************************
  15. * @attention
  16. *
  17. * Copyright (c) 2016 STMicroelectronics.
  18. * All rights reserved.
  19. *
  20. * This software is licensed under terms that can be found in the LICENSE file
  21. * in the root directory of this software component.
  22. * If no LICENSE file comes with this software, it is provided AS-IS.
  23. *
  24. ******************************************************************************
  25. @verbatim
  26. ==============================================================================
  27. ##### How to use this driver #####
  28. ==============================================================================
  29. [..]
  30. (+) Enable the RTC domain access.
  31. (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
  32. format using the HAL_RTC_Init() function.
  33. *** RTC Wakeup configuration ***
  34. ================================
  35. [..]
  36. (+) To configure the RTC Wakeup Clock source and Counter use the
  37. HAL_RTCEx_SetWakeUpTimer() function.
  38. You can also configure the RTC Wakeup timer in interrupt mode using the
  39. HAL_RTCEx_SetWakeUpTimer_IT() function.
  40. (+) To read the RTC Wakeup Counter register, use the HAL_RTCEx_GetWakeUpTimer()
  41. function.
  42. This feature is not available on F030x4/x6/x8 nor on F070x6 Value Line
  43. devices.
  44. *** Timestamp configuration ***
  45. ===============================
  46. [..]
  47. (+) To configure the RTC Timestamp use the HAL_RTCEx_SetTimeStamp() function.
  48. You can also configure the RTC Timestamp with interrupt mode using the
  49. HAL_RTCEx_SetTimeStamp_IT() function.
  50. (+) To read the RTC Timestamp Time and Date register, use the
  51. HAL_RTCEx_GetTimeStamp() function.
  52. (+) The Timestamp alternate function is mapped to RTC_AF1 (PC13).
  53. *** Tamper configuration ***
  54. ============================
  55. [..]
  56. (+) To enable the RTC Tamper and configure the Tamper filter count, trigger
  57. Edge or Level according to the Tamper filter value (if equal to 0 Edge
  58. else Level), sampling frequency, precharge or discharge and Pull-UP use
  59. the HAL_RTCEx_SetTamper() function.
  60. You can configure RTC Tamper in interrupt mode using HAL_RTCEx_SetTamper_IT()
  61. function.
  62. (+) The TAMPER1 alternate function is mapped to RTC_AF1 (PC13).
  63. (+) The TAMPER2 alternate function is mapped to RTC_AF2 (PA0).
  64. (+) The TAMPER3 alternate function is mapped to RTC_AF3 (PE6).
  65. Tamper 3 is only available on F071xB, F072xB, F078xx, F091xC, and F098xx
  66. devices.
  67. *** Backup Data Registers configuration ***
  68. ===========================================
  69. [..]
  70. (+) To write to the RTC Backup Data registers, use the HAL_RTCEx_BKUPWrite()
  71. function.
  72. (+) To read the RTC Backup Data registers, use the HAL_RTCEx_BKUPRead()
  73. function.
  74. This feature is not available on F030x6/x8/xC nor on F070x6/xB Value
  75. Line devices.
  76. *** Smooth Digital Calibration configuration ***
  77. ================================================
  78. [..]
  79. (+) RTC frequency can be digitally calibrated with a resolution of about
  80. 0.954 ppm with a range from -487.1 ppm to +488.5 ppm.
  81. The correction of the frequency is performed using a series of small
  82. adjustments (adding and/or subtracting individual RTCCLK pulses).
  83. (+) The smooth digital calibration is performed during a cycle of about 2^20
  84. RTCCLK pulses (or 32 seconds) when the input frequency is 32,768 Hz.
  85. This cycle is maintained by a 20-bit counter clocked by RTCCLK.
  86. (+) The smooth calibration register (RTC_CALR) specifies the number of RTCCLK
  87. clock cycles to be masked during the 32-second cycle.
  88. (+) To configure the RTC Smooth Digital Calibration value and the corresponding
  89. calibration cycle period (32s,16s and 8s) use the HAL_RTCEx_SetSmoothCalib()
  90. function.
  91. @endverbatim
  92. ******************************************************************************
  93. */
  94. /* Includes ------------------------------------------------------------------*/
  95. #include "stm32f0xx_hal.h"
  96. /** @addtogroup STM32F0xx_HAL_Driver
  97. * @{
  98. */
  99. /** @defgroup RTCEx RTCEx
  100. * @brief RTC Extended HAL module driver
  101. * @{
  102. */
  103. #ifdef HAL_RTC_MODULE_ENABLED
  104. /* Private typedef -----------------------------------------------------------*/
  105. /* Private define ------------------------------------------------------------*/
  106. /* Private macro -------------------------------------------------------------*/
  107. /* Private variables ---------------------------------------------------------*/
  108. /* Private function prototypes -----------------------------------------------*/
  109. /* Exported functions --------------------------------------------------------*/
  110. /** @defgroup RTCEx_Exported_Functions RTCEx Exported Functions
  111. * @{
  112. */
  113. /** @defgroup RTCEx_Exported_Functions_Group1 RTC Timestamp and Tamper functions
  114. * @brief RTC Timestamp and Tamper functions
  115. *
  116. @verbatim
  117. ===============================================================================
  118. ##### RTC Timestamp and Tamper functions #####
  119. ===============================================================================
  120. [..] This section provides functions allowing to configure Timestamp feature
  121. @endverbatim
  122. * @{
  123. */
  124. /**
  125. * @brief Sets Timestamp.
  126. * @note This API must be called before enabling the Timestamp feature.
  127. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  128. * the configuration information for RTC.
  129. * @param RTC_TimeStampEdge Specifies the pin edge on which the Timestamp is
  130. * activated.
  131. * This parameter can be one of the following values:
  132. * @arg RTC_TIMESTAMPEDGE_RISING: the Timestamp event occurs on
  133. * the rising edge of the related pin.
  134. * @arg RTC_TIMESTAMPEDGE_FALLING: the Timestamp event occurs on
  135. * the falling edge of the related pin.
  136. * @param RTC_TimeStampPin Specifies the RTC Timestamp Pin.
  137. * This parameter can be one of the following values:
  138. * @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC Timestamp Pin.
  139. * @note Although unused, parameter RTC_TimeStampPin has been kept for portability
  140. * reasons.
  141. * @retval HAL status
  142. */
  143. HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t RTC_TimeStampEdge, uint32_t RTC_TimeStampPin)
  144. {
  145. uint32_t tmpreg = 0U;
  146. /* Check the parameters */
  147. assert_param(IS_TIMESTAMP_EDGE(RTC_TimeStampEdge));
  148. assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
  149. /* Prevent compilation warning due to unused argument(s) if assert_param check
  150. is disabled */
  151. UNUSED(RTC_TimeStampPin);
  152. /* Process Locked */
  153. __HAL_LOCK(hrtc);
  154. /* Change RTC state to BUSY */
  155. hrtc->State = HAL_RTC_STATE_BUSY;
  156. /* Get the RTC_CR register and clear the bits to be configured */
  157. tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  158. /* Configure the Timestamp TSEDGE bit */
  159. tmpreg |= RTC_TimeStampEdge;
  160. /* Disable the write protection for RTC registers */
  161. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  162. /* Copy the desired configuration into the CR register */
  163. hrtc->Instance->CR = (uint32_t)tmpreg;
  164. /* Clear RTC Timestamp flag */
  165. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
  166. /* Clear RTC Timestamp overrun Flag */
  167. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
  168. /* Enable the Timestamp saving */
  169. __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
  170. /* Enable the write protection for RTC registers */
  171. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  172. /* Change RTC state back to READY */
  173. hrtc->State = HAL_RTC_STATE_READY;
  174. /* Process Unlocked */
  175. __HAL_UNLOCK(hrtc);
  176. return HAL_OK;
  177. }
  178. /**
  179. * @brief Sets Timestamp with Interrupt.
  180. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  181. * the configuration information for RTC.
  182. * @note This API must be called before enabling the Timestamp feature.
  183. * @param RTC_TimeStampEdge Specifies the pin edge on which the Timestamp is
  184. * activated.
  185. * This parameter can be one of the following values:
  186. * @arg RTC_TIMESTAMPEDGE_RISING: the Timestamp event occurs on
  187. * the rising edge of the related pin.
  188. * @arg RTC_TIMESTAMPEDGE_FALLING: the Timestamp event occurs on
  189. * the falling edge of the related pin.
  190. * @param RTC_TimeStampPin Specifies the RTC Timestamp Pin.
  191. * This parameter can be one of the following values:
  192. * @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC Timestamp Pin.
  193. * @note Although unused, parameter RTC_TimeStampPin has been kept for portability
  194. * reasons.
  195. * @retval HAL status
  196. */
  197. HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t RTC_TimeStampEdge, uint32_t RTC_TimeStampPin)
  198. {
  199. uint32_t tmpreg = 0U;
  200. /* Check the parameters */
  201. assert_param(IS_TIMESTAMP_EDGE(RTC_TimeStampEdge));
  202. assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
  203. /* Prevent compilation warning due to unused argument(s) if assert_param check
  204. is disabled */
  205. UNUSED(RTC_TimeStampPin);
  206. /* Process Locked */
  207. __HAL_LOCK(hrtc);
  208. /* Change RTC state to BUSY */
  209. hrtc->State = HAL_RTC_STATE_BUSY;
  210. /* Get the RTC_CR register and clear the bits to be configured */
  211. tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  212. /* Configure the Timestamp TSEDGE bit */
  213. tmpreg |= RTC_TimeStampEdge;
  214. /* Disable the write protection for RTC registers */
  215. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  216. /* Copy the desired configuration into the CR register */
  217. hrtc->Instance->CR = (uint32_t)tmpreg;
  218. /* Clear RTC Timestamp flag */
  219. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
  220. /* Clear RTC Timestamp overrun Flag */
  221. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
  222. /* Enable the Timestamp saving */
  223. __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
  224. /* Enable IT Timestamp */
  225. __HAL_RTC_TIMESTAMP_ENABLE_IT(hrtc, RTC_IT_TS);
  226. /* Enable the write protection for RTC registers */
  227. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  228. /* Enable and configure the EXTI line associated to the RTC Timestamp and Tamper interrupts */
  229. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
  230. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
  231. /* Change RTC state back to READY */
  232. hrtc->State = HAL_RTC_STATE_READY;
  233. /* Process Unlocked */
  234. __HAL_UNLOCK(hrtc);
  235. return HAL_OK;
  236. }
  237. /**
  238. * @brief Deactivates Timestamp.
  239. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  240. * the configuration information for RTC.
  241. * @retval HAL status
  242. */
  243. HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
  244. {
  245. uint32_t tmpreg = 0U;
  246. /* Process Locked */
  247. __HAL_LOCK(hrtc);
  248. hrtc->State = HAL_RTC_STATE_BUSY;
  249. /* Disable the write protection for RTC registers */
  250. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  251. /* In case interrupt mode is used, the interrupt source must disabled */
  252. __HAL_RTC_TIMESTAMP_DISABLE_IT(hrtc, RTC_IT_TS);
  253. /* Get the RTC_CR register and clear the bits to be configured */
  254. tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  255. /* Configure the Timestamp TSEDGE and Enable bits */
  256. hrtc->Instance->CR = (uint32_t)tmpreg;
  257. /* Enable the write protection for RTC registers */
  258. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  259. hrtc->State = HAL_RTC_STATE_READY;
  260. /* Process Unlocked */
  261. __HAL_UNLOCK(hrtc);
  262. return HAL_OK;
  263. }
  264. /**
  265. * @brief Gets the RTC Timestamp value.
  266. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  267. * the configuration information for RTC.
  268. * @param sTimeStamp Pointer to Time structure
  269. * @param sTimeStampDate Pointer to Date structure
  270. * @param Format specifies the format of the entered parameters.
  271. * This parameter can be one of the following values:
  272. * @arg RTC_FORMAT_BIN: Binary data format
  273. * @arg RTC_FORMAT_BCD: BCD data format
  274. * @retval HAL status
  275. */
  276. HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTimeStamp, RTC_DateTypeDef *sTimeStampDate, uint32_t Format)
  277. {
  278. uint32_t tmptime = 0U;
  279. uint32_t tmpdate = 0U;
  280. /* Check the parameters */
  281. assert_param(IS_RTC_FORMAT(Format));
  282. /* Get the Timestamp time and date registers values */
  283. tmptime = (uint32_t)(hrtc->Instance->TSTR & RTC_TR_RESERVED_MASK);
  284. tmpdate = (uint32_t)(hrtc->Instance->TSDR & RTC_DR_RESERVED_MASK);
  285. /* Fill the Time structure fields with the read parameters */
  286. sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TSTR_HT | RTC_TSTR_HU)) >> RTC_TSTR_HU_Pos);
  287. sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TSTR_MNT | RTC_TSTR_MNU)) >> RTC_TSTR_MNU_Pos);
  288. sTimeStamp->Seconds = (uint8_t)((tmptime & (RTC_TSTR_ST | RTC_TSTR_SU)) >> RTC_TSTR_SU_Pos);
  289. sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TSTR_PM)) >> RTC_TSTR_PM_Pos);
  290. sTimeStamp->SubSeconds = (uint32_t) hrtc->Instance->TSSSR;
  291. /* Fill the Date structure fields with the read parameters */
  292. sTimeStampDate->Year = 0U;
  293. sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_TSDR_MT | RTC_TSDR_MU)) >> RTC_TSDR_MU_Pos);
  294. sTimeStampDate->Date = (uint8_t)((tmpdate & (RTC_TSDR_DT | RTC_TSDR_DU)) >> RTC_TSDR_DU_Pos);
  295. sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_TSDR_WDU)) >> RTC_TSDR_WDU_Pos);
  296. /* Check the input parameters format */
  297. if (Format == RTC_FORMAT_BIN)
  298. {
  299. /* Convert the Timestamp structure parameters to Binary format */
  300. sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
  301. sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
  302. sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
  303. /* Convert the DateTimeStamp structure parameters to Binary format */
  304. sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
  305. sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
  306. sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
  307. }
  308. /* Clear the Timestamp Flag */
  309. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
  310. return HAL_OK;
  311. }
  312. /**
  313. * @brief Sets Tamper.
  314. * @note By calling this API the tamper global interrupt will be disabled.
  315. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  316. * the configuration information for RTC.
  317. * @param sTamper Pointer to Tamper Structure.
  318. * @retval HAL status
  319. */
  320. HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
  321. {
  322. uint32_t tmpreg = 0U;
  323. /* Check the parameters */
  324. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  325. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  326. assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
  327. assert_param(IS_RTC_TAMPER_FILTER_CONFIG_CORRECT(sTamper->Filter, sTamper->Trigger));
  328. assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
  329. assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  330. assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  331. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
  332. /* Process Locked */
  333. __HAL_LOCK(hrtc);
  334. hrtc->State = HAL_RTC_STATE_BUSY;
  335. /* Copy control register into temporary variable */
  336. tmpreg = hrtc->Instance->TAFCR;
  337. /* Enable selected tamper */
  338. tmpreg |= (sTamper->Tamper);
  339. /* Configure the tamper trigger bit (this bit is just on the right of the
  340. tamper enable bit, hence the one-time right shift before updating it) */
  341. if (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE)
  342. {
  343. /* Set the tamper trigger bit (case of falling edge or high level) */
  344. tmpreg |= (uint32_t)(sTamper->Tamper << 1U);
  345. }
  346. else
  347. {
  348. /* Clear the tamper trigger bit (case of rising edge or low level) */
  349. tmpreg &= (uint32_t)~(sTamper->Tamper << 1U);
  350. }
  351. /* Clear remaining fields before setting them */
  352. tmpreg &= ~(RTC_TAMPERFILTER_MASK | \
  353. RTC_TAMPERSAMPLINGFREQ_RTCCLK_MASK | \
  354. RTC_TAMPERPRECHARGEDURATION_MASK | \
  355. RTC_TAMPER_PULLUP_MASK | \
  356. RTC_TIMESTAMPONTAMPERDETECTION_MASK);
  357. /* Set remaining parameters of desired configuration into temporary variable */
  358. tmpreg |= ((uint32_t)sTamper->Filter | \
  359. (uint32_t)sTamper->SamplingFrequency | \
  360. (uint32_t)sTamper->PrechargeDuration | \
  361. (uint32_t)sTamper->TamperPullUp | \
  362. (uint32_t)sTamper->TimeStampOnTamperDetection);
  363. /* Disable tamper global interrupt in case it is enabled */
  364. tmpreg &= (uint32_t)~RTC_TAFCR_TAMPIE;
  365. /* Copy desired configuration into configuration register */
  366. hrtc->Instance->TAFCR = tmpreg;
  367. hrtc->State = HAL_RTC_STATE_READY;
  368. /* Process Unlocked */
  369. __HAL_UNLOCK(hrtc);
  370. return HAL_OK;
  371. }
  372. /**
  373. * @brief Sets Tamper with interrupt.
  374. * @note By calling this API the tamper global interrupt will be enabled.
  375. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  376. * the configuration information for RTC.
  377. * @param sTamper Pointer to RTC Tamper.
  378. * @retval HAL status
  379. */
  380. HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
  381. {
  382. uint32_t tmpreg = 0U;
  383. /* Check the parameters */
  384. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  385. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  386. assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
  387. assert_param(IS_RTC_TAMPER_FILTER_CONFIG_CORRECT(sTamper->Filter, sTamper->Trigger));
  388. assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
  389. assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  390. assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  391. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
  392. /* Process Locked */
  393. __HAL_LOCK(hrtc);
  394. hrtc->State = HAL_RTC_STATE_BUSY;
  395. /* Copy control register into temporary variable */
  396. tmpreg = hrtc->Instance->TAFCR;
  397. /* Enable selected tamper */
  398. tmpreg |= (sTamper->Tamper);
  399. /* Configure the tamper trigger bit (this bit is just on the right of the
  400. tamper enable bit, hence the one-time right shift before updating it) */
  401. if (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE)
  402. {
  403. /* Set the tamper trigger bit (case of falling edge or high level) */
  404. tmpreg |= (uint32_t)(sTamper->Tamper << 1U);
  405. }
  406. else
  407. {
  408. /* Clear the tamper trigger bit (case of rising edge or low level) */
  409. tmpreg &= (uint32_t)~(sTamper->Tamper << 1U);
  410. }
  411. /* Clear remaining fields before setting them */
  412. tmpreg &= ~(RTC_TAMPERFILTER_MASK | \
  413. RTC_TAMPERSAMPLINGFREQ_RTCCLK_MASK | \
  414. RTC_TAMPERPRECHARGEDURATION_MASK | \
  415. RTC_TAMPER_PULLUP_MASK | \
  416. RTC_TIMESTAMPONTAMPERDETECTION_MASK);
  417. /* Set remaining parameters of desired configuration into temporary variable */
  418. tmpreg |= ((uint32_t)sTamper->Filter | \
  419. (uint32_t)sTamper->SamplingFrequency | \
  420. (uint32_t)sTamper->PrechargeDuration | \
  421. (uint32_t)sTamper->TamperPullUp | \
  422. (uint32_t)sTamper->TimeStampOnTamperDetection);
  423. /* Enable global tamper interrupt */
  424. tmpreg |= (uint32_t)RTC_TAFCR_TAMPIE;
  425. /* Copy desired configuration into configuration register */
  426. hrtc->Instance->TAFCR = tmpreg;
  427. /* Enable and configure the EXTI line associated to the RTC Timestamp and Tamper interrupts */
  428. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
  429. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
  430. hrtc->State = HAL_RTC_STATE_READY;
  431. /* Process Unlocked */
  432. __HAL_UNLOCK(hrtc);
  433. return HAL_OK;
  434. }
  435. /**
  436. * @brief Deactivates Tamper.
  437. * @note The tamper global interrupt bit will remain unchanged.
  438. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  439. * the configuration information for RTC.
  440. * @param Tamper Selected tamper pin.
  441. * This parameter can be any combination of the following values:
  442. * @arg RTC_TAMPER_1: Tamper 1
  443. * @arg RTC_TAMPER_2: Tamper 2
  444. * @arg RTC_TAMPER_3: Tamper 3 (*)
  445. *
  446. * (*) value not applicable to all devices.
  447. * @retval HAL status
  448. */
  449. HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
  450. {
  451. assert_param(IS_RTC_TAMPER(Tamper));
  452. /* Process Locked */
  453. __HAL_LOCK(hrtc);
  454. hrtc->State = HAL_RTC_STATE_BUSY;
  455. /* Disable the selected Tamper pin */
  456. hrtc->Instance->TAFCR &= (uint32_t)~Tamper;
  457. hrtc->State = HAL_RTC_STATE_READY;
  458. /* Process Unlocked */
  459. __HAL_UNLOCK(hrtc);
  460. return HAL_OK;
  461. }
  462. /**
  463. * @brief Handles Timestamp and Tamper interrupt request.
  464. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  465. * the configuration information for RTC.
  466. * @retval None
  467. */
  468. void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
  469. {
  470. /* Clear the EXTI flag associated to the RTC Timestamp and Tamper interrupts */
  471. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
  472. /* Get the Timestamp interrupt source enable status */
  473. if (__HAL_RTC_TIMESTAMP_GET_IT_SOURCE(hrtc, RTC_IT_TS) != 0U)
  474. {
  475. /* Get the pending status of the Timestamp Interrupt */
  476. if (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) != 0U)
  477. {
  478. /* Timestamp callback */
  479. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  480. hrtc->TimeStampEventCallback(hrtc);
  481. #else
  482. HAL_RTCEx_TimeStampEventCallback(hrtc);
  483. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  484. /* Clear the Timestamp interrupt pending bit after returning from callback
  485. as RTC_TSTR and RTC_TSDR registers are cleared when TSF bit is reset */
  486. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
  487. }
  488. }
  489. /* Get the Tamper 1 interrupt source enable status */
  490. if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP) != 0U)
  491. {
  492. /* Get the pending status of the Tamper 1 Interrupt */
  493. if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) != 0U)
  494. {
  495. /* Clear the Tamper interrupt pending bit */
  496. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
  497. /* Tamper callback */
  498. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  499. hrtc->Tamper1EventCallback(hrtc);
  500. #else
  501. HAL_RTCEx_Tamper1EventCallback(hrtc);
  502. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  503. }
  504. }
  505. /* Get the Tamper 2 interrupt source enable status */
  506. if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP) != 0U)
  507. {
  508. /* Get the pending status of the Tamper 2 Interrupt */
  509. if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) != 0U)
  510. {
  511. /* Clear the Tamper interrupt pending bit */
  512. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
  513. /* Tamper callback */
  514. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  515. hrtc->Tamper2EventCallback(hrtc);
  516. #else
  517. HAL_RTCEx_Tamper2EventCallback(hrtc);
  518. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  519. }
  520. }
  521. #if defined(RTC_TAMPER3_SUPPORT)
  522. /* Get the Tamper 3 interrupt source enable status */
  523. if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP) != 0U)
  524. {
  525. /* Get the pending status of the Tamper 3 Interrupt */
  526. if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) != 0U)
  527. {
  528. /* Clear the Tamper interrupt pending bit */
  529. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
  530. /* Tamper callback */
  531. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  532. hrtc->Tamper3EventCallback(hrtc);
  533. #else
  534. HAL_RTCEx_Tamper3EventCallback(hrtc);
  535. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  536. }
  537. }
  538. #endif /* RTC_TAMPER3_SUPPORT */
  539. /* Change RTC state */
  540. hrtc->State = HAL_RTC_STATE_READY;
  541. }
  542. /**
  543. * @brief Timestamp callback.
  544. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  545. * the configuration information for RTC.
  546. * @retval None
  547. */
  548. __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
  549. {
  550. /* Prevent unused argument(s) compilation warning */
  551. UNUSED(hrtc);
  552. /* NOTE: This function should not be modified, when the callback is needed,
  553. the HAL_RTCEx_TimeStampEventCallback could be implemented in the user file
  554. */
  555. }
  556. /**
  557. * @brief Tamper 1 callback.
  558. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  559. * the configuration information for RTC.
  560. * @retval None
  561. */
  562. __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
  563. {
  564. /* Prevent unused argument(s) compilation warning */
  565. UNUSED(hrtc);
  566. /* NOTE: This function should not be modified, when the callback is needed,
  567. the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
  568. */
  569. }
  570. /**
  571. * @brief Tamper 2 callback.
  572. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  573. * the configuration information for RTC.
  574. * @retval None
  575. */
  576. __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
  577. {
  578. /* Prevent unused argument(s) compilation warning */
  579. UNUSED(hrtc);
  580. /* NOTE: This function should not be modified, when the callback is needed,
  581. the HAL_RTCEx_Tamper2EventCallback could be implemented in the user file
  582. */
  583. }
  584. #if defined(RTC_TAMPER3_SUPPORT)
  585. /**
  586. * @brief Tamper 3 callback.
  587. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  588. * the configuration information for RTC.
  589. * @retval None
  590. */
  591. __weak void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef *hrtc)
  592. {
  593. /* Prevent unused argument(s) compilation warning */
  594. UNUSED(hrtc);
  595. /* NOTE: This function should not be modified, when the callback is needed,
  596. the HAL_RTCEx_Tamper3EventCallback could be implemented in the user file
  597. */
  598. }
  599. #endif /* RTC_TAMPER3_SUPPORT */
  600. /**
  601. * @brief Handles Timestamp polling request.
  602. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  603. * the configuration information for RTC.
  604. * @param Timeout Timeout duration
  605. * @retval HAL status
  606. */
  607. HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  608. {
  609. uint32_t tickstart = 0U;
  610. /* Get tick */
  611. tickstart = HAL_GetTick();
  612. while (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == 0U)
  613. {
  614. if (Timeout != HAL_MAX_DELAY)
  615. {
  616. if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  617. {
  618. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  619. return HAL_TIMEOUT;
  620. }
  621. }
  622. if (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != 0U)
  623. {
  624. /* Clear the Timestamp Overrun Flag */
  625. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
  626. /* Change Timestamp state */
  627. hrtc->State = HAL_RTC_STATE_ERROR;
  628. return HAL_ERROR;
  629. }
  630. }
  631. /* Change RTC state */
  632. hrtc->State = HAL_RTC_STATE_READY;
  633. return HAL_OK;
  634. }
  635. /**
  636. * @brief Handles Tamper 1 Polling.
  637. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  638. * the configuration information for RTC.
  639. * @param Timeout Timeout duration
  640. * @retval HAL status
  641. */
  642. HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  643. {
  644. uint32_t tickstart = 0U;
  645. /* Get tick */
  646. tickstart = HAL_GetTick();
  647. /* Get the status of the Interrupt */
  648. while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) == 0U)
  649. {
  650. if (Timeout != HAL_MAX_DELAY)
  651. {
  652. if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  653. {
  654. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  655. return HAL_TIMEOUT;
  656. }
  657. }
  658. }
  659. /* Clear the Tamper Flag */
  660. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
  661. /* Change RTC state */
  662. hrtc->State = HAL_RTC_STATE_READY;
  663. return HAL_OK;
  664. }
  665. /**
  666. * @brief Handles Tamper 2 Polling.
  667. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  668. * the configuration information for RTC.
  669. * @param Timeout Timeout duration
  670. * @retval HAL status
  671. */
  672. HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  673. {
  674. uint32_t tickstart = 0U;
  675. /* Get tick */
  676. tickstart = HAL_GetTick();
  677. /* Get the status of the Interrupt */
  678. while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) == 0U)
  679. {
  680. if (Timeout != HAL_MAX_DELAY)
  681. {
  682. if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  683. {
  684. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  685. return HAL_TIMEOUT;
  686. }
  687. }
  688. }
  689. /* Clear the Tamper Flag */
  690. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
  691. /* Change RTC state */
  692. hrtc->State = HAL_RTC_STATE_READY;
  693. return HAL_OK;
  694. }
  695. #if defined(RTC_TAMPER3_SUPPORT)
  696. /**
  697. * @brief Handles Tamper 3 Polling.
  698. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  699. * the configuration information for RTC.
  700. * @param Timeout Timeout duration
  701. * @retval HAL status
  702. */
  703. HAL_StatusTypeDef HAL_RTCEx_PollForTamper3Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  704. {
  705. uint32_t tickstart = HAL_GetTick();
  706. /* Get the status of the Interrupt */
  707. while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) == 0U)
  708. {
  709. if (Timeout != HAL_MAX_DELAY)
  710. {
  711. if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  712. {
  713. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  714. return HAL_TIMEOUT;
  715. }
  716. }
  717. }
  718. /* Clear the Tamper Flag */
  719. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
  720. /* Change RTC state */
  721. hrtc->State = HAL_RTC_STATE_READY;
  722. return HAL_OK;
  723. }
  724. #endif /* RTC_TAMPER3_SUPPORT */
  725. /**
  726. * @}
  727. */
  728. #if defined(RTC_WAKEUP_SUPPORT)
  729. /** @defgroup RTCEx_Exported_Functions_Group2 RTC Wakeup functions
  730. * @brief RTC Wakeup functions
  731. *
  732. @verbatim
  733. ===============================================================================
  734. ##### RTC Wakeup functions #####
  735. ===============================================================================
  736. [..] This section provides functions allowing to configure Wakeup feature
  737. @endverbatim
  738. * @{
  739. */
  740. /**
  741. * @brief Sets wakeup timer.
  742. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  743. * the configuration information for RTC.
  744. * @param WakeUpCounter Wakeup counter
  745. * @param WakeUpClock Wakeup clock
  746. * @retval HAL status
  747. */
  748. HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
  749. {
  750. uint32_t tickstart = 0U;
  751. /* Check the parameters */
  752. assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
  753. assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
  754. /* Process Locked */
  755. __HAL_LOCK(hrtc);
  756. hrtc->State = HAL_RTC_STATE_BUSY;
  757. /* Disable the write protection for RTC registers */
  758. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  759. /* Check RTC WUTWF flag is reset only when wakeup timer enabled*/
  760. if ((hrtc->Instance->CR & RTC_CR_WUTE) != 0U)
  761. {
  762. tickstart = HAL_GetTick();
  763. /* Wait till RTC WUTWF flag is reset and if timeout is reached exit */
  764. while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) != 0U)
  765. {
  766. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  767. {
  768. /* Enable the write protection for RTC registers */
  769. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  770. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  771. /* Process Unlocked */
  772. __HAL_UNLOCK(hrtc);
  773. return HAL_TIMEOUT;
  774. }
  775. }
  776. }
  777. /* Disable the Wakeup timer */
  778. __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  779. /* Clear the Wakeup flag */
  780. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  781. /* Get tick */
  782. tickstart = HAL_GetTick();
  783. /* Wait till RTC WUTWF flag is set and if timeout is reached exit */
  784. while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
  785. {
  786. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  787. {
  788. /* Enable the write protection for RTC registers */
  789. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  790. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  791. /* Process Unlocked */
  792. __HAL_UNLOCK(hrtc);
  793. return HAL_TIMEOUT;
  794. }
  795. }
  796. /* Clear the Wakeup Timer clock source bits in CR register */
  797. hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
  798. /* Configure the clock source */
  799. hrtc->Instance->CR |= (uint32_t)WakeUpClock;
  800. /* Configure the Wakeup Timer counter */
  801. hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
  802. /* Enable the Wakeup Timer */
  803. __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
  804. /* Enable the write protection for RTC registers */
  805. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  806. hrtc->State = HAL_RTC_STATE_READY;
  807. /* Process Unlocked */
  808. __HAL_UNLOCK(hrtc);
  809. return HAL_OK;
  810. }
  811. /**
  812. * @brief Sets wakeup timer with interrupt.
  813. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  814. * the configuration information for RTC.
  815. * @param WakeUpCounter Wakeup counter
  816. * @param WakeUpClock Wakeup clock
  817. * @retval HAL status
  818. */
  819. HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
  820. {
  821. __IO uint32_t count = RTC_TIMEOUT_VALUE * (SystemCoreClock / 32U / 1000U);
  822. /* Check the parameters */
  823. assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
  824. assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
  825. /* Process Locked */
  826. __HAL_LOCK(hrtc);
  827. hrtc->State = HAL_RTC_STATE_BUSY;
  828. /* Disable the write protection for RTC registers */
  829. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  830. /* Check RTC WUTWF flag is reset only when wakeup timer enabled */
  831. if ((hrtc->Instance->CR & RTC_CR_WUTE) != 0U)
  832. {
  833. /* Wait till RTC WUTWF flag is reset and if timeout is reached exit */
  834. do
  835. {
  836. count = count - 1U;
  837. if (count == 0U)
  838. {
  839. /* Enable the write protection for RTC registers */
  840. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  841. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  842. /* Process Unlocked */
  843. __HAL_UNLOCK(hrtc);
  844. return HAL_TIMEOUT;
  845. }
  846. } while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) != 0U);
  847. }
  848. /* Disable the Wakeup timer */
  849. __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  850. /* Clear the Wakeup flag */
  851. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  852. /* Reload the counter */
  853. count = RTC_TIMEOUT_VALUE * (SystemCoreClock / 32U / 1000U);
  854. /* Wait till RTC WUTWF flag is set and if timeout is reached exit */
  855. do
  856. {
  857. count = count - 1U;
  858. if (count == 0U)
  859. {
  860. /* Enable the write protection for RTC registers */
  861. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  862. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  863. /* Process Unlocked */
  864. __HAL_UNLOCK(hrtc);
  865. return HAL_TIMEOUT;
  866. }
  867. } while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U);
  868. /* Clear the Wakeup Timer clock source bits in CR register */
  869. hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
  870. /* Configure the clock source */
  871. hrtc->Instance->CR |= (uint32_t)WakeUpClock;
  872. /* Configure the Wakeup Timer counter */
  873. hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
  874. /* Enable and configure the EXTI line associated to the RTC Wakeup Timer interrupt */
  875. __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
  876. __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
  877. /* Configure the interrupt in the RTC_CR register */
  878. __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc, RTC_IT_WUT);
  879. /* Enable the Wakeup Timer */
  880. __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
  881. /* Enable the write protection for RTC registers */
  882. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  883. hrtc->State = HAL_RTC_STATE_READY;
  884. /* Process Unlocked */
  885. __HAL_UNLOCK(hrtc);
  886. return HAL_OK;
  887. }
  888. /**
  889. * @brief Deactivates wakeup timer counter.
  890. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  891. * the configuration information for RTC.
  892. * @retval HAL status
  893. */
  894. HAL_StatusTypeDef HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
  895. {
  896. uint32_t tickstart = 0U;
  897. /* Process Locked */
  898. __HAL_LOCK(hrtc);
  899. hrtc->State = HAL_RTC_STATE_BUSY;
  900. /* Disable the write protection for RTC registers */
  901. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  902. /* Disable the Wakeup Timer */
  903. __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  904. /* In case interrupt mode is used, the interrupt source must disabled */
  905. __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc, RTC_IT_WUT);
  906. /* Get tick */
  907. tickstart = HAL_GetTick();
  908. /* Wait till RTC WUTWF flag is set and if timeout is reached exit */
  909. while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
  910. {
  911. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  912. {
  913. /* Enable the write protection for RTC registers */
  914. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  915. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  916. /* Process Unlocked */
  917. __HAL_UNLOCK(hrtc);
  918. return HAL_TIMEOUT;
  919. }
  920. }
  921. /* Enable the write protection for RTC registers */
  922. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  923. hrtc->State = HAL_RTC_STATE_READY;
  924. /* Process Unlocked */
  925. __HAL_UNLOCK(hrtc);
  926. return HAL_OK;
  927. }
  928. /**
  929. * @brief Gets wakeup timer counter.
  930. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  931. * the configuration information for RTC.
  932. * @retval Counter value
  933. */
  934. uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
  935. {
  936. /* Get the counter value */
  937. return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
  938. }
  939. /**
  940. * @brief Handles Wakeup Timer interrupt request.
  941. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  942. * the configuration information for RTC.
  943. * @retval None
  944. */
  945. void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
  946. {
  947. /* Clear the EXTI flag associated to the RTC Wakeup Timer interrupt */
  948. __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
  949. /* Get the Wakeup timer interrupt source enable status */
  950. if (__HAL_RTC_WAKEUPTIMER_GET_IT_SOURCE(hrtc, RTC_IT_WUT) != RESET)
  951. {
  952. /* Get the pending status of the Wakeup timer Interrupt */
  953. if (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) != 0U)
  954. {
  955. /* Clear the Wakeup timer interrupt pending bit */
  956. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  957. /* Wakeup timer callback */
  958. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  959. hrtc->WakeUpTimerEventCallback(hrtc);
  960. #else
  961. HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
  962. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  963. }
  964. }
  965. /* Change RTC state */
  966. hrtc->State = HAL_RTC_STATE_READY;
  967. }
  968. /**
  969. * @brief Wakeup Timer callback.
  970. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  971. * the configuration information for RTC.
  972. * @retval None
  973. */
  974. __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
  975. {
  976. /* Prevent unused argument(s) compilation warning */
  977. UNUSED(hrtc);
  978. /* NOTE: This function should not be modified, when the callback is needed,
  979. the HAL_RTCEx_WakeUpTimerEventCallback could be implemented in the user file
  980. */
  981. }
  982. /**
  983. * @brief Handles Wakeup Timer Polling.
  984. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  985. * the configuration information for RTC.
  986. * @param Timeout Timeout duration
  987. * @retval HAL status
  988. */
  989. HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  990. {
  991. uint32_t tickstart = 0U;
  992. /* Get tick */
  993. tickstart = HAL_GetTick();
  994. while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == 0U)
  995. {
  996. if (Timeout != HAL_MAX_DELAY)
  997. {
  998. if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  999. {
  1000. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1001. return HAL_TIMEOUT;
  1002. }
  1003. }
  1004. }
  1005. /* Clear the Wakeup timer Flag */
  1006. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  1007. /* Change RTC state */
  1008. hrtc->State = HAL_RTC_STATE_READY;
  1009. return HAL_OK;
  1010. }
  1011. /**
  1012. * @}
  1013. */
  1014. #endif /* RTC_WAKEUP_SUPPORT */
  1015. /** @defgroup RTCEx_Exported_Functions_Group3 Extended Peripheral Control functions
  1016. * @brief Extended Peripheral Control functions
  1017. *
  1018. @verbatim
  1019. ===============================================================================
  1020. ##### Extended Peripheral Control functions #####
  1021. ===============================================================================
  1022. [..]
  1023. This subsection provides functions allowing to
  1024. (+) Write a data in a specified RTC Backup data register
  1025. (+) Read a data in a specified RTC Backup data register
  1026. (+) Set the Smooth calibration parameters.
  1027. (+) Configure the Synchronization Shift Control Settings.
  1028. (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1029. (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1030. (+) Enable the RTC reference clock detection.
  1031. (+) Disable the RTC reference clock detection.
  1032. (+) Enable the Bypass Shadow feature.
  1033. (+) Disable the Bypass Shadow feature.
  1034. @endverbatim
  1035. * @{
  1036. */
  1037. #if defined(RTC_BACKUP_SUPPORT)
  1038. /**
  1039. * @brief Writes a data in a specified RTC Backup data register.
  1040. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1041. * the configuration information for RTC.
  1042. * @param BackupRegister RTC Backup data Register number.
  1043. * This parameter can be: RTC_BKP_DRx (where x can be from 0 to 4)
  1044. * to specify the register.
  1045. * @param Data Data to be written in the specified RTC Backup data register.
  1046. * @retval None
  1047. */
  1048. void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
  1049. {
  1050. uint32_t tmp = 0U;
  1051. /* Check the parameters */
  1052. assert_param(IS_RTC_BKP(BackupRegister));
  1053. tmp = (uint32_t) &(hrtc->Instance->BKP0R);
  1054. tmp += (BackupRegister * 4U);
  1055. /* Write the specified register */
  1056. *(__IO uint32_t *)tmp = (uint32_t)Data;
  1057. }
  1058. /**
  1059. * @brief Reads data from the specified RTC Backup data Register.
  1060. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1061. * the configuration information for RTC.
  1062. * @param BackupRegister RTC Backup data Register number.
  1063. * This parameter can be: RTC_BKP_DRx (where x can be from 0 to 4)
  1064. * to specify the register.
  1065. * @retval Read value
  1066. */
  1067. uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
  1068. {
  1069. uint32_t tmp = 0U;
  1070. /* Check the parameters */
  1071. assert_param(IS_RTC_BKP(BackupRegister));
  1072. tmp = (uint32_t) &(hrtc->Instance->BKP0R);
  1073. tmp += (BackupRegister * 4U);
  1074. /* Read the specified register */
  1075. return (*(__IO uint32_t *)tmp);
  1076. }
  1077. #endif /* RTC_BACKUP_SUPPORT */
  1078. /**
  1079. * @brief Sets the Smooth calibration parameters.
  1080. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1081. * the configuration information for RTC.
  1082. * @param SmoothCalibPeriod Select the Smooth Calibration Period.
  1083. * This parameter can be can be one of the following values:
  1084. * @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
  1085. * @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
  1086. * @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
  1087. * @param SmoothCalibPlusPulses Select to Set or reset the CALP bit.
  1088. * This parameter can be one of the following values:
  1089. * @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulse every 2*11 pulses.
  1090. * @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
  1091. * @param SmoothCalibMinusPulsesValue Select the value of CALM[8:0] bits.
  1092. * This parameter can be one any value from 0 to 0x000001FF.
  1093. * @note To deactivate the smooth calibration, the field SmoothCalibPlusPulses
  1094. * must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
  1095. * SmoothCalibMinusPulsesValue must be equal to 0.
  1096. * @retval HAL status
  1097. */
  1098. HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef *hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue)
  1099. {
  1100. uint32_t tickstart = 0U;
  1101. /* Check the parameters */
  1102. assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
  1103. assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
  1104. assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmoothCalibMinusPulsesValue));
  1105. /* Process Locked */
  1106. __HAL_LOCK(hrtc);
  1107. hrtc->State = HAL_RTC_STATE_BUSY;
  1108. /* Disable the write protection for RTC registers */
  1109. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1110. /* check if a calibration is pending*/
  1111. if ((hrtc->Instance->ISR & RTC_ISR_RECALPF) != 0U)
  1112. {
  1113. /* Get tick */
  1114. tickstart = HAL_GetTick();
  1115. /* check if a calibration is pending*/
  1116. while ((hrtc->Instance->ISR & RTC_ISR_RECALPF) != 0U)
  1117. {
  1118. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1119. {
  1120. /* Enable the write protection for RTC registers */
  1121. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1122. /* Change RTC state */
  1123. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1124. /* Process Unlocked */
  1125. __HAL_UNLOCK(hrtc);
  1126. return HAL_TIMEOUT;
  1127. }
  1128. }
  1129. }
  1130. /* Configure the Smooth calibration settings */
  1131. hrtc->Instance->CALR = (uint32_t)((uint32_t)SmoothCalibPeriod | \
  1132. (uint32_t)SmoothCalibPlusPulses | \
  1133. (uint32_t)SmoothCalibMinusPulsesValue);
  1134. /* Enable the write protection for RTC registers */
  1135. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1136. /* Change RTC state */
  1137. hrtc->State = HAL_RTC_STATE_READY;
  1138. /* Process Unlocked */
  1139. __HAL_UNLOCK(hrtc);
  1140. return HAL_OK;
  1141. }
  1142. /**
  1143. * @brief Configures the Synchronization Shift Control Settings.
  1144. * @note When REFCKON is set, firmware must not write to Shift control register.
  1145. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1146. * the configuration information for RTC.
  1147. * @param ShiftAdd1S Select to add or not 1 second to the time calendar.
  1148. * This parameter can be one of the following values:
  1149. * @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
  1150. * @arg RTC_SHIFTADD1S_RESET: No effect.
  1151. * @param ShiftSubFS Select the number of Second Fractions to substitute.
  1152. * This parameter can be one any value from 0 to 0x7FFF.
  1153. * @retval HAL status
  1154. */
  1155. HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef *hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
  1156. {
  1157. uint32_t tickstart = 0U;
  1158. /* Check the parameters */
  1159. assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
  1160. assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
  1161. /* Process Locked */
  1162. __HAL_LOCK(hrtc);
  1163. hrtc->State = HAL_RTC_STATE_BUSY;
  1164. /* Disable the write protection for RTC registers */
  1165. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1166. /* Get tick */
  1167. tickstart = HAL_GetTick();
  1168. /* Wait until the shift is completed */
  1169. while ((hrtc->Instance->ISR & RTC_ISR_SHPF) != 0U)
  1170. {
  1171. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1172. {
  1173. /* Enable the write protection for RTC registers */
  1174. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1175. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1176. /* Process Unlocked */
  1177. __HAL_UNLOCK(hrtc);
  1178. return HAL_TIMEOUT;
  1179. }
  1180. }
  1181. /* Check if the reference clock detection is disabled */
  1182. if ((hrtc->Instance->CR & RTC_CR_REFCKON) == 0U)
  1183. {
  1184. /* Configure the Shift settings */
  1185. hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);
  1186. /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  1187. if ((hrtc->Instance->CR & RTC_CR_BYPSHAD) == 0U)
  1188. {
  1189. if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  1190. {
  1191. /* Enable the write protection for RTC registers */
  1192. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1193. hrtc->State = HAL_RTC_STATE_ERROR;
  1194. /* Process Unlocked */
  1195. __HAL_UNLOCK(hrtc);
  1196. return HAL_ERROR;
  1197. }
  1198. }
  1199. }
  1200. else
  1201. {
  1202. /* Enable the write protection for RTC registers */
  1203. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1204. /* Change RTC state */
  1205. hrtc->State = HAL_RTC_STATE_ERROR;
  1206. /* Process Unlocked */
  1207. __HAL_UNLOCK(hrtc);
  1208. return HAL_ERROR;
  1209. }
  1210. /* Enable the write protection for RTC registers */
  1211. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1212. /* Change RTC state */
  1213. hrtc->State = HAL_RTC_STATE_READY;
  1214. /* Process Unlocked */
  1215. __HAL_UNLOCK(hrtc);
  1216. return HAL_OK;
  1217. }
  1218. /**
  1219. * @brief Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1220. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1221. * the configuration information for RTC.
  1222. * @param CalibOutput Select the Calibration output Selection.
  1223. * This parameter can be one of the following values:
  1224. * @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
  1225. * @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
  1226. * @retval HAL status
  1227. */
  1228. HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef *hrtc, uint32_t CalibOutput)
  1229. {
  1230. /* Check the parameters */
  1231. assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
  1232. /* Process Locked */
  1233. __HAL_LOCK(hrtc);
  1234. hrtc->State = HAL_RTC_STATE_BUSY;
  1235. /* Disable the write protection for RTC registers */
  1236. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1237. /* Clear flags before config */
  1238. hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;
  1239. /* Configure the RTC_CR register */
  1240. hrtc->Instance->CR |= (uint32_t)CalibOutput;
  1241. __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
  1242. /* Enable the write protection for RTC registers */
  1243. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1244. /* Change RTC state */
  1245. hrtc->State = HAL_RTC_STATE_READY;
  1246. /* Process Unlocked */
  1247. __HAL_UNLOCK(hrtc);
  1248. return HAL_OK;
  1249. }
  1250. /**
  1251. * @brief Deactivates the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1252. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1253. * the configuration information for RTC.
  1254. * @retval HAL status
  1255. */
  1256. HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef *hrtc)
  1257. {
  1258. /* Process Locked */
  1259. __HAL_LOCK(hrtc);
  1260. hrtc->State = HAL_RTC_STATE_BUSY;
  1261. /* Disable the write protection for RTC registers */
  1262. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1263. __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
  1264. /* Enable the write protection for RTC registers */
  1265. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1266. /* Change RTC state */
  1267. hrtc->State = HAL_RTC_STATE_READY;
  1268. /* Process Unlocked */
  1269. __HAL_UNLOCK(hrtc);
  1270. return HAL_OK;
  1271. }
  1272. /**
  1273. * @brief Enables the RTC reference clock detection.
  1274. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1275. * the configuration information for RTC.
  1276. * @retval HAL status
  1277. */
  1278. HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef *hrtc)
  1279. {
  1280. HAL_StatusTypeDef status;
  1281. /* Process Locked */
  1282. __HAL_LOCK(hrtc);
  1283. hrtc->State = HAL_RTC_STATE_BUSY;
  1284. /* Disable the write protection for RTC registers */
  1285. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1286. /* Enter Initialization mode */
  1287. status = RTC_EnterInitMode(hrtc);
  1288. if (status == HAL_OK)
  1289. {
  1290. /* Enable the reference clock detection */
  1291. __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);
  1292. /* Exit Initialization mode */
  1293. status = RTC_ExitInitMode(hrtc);
  1294. }
  1295. if (status == HAL_OK)
  1296. {
  1297. hrtc->State = HAL_RTC_STATE_READY;
  1298. }
  1299. /* Enable the write protection for RTC registers */
  1300. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1301. /* Process Unlocked */
  1302. __HAL_UNLOCK(hrtc);
  1303. return status;
  1304. }
  1305. /**
  1306. * @brief Disable the RTC reference clock detection.
  1307. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1308. * the configuration information for RTC.
  1309. * @retval HAL status
  1310. */
  1311. HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef *hrtc)
  1312. {
  1313. HAL_StatusTypeDef status;
  1314. /* Process Locked */
  1315. __HAL_LOCK(hrtc);
  1316. hrtc->State = HAL_RTC_STATE_BUSY;
  1317. /* Disable the write protection for RTC registers */
  1318. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1319. /* Enter Initialization mode */
  1320. status = RTC_EnterInitMode(hrtc);
  1321. if (status == HAL_OK)
  1322. {
  1323. /* Disable the reference clock detection */
  1324. __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
  1325. /* Exit Initialization mode */
  1326. status = RTC_ExitInitMode(hrtc);
  1327. }
  1328. if (status == HAL_OK)
  1329. {
  1330. hrtc->State = HAL_RTC_STATE_READY;
  1331. }
  1332. /* Enable the write protection for RTC registers */
  1333. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1334. /* Process Unlocked */
  1335. __HAL_UNLOCK(hrtc);
  1336. return status;
  1337. }
  1338. /**
  1339. * @brief Enables the Bypass Shadow feature.
  1340. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1341. * the configuration information for RTC.
  1342. * @note When the Bypass Shadow is enabled the calendar value are taken
  1343. * directly from the Calendar counter.
  1344. * @retval HAL status
  1345. */
  1346. HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef *hrtc)
  1347. {
  1348. /* Process Locked */
  1349. __HAL_LOCK(hrtc);
  1350. hrtc->State = HAL_RTC_STATE_BUSY;
  1351. /* Disable the write protection for RTC registers */
  1352. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1353. /* Set the BYPSHAD bit */
  1354. hrtc->Instance->CR |= (uint8_t)RTC_CR_BYPSHAD;
  1355. /* Enable the write protection for RTC registers */
  1356. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1357. /* Change RTC state */
  1358. hrtc->State = HAL_RTC_STATE_READY;
  1359. /* Process Unlocked */
  1360. __HAL_UNLOCK(hrtc);
  1361. return HAL_OK;
  1362. }
  1363. /**
  1364. * @brief Disables the Bypass Shadow feature.
  1365. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1366. * the configuration information for RTC.
  1367. * @note When the Bypass Shadow is enabled the calendar value are taken
  1368. * directly from the Calendar counter.
  1369. * @retval HAL status
  1370. */
  1371. HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef *hrtc)
  1372. {
  1373. /* Process Locked */
  1374. __HAL_LOCK(hrtc);
  1375. hrtc->State = HAL_RTC_STATE_BUSY;
  1376. /* Disable the write protection for RTC registers */
  1377. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1378. /* Reset the BYPSHAD bit */
  1379. hrtc->Instance->CR &= (uint8_t)~RTC_CR_BYPSHAD;
  1380. /* Enable the write protection for RTC registers */
  1381. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1382. /* Change RTC state */
  1383. hrtc->State = HAL_RTC_STATE_READY;
  1384. /* Process Unlocked */
  1385. __HAL_UNLOCK(hrtc);
  1386. return HAL_OK;
  1387. }
  1388. /**
  1389. * @}
  1390. */
  1391. /**
  1392. * @}
  1393. */
  1394. #endif /* HAL_RTC_MODULE_ENABLED */
  1395. /**
  1396. * @}
  1397. */
  1398. /**
  1399. * @}
  1400. */