stm32f7xx_hal_rtc_ex.c 58 KB

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