stm32l4xx_hal_rtc_ex.c 60 KB

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