stm32l4xx_hal_rtc.c 93 KB

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