stm32l4xx_hal_rtc.c 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_rtc.c
  4. * @author MCD Application Team
  5. * @brief RTC HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Real-Time Clock (RTC) peripheral:
  8. * + Initialization/de-initialization functions
  9. * + Calendar (Time and Date) configuration
  10. * + Alarms (Alarm A and Alarm B) configuration
  11. * + WakeUp Timer configuration
  12. * + TimeStamp configuration
  13. * + Tampers configuration
  14. * + Backup Data Registers configuration
  15. * + RTC Tamper and TimeStamp Pins Selection
  16. * + Interrupts and flags management
  17. *
  18. ******************************************************************************
  19. * @attention
  20. *
  21. * Copyright (c) 2017 STMicroelectronics.
  22. * All rights reserved.
  23. *
  24. * This software is licensed under terms that can be found in the LICENSE file
  25. * in the root directory of this software component.
  26. * If no LICENSE file comes with this software, it is provided AS-IS.
  27. *
  28. ******************************************************************************
  29. @verbatim
  30. ===============================================================================
  31. ##### RTC Operating Condition #####
  32. ===============================================================================
  33. [..] The real-time clock (RTC) and the RTC backup registers can be powered
  34. from the VBAT voltage when the main VDD supply is powered off.
  35. To retain the content of the RTC backup registers and supply the RTC
  36. when VDD is turned off, VBAT pin can be connected to an optional
  37. standby voltage supplied by a battery or by another source.
  38. ##### Backup Domain Reset #####
  39. ===============================================================================
  40. [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
  41. to their reset values.
  42. A backup domain reset is generated when one of the following events occurs:
  43. (#) Software reset, triggered by setting the BDRST bit in the
  44. RCC Backup domain control register (RCC_BDCR).
  45. (#) VDD or VBAT power on, if both supplies have previously been powered off.
  46. (#) Tamper detection event resets all data backup registers.
  47. ##### Backup Domain Access #####
  48. ==================================================================
  49. [..] After reset, the backup domain (RTC registers and RTC backup data registers)
  50. is protected against possible unwanted write accesses.
  51. [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
  52. (+) Enable the Power Controller (PWR) APB1 interface clock using the
  53. __HAL_RCC_PWR_CLK_ENABLE() function.
  54. (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
  55. (+) Select the RTC clock source using the __HAL_RCC_RTC_CONFIG() function.
  56. (+) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() function.
  57. [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
  58. (#) Call the function HAL_RCCEx_PeriphCLKConfig with RCC_PERIPHCLK_RTC for
  59. PeriphClockSelection and select RTCClockSelection (LSE, LSI or HSEdiv32)
  60. (#) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() macro.
  61. ##### How to use RTC Driver #####
  62. ===================================================================
  63. [..]
  64. (+) Enable the RTC domain access (see description in the section above).
  65. (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
  66. format using the HAL_RTC_Init() function.
  67. *** Time and Date configuration ***
  68. ===================================
  69. [..]
  70. (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
  71. and HAL_RTC_SetDate() functions.
  72. (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
  73. *** Alarm configuration ***
  74. ===========================
  75. [..]
  76. (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
  77. You can also configure the RTC Alarm with interrupt mode using the
  78. HAL_RTC_SetAlarm_IT() function.
  79. (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
  80. ##### RTC and low power modes #####
  81. ==================================================================
  82. [..] The MCU can be woken up from a low power mode by an RTC alternate
  83. function.
  84. [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
  85. RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
  86. These RTC alternate functions can wake up the system from the Stop and
  87. Standby low power modes.
  88. [..] The system can also wake up from low power modes without depending
  89. on an external interrupt (Auto-wakeup mode), by using the RTC alarm
  90. or the RTC wakeup events.
  91. [..] The RTC provides a programmable time base for waking up from the
  92. Stop or Standby mode at regular intervals.
  93. Wakeup from STOP and STANDBY modes is possible only when the RTC clock source
  94. is LSE or LSI.
  95. *** Callback registration ***
  96. =============================================
  97. [..]
  98. When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
  99. not defined, the callback registration feature is not available and all callbacks
  100. are set to the corresponding weak functions. This is the recommended configuration
  101. in order to optimize memory/code consumption footprint/performances.
  102. [..]
  103. The compilation define USE_RTC_REGISTER_CALLBACKS when set to 1
  104. allows the user to configure dynamically the driver callbacks.
  105. Use Function HAL_RTC_RegisterCallback() to register an interrupt callback.
  106. [..]
  107. Function HAL_RTC_RegisterCallback() allows to register following callbacks:
  108. (+) AlarmAEventCallback : RTC Alarm A Event callback.
  109. (+) AlarmBEventCallback : RTC Alarm B Event callback.
  110. (+) TimeStampEventCallback : RTC TimeStamp Event callback.
  111. (+) WakeUpTimerEventCallback : RTC WakeUpTimer Event callback.
  112. #if defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  113. (+) SSRUEventCallback : RTC SSRU Event callback.
  114. #endif
  115. (+) Tamper1EventCallback : RTC Tamper 1 Event callback.
  116. (+) Tamper2EventCallback : RTC Tamper 2 Event callback.
  117. (+) Tamper3EventCallback : RTC Tamper 3 Event callback.
  118. (+) MspInitCallback : RTC MspInit callback.
  119. (+) MspDeInitCallback : RTC MspDeInit callback.
  120. This function takes as parameters the HAL peripheral handle, the Callback ID
  121. and a pointer to the user callback function.
  122. [..]
  123. Use function HAL_RTC_UnRegisterCallback() to reset a callback to the default
  124. weak function.
  125. HAL_RTC_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  126. and the Callback ID.
  127. This function allows to reset following callbacks:
  128. (+) AlarmAEventCallback : RTC Alarm A Event callback.
  129. (+) AlarmBEventCallback : RTC Alarm B Event callback.
  130. (+) TimeStampEventCallback : RTC TimeStamp Event callback.
  131. (+) WakeUpTimerEventCallback : RTC WakeUpTimer Event callback.
  132. #if defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  133. (+) SSRUEventCallback : RTC SSRU Event callback.
  134. #endif
  135. (+) Tamper1EventCallback : RTC Tamper 1 Event callback.
  136. (+) Tamper2EventCallback : RTC Tamper 2 Event callback.
  137. (+) Tamper3EventCallback : RTC Tamper 3 Event callback.
  138. (+) MspInitCallback : RTC MspInit callback.
  139. (+) MspDeInitCallback : RTC MspDeInit callback.
  140. [..]
  141. By default, after the HAL_RTC_Init() and when the state is HAL_RTC_STATE_RESET,
  142. all callbacks are set to the corresponding weak functions :
  143. examples AlarmAEventCallback(), TimeStampEventCallback().
  144. Exception done for MspInit and MspDeInit callbacks that are reset to the legacy weak function
  145. in the HAL_RTC_Init()/HAL_RTC_DeInit() only when these callbacks are null
  146. (not registered beforehand).
  147. If not, MspInit or MspDeInit are not null, HAL_RTC_Init()/HAL_RTC_DeInit()
  148. keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
  149. [..]
  150. Callbacks can be registered/unregistered in HAL_RTC_STATE_READY state only.
  151. Exception done MspInit/MspDeInit that can be registered/unregistered
  152. in HAL_RTC_STATE_READY or HAL_RTC_STATE_RESET state,
  153. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  154. In that case first register the MspInit/MspDeInit user callbacks
  155. using HAL_RTC_RegisterCallback() before calling HAL_RTC_DeInit()
  156. or HAL_RTC_Init() function.
  157. [..]
  158. When The compilation define USE_HAL_RTC_REGISTER_CALLBACKS is set to 0 or
  159. not defined, the callback registration feature is not available and all callbacks
  160. are set to the corresponding weak functions.
  161. @endverbatim
  162. ******************************************************************************
  163. */
  164. /* Includes ------------------------------------------------------------------*/
  165. #include "stm32l4xx_hal.h"
  166. /** @addtogroup STM32L4xx_HAL_Driver
  167. * @{
  168. */
  169. /** @addtogroup RTC
  170. * @brief RTC HAL module driver
  171. * @{
  172. */
  173. #ifdef HAL_RTC_MODULE_ENABLED
  174. /* Private typedef -----------------------------------------------------------*/
  175. /* Private define ------------------------------------------------------------*/
  176. /* Private macro -------------------------------------------------------------*/
  177. /* Private variables ---------------------------------------------------------*/
  178. /* Private function prototypes -----------------------------------------------*/
  179. /* Exported functions --------------------------------------------------------*/
  180. /** @addtogroup RTC_Exported_Functions
  181. * @{
  182. */
  183. /** @addtogroup RTC_Exported_Functions_Group1
  184. * @brief Initialization and Configuration functions
  185. *
  186. @verbatim
  187. ===============================================================================
  188. ##### Initialization and de-initialization functions #####
  189. ===============================================================================
  190. [..] This section provides functions allowing to initialize and configure the
  191. RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
  192. RTC registers Write protection, enter and exit the RTC initialization mode,
  193. RTC registers synchronization check and reference clock detection enable.
  194. (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
  195. It is split into 2 programmable prescalers to minimize power consumption.
  196. (++) A 7-bit asynchronous prescaler and a 15-bit synchronous prescaler.
  197. (++) When both prescalers are used, it is recommended to configure the
  198. asynchronous prescaler to a high value to minimize power consumption.
  199. (#) All RTC registers are Write protected. Writing to the RTC registers
  200. is enabled by writing a key into the Write Protection register, RTC_WPR.
  201. (#) To configure the RTC Calendar, user application should enter
  202. initialization mode. In this mode, the calendar counter is stopped
  203. and its value can be updated. When the initialization sequence is
  204. complete, the calendar restarts counting after 4 RTCCLK cycles.
  205. (#) To read the calendar through the shadow registers after Calendar
  206. initialization, calendar update or after wakeup from low power modes
  207. the software must first clear the RSF flag. The software must then
  208. wait until it is set again before reading the calendar, which means
  209. that the calendar registers have been correctly copied into the
  210. RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function
  211. implements the above software sequence (RSF clear and RSF check).
  212. @endverbatim
  213. * @{
  214. */
  215. /**
  216. * @brief Initialize the RTC peripheral
  217. * @param hrtc RTC handle
  218. * @retval HAL status
  219. */
  220. HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
  221. {
  222. HAL_StatusTypeDef status = HAL_ERROR;
  223. /* Check the RTC peripheral state */
  224. if (hrtc != NULL)
  225. {
  226. /* Check the parameters */
  227. assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
  228. assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
  229. assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
  230. assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
  231. assert_param(IS_RTC_OUTPUT(hrtc->Init.OutPut));
  232. assert_param(IS_RTC_OUTPUT_REMAP(hrtc->Init.OutPutRemap));
  233. assert_param(IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
  234. assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
  235. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  236. assert_param(IS_RTC_OUTPUT_PULLUP(hrtc->Init.OutPutPullUp));
  237. #endif
  238. #if defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  239. assert_param(IS_RTC_BINARY_MODE(hrtc->Init.BinMode));
  240. assert_param(IS_RTC_BINARY_MIX_BCDU(hrtc->Init.BinMixBcdU));
  241. #endif
  242. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  243. if (hrtc->State == HAL_RTC_STATE_RESET)
  244. {
  245. /* Allocate lock resource and initialize it */
  246. hrtc->Lock = HAL_UNLOCKED;
  247. hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */
  248. hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback; /* Legacy weak AlarmBEventCallback */
  249. hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback; /* Legacy weak TimeStampEventCallback */
  250. hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
  251. #if defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  252. hrtc->SSRUEventCallback = HAL_RTCEx_SSRUEventCallback; /* Legacy weak SSRUEventCallback */
  253. #endif
  254. #if defined(RTC_TAMPER1_SUPPORT)
  255. hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */
  256. #endif /* RTC_TAMPER1_SUPPORT */
  257. hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback; /* Legacy weak Tamper2EventCallback */
  258. #if defined(RTC_TAMPER3_SUPPORT)
  259. hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback; /* Legacy weak Tamper3EventCallback */
  260. #endif /* RTC_TAMPER3_SUPPORT */
  261. if (hrtc->MspInitCallback == NULL)
  262. {
  263. hrtc->MspInitCallback = HAL_RTC_MspInit;
  264. }
  265. /* Init the low level hardware */
  266. hrtc->MspInitCallback(hrtc);
  267. if (hrtc->MspDeInitCallback == NULL)
  268. {
  269. hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
  270. }
  271. }
  272. #else /* #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
  273. if (hrtc->State == HAL_RTC_STATE_RESET)
  274. {
  275. /* Allocate lock resource and initialize it */
  276. hrtc->Lock = HAL_UNLOCKED;
  277. /* Initialize RTC MSP */
  278. HAL_RTC_MspInit(hrtc);
  279. }
  280. #endif /* #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
  281. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  282. /* Process TAMP ip offset from RTC one */
  283. hrtc->TampOffset = (TAMP_BASE - RTC_BASE);
  284. #endif
  285. /* Set RTC state */
  286. hrtc->State = HAL_RTC_STATE_BUSY;
  287. /* Disable the write protection for RTC registers */
  288. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  289. /* Enter Initialization mode */
  290. status = RTC_EnterInitMode(hrtc);
  291. if (status == HAL_OK)
  292. {
  293. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  294. /* Clear RTC_CR FMT, OSEL, POL and TAMPOE Bits */
  295. hrtc->Instance->CR &= ~(RTC_CR_FMT | RTC_CR_POL | RTC_CR_OSEL | RTC_CR_TAMPOE);
  296. #else
  297. /* Clear RTC_CR FMT, OSEL and POL Bits */
  298. hrtc->Instance->CR &= ~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL);
  299. #endif
  300. /* Set RTC_CR register */
  301. hrtc->Instance->CR |= (hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity);
  302. /* Configure the RTC PRER */
  303. hrtc->Instance->PRER = (hrtc->Init.SynchPrediv);
  304. hrtc->Instance->PRER |= (hrtc->Init.AsynchPrediv << RTC_PRER_PREDIV_A_Pos);
  305. #if defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  306. /* Configure the Binary mode */
  307. MODIFY_REG(RTC->ICSR, RTC_ICSR_BIN | RTC_ICSR_BCDU, hrtc->Init.BinMode | hrtc->Init.BinMixBcdU);
  308. #endif
  309. /* Exit Initialization mode */
  310. status = RTC_ExitInitMode(hrtc);
  311. if (status == HAL_OK)
  312. {
  313. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  314. hrtc->Instance->CR &= ~(RTC_CR_TAMPALRM_PU | RTC_CR_TAMPALRM_TYPE | RTC_CR_OUT2EN);
  315. hrtc->Instance->CR |= (hrtc->Init.OutPutPullUp | hrtc->Init.OutPutType | hrtc->Init.OutPutRemap);
  316. #else
  317. hrtc->Instance->OR &= ~(RTC_OR_ALARMOUTTYPE | RTC_OR_OUT_RMP);
  318. hrtc->Instance->OR |= (hrtc->Init.OutPutType | hrtc->Init.OutPutRemap);
  319. #endif
  320. /* Enable the write protection for RTC registers */
  321. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  322. hrtc->State = HAL_RTC_STATE_READY;
  323. }
  324. }
  325. }
  326. return status;
  327. }
  328. /**
  329. * @brief DeInitialize the RTC peripheral.
  330. * @note This function does not reset the RTC Backup Data registers.
  331. * @param hrtc RTC handle
  332. * @retval HAL status
  333. */
  334. HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
  335. {
  336. HAL_StatusTypeDef status = HAL_ERROR;
  337. /* Check the RTC peripheral state */
  338. if (hrtc != NULL)
  339. {
  340. /* Check the parameters */
  341. assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
  342. /* Set RTC state */
  343. hrtc->State = HAL_RTC_STATE_BUSY;
  344. /* Disable the write protection for RTC registers */
  345. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  346. /* Enter Initialization mode */
  347. status = RTC_EnterInitMode(hrtc);
  348. if (status == HAL_OK)
  349. {
  350. /* Reset all RTC CR register bits */
  351. hrtc->Instance->TR = 0x00000000U;
  352. hrtc->Instance->DR = ((uint32_t)(RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
  353. hrtc->Instance->CR &= 0x00000000U;
  354. hrtc->Instance->WUTR = RTC_WUTR_WUT;
  355. hrtc->Instance->PRER = ((uint32_t)(RTC_PRER_PREDIV_A | 0x000000FFU));
  356. hrtc->Instance->ALRMAR = 0x00000000U;
  357. hrtc->Instance->ALRMBR = 0x00000000U;
  358. hrtc->Instance->SHIFTR = 0x00000000U;
  359. hrtc->Instance->CALR = 0x00000000U;
  360. hrtc->Instance->ALRMASSR = 0x00000000U;
  361. hrtc->Instance->ALRMBSSR = 0x00000000U;
  362. /* Exit initialization mode */
  363. status = RTC_ExitInitMode(hrtc);
  364. if (status == HAL_OK)
  365. {
  366. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  367. /* Reset TAMP registers */
  368. ((TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset))->CR1 = 0xFFFF0000U;
  369. ((TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset))->CR2 = 0x00000000U;
  370. #else
  371. /* Reset Tamper configuration register */
  372. hrtc->Instance->TAMPCR = 0x00000000U;
  373. /* Reset Option register */
  374. hrtc->Instance->OR = 0x00000000U;
  375. #endif
  376. /* Enable the write protection for RTC registers */
  377. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  378. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  379. if (hrtc->MspDeInitCallback == NULL)
  380. {
  381. hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
  382. }
  383. /* DeInit the low level hardware: CLOCK, NVIC.*/
  384. hrtc->MspDeInitCallback(hrtc);
  385. #else
  386. /* De-Initialize RTC MSP */
  387. HAL_RTC_MspDeInit(hrtc);
  388. #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
  389. hrtc->State = HAL_RTC_STATE_RESET;
  390. /* Release Lock */
  391. __HAL_UNLOCK(hrtc);
  392. }
  393. }
  394. }
  395. return status;
  396. }
  397. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  398. /**
  399. * @brief Register a User RTC Callback
  400. * To be used instead of the weak predefined callback
  401. * @param hrtc RTC handle
  402. * @param CallbackID ID of the callback to be registered
  403. * This parameter can be one of the following values:
  404. * @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID Alarm A Event Callback ID
  405. * @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID Alarm B Event Callback ID
  406. * @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID TimeStamp Event Callback ID
  407. * @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID WakeUp Timer Event Callback ID
  408. * @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID Tamper 1 Callback ID
  409. * @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID Tamper 2 Callback ID
  410. * @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID Tamper 3 Callback ID
  411. * @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
  412. * @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
  413. * @param pCallback pointer to the Callback function
  414. * @retval HAL status
  415. */
  416. HAL_StatusTypeDef HAL_RTC_RegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID, pRTC_CallbackTypeDef pCallback)
  417. {
  418. HAL_StatusTypeDef status = HAL_OK;
  419. if (pCallback == NULL)
  420. {
  421. return HAL_ERROR;
  422. }
  423. /* Process locked */
  424. __HAL_LOCK(hrtc);
  425. if (HAL_RTC_STATE_READY == hrtc->State)
  426. {
  427. switch (CallbackID)
  428. {
  429. case HAL_RTC_ALARM_A_EVENT_CB_ID :
  430. hrtc->AlarmAEventCallback = pCallback;
  431. break;
  432. case HAL_RTC_ALARM_B_EVENT_CB_ID :
  433. hrtc->AlarmBEventCallback = pCallback;
  434. break;
  435. case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
  436. hrtc->TimeStampEventCallback = pCallback;
  437. break;
  438. case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
  439. hrtc->WakeUpTimerEventCallback = pCallback;
  440. break;
  441. #if defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  442. case HAL_RTC_SSRU_EVENT_CB_ID :
  443. hrtc->SSRUEventCallback = pCallback;
  444. break;
  445. #endif
  446. #if defined(RTC_TAMPER1_SUPPORT)
  447. case HAL_RTC_TAMPER1_EVENT_CB_ID :
  448. hrtc->Tamper1EventCallback = pCallback;
  449. break;
  450. #endif /* RTC_TAMPER1_SUPPORT */
  451. case HAL_RTC_TAMPER2_EVENT_CB_ID :
  452. hrtc->Tamper2EventCallback = pCallback;
  453. break;
  454. #if defined(RTC_TAMPER3_SUPPORT)
  455. case HAL_RTC_TAMPER3_EVENT_CB_ID :
  456. hrtc->Tamper3EventCallback = pCallback;
  457. break;
  458. #endif /* RTC_TAMPER3_SUPPORT */
  459. case HAL_RTC_MSPINIT_CB_ID :
  460. hrtc->MspInitCallback = pCallback;
  461. break;
  462. case HAL_RTC_MSPDEINIT_CB_ID :
  463. hrtc->MspDeInitCallback = pCallback;
  464. break;
  465. default :
  466. /* Return error status */
  467. status = HAL_ERROR;
  468. break;
  469. }
  470. }
  471. else if (HAL_RTC_STATE_RESET == hrtc->State)
  472. {
  473. switch (CallbackID)
  474. {
  475. case HAL_RTC_MSPINIT_CB_ID :
  476. hrtc->MspInitCallback = pCallback;
  477. break;
  478. case HAL_RTC_MSPDEINIT_CB_ID :
  479. hrtc->MspDeInitCallback = pCallback;
  480. break;
  481. default :
  482. /* Return error status */
  483. status = HAL_ERROR;
  484. break;
  485. }
  486. }
  487. else
  488. {
  489. /* Return error status */
  490. status = HAL_ERROR;
  491. }
  492. /* Release Lock */
  493. __HAL_UNLOCK(hrtc);
  494. return status;
  495. }
  496. /**
  497. * @brief Unregister an RTC Callback
  498. * RTC callback is redirected to the weak predefined callback
  499. * @param hrtc RTC handle
  500. * @param CallbackID ID of the callback to be unregistered
  501. * This parameter can be one of the following values:
  502. * @arg @ref HAL_RTC_ALARM_A_EVENT_CB_ID Alarm A Event Callback ID
  503. * @arg @ref HAL_RTC_ALARM_B_EVENT_CB_ID Alarm B Event Callback ID
  504. * @arg @ref HAL_RTC_TIMESTAMP_EVENT_CB_ID TimeStamp Event Callback ID
  505. #if defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  506. * @arg @ref HAL_RTC_SSRU_EVENT_CB_ID SSRU Callback ID
  507. #endif
  508. * @arg @ref HAL_RTC_WAKEUPTIMER_EVENT_CB_ID WakeUp Timer Event Callback ID
  509. * @arg @ref HAL_RTC_TAMPER1_EVENT_CB_ID Tamper 1 Callback ID
  510. * @arg @ref HAL_RTC_TAMPER2_EVENT_CB_ID Tamper 2 Callback ID
  511. * @arg @ref HAL_RTC_TAMPER3_EVENT_CB_ID Tamper 3 Callback ID
  512. * @arg @ref HAL_RTC_MSPINIT_CB_ID Msp Init callback ID
  513. * @arg @ref HAL_RTC_MSPDEINIT_CB_ID Msp DeInit callback ID
  514. * @retval HAL status
  515. */
  516. HAL_StatusTypeDef HAL_RTC_UnRegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID)
  517. {
  518. HAL_StatusTypeDef status = HAL_OK;
  519. /* Process locked */
  520. __HAL_LOCK(hrtc);
  521. if (HAL_RTC_STATE_READY == hrtc->State)
  522. {
  523. switch (CallbackID)
  524. {
  525. case HAL_RTC_ALARM_A_EVENT_CB_ID :
  526. hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */
  527. break;
  528. case HAL_RTC_ALARM_B_EVENT_CB_ID :
  529. hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback; /* Legacy weak AlarmBEventCallback */
  530. break;
  531. case HAL_RTC_TIMESTAMP_EVENT_CB_ID :
  532. hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback; /* Legacy weak TimeStampEventCallback */
  533. break;
  534. case HAL_RTC_WAKEUPTIMER_EVENT_CB_ID :
  535. hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
  536. break;
  537. #if defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  538. case HAL_RTC_SSRU_EVENT_CB_ID :
  539. hrtc->SSRUEventCallback = HAL_RTCEx_SSRUEventCallback; /* Legacy weak SSRUEventCallback */
  540. break;
  541. #endif
  542. #if defined(RTC_TAMPER1_SUPPORT)
  543. case HAL_RTC_TAMPER1_EVENT_CB_ID :
  544. hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */
  545. break;
  546. #endif /* RTC_TAMPER1_SUPPORT */
  547. case HAL_RTC_TAMPER2_EVENT_CB_ID :
  548. hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback; /* Legacy weak Tamper2EventCallback */
  549. break;
  550. #if defined(RTC_TAMPER3_SUPPORT)
  551. case HAL_RTC_TAMPER3_EVENT_CB_ID :
  552. hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback; /* Legacy weak Tamper3EventCallback */
  553. break;
  554. #endif /* RTC_TAMPER3_SUPPORT */
  555. case HAL_RTC_MSPINIT_CB_ID :
  556. hrtc->MspInitCallback = HAL_RTC_MspInit;
  557. break;
  558. case HAL_RTC_MSPDEINIT_CB_ID :
  559. hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
  560. break;
  561. default :
  562. /* Return error status */
  563. status = HAL_ERROR;
  564. break;
  565. }
  566. }
  567. else if (HAL_RTC_STATE_RESET == hrtc->State)
  568. {
  569. switch (CallbackID)
  570. {
  571. case HAL_RTC_MSPINIT_CB_ID :
  572. hrtc->MspInitCallback = HAL_RTC_MspInit;
  573. break;
  574. case HAL_RTC_MSPDEINIT_CB_ID :
  575. hrtc->MspDeInitCallback = HAL_RTC_MspDeInit;
  576. break;
  577. default :
  578. /* Return error status */
  579. status = HAL_ERROR;
  580. break;
  581. }
  582. }
  583. else
  584. {
  585. /* Return error status */
  586. status = HAL_ERROR;
  587. }
  588. /* Release Lock */
  589. __HAL_UNLOCK(hrtc);
  590. return status;
  591. }
  592. #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
  593. /**
  594. * @brief Initialize the RTC MSP.
  595. * @param hrtc RTC handle
  596. * @retval None
  597. */
  598. __weak void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
  599. {
  600. /* Prevent unused argument(s) compilation warning */
  601. UNUSED(hrtc);
  602. /* NOTE : This function should not be modified, when the callback is needed,
  603. the HAL_RTC_MspInit could be implemented in the user file
  604. */
  605. }
  606. /**
  607. * @brief DeInitialize the RTC MSP.
  608. * @param hrtc RTC handle
  609. * @retval None
  610. */
  611. __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc)
  612. {
  613. /* Prevent unused argument(s) compilation warning */
  614. UNUSED(hrtc);
  615. /* NOTE : This function should not be modified, when the callback is needed,
  616. the HAL_RTC_MspDeInit could be implemented in the user file
  617. */
  618. }
  619. /**
  620. * @}
  621. */
  622. /** @addtogroup RTC_Exported_Functions_Group2
  623. * @brief RTC Time and Date functions
  624. *
  625. @verbatim
  626. ===============================================================================
  627. ##### RTC Time and Date functions #####
  628. ===============================================================================
  629. [..] This section provides functions allowing to configure Time and Date features
  630. @endverbatim
  631. * @{
  632. */
  633. #if defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  634. /**
  635. * @brief Set RTC current time.
  636. * @param hrtc RTC handle
  637. * @param sTime Pointer to Time structure
  638. * if Binary mode is RTC_BINARY_ONLY, this parameter is not used and RTC_SSR will be automatically reset to 0xFFFFFFFF
  639. else sTime->SubSeconds is not used and RTC_SSR will be automatically reset to the A 7-bit async prescaler (RTC_PRER_PREDIV_A)
  640. * @param Format Format of sTime->Hours, sTime->Minutes and sTime->Seconds.
  641. * if Binary mode is RTC_BINARY_ONLY, this parameter is not used
  642. * else this parameter can be one of the following values
  643. * @arg RTC_FORMAT_BIN: Binary format
  644. * @arg RTC_FORMAT_BCD: BCD format
  645. * @retval HAL status
  646. */
  647. HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
  648. {
  649. uint32_t tmpreg;
  650. HAL_StatusTypeDef status;
  651. #ifdef USE_FULL_ASSERT
  652. /* Check the parameters depending of the Binary mode with 32-bit free-running counter configuration. */
  653. if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_NONE)
  654. {
  655. /* Check the parameters */
  656. assert_param(IS_RTC_FORMAT(Format));
  657. assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
  658. assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
  659. }
  660. #endif
  661. /* Process Locked */
  662. __HAL_LOCK(hrtc);
  663. hrtc->State = HAL_RTC_STATE_BUSY;
  664. /* Disable the write protection for RTC registers */
  665. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  666. /* Enter Initialization mode */
  667. status = RTC_EnterInitMode(hrtc);
  668. if (status == HAL_OK)
  669. {
  670. /* Check Binary mode ((32-bit free-running counter) */
  671. if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) != RTC_BINARY_ONLY)
  672. {
  673. if (Format == RTC_FORMAT_BIN)
  674. {
  675. if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
  676. {
  677. assert_param(IS_RTC_HOUR12(sTime->Hours));
  678. assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
  679. }
  680. else
  681. {
  682. sTime->TimeFormat = 0x00U;
  683. assert_param(IS_RTC_HOUR24(sTime->Hours));
  684. }
  685. assert_param(IS_RTC_MINUTES(sTime->Minutes));
  686. assert_param(IS_RTC_SECONDS(sTime->Seconds));
  687. tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << RTC_TR_HU_Pos) | \
  688. ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << RTC_TR_MNU_Pos) | \
  689. ((uint32_t)RTC_ByteToBcd2(sTime->Seconds) << RTC_TR_SU_Pos) | \
  690. (((uint32_t)sTime->TimeFormat) << RTC_TR_PM_Pos));
  691. }
  692. else
  693. {
  694. if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
  695. {
  696. assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sTime->Hours)));
  697. assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
  698. }
  699. else
  700. {
  701. sTime->TimeFormat = 0x00U;
  702. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
  703. }
  704. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
  705. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
  706. tmpreg = (((uint32_t)(sTime->Hours) << RTC_TR_HU_Pos) | \
  707. ((uint32_t)(sTime->Minutes) << RTC_TR_MNU_Pos) | \
  708. ((uint32_t)(sTime->Seconds) << RTC_TR_SU_Pos) | \
  709. ((uint32_t)(sTime->TimeFormat) << RTC_TR_PM_Pos));
  710. }
  711. /* Set the RTC_TR register */
  712. WRITE_REG(RTC->TR, (tmpreg & RTC_TR_RESERVED_MASK));
  713. /* This interface is deprecated. To manage Daylight Saving Time, please use HAL_RTC_DST_xxx functions */
  714. CLEAR_BIT(RTC->CR, RTC_CR_BKP);
  715. /* This interface is deprecated. To manage Daylight Saving Time, please use HAL_RTC_DST_xxx functions */
  716. SET_BIT(RTC->CR, (sTime->DayLightSaving | sTime->StoreOperation));
  717. }
  718. }
  719. /* Exit Initialization mode */
  720. status = RTC_ExitInitMode(hrtc);
  721. /* Enable the write protection for RTC registers */
  722. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  723. if (status == HAL_OK)
  724. {
  725. hrtc->State = HAL_RTC_STATE_READY;
  726. }
  727. /* Process Unlocked */
  728. __HAL_UNLOCK(hrtc);
  729. return status;
  730. }
  731. /**
  732. * @brief Get RTC current time.
  733. * @note You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
  734. * value in second fraction ratio with time unit following generic formula:
  735. * Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
  736. * This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
  737. * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
  738. * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
  739. * Reading RTC current time locks the values in calendar shadow registers until Current date is read
  740. * to ensure consistency between the time and date values.
  741. * @param hrtc RTC handle
  742. * @param sTime
  743. * if Binary mode is RTC_BINARY_ONLY, sTime->SubSeconds only is updated
  744. * else
  745. * Pointer to Time structure with Hours, Minutes and Seconds fields returned
  746. * with input format (BIN or BCD), also SubSeconds field returning the
  747. * RTC_SSR register content and SecondFraction field the Synchronous pre-scaler
  748. * factor to be used for second fraction ratio computation.
  749. * @param Format Format of sTime->Hours, sTime->Minutes and sTime->Seconds.
  750. * if Binary mode is RTC_BINARY_ONLY, this parameter is not used
  751. * else this parameter can be one of the following values:
  752. * @arg RTC_FORMAT_BIN: Binary format
  753. * @arg RTC_FORMAT_BCD: BCD format
  754. * @retval HAL status
  755. */
  756. HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
  757. {
  758. uint32_t tmpreg;
  759. UNUSED(hrtc);
  760. /* Get subseconds structure field from the corresponding register*/
  761. sTime->SubSeconds = READ_REG(RTC->SSR);
  762. if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) != RTC_BINARY_ONLY)
  763. {
  764. /* Check the parameters */
  765. assert_param(IS_RTC_FORMAT(Format));
  766. /* Get SecondFraction structure field from the corresponding register field*/
  767. sTime->SecondFraction = (uint32_t)(READ_REG(RTC->PRER) & RTC_PRER_PREDIV_S);
  768. /* Get the TR register */
  769. tmpreg = (uint32_t)(READ_REG(RTC->TR) & RTC_TR_RESERVED_MASK);
  770. /* Fill the structure fields with the read parameters */
  771. sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> RTC_TR_HU_Pos);
  772. sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> RTC_TR_MNU_Pos);
  773. sTime->Seconds = (uint8_t)((tmpreg & (RTC_TR_ST | RTC_TR_SU)) >> RTC_TR_SU_Pos);
  774. sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> RTC_TR_PM_Pos);
  775. /* Check the input parameters format */
  776. if (Format == RTC_FORMAT_BIN)
  777. {
  778. /* Convert the time structure parameters to Binary format */
  779. sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
  780. sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
  781. sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
  782. }
  783. }
  784. return HAL_OK;
  785. }
  786. /**
  787. * @brief Set RTC current date.
  788. * @param hrtc RTC handle
  789. * @param sDate Pointer to date structure
  790. * @param Format Format of sDate->Year, sDate->Month and sDate->Weekday.
  791. * This parameter can be one of the following values:
  792. * @arg RTC_FORMAT_BIN: Binary format
  793. * @arg RTC_FORMAT_BCD: BCD format
  794. * @retval HAL status
  795. */
  796. HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
  797. {
  798. uint32_t datetmpreg;
  799. HAL_StatusTypeDef status;
  800. /* Check the parameters */
  801. assert_param(IS_RTC_FORMAT(Format));
  802. /* Process Locked */
  803. __HAL_LOCK(hrtc);
  804. hrtc->State = HAL_RTC_STATE_BUSY;
  805. if ((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U))
  806. {
  807. sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU);
  808. }
  809. assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
  810. if (Format == RTC_FORMAT_BIN)
  811. {
  812. assert_param(IS_RTC_YEAR(sDate->Year));
  813. assert_param(IS_RTC_MONTH(sDate->Month));
  814. assert_param(IS_RTC_DATE(sDate->Date));
  815. datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << RTC_DR_YU_Pos) | \
  816. ((uint32_t)RTC_ByteToBcd2(sDate->Month) << RTC_DR_MU_Pos) | \
  817. ((uint32_t)RTC_ByteToBcd2(sDate->Date) << RTC_DR_DU_Pos) | \
  818. ((uint32_t)sDate->WeekDay << RTC_DR_WDU_Pos));
  819. }
  820. else
  821. {
  822. assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
  823. assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
  824. assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));
  825. datetmpreg = ((((uint32_t)sDate->Year) << RTC_DR_YU_Pos) | \
  826. (((uint32_t)sDate->Month) << RTC_DR_MU_Pos) | \
  827. (((uint32_t)sDate->Date) << RTC_DR_DU_Pos) | \
  828. (((uint32_t)sDate->WeekDay) << RTC_DR_WDU_Pos));
  829. }
  830. /* Disable the write protection for RTC registers */
  831. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  832. /* Enter Initialization mode */
  833. status = RTC_EnterInitMode(hrtc);
  834. if (status == HAL_OK)
  835. {
  836. /* Set the RTC_DR register */
  837. WRITE_REG(RTC->DR, (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK));
  838. /* Exit Initialization mode */
  839. status = RTC_ExitInitMode(hrtc);
  840. }
  841. /* Enable the write protection for RTC registers */
  842. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  843. if (status == HAL_OK)
  844. {
  845. hrtc->State = HAL_RTC_STATE_READY ;
  846. }
  847. /* Process Unlocked */
  848. __HAL_UNLOCK(hrtc);
  849. return status;
  850. }
  851. /**
  852. * @brief Get RTC current date.
  853. * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
  854. * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
  855. * Reading RTC current time locks the values in calendar shadow registers until Current date is read.
  856. * @param hrtc RTC handle
  857. * @param sDate Pointer to Date structure
  858. * @param Format Format of sDate->Year, sDate->Month and sDate->Weekday.
  859. * This parameter can be one of the following values:
  860. * @arg RTC_FORMAT_BIN: Binary format
  861. * @arg RTC_FORMAT_BCD: BCD format
  862. * @retval HAL status
  863. */
  864. HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
  865. {
  866. uint32_t datetmpreg;
  867. UNUSED(hrtc);
  868. /* Check the parameters */
  869. assert_param(IS_RTC_FORMAT(Format));
  870. /* Get the DR register */
  871. datetmpreg = (uint32_t)(READ_REG(RTC->DR) & RTC_DR_RESERVED_MASK);
  872. /* Fill the structure fields with the read parameters */
  873. sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> RTC_DR_YU_Pos);
  874. sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> RTC_DR_MU_Pos);
  875. sDate->Date = (uint8_t)((datetmpreg & (RTC_DR_DT | RTC_DR_DU)) >> RTC_DR_DU_Pos);
  876. sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> RTC_DR_WDU_Pos);
  877. /* Check the input parameters format */
  878. if (Format == RTC_FORMAT_BIN)
  879. {
  880. /* Convert the date structure parameters to Binary format */
  881. sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
  882. sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
  883. sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
  884. }
  885. return HAL_OK;
  886. }
  887. /**
  888. * @}
  889. */
  890. /** @addtogroup RTC_Exported_Functions_Group3
  891. * @brief RTC Alarm functions
  892. *
  893. @verbatim
  894. ===============================================================================
  895. ##### RTC Alarm functions #####
  896. ===============================================================================
  897. [..] This section provides functions allowing to configure Alarm feature
  898. @endverbatim
  899. * @{
  900. */
  901. /**
  902. * @brief Set the specified RTC Alarm.
  903. * @param hrtc RTC handle
  904. * @param sAlarm Pointer to Alarm structure
  905. * if Binary mode is RTC_BINARY_ONLY, 3 fields only are used
  906. * sAlarm->AlarmTime.SubSeconds
  907. * sAlarm->AlarmSubSecondMask
  908. * sAlarm->BinaryAutoClr
  909. * @param Format of the entered parameters.
  910. * if Binary mode is RTC_BINARY_ONLY, this parameter is not used
  911. * else this parameter can be one of the following values
  912. * @arg RTC_FORMAT_BIN: Binary format
  913. * @arg RTC_FORMAT_BCD: BCD format
  914. * @retval HAL status
  915. */
  916. HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
  917. {
  918. uint32_t tmpreg = 0, binaryMode;
  919. __HAL_LOCK(hrtc);
  920. hrtc->State = HAL_RTC_STATE_BUSY;
  921. #ifdef USE_FULL_ASSERT
  922. /* Check the parameters depending of the Binary mode (32-bit free-running counter configuration). */
  923. if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_NONE)
  924. {
  925. assert_param(IS_RTC_FORMAT(Format));
  926. assert_param(IS_RTC_ALARM(sAlarm->Alarm));
  927. assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
  928. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
  929. assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
  930. assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
  931. }
  932. else if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_ONLY)
  933. {
  934. assert_param(IS_RTC_ALARM_SUB_SECOND_BINARY_MASK(sAlarm->AlarmSubSecondMask));
  935. assert_param(IS_RTC_ALARMSUBSECONDBIN_AUTOCLR(sAlarm->BinaryAutoClr));
  936. }
  937. else /* RTC_BINARY_MIX */
  938. {
  939. assert_param(IS_RTC_FORMAT(Format));
  940. assert_param(IS_RTC_ALARM(sAlarm->Alarm));
  941. assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
  942. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
  943. /* In Binary Mix Mode, the RTC can not generate an alarm on a match involving all calendar items + the upper SSR bits */
  944. assert_param((sAlarm->AlarmSubSecondMask >> RTC_ALRMASSR_MASKSS_Pos) <= (8U + (READ_BIT(RTC->ICSR, RTC_ICSR_BCDU) >> RTC_ICSR_BCDU_Pos)));
  945. }
  946. #endif
  947. /* Get Binary mode (32-bit free-running counter configuration) */
  948. binaryMode = READ_BIT(RTC->ICSR, RTC_ICSR_BIN);
  949. if (binaryMode != RTC_BINARY_ONLY)
  950. {
  951. if (Format == RTC_FORMAT_BIN)
  952. {
  953. if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
  954. {
  955. assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
  956. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  957. }
  958. else
  959. {
  960. sAlarm->AlarmTime.TimeFormat = 0x00U;
  961. assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
  962. }
  963. assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
  964. assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
  965. if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  966. {
  967. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
  968. }
  969. else
  970. {
  971. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
  972. }
  973. tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
  974. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
  975. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
  976. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
  977. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
  978. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  979. ((uint32_t)sAlarm->AlarmMask));
  980. }
  981. else /* format BCD */
  982. {
  983. if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
  984. {
  985. assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  986. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  987. }
  988. else
  989. {
  990. sAlarm->AlarmTime.TimeFormat = 0x00U;
  991. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  992. }
  993. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
  994. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
  995. #ifdef USE_FULL_ASSERT
  996. if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  997. {
  998. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
  999. }
  1000. else
  1001. {
  1002. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
  1003. }
  1004. #endif /* USE_FULL_ASSERT */
  1005. tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
  1006. ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
  1007. ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
  1008. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
  1009. ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
  1010. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  1011. ((uint32_t)sAlarm->AlarmMask));
  1012. }
  1013. }
  1014. /* Disable the write protection for RTC registers */
  1015. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1016. /* Configure the Alarm register */
  1017. if (sAlarm->Alarm == RTC_ALARM_A)
  1018. {
  1019. /* Disable the Alarm A interrupt */
  1020. /* In case of interrupt mode is used, the interrupt source must disabled */
  1021. CLEAR_BIT(RTC->CR, (RTC_CR_ALRAE | RTC_CR_ALRAIE));
  1022. /* Clear flag alarm A */
  1023. WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
  1024. if (binaryMode == RTC_BINARY_ONLY)
  1025. {
  1026. WRITE_REG(RTC->ALRMASSR, sAlarm->AlarmSubSecondMask | sAlarm->BinaryAutoClr);
  1027. }
  1028. else
  1029. {
  1030. WRITE_REG(RTC->ALRMAR, tmpreg);
  1031. WRITE_REG(RTC->ALRMASSR, sAlarm->AlarmSubSecondMask);
  1032. }
  1033. WRITE_REG(RTC->ALRABINR, sAlarm->AlarmTime.SubSeconds);
  1034. /* Configure the Alarm state: Enable Alarm */
  1035. SET_BIT(RTC->CR, RTC_CR_ALRAE);
  1036. }
  1037. else
  1038. {
  1039. /* Disable the Alarm B interrupt */
  1040. /* In case of interrupt mode is used, the interrupt source must disabled */
  1041. CLEAR_BIT(RTC->CR, (RTC_CR_ALRBE | RTC_CR_ALRBIE));
  1042. /* Clear flag alarm B */
  1043. WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
  1044. if (binaryMode == RTC_BINARY_ONLY)
  1045. {
  1046. WRITE_REG(RTC->ALRMBSSR, sAlarm->AlarmSubSecondMask | sAlarm->BinaryAutoClr);
  1047. }
  1048. else
  1049. {
  1050. WRITE_REG(RTC->ALRMBR, tmpreg);
  1051. WRITE_REG(RTC->ALRMBSSR, sAlarm->AlarmSubSecondMask);
  1052. }
  1053. WRITE_REG(RTC->ALRBBINR, sAlarm->AlarmTime.SubSeconds);
  1054. /* Configure the Alarm state: Enable Alarm */
  1055. SET_BIT(RTC->CR, RTC_CR_ALRBE);
  1056. }
  1057. /* Enable the write protection for RTC registers */
  1058. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1059. /* Change RTC state */
  1060. hrtc->State = HAL_RTC_STATE_READY;
  1061. /* Process Unlocked */
  1062. __HAL_UNLOCK(hrtc);
  1063. return HAL_OK;
  1064. }
  1065. /**
  1066. * @brief Set the specified RTC Alarm with Interrupt.
  1067. * @param hrtc RTC handle
  1068. * @param sAlarm Pointer to Alarm structure
  1069. * if Binary mode is RTC_BINARY_ONLY, 3 fields only are used
  1070. * sAlarm->AlarmTime.SubSeconds
  1071. * sAlarm->AlarmSubSecondMask
  1072. * sAlarm->BinaryAutoClr
  1073. * @param Format Specifies the format of the entered parameters.
  1074. * if Binary mode is RTC_BINARY_ONLY, this parameter is not used
  1075. * else this parameter can be one of the following values
  1076. * @arg RTC_FORMAT_BIN: Binary format
  1077. * @arg RTC_FORMAT_BCD: BCD format
  1078. * @retval HAL status
  1079. */
  1080. HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
  1081. {
  1082. uint32_t tmpreg = 0, binaryMode;
  1083. /* Process Locked */
  1084. __HAL_LOCK(hrtc);
  1085. hrtc->State = HAL_RTC_STATE_BUSY;
  1086. #ifdef USE_FULL_ASSERT
  1087. /* Check the parameters depending of the Binary mode (32-bit free-running counter configuration). */
  1088. if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_NONE)
  1089. {
  1090. assert_param(IS_RTC_FORMAT(Format));
  1091. assert_param(IS_RTC_ALARM(sAlarm->Alarm));
  1092. assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
  1093. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
  1094. assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
  1095. assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
  1096. }
  1097. else if (READ_BIT(RTC->ICSR, RTC_ICSR_BIN) == RTC_BINARY_ONLY)
  1098. {
  1099. assert_param(IS_RTC_ALARM_SUB_SECOND_BINARY_MASK(sAlarm->AlarmSubSecondMask));
  1100. assert_param(IS_RTC_ALARMSUBSECONDBIN_AUTOCLR(sAlarm->BinaryAutoClr));
  1101. }
  1102. else /* RTC_BINARY_MIX */
  1103. {
  1104. assert_param(IS_RTC_FORMAT(Format));
  1105. assert_param(IS_RTC_ALARM(sAlarm->Alarm));
  1106. assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
  1107. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
  1108. /* In Binary Mix Mode, the RTC can not generate an alarm on a match involving all calendar items + the upper SSR bits */
  1109. assert_param((sAlarm->AlarmSubSecondMask >> RTC_ALRMASSR_MASKSS_Pos) <= (8U + (READ_BIT(RTC->ICSR, RTC_ICSR_BCDU) >> RTC_ICSR_BCDU_Pos)));
  1110. }
  1111. #endif
  1112. /* Get Binary mode (32-bit free-running counter configuration) */
  1113. binaryMode = READ_BIT(RTC->ICSR, RTC_ICSR_BIN);
  1114. if (binaryMode != RTC_BINARY_ONLY)
  1115. {
  1116. if (Format == RTC_FORMAT_BIN)
  1117. {
  1118. if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
  1119. {
  1120. assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
  1121. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  1122. }
  1123. else
  1124. {
  1125. sAlarm->AlarmTime.TimeFormat = 0x00U;
  1126. assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
  1127. }
  1128. assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
  1129. assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
  1130. if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  1131. {
  1132. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
  1133. }
  1134. else
  1135. {
  1136. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
  1137. }
  1138. tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
  1139. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
  1140. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
  1141. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
  1142. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
  1143. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  1144. ((uint32_t)sAlarm->AlarmMask));
  1145. }
  1146. else /* Format BCD */
  1147. {
  1148. if (READ_BIT(RTC->CR, RTC_CR_FMT) != 0U)
  1149. {
  1150. assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  1151. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  1152. }
  1153. else
  1154. {
  1155. sAlarm->AlarmTime.TimeFormat = 0x00U;
  1156. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  1157. }
  1158. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
  1159. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
  1160. #ifdef USE_FULL_ASSERT
  1161. if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  1162. {
  1163. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
  1164. }
  1165. else
  1166. {
  1167. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
  1168. }
  1169. #endif /* USE_FULL_ASSERT */
  1170. tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
  1171. ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
  1172. ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
  1173. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
  1174. ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
  1175. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  1176. ((uint32_t)sAlarm->AlarmMask));
  1177. }
  1178. }
  1179. /* Disable the write protection for RTC registers */
  1180. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1181. /* Configure the Alarm register */
  1182. if (sAlarm->Alarm == RTC_ALARM_A)
  1183. {
  1184. /* Disable the Alarm A interrupt */
  1185. CLEAR_BIT(RTC->CR, RTC_CR_ALRAE | RTC_CR_ALRAIE);
  1186. /* Clear flag alarm A */
  1187. WRITE_REG(RTC->SCR, RTC_SCR_CALRAF);
  1188. if (binaryMode == RTC_BINARY_ONLY)
  1189. {
  1190. RTC->ALRMASSR = sAlarm->AlarmSubSecondMask | sAlarm->BinaryAutoClr;
  1191. }
  1192. else
  1193. {
  1194. WRITE_REG(RTC->ALRMAR, tmpreg);
  1195. WRITE_REG(RTC->ALRMASSR, sAlarm->AlarmSubSecondMask);
  1196. }
  1197. WRITE_REG(RTC->ALRABINR, sAlarm->AlarmTime.SubSeconds);
  1198. /* Configure the Alarm interrupt */
  1199. SET_BIT(RTC->CR, RTC_CR_ALRAE | RTC_CR_ALRAIE);
  1200. }
  1201. else
  1202. {
  1203. /* Disable the Alarm B interrupt */
  1204. CLEAR_BIT(RTC->CR, RTC_CR_ALRBE | RTC_CR_ALRBIE);
  1205. /* Clear flag alarm B */
  1206. WRITE_REG(RTC->SCR, RTC_SCR_CALRBF);
  1207. if (binaryMode == RTC_BINARY_ONLY)
  1208. {
  1209. WRITE_REG(RTC->ALRMBSSR, sAlarm->AlarmSubSecondMask | sAlarm->BinaryAutoClr);
  1210. }
  1211. else
  1212. {
  1213. WRITE_REG(RTC->ALRMBR, tmpreg);
  1214. WRITE_REG(RTC->ALRMBSSR, sAlarm->AlarmSubSecondMask);
  1215. }
  1216. WRITE_REG(RTC->ALRBBINR, sAlarm->AlarmTime.SubSeconds);
  1217. /* Configure the Alarm interrupt */
  1218. SET_BIT(RTC->CR, RTC_CR_ALRBE | RTC_CR_ALRBIE);
  1219. }
  1220. /* RTC Alarm Interrupt Configuration: EXTI configuration */
  1221. __HAL_RTC_ALARM_EXTI_ENABLE_IT();
  1222. __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
  1223. /* Enable the write protection for RTC registers */
  1224. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1225. hrtc->State = HAL_RTC_STATE_READY;
  1226. /* Process Unlocked */
  1227. __HAL_UNLOCK(hrtc);
  1228. return HAL_OK;
  1229. }
  1230. #else /* #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
  1231. /**
  1232. * @brief Set RTC current time.
  1233. * @param hrtc RTC handle
  1234. * @param sTime Pointer to Time structure
  1235. * @param Format Specifies the format of the entered parameters.
  1236. * This parameter can be one of the following values:
  1237. * @arg RTC_FORMAT_BIN: Binary data format
  1238. * @arg RTC_FORMAT_BCD: BCD data format
  1239. * @retval HAL status
  1240. */
  1241. HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
  1242. {
  1243. uint32_t tmpreg;
  1244. HAL_StatusTypeDef status;
  1245. /* Check the parameters */
  1246. assert_param(IS_RTC_FORMAT(Format));
  1247. assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
  1248. assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
  1249. /* Process Locked */
  1250. __HAL_LOCK(hrtc);
  1251. hrtc->State = HAL_RTC_STATE_BUSY;
  1252. /* Disable the write protection for RTC registers */
  1253. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1254. /* Enter Initialization mode */
  1255. status = RTC_EnterInitMode(hrtc);
  1256. if (status == HAL_OK)
  1257. {
  1258. if (Format == RTC_FORMAT_BIN)
  1259. {
  1260. if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
  1261. {
  1262. assert_param(IS_RTC_HOUR12(sTime->Hours));
  1263. assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
  1264. }
  1265. else
  1266. {
  1267. sTime->TimeFormat = 0x00U;
  1268. assert_param(IS_RTC_HOUR24(sTime->Hours));
  1269. }
  1270. assert_param(IS_RTC_MINUTES(sTime->Minutes));
  1271. assert_param(IS_RTC_SECONDS(sTime->Seconds));
  1272. tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << RTC_TR_HU_Pos) | \
  1273. ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << RTC_TR_MNU_Pos) | \
  1274. ((uint32_t)RTC_ByteToBcd2(sTime->Seconds) << RTC_TR_SU_Pos) | \
  1275. (((uint32_t)sTime->TimeFormat) << RTC_TR_PM_Pos));
  1276. }
  1277. else
  1278. {
  1279. if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
  1280. {
  1281. assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sTime->Hours)));
  1282. assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
  1283. }
  1284. else
  1285. {
  1286. sTime->TimeFormat = 0x00U;
  1287. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
  1288. }
  1289. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
  1290. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
  1291. tmpreg = (((uint32_t)(sTime->Hours) << RTC_TR_HU_Pos) | \
  1292. ((uint32_t)(sTime->Minutes) << RTC_TR_MNU_Pos) | \
  1293. ((uint32_t)(sTime->Seconds) << RTC_TR_SU_Pos) | \
  1294. ((uint32_t)(sTime->TimeFormat) << RTC_TR_PM_Pos));
  1295. }
  1296. /* Set the RTC_TR register */
  1297. hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
  1298. /* Clear the bits to be configured */
  1299. hrtc->Instance->CR &= ((uint32_t)~RTC_CR_BKP);
  1300. /* Configure the RTC_CR register */
  1301. hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
  1302. /* Exit Initialization mode */
  1303. status = RTC_ExitInitMode(hrtc);
  1304. }
  1305. /* Enable the write protection for RTC registers */
  1306. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1307. if (status == HAL_OK)
  1308. {
  1309. hrtc->State = HAL_RTC_STATE_READY;
  1310. }
  1311. /* Process Unlocked */
  1312. __HAL_UNLOCK(hrtc);
  1313. return status;
  1314. }
  1315. /**
  1316. * @brief Get RTC current time.
  1317. * @note You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
  1318. * value in second fraction ratio with time unit following generic formula:
  1319. * Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
  1320. * This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
  1321. * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
  1322. * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
  1323. * Reading RTC current time locks the values in calendar shadow registers until Current date is read
  1324. * to ensure consistency between the time and date values.
  1325. * @param hrtc RTC handle
  1326. * @param sTime Pointer to Time structure with Hours, Minutes and Seconds fields returned
  1327. * with input format (BIN or BCD), also SubSeconds field returning the
  1328. * RTC_SSR register content and SecondFraction field the Synchronous pre-scaler
  1329. * factor to be used for second fraction ratio computation.
  1330. * @param Format Specifies the format of the entered parameters.
  1331. * This parameter can be one of the following values:
  1332. * @arg RTC_FORMAT_BIN: Binary data format
  1333. * @arg RTC_FORMAT_BCD: BCD data format
  1334. * @retval HAL status
  1335. */
  1336. HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
  1337. {
  1338. uint32_t tmpreg;
  1339. /* Check the parameters */
  1340. assert_param(IS_RTC_FORMAT(Format));
  1341. /* Get subseconds structure field from the corresponding register*/
  1342. sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR);
  1343. /* Get SecondFraction structure field from the corresponding register field*/
  1344. sTime->SecondFraction = (uint32_t)(hrtc->Instance->PRER & RTC_PRER_PREDIV_S);
  1345. /* Get the TR register */
  1346. tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);
  1347. /* Fill the structure fields with the read parameters */
  1348. sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> RTC_TR_HU_Pos);
  1349. sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> RTC_TR_MNU_Pos);
  1350. sTime->Seconds = (uint8_t)((tmpreg & (RTC_TR_ST | RTC_TR_SU)) >> RTC_TR_SU_Pos);
  1351. sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> RTC_TR_PM_Pos);
  1352. /* Check the input parameters format */
  1353. if (Format == RTC_FORMAT_BIN)
  1354. {
  1355. /* Convert the time structure parameters to Binary format */
  1356. sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
  1357. sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
  1358. sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
  1359. }
  1360. return HAL_OK;
  1361. }
  1362. /**
  1363. * @brief Set RTC current date.
  1364. * @param hrtc RTC handle
  1365. * @param sDate Pointer to date structure
  1366. * @param Format specifies the format of the entered parameters.
  1367. * This parameter can be one of the following values:
  1368. * @arg RTC_FORMAT_BIN: Binary data format
  1369. * @arg RTC_FORMAT_BCD: BCD data format
  1370. * @retval HAL status
  1371. */
  1372. HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
  1373. {
  1374. uint32_t datetmpreg;
  1375. HAL_StatusTypeDef status;
  1376. /* Check the parameters */
  1377. assert_param(IS_RTC_FORMAT(Format));
  1378. /* Process Locked */
  1379. __HAL_LOCK(hrtc);
  1380. hrtc->State = HAL_RTC_STATE_BUSY;
  1381. if ((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U))
  1382. {
  1383. sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU);
  1384. }
  1385. assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
  1386. if (Format == RTC_FORMAT_BIN)
  1387. {
  1388. assert_param(IS_RTC_YEAR(sDate->Year));
  1389. assert_param(IS_RTC_MONTH(sDate->Month));
  1390. assert_param(IS_RTC_DATE(sDate->Date));
  1391. datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << RTC_DR_YU_Pos) | \
  1392. ((uint32_t)RTC_ByteToBcd2(sDate->Month) << RTC_DR_MU_Pos) | \
  1393. ((uint32_t)RTC_ByteToBcd2(sDate->Date) << RTC_DR_DU_Pos) | \
  1394. ((uint32_t)sDate->WeekDay << RTC_DR_WDU_Pos));
  1395. }
  1396. else
  1397. {
  1398. assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
  1399. assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
  1400. assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));
  1401. datetmpreg = ((((uint32_t)sDate->Year) << RTC_DR_YU_Pos) | \
  1402. (((uint32_t)sDate->Month) << RTC_DR_MU_Pos) | \
  1403. (((uint32_t)sDate->Date) << RTC_DR_DU_Pos) | \
  1404. (((uint32_t)sDate->WeekDay) << RTC_DR_WDU_Pos));
  1405. }
  1406. /* Disable the write protection for RTC registers */
  1407. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1408. /* Enter Initialization mode */
  1409. status = RTC_EnterInitMode(hrtc);
  1410. if (status == HAL_OK)
  1411. {
  1412. /* Set the RTC_DR register */
  1413. hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
  1414. /* Exit Initialization mode */
  1415. status = RTC_ExitInitMode(hrtc);
  1416. }
  1417. /* Enable the write protection for RTC registers */
  1418. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1419. if (status == HAL_OK)
  1420. {
  1421. hrtc->State = HAL_RTC_STATE_READY ;
  1422. }
  1423. /* Process Unlocked */
  1424. __HAL_UNLOCK(hrtc);
  1425. return status;
  1426. }
  1427. /**
  1428. * @brief Get RTC current date.
  1429. * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
  1430. * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
  1431. * Reading RTC current time locks the values in calendar shadow registers until Current date is read.
  1432. * @param hrtc RTC handle
  1433. * @param sDate Pointer to Date structure
  1434. * @param Format Specifies the format of the entered parameters.
  1435. * This parameter can be one of the following values:
  1436. * @arg RTC_FORMAT_BIN: Binary data format
  1437. * @arg RTC_FORMAT_BCD: BCD data format
  1438. * @retval HAL status
  1439. */
  1440. HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
  1441. {
  1442. uint32_t datetmpreg;
  1443. /* Check the parameters */
  1444. assert_param(IS_RTC_FORMAT(Format));
  1445. /* Get the DR register */
  1446. datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
  1447. /* Fill the structure fields with the read parameters */
  1448. sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> RTC_DR_YU_Pos);
  1449. sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> RTC_DR_MU_Pos);
  1450. sDate->Date = (uint8_t)((datetmpreg & (RTC_DR_DT | RTC_DR_DU)) >> RTC_DR_DU_Pos);
  1451. sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> RTC_DR_WDU_Pos);
  1452. /* Check the input parameters format */
  1453. if (Format == RTC_FORMAT_BIN)
  1454. {
  1455. /* Convert the date structure parameters to Binary format */
  1456. sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
  1457. sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
  1458. sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
  1459. }
  1460. return HAL_OK;
  1461. }
  1462. /**
  1463. * @}
  1464. */
  1465. /** @addtogroup RTC_Exported_Functions_Group3
  1466. * @brief RTC Alarm functions
  1467. *
  1468. @verbatim
  1469. ===============================================================================
  1470. ##### RTC Alarm functions #####
  1471. ===============================================================================
  1472. [..] This section provides functions allowing to configure Alarm feature
  1473. @endverbatim
  1474. * @{
  1475. */
  1476. /**
  1477. * @brief Set the specified RTC Alarm.
  1478. * @param hrtc RTC handle
  1479. * @param sAlarm Pointer to Alarm structure
  1480. * @param Format Specifies the format of the entered parameters.
  1481. * This parameter can be one of the following values:
  1482. * @arg RTC_FORMAT_BIN: Binary data format
  1483. * @arg RTC_FORMAT_BCD: BCD data format
  1484. * @retval HAL status
  1485. */
  1486. HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
  1487. {
  1488. uint32_t tmpreg, subsecondtmpreg;
  1489. /* Check the parameters */
  1490. assert_param(IS_RTC_FORMAT(Format));
  1491. assert_param(IS_RTC_ALARM(sAlarm->Alarm));
  1492. assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
  1493. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
  1494. assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
  1495. assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
  1496. /* Process Locked */
  1497. __HAL_LOCK(hrtc);
  1498. hrtc->State = HAL_RTC_STATE_BUSY;
  1499. if (Format == RTC_FORMAT_BIN)
  1500. {
  1501. if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
  1502. {
  1503. assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
  1504. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  1505. }
  1506. else
  1507. {
  1508. sAlarm->AlarmTime.TimeFormat = 0x00U;
  1509. assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
  1510. }
  1511. assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
  1512. assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
  1513. if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  1514. {
  1515. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
  1516. }
  1517. else
  1518. {
  1519. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
  1520. }
  1521. tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
  1522. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
  1523. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
  1524. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
  1525. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
  1526. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  1527. ((uint32_t)sAlarm->AlarmMask));
  1528. }
  1529. else
  1530. {
  1531. if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
  1532. {
  1533. assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  1534. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  1535. }
  1536. else
  1537. {
  1538. sAlarm->AlarmTime.TimeFormat = 0x00U;
  1539. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  1540. }
  1541. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
  1542. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
  1543. #ifdef USE_FULL_ASSERT
  1544. if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  1545. {
  1546. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
  1547. }
  1548. else
  1549. {
  1550. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
  1551. }
  1552. #endif /* USE_FULL_ASSERT */
  1553. tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
  1554. ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
  1555. ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
  1556. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
  1557. ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
  1558. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  1559. ((uint32_t)sAlarm->AlarmMask));
  1560. }
  1561. /* Configure the Alarm A or Alarm B Sub Second registers */
  1562. subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
  1563. /* Disable the write protection for RTC registers */
  1564. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1565. /* Configure the Alarm register */
  1566. if (sAlarm->Alarm == RTC_ALARM_A)
  1567. {
  1568. /* Disable the Alarm A interrupt */
  1569. __HAL_RTC_ALARMA_DISABLE(hrtc);
  1570. /* Clear flag alarm A */
  1571. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
  1572. /* In case of interrupt mode is used, the interrupt source must disabled */
  1573. __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
  1574. #if defined (RTC_FLAG_ALRAWF)
  1575. uint32_t tickstart = HAL_GetTick();
  1576. /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
  1577. while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
  1578. {
  1579. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1580. {
  1581. /* Enable the write protection for RTC registers */
  1582. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1583. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1584. /* Process Unlocked */
  1585. __HAL_UNLOCK(hrtc);
  1586. return HAL_TIMEOUT;
  1587. }
  1588. }
  1589. #endif
  1590. hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
  1591. /* Configure the Alarm A Sub Second register */
  1592. hrtc->Instance->ALRMASSR = subsecondtmpreg;
  1593. /* Configure the Alarm state: Enable Alarm */
  1594. __HAL_RTC_ALARMA_ENABLE(hrtc);
  1595. }
  1596. else
  1597. {
  1598. /* Disable the Alarm B interrupt */
  1599. __HAL_RTC_ALARMB_DISABLE(hrtc);
  1600. /* Clear flag alarm B */
  1601. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
  1602. /* In case of interrupt mode is used, the interrupt source must disabled */
  1603. __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
  1604. #if defined (RTC_FLAG_ALRBWF)
  1605. uint32_t tickstart = HAL_GetTick();
  1606. /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
  1607. while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
  1608. {
  1609. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1610. {
  1611. /* Enable the write protection for RTC registers */
  1612. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1613. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1614. /* Process Unlocked */
  1615. __HAL_UNLOCK(hrtc);
  1616. return HAL_TIMEOUT;
  1617. }
  1618. }
  1619. #endif
  1620. hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
  1621. /* Configure the Alarm B Sub Second register */
  1622. hrtc->Instance->ALRMBSSR = subsecondtmpreg;
  1623. /* Configure the Alarm state: Enable Alarm */
  1624. __HAL_RTC_ALARMB_ENABLE(hrtc);
  1625. }
  1626. /* Enable the write protection for RTC registers */
  1627. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1628. /* Change RTC state */
  1629. hrtc->State = HAL_RTC_STATE_READY;
  1630. /* Process Unlocked */
  1631. __HAL_UNLOCK(hrtc);
  1632. return HAL_OK;
  1633. }
  1634. /**
  1635. * @brief Set the specified RTC Alarm with Interrupt.
  1636. * @note The Alarm register can only be written when the corresponding Alarm
  1637. * is disabled (Use the HAL_RTC_DeactivateAlarm()).
  1638. * @note The HAL_RTC_SetTime() must be called before enabling the Alarm feature.
  1639. * @param hrtc RTC handle
  1640. * @param sAlarm Pointer to Alarm structure
  1641. * @param Format Specifies the format of the entered parameters.
  1642. * This parameter can be one of the following values:
  1643. * @arg RTC_FORMAT_BIN: Binary data format
  1644. * @arg RTC_FORMAT_BCD: BCD data format
  1645. * @retval HAL status
  1646. */
  1647. HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
  1648. {
  1649. uint32_t tmpreg, subsecondtmpreg;
  1650. /* Check the parameters */
  1651. assert_param(IS_RTC_FORMAT(Format));
  1652. assert_param(IS_RTC_ALARM(sAlarm->Alarm));
  1653. assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
  1654. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
  1655. assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
  1656. assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
  1657. /* Process Locked */
  1658. __HAL_LOCK(hrtc);
  1659. hrtc->State = HAL_RTC_STATE_BUSY;
  1660. if (Format == RTC_FORMAT_BIN)
  1661. {
  1662. if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
  1663. {
  1664. assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
  1665. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  1666. }
  1667. else
  1668. {
  1669. sAlarm->AlarmTime.TimeFormat = 0x00U;
  1670. assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
  1671. }
  1672. assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
  1673. assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
  1674. if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  1675. {
  1676. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
  1677. }
  1678. else
  1679. {
  1680. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
  1681. }
  1682. tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
  1683. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
  1684. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
  1685. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
  1686. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
  1687. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  1688. ((uint32_t)sAlarm->AlarmMask));
  1689. }
  1690. else
  1691. {
  1692. if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
  1693. {
  1694. assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  1695. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  1696. }
  1697. else
  1698. {
  1699. sAlarm->AlarmTime.TimeFormat = 0x00U;
  1700. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  1701. }
  1702. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
  1703. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
  1704. #ifdef USE_FULL_ASSERT
  1705. if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  1706. {
  1707. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
  1708. }
  1709. else
  1710. {
  1711. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
  1712. }
  1713. #endif /* USE_FULL_ASSERT */
  1714. tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
  1715. ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
  1716. ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
  1717. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
  1718. ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
  1719. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  1720. ((uint32_t)sAlarm->AlarmMask));
  1721. }
  1722. /* Configure the Alarm A or Alarm B Sub Second registers */
  1723. subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
  1724. /* Disable the write protection for RTC registers */
  1725. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1726. /* Configure the Alarm register */
  1727. if (sAlarm->Alarm == RTC_ALARM_A)
  1728. {
  1729. /* Disable the Alarm A interrupt */
  1730. __HAL_RTC_ALARMA_DISABLE(hrtc);
  1731. /* Clear flag alarm A */
  1732. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
  1733. #if defined (RTC_FLAG_ALRAWF)
  1734. uint32_t tickstart = HAL_GetTick();
  1735. /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
  1736. while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
  1737. {
  1738. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1739. {
  1740. /* Enable the write protection for RTC registers */
  1741. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1742. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1743. /* Process Unlocked */
  1744. __HAL_UNLOCK(hrtc);
  1745. return HAL_TIMEOUT;
  1746. }
  1747. }
  1748. #endif
  1749. hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
  1750. /* Configure the Alarm A Sub Second register */
  1751. hrtc->Instance->ALRMASSR = subsecondtmpreg;
  1752. /* Configure the Alarm state: Enable Alarm */
  1753. __HAL_RTC_ALARMA_ENABLE(hrtc);
  1754. /* Configure the Alarm interrupt */
  1755. __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRA);
  1756. }
  1757. else
  1758. {
  1759. /* Disable the Alarm B interrupt */
  1760. __HAL_RTC_ALARMB_DISABLE(hrtc);
  1761. /* Clear flag alarm B */
  1762. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
  1763. #if defined (RTC_FLAG_ALRBWF)
  1764. uint32_t tickstart = HAL_GetTick();
  1765. /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
  1766. while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
  1767. {
  1768. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1769. {
  1770. /* Enable the write protection for RTC registers */
  1771. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1772. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1773. /* Process Unlocked */
  1774. __HAL_UNLOCK(hrtc);
  1775. return HAL_TIMEOUT;
  1776. }
  1777. }
  1778. #endif
  1779. hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
  1780. /* Configure the Alarm B Sub Second register */
  1781. hrtc->Instance->ALRMBSSR = subsecondtmpreg;
  1782. /* Configure the Alarm state: Enable Alarm */
  1783. __HAL_RTC_ALARMB_ENABLE(hrtc);
  1784. /* Configure the Alarm interrupt */
  1785. __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRB);
  1786. }
  1787. /* RTC Alarm Interrupt Configuration: EXTI configuration */
  1788. __HAL_RTC_ALARM_EXTI_ENABLE_IT();
  1789. __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
  1790. /* Enable the write protection for RTC registers */
  1791. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1792. hrtc->State = HAL_RTC_STATE_READY;
  1793. /* Process Unlocked */
  1794. __HAL_UNLOCK(hrtc);
  1795. return HAL_OK;
  1796. }
  1797. #endif /* #if defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
  1798. /**
  1799. * @brief Deactivate the specified RTC Alarm.
  1800. * @param hrtc RTC handle
  1801. * @param Alarm Specifies the Alarm.
  1802. * This parameter can be one of the following values:
  1803. * @arg RTC_ALARM_A: AlarmA
  1804. * @arg RTC_ALARM_B: AlarmB
  1805. * @retval HAL status
  1806. */
  1807. HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
  1808. {
  1809. /* Check the parameters */
  1810. assert_param(IS_RTC_ALARM(Alarm));
  1811. /* Process Locked */
  1812. __HAL_LOCK(hrtc);
  1813. hrtc->State = HAL_RTC_STATE_BUSY;
  1814. /* Disable the write protection for RTC registers */
  1815. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1816. if (Alarm == RTC_ALARM_A)
  1817. {
  1818. /* AlarmA */
  1819. #if defined (RTC_ALRMASSR_SSCLR)
  1820. CLEAR_BIT(RTC->ALRMASSR, RTC_ALRMASSR_SSCLR);
  1821. #endif
  1822. __HAL_RTC_ALARMA_DISABLE(hrtc);
  1823. /* In case of interrupt mode is used, the interrupt source must disabled */
  1824. __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
  1825. #if defined (RTC_FLAG_ALRAWF)
  1826. uint32_t tickstart = HAL_GetTick();
  1827. /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
  1828. while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
  1829. {
  1830. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1831. {
  1832. /* Enable the write protection for RTC registers */
  1833. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1834. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1835. /* Process Unlocked */
  1836. __HAL_UNLOCK(hrtc);
  1837. return HAL_TIMEOUT;
  1838. }
  1839. }
  1840. #endif
  1841. }
  1842. else
  1843. {
  1844. /* AlarmB */
  1845. #if defined (RTC_ALRMBSSR_SSCLR)
  1846. CLEAR_BIT(RTC->ALRMBSSR, RTC_ALRMASSR_SSCLR);
  1847. #endif
  1848. __HAL_RTC_ALARMB_DISABLE(hrtc);
  1849. /* In case of interrupt mode is used, the interrupt source must disabled */
  1850. __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
  1851. #if defined (RTC_FLAG_ALRBWF)
  1852. uint32_t tickstart = HAL_GetTick();
  1853. /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
  1854. while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
  1855. {
  1856. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1857. {
  1858. /* Enable the write protection for RTC registers */
  1859. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1860. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1861. /* Process Unlocked */
  1862. __HAL_UNLOCK(hrtc);
  1863. return HAL_TIMEOUT;
  1864. }
  1865. }
  1866. #endif
  1867. }
  1868. /* Enable the write protection for RTC registers */
  1869. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1870. hrtc->State = HAL_RTC_STATE_READY;
  1871. /* Process Unlocked */
  1872. __HAL_UNLOCK(hrtc);
  1873. return HAL_OK;
  1874. }
  1875. /**
  1876. * @brief Get the RTC Alarm value and masks.
  1877. * @param hrtc RTC handle
  1878. * @param sAlarm Pointer to Date structure
  1879. * @param Alarm Specifies the Alarm.
  1880. * This parameter can be one of the following values:
  1881. * @arg RTC_ALARM_A: AlarmA
  1882. * @arg RTC_ALARM_B: AlarmB
  1883. * @param Format Specifies the format of the entered parameters.
  1884. * This parameter can be one of the following values:
  1885. * @arg RTC_FORMAT_BIN: Binary data format
  1886. * @arg RTC_FORMAT_BCD: BCD data format
  1887. * @retval HAL status
  1888. */
  1889. HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
  1890. {
  1891. uint32_t tmpreg, subsecondtmpreg;
  1892. /* Check the parameters */
  1893. assert_param(IS_RTC_FORMAT(Format));
  1894. assert_param(IS_RTC_ALARM(Alarm));
  1895. if (Alarm == RTC_ALARM_A)
  1896. {
  1897. /* AlarmA */
  1898. sAlarm->Alarm = RTC_ALARM_A;
  1899. tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
  1900. subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR) & RTC_ALRMASSR_SS);
  1901. /* Fill the structure with the read parameters */
  1902. sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> RTC_ALRMAR_HU_Pos);
  1903. sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> RTC_ALRMAR_MNU_Pos);
  1904. sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU)) >> RTC_ALRMAR_SU_Pos);
  1905. sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMAR_PM) >> RTC_ALRMAR_PM_Pos);
  1906. sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
  1907. sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> RTC_ALRMAR_DU_Pos);
  1908. sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
  1909. sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
  1910. }
  1911. else
  1912. {
  1913. sAlarm->Alarm = RTC_ALARM_B;
  1914. tmpreg = (uint32_t)(hrtc->Instance->ALRMBR);
  1915. subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS);
  1916. /* Fill the structure with the read parameters */
  1917. sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMBR_HT | RTC_ALRMBR_HU)) >> RTC_ALRMBR_HU_Pos);
  1918. sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMBR_MNT | RTC_ALRMBR_MNU)) >> RTC_ALRMBR_MNU_Pos);
  1919. sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMBR_ST | RTC_ALRMBR_SU)) >> RTC_ALRMBR_SU_Pos);
  1920. sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMBR_PM) >> RTC_ALRMBR_PM_Pos);
  1921. sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
  1922. sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMBR_DT | RTC_ALRMBR_DU)) >> RTC_ALRMBR_DU_Pos);
  1923. sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMBR_WDSEL);
  1924. sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
  1925. }
  1926. if (Format == RTC_FORMAT_BIN)
  1927. {
  1928. sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
  1929. sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
  1930. sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
  1931. sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
  1932. }
  1933. return HAL_OK;
  1934. }
  1935. /**
  1936. * @brief Daylight Saving Time, Add one hour to the calendar in one single operation
  1937. * without going through the initialization procedure.
  1938. * @param hrtc RTC handle
  1939. * @retval None
  1940. */
  1941. void HAL_RTC_DST_Add1Hour(RTC_HandleTypeDef *hrtc)
  1942. {
  1943. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1944. SET_BIT(hrtc->Instance->CR, RTC_CR_ADD1H);
  1945. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1946. }
  1947. /**
  1948. * @brief Daylight Saving Time, Subtract one hour from the calendar in one
  1949. * single operation without going through the initialization procedure.
  1950. * @param hrtc RTC handle
  1951. * @retval None
  1952. */
  1953. void HAL_RTC_DST_Sub1Hour(RTC_HandleTypeDef *hrtc)
  1954. {
  1955. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1956. SET_BIT(hrtc->Instance->CR, RTC_CR_SUB1H);
  1957. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1958. }
  1959. /**
  1960. * @brief Daylight Saving Time, Set the store operation bit.
  1961. * @note It can be used by the software in order to memorize the DST status.
  1962. * @param hrtc RTC handle
  1963. * @retval None
  1964. */
  1965. void HAL_RTC_DST_SetStoreOperation(RTC_HandleTypeDef *hrtc)
  1966. {
  1967. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1968. SET_BIT(hrtc->Instance->CR, RTC_CR_BKP);
  1969. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1970. }
  1971. /**
  1972. * @brief Daylight Saving Time, Clear the store operation bit.
  1973. * @param hrtc RTC handle
  1974. * @retval None
  1975. */
  1976. void HAL_RTC_DST_ClearStoreOperation(RTC_HandleTypeDef *hrtc)
  1977. {
  1978. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1979. CLEAR_BIT(hrtc->Instance->CR, RTC_CR_BKP);
  1980. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1981. }
  1982. /**
  1983. * @brief Daylight Saving Time, Read the store operation bit.
  1984. * @param hrtc RTC handle
  1985. * @retval operation see RTC_StoreOperation_Definitions
  1986. */
  1987. uint32_t HAL_RTC_DST_ReadStoreOperation(RTC_HandleTypeDef *hrtc)
  1988. {
  1989. return READ_BIT(hrtc->Instance->CR, RTC_CR_BKP);
  1990. }
  1991. /**
  1992. * @brief Handle Alarm interrupt request.
  1993. * @param hrtc RTC handle
  1994. * @retval None
  1995. */
  1996. void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
  1997. {
  1998. /* Clear the EXTI's line Flag for RTC Alarm */
  1999. __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
  2000. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  2001. /* Get interrupt status */
  2002. uint32_t tmp = hrtc->Instance->MISR;
  2003. if ((tmp & RTC_MISR_ALRAMF) != 0u)
  2004. {
  2005. /* Clear the AlarmA interrupt pending bit */
  2006. hrtc->Instance->SCR = RTC_SCR_CALRAF;
  2007. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  2008. /* Call Compare Match registered Callback */
  2009. hrtc->AlarmAEventCallback(hrtc);
  2010. #else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
  2011. HAL_RTC_AlarmAEventCallback(hrtc);
  2012. #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
  2013. }
  2014. if ((tmp & RTC_MISR_ALRBMF) != 0u)
  2015. {
  2016. /* Clear the AlarmB interrupt pending bit */
  2017. hrtc->Instance->SCR = RTC_SCR_CALRBF;
  2018. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  2019. /* Call Compare Match registered Callback */
  2020. hrtc->AlarmBEventCallback(hrtc);
  2021. #else
  2022. HAL_RTCEx_AlarmBEventCallback(hrtc);
  2023. #endif
  2024. }
  2025. #else /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
  2026. /* Get the AlarmA interrupt source enable status */
  2027. if (__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRA) != 0U)
  2028. {
  2029. /* Get the pending status of the AlarmA Interrupt */
  2030. if (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) != 0U)
  2031. {
  2032. /* Clear the AlarmA interrupt pending bit */
  2033. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
  2034. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  2035. hrtc->AlarmAEventCallback(hrtc);
  2036. #else
  2037. HAL_RTC_AlarmAEventCallback(hrtc);
  2038. #endif
  2039. }
  2040. }
  2041. /* Get the AlarmB interrupt source enable status */
  2042. if (__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRB) != 0U)
  2043. {
  2044. /* Get the pending status of the AlarmB Interrupt */
  2045. if (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) != 0U)
  2046. {
  2047. /* Clear the AlarmB interrupt pending bit */
  2048. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
  2049. #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
  2050. hrtc->AlarmBEventCallback(hrtc);
  2051. #else
  2052. HAL_RTCEx_AlarmBEventCallback(hrtc);
  2053. #endif
  2054. }
  2055. }
  2056. #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
  2057. /* Change RTC state */
  2058. hrtc->State = HAL_RTC_STATE_READY;
  2059. }
  2060. /**
  2061. * @brief Alarm A callback.
  2062. * @param hrtc RTC handle
  2063. * @retval None
  2064. */
  2065. __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
  2066. {
  2067. /* Prevent unused argument(s) compilation warning */
  2068. UNUSED(hrtc);
  2069. /* NOTE : This function should not be modified, when the callback is needed,
  2070. the HAL_RTC_AlarmAEventCallback could be implemented in the user file
  2071. */
  2072. }
  2073. /**
  2074. * @brief Handle AlarmA Polling request.
  2075. * @param hrtc RTC handle
  2076. * @param Timeout Timeout duration
  2077. * @retval HAL status
  2078. */
  2079. HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  2080. {
  2081. uint32_t tickstart = HAL_GetTick();
  2082. while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == 0U)
  2083. {
  2084. if (Timeout != HAL_MAX_DELAY)
  2085. {
  2086. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  2087. {
  2088. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  2089. return HAL_TIMEOUT;
  2090. }
  2091. }
  2092. }
  2093. /* Clear the Alarm interrupt pending bit */
  2094. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
  2095. /* Change RTC state */
  2096. hrtc->State = HAL_RTC_STATE_READY;
  2097. return HAL_OK;
  2098. }
  2099. /**
  2100. * @}
  2101. */
  2102. /** @addtogroup RTC_Exported_Functions_Group4
  2103. * @brief Peripheral Control functions
  2104. *
  2105. @verbatim
  2106. ===============================================================================
  2107. ##### Peripheral Control functions #####
  2108. ===============================================================================
  2109. [..]
  2110. This subsection provides functions allowing to
  2111. (+) Wait for RTC Time and Date Synchronization
  2112. @endverbatim
  2113. * @{
  2114. */
  2115. /**
  2116. * @brief Wait until the RTC Time and Date registers (RTC_TR and RTC_DR) are
  2117. * synchronized with RTC APB clock.
  2118. * @note The RTC Resynchronization mode is write protected, use the
  2119. * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
  2120. * @note To read the calendar through the shadow registers after Calendar
  2121. * initialization, calendar update or after wakeup from low power modes
  2122. * the software must first clear the RSF flag.
  2123. * The software must then wait until it is set again before reading
  2124. * the calendar, which means that the calendar registers have been
  2125. * correctly copied into the RTC_TR and RTC_DR shadow registers.
  2126. * @param hrtc RTC handle
  2127. * @retval HAL status
  2128. */
  2129. HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef *hrtc)
  2130. {
  2131. uint32_t tickstart;
  2132. /* Clear RSF flag */
  2133. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  2134. hrtc->Instance->ICSR &= (uint32_t)RTC_RSF_MASK;
  2135. #else
  2136. hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK;
  2137. #endif
  2138. tickstart = HAL_GetTick();
  2139. /* Wait the registers to be synchronised */
  2140. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  2141. while ((hrtc->Instance->ICSR & RTC_ICSR_RSF) == 0U)
  2142. #else
  2143. while ((hrtc->Instance->ISR & RTC_ISR_RSF) == 0U)
  2144. #endif
  2145. {
  2146. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  2147. {
  2148. return HAL_TIMEOUT;
  2149. }
  2150. }
  2151. return HAL_OK;
  2152. }
  2153. /**
  2154. * @}
  2155. */
  2156. /** @addtogroup RTC_Exported_Functions_Group5
  2157. * @brief Peripheral State functions
  2158. *
  2159. @verbatim
  2160. ===============================================================================
  2161. ##### Peripheral State functions #####
  2162. ===============================================================================
  2163. [..]
  2164. This subsection provides functions allowing to
  2165. (+) Get RTC state
  2166. @endverbatim
  2167. * @{
  2168. */
  2169. /**
  2170. * @brief Return the RTC handle state.
  2171. * @param hrtc RTC handle
  2172. * @retval HAL state
  2173. */
  2174. HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef *hrtc)
  2175. {
  2176. /* Return RTC handle state */
  2177. return hrtc->State;
  2178. }
  2179. /**
  2180. * @}
  2181. */
  2182. /**
  2183. * @}
  2184. */
  2185. /** @addtogroup RTC_Private_Functions
  2186. * @{
  2187. */
  2188. /**
  2189. * @brief Enter the RTC Initialization mode.
  2190. * @note The RTC Initialization mode is write protected, use the
  2191. * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
  2192. * @param hrtc RTC handle
  2193. * @retval HAL status
  2194. */
  2195. HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
  2196. {
  2197. uint32_t tickstart;
  2198. HAL_StatusTypeDef status = HAL_OK;
  2199. /* Check if the Initialization mode is set */
  2200. #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx)
  2201. if ((hrtc->Instance->ICSR & RTC_ICSR_INITF) == 0U)
  2202. {
  2203. /* Set the Initialization mode */
  2204. SET_BIT(hrtc->Instance->ICSR, RTC_ICSR_INIT);
  2205. tickstart = HAL_GetTick();
  2206. /* Wait till RTC is in INIT state and if Time out is reached exit */
  2207. while ((READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_INITF) == 0U) && (status != HAL_TIMEOUT))
  2208. {
  2209. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  2210. {
  2211. status = HAL_TIMEOUT;
  2212. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  2213. }
  2214. }
  2215. }
  2216. #else /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
  2217. if ((hrtc->Instance->ISR & RTC_ISR_INITF) == 0U)
  2218. {
  2219. /* Set the Initialization mode */
  2220. hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
  2221. tickstart = HAL_GetTick();
  2222. /* Wait till RTC is in INIT state and if Time out is reached exit */
  2223. while ((READ_BIT(hrtc->Instance->ISR, RTC_ISR_INITF) == 0U) && (status != HAL_TIMEOUT))
  2224. {
  2225. if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  2226. {
  2227. status = HAL_TIMEOUT;
  2228. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  2229. }
  2230. }
  2231. }
  2232. #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) || defined (STM32L4P5xx) || defined (STM32L4Q5xx) */
  2233. return status;
  2234. }
  2235. /**
  2236. * @brief Exit the RTC Initialization mode.
  2237. * @param hrtc RTC handle
  2238. * @retval HAL status
  2239. */
  2240. HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef *hrtc)
  2241. {
  2242. HAL_StatusTypeDef status = HAL_OK;
  2243. /* Exit Initialization mode */
  2244. #if defined(STM32L412xx) || defined(STM32L422xx) || defined(STM32L4P5xx) || defined(STM32L4Q5xx)
  2245. CLEAR_BIT(RTC->ICSR, RTC_ICSR_INIT);
  2246. #else
  2247. /* Exit Initialization mode */
  2248. CLEAR_BIT(RTC->ISR, RTC_ISR_INIT);
  2249. #endif
  2250. /* If CR_BYPSHAD bit = 0, wait for synchro */
  2251. if (READ_BIT(RTC->CR, RTC_CR_BYPSHAD) == 0U)
  2252. {
  2253. if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  2254. {
  2255. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  2256. status = HAL_TIMEOUT;
  2257. }
  2258. }
  2259. else /* WA 2.9.6 Calendar initialization may fail in case of consecutive INIT mode entry */
  2260. {
  2261. /* Clear BYPSHAD bit */
  2262. CLEAR_BIT(RTC->CR, RTC_CR_BYPSHAD);
  2263. if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  2264. {
  2265. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  2266. status = HAL_TIMEOUT;
  2267. }
  2268. /* Restore BYPSHAD bit */
  2269. SET_BIT(RTC->CR, RTC_CR_BYPSHAD);
  2270. }
  2271. return status;
  2272. }
  2273. /**
  2274. * @brief Convert a 2 digit decimal to BCD format.
  2275. * @param Value Byte to be converted
  2276. * @retval Converted byte
  2277. */
  2278. uint8_t RTC_ByteToBcd2(uint8_t Value)
  2279. {
  2280. uint32_t bcdhigh = 0U;
  2281. uint8_t temp = Value;
  2282. while (temp >= 10U)
  2283. {
  2284. bcdhigh++;
  2285. temp -= 10U;
  2286. }
  2287. return ((uint8_t)(bcdhigh << 4U) | temp);
  2288. }
  2289. /**
  2290. * @brief Convert from 2 digit BCD to Binary.
  2291. * @param Value BCD value to be converted
  2292. * @retval Converted word
  2293. */
  2294. uint8_t RTC_Bcd2ToByte(uint8_t Value)
  2295. {
  2296. uint8_t tmp;
  2297. tmp = ((Value & 0xF0U) >> 4U) * 10U;
  2298. return (tmp + (Value & 0x0FU));
  2299. }
  2300. /**
  2301. * @}
  2302. */
  2303. #endif /* HAL_RTC_MODULE_ENABLED */
  2304. /**
  2305. * @}
  2306. */
  2307. /**
  2308. * @}
  2309. */