stm32f3xx_hal_rtc_ex.c 56 KB

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