stm32l1xx_hal_rtc_ex.c 95 KB

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