stm32l1xx_hal_rtc_ex.c 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_hal_rtc_ex.c
  4. * @author MCD Application Team
  5. * @brief Extended RTC HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Real Time Clock (RTC) Extension peripheral:
  8. * + RTC Time Stamp functions
  9. * + RTC Tamper functions
  10. * + RTC Wake-up functions
  11. * + Extension Control functions
  12. * + Extension RTC features functions
  13. *
  14. @verbatim
  15. ==============================================================================
  16. ##### How to use this driver #####
  17. ==============================================================================
  18. [..]
  19. (+) Enable the RTC domain access.
  20. (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
  21. format using the HAL_RTC_Init() function.
  22. *** RTC Wakeup configuration ***
  23. ================================
  24. [..]
  25. (+) To configure the RTC Wakeup Clock source and Counter use the HAL_RTCEx_SetWakeUpTimer()
  26. function. You can also configure the RTC Wakeup timer with interrupt mode
  27. using the HAL_RTCEx_SetWakeUpTimer_IT() function.
  28. (+) To read the RTC WakeUp Counter register, use the HAL_RTCEx_GetWakeUpTimer()
  29. function.
  30. *** TimeStamp configuration ***
  31. ===============================
  32. [..]
  33. (+) Configure the RTC_AFx trigger and enable the RTC TimeStamp using the
  34. HAL_RTCEx_SetTimeStamp() function. You can also configure the RTC TimeStamp with
  35. interrupt mode using the HAL_RTCEx_SetTimeStamp_IT() function.
  36. (+) To read the RTC TimeStamp Time and Date register, use the HAL_RTCEx_GetTimeStamp()
  37. function.
  38. (+) The TIMESTAMP alternate function can be mapped to RTC_AF1 (PC13).
  39. *** Tamper configuration ***
  40. ============================
  41. [..]
  42. (+) Enable the RTC Tamper and configure the Tamper filter count, trigger Edge
  43. or Level according to the Tamper filter (if equal to 0 Edge else Level)
  44. value, sampling frequency, precharge or discharge and Pull-UP using the
  45. HAL_RTCEx_SetTamper() function. You can configure RTC Tamper with interrupt
  46. mode using HAL_RTCEx_SetTamper_IT() function.
  47. (+) The TAMPER1 alternate function can be mapped to RTC_AF1 (PC13).
  48. *** Backup Data Registers configuration ***
  49. ===========================================
  50. [..]
  51. (+) To write to the RTC Backup Data registers, use the HAL_RTCEx_BKUPWrite()
  52. function.
  53. (+) To read the RTC Backup Data registers, use the HAL_RTCEx_BKUPRead()
  54. function.
  55. @endverbatim
  56. ******************************************************************************
  57. * @attention
  58. *
  59. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  60. *
  61. * Redistribution and use in source and binary forms, with or without modification,
  62. * are permitted provided that the following conditions are met:
  63. * 1. Redistributions of source code must retain the above copyright notice,
  64. * this list of conditions and the following disclaimer.
  65. * 2. Redistributions in binary form must reproduce the above copyright notice,
  66. * this list of conditions and the following disclaimer in the documentation
  67. * and/or other materials provided with the distribution.
  68. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  69. * may be used to endorse or promote products derived from this software
  70. * without specific prior written permission.
  71. *
  72. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  73. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  74. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  75. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  76. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  77. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  78. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  79. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  80. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  81. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  82. *
  83. ******************************************************************************
  84. */
  85. /* Includes ------------------------------------------------------------------*/
  86. #include "stm32l1xx_hal.h"
  87. /** @addtogroup STM32L1xx_HAL_Driver
  88. * @{
  89. */
  90. /** @addtogroup RTC
  91. * @{
  92. */
  93. #ifdef HAL_RTC_MODULE_ENABLED
  94. /* Private typedef -----------------------------------------------------------*/
  95. /* Private define ------------------------------------------------------------*/
  96. /* Private macro -------------------------------------------------------------*/
  97. /* Private variables ---------------------------------------------------------*/
  98. /* Private function prototypes -----------------------------------------------*/
  99. /* Private functions ---------------------------------------------------------*/
  100. /** @addtogroup RTC_Exported_Functions
  101. * @{
  102. */
  103. /** @addtogroup RTC_Exported_Functions_Group1
  104. * @{
  105. */
  106. /**
  107. * @brief DeInitializes the RTC peripheral
  108. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  109. * the configuration information for RTC.
  110. * @note This function does not reset the RTC Backup Data registers.
  111. * @retval HAL status
  112. */
  113. HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
  114. {
  115. uint32_t tickstart = 0;
  116. /* Check the parameters */
  117. assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
  118. /* Set RTC state */
  119. hrtc->State = HAL_RTC_STATE_BUSY;
  120. /* Disable the write protection for RTC registers */
  121. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  122. /* Set Initialization mode */
  123. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  124. {
  125. /* Enable the write protection for RTC registers */
  126. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  127. /* Set RTC state */
  128. hrtc->State = HAL_RTC_STATE_ERROR;
  129. return HAL_ERROR;
  130. }
  131. else
  132. {
  133. /* Reset TR, DR and CR registers */
  134. hrtc->Instance->TR = 0x00000000U;
  135. hrtc->Instance->DR = 0x00002101U;
  136. /* Reset All CR bits except CR[2:0] */
  137. hrtc->Instance->CR &= 0x00000007U;
  138. tickstart = HAL_GetTick();
  139. /* Wait till WUTWF flag is set and if Time out is reached exit */
  140. while(((hrtc->Instance->ISR) & RTC_ISR_WUTWF) == (uint32_t)RESET)
  141. {
  142. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  143. {
  144. /* Enable the write protection for RTC registers */
  145. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  146. /* Set RTC state */
  147. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  148. return HAL_TIMEOUT;
  149. }
  150. }
  151. /* Reset all RTC CR register bits */
  152. hrtc->Instance->CR &= 0x00000000U;
  153. hrtc->Instance->WUTR = 0x0000FFFFU;
  154. hrtc->Instance->PRER = 0x007F00FFU;
  155. hrtc->Instance->CALIBR = 0x00000000U;
  156. hrtc->Instance->ALRMAR = 0x00000000U;
  157. hrtc->Instance->ALRMBR = 0x00000000U;
  158. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  159. hrtc->Instance->SHIFTR = 0x00000000U;
  160. hrtc->Instance->CALR = 0x00000000U;
  161. hrtc->Instance->ALRMASSR = 0x00000000U;
  162. hrtc->Instance->ALRMBSSR = 0x00000000U;
  163. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  164. /* Reset ISR register and exit initialization mode */
  165. hrtc->Instance->ISR = 0x00000000U;
  166. /* Reset Tamper and alternate functions configuration register */
  167. hrtc->Instance->TAFCR = 0x00000000U;
  168. /* Wait for synchro */
  169. if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  170. {
  171. /* Enable the write protection for RTC registers */
  172. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  173. hrtc->State = HAL_RTC_STATE_ERROR;
  174. return HAL_ERROR;
  175. }
  176. }
  177. /* Enable the write protection for RTC registers */
  178. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  179. /* De-Initialize RTC MSP */
  180. HAL_RTC_MspDeInit(hrtc);
  181. hrtc->State = HAL_RTC_STATE_RESET;
  182. /* Release Lock */
  183. __HAL_UNLOCK(hrtc);
  184. return HAL_OK;
  185. }
  186. /**
  187. * @}
  188. */
  189. /** @addtogroup RTC_Exported_Functions_Group2
  190. * @{
  191. */
  192. /**
  193. * @brief Get RTC current time.
  194. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  195. * the configuration information for RTC.
  196. * @param sTime: Pointer to Time structure with Hours, Minutes and Seconds fields returned
  197. * with input format (BIN or BCD), also SubSeconds field (if availabale) returning the
  198. * RTC_SSR register content and SecondFraction field the Synchronous pre-scaler
  199. * factor to be used for second fraction ratio computation.
  200. * @param Format: Specifies the format of the entered parameters.
  201. * This parameter can be one of the following values:
  202. * @arg RTC_FORMAT_BIN: Binary data format
  203. * @arg RTC_FORMAT_BCD: BCD data format
  204. * @note If available, you can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
  205. * value in second fraction ratio with time unit following generic formula:
  206. * Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
  207. * This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
  208. * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
  209. * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
  210. * Reading RTC current time locks the values in calendar shadow registers until Current date is read
  211. * to ensure consistency between the time and date values.
  212. * @retval HAL status
  213. */
  214. HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
  215. {
  216. uint32_t tmpreg = 0;
  217. /* Check the parameters */
  218. assert_param(IS_RTC_FORMAT(Format));
  219. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  220. /* Get subseconds structure field from the corresponding register*/
  221. sTime->SubSeconds = (uint32_t)((hrtc->Instance->SSR) & RTC_SSR_SS);
  222. /* Get SecondFraction structure field from the corresponding register field*/
  223. sTime->SecondFraction = (uint32_t)(hrtc->Instance->PRER & RTC_PRER_PREDIV_S);
  224. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  225. /* Get the TR register */
  226. tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);
  227. /* Fill the structure fields with the read parameters */
  228. sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16);
  229. sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8);
  230. sTime->Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
  231. sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16);
  232. /* Check the input parameters format */
  233. if(Format == RTC_FORMAT_BIN)
  234. {
  235. /* Convert the time structure parameters to Binary format */
  236. sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
  237. sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
  238. sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
  239. }
  240. return HAL_OK;
  241. }
  242. /**
  243. * @}
  244. */
  245. /** @addtogroup RTC_Exported_Functions_Group3
  246. * @{
  247. */
  248. /**
  249. * @brief Sets the specified RTC Alarm.
  250. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  251. * the configuration information for RTC.
  252. * @param sAlarm: Pointer to Alarm structure
  253. * @param Format: Specifies the format of the entered parameters.
  254. * This parameter can be one of the following values:
  255. * @arg RTC_FORMAT_BIN: Binary data format
  256. * @arg RTC_FORMAT_BCD: BCD data format
  257. * @retval HAL status
  258. */
  259. HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
  260. {
  261. uint32_t tickstart = 0;
  262. uint32_t tmpreg = 0;
  263. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  264. uint32_t subsecondtmpreg = 0;
  265. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  266. /* Check the parameters */
  267. assert_param(IS_RTC_FORMAT(Format));
  268. assert_param(IS_RTC_ALARM(sAlarm->Alarm));
  269. assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
  270. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
  271. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  272. assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
  273. assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
  274. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  275. /* Process Locked */
  276. __HAL_LOCK(hrtc);
  277. hrtc->State = HAL_RTC_STATE_BUSY;
  278. if(Format == RTC_FORMAT_BIN)
  279. {
  280. if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
  281. {
  282. assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
  283. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  284. }
  285. else
  286. {
  287. sAlarm->AlarmTime.TimeFormat = 0x00;
  288. assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
  289. }
  290. assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
  291. assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
  292. if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  293. {
  294. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
  295. }
  296. else
  297. {
  298. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
  299. }
  300. tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16) | \
  301. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8) | \
  302. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
  303. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
  304. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24) | \
  305. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  306. ((uint32_t)sAlarm->AlarmMask));
  307. }
  308. else
  309. {
  310. if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
  311. {
  312. tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
  313. assert_param(IS_RTC_HOUR12(tmpreg));
  314. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  315. }
  316. else
  317. {
  318. sAlarm->AlarmTime.TimeFormat = 0x00;
  319. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  320. }
  321. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
  322. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
  323. if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  324. {
  325. tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
  326. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
  327. }
  328. else
  329. {
  330. tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
  331. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
  332. }
  333. tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16) | \
  334. ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8) | \
  335. ((uint32_t) sAlarm->AlarmTime.Seconds) | \
  336. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
  337. ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24) | \
  338. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  339. ((uint32_t)sAlarm->AlarmMask));
  340. }
  341. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  342. /* Configure the Alarm A or Alarm B Sub Second registers */
  343. subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
  344. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  345. /* Disable the write protection for RTC registers */
  346. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  347. /* Configure the Alarm register */
  348. if(sAlarm->Alarm == RTC_ALARM_A)
  349. {
  350. /* Disable the Alarm A interrupt */
  351. __HAL_RTC_ALARMA_DISABLE(hrtc);
  352. /* In case of interrupt mode is used, the interrupt source must disabled */
  353. __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
  354. tickstart = HAL_GetTick();
  355. /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
  356. while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
  357. {
  358. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  359. {
  360. /* Enable the write protection for RTC registers */
  361. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  362. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  363. /* Process Unlocked */
  364. __HAL_UNLOCK(hrtc);
  365. return HAL_TIMEOUT;
  366. }
  367. }
  368. hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
  369. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  370. /* Configure the Alarm A Sub Second register */
  371. hrtc->Instance->ALRMASSR = subsecondtmpreg;
  372. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  373. /* Configure the Alarm state: Enable Alarm */
  374. __HAL_RTC_ALARMA_ENABLE(hrtc);
  375. }
  376. else
  377. {
  378. /* Disable the Alarm B interrupt */
  379. __HAL_RTC_ALARMB_DISABLE(hrtc);
  380. /* In case of interrupt mode is used, the interrupt source must disabled */
  381. __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
  382. tickstart = HAL_GetTick();
  383. /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
  384. while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
  385. {
  386. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  387. {
  388. /* Enable the write protection for RTC registers */
  389. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  390. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  391. /* Process Unlocked */
  392. __HAL_UNLOCK(hrtc);
  393. return HAL_TIMEOUT;
  394. }
  395. }
  396. hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
  397. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  398. /* Configure the Alarm B Sub Second register */
  399. hrtc->Instance->ALRMBSSR = subsecondtmpreg;
  400. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  401. /* Configure the Alarm state: Enable Alarm */
  402. __HAL_RTC_ALARMB_ENABLE(hrtc);
  403. }
  404. /* Enable the write protection for RTC registers */
  405. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  406. /* Change RTC state */
  407. hrtc->State = HAL_RTC_STATE_READY;
  408. /* Process Unlocked */
  409. __HAL_UNLOCK(hrtc);
  410. return HAL_OK;
  411. }
  412. /**
  413. * @brief Sets the specified RTC Alarm with Interrupt
  414. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  415. * the configuration information for RTC.
  416. * @param sAlarm: Pointer to Alarm structure
  417. * @param Format: Specifies the format of the entered parameters.
  418. * This parameter can be one of the following values:
  419. * @arg RTC_FORMAT_BIN: Binary data format
  420. * @arg RTC_FORMAT_BCD: BCD data format
  421. * @note The Alarm register can only be written when the corresponding Alarm
  422. * is disabled (Use the HAL_RTC_DeactivateAlarm()).
  423. * @note The HAL_RTC_SetTime() must be called before enabling the Alarm feature.
  424. * @retval HAL status
  425. */
  426. HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
  427. {
  428. uint32_t tickstart = 0;
  429. uint32_t tmpreg = 0;
  430. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  431. uint32_t subsecondtmpreg = 0;
  432. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  433. /* Check the parameters */
  434. assert_param(IS_RTC_FORMAT(Format));
  435. assert_param(IS_RTC_ALARM(sAlarm->Alarm));
  436. assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
  437. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
  438. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  439. assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
  440. assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
  441. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  442. /* Process Locked */
  443. __HAL_LOCK(hrtc);
  444. hrtc->State = HAL_RTC_STATE_BUSY;
  445. if(Format == RTC_FORMAT_BIN)
  446. {
  447. if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
  448. {
  449. assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
  450. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  451. }
  452. else
  453. {
  454. sAlarm->AlarmTime.TimeFormat = 0x00;
  455. assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
  456. }
  457. assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
  458. assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
  459. if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  460. {
  461. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
  462. }
  463. else
  464. {
  465. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
  466. }
  467. tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16) | \
  468. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8) | \
  469. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
  470. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
  471. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24) | \
  472. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  473. ((uint32_t)sAlarm->AlarmMask));
  474. }
  475. else
  476. {
  477. if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
  478. {
  479. tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
  480. assert_param(IS_RTC_HOUR12(tmpreg));
  481. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  482. }
  483. else
  484. {
  485. sAlarm->AlarmTime.TimeFormat = 0x00;
  486. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  487. }
  488. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
  489. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
  490. if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  491. {
  492. tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
  493. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
  494. }
  495. else
  496. {
  497. tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
  498. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
  499. }
  500. tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16) | \
  501. ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8) | \
  502. ((uint32_t) sAlarm->AlarmTime.Seconds) | \
  503. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16) | \
  504. ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24) | \
  505. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  506. ((uint32_t)sAlarm->AlarmMask));
  507. }
  508. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  509. /* Configure the Alarm A or Alarm B Sub Second registers */
  510. subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
  511. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  512. /* Disable the write protection for RTC registers */
  513. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  514. /* Configure the Alarm register */
  515. if(sAlarm->Alarm == RTC_ALARM_A)
  516. {
  517. /* Disable the Alarm A interrupt */
  518. __HAL_RTC_ALARMA_DISABLE(hrtc);
  519. /* Clear flag alarm A */
  520. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
  521. tickstart = HAL_GetTick();
  522. /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
  523. while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
  524. {
  525. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  526. {
  527. /* Enable the write protection for RTC registers */
  528. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  529. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  530. /* Process Unlocked */
  531. __HAL_UNLOCK(hrtc);
  532. return HAL_TIMEOUT;
  533. }
  534. }
  535. hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
  536. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  537. /* Configure the Alarm A Sub Second register */
  538. hrtc->Instance->ALRMASSR = subsecondtmpreg;
  539. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  540. /* Configure the Alarm state: Enable Alarm */
  541. __HAL_RTC_ALARMA_ENABLE(hrtc);
  542. /* Configure the Alarm interrupt */
  543. __HAL_RTC_ALARM_ENABLE_IT(hrtc,RTC_IT_ALRA);
  544. }
  545. else
  546. {
  547. /* Disable the Alarm B interrupt */
  548. __HAL_RTC_ALARMB_DISABLE(hrtc);
  549. /* Clear flag alarm B */
  550. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
  551. tickstart = HAL_GetTick();
  552. /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
  553. while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
  554. {
  555. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  556. {
  557. /* Enable the write protection for RTC registers */
  558. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  559. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  560. /* Process Unlocked */
  561. __HAL_UNLOCK(hrtc);
  562. return HAL_TIMEOUT;
  563. }
  564. }
  565. hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
  566. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  567. /* Configure the Alarm B Sub Second register */
  568. hrtc->Instance->ALRMBSSR = subsecondtmpreg;
  569. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  570. /* Configure the Alarm state: Enable Alarm */
  571. __HAL_RTC_ALARMB_ENABLE(hrtc);
  572. /* Configure the Alarm interrupt */
  573. __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRB);
  574. }
  575. /* RTC Alarm Interrupt Configuration: EXTI configuration */
  576. __HAL_RTC_ALARM_EXTI_ENABLE_IT();
  577. __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
  578. /* Enable the write protection for RTC registers */
  579. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  580. hrtc->State = HAL_RTC_STATE_READY;
  581. /* Process Unlocked */
  582. __HAL_UNLOCK(hrtc);
  583. return HAL_OK;
  584. }
  585. /**
  586. * @brief Gets the RTC Alarm value and masks.
  587. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  588. * the configuration information for RTC.
  589. * @param sAlarm: Pointer to Date structure
  590. * @param Alarm: Specifies the Alarm.
  591. * This parameter can be one of the following values:
  592. * @arg RTC_ALARM_A: AlarmA
  593. * @arg RTC_ALARM_B: AlarmB
  594. * @param Format: Specifies the format of the entered parameters.
  595. * This parameter can be one of the following values:
  596. * @arg RTC_FORMAT_BIN: Binary data format
  597. * @arg RTC_FORMAT_BCD: BCD data format
  598. * @retval HAL status
  599. */
  600. HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
  601. {
  602. uint32_t tmpreg = 0;
  603. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  604. uint32_t subsecondtmpreg = 0;
  605. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  606. /* Check the parameters */
  607. assert_param(IS_RTC_FORMAT(Format));
  608. assert_param(IS_RTC_ALARM(Alarm));
  609. if(Alarm == RTC_ALARM_A)
  610. {
  611. /* AlarmA */
  612. sAlarm->Alarm = RTC_ALARM_A;
  613. tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
  614. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  615. subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR) & RTC_ALRMASSR_SS);
  616. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  617. }
  618. else
  619. {
  620. sAlarm->Alarm = RTC_ALARM_B;
  621. tmpreg = (uint32_t)(hrtc->Instance->ALRMBR);
  622. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  623. subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS);
  624. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  625. }
  626. /* Fill the structure with the read parameters */
  627. sAlarm->AlarmTime.Hours = (uint32_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> 16);
  628. sAlarm->AlarmTime.Minutes = (uint32_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> 8);
  629. sAlarm->AlarmTime.Seconds = (uint32_t)(tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU));
  630. sAlarm->AlarmTime.TimeFormat = (uint32_t)((tmpreg & RTC_ALRMAR_PM) >> 16);
  631. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  632. sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
  633. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  634. sAlarm->AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> 24);
  635. sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
  636. sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
  637. if(Format == RTC_FORMAT_BIN)
  638. {
  639. sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
  640. sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
  641. sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
  642. sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
  643. }
  644. return HAL_OK;
  645. }
  646. /**
  647. * @}
  648. */
  649. /** @defgroup RTC_Exported_Functions_Group6 Peripheral Control functions
  650. * @brief Peripheral Control functions
  651. *
  652. @verbatim
  653. ===============================================================================
  654. ##### Peripheral Control functions #####
  655. ===============================================================================
  656. [..]
  657. This subsection provides functions allowing to
  658. (+) Wait for RTC Time and Date Synchronization
  659. @endverbatim
  660. * @{
  661. */
  662. /**
  663. * @brief Waits until the RTC Time and Date registers (RTC_TR and RTC_DR) are
  664. * synchronized with RTC APB clock.
  665. * @note The RTC Resynchronization mode is write protected, use the
  666. * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
  667. * @note To read the calendar through the shadow registers after Calendar
  668. * initialization, calendar update or after wakeup from low power modes
  669. * the software must first clear the RSF flag.
  670. * The software must then wait until it is set again before reading
  671. * the calendar, which means that the calendar registers have been
  672. * correctly copied into the RTC_TR and RTC_DR shadow registers.
  673. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  674. * the configuration information for RTC.
  675. * @retval HAL status
  676. */
  677. HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef* hrtc)
  678. {
  679. uint32_t tickstart = 0;
  680. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  681. /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  682. if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
  683. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  684. {
  685. /* Clear RSF flag */
  686. hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK;
  687. tickstart = HAL_GetTick();
  688. /* Wait the registers to be synchronised */
  689. while((hrtc->Instance->ISR & RTC_ISR_RSF) == (uint32_t)RESET)
  690. {
  691. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  692. {
  693. return HAL_TIMEOUT;
  694. }
  695. }
  696. }
  697. return HAL_OK;
  698. }
  699. /**
  700. * @}
  701. */
  702. /**
  703. * @}
  704. */
  705. /**
  706. * @}
  707. */
  708. /** @defgroup RTCEx RTCEx
  709. * @brief RTC Extended HAL module driver
  710. * @{
  711. */
  712. /** @defgroup RTCEx_Exported_Functions RTCEx Exported Functions
  713. * @{
  714. */
  715. /** @defgroup RTCEx_Exported_Functions_Group4 RTC TimeStamp and Tamper functions
  716. * @brief RTC TimeStamp and Tamper functions
  717. *
  718. @verbatim
  719. ===============================================================================
  720. ##### RTC TimeStamp and Tamper functions #####
  721. ===============================================================================
  722. [..] This section provides functions allowing to configure TimeStamp feature
  723. @endverbatim
  724. * @{
  725. */
  726. /**
  727. * @brief Sets TimeStamp.
  728. * @note This API must be called before enabling the TimeStamp feature.
  729. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  730. * the configuration information for RTC.
  731. * @param TimeStampEdge: Specifies the pin edge on which the TimeStamp is
  732. * activated.
  733. * This parameter can be one of the following values:
  734. * @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
  735. * rising edge of the related pin.
  736. * @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
  737. * falling edge of the related pin.
  738. * @retval HAL status
  739. */
  740. HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge)
  741. {
  742. uint32_t tmpreg = 0;
  743. /* Check the parameters */
  744. assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
  745. /* Process Locked */
  746. __HAL_LOCK(hrtc);
  747. hrtc->State = HAL_RTC_STATE_BUSY;
  748. /* Get the RTC_CR register and clear the bits to be configured */
  749. tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  750. tmpreg|= TimeStampEdge;
  751. /* Disable the write protection for RTC registers */
  752. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  753. /* Configure the Time Stamp TSEDGE and Enable bits */
  754. hrtc->Instance->CR = (uint32_t)tmpreg;
  755. __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
  756. /* Enable the write protection for RTC registers */
  757. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  758. /* Change RTC state */
  759. hrtc->State = HAL_RTC_STATE_READY;
  760. /* Process Unlocked */
  761. __HAL_UNLOCK(hrtc);
  762. return HAL_OK;
  763. }
  764. /**
  765. * @brief Sets TimeStamp with Interrupt.
  766. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  767. * the configuration information for RTC.
  768. * @note This API must be called before enabling the TimeStamp feature.
  769. * @param TimeStampEdge: Specifies the pin edge on which the TimeStamp is
  770. * activated.
  771. * This parameter can be one of the following values:
  772. * @arg RTC_TIMESTAMPEDGE_RISING: the Time stamp event occurs on the
  773. * rising edge of the related pin.
  774. * @arg RTC_TIMESTAMPEDGE_FALLING: the Time stamp event occurs on the
  775. * falling edge of the related pin.
  776. * @retval HAL status
  777. */
  778. HAL_StatusTypeDef HAL_RTCEx_SetTimeStamp_IT(RTC_HandleTypeDef *hrtc, uint32_t TimeStampEdge)
  779. {
  780. uint32_t tmpreg = 0;
  781. /* Check the parameters */
  782. assert_param(IS_TIMESTAMP_EDGE(TimeStampEdge));
  783. /* Process Locked */
  784. __HAL_LOCK(hrtc);
  785. hrtc->State = HAL_RTC_STATE_BUSY;
  786. /* Get the RTC_CR register and clear the bits to be configured */
  787. tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  788. tmpreg |= TimeStampEdge;
  789. /* Disable the write protection for RTC registers */
  790. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  791. /* Configure the Time Stamp TSEDGE and Enable bits */
  792. hrtc->Instance->CR = (uint32_t)tmpreg;
  793. __HAL_RTC_TIMESTAMP_ENABLE(hrtc);
  794. /* Enable IT timestamp */
  795. __HAL_RTC_TIMESTAMP_ENABLE_IT(hrtc,RTC_IT_TS);
  796. /* RTC timestamp Interrupt Configuration: EXTI configuration */
  797. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
  798. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
  799. /* Enable the write protection for RTC registers */
  800. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  801. hrtc->State = HAL_RTC_STATE_READY;
  802. /* Process Unlocked */
  803. __HAL_UNLOCK(hrtc);
  804. return HAL_OK;
  805. }
  806. /**
  807. * @brief Deactivates TimeStamp.
  808. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  809. * the configuration information for RTC.
  810. * @retval HAL status
  811. */
  812. HAL_StatusTypeDef HAL_RTCEx_DeactivateTimeStamp(RTC_HandleTypeDef *hrtc)
  813. {
  814. uint32_t tmpreg = 0;
  815. /* Process Locked */
  816. __HAL_LOCK(hrtc);
  817. hrtc->State = HAL_RTC_STATE_BUSY;
  818. /* Disable the write protection for RTC registers */
  819. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  820. /* In case of interrupt mode is used, the interrupt source must disabled */
  821. __HAL_RTC_TIMESTAMP_DISABLE_IT(hrtc, RTC_IT_TS);
  822. /* Get the RTC_CR register and clear the bits to be configured */
  823. tmpreg = (uint32_t)(hrtc->Instance->CR & (uint32_t)~(RTC_CR_TSEDGE | RTC_CR_TSE));
  824. /* Configure the Time Stamp TSEDGE and Enable bits */
  825. hrtc->Instance->CR = (uint32_t)tmpreg;
  826. /* Enable the write protection for RTC registers */
  827. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  828. hrtc->State = HAL_RTC_STATE_READY;
  829. /* Process Unlocked */
  830. __HAL_UNLOCK(hrtc);
  831. return HAL_OK;
  832. }
  833. /**
  834. * @brief Gets the RTC TimeStamp value.
  835. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  836. * the configuration information for RTC.
  837. * @param sTimeStamp: Pointer to Time structure
  838. * @param sTimeStampDate: Pointer to Date structure
  839. * @param Format: specifies the format of the entered parameters.
  840. * This parameter can be one of the following values:
  841. * RTC_FORMAT_BIN: Binary data format
  842. * RTC_FORMAT_BCD: BCD data format
  843. * @retval HAL status
  844. */
  845. HAL_StatusTypeDef HAL_RTCEx_GetTimeStamp(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef* sTimeStamp, RTC_DateTypeDef* sTimeStampDate, uint32_t Format)
  846. {
  847. uint32_t tmptime = 0, tmpdate = 0;
  848. /* Check the parameters */
  849. assert_param(IS_RTC_FORMAT(Format));
  850. /* Get the TimeStamp time and date registers values */
  851. tmptime = (uint32_t)(hrtc->Instance->TSTR & RTC_TR_RESERVED_MASK);
  852. tmpdate = (uint32_t)(hrtc->Instance->TSDR & RTC_DR_RESERVED_MASK);
  853. /* Fill the Time structure fields with the read parameters */
  854. sTimeStamp->Hours = (uint8_t)((tmptime & (RTC_TR_HT | RTC_TR_HU)) >> 16);
  855. sTimeStamp->Minutes = (uint8_t)((tmptime & (RTC_TR_MNT | RTC_TR_MNU)) >> 8);
  856. sTimeStamp->Seconds = (uint8_t)(tmptime & (RTC_TR_ST | RTC_TR_SU));
  857. sTimeStamp->TimeFormat = (uint8_t)((tmptime & (RTC_TR_PM)) >> 16);
  858. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  859. sTimeStamp->SubSeconds = (uint32_t)((hrtc->Instance->TSSSR) & RTC_TSSSR_SS);
  860. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  861. /* Fill the Date structure fields with the read parameters */
  862. sTimeStampDate->Year = 0;
  863. sTimeStampDate->Month = (uint8_t)((tmpdate & (RTC_DR_MT | RTC_DR_MU)) >> 8);
  864. sTimeStampDate->Date = (uint8_t)(tmpdate & (RTC_DR_DT | RTC_DR_DU));
  865. sTimeStampDate->WeekDay = (uint8_t)((tmpdate & (RTC_DR_WDU)) >> 13);
  866. /* Check the input parameters format */
  867. if(Format == RTC_FORMAT_BIN)
  868. {
  869. /* Convert the TimeStamp structure parameters to Binary format */
  870. sTimeStamp->Hours = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Hours);
  871. sTimeStamp->Minutes = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Minutes);
  872. sTimeStamp->Seconds = (uint8_t)RTC_Bcd2ToByte(sTimeStamp->Seconds);
  873. /* Convert the DateTimeStamp structure parameters to Binary format */
  874. sTimeStampDate->Month = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Month);
  875. sTimeStampDate->Date = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->Date);
  876. sTimeStampDate->WeekDay = (uint8_t)RTC_Bcd2ToByte(sTimeStampDate->WeekDay);
  877. }
  878. /* Clear the TIMESTAMP Flag */
  879. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
  880. return HAL_OK;
  881. }
  882. /**
  883. * @brief Sets Tamper
  884. * @note By calling this API we disable the tamper interrupt for all tampers.
  885. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  886. * the configuration information for RTC.
  887. * @param sTamper: Pointer to Tamper Structure.
  888. * @retval HAL status
  889. */
  890. HAL_StatusTypeDef HAL_RTCEx_SetTamper(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
  891. {
  892. uint32_t tmpreg = 0;
  893. /* Check the parameters */
  894. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  895. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  896. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  897. assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
  898. assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
  899. assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  900. assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  901. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
  902. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  903. /* Process Locked */
  904. __HAL_LOCK(hrtc);
  905. hrtc->State = HAL_RTC_STATE_BUSY;
  906. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  907. if((sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE))
  908. {
  909. /* Configure the RTC_TAFCR register */
  910. sTamper->Trigger = RTC_TAMPERTRIGGER_RISINGEDGE;
  911. }
  912. else
  913. {
  914. sTamper->Trigger = (uint32_t)(sTamper->Tamper << 1);
  915. }
  916. tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->Trigger | (uint32_t)sTamper->Filter |\
  917. (uint32_t)sTamper->SamplingFrequency | (uint32_t)sTamper->PrechargeDuration |\
  918. (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);
  919. hrtc->Instance->TAFCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1) | (uint32_t)RTC_TAFCR_TAMPTS |\
  920. (uint32_t)RTC_TAFCR_TAMPFREQ | (uint32_t)RTC_TAFCR_TAMPFLT | (uint32_t)RTC_TAFCR_TAMPPRCH |\
  921. (uint32_t)RTC_TAFCR_TAMPPUDIS | (uint32_t)RTC_TAFCR_TAMPIE);
  922. #else
  923. tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Trigger));
  924. hrtc->Instance->TAFCR &= (uint32_t)~((uint32_t)RTC_TAFCR_TAMP1E | (uint32_t)RTC_TAFCR_TAMP1TRG);
  925. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  926. hrtc->Instance->TAFCR |= tmpreg;
  927. hrtc->State = HAL_RTC_STATE_READY;
  928. /* Process Unlocked */
  929. __HAL_UNLOCK(hrtc);
  930. return HAL_OK;
  931. }
  932. /**
  933. * @brief Sets Tamper with interrupt.
  934. * @note By calling this API we force the tamper interrupt for all tampers.
  935. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  936. * the configuration information for RTC.
  937. * @param sTamper: Pointer to RTC Tamper.
  938. * @retval HAL status
  939. */
  940. HAL_StatusTypeDef HAL_RTCEx_SetTamper_IT(RTC_HandleTypeDef *hrtc, RTC_TamperTypeDef* sTamper)
  941. {
  942. uint32_t tmpreg = 0;
  943. /* Check the parameters */
  944. assert_param(IS_RTC_TAMPER(sTamper->Tamper));
  945. assert_param(IS_RTC_TAMPER_TRIGGER(sTamper->Trigger));
  946. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  947. assert_param(IS_RTC_TAMPER_FILTER(sTamper->Filter));
  948. assert_param(IS_RTC_TAMPER_SAMPLING_FREQ(sTamper->SamplingFrequency));
  949. assert_param(IS_RTC_TAMPER_PRECHARGE_DURATION(sTamper->PrechargeDuration));
  950. assert_param(IS_RTC_TAMPER_PULLUP_STATE(sTamper->TamperPullUp));
  951. assert_param(IS_RTC_TAMPER_TIMESTAMPONTAMPER_DETECTION(sTamper->TimeStampOnTamperDetection));
  952. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  953. /* Process Locked */
  954. __HAL_LOCK(hrtc);
  955. hrtc->State = HAL_RTC_STATE_BUSY;
  956. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  957. /* Configure the tamper trigger */
  958. if((sTamper->Trigger == RTC_TAMPERTRIGGER_RISINGEDGE))
  959. {
  960. sTamper->Trigger = RTC_TAMPERTRIGGER_RISINGEDGE;
  961. }
  962. else
  963. {
  964. sTamper->Trigger = (uint32_t) (sTamper->Tamper<<1);
  965. }
  966. tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->Trigger | (uint32_t)sTamper->Filter |\
  967. (uint32_t)sTamper->SamplingFrequency | (uint32_t)sTamper->PrechargeDuration |\
  968. (uint32_t)sTamper->TamperPullUp | sTamper->TimeStampOnTamperDetection);
  969. hrtc->Instance->TAFCR &= (uint32_t)~((uint32_t)sTamper->Tamper | (uint32_t)(sTamper->Tamper << 1) | (uint32_t)RTC_TAFCR_TAMPTS |\
  970. (uint32_t)RTC_TAFCR_TAMPFREQ | (uint32_t)RTC_TAFCR_TAMPFLT | (uint32_t)RTC_TAFCR_TAMPPRCH |\
  971. (uint32_t)RTC_TAFCR_TAMPPUDIS);
  972. #else
  973. tmpreg = ((uint32_t)sTamper->Tamper | (uint32_t)sTamper->Trigger);
  974. hrtc->Instance->TAFCR &= (uint32_t)~((uint32_t)RTC_TAFCR_TAMP1E | (uint32_t)RTC_TAFCR_TAMP1TRG | (uint32_t)RTC_TAFCR_TAMPIE);
  975. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  976. hrtc->Instance->TAFCR |= tmpreg;
  977. /* Configure the Tamper Interrupt in the RTC_TAFCR */
  978. hrtc->Instance->TAFCR |= (uint32_t)RTC_TAFCR_TAMPIE;
  979. /* RTC Tamper Interrupt Configuration: EXTI configuration */
  980. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_IT();
  981. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_ENABLE_RISING_EDGE();
  982. hrtc->State = HAL_RTC_STATE_READY;
  983. /* Process Unlocked */
  984. __HAL_UNLOCK(hrtc);
  985. return HAL_OK;
  986. }
  987. /**
  988. * @brief Deactivates Tamper.
  989. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  990. * the configuration information for RTC.
  991. * @param Tamper: Selected tamper pin.
  992. * This parameter can be a value of @ref RTCEx_Tamper_Pins_Definitions
  993. * @retval HAL status
  994. */
  995. HAL_StatusTypeDef HAL_RTCEx_DeactivateTamper(RTC_HandleTypeDef *hrtc, uint32_t Tamper)
  996. {
  997. assert_param(IS_RTC_TAMPER(Tamper));
  998. /* Process Locked */
  999. __HAL_LOCK(hrtc);
  1000. hrtc->State = HAL_RTC_STATE_BUSY;
  1001. /* Disable the selected Tamper pin */
  1002. hrtc->Instance->TAFCR &= (uint32_t)~Tamper;
  1003. hrtc->State = HAL_RTC_STATE_READY;
  1004. /* Process Unlocked */
  1005. __HAL_UNLOCK(hrtc);
  1006. return HAL_OK;
  1007. }
  1008. /**
  1009. * @brief This function handles TimeStamp interrupt request.
  1010. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1011. * the configuration information for RTC.
  1012. * @retval None
  1013. */
  1014. void HAL_RTCEx_TamperTimeStampIRQHandler(RTC_HandleTypeDef *hrtc)
  1015. {
  1016. /* Get the TimeStamp interrupt source enable status */
  1017. if(__HAL_RTC_TIMESTAMP_GET_IT_SOURCE(hrtc, RTC_IT_TS) != RESET)
  1018. {
  1019. /* Get the pending status of the TIMESTAMP Interrupt */
  1020. if(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) != RESET)
  1021. {
  1022. /* TIMESTAMP callback */
  1023. HAL_RTCEx_TimeStampEventCallback(hrtc);
  1024. /* Clear the TIMESTAMP interrupt pending bit */
  1025. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSF);
  1026. }
  1027. }
  1028. /* Get the Tamper1 interrupts source enable status */
  1029. if(__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP1) != RESET)
  1030. {
  1031. /* Get the pending status of the Tamper1 Interrupt */
  1032. if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP1F) != RESET)
  1033. {
  1034. /* Tamper1 callback */
  1035. HAL_RTCEx_Tamper1EventCallback(hrtc);
  1036. /* Clear the Tamper1 interrupt pending bit */
  1037. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP1F);
  1038. }
  1039. }
  1040. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  1041. /* Get the Tamper2 interrupts source enable status */
  1042. if(__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP2) != RESET)
  1043. {
  1044. /* Get the pending status of the Tamper2 Interrupt */
  1045. if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP2F) != RESET)
  1046. {
  1047. /* Tamper2 callback */
  1048. HAL_RTCEx_Tamper2EventCallback(hrtc);
  1049. /* Clear the Tamper2 interrupt pending bit */
  1050. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP2F);
  1051. }
  1052. }
  1053. /* Get the Tamper3 interrupts source enable status */
  1054. if(__HAL_RTC_TAMPER_GET_IT_SOURCE(hrtc, RTC_IT_TAMP | RTC_IT_TAMP3) != RESET)
  1055. {
  1056. /* Get the pending status of the Tamper3 Interrupt */
  1057. if(__HAL_RTC_TAMPER_GET_FLAG(hrtc, RTC_FLAG_TAMP3F) != RESET)
  1058. {
  1059. /* Tamper3 callback */
  1060. HAL_RTCEx_Tamper3EventCallback(hrtc);
  1061. /* Clear the Tamper3 interrupt pending bit */
  1062. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc, RTC_FLAG_TAMP3F);
  1063. }
  1064. }
  1065. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  1066. /* Clear the EXTI's Flag for RTC TimeStamp and Tamper */
  1067. __HAL_RTC_TAMPER_TIMESTAMP_EXTI_CLEAR_FLAG();
  1068. /* Change RTC state */
  1069. hrtc->State = HAL_RTC_STATE_READY;
  1070. }
  1071. /**
  1072. * @brief TimeStamp callback.
  1073. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1074. * the configuration information for RTC.
  1075. * @retval None
  1076. */
  1077. __weak void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
  1078. {
  1079. /* Prevent unused argument(s) compilation warning */
  1080. UNUSED(hrtc);
  1081. /* NOTE : This function Should not be modified, when the callback is needed,
  1082. the HAL_RTCEx_TimeStampEventCallback could be implemented in the user file
  1083. */
  1084. }
  1085. /**
  1086. * @brief Tamper 1 callback.
  1087. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1088. * the configuration information for RTC.
  1089. * @retval None
  1090. */
  1091. __weak void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
  1092. {
  1093. /* Prevent unused argument(s) compilation warning */
  1094. UNUSED(hrtc);
  1095. /* NOTE : This function Should not be modified, when the callback is needed,
  1096. the HAL_RTCEx_Tamper1EventCallback could be implemented in the user file
  1097. */
  1098. }
  1099. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  1100. /**
  1101. * @brief Tamper 2 callback.
  1102. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1103. * the configuration information for RTC.
  1104. * @retval None
  1105. */
  1106. __weak void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
  1107. {
  1108. /* Prevent unused argument(s) compilation warning */
  1109. UNUSED(hrtc);
  1110. /* NOTE : This function should not be modified, when the callback is needed,
  1111. the HAL_RTCEx_Tamper2EventCallback could be implemented in the user file
  1112. */
  1113. }
  1114. /**
  1115. * @brief Tamper 3 callback.
  1116. * @param hrtc: RTC handle
  1117. * @retval None
  1118. */
  1119. __weak void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef *hrtc)
  1120. {
  1121. /* Prevent unused argument(s) compilation warning */
  1122. UNUSED(hrtc);
  1123. /* NOTE : This function Should not be modified, when the callback is needed,
  1124. the HAL_RTCEx_Tamper3EventCallback could be implemented in the user file
  1125. */
  1126. }
  1127. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  1128. /**
  1129. * @brief This function handles TimeStamp polling request.
  1130. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1131. * the configuration information for RTC.
  1132. * @param Timeout: Timeout duration
  1133. * @retval HAL status
  1134. */
  1135. HAL_StatusTypeDef HAL_RTCEx_PollForTimeStampEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  1136. {
  1137. uint32_t tickstart = HAL_GetTick();
  1138. while(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSF) == RESET)
  1139. {
  1140. if(__HAL_RTC_TIMESTAMP_GET_FLAG(hrtc, RTC_FLAG_TSOVF) != RESET)
  1141. {
  1142. /* Clear the TIMESTAMP OverRun Flag */
  1143. __HAL_RTC_TIMESTAMP_CLEAR_FLAG(hrtc, RTC_FLAG_TSOVF);
  1144. /* Change TIMESTAMP state */
  1145. hrtc->State = HAL_RTC_STATE_ERROR;
  1146. return HAL_ERROR;
  1147. }
  1148. if(Timeout != HAL_MAX_DELAY)
  1149. {
  1150. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1151. {
  1152. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1153. return HAL_TIMEOUT;
  1154. }
  1155. }
  1156. }
  1157. /* Change RTC state */
  1158. hrtc->State = HAL_RTC_STATE_READY;
  1159. return HAL_OK;
  1160. }
  1161. /**
  1162. * @brief This function handles Tamper1 Polling.
  1163. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1164. * the configuration information for RTC.
  1165. * @param Timeout: Timeout duration
  1166. * @retval HAL status
  1167. */
  1168. HAL_StatusTypeDef HAL_RTCEx_PollForTamper1Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  1169. {
  1170. uint32_t tickstart = HAL_GetTick();
  1171. /* Get the status of the Interrupt */
  1172. while(__HAL_RTC_TAMPER_GET_FLAG(hrtc,RTC_FLAG_TAMP1F)== RESET)
  1173. {
  1174. if(Timeout != HAL_MAX_DELAY)
  1175. {
  1176. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1177. {
  1178. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1179. return HAL_TIMEOUT;
  1180. }
  1181. }
  1182. }
  1183. /* Clear the Tamper Flag */
  1184. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP1F);
  1185. /* Change RTC state */
  1186. hrtc->State = HAL_RTC_STATE_READY;
  1187. return HAL_OK;
  1188. }
  1189. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  1190. /**
  1191. * @brief This function handles Tamper2 Polling.
  1192. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1193. * the configuration information for RTC.
  1194. * @param Timeout: Timeout duration
  1195. * @retval HAL status
  1196. */
  1197. HAL_StatusTypeDef HAL_RTCEx_PollForTamper2Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  1198. {
  1199. uint32_t tickstart = HAL_GetTick();
  1200. /* Get the status of the Interrupt */
  1201. while(__HAL_RTC_TAMPER_GET_FLAG(hrtc,RTC_FLAG_TAMP2F) == RESET)
  1202. {
  1203. if(Timeout != HAL_MAX_DELAY)
  1204. {
  1205. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1206. {
  1207. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1208. return HAL_TIMEOUT;
  1209. }
  1210. }
  1211. }
  1212. /* Clear the Tamper Flag */
  1213. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP2F);
  1214. /* Change RTC state */
  1215. hrtc->State = HAL_RTC_STATE_READY;
  1216. return HAL_OK;
  1217. }
  1218. /**
  1219. * @brief This function handles Tamper3 Polling.
  1220. * @param hrtc: RTC handle
  1221. * @param Timeout: Timeout duration
  1222. * @retval HAL status
  1223. */
  1224. HAL_StatusTypeDef HAL_RTCEx_PollForTamper3Event(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  1225. {
  1226. uint32_t tickstart = HAL_GetTick();
  1227. /* Get the status of the Interrupt */
  1228. while(__HAL_RTC_TAMPER_GET_FLAG(hrtc,RTC_FLAG_TAMP3F) == RESET)
  1229. {
  1230. if(Timeout != HAL_MAX_DELAY)
  1231. {
  1232. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1233. {
  1234. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1235. return HAL_TIMEOUT;
  1236. }
  1237. }
  1238. }
  1239. /* Clear the Tamper Flag */
  1240. __HAL_RTC_TAMPER_CLEAR_FLAG(hrtc,RTC_FLAG_TAMP3F);
  1241. /* Change RTC state */
  1242. hrtc->State = HAL_RTC_STATE_READY;
  1243. return HAL_OK;
  1244. }
  1245. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  1246. /**
  1247. * @}
  1248. */
  1249. /** @defgroup RTCEx_Exported_Functions_Group5 RTC Wake-up functions
  1250. * @brief RTC Wake-up functions
  1251. *
  1252. @verbatim
  1253. ===============================================================================
  1254. ##### RTC Wake-up functions #####
  1255. ===============================================================================
  1256. [..] This section provides functions allowing to configure Wake-up feature
  1257. @endverbatim
  1258. * @{
  1259. */
  1260. /**
  1261. * @brief Set wake up timer.
  1262. * @param hrtc: RTC handle
  1263. * @param WakeUpCounter: Wake up counter
  1264. * @param WakeUpClock: Wake up clock
  1265. * @retval HAL status
  1266. */
  1267. HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
  1268. {
  1269. uint32_t tickstart = 0;
  1270. /* Check the parameters */
  1271. assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
  1272. assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
  1273. /* Process Locked */
  1274. __HAL_LOCK(hrtc);
  1275. hrtc->State = HAL_RTC_STATE_BUSY;
  1276. /* Disable the write protection for RTC registers */
  1277. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1278. /*Check RTC WUTWF flag is reset only when wake up timer enabled*/
  1279. if((hrtc->Instance->CR & RTC_CR_WUTE) != RESET){
  1280. tickstart = HAL_GetTick();
  1281. /* Wait till RTC WUTWF flag is reset and if Time out is reached exit */
  1282. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == SET)
  1283. {
  1284. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  1285. {
  1286. /* Enable the write protection for RTC registers */
  1287. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1288. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1289. /* Process Unlocked */
  1290. __HAL_UNLOCK(hrtc);
  1291. return HAL_TIMEOUT;
  1292. }
  1293. }
  1294. }
  1295. __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  1296. tickstart = HAL_GetTick();
  1297. /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
  1298. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
  1299. {
  1300. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  1301. {
  1302. /* Enable the write protection for RTC registers */
  1303. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1304. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1305. /* Process Unlocked */
  1306. __HAL_UNLOCK(hrtc);
  1307. return HAL_TIMEOUT;
  1308. }
  1309. }
  1310. /* Clear the Wakeup Timer clock source bits in CR register */
  1311. hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
  1312. /* Configure the clock source */
  1313. hrtc->Instance->CR |= (uint32_t)WakeUpClock;
  1314. /* Configure the Wakeup Timer counter */
  1315. hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
  1316. /* Enable the Wakeup Timer */
  1317. __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
  1318. /* Enable the write protection for RTC registers */
  1319. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1320. hrtc->State = HAL_RTC_STATE_READY;
  1321. /* Process Unlocked */
  1322. __HAL_UNLOCK(hrtc);
  1323. return HAL_OK;
  1324. }
  1325. /**
  1326. * @brief Set wake up timer with interrupt.
  1327. * @param hrtc: RTC handle
  1328. * @param WakeUpCounter: Wake up counter
  1329. * @param WakeUpClock: Wake up clock
  1330. * @retval HAL status
  1331. */
  1332. HAL_StatusTypeDef HAL_RTCEx_SetWakeUpTimer_IT(RTC_HandleTypeDef *hrtc, uint32_t WakeUpCounter, uint32_t WakeUpClock)
  1333. {
  1334. uint32_t tickstart = 0;
  1335. /* Check the parameters */
  1336. assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock));
  1337. assert_param(IS_RTC_WAKEUP_COUNTER(WakeUpCounter));
  1338. /* Process Locked */
  1339. __HAL_LOCK(hrtc);
  1340. hrtc->State = HAL_RTC_STATE_BUSY;
  1341. /* Disable the write protection for RTC registers */
  1342. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1343. /*Check RTC WUTWF flag is reset only when wake up timer enabled*/
  1344. if((hrtc->Instance->CR & RTC_CR_WUTE) != RESET){
  1345. tickstart = HAL_GetTick();
  1346. /* Wait till RTC WUTWF flag is reset and if Time out is reached exit */
  1347. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == SET)
  1348. {
  1349. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  1350. {
  1351. /* Enable the write protection for RTC registers */
  1352. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1353. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1354. /* Process Unlocked */
  1355. __HAL_UNLOCK(hrtc);
  1356. return HAL_TIMEOUT;
  1357. }
  1358. }
  1359. }
  1360. /* Disable the Wake-Up timer */
  1361. __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  1362. /* Clear flag Wake-Up */
  1363. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  1364. tickstart = HAL_GetTick();
  1365. /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
  1366. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
  1367. {
  1368. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  1369. {
  1370. /* Enable the write protection for RTC registers */
  1371. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1372. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1373. /* Process Unlocked */
  1374. __HAL_UNLOCK(hrtc);
  1375. return HAL_TIMEOUT;
  1376. }
  1377. }
  1378. /* Configure the Wakeup Timer counter */
  1379. hrtc->Instance->WUTR = (uint32_t)WakeUpCounter;
  1380. /* Clear the Wakeup Timer clock source bits in CR register */
  1381. hrtc->Instance->CR &= (uint32_t)~RTC_CR_WUCKSEL;
  1382. /* Configure the clock source */
  1383. hrtc->Instance->CR |= (uint32_t)WakeUpClock;
  1384. /* RTC WakeUpTimer Interrupt Configuration: EXTI configuration */
  1385. __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_IT();
  1386. __HAL_RTC_WAKEUPTIMER_EXTI_ENABLE_RISING_EDGE();
  1387. /* Configure the Interrupt in the RTC_CR register */
  1388. __HAL_RTC_WAKEUPTIMER_ENABLE_IT(hrtc,RTC_IT_WUT);
  1389. /* Enable the Wakeup Timer */
  1390. __HAL_RTC_WAKEUPTIMER_ENABLE(hrtc);
  1391. /* Enable the write protection for RTC registers */
  1392. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1393. hrtc->State = HAL_RTC_STATE_READY;
  1394. /* Process Unlocked */
  1395. __HAL_UNLOCK(hrtc);
  1396. return HAL_OK;
  1397. }
  1398. /**
  1399. * @brief Deactivates wake up timer counter.
  1400. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1401. * the configuration information for RTC.
  1402. * @retval HAL status
  1403. */
  1404. uint32_t HAL_RTCEx_DeactivateWakeUpTimer(RTC_HandleTypeDef *hrtc)
  1405. {
  1406. uint32_t tickstart = 0;
  1407. /* Process Locked */
  1408. __HAL_LOCK(hrtc);
  1409. hrtc->State = HAL_RTC_STATE_BUSY;
  1410. /* Disable the write protection for RTC registers */
  1411. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1412. /* Disable the Wakeup Timer */
  1413. __HAL_RTC_WAKEUPTIMER_DISABLE(hrtc);
  1414. /* In case of interrupt mode is used, the interrupt source must disabled */
  1415. __HAL_RTC_WAKEUPTIMER_DISABLE_IT(hrtc,RTC_IT_WUT);
  1416. tickstart = HAL_GetTick();
  1417. /* Wait till RTC WUTWF flag is set and if Time out is reached exit */
  1418. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTWF) == RESET)
  1419. {
  1420. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  1421. {
  1422. /* Enable the write protection for RTC registers */
  1423. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1424. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1425. /* Process Unlocked */
  1426. __HAL_UNLOCK(hrtc);
  1427. return HAL_TIMEOUT;
  1428. }
  1429. }
  1430. /* Enable the write protection for RTC registers */
  1431. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1432. hrtc->State = HAL_RTC_STATE_READY;
  1433. /* Process Unlocked */
  1434. __HAL_UNLOCK(hrtc);
  1435. return HAL_OK;
  1436. }
  1437. /**
  1438. * @brief Gets wake up timer counter.
  1439. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1440. * the configuration information for RTC.
  1441. * @retval Counter value
  1442. */
  1443. uint32_t HAL_RTCEx_GetWakeUpTimer(RTC_HandleTypeDef *hrtc)
  1444. {
  1445. /* Get the counter value */
  1446. return ((uint32_t)(hrtc->Instance->WUTR & RTC_WUTR_WUT));
  1447. }
  1448. /**
  1449. * @brief This function handles Wake Up Timer interrupt request.
  1450. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1451. * the configuration information for RTC.
  1452. * @retval None
  1453. */
  1454. void HAL_RTCEx_WakeUpTimerIRQHandler(RTC_HandleTypeDef *hrtc)
  1455. {
  1456. /* Get the pending status of the WAKEUPTIMER Interrupt */
  1457. if(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) != RESET)
  1458. {
  1459. /* WAKEUPTIMER callback */
  1460. HAL_RTCEx_WakeUpTimerEventCallback(hrtc);
  1461. /* Clear the WAKEUPTIMER interrupt pending bit */
  1462. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  1463. }
  1464. /* Clear the EXTI's line Flag for RTC WakeUpTimer */
  1465. __HAL_RTC_WAKEUPTIMER_EXTI_CLEAR_FLAG();
  1466. /* Change RTC state */
  1467. hrtc->State = HAL_RTC_STATE_READY;
  1468. }
  1469. /**
  1470. * @brief Wake Up Timer callback.
  1471. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1472. * the configuration information for RTC.
  1473. * @retval None
  1474. */
  1475. __weak void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
  1476. {
  1477. /* Prevent unused argument(s) compilation warning */
  1478. UNUSED(hrtc);
  1479. /* NOTE : This function Should not be modified, when the callback is needed,
  1480. the HAL_RTCEx_WakeUpTimerEventCallback could be implemented in the user file
  1481. */
  1482. }
  1483. /**
  1484. * @brief This function handles Wake Up Timer Polling.
  1485. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1486. * the configuration information for RTC.
  1487. * @param Timeout: Timeout duration
  1488. * @retval HAL status
  1489. */
  1490. HAL_StatusTypeDef HAL_RTCEx_PollForWakeUpTimerEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  1491. {
  1492. uint32_t tickstart = HAL_GetTick();
  1493. while(__HAL_RTC_WAKEUPTIMER_GET_FLAG(hrtc, RTC_FLAG_WUTF) == RESET)
  1494. {
  1495. if(Timeout != HAL_MAX_DELAY)
  1496. {
  1497. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1498. {
  1499. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1500. return HAL_TIMEOUT;
  1501. }
  1502. }
  1503. }
  1504. /* Clear the WAKEUPTIMER Flag */
  1505. __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(hrtc, RTC_FLAG_WUTF);
  1506. /* Change RTC state */
  1507. hrtc->State = HAL_RTC_STATE_READY;
  1508. return HAL_OK;
  1509. }
  1510. /**
  1511. * @}
  1512. */
  1513. /** @defgroup RTCEx_Exported_Functions_Group7 Extended Peripheral Control functions
  1514. * @brief Extended Peripheral Control functions
  1515. *
  1516. @verbatim
  1517. ===============================================================================
  1518. ##### Extension Peripheral Control functions #####
  1519. ===============================================================================
  1520. [..]
  1521. This subsection provides functions allowing to
  1522. (+) Writes a data in a specified RTC Backup data register
  1523. (+) Read a data in a specified RTC Backup data register
  1524. (+) Sets the Coarse calibration parameters.
  1525. (+) Deactivates the Coarse calibration parameters
  1526. (+) Sets the Smooth calibration parameters.
  1527. (+) Configures the Synchronization Shift Control Settings.
  1528. (+) Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1529. (+) Deactivates the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1530. (+) Enables the RTC reference clock detection.
  1531. (+) Disable the RTC reference clock detection.
  1532. (+) Enables the Bypass Shadow feature.
  1533. (+) Disables the Bypass Shadow feature.
  1534. @endverbatim
  1535. * @{
  1536. */
  1537. /**
  1538. * @brief Writes a data in a specified RTC Backup data register.
  1539. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1540. * the configuration information for RTC.
  1541. * @param BackupRegister: RTC Backup data Register number.
  1542. * This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
  1543. * specify the register.
  1544. * @param Data: Data to be written in the specified RTC Backup data register.
  1545. * @retval None
  1546. */
  1547. void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)
  1548. {
  1549. uint32_t tmp = 0;
  1550. /* Check the parameters */
  1551. assert_param(IS_RTC_BKP(BackupRegister));
  1552. tmp = (uint32_t)&(hrtc->Instance->BKP0R);
  1553. tmp += (BackupRegister * 4);
  1554. /* Write the specified register */
  1555. *(__IO uint32_t *)tmp = (uint32_t)Data;
  1556. }
  1557. /**
  1558. * @brief Reads data from the specified RTC Backup data Register.
  1559. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1560. * the configuration information for RTC.
  1561. * @param BackupRegister: RTC Backup data Register number.
  1562. * This parameter can be: RTC_BKP_DRx where x can be from 0 to 19 to
  1563. * specify the register.
  1564. * @retval Read value
  1565. */
  1566. uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
  1567. {
  1568. uint32_t tmp = 0;
  1569. /* Check the parameters */
  1570. assert_param(IS_RTC_BKP(BackupRegister));
  1571. tmp = (uint32_t)&(hrtc->Instance->BKP0R);
  1572. tmp += (BackupRegister * 4);
  1573. /* Read the specified register */
  1574. return (*(__IO uint32_t *)tmp);
  1575. }
  1576. /**
  1577. * @brief Sets the Coarse calibration parameters.
  1578. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1579. * the configuration information for RTC.
  1580. * @param CalibSign: Specifies the sign of the coarse calibration value.
  1581. * This parameter can be one of the following values :
  1582. * @arg RTC_CALIBSIGN_POSITIVE: The value sign is positive
  1583. * @arg RTC_CALIBSIGN_NEGATIVE: The value sign is negative
  1584. * @param Value: value of coarse calibration expressed in ppm (coded on 5 bits).
  1585. *
  1586. * @note This Calibration value should be between 0 and 63 when using negative
  1587. * sign with a 2-ppm step.
  1588. *
  1589. * @note This Calibration value should be between 0 and 126 when using positive
  1590. * sign with a 4-ppm step.
  1591. * @retval HAL status
  1592. */
  1593. HAL_StatusTypeDef HAL_RTCEx_SetCoarseCalib(RTC_HandleTypeDef* hrtc, uint32_t CalibSign, uint32_t Value)
  1594. {
  1595. /* Check the parameters */
  1596. assert_param(IS_RTC_CALIB_SIGN(CalibSign));
  1597. assert_param(IS_RTC_CALIB_VALUE(Value));
  1598. /* Process Locked */
  1599. __HAL_LOCK(hrtc);
  1600. hrtc->State = HAL_RTC_STATE_BUSY;
  1601. /* Disable the write protection for RTC registers */
  1602. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1603. /* Set Initialization mode */
  1604. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  1605. {
  1606. /* Enable the write protection for RTC registers */
  1607. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1608. /* Set RTC state*/
  1609. hrtc->State = HAL_RTC_STATE_ERROR;
  1610. /* Process Unlocked */
  1611. __HAL_UNLOCK(hrtc);
  1612. return HAL_ERROR;
  1613. }
  1614. else
  1615. {
  1616. /* Enable the Coarse Calibration */
  1617. __HAL_RTC_COARSE_CALIB_ENABLE(hrtc);
  1618. /* Set the coarse calibration value */
  1619. hrtc->Instance->CALIBR = (uint32_t)(CalibSign|Value);
  1620. /* Exit Initialization mode */
  1621. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  1622. }
  1623. /* Enable the write protection for RTC registers */
  1624. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1625. /* Change state */
  1626. hrtc->State = HAL_RTC_STATE_READY;
  1627. /* Process Unlocked */
  1628. __HAL_UNLOCK(hrtc);
  1629. return HAL_OK;
  1630. }
  1631. /**
  1632. * @brief Deactivates the Coarse calibration parameters.
  1633. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1634. * the configuration information for RTC.
  1635. * @retval HAL status
  1636. */
  1637. HAL_StatusTypeDef HAL_RTCEx_DeactivateCoarseCalib(RTC_HandleTypeDef* hrtc)
  1638. {
  1639. /* Process Locked */
  1640. __HAL_LOCK(hrtc);
  1641. hrtc->State = HAL_RTC_STATE_BUSY;
  1642. /* Disable the write protection for RTC registers */
  1643. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1644. /* Set Initialization mode */
  1645. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  1646. {
  1647. /* Enable the write protection for RTC registers */
  1648. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1649. /* Set RTC state*/
  1650. hrtc->State = HAL_RTC_STATE_ERROR;
  1651. /* Process Unlocked */
  1652. __HAL_UNLOCK(hrtc);
  1653. return HAL_ERROR;
  1654. }
  1655. else
  1656. {
  1657. /* Enable the Coarse Calibration */
  1658. __HAL_RTC_COARSE_CALIB_DISABLE(hrtc);
  1659. /* Exit Initialization mode */
  1660. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  1661. }
  1662. /* Enable the write protection for RTC registers */
  1663. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1664. /* Change state */
  1665. hrtc->State = HAL_RTC_STATE_READY;
  1666. /* Process Unlocked */
  1667. __HAL_UNLOCK(hrtc);
  1668. return HAL_OK;
  1669. }
  1670. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  1671. /**
  1672. * @brief Sets the Smooth calibration parameters.
  1673. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1674. * the configuration information for RTC.
  1675. * @param SmoothCalibPeriod: Select the Smooth Calibration Period.
  1676. * This parameter can be can be one of the following values :
  1677. * @arg RTC_SMOOTHCALIB_PERIOD_32SEC: The smooth calibration periode is 32s.
  1678. * @arg RTC_SMOOTHCALIB_PERIOD_16SEC: The smooth calibration periode is 16s.
  1679. * @arg RTC_SMOOTHCALIB_PERIOD_8SEC: The smooth calibartion periode is 8s.
  1680. * @param SmoothCalibPlusPulses: Select to Set or reset the CALP bit.
  1681. * This parameter can be one of the following values:
  1682. * @arg RTC_SMOOTHCALIB_PLUSPULSES_SET: Add one RTCCLK puls every 2*11 pulses.
  1683. * @arg RTC_SMOOTHCALIB_PLUSPULSES_RESET: No RTCCLK pulses are added.
  1684. * @param SmoothCalibMinusPulsesValue: Select the value of CALM[8:0] bits.
  1685. * This parameter can be one any value from 0 to 0x000001FF.
  1686. * @note To deactivate the smooth calibration, the field SmoothCalibPlusPulses
  1687. * must be equal to SMOOTHCALIB_PLUSPULSES_RESET and the field
  1688. * SmoothCalibMinusPulsesValue mut be equal to 0.
  1689. * @retval HAL status
  1690. */
  1691. HAL_StatusTypeDef HAL_RTCEx_SetSmoothCalib(RTC_HandleTypeDef* hrtc, uint32_t SmoothCalibPeriod, uint32_t SmoothCalibPlusPulses, uint32_t SmoothCalibMinusPulsesValue)
  1692. {
  1693. uint32_t tickstart = 0;
  1694. /* Check the parameters */
  1695. assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(SmoothCalibPeriod));
  1696. assert_param(IS_RTC_SMOOTH_CALIB_PLUS(SmoothCalibPlusPulses));
  1697. assert_param(IS_RTC_SMOOTH_CALIB_MINUS(SmoothCalibMinusPulsesValue));
  1698. /* Process Locked */
  1699. __HAL_LOCK(hrtc);
  1700. hrtc->State = HAL_RTC_STATE_BUSY;
  1701. /* Disable the write protection for RTC registers */
  1702. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1703. /* check if a calibration is pending*/
  1704. if((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)
  1705. {
  1706. tickstart = HAL_GetTick();
  1707. /* check if a calibration is pending*/
  1708. while((hrtc->Instance->ISR & RTC_ISR_RECALPF) != RESET)
  1709. {
  1710. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  1711. {
  1712. /* Enable the write protection for RTC registers */
  1713. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1714. /* Change RTC state */
  1715. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1716. /* Process Unlocked */
  1717. __HAL_UNLOCK(hrtc);
  1718. return HAL_TIMEOUT;
  1719. }
  1720. }
  1721. }
  1722. /* Configure the Smooth calibration settings */
  1723. hrtc->Instance->CALR = (uint32_t)((uint32_t)SmoothCalibPeriod | (uint32_t)SmoothCalibPlusPulses | (uint32_t)SmoothCalibMinusPulsesValue);
  1724. /* Enable the write protection for RTC registers */
  1725. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1726. /* Change RTC state */
  1727. hrtc->State = HAL_RTC_STATE_READY;
  1728. /* Process Unlocked */
  1729. __HAL_UNLOCK(hrtc);
  1730. return HAL_OK;
  1731. }
  1732. /**
  1733. * @brief Configures the Synchronization Shift Control Settings.
  1734. * @note When REFCKON is set, firmware must not write to Shift control register.
  1735. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1736. * the configuration information for RTC.
  1737. * @param ShiftAdd1S: Select to add or not 1 second to the time calendar.
  1738. * This parameter can be one of the following values :
  1739. * @arg RTC_SHIFTADD1S_SET: Add one second to the clock calendar.
  1740. * @arg RTC_SHIFTADD1S_RESET: No effect.
  1741. * @param ShiftSubFS: Select the number of Second Fractions to substitute.
  1742. * This parameter can be one any value from 0 to 0x7FFF.
  1743. * @retval HAL status
  1744. */
  1745. HAL_StatusTypeDef HAL_RTCEx_SetSynchroShift(RTC_HandleTypeDef* hrtc, uint32_t ShiftAdd1S, uint32_t ShiftSubFS)
  1746. {
  1747. uint32_t tickstart = 0;
  1748. /* Check the parameters */
  1749. assert_param(IS_RTC_SHIFT_ADD1S(ShiftAdd1S));
  1750. assert_param(IS_RTC_SHIFT_SUBFS(ShiftSubFS));
  1751. /* Process Locked */
  1752. __HAL_LOCK(hrtc);
  1753. hrtc->State = HAL_RTC_STATE_BUSY;
  1754. /* Disable the write protection for RTC registers */
  1755. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1756. tickstart = HAL_GetTick();
  1757. /* Wait until the shift is completed*/
  1758. while((hrtc->Instance->ISR & RTC_ISR_SHPF) != RESET)
  1759. {
  1760. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  1761. {
  1762. /* Enable the write protection for RTC registers */
  1763. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1764. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1765. /* Process Unlocked */
  1766. __HAL_UNLOCK(hrtc);
  1767. return HAL_TIMEOUT;
  1768. }
  1769. }
  1770. /* Check if the reference clock detection is disabled */
  1771. if((hrtc->Instance->CR & RTC_CR_REFCKON) == RESET)
  1772. {
  1773. /* Configure the Shift settings */
  1774. hrtc->Instance->SHIFTR = (uint32_t)(uint32_t)(ShiftSubFS) | (uint32_t)(ShiftAdd1S);
  1775. /* Wait for synchro */
  1776. if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  1777. {
  1778. /* Enable the write protection for RTC registers */
  1779. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1780. hrtc->State = HAL_RTC_STATE_ERROR;
  1781. /* Process Unlocked */
  1782. __HAL_UNLOCK(hrtc);
  1783. return HAL_ERROR;
  1784. }
  1785. }
  1786. else
  1787. {
  1788. /* Enable the write protection for RTC registers */
  1789. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1790. /* Change RTC state */
  1791. hrtc->State = HAL_RTC_STATE_ERROR;
  1792. /* Process Unlocked */
  1793. __HAL_UNLOCK(hrtc);
  1794. return HAL_ERROR;
  1795. }
  1796. /* Enable the write protection for RTC registers */
  1797. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1798. /* Change RTC state */
  1799. hrtc->State = HAL_RTC_STATE_READY;
  1800. /* Process Unlocked */
  1801. __HAL_UNLOCK(hrtc);
  1802. return HAL_OK;
  1803. }
  1804. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  1805. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  1806. /**
  1807. * @brief Configures the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1808. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1809. * the configuration information for RTC.
  1810. * @param CalibOutput : Select the Calibration output Selection .
  1811. * This parameter can be one of the following values:
  1812. * @arg RTC_CALIBOUTPUT_512HZ: A signal has a regular waveform at 512Hz.
  1813. * @arg RTC_CALIBOUTPUT_1HZ: A signal has a regular waveform at 1Hz.
  1814. * @retval HAL status
  1815. */
  1816. HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef* hrtc, uint32_t CalibOutput)
  1817. #else
  1818. /**
  1819. * @brief Configure the Calibration Pinout (RTC_CALIB).
  1820. * @param hrtc : RTC handle
  1821. * @retval HAL status
  1822. */
  1823. HAL_StatusTypeDef HAL_RTCEx_SetCalibrationOutPut(RTC_HandleTypeDef* hrtc)
  1824. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  1825. {
  1826. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  1827. /* Check the parameters */
  1828. assert_param(IS_RTC_CALIB_OUTPUT(CalibOutput));
  1829. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  1830. /* Process Locked */
  1831. __HAL_LOCK(hrtc);
  1832. hrtc->State = HAL_RTC_STATE_BUSY;
  1833. /* Disable the write protection for RTC registers */
  1834. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1835. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  1836. /* Clear flags before config */
  1837. hrtc->Instance->CR &= (uint32_t)~RTC_CR_COSEL;
  1838. /* Configure the RTC_CR register */
  1839. hrtc->Instance->CR |= (uint32_t)CalibOutput;
  1840. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  1841. __HAL_RTC_CALIBRATION_OUTPUT_ENABLE(hrtc);
  1842. /* Enable the write protection for RTC registers */
  1843. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1844. /* Change RTC state */
  1845. hrtc->State = HAL_RTC_STATE_READY;
  1846. /* Process Unlocked */
  1847. __HAL_UNLOCK(hrtc);
  1848. return HAL_OK;
  1849. }
  1850. /**
  1851. * @brief Deactivates the Calibration Pinout (RTC_CALIB) Selection (1Hz or 512Hz).
  1852. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1853. * the configuration information for RTC.
  1854. * @retval HAL status
  1855. */
  1856. HAL_StatusTypeDef HAL_RTCEx_DeactivateCalibrationOutPut(RTC_HandleTypeDef* hrtc)
  1857. {
  1858. /* Process Locked */
  1859. __HAL_LOCK(hrtc);
  1860. hrtc->State = HAL_RTC_STATE_BUSY;
  1861. /* Disable the write protection for RTC registers */
  1862. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1863. __HAL_RTC_CALIBRATION_OUTPUT_DISABLE(hrtc);
  1864. /* Enable the write protection for RTC registers */
  1865. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1866. /* Change RTC state */
  1867. hrtc->State = HAL_RTC_STATE_READY;
  1868. /* Process Unlocked */
  1869. __HAL_UNLOCK(hrtc);
  1870. return HAL_OK;
  1871. }
  1872. /**
  1873. * @brief Enables the RTC reference clock detection.
  1874. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1875. * the configuration information for RTC.
  1876. * @retval HAL status
  1877. */
  1878. HAL_StatusTypeDef HAL_RTCEx_SetRefClock(RTC_HandleTypeDef* hrtc)
  1879. {
  1880. /* Process Locked */
  1881. __HAL_LOCK(hrtc);
  1882. hrtc->State = HAL_RTC_STATE_BUSY;
  1883. /* Disable the write protection for RTC registers */
  1884. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1885. /* Set Initialization mode */
  1886. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  1887. {
  1888. /* Enable the write protection for RTC registers */
  1889. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1890. /* Set RTC state*/
  1891. hrtc->State = HAL_RTC_STATE_ERROR;
  1892. /* Process Unlocked */
  1893. __HAL_UNLOCK(hrtc);
  1894. return HAL_ERROR;
  1895. }
  1896. else
  1897. {
  1898. __HAL_RTC_CLOCKREF_DETECTION_ENABLE(hrtc);
  1899. /* Exit Initialization mode */
  1900. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  1901. }
  1902. /* Enable the write protection for RTC registers */
  1903. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1904. /* Change RTC state */
  1905. hrtc->State = HAL_RTC_STATE_READY;
  1906. /* Process Unlocked */
  1907. __HAL_UNLOCK(hrtc);
  1908. return HAL_OK;
  1909. }
  1910. /**
  1911. * @brief Disable the RTC reference clock detection.
  1912. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1913. * the configuration information for RTC.
  1914. * @retval HAL status
  1915. */
  1916. HAL_StatusTypeDef HAL_RTCEx_DeactivateRefClock(RTC_HandleTypeDef* hrtc)
  1917. {
  1918. /* Process Locked */
  1919. __HAL_LOCK(hrtc);
  1920. hrtc->State = HAL_RTC_STATE_BUSY;
  1921. /* Disable the write protection for RTC registers */
  1922. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1923. /* Set Initialization mode */
  1924. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  1925. {
  1926. /* Enable the write protection for RTC registers */
  1927. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1928. /* Set RTC state*/
  1929. hrtc->State = HAL_RTC_STATE_ERROR;
  1930. /* Process Unlocked */
  1931. __HAL_UNLOCK(hrtc);
  1932. return HAL_ERROR;
  1933. }
  1934. else
  1935. {
  1936. __HAL_RTC_CLOCKREF_DETECTION_DISABLE(hrtc);
  1937. /* Exit Initialization mode */
  1938. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  1939. }
  1940. /* Enable the write protection for RTC registers */
  1941. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1942. /* Change RTC state */
  1943. hrtc->State = HAL_RTC_STATE_READY;
  1944. /* Process Unlocked */
  1945. __HAL_UNLOCK(hrtc);
  1946. return HAL_OK;
  1947. }
  1948. #if defined(STM32L100xBA) || defined (STM32L151xBA) || defined (STM32L152xBA) || defined(STM32L100xC) || defined (STM32L151xC) || defined (STM32L152xC) || defined (STM32L162xC) || defined(STM32L151xCA) || defined (STM32L151xD) || defined (STM32L152xCA) || defined (STM32L152xD) || defined (STM32L162xCA) || defined (STM32L162xD) || defined(STM32L151xE) || defined(STM32L151xDX) || defined (STM32L152xE) || defined (STM32L152xDX) || defined (STM32L162xE) || defined (STM32L162xDX)
  1949. /**
  1950. * @brief Enables the Bypass Shadow feature.
  1951. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1952. * the configuration information for RTC.
  1953. * @note When the Bypass Shadow is enabled the calendar value are taken
  1954. * directly from the Calendar counter.
  1955. * @retval HAL status
  1956. */
  1957. HAL_StatusTypeDef HAL_RTCEx_EnableBypassShadow(RTC_HandleTypeDef* hrtc)
  1958. {
  1959. /* Process Locked */
  1960. __HAL_LOCK(hrtc);
  1961. hrtc->State = HAL_RTC_STATE_BUSY;
  1962. /* Disable the write protection for RTC registers */
  1963. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1964. /* Set the BYPSHAD bit */
  1965. hrtc->Instance->CR |= (uint8_t)RTC_CR_BYPSHAD;
  1966. /* Enable the write protection for RTC registers */
  1967. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1968. /* Change RTC state */
  1969. hrtc->State = HAL_RTC_STATE_READY;
  1970. /* Process Unlocked */
  1971. __HAL_UNLOCK(hrtc);
  1972. return HAL_OK;
  1973. }
  1974. /**
  1975. * @brief Disables the Bypass Shadow feature.
  1976. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  1977. * the configuration information for RTC.
  1978. * @note When the Bypass Shadow is enabled the calendar value are taken
  1979. * directly from the Calendar counter.
  1980. * @retval HAL status
  1981. */
  1982. HAL_StatusTypeDef HAL_RTCEx_DisableBypassShadow(RTC_HandleTypeDef* hrtc)
  1983. {
  1984. /* Process Locked */
  1985. __HAL_LOCK(hrtc);
  1986. hrtc->State = HAL_RTC_STATE_BUSY;
  1987. /* Disable the write protection for RTC registers */
  1988. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1989. /* Reset the BYPSHAD bit */
  1990. hrtc->Instance->CR &= (uint8_t)~RTC_CR_BYPSHAD;
  1991. /* Enable the write protection for RTC registers */
  1992. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1993. /* Change RTC state */
  1994. hrtc->State = HAL_RTC_STATE_READY;
  1995. /* Process Unlocked */
  1996. __HAL_UNLOCK(hrtc);
  1997. return HAL_OK;
  1998. }
  1999. #endif /* STM32L100xBA || STM32L151xBA || STM32L152xBA || STM32L100xC || STM32L151xC || STM32L152xC || STM32L162xC || STM32L151xCA || STM32L151xD || STM32L152xCA || STM32L152xD || STM32L162xCA || STM32L162xD || STM32L151xE || STM32L151xDX || STM32L152xE || STM32L152xDX || STM32L162xE || STM32L162xDX */
  2000. /**
  2001. * @}
  2002. */
  2003. /** @defgroup RTCEx_Exported_Functions_Group8 Extended features functions
  2004. * @brief Extended features functions
  2005. *
  2006. @verbatim
  2007. ===============================================================================
  2008. ##### Extended features functions #####
  2009. ===============================================================================
  2010. [..] This section provides functions allowing to:
  2011. (+) RTC Alram B callback
  2012. (+) RTC Poll for Alarm B request
  2013. @endverbatim
  2014. * @{
  2015. */
  2016. /**
  2017. * @brief Alarm B callback.
  2018. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  2019. * the configuration information for RTC.
  2020. * @retval None
  2021. */
  2022. __weak void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
  2023. {
  2024. /* Prevent unused argument(s) compilation warning */
  2025. UNUSED(hrtc);
  2026. /* NOTE : This function Should not be modified, when the callback is needed,
  2027. the HAL_RTCEx_AlarmBEventCallback could be implemented in the user file
  2028. */
  2029. }
  2030. /**
  2031. * @brief This function handles AlarmB Polling request.
  2032. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  2033. * the configuration information for RTC.
  2034. * @param Timeout: Timeout duration
  2035. * @retval HAL status
  2036. */
  2037. HAL_StatusTypeDef HAL_RTCEx_PollForAlarmBEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  2038. {
  2039. uint32_t tickstart = HAL_GetTick();
  2040. while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) == RESET)
  2041. {
  2042. if(Timeout != HAL_MAX_DELAY)
  2043. {
  2044. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  2045. {
  2046. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  2047. return HAL_TIMEOUT;
  2048. }
  2049. }
  2050. }
  2051. /* Clear the Alarm Flag */
  2052. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
  2053. /* Change RTC state */
  2054. hrtc->State = HAL_RTC_STATE_READY;
  2055. return HAL_OK;
  2056. }
  2057. /**
  2058. * @}
  2059. */
  2060. /**
  2061. * @}
  2062. */
  2063. /**
  2064. * @}
  2065. */
  2066. #endif /* HAL_RTC_MODULE_ENABLED */
  2067. /**
  2068. * @}
  2069. */
  2070. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/