stm32g0xx_hal_rtc_ex.c 60 KB

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