stm32f4xx_hal_rtc_ex.c 58 KB

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