stm32l0xx_hal_rtc_ex.c 65 KB

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