You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32l1xx_hal_rtc_ex.c 60KB

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