stm32l4xx_hal_rtc_ex.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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. ******************************************************************************
  15. * @attention
  16. *
  17. * Copyright (c) 2017 STMicroelectronics.
  18. * All rights reserved.
  19. *
  20. * This software is licensed under terms that can be found in the LICENSE file
  21. * in the root directory of this software component.
  22. * If no LICENSE file comes with this software, it is provided AS-IS.
  23. *
  24. ******************************************************************************
  25. @verbatim
  26. ==============================================================================
  27. ##### How to use this driver #####
  28. ==============================================================================
  29. [..]
  30. (+) Enable the RTC domain access.
  31. (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
  32. format using the HAL_RTC_Init() function.
  33. *** RTC Wakeup configuration ***
  34. ================================
  35. [..]
  36. (+) To configure the RTC Wakeup Clock source and Counter use the HAL_RTCEx_SetWakeUpTimer()
  37. function. You can also configure the RTC Wakeup timer with interrupt mode
  38. using the HAL_RTCEx_SetWakeUpTimer_IT() function.
  39. (+) To read the RTC WakeUp Counter register, use the HAL_RTCEx_GetWakeUpTimer()
  40. function.
  41. *** Outputs configuration ***
  42. =============================
  43. [..] The RTC has 2 different outputs:
  44. (+) RTC_ALARM: this output is used to manage the RTC Alarm A, Alarm B
  45. and WaKeUp signals.
  46. To output the selected RTC signal, use the HAL_RTC_Init() function.
  47. (+) RTC_CALIB: this output is 512Hz signal or 1Hz.
  48. To enable the RTC_CALIB, use the HAL_RTCEx_SetCalibrationOutPut() function.
  49. (+) Two pins can be used as RTC_ALARM or RTC_CALIB (PC13, PB2) managed on
  50. the RTC_OR register.
  51. (+) When the RTC_CALIB or RTC_ALARM output is selected, the RTC_OUT pin is
  52. automatically configured in output alternate function.
  53. *** Smooth digital Calibration configuration ***
  54. ================================================
  55. [..]
  56. (+) Configure the RTC Original Digital Calibration Value and the corresponding
  57. calibration cycle period (32s,16s and 8s) using the HAL_RTCEx_SetSmoothCalib()
  58. function.
  59. *** TimeStamp configuration ***
  60. ===============================
  61. [..]
  62. (+) Enable the RTC TimeStamp using the HAL_RTCEx_SetTimeStamp() function.
  63. You can also configure the RTC TimeStamp with interrupt mode using the
  64. HAL_RTCEx_SetTimeStamp_IT() function.
  65. (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTCEx_GetTimeStamp()
  66. function.
  67. *** Internal TimeStamp configuration ***
  68. ===============================
  69. [..]
  70. (+) Enable the RTC internal TimeStamp using the HAL_RTCEx_SetInternalTimeStamp() function.
  71. User has to check internal timestamp occurrence using __HAL_RTC_INTERNAL_TIMESTAMP_GET_FLAG.
  72. (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTCEx_GetTimeStamp()
  73. function.
  74. *** Tamper configuration ***
  75. ============================
  76. [..]
  77. (+) Enable the RTC Tamper and configure the Tamper filter count, trigger Edge
  78. or Level according to the Tamper filter (if equal to 0 Edge else Level)
  79. value, sampling frequency, NoErase, MaskFlag, precharge or discharge and
  80. Pull-UP using the HAL_RTCEx_SetTamper() function. You can configure RTC Tamper
  81. with interrupt mode using HAL_RTCEx_SetTamper_IT() function.
  82. (+) The default configuration of the Tamper erases the backup registers. To avoid
  83. erase, enable the NoErase field on the RTC_TAMPCR register.
  84. (+) STM32L412xx and STM32L422xx only : With new RTC tamper configuration, you have to call HAL_RTC_Init() in order to
  85. perform TAMP base address offset calculation.
  86. (+) STM32L412xx and STM32L422xx only : If you don't intend to have tamper using RTC clock, you can bypass its initialization
  87. by setting ClockEnable inti field to RTC_CLOCK_DISABLE.
  88. (+) STM32L412xx and STM32L422xx only : Enable Internal tamper using HAL_RTCEx_SetInternalTamper. IT mode can be chosen using
  89. setting Interrupt field.
  90. *** Backup Data Registers configuration ***
  91. ===========================================
  92. [..]
  93. (+) To write to the RTC Backup Data registers, use the HAL_RTCEx_BKUPWrite()
  94. function.
  95. (+) To read the RTC Backup Data registers, use the HAL_RTCEx_BKUPRead()
  96. function.
  97. (+) STM32L412xx and STM32L422xx only : Before calling these functions you have to call HAL_RTC_Init() in order to
  98. perform TAMP base address offset calculation.
  99. @endverbatim
  100. ******************************************************************************
  101. */
  102. /* Includes ------------------------------------------------------------------*/
  103. #include "stm32l4xx_hal.h"
  104. /** @addtogroup STM32L4xx_HAL_Driver
  105. * @{
  106. */
  107. /** @addtogroup RTCEx
  108. * @brief RTC Extended HAL module driver
  109. * @{
  110. */
  111. #ifdef HAL_RTC_MODULE_ENABLED
  112. /* Private typedef -----------------------------------------------------------*/
  113. /* Private define ------------------------------------------------------------*/
  114. /* Private macro -------------------------------------------------------------*/
  115. /* Private variables ---------------------------------------------------------*/
  116. /* Private function prototypes -----------------------------------------------*/
  117. /* Exported functions --------------------------------------------------------*/
  118. /** @addtogroup RTCEx_Exported_Functions
  119. * @{
  120. */
  121. /** @addtogroup RTCEx_Exported_Functions_Group1
  122. * @brief RTC TimeStamp and Tamper functions
  123. *
  124. @verbatim
  125. ===============================================================================
  126. ##### RTC TimeStamp and Tamper functions #####
  127. ===============================================================================
  128. [..] This section provides functions allowing to configure TimeStamp feature
  129. @endverbatim
  130. * @{
  131. */
  132. /**
  133. * @brief Set TimeStamp.
  134. * @note This API must be called before enabling the TimeStamp feature.
  135. * @param hrtc RTC handle
  136. * @param TimeStampEdge Specifies the pin edge on which the TimeStamp is
  137. * activated.
  138. * This parameter can be one of the following values:
  139. * @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
  140. * rising edge of the related pin.
  141. * @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
  142. * falling edge of the related pin.
  143. * @param RTC_TimeStampPin specifies the RTC TimeStamp Pin.
  144. * This parameter can be one of the following values:
  145. * @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
  146. * The RTC TimeStamp Pin is per default PC13, but for reasons of
  147. * compatibility, this parameter is required.
  148. * @retval HAL status
  149. */
  150. HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
  151. {
  152. uint32_t tmpreg;
  153. /* Check the parameters */
  154. assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
  155. assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
  156. /* Prevent unused argument(s) compilation warning if no assert_param check */
  157. UNUSED(RTC_TimeStampPin);
  158. /* Process Locked */
  159. __HAL_LOCK(hrtc);
  160. hrtc->State = HAL_RTC_STATE_BUSY;
  161. /* Get the RTC_CR register and clear the bits to be configured */
  162. tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  163. tmpreg |= TimeStampEdge;
  164. /* Disable the write protection for RTC registers */
  165. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  166. /* Configure the Time Stamp TSEDGE and Enable bits */
  167. hrtc->Instance->CR = (uint32_t)tmpreg;
  168. __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
  169. /* Enable the write protection for RTC registers */
  170. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  171. /* Change RTC state */
  172. hrtc->State = HAL_RTC_STATE_READY;
  173. /* Process Unlocked */
  174. __HAL_UNLOCK(hrtc);
  175. return HAL_OK;
  176. }
  177. /**
  178. * @brief Set TimeStamp with Interrupt.
  179. * @note This API must be called before enabling the TimeStamp feature.
  180. * @param hrtc RTC handle
  181. * @param TimeStampEdge Specifies the pin edge on which the TimeStamp is
  182. * activated.
  183. * This parameter can be one of the following values:
  184. * @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
  185. * rising edge of the related pin.
  186. * @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
  187. * falling edge of the related pin.
  188. * @param RTC_TimeStampPin Specifies the RTC TimeStamp Pin.
  189. * This parameter can be one of the following values:
  190. * @arg RTC_TIMESTAMPPIN_DEFAULT: PC13 is selected as RTC TimeStamp Pin.
  191. * The RTC TimeStamp Pin is per default PC13, but for reasons of
  192. * compatibility, this parameter is required.
  193. * @retval HAL status
  194. */
  195. HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge, uint32_t RTC_TimeStampPin)
  196. {
  197. uint32_t tmpreg;
  198. /* Check the parameters */
  199. assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
  200. assert_param(IS_RTC_TIMESTAMP_PIN(RTC_TimeStampPin));
  201. /* Prevent unused argument(s) compilation warning if no assert_param check */
  202. UNUSED(RTC_TimeStampPin);
  203. /* Process Locked */
  204. __HAL_LOCK(hrtc);
  205. hrtc->State = HAL_RTC_STATE_BUSY;
  206. /* Get the RTC_CR register and clear the bits to be configured */
  207. tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  208. tmpreg |= TimeStampEdge;
  209. /* Disable the write protection for RTC registers */
  210. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  211. /* Configure the Time Stamp TSEDGE and Enable bits */
  212. hrtc->Instance->CR = (uint32_t)tmpreg;
  213. __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
  214. /* Enable IT timestamp */
  215. __HAL_RTC_TIMESTAMP_ENABLE_IT(hrtc, RTC_IT_TS);
  216. /* RTC timestamp Interrupt Configuration: EXTI configuration */
  217. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
  218. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
  219. /* Enable the write protection for RTC registers */
  220. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  221. hrtc->State = HAL_RTC_STATE_READY;
  222. /* Process Unlocked */
  223. __HAL_UNLOCK(hrtc);
  224. return HAL_OK;
  225. }
  226. /**
  227. * @brief Deactivate TimeStamp.
  228. * @param hrtc RTC handle
  229. * @retval HAL status
  230. */
  231. HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
  232. {
  233. uint32_t tmpreg;
  234. /* Process Locked */
  235. __HAL_LOCK(hrtc);
  236. hrtc->State = HAL_RTC_STATE_BUSY;
  237. /* Disable the write protection for RTC registers */
  238. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  239. /* In case of interrupt mode is used, the interrupt source must disabled */
  240. __HAL_RTC_TIMESTAMP_DISABLE_IT(hrtc, RTC_IT_TS);
  241. /* Get the RTC_CR register and clear the bits to be configured */
  242. tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  243. /* Configure the Time Stamp TSEDGE and Enable bits */
  244. hrtc->Instance->CR = (uint32_t)tmpreg;
  245. /* Enable the write protection for RTC registers */
  246. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  247. hrtc->State = HAL_RTC_STATE_READY;
  248. /* Process Unlocked */
  249. __HAL_UNLOCK(hrtc);
  250. return HAL_OK;
  251. }
  252. /**
  253. * @brief Set Internal TimeStamp.
  254. * @note This API must be called before enabling the internal TimeStamp feature.
  255. * @param hrtc RTC handle
  256. * @retval HAL status
  257. */
  258. HAL_StatusTypeDef HAL_RTCEx_SetInternalTimeStamp(RTC_HandleTypeDef *hrtc)
  259. {
  260. /* Process Locked */
  261. __HAL_LOCK(hrtc);
  262. hrtc->State = HAL_RTC_STATE_BUSY;
  263. /* Disable the write protection for RTC registers */
  264. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  265. /* Configure the internal Time Stamp Enable bits */
  266. __HAL_RTC_INTERNAL_TIMESTAMP_ENABLE(hrtc);
  267. /* Enable the write protection for RTC registers */
  268. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  269. /* Change RTC state */
  270. hrtc->State = HAL_RTC_STATE_READY;
  271. /* Process Unlocked */
  272. __HAL_UNLOCK(hrtc);
  273. return HAL_OK;
  274. }
  275. /**
  276. * @brief Deactivate Internal TimeStamp.
  277. * @param hrtc RTC handle
  278. * @retval HAL status
  279. */
  280. HAL_StatusTypeDef HAL_RTCEx_DeactivateInternalTimeStamp(RTC_HandleTypeDef *hrtc)
  281. {
  282. /* Process Locked */
  283. __HAL_LOCK(hrtc);
  284. hrtc->State = HAL_RTC_STATE_BUSY;
  285. /* Disable the write protection for RTC registers */
  286. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  287. /* Configure the internal Time Stamp Enable bits */
  288. __HAL_RTC_INTERNAL_TIMESTAMP_DISABLE(hrtc);
  289. /* Enable the write protection for RTC registers */
  290. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  291. hrtc->State = HAL_RTC_STATE_READY;
  292. /* Process Unlocked */
  293. __HAL_UNLOCK(hrtc);
  294. return HAL_OK;
  295. }
  296. #if defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  297. /**
  298. * @brief Get the RTC TimeStamp value.
  299. * @param hrtc RTC handle
  300. * @param sTimeStamp Pointer to Time structure
  301. * if BinMode = RTC_BINARY_ONLY, sTimeStamp->SubSeconds only is used
  302. * @param sTimeStampDate Pointer to Date structure
  303. * if BinMode = RTC_BINARY_ONLY, this parameter is not used.
  304. * @param Format specifies the format of the entered parameters.
  305. * if BinMode = RTC_BINARY_ONLY, this parameter is not used
  306. * else this parameter can be one of the following values
  307. * @arg RTC_FORMAT_BIN: Binary data format
  308. * @arg RTC_FORMAT_BCD: BCD data format
  309. * @retval HAL status
  310. */
  311. HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTimeStamp, RTC_DateTypeDef *sTimeStampDate, uint32_t Format)
  312. {
  313. uint32_t tmptime, tmpdate;
  314. UNUSED(hrtc);
  315. sTimeStamp->SubSeconds = READ_REG(RTC->TSSSR);
  316. if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) != RTC_BINARY_ONLY)
  317. {
  318. /* Check the parameters */
  319. assert_param(IS_RTC_FORMAT(Format));
  320. /* Get the TimeStamp time and date registers values */
  321. tmptime = READ_BIT(RTC->TSTR, RTC_TR_RESERVED_MASK);
  322. tmpdate = READ_BIT(RTC->TSDR, RTC_DR_RESERVED_MASK);
  323. /* Fill the Time structure fields with the read parameters */
  324. sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TSTR_HT | RTC_TSTR_HU)) >> RTC_TSTR_HU_Pos);
  325. sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TSTR_MNT | RTC_TSTR_MNU)) >> RTC_TSTR_MNU_Pos);
  326. sTimeStamp->Seconds = (uint8_t)((tmptime & (RTC_TSTR_ST | RTC_TSTR_SU)) >> RTC_TSTR_SU_Pos);
  327. sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TSTR_PM)) >> RTC_TSTR_PM_Pos);
  328. sTimeStamp->SubSeconds = READ_BIT(RTC->TSSSR, RTC_TSSSR_SS);
  329. /* Fill the Date structure fields with the read parameters */
  330. sTimeStampDate->Year = 0U;
  331. sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_TSDR_MT | RTC_TSDR_MU)) >> RTC_TSDR_MU_Pos);
  332. sTimeStampDate->Date = (uint8_t)(tmpdate & (RTC_TSDR_DT | RTC_TSDR_DU));
  333. sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_TSDR_WDU)) >> RTC_TSDR_WDU_Pos);
  334. /* Check the input parameters format */
  335. if (Format == RTC_FORMAT_BIN)
  336. {
  337. /* Convert the TimeStamp structure parameters to Binary format */
  338. sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
  339. sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
  340. sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
  341. /* Convert the DateTimeStamp structure parameters to Binary format */
  342. sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
  343. sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
  344. sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
  345. }
  346. }
  347. /* Clear the TIMESTAMP Flags */
  348. WRITE_REG(RTC->SCR, (RTC_SCR_CITSF | RTC_SCR_CTSF));
  349. return HAL_OK;
  350. }
  351. #else /* #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
  352. /**
  353. * @brief Get the RTC TimeStamp value.
  354. * @param hrtc RTC handle
  355. * @param sTimeStamp Pointer to Time structure
  356. * @param sTimeStampDate Pointer to Date structure
  357. * @param Format specifies the format of the entered parameters.
  358. * This parameter can be one of the following values:
  359. * @arg RTC_FORMAT_BIN: Binary data format
  360. * @arg RTC_FORMAT_BCD: BCD data format
  361. * @retval HAL status
  362. */
  363. HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTimeStamp, RTC_DateTypeDef *sTimeStampDate, uint32_t Format)
  364. {
  365. uint32_t tmptime, tmpdate;
  366. /* Check the parameters */
  367. assert_param(IS_RTC_FORMAT(Format));
  368. /* Get the TimeStamp time and date registers values */
  369. tmptime = (uint32_t)(hrtc->Instance->TSTR & RTC_TR_RESERVED_MASK);
  370. tmpdate = (uint32_t)(hrtc->Instance->TSDR & RTC_DR_RESERVED_MASK);
  371. /* Fill the Time structure fields with the read parameters */
  372. sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TSTR_HT | RTC_TSTR_HU)) >> RTC_TSTR_HU_Pos);
  373. sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TSTR_MNT | RTC_TSTR_MNU)) >> RTC_TSTR_MNU_Pos);
  374. sTimeStamp->Seconds = (uint8_t)((tmptime & (RTC_TSTR_ST | RTC_TSTR_SU)) >> RTC_TSTR_SU_Pos);
  375. sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TSTR_PM)) >> RTC_TSTR_PM_Pos);
  376. sTimeStamp->SubSeconds = (uint32_t) hrtc->Instance->TSSSR;
  377. /* Fill the Date structure fields with the read parameters */
  378. sTimeStampDate->Year = 0U;
  379. sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_TSDR_MT | RTC_TSDR_MU)) >> RTC_TSDR_MU_Pos);
  380. sTimeStampDate->Date = (uint8_t)((tmpdate & (RTC_TSDR_DT | RTC_TSDR_DU)) >> RTC_TSDR_DU_Pos);
  381. sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_TSDR_WDU)) >> RTC_TSDR_WDU_Pos);
  382. /* Check the input parameters format */
  383. if (Format == RTC_FORMAT_BIN)
  384. {
  385. /* Convert the TimeStamp structure parameters to Binary format */
  386. sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
  387. sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
  388. sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
  389. /* Convert the DateTimeStamp structure parameters to Binary format */
  390. sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
  391. sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
  392. sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
  393. }
  394. /* Clear the TIMESTAMP Flags */
  395. __HAL_RTC_INTERNAL_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_ITSF);
  396. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
  397. return HAL_OK;
  398. }
  399. #endif /* #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
  400. /**
  401. * @brief Handle TimeStamp interrupt request.
  402. * @param hrtc RTC handle
  403. * @retval None
  404. */
  405. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  406. void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
  407. {
  408. /* Process TAMP instance pointer */
  409. TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset);
  410. /* Clear the EXTI's Flag for RTC TimeStamp and Tamper */
  411. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
  412. if ((hrtc->Instance->MISR & RTC_MISR_TSMF) != 0u)
  413. {
  414. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  415. /* Call TimeStampEvent registered Callback */
  416. hrtc->TimeStampEventCallback(hrtc);
  417. #else
  418. HAL_RTCEx_TimeStampEventCallback(hrtc);
  419. #endif
  420. /* Not immediately clear flags because the content of RTC_TSTR and RTC_TSDR arecleared when TSF bit is reset.*/
  421. hrtc->Instance->SCR = RTC_SCR_CTSF;
  422. }
  423. /* Get interrupt status */
  424. uint32_t tmp = tamp->MISR;
  425. /* Immediately clear flags */
  426. tamp->SCR = tmp;
  427. #if defined(RTC_TAMPER1_SUPPORT)
  428. /* Check Tamper1 status */
  429. if ((tmp & RTC_TAMPER_1) == RTC_TAMPER_1)
  430. {
  431. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  432. /* Call Tamper 1 Event registered Callback */
  433. hrtc->Tamper1EventCallback(hrtc);
  434. #else
  435. /* Tamper1 callback */
  436. HAL_RTCEx_Tamper1EventCallback(hrtc);
  437. #endif
  438. }
  439. #endif /* RTC_TAMPER1_SUPPORT */
  440. /* Check Tamper2 status */
  441. if ((tmp & RTC_TAMPER_2) == RTC_TAMPER_2)
  442. {
  443. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  444. /* Call Tamper 2 Event registered Callback */
  445. hrtc->Tamper2EventCallback(hrtc);
  446. #else
  447. /* Tamper2 callback */
  448. HAL_RTCEx_Tamper2EventCallback(hrtc);
  449. #endif
  450. }
  451. #if defined(RTC_TAMPER3_SUPPORT)
  452. /* Check Tamper3 status */
  453. if ((tmp & RTC_TAMPER_3) == RTC_TAMPER_3)
  454. {
  455. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  456. /* Call Tamper 3 Event registered Callback */
  457. hrtc->Tamper3EventCallback(hrtc);
  458. #else
  459. /* Tamper3 callback */
  460. HAL_RTCEx_Tamper3EventCallback(hrtc);
  461. #endif
  462. }
  463. #endif /* RTC_TAMPER3_SUPPORT */
  464. /* Change RTC state */
  465. hrtc->State = HAL_RTC_STATE_READY;
  466. }
  467. #else /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
  468. void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
  469. {
  470. /* Clear the EXTI's Flag for RTC TimeStamp and Tamper */
  471. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
  472. /* Get the TimeStamp interrupt source enable status and pending flag status */
  473. if (__HAL_RTC_TIMESTAMP_GET_IT_SOURCE(hrtc, RTC_IT_TS) != 0U)
  474. {
  475. if (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) != 0U)
  476. {
  477. /* TIMESTAMP callback */
  478. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  479. hrtc->TimeStampEventCallback(hrtc);
  480. #else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
  481. HAL_RTCEx_TimeStampEventCallback(hrtc);
  482. #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
  483. /* Clear the TIMESTAMP interrupt pending bit (this will clear timestamp time and date registers) */
  484. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
  485. }
  486. }
  487. #if defined(RTC_TAMPER1_SUPPORT)
  488. /* Get the Tamper1 interrupt source enable status and pending flag status */
  489. if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP1) != 0U)
  490. {
  491. if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) != 0U)
  492. {
  493. /* Clear the Tamper1 interrupt pending bit */
  494. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
  495. /* Tamper1 callback */
  496. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  497. hrtc->Tamper1EventCallback(hrtc);
  498. #else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
  499. HAL_RTCEx_Tamper1EventCallback(hrtc);
  500. #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
  501. }
  502. }
  503. #endif /* RTC_TAMPER1_SUPPORT */
  504. /* Get the Tamper2 interrupt source enable status and pending flag status */
  505. if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP2) != 0U)
  506. {
  507. if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) != 0U)
  508. {
  509. /* Clear the Tamper2 interrupt pending bit */
  510. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
  511. /* Tamper2 callback */
  512. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  513. hrtc->Tamper2EventCallback(hrtc);
  514. #else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
  515. HAL_RTCEx_Tamper2EventCallback(hrtc);
  516. #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
  517. }
  518. }
  519. #if defined(RTC_TAMPER3_SUPPORT)
  520. /* Get the Tamper3 interrupts source enable status */
  521. if (__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP3) != 0U)
  522. {
  523. if (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) != 0U)
  524. {
  525. /* Clear the Tamper3 interrupt pending bit */
  526. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
  527. /* Tamper3 callback */
  528. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  529. hrtc->Tamper3EventCallback(hrtc);
  530. #else
  531. HAL_RTCEx_Tamper3EventCallback(hrtc);
  532. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  533. }
  534. }
  535. #endif /* RTC_TAMPER3_SUPPORT */
  536. /* Change RTC state */
  537. hrtc->State = HAL_RTC_STATE_READY;
  538. }
  539. #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
  540. /**
  541. * @brief TimeStamp callback.
  542. * @param hrtc RTC handle
  543. * @retval None
  544. */
  545. __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
  546. {
  547. /* Prevent unused argument(s) compilation warning */
  548. UNUSED(hrtc);
  549. /* NOTE : This function should not be modified, when the callback is needed,
  550. the HAL_RTCEx_TimeStampEventCallback could be implemented in the user file
  551. */
  552. }
  553. /**
  554. * @brief Handle TimeStamp polling request.
  555. * @param hrtc RTC handle
  556. * @param Timeout Timeout duration
  557. * @retval HAL status
  558. */
  559. HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  560. {
  561. uint32_t tickstart = HAL_GetTick();
  562. while (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == 0U)
  563. {
  564. if (__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != 0U)
  565. {
  566. /* Clear the TIMESTAMP OverRun Flag */
  567. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
  568. /* Change TIMESTAMP state */
  569. hrtc->State = HAL_RTC_STATE_ERROR;
  570. return HAL_ERROR;
  571. }
  572. if (Timeout != HAL_MAX_DELAY)
  573. {
  574. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  575. {
  576. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  577. return HAL_TIMEOUT;
  578. }
  579. }
  580. }
  581. /* Change RTC state */
  582. hrtc->State = HAL_RTC_STATE_READY;
  583. return HAL_OK;
  584. }
  585. /**
  586. * @}
  587. */
  588. /** @addtogroup RTCEx_Exported_Functions_Group2
  589. * @brief RTC Wake-up functions
  590. *
  591. @verbatim
  592. ===============================================================================
  593. ##### RTC Wake-up functions #####
  594. ===============================================================================
  595. [..] This section provides functions allowing to configure Wake-up feature
  596. @endverbatim
  597. * @{
  598. */
  599. /**
  600. * @brief Set wake up timer.
  601. * @param hrtc RTC handle
  602. * @param WakeUpCounter Wake up counter
  603. * @param WakeUpClock Wake up clock
  604. * @retval HAL status
  605. */
  606. HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
  607. {
  608. uint32_t tickstart;
  609. /* Check the parameters */
  610. assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
  611. assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
  612. /* Process Locked */
  613. __HAL_LOCK(hrtc);
  614. hrtc->State = HAL_RTC_STATE_BUSY;
  615. /* Disable the write protection for RTC registers */
  616. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  617. /* Clear WUTE in RTC_CR to disable the wakeup timer */
  618. CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE);
  619. /* Poll WUTWF until it is set in RTC_ICSR to make sure the access to wakeup autoreload
  620. counter and to WUCKSEL[2:0] bits is allowed. This step must be skipped in
  621. calendar initialization mode. */
  622. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  623. if (READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_INITF) == 0U)
  624. #else
  625. if (READ_BIT(hrtc->Instance->ISR, RTC_ISR_INITF) == 0U)
  626. #endif
  627. {
  628. tickstart = HAL_GetTick();
  629. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  630. while (READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_WUTWF) == 0U)
  631. #else
  632. while (READ_BIT(hrtc->Instance->ISR, RTC_ISR_WUTWF) == 0U)
  633. #endif
  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. /* Configure the clock source */
  647. MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL, (uint32_t)WakeUpClock);
  648. /* Configure the Wakeup Timer counter */
  649. WRITE_REG(hrtc->Instance->WUTR, (uint32_t)WakeUpCounter);
  650. /* Enable the Wakeup Timer */
  651. SET_BIT(hrtc->Instance->CR, RTC_CR_WUTE);
  652. /* Enable the write protection for RTC registers */
  653. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  654. hrtc->State = HAL_RTC_STATE_READY;
  655. /* Process Unlocked */
  656. __HAL_UNLOCK(hrtc);
  657. return HAL_OK;
  658. }
  659. /**
  660. * @brief Set wake up timer with interrupt.
  661. * @param hrtc RTC handle
  662. * @param WakeUpCounter Wake up counter
  663. * @param WakeUpClock Wake up clock
  664. * @param WakeUpAutoClr Wake up auto clear value (look at WUTOCLR in reference manual)
  665. * - Only available for STM32L412xx and STM32L422xx
  666. * - No effect if WakeUpAutoClr is set to zero
  667. * - This feature is meaningful in case of Low power mode to avoid any RTC software execution after Wake Up.
  668. * That is why when WakeUpAutoClr is set, EXTI is configured as EVENT instead of Interrupt to avoid useless IRQ handler execution.
  669. * @retval HAL status
  670. */
  671. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  672. HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock, uint32_t WakeUpAutoClr)
  673. #else
  674. HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
  675. #endif
  676. {
  677. uint32_t tickstart;
  678. /* Check the parameters */
  679. assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
  680. assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
  681. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  682. /* (0x0000<=WUTOCLR<=WUT) */
  683. assert_param(WakeUpAutoClr <= WakeUpCounter);
  684. #endif
  685. /* Process Locked */
  686. __HAL_LOCK(hrtc);
  687. hrtc->State = HAL_RTC_STATE_BUSY;
  688. /* Disable the write protection for RTC registers */
  689. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  690. /* Clear WUTE in RTC_CR to disable the wakeup timer */
  691. CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE);
  692. /* Clear flag Wake-Up */
  693. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  694. /* Poll WUTWF until it is set in RTC_ICSR to make sure the access to wakeup autoreload
  695. counter and to WUCKSEL[2:0] bits is allowed. This step must be skipped in
  696. calendar initialization mode. */
  697. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  698. if (READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_INITF) == 0U)
  699. #else
  700. if (READ_BIT(hrtc->Instance->ISR, RTC_ISR_INITF) == 0U)
  701. #endif
  702. {
  703. tickstart = HAL_GetTick();
  704. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  705. while (READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_WUTWF) == 0U)
  706. #else
  707. while (READ_BIT(hrtc->Instance->ISR, RTC_ISR_WUTWF) == 0U)
  708. #endif
  709. {
  710. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  711. {
  712. /* Enable the write protection for RTC registers */
  713. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  714. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  715. /* Process Unlocked */
  716. __HAL_UNLOCK(hrtc);
  717. return HAL_TIMEOUT;
  718. }
  719. }
  720. }
  721. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  722. /* Configure the Wakeup Timer counter and auto clear value */
  723. hrtc->Instance->WUTR = (uint32_t)(WakeUpCounter | (WakeUpAutoClr << RTC_WUTR_WUTOCLR_Pos));
  724. #else
  725. /* Configure the Wakeup Timer counter */
  726. hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
  727. #endif
  728. /* Configure the clock source */
  729. MODIFY_REG(hrtc->Instance->CR, RTC_CR_WUCKSEL, (uint32_t)WakeUpClock);
  730. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  731. /* In case of WUT autoclr, the IRQ handler should not be called */
  732. if (WakeUpAutoClr != 0u)
  733. {
  734. /* RTC WakeUpTimer EXTI Configuration: Event configuration */
  735. __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_EVENT();
  736. }
  737. else
  738. {
  739. /* RTC WakeUpTimer EXTI Configuration: Interrupt configuration */
  740. __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
  741. }
  742. #else /* defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
  743. __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
  744. #endif /* defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
  745. __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
  746. /* Configure the Interrupt in the RTC_CR register */
  747. __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc, RTC_IT_WUT);
  748. /* Enable the Wakeup Timer */
  749. __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
  750. /* Enable the write protection for RTC registers */
  751. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  752. hrtc->State = HAL_RTC_STATE_READY;
  753. /* Process Unlocked */
  754. __HAL_UNLOCK(hrtc);
  755. return HAL_OK;
  756. }
  757. /**
  758. * @brief Deactivate wake up timer counter.
  759. * @param hrtc RTC handle
  760. * @retval HAL status
  761. */
  762. HAL_StatusTypeDef HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
  763. {
  764. uint32_t tickstart;
  765. /* Process Locked */
  766. __HAL_LOCK(hrtc);
  767. hrtc->State = HAL_RTC_STATE_BUSY;
  768. /* Disable the write protection for RTC registers */
  769. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  770. /* Disable the Wakeup Timer */
  771. __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  772. /* In case of interrupt mode is used, the interrupt source must disabled */
  773. __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc, RTC_IT_WUT);
  774. tickstart = HAL_GetTick();
  775. /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
  776. while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == 0U)
  777. {
  778. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  779. {
  780. /* Enable the write protection for RTC registers */
  781. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  782. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  783. /* Process Unlocked */
  784. __HAL_UNLOCK(hrtc);
  785. return HAL_TIMEOUT;
  786. }
  787. }
  788. /* Enable the write protection for RTC registers */
  789. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  790. hrtc->State = HAL_RTC_STATE_READY;
  791. /* Process Unlocked */
  792. __HAL_UNLOCK(hrtc);
  793. return HAL_OK;
  794. }
  795. /**
  796. * @brief Get wake up timer counter.
  797. * @param hrtc RTC handle
  798. * @retval Counter value
  799. */
  800. uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
  801. {
  802. /* Get the counter value */
  803. return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
  804. }
  805. /**
  806. * @brief Handle Wake Up Timer interrupt request.
  807. * @param hrtc RTC handle
  808. * @retval None
  809. */
  810. void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
  811. {
  812. /* Clear the EXTI's line Flag for RTC WakeUpTimer */
  813. __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
  814. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  815. if ((hrtc->Instance->MISR & RTC_MISR_WUTMF) != 0u)
  816. {
  817. /* Immediately clear flags */
  818. hrtc->Instance->SCR = RTC_SCR_CWUTF;
  819. #else
  820. /* Get the pending status of the WAKEUPTIMER Interrupt */
  821. if (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) != 0U)
  822. {
  823. /* Clear the WAKEUPTIMER interrupt pending bit */
  824. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  825. #endif
  826. /* WAKEUPTIMER callback */
  827. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  828. /* Call WakeUpTimerEvent registered Callback */
  829. hrtc->WakeUpTimerEventCallback(hrtc);
  830. #else
  831. HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
  832. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  833. }
  834. /* Change RTC state */
  835. hrtc->State = HAL_RTC_STATE_READY;
  836. }
  837. /**
  838. * @brief Wake Up Timer callback.
  839. * @param hrtc RTC handle
  840. * @retval None
  841. */
  842. __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
  843. {
  844. /* Prevent unused argument(s) compilation warning */
  845. UNUSED(hrtc);
  846. /* NOTE : This function should not be modified, when the callback is needed,
  847. the HAL_RTCEx_WakeUpTimerEventCallback could be implemented in the user file
  848. */
  849. }
  850. /**
  851. * @brief Handle Wake Up Timer Polling.
  852. * @param hrtc RTC handle
  853. * @param Timeout Timeout duration
  854. * @retval HAL status
  855. */
  856. HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  857. {
  858. uint32_t tickstart = HAL_GetTick();
  859. while (__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == 0U)
  860. {
  861. if (Timeout != HAL_MAX_DELAY)
  862. {
  863. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  864. {
  865. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  866. return HAL_TIMEOUT;
  867. }
  868. }
  869. }
  870. /* Clear the WAKEUPTIMER Flag */
  871. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  872. /* Change RTC state */
  873. hrtc->State = HAL_RTC_STATE_READY;
  874. return HAL_OK;
  875. }
  876. /**
  877. * @}
  878. */
  879. /** @addtogroup RTCEx_Exported_Functions_Group3
  880. * @brief Extended Peripheral Control functions
  881. *
  882. @verbatim
  883. ===============================================================================
  884. ##### Extended Peripheral Control functions #####
  885. ===============================================================================
  886. [..]
  887. This subsection provides functions allowing to
  888. (+) Write a data in a specified RTC Backup data register
  889. (+) Read a data in a specified RTC Backup data register
  890. (+) Set the Coarse calibration parameters.
  891. (+) Deactivate the Coarse calibration parameters
  892. (+) Set the Smooth calibration parameters.
  893. (+) STM32L412xx and STM32L422xx only : Set Low Power calibration parameter.
  894. (+) Configure the Synchronization Shift Control Settings.
  895. (+) Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  896. (+) Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  897. (+) Enable the RTC reference clock detection.
  898. (+) Disable the RTC reference clock detection.
  899. (+) Enable the Bypass Shadow feature.
  900. (+) Disable the Bypass Shadow feature.
  901. @endverbatim
  902. * @{
  903. */
  904. /**
  905. * @brief Set the Smooth calibration parameters.
  906. * @param hrtc RTC handle
  907. * @param SmoothCalibPeriod Select the Smooth Calibration Period.
  908. * This parameter can be can be one of the following values :
  909. * @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration period is 32s.
  910. * @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration period is 16s.
  911. * @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibration period is 8s.
  912. * @param SmoothCalibPlusPulses Select to Set or reset the CALP bit.
  913. * This parameter can be one of the following values:
  914. * @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK pulse every 2*11 pulses.
  915. * @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
  916. * @param SmoothCalibMinusPulsesValue Select the value of CALM[8:0] bits.
  917. * This parameter can be one any value from 0 to 0x000001FF.
  918. * @note To deactivate the smooth calibration, the field SmoothCalibPlusPulses
  919. * must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
  920. * SmoothCalibMinusPulsesValue must be equal to 0.
  921. * @retval HAL status
  922. */
  923. HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef *hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue)
  924. {
  925. uint32_t tickstart;
  926. /* Check the parameters */
  927. assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
  928. assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
  929. assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmoothCalibMinusPulsesValue));
  930. /* Process Locked */
  931. __HAL_LOCK(hrtc);
  932. hrtc->State = HAL_RTC_STATE_BUSY;
  933. /* Disable the write protection for RTC registers */
  934. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  935. /* check if a calibration is pending*/
  936. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  937. if ((hrtc->Instance->ICSR & RTC_ICSR_RECALPF) != 0U)
  938. #else
  939. if ((hrtc->Instance->ISR & RTC_ISR_RECALPF) != 0U)
  940. #endif
  941. {
  942. tickstart = HAL_GetTick();
  943. /* check if a calibration is pending*/
  944. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  945. while ((hrtc->Instance->ICSR & RTC_ICSR_RECALPF) != 0U)
  946. #else
  947. while ((hrtc->Instance->ISR & RTC_ISR_RECALPF) != 0U)
  948. #endif
  949. {
  950. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  951. {
  952. /* Enable the write protection for RTC registers */
  953. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  954. /* Change RTC state */
  955. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  956. /* Process Unlocked */
  957. __HAL_UNLOCK(hrtc);
  958. return HAL_TIMEOUT;
  959. }
  960. }
  961. }
  962. /* Configure the Smooth calibration settings */
  963. MODIFY_REG(hrtc->Instance->CALR, (RTC_CALR_CALP | RTC_CALR_CALW8 | RTC_CALR_CALW16 | RTC_CALR_CALM), (uint32_t)(SmoothCalibPeriod | SmoothCalibPlusPulses | SmoothCalibMinusPulsesValue));
  964. /* Enable the write protection for RTC registers */
  965. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  966. /* Change RTC state */
  967. hrtc->State = HAL_RTC_STATE_READY;
  968. /* Process Unlocked */
  969. __HAL_UNLOCK(hrtc);
  970. return HAL_OK;
  971. }
  972. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  973. /**
  974. * @brief Select the low power Calibration mode.
  975. * @param hrtc RTC handle
  976. * @param LowPowerCalib Low power Calibration mode.
  977. * This parameter can be can be one of the following values :
  978. * @arg RTC_LPCAL_SET: Low power mode.
  979. * @arg RTC_LPCAL_RESET: High consumption mode.
  980. * @retval HAL status
  981. */
  982. HAL_StatusTypeDef HAL_RTCEx_SetLowPowerCalib(RTC_HandleTypeDef *hrtc, uint32_t LowPowerCalib)
  983. {
  984. /* Check the parameters */
  985. assert_param(IS_RTC_LOW_POWER_CALIB(LowPowerCalib));
  986. /* Process Locked */
  987. __HAL_LOCK(hrtc);
  988. hrtc->State = HAL_RTC_STATE_BUSY;
  989. /* Disable the write protection for RTC registers */
  990. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  991. /* Configure the Smooth calibration settings */
  992. MODIFY_REG(hrtc->Instance->CALR, RTC_CALR_LPCAL, LowPowerCalib);
  993. /* Enable the write protection for RTC registers */
  994. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  995. /* Change RTC state */
  996. hrtc->State = HAL_RTC_STATE_READY;
  997. /* Process Unlocked */
  998. __HAL_UNLOCK(hrtc);
  999. return HAL_OK;
  1000. }
  1001. #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
  1002. /**
  1003. * @brief Configure the Synchronization Shift Control Settings.
  1004. * @note When REFCKON is set, firmware must not write to Shift control register.
  1005. * @param hrtc RTC handle
  1006. * @param ShiftAdd1S Select to add or not 1 second to the time calendar.
  1007. * This parameter can be one of the following values:
  1008. * @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
  1009. * @arg RTC_SHIFTADD1S_RESET: No effect.
  1010. * @param ShiftSubFS Select the number of Second Fractions to substitute.
  1011. * This parameter can be one any value from 0 to 0x7FFF.
  1012. * @retval HAL status
  1013. */
  1014. HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef *hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
  1015. {
  1016. uint32_t tickstart;
  1017. /* Check the parameters */
  1018. assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
  1019. assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
  1020. /* Process Locked */
  1021. __HAL_LOCK(hrtc);
  1022. hrtc->State = HAL_RTC_STATE_BUSY;
  1023. /* Disable the write protection for RTC registers */
  1024. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1025. tickstart = HAL_GetTick();
  1026. /* Wait until the shift is completed*/
  1027. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  1028. while ((hrtc->Instance->ICSR & RTC_ICSR_SHPF) != 0U)
  1029. #else
  1030. while ((hrtc->Instance->ISR & RTC_ISR_SHPF) != 0U)
  1031. #endif
  1032. {
  1033. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1034. {
  1035. /* Enable the write protection for RTC registers */
  1036. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1037. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1038. /* Process Unlocked */
  1039. __HAL_UNLOCK(hrtc);
  1040. return HAL_TIMEOUT;
  1041. }
  1042. }
  1043. /* Check if the reference clock detection is disabled */
  1044. if ((hrtc->Instance->CR & RTC_CR_REFCKON) == 0U)
  1045. {
  1046. /* Configure the Shift settings */
  1047. hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);
  1048. /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  1049. if ((hrtc->Instance->CR & RTC_CR_BYPSHAD) == 0U)
  1050. {
  1051. if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  1052. {
  1053. /* Enable the write protection for RTC registers */
  1054. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1055. hrtc->State = HAL_RTC_STATE_ERROR;
  1056. /* Process Unlocked */
  1057. __HAL_UNLOCK(hrtc);
  1058. return HAL_ERROR;
  1059. }
  1060. }
  1061. }
  1062. else
  1063. {
  1064. /* Enable the write protection for RTC registers */
  1065. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1066. /* Change RTC state */
  1067. hrtc->State = HAL_RTC_STATE_ERROR;
  1068. /* Process Unlocked */
  1069. __HAL_UNLOCK(hrtc);
  1070. return HAL_ERROR;
  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_READY;
  1076. /* Process Unlocked */
  1077. __HAL_UNLOCK(hrtc);
  1078. return HAL_OK;
  1079. }
  1080. /**
  1081. * @brief Configure the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1082. * @param hrtc RTC handle
  1083. * @param CalibOutput Select the Calibration output Selection .
  1084. * This parameter can be one of the following values:
  1085. * @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
  1086. * @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
  1087. * @retval HAL status
  1088. */
  1089. HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef *hrtc, uint32_t CalibOutput)
  1090. {
  1091. /* Check the parameters */
  1092. assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
  1093. /* Process Locked */
  1094. __HAL_LOCK(hrtc);
  1095. hrtc->State = HAL_RTC_STATE_BUSY;
  1096. /* Disable the write protection for RTC registers */
  1097. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1098. /* Clear flags before config */
  1099. hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;
  1100. /* Configure the RTC_CR register */
  1101. hrtc->Instance->CR |= (uint32_t)CalibOutput;
  1102. __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
  1103. /* Enable the write protection for RTC registers */
  1104. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1105. /* Change RTC state */
  1106. hrtc->State = HAL_RTC_STATE_READY;
  1107. /* Process Unlocked */
  1108. __HAL_UNLOCK(hrtc);
  1109. return HAL_OK;
  1110. }
  1111. /**
  1112. * @brief Deactivate the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1113. * @param hrtc RTC handle
  1114. * @retval HAL status
  1115. */
  1116. HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef *hrtc)
  1117. {
  1118. /* Process Locked */
  1119. __HAL_LOCK(hrtc);
  1120. hrtc->State = HAL_RTC_STATE_BUSY;
  1121. /* Disable the write protection for RTC registers */
  1122. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1123. __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
  1124. /* Enable the write protection for RTC registers */
  1125. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1126. /* Change RTC state */
  1127. hrtc->State = HAL_RTC_STATE_READY;
  1128. /* Process Unlocked */
  1129. __HAL_UNLOCK(hrtc);
  1130. return HAL_OK;
  1131. }
  1132. /**
  1133. * @brief Enable the RTC reference clock detection.
  1134. * @param hrtc RTC handle
  1135. * @retval HAL status
  1136. */
  1137. HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef *hrtc)
  1138. {
  1139. HAL_StatusTypeDef status;
  1140. /* Process Locked */
  1141. __HAL_LOCK(hrtc);
  1142. hrtc->State = HAL_RTC_STATE_BUSY;
  1143. /* Disable the write protection for RTC registers */
  1144. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1145. /* Enter Initialization mode */
  1146. status = RTC_EnterInitMode(hrtc);
  1147. if (status == HAL_OK)
  1148. {
  1149. __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);
  1150. /* Exit Initialization mode */
  1151. status = RTC_ExitInitMode(hrtc);
  1152. }
  1153. /* Enable the write protection for RTC registers */
  1154. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1155. if (status == HAL_OK)
  1156. {
  1157. hrtc->State = HAL_RTC_STATE_READY;
  1158. }
  1159. /* Process Unlocked */
  1160. __HAL_UNLOCK(hrtc);
  1161. return status;
  1162. }
  1163. /**
  1164. * @brief Disable the RTC reference clock detection.
  1165. * @param hrtc RTC handle
  1166. * @retval HAL status
  1167. */
  1168. HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef *hrtc)
  1169. {
  1170. HAL_StatusTypeDef status;
  1171. /* Process Locked */
  1172. __HAL_LOCK(hrtc);
  1173. hrtc->State = HAL_RTC_STATE_BUSY;
  1174. /* Disable the write protection for RTC registers */
  1175. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1176. /* Enter Initialization mode */
  1177. status = RTC_EnterInitMode(hrtc);
  1178. if (status == HAL_OK)
  1179. {
  1180. __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
  1181. /* Exit Initialization mode */
  1182. status = RTC_ExitInitMode(hrtc);
  1183. }
  1184. /* Enable the write protection for RTC registers */
  1185. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1186. if (status == HAL_OK)
  1187. {
  1188. hrtc->State = HAL_RTC_STATE_READY;
  1189. }
  1190. /* Process Unlocked */
  1191. __HAL_UNLOCK(hrtc);
  1192. return status;
  1193. }
  1194. /**
  1195. * @brief Enable the Bypass Shadow feature.
  1196. * @note When the Bypass Shadow is enabled the calendar value are taken
  1197. * directly from the Calendar counter.
  1198. * @param hrtc RTC handle
  1199. * @retval HAL status
  1200. */
  1201. HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef *hrtc)
  1202. {
  1203. /* Process Locked */
  1204. __HAL_LOCK(hrtc);
  1205. hrtc->State = HAL_RTC_STATE_BUSY;
  1206. /* Disable the write protection for RTC registers */
  1207. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1208. /* Set the BYPSHAD bit */
  1209. SET_BIT(hrtc->Instance->CR, RTC_CR_BYPSHAD);
  1210. /* Enable the write protection for RTC registers */
  1211. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1212. /* Change RTC state */
  1213. hrtc->State = HAL_RTC_STATE_READY;
  1214. /* Process Unlocked */
  1215. __HAL_UNLOCK(hrtc);
  1216. return HAL_OK;
  1217. }
  1218. /**
  1219. * @brief Disable the Bypass Shadow feature.
  1220. * @note When the Bypass Shadow is enabled the calendar value are taken
  1221. * directly from the Calendar counter.
  1222. * @param hrtc RTC handle
  1223. * @retval HAL status
  1224. */
  1225. HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef *hrtc)
  1226. {
  1227. /* Process Locked */
  1228. __HAL_LOCK(hrtc);
  1229. hrtc->State = HAL_RTC_STATE_BUSY;
  1230. /* Disable the write protection for RTC registers */
  1231. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1232. /* Clear the BYPSHAD bit */
  1233. CLEAR_BIT(RTC->CR, RTC_CR_BYPSHAD);
  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. #if defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  1243. /**
  1244. * @brief Set SSR Underflow detection with Interrupt.
  1245. * @param hrtc RTC handle
  1246. * @retval HAL status
  1247. */
  1248. HAL_StatusTypeDef HAL_RTCEx_SetSSRU_IT(RTC_HandleTypeDef *hrtc)
  1249. {
  1250. /* Process Locked */
  1251. __HAL_LOCK(hrtc);
  1252. hrtc->State = HAL_RTC_STATE_BUSY;
  1253. /* Disable the write protection for RTC registers */
  1254. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1255. /* Enable IT SSRU */
  1256. __HAL_RTC_SSRU_ENABLE_IT(hrtc, RTC_IT_SSRU);
  1257. /* RTC SSRU Interrupt Configuration: EXTI configuration */
  1258. __HAL_RTC_SSRU_EXTI_ENABLE_IT();
  1259. __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
  1260. /* Enable the write protection for RTC registers */
  1261. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1262. hrtc->State = HAL_RTC_STATE_READY;
  1263. /* Process Unlocked */
  1264. __HAL_UNLOCK(hrtc);
  1265. return HAL_OK;
  1266. }
  1267. /**
  1268. * @brief Deactivate SSR Underflow.
  1269. * @param hrtc RTC handle
  1270. * @retval HAL status
  1271. */
  1272. HAL_StatusTypeDef HAL_RTCEx_DeactivateSSRU(RTC_HandleTypeDef *hrtc)
  1273. {
  1274. /* Process Locked */
  1275. __HAL_LOCK(hrtc);
  1276. hrtc->State = HAL_RTC_STATE_BUSY;
  1277. /* Disable the write protection for RTC registers */
  1278. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1279. /* In case of interrupt mode is used, the interrupt source must disabled */
  1280. __HAL_RTC_SSRU_DISABLE_IT(hrtc, RTC_IT_TS);
  1281. /* Enable the write protection for RTC registers */
  1282. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1283. hrtc->State = HAL_RTC_STATE_READY;
  1284. /* Process Unlocked */
  1285. __HAL_UNLOCK(hrtc);
  1286. return HAL_OK;
  1287. }
  1288. /**
  1289. * @brief Handle SSR underflow interrupt request.
  1290. * @param hrtc RTC handle
  1291. * @retval None
  1292. */
  1293. void HAL_RTCEx_SSRUIRQHandler(RTC_HandleTypeDef *hrtc)
  1294. {
  1295. if ((RTC->MISR & RTC_MISR_SSRUMF) != 0u)
  1296. {
  1297. /* Immediately clear flags */
  1298. RTC->SCR = RTC_SCR_CSSRUF;
  1299. /* SSRU callback */
  1300. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  1301. /* Call SSRUEvent registered Callback */
  1302. hrtc->SSRUEventCallback(hrtc);
  1303. #else
  1304. HAL_RTCEx_SSRUEventCallback(hrtc);
  1305. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  1306. }
  1307. /* Change RTC state */
  1308. hrtc->State = HAL_RTC_STATE_READY;
  1309. }
  1310. /**
  1311. * @brief SSR underflow callback.
  1312. * @param hrtc RTC handle
  1313. * @retval None
  1314. */
  1315. __weak void HAL_RTCEx_SSRUEventCallback(RTC_HandleTypeDef *hrtc)
  1316. {
  1317. /* Prevent unused argument(s) compilation warning */
  1318. UNUSED(hrtc);
  1319. /* NOTE : This function should not be modified, when the callback is needed,
  1320. the HAL_RTCEx_SSRUEventCallback could be implemented in the user file
  1321. */
  1322. }
  1323. #endif /* #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
  1324. /**
  1325. * @}
  1326. */
  1327. /** @addtogroup RTCEx_Exported_Functions_Group4
  1328. * @brief Extended features functions
  1329. *
  1330. @verbatim
  1331. ===============================================================================
  1332. ##### Extended features functions #####
  1333. ===============================================================================
  1334. [..] This section provides functions allowing to:
  1335. (+) RTC Alarm B callback
  1336. (+) RTC Poll for Alarm B request
  1337. @endverbatim
  1338. * @{
  1339. */
  1340. /**
  1341. * @brief Alarm B callback.
  1342. * @param hrtc RTC handle
  1343. * @retval None
  1344. */
  1345. __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
  1346. {
  1347. /* Prevent unused argument(s) compilation warning */
  1348. UNUSED(hrtc);
  1349. /* NOTE : This function should not be modified, when the callback is needed,
  1350. the HAL_RTCEx_AlarmBEventCallback could be implemented in the user file
  1351. */
  1352. }
  1353. /**
  1354. * @brief Handle Alarm B Polling request.
  1355. * @param hrtc RTC handle
  1356. * @param Timeout Timeout duration
  1357. * @retval HAL status
  1358. */
  1359. HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  1360. {
  1361. uint32_t tickstart = HAL_GetTick();
  1362. while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == 0U)
  1363. {
  1364. if (Timeout != HAL_MAX_DELAY)
  1365. {
  1366. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  1367. {
  1368. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1369. return HAL_TIMEOUT;
  1370. }
  1371. }
  1372. }
  1373. /* Clear the Alarm Flag */
  1374. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
  1375. /* Change RTC state */
  1376. hrtc->State = HAL_RTC_STATE_READY;
  1377. return HAL_OK;
  1378. }
  1379. /**
  1380. * @}
  1381. */
  1382. /** @addtogroup RTCEx_Exported_Functions_Group5
  1383. * @brief Extended RTC Tamper functions
  1384. *
  1385. @verbatim
  1386. ==============================================================================
  1387. ##### Tamper functions #####
  1388. ==============================================================================
  1389. [..]
  1390. (+) Before calling any tamper or internal tamper function, you have to call first
  1391. HAL_RTC_Init() function.
  1392. (+) In that ine you can select to output tamper event on RTC pin.
  1393. [..]
  1394. (+) Enable the Tamper and configure the Tamper filter count, trigger Edge
  1395. or Level according to the Tamper filter (if equal to 0 Edge else Level)
  1396. value, sampling frequency, NoErase, MaskFlag, precharge or discharge and
  1397. Pull-UP, timestamp using the HAL_RTCEx_SetTamper() function.
  1398. You can configure Tamper with interrupt mode using HAL_RTCEx_SetTamper_IT() function.
  1399. (+) The default configuration of the Tamper erases the backup registers. To avoid
  1400. erase, enable the NoErase field on the TAMP_TAMPCR register.
  1401. [..]
  1402. (+) Enable Internal Tamper and configure it with interrupt, timestamp using
  1403. the HAL_RTCEx_SetInternalTamper() function.
  1404. @endverbatim
  1405. * @{
  1406. */
  1407. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  1408. /**
  1409. * @brief Set Tamper
  1410. * @param hrtc RTC handle
  1411. * @param sTamper Pointer to Tamper Structure.
  1412. * @retval HAL status
  1413. */
  1414. HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
  1415. {
  1416. uint32_t tmpreg;
  1417. /* Process TAMP instance pointer */
  1418. TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset);
  1419. /* Check the parameters */
  1420. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  1421. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  1422. assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
  1423. assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
  1424. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
  1425. assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
  1426. assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
  1427. assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  1428. assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  1429. /* Trigger and Filter have exclusive configurations */
  1430. assert_param(((sTamper->Filter != RTC_TAMPERFILTER_DISABLE) && ((sTamper->Trigger == RTC_TAMPERTRIGGER_LOWLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL)))
  1431. || ((sTamper->Filter == RTC_TAMPERFILTER_DISABLE) && ((sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))));
  1432. /* Configuration register 2 */
  1433. tmpreg = tamp->CR2;
  1434. tmpreg &= ~((sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
  1435. /* Configure the tamper trigger bit */
  1436. if ((sTamper->Trigger == RTC_TAMPERTRIGGER_HIGHLEVEL) || (sTamper->Trigger == RTC_TAMPERTRIGGER_FALLINGEDGE))
  1437. {
  1438. tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos);
  1439. }
  1440. /* Configure the tamper flags masking bit */
  1441. if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
  1442. {
  1443. tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos);
  1444. }
  1445. /* Configure the tamper backup registers erasure bit */
  1446. if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
  1447. {
  1448. tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos);
  1449. }
  1450. tamp->CR2 = tmpreg;
  1451. /* Configure filtering parameters */
  1452. tamp->FLTCR = (sTamper->Filter) | (sTamper->SamplingFrequency) | \
  1453. (sTamper->PrechargeDuration) | (sTamper->TamperPullUp);
  1454. /* Configure Timestamp saving on tamper detection */
  1455. if ((hrtc->Instance->CR & RTC_CR_TAMPTS) != (sTamper->TimeStampOnTamperDetection))
  1456. {
  1457. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1458. tmpreg = (hrtc->Instance->CR & ~RTC_CR_TAMPTS);
  1459. hrtc->Instance->CR = (tmpreg | (sTamper->TimeStampOnTamperDetection));
  1460. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1461. }
  1462. /* Enable selected tamper */
  1463. tamp->CR1 |= (sTamper->Tamper);
  1464. return HAL_OK;
  1465. }
  1466. #else /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
  1467. /**
  1468. * @brief Set Tamper.
  1469. * @note By calling this API we disable the tamper interrupt for all tampers.
  1470. * @param hrtc RTC handle
  1471. * @param sTamper Pointer to Tamper Structure.
  1472. * @retval HAL status
  1473. */
  1474. HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
  1475. {
  1476. uint32_t tmpreg;
  1477. /* Check the parameters */
  1478. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  1479. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  1480. assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
  1481. assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
  1482. assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
  1483. assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
  1484. assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  1485. assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  1486. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
  1487. /* Process Locked */
  1488. __HAL_LOCK(hrtc);
  1489. hrtc->State = HAL_RTC_STATE_BUSY;
  1490. /* Read register */
  1491. tmpreg = hrtc->Instance->TAMPCR;
  1492. #if defined(RTC_TAMPER1_SUPPORT)
  1493. if ((sTamper->Tamper & RTC_TAMPER_1) != 0)
  1494. {
  1495. MODIFY_REG(tmpreg,
  1496. (RTC_TAMPCR_TAMP1E | RTC_TAMPCR_TAMP1TRG | RTC_TAMPCR_TAMPIE | RTC_TAMPCR_TAMP1IE | RTC_TAMPCR_TAMP1NOERASE | RTC_TAMPCR_TAMP1MF), \
  1497. sTamper->Tamper | \
  1498. (sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE ? 0U : RTC_TAMPCR_TAMP1TRG) | \
  1499. (sTamper->NoErase == RTC_TAMPER_ERASE_BACKUP_ENABLE ? 0U : RTC_TAMPCR_TAMP1NOERASE) | \
  1500. (sTamper->MaskFlag == RTC_TAMPERMASK_FLAG_ENABLE ? RTC_TAMPCR_TAMP1MF : 0U) \
  1501. );
  1502. }
  1503. #endif /* RTC_TAMPER1_SUPPORT */
  1504. #if defined(RTC_TAMPER2_SUPPORT)
  1505. if ((sTamper->Tamper & RTC_TAMPER_2) != 0)
  1506. {
  1507. MODIFY_REG(tmpreg,
  1508. (RTC_TAMPCR_TAMP2E | RTC_TAMPCR_TAMP2TRG | RTC_TAMPCR_TAMPIE | RTC_TAMPCR_TAMP2IE | RTC_TAMPCR_TAMP2NOERASE | RTC_TAMPCR_TAMP2MF), \
  1509. sTamper->Tamper | \
  1510. (sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE ? 0U : RTC_TAMPCR_TAMP2TRG) | \
  1511. (sTamper->NoErase == RTC_TAMPER_ERASE_BACKUP_ENABLE ? 0U : RTC_TAMPCR_TAMP2NOERASE) | \
  1512. (sTamper->MaskFlag == RTC_TAMPERMASK_FLAG_ENABLE ? RTC_TAMPCR_TAMP2MF : 0U) \
  1513. );
  1514. }
  1515. #endif /* RTC_TAMPER2_SUPPORT */
  1516. #if defined(RTC_TAMPER3_SUPPORT)
  1517. if ((sTamper->Tamper & RTC_TAMPER_3) != 0)
  1518. {
  1519. MODIFY_REG(tmpreg,
  1520. (RTC_TAMPCR_TAMP3E | RTC_TAMPCR_TAMP3TRG | RTC_TAMPCR_TAMPIE | RTC_TAMPCR_TAMP3IE | RTC_TAMPCR_TAMP3NOERASE | RTC_TAMPCR_TAMP3MF), \
  1521. sTamper->Tamper | \
  1522. (sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE ? 0U : RTC_TAMPCR_TAMP3TRG) | \
  1523. (sTamper->NoErase == RTC_TAMPER_ERASE_BACKUP_ENABLE ? 0U : RTC_TAMPCR_TAMP3NOERASE) | \
  1524. (sTamper->MaskFlag == RTC_TAMPERMASK_FLAG_ENABLE ? RTC_TAMPCR_TAMP3MF : 0U) \
  1525. );
  1526. }
  1527. #endif /* RTC_TAMPER3_SUPPORT */
  1528. /* Update common parameters */
  1529. MODIFY_REG(tmpreg,
  1530. (RTC_TAMPCR_TAMPTS | RTC_TAMPCR_TAMPFREQ | RTC_TAMPCR_TAMPFLT | RTC_TAMPCR_TAMPPRCH | RTC_TAMPCR_TAMPPUDIS), \
  1531. sTamper->Filter | sTamper->SamplingFrequency | sTamper->PrechargeDuration | sTamper->TamperPullUp | \
  1532. sTamper->TimeStampOnTamperDetection \
  1533. );
  1534. /* Set register */
  1535. WRITE_REG(hrtc->Instance->TAMPCR, tmpreg);
  1536. hrtc->State = HAL_RTC_STATE_READY;
  1537. /* Process Unlocked */
  1538. __HAL_UNLOCK(hrtc);
  1539. return HAL_OK;
  1540. }
  1541. #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
  1542. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  1543. /**
  1544. * @brief Set Tamper with interrupt.
  1545. * @param hrtc RTC handle
  1546. * @param sTamper Pointer to Tamper Structure.
  1547. * @retval HAL status
  1548. */
  1549. HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
  1550. {
  1551. uint32_t tmpreg;
  1552. /* Process TAMP instance pointer */
  1553. TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset);
  1554. /* Check the parameters */
  1555. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  1556. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  1557. assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
  1558. assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
  1559. assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
  1560. assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
  1561. assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  1562. assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  1563. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
  1564. /* Copy configuration register into temporary variable */
  1565. tmpreg = tamp->CR2;
  1566. /* Clear the bits that are going to be configured and leave the others unchanged */
  1567. tmpreg &= ~((sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos) | (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos));
  1568. if (sTamper->Trigger != RTC_TAMPERTRIGGER_RISINGEDGE)
  1569. {
  1570. tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1TRG_Pos);
  1571. }
  1572. /* Configure the tamper flags masking bit */
  1573. if (sTamper->MaskFlag != RTC_TAMPERMASK_FLAG_DISABLE)
  1574. {
  1575. tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1MSK_Pos);
  1576. }
  1577. /* Configure the tamper backup registers erasure bit */
  1578. if (sTamper->NoErase != RTC_TAMPER_ERASE_BACKUP_ENABLE)
  1579. {
  1580. tmpreg |= (sTamper->Tamper << TAMP_CR2_TAMP1NOERASE_Pos);
  1581. }
  1582. tamp->CR2 = tmpreg;
  1583. /* Configure filtering parameters */
  1584. tamp->FLTCR = (sTamper->Filter) | (sTamper->SamplingFrequency) | \
  1585. (sTamper->PrechargeDuration) | (sTamper->TamperPullUp);
  1586. /* Configure Timestamp saving on tamper detection */
  1587. if ((hrtc->Instance->CR & RTC_CR_TAMPTS) != (sTamper->TimeStampOnTamperDetection))
  1588. {
  1589. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1590. tmpreg = (hrtc->Instance->CR & ~RTC_CR_TAMPTS);
  1591. hrtc->Instance->CR = (tmpreg | (sTamper->TimeStampOnTamperDetection));
  1592. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1593. }
  1594. /* Configure RTC Tamper Interrupt: EXTI configuration */
  1595. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
  1596. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_FALLING_EDGE();
  1597. /* Enable interrupt on selected tamper */
  1598. tamp->IER |= sTamper->Tamper;
  1599. /* Enable selected tamper */
  1600. tamp->CR1 |= sTamper->Tamper;
  1601. return HAL_OK;
  1602. }
  1603. #else /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
  1604. /**
  1605. * @brief Set Tamper with interrupt.
  1606. * @param hrtc RTC handle
  1607. * @param sTamper Pointer to Tamper Structure.
  1608. * @retval HAL status
  1609. */
  1610. HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef *sTamper)
  1611. {
  1612. uint32_t tmpreg;
  1613. /* Check the parameters */
  1614. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  1615. assert_param(IS_RTC_TAMPER_INTERRUPT(sTamper->Interrupt));
  1616. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  1617. assert_param(IS_RTC_TAMPER_ERASE_MODE(sTamper->NoErase));
  1618. assert_param(IS_RTC_TAMPER_MASKFLAG_STATE(sTamper->MaskFlag));
  1619. assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
  1620. assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
  1621. assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  1622. assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  1623. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
  1624. /* Process Locked */
  1625. __HAL_LOCK(hrtc);
  1626. hrtc->State = HAL_RTC_STATE_BUSY;
  1627. /* Read register */
  1628. tmpreg = hrtc->Instance->TAMPCR;
  1629. #if defined(RTC_TAMPER1_SUPPORT)
  1630. if ((sTamper->Tamper & RTC_TAMPER_1) != 0)
  1631. {
  1632. MODIFY_REG(tmpreg,
  1633. (RTC_TAMPCR_TAMP1E | RTC_TAMPCR_TAMP1TRG | RTC_TAMPCR_TAMPIE | RTC_TAMPCR_TAMP1IE | RTC_TAMPCR_TAMP1NOERASE | RTC_TAMPCR_TAMP1MF), \
  1634. sTamper->Tamper | sTamper->Interrupt | \
  1635. (sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE ? 0U : RTC_TAMPCR_TAMP1TRG) | \
  1636. (sTamper->NoErase == RTC_TAMPER_ERASE_BACKUP_ENABLE ? 0U : RTC_TAMPCR_TAMP1NOERASE) | \
  1637. (sTamper->MaskFlag == RTC_TAMPERMASK_FLAG_ENABLE ? RTC_TAMPCR_TAMP1MF : 0U) \
  1638. );
  1639. }
  1640. #endif /* RTC_TAMPER1_SUPPORT */
  1641. #if defined(RTC_TAMPER2_SUPPORT)
  1642. if ((sTamper->Tamper & RTC_TAMPER_2) != 0)
  1643. {
  1644. MODIFY_REG(tmpreg,
  1645. (RTC_TAMPCR_TAMP2E | RTC_TAMPCR_TAMP2TRG | RTC_TAMPCR_TAMPIE | RTC_TAMPCR_TAMP2IE | RTC_TAMPCR_TAMP2NOERASE | RTC_TAMPCR_TAMP2MF), \
  1646. sTamper->Tamper | sTamper->Interrupt | \
  1647. (sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE ? 0U : RTC_TAMPCR_TAMP2TRG) | \
  1648. (sTamper->NoErase == RTC_TAMPER_ERASE_BACKUP_ENABLE ? 0U : RTC_TAMPCR_TAMP2NOERASE) | \
  1649. (sTamper->MaskFlag == RTC_TAMPERMASK_FLAG_ENABLE ? RTC_TAMPCR_TAMP2MF : 0U) \
  1650. );
  1651. }
  1652. #endif /* RTC_TAMPER2_SUPPORT */
  1653. #if defined(RTC_TAMPER3_SUPPORT)
  1654. if ((sTamper->Tamper & RTC_TAMPER_3) != 0)
  1655. {
  1656. MODIFY_REG(tmpreg,
  1657. (RTC_TAMPCR_TAMP3E | RTC_TAMPCR_TAMP3TRG | RTC_TAMPCR_TAMPIE | RTC_TAMPCR_TAMP3IE | RTC_TAMPCR_TAMP3NOERASE | RTC_TAMPCR_TAMP3MF), \
  1658. sTamper->Tamper | sTamper->Interrupt | \
  1659. (sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE ? 0U : RTC_TAMPCR_TAMP3TRG) | \
  1660. (sTamper->NoErase == RTC_TAMPER_ERASE_BACKUP_ENABLE ? 0U : RTC_TAMPCR_TAMP3NOERASE) | \
  1661. (sTamper->MaskFlag == RTC_TAMPERMASK_FLAG_ENABLE ? RTC_TAMPCR_TAMP3MF : 0U) \
  1662. );
  1663. }
  1664. #endif /* RTC_TAMPER3_SUPPORT */
  1665. /* Update common parameters */
  1666. MODIFY_REG(tmpreg,
  1667. (RTC_TAMPCR_TAMPTS | RTC_TAMPCR_TAMPFREQ | RTC_TAMPCR_TAMPFLT | RTC_TAMPCR_TAMPPRCH | RTC_TAMPCR_TAMPPUDIS), \
  1668. sTamper->Filter | sTamper->SamplingFrequency | sTamper->PrechargeDuration | sTamper->TamperPullUp | \
  1669. sTamper->TimeStampOnTamperDetection \
  1670. );
  1671. /* Set register */
  1672. WRITE_REG(hrtc->Instance->TAMPCR, tmpreg);
  1673. /* RTC Tamper Interrupt Configuration: EXTI configuration */
  1674. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
  1675. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
  1676. hrtc->State = HAL_RTC_STATE_READY;
  1677. /* Process Unlocked */
  1678. __HAL_UNLOCK(hrtc);
  1679. return HAL_OK;
  1680. }
  1681. #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
  1682. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  1683. /**
  1684. * @brief Deactivate Tamper.
  1685. * @param hrtc RTC handle
  1686. * @param Tamper Selected tamper pin.
  1687. * This parameter can be a combination of the following values:
  1688. * @arg RTC_TAMPER_1
  1689. * @arg RTC_TAMPER_2
  1690. * @retval HAL status
  1691. */
  1692. HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
  1693. {
  1694. /* Process TAMP instance pointer */
  1695. TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset);
  1696. assert_param(IS_RTC_TAMPER(Tamper));
  1697. /* Disable the selected Tamper pin */
  1698. tamp->CR1 &= ~Tamper;
  1699. /* Clear tamper mask/noerase/trigger configuration */
  1700. tamp->CR2 &= ~((Tamper << 24) | (Tamper << 16) | Tamper);
  1701. /* Clear tamper interrupt mode configuration */
  1702. tamp->IER &= ~Tamper;
  1703. /* Clear tamper interrupt and event flags (WO register) */
  1704. tamp->SCR = Tamper;
  1705. return HAL_OK;
  1706. }
  1707. #else /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
  1708. /**
  1709. * @brief Deactivate Tamper.
  1710. * @param hrtc RTC handle
  1711. * @param Tamper Selected tamper pin.
  1712. * This parameter can be any combination of the following values:
  1713. * @arg RTC_TAMPER_1
  1714. * @arg RTC_TAMPER_2
  1715. * @arg RTC_TAMPER_3
  1716. * @retval HAL status
  1717. */
  1718. HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
  1719. {
  1720. assert_param(IS_RTC_TAMPER(Tamper));
  1721. /* Process Locked */
  1722. __HAL_LOCK(hrtc);
  1723. hrtc->State = HAL_RTC_STATE_BUSY;
  1724. /* Disable the selected Tamper pin */
  1725. hrtc->Instance->TAMPCR &= ~Tamper;
  1726. #if defined(RTC_TAMPER1_SUPPORT)
  1727. if ((Tamper & RTC_TAMPER_1) != 0U)
  1728. {
  1729. /* Disable the Tamper1 interrupt */
  1730. hrtc->Instance->TAMPCR &= ((uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP1));
  1731. }
  1732. #endif /* RTC_TAMPER1_SUPPORT */
  1733. if ((Tamper & RTC_TAMPER_2) != 0U)
  1734. {
  1735. /* Disable the Tamper2 interrupt */
  1736. hrtc->Instance->TAMPCR &= ((uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP2));
  1737. }
  1738. #if defined(RTC_TAMPER3_SUPPORT)
  1739. if ((Tamper & RTC_TAMPER_3) != 0U)
  1740. {
  1741. /* Disable the Tamper3 interrupt */
  1742. hrtc->Instance->TAMPCR &= ((uint32_t)~(RTC_IT_TAMP | RTC_IT_TAMP3));
  1743. }
  1744. #endif /* RTC_TAMPER3_SUPPORT */
  1745. hrtc->State = HAL_RTC_STATE_READY;
  1746. /* Process Unlocked */
  1747. __HAL_UNLOCK(hrtc);
  1748. return HAL_OK;
  1749. }
  1750. #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
  1751. #if defined(RTC_TAMPER1_SUPPORT)
  1752. /**
  1753. * @brief Handle Tamper 1 Polling.
  1754. * @param hrtc RTC handle
  1755. * @param Timeout Timeout duration
  1756. * @retval HAL status
  1757. */
  1758. HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  1759. {
  1760. uint32_t tickstart = HAL_GetTick();
  1761. /* Get the status of the Interrupt */
  1762. while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) == 0U)
  1763. {
  1764. if (Timeout != HAL_MAX_DELAY)
  1765. {
  1766. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  1767. {
  1768. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1769. return HAL_TIMEOUT;
  1770. }
  1771. }
  1772. }
  1773. /* Clear the Tamper Flag */
  1774. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
  1775. /* Change RTC state */
  1776. hrtc->State = HAL_RTC_STATE_READY;
  1777. return HAL_OK;
  1778. }
  1779. #endif /* RTC_TAMPER1_SUPPORT */
  1780. /**
  1781. * @brief Handle Tamper 2 Polling.
  1782. * @param hrtc RTC handle
  1783. * @param Timeout Timeout duration
  1784. * @retval HAL status
  1785. */
  1786. HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  1787. {
  1788. uint32_t tickstart = HAL_GetTick();
  1789. /* Get the status of the Interrupt */
  1790. while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) == 0U)
  1791. {
  1792. if (Timeout != HAL_MAX_DELAY)
  1793. {
  1794. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  1795. {
  1796. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1797. return HAL_TIMEOUT;
  1798. }
  1799. }
  1800. }
  1801. /* Clear the Tamper Flag */
  1802. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
  1803. /* Change RTC state */
  1804. hrtc->State = HAL_RTC_STATE_READY;
  1805. return HAL_OK;
  1806. }
  1807. #if defined(RTC_TAMPER3_SUPPORT)
  1808. /**
  1809. * @brief Handle Tamper 3 Polling.
  1810. * @param hrtc RTC handle
  1811. * @param Timeout Timeout duration
  1812. * @retval HAL status
  1813. */
  1814. HAL_StatusTypeDef HAL_RTCEx_PollForTamper3Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  1815. {
  1816. uint32_t tickstart = HAL_GetTick();
  1817. /* Get the status of the Interrupt */
  1818. while (__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) == 0U)
  1819. {
  1820. if (Timeout != HAL_MAX_DELAY)
  1821. {
  1822. if ((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
  1823. {
  1824. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1825. return HAL_TIMEOUT;
  1826. }
  1827. }
  1828. }
  1829. /* Clear the Tamper Flag */
  1830. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
  1831. /* Change RTC state */
  1832. hrtc->State = HAL_RTC_STATE_READY;
  1833. return HAL_OK;
  1834. }
  1835. #endif /* RTC_TAMPER3_SUPPORT */
  1836. #if defined(RTC_TAMPER1_SUPPORT)
  1837. /**
  1838. * @brief Tamper 1 callback.
  1839. * @param hrtc RTC handle
  1840. * @retval None
  1841. */
  1842. __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
  1843. {
  1844. /* Prevent unused argument(s) compilation warning */
  1845. UNUSED(hrtc);
  1846. /* NOTE : This function should not be modified, when the callback is needed,
  1847. the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
  1848. */
  1849. }
  1850. #endif /* RTC_TAMPER1_SUPPORT */
  1851. /**
  1852. * @brief Tamper 2 callback.
  1853. * @param hrtc RTC handle
  1854. * @retval None
  1855. */
  1856. __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
  1857. {
  1858. /* Prevent unused argument(s) compilation warning */
  1859. UNUSED(hrtc);
  1860. /* NOTE : This function should not be modified, when the callback is needed,
  1861. the HAL_RTCEx_Tamper2EventCallback could be implemented in the user file
  1862. */
  1863. }
  1864. #if defined(RTC_TAMPER3_SUPPORT)
  1865. /**
  1866. * @brief Tamper 3 callback.
  1867. * @param hrtc RTC handle
  1868. * @retval None
  1869. */
  1870. __weak void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef *hrtc)
  1871. {
  1872. /* Prevent unused argument(s) compilation warning */
  1873. UNUSED(hrtc);
  1874. /* NOTE : This function should not be modified, when the callback is needed,
  1875. the HAL_RTCEx_Tamper3EventCallback could be implemented in the user file
  1876. */
  1877. }
  1878. #endif /* RTC_TAMPER3_SUPPORT */
  1879. /**
  1880. * @}
  1881. */
  1882. /** @addtogroup RTCEx_Exported_Functions_Group6
  1883. * @brief Extended RTC Backup register functions
  1884. *
  1885. @verbatim
  1886. ===============================================================================
  1887. ##### Extended RTC Backup register functions #####
  1888. ===============================================================================
  1889. [..]
  1890. (+) Before calling any tamper or internal tamper function, you have to call first
  1891. HAL_RTC_Init() function.
  1892. (+) In that ine you can select to output tamper event on RTC pin.
  1893. [..]
  1894. This subsection provides functions allowing to
  1895. (+) Write a data in a specified RTC Backup data register
  1896. (+) Read a data in a specified RTC Backup data register
  1897. @endverbatim
  1898. * @{
  1899. */
  1900. /**
  1901. * @brief Write a data in a specified RTC Backup data register.
  1902. * @param hrtc RTC handle
  1903. * @param BackupRegister RTC Backup data Register number.
  1904. * This parameter can be: RTC_BKP_DRx where x can be from 0 to 31 to
  1905. * specify the register.
  1906. * @param Data Data to be written in the specified Backup data register.
  1907. * @retval None
  1908. */
  1909. void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
  1910. {
  1911. uint32_t __IO tmp;
  1912. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  1913. /* Process TAMP instance pointer */
  1914. TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset);
  1915. /* Check the parameters */
  1916. assert_param(IS_RTC_BKP(BackupRegister));
  1917. tmp = (uint32_t) & (tamp->BKP0R);
  1918. #else /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
  1919. /* Check the parameters */
  1920. assert_param(IS_RTC_BKP(BackupRegister));
  1921. tmp = (uint32_t) & (hrtc->Instance->BKP0R);
  1922. #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
  1923. tmp += (BackupRegister * 4U);
  1924. /* Write the specified register */
  1925. *(__IO uint32_t *)tmp = (uint32_t)Data;
  1926. }
  1927. /**
  1928. * @brief Read data from the specified RTC Backup data Register.
  1929. * @param hrtc RTC handle
  1930. * @param BackupRegister RTC Backup data Register number.
  1931. * This parameter can be: RTC_BKP_DRx where x can be from 0 to 31 to
  1932. * specify the register.
  1933. * @retval Read value
  1934. */
  1935. uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
  1936. {
  1937. uint32_t tmp;
  1938. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  1939. /* Process TAMP instance pointer */
  1940. TAMP_TypeDef *tamp = (TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset);
  1941. /* Check the parameters */
  1942. assert_param(IS_RTC_BKP(BackupRegister));
  1943. tmp = (uint32_t) & (tamp->BKP0R);
  1944. #else /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
  1945. /* Check the parameters */
  1946. assert_param(IS_RTC_BKP(BackupRegister));
  1947. tmp = (uint32_t) & (hrtc->Instance->BKP0R);
  1948. #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
  1949. tmp += (BackupRegister * 4U);
  1950. /* Read the specified register */
  1951. return (*(__IO uint32_t *)tmp);
  1952. }
  1953. /**
  1954. * @}
  1955. */
  1956. /**
  1957. * @}
  1958. */
  1959. #endif /* HAL_RTC_MODULE_ENABLED */
  1960. /**
  1961. * @}
  1962. */
  1963. /**
  1964. * @}
  1965. */