stm32f4xx_hal_rtc.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_rtc.c
  4. * @author MCD Application Team
  5. * @brief RTC HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Real Time Clock (RTC) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + RTC Time and Date functions
  10. * + RTC Alarm functions
  11. * + Peripheral Control functions
  12. * + Peripheral State functions
  13. *
  14. @verbatim
  15. ==============================================================================
  16. ##### Backup Domain Operating Condition #####
  17. ==============================================================================
  18. [..] The real-time clock (RTC), the RTC backup registers, and the backup
  19. SRAM (BKP SRAM) can be powered from the VBAT voltage when the main
  20. VDD supply is powered off.
  21. To retain the content of the RTC backup registers, backup SRAM, and supply
  22. the RTC when VDD is turned off, VBAT pin can be connected to an optional
  23. standby voltage supplied by a battery or by another source.
  24. [..] To allow the RTC operating even when the main digital supply (VDD) is turned
  25. off, the VBAT pin powers the following blocks:
  26. (#) The RTC
  27. (#) The LSE oscillator
  28. (#) The backup SRAM when the low power backup regulator is enabled
  29. (#) PC13 to PC15 I/Os, plus PI8 I/O (when available)
  30. [..] When the backup domain is supplied by VDD (analog switch connected to VDD),
  31. the following pins are available:
  32. (#) PC14 and PC15 can be used as either GPIO or LSE pins
  33. (#) PC13 can be used as a GPIO or as the RTC_AF1 pin
  34. (#) PI8 can be used as a GPIO or as the RTC_AF2 pin
  35. [..] When the backup domain is supplied by VBAT (analog switch connected to VBAT
  36. because VDD is not present), the following pins are available:
  37. (#) PC14 and PC15 can be used as LSE pins only
  38. (#) PC13 can be used as the RTC_AF1 pin
  39. (#) PI8 can be used as the RTC_AF2 pin
  40. ##### Backup Domain Reset #####
  41. ==================================================================
  42. [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
  43. to their reset values. The BKPSRAM is not affected by this reset. The only
  44. way to reset the BKPSRAM is through the Flash interface by requesting
  45. a protection level change from 1 to 0.
  46. [..] A backup domain reset is generated when one of the following events occurs:
  47. (#) Software reset, triggered by setting the BDRST bit in the
  48. RCC Backup domain control register (RCC_BDCR).
  49. (#) VDD or VBAT power on, if both supplies have previously been powered off.
  50. ##### Backup Domain Access #####
  51. ==================================================================
  52. [..] After reset, the backup domain (RTC registers, RTC backup data
  53. registers and backup SRAM) is protected against possible unwanted write
  54. accesses.
  55. [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
  56. (+) Enable the Power Controller (PWR) APB1 interface clock using the
  57. __HAL_RCC_PWR_CLK_ENABLE() function.
  58. (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
  59. (+) Select the RTC clock source using the __HAL_RCC_RTC_CONFIG() function.
  60. (+) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() function.
  61. ##### How to use this driver #####
  62. ==================================================================
  63. [..]
  64. (+) Enable the RTC domain access (see description in the section above).
  65. (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
  66. format using the HAL_RTC_Init() function.
  67. *** Time and Date configuration ***
  68. ===================================
  69. [..]
  70. (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
  71. and HAL_RTC_SetDate() functions.
  72. (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
  73. *** Alarm configuration ***
  74. ===========================
  75. [..]
  76. (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
  77. You can also configure the RTC Alarm with interrupt mode using the HAL_RTC_SetAlarm_IT() function.
  78. (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
  79. ##### RTC and low power modes #####
  80. ==================================================================
  81. [..] The MCU can be woken up from a low power mode by an RTC alternate
  82. function.
  83. [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
  84. RTC wake-up, RTC tamper event detection and RTC time stamp event detection.
  85. These RTC alternate functions can wake up the system from the Stop and
  86. Standby low power modes.
  87. [..] The system can also wake up from low power modes without depending
  88. on an external interrupt (Auto-wake-up mode), by using the RTC alarm
  89. or the RTC wake-up events.
  90. [..] The RTC provides a programmable time base for waking up from the
  91. Stop or Standby mode at regular intervals.
  92. Wake-up from STOP and STANDBY modes is possible only when the RTC clock source
  93. is LSE or LSI.
  94. @endverbatim
  95. ******************************************************************************
  96. * @attention
  97. *
  98. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  99. *
  100. * Redistribution and use in source and binary forms, with or without modification,
  101. * are permitted provided that the following conditions are met:
  102. * 1. Redistributions of source code must retain the above copyright notice,
  103. * this list of conditions and the following disclaimer.
  104. * 2. Redistributions in binary form must reproduce the above copyright notice,
  105. * this list of conditions and the following disclaimer in the documentation
  106. * and/or other materials provided with the distribution.
  107. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  108. * may be used to endorse or promote products derived from this software
  109. * without specific prior written permission.
  110. *
  111. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  112. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  113. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  114. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  115. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  116. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  117. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  118. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  119. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  120. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  121. *
  122. ******************************************************************************
  123. */
  124. /* Includes ------------------------------------------------------------------*/
  125. #include "stm32f4xx_hal.h"
  126. /** @addtogroup STM32F4xx_HAL_Driver
  127. * @{
  128. */
  129. /** @defgroup RTC RTC
  130. * @brief RTC HAL module driver
  131. * @{
  132. */
  133. #ifdef HAL_RTC_MODULE_ENABLED
  134. /* Private typedef -----------------------------------------------------------*/
  135. /* Private define ------------------------------------------------------------*/
  136. /* Private macro -------------------------------------------------------------*/
  137. /* Private variables ---------------------------------------------------------*/
  138. /* Private function prototypes -----------------------------------------------*/
  139. /* Private functions ---------------------------------------------------------*/
  140. /** @defgroup RTC_Exported_Functions RTC Exported Functions
  141. * @{
  142. */
  143. /** @defgroup RTC_Exported_Functions_Group1 Initialization and de-initialization functions
  144. * @brief Initialization and Configuration functions
  145. *
  146. @verbatim
  147. ===============================================================================
  148. ##### Initialization and de-initialization functions #####
  149. ===============================================================================
  150. [..] This section provides functions allowing to initialize and configure the
  151. RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
  152. RTC registers Write protection, enter and exit the RTC initialization mode,
  153. RTC registers synchronization check and reference clock detection enable.
  154. (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
  155. It is split into 2 programmable prescalers to minimize power consumption.
  156. (++) A 7-bit asynchronous prescaler and a 13-bit synchronous prescaler.
  157. (++) When both prescalers are used, it is recommended to configure the
  158. asynchronous prescaler to a high value to minimize power consumption.
  159. (#) All RTC registers are Write protected. Writing to the RTC registers
  160. is enabled by writing a key into the Write Protection register, RTC_WPR.
  161. (#) To configure the RTC Calendar, user application should enter
  162. initialization mode. In this mode, the calendar counter is stopped
  163. and its value can be updated. When the initialization sequence is
  164. complete, the calendar restarts counting after 4 RTCCLK cycles.
  165. (#) To read the calendar through the shadow registers after Calendar
  166. initialization, calendar update or after wake-up from low power modes
  167. the software must first clear the RSF flag. The software must then
  168. wait until it is set again before reading the calendar, which means
  169. that the calendar registers have been correctly copied into the
  170. RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function
  171. implements the above software sequence (RSF clear and RSF check).
  172. @endverbatim
  173. * @{
  174. */
  175. /**
  176. * @brief Initializes the RTC peripheral
  177. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  178. * the configuration information for RTC.
  179. * @retval HAL status
  180. */
  181. HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
  182. {
  183. /* Check the RTC peripheral state */
  184. if(hrtc == NULL)
  185. {
  186. return HAL_ERROR;
  187. }
  188. /* Check the parameters */
  189. assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
  190. assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
  191. assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
  192. assert_param (IS_RTC_OUTPUT(hrtc->Init.OutPut));
  193. assert_param (IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
  194. assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
  195. if(hrtc->State == HAL_RTC_STATE_RESET)
  196. {
  197. /* Allocate lock resource and initialize it */
  198. hrtc->Lock = HAL_UNLOCKED;
  199. /* Initialize RTC MSP */
  200. HAL_RTC_MspInit(hrtc);
  201. }
  202. /* Set RTC state */
  203. hrtc->State = HAL_RTC_STATE_BUSY;
  204. /* Disable the write protection for RTC registers */
  205. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  206. /* Set Initialization mode */
  207. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  208. {
  209. /* Enable the write protection for RTC registers */
  210. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  211. /* Set RTC state */
  212. hrtc->State = HAL_RTC_STATE_ERROR;
  213. return HAL_ERROR;
  214. }
  215. else
  216. {
  217. /* Clear RTC_CR FMT, OSEL and POL Bits */
  218. hrtc->Instance->CR &= ((uint32_t)~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL));
  219. /* Set RTC_CR register */
  220. hrtc->Instance->CR |= (uint32_t)(hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity);
  221. /* Configure the RTC PRER */
  222. hrtc->Instance->PRER = (uint32_t)(hrtc->Init.SynchPrediv);
  223. hrtc->Instance->PRER |= (uint32_t)(hrtc->Init.AsynchPrediv << 16U);
  224. /* Exit Initialization mode */
  225. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  226. /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  227. if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
  228. {
  229. if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  230. {
  231. /* Enable the write protection for RTC registers */
  232. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  233. hrtc->State = HAL_RTC_STATE_ERROR;
  234. return HAL_ERROR;
  235. }
  236. }
  237. hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_ALARMOUTTYPE;
  238. hrtc->Instance->TAFCR |= (uint32_t)(hrtc->Init.OutPutType);
  239. /* Enable the write protection for RTC registers */
  240. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  241. /* Set RTC state */
  242. hrtc->State = HAL_RTC_STATE_READY;
  243. return HAL_OK;
  244. }
  245. }
  246. /**
  247. * @brief DeInitializes the RTC peripheral
  248. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  249. * the configuration information for RTC.
  250. * @note This function doesn't reset the RTC Backup Data registers.
  251. * @retval HAL status
  252. */
  253. HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
  254. {
  255. uint32_t tickstart = 0U;
  256. /* Set RTC state */
  257. hrtc->State = HAL_RTC_STATE_BUSY;
  258. /* Disable the write protection for RTC registers */
  259. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  260. /* Set Initialization mode */
  261. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  262. {
  263. /* Enable the write protection for RTC registers */
  264. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  265. /* Set RTC state */
  266. hrtc->State = HAL_RTC_STATE_ERROR;
  267. return HAL_ERROR;
  268. }
  269. else
  270. {
  271. /* Reset TR, DR and CR registers */
  272. hrtc->Instance->TR = 0x00000000U;
  273. hrtc->Instance->DR = 0x00002101U;
  274. /* Reset All CR bits except CR[2:0] */
  275. hrtc->Instance->CR &= 0x00000007U;
  276. /* Get tick */
  277. tickstart = HAL_GetTick();
  278. /* Wait till WUTWF flag is set and if Time out is reached exit */
  279. while(((hrtc->Instance->ISR) & RTC_ISR_WUTWF) == (uint32_t)RESET)
  280. {
  281. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  282. {
  283. /* Enable the write protection for RTC registers */
  284. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  285. /* Set RTC state */
  286. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  287. return HAL_TIMEOUT;
  288. }
  289. }
  290. /* Reset all RTC CR register bits */
  291. hrtc->Instance->CR &= 0x00000000U;
  292. hrtc->Instance->WUTR = 0x0000FFFFU;
  293. hrtc->Instance->PRER = 0x007F00FFU;
  294. hrtc->Instance->CALIBR = 0x00000000U;
  295. hrtc->Instance->ALRMAR = 0x00000000U;
  296. hrtc->Instance->ALRMBR = 0x00000000U;
  297. hrtc->Instance->SHIFTR = 0x00000000U;
  298. hrtc->Instance->CALR = 0x00000000U;
  299. hrtc->Instance->ALRMASSR = 0x00000000U;
  300. hrtc->Instance->ALRMBSSR = 0x00000000U;
  301. /* Reset ISR register and exit initialization mode */
  302. hrtc->Instance->ISR = 0x00000000U;
  303. /* Reset Tamper and alternate functions configuration register */
  304. hrtc->Instance->TAFCR = 0x00000000U;
  305. /* If RTC_CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  306. if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
  307. {
  308. if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  309. {
  310. /* Enable the write protection for RTC registers */
  311. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  312. hrtc->State = HAL_RTC_STATE_ERROR;
  313. return HAL_ERROR;
  314. }
  315. }
  316. }
  317. /* Enable the write protection for RTC registers */
  318. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  319. /* De-Initialize RTC MSP */
  320. HAL_RTC_MspDeInit(hrtc);
  321. hrtc->State = HAL_RTC_STATE_RESET;
  322. /* Release Lock */
  323. __HAL_UNLOCK(hrtc);
  324. return HAL_OK;
  325. }
  326. /**
  327. * @brief Initializes the RTC MSP.
  328. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  329. * the configuration information for RTC.
  330. * @retval None
  331. */
  332. __weak void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
  333. {
  334. /* Prevent unused argument(s) compilation warning */
  335. UNUSED(hrtc);
  336. /* NOTE : This function Should not be modified, when the callback is needed,
  337. the HAL_RTC_MspInit could be implemented in the user file
  338. */
  339. }
  340. /**
  341. * @brief DeInitializes the RTC MSP.
  342. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  343. * the configuration information for RTC.
  344. * @retval None
  345. */
  346. __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
  347. {
  348. /* Prevent unused argument(s) compilation warning */
  349. UNUSED(hrtc);
  350. /* NOTE : This function Should not be modified, when the callback is needed,
  351. the HAL_RTC_MspDeInit could be implemented in the user file
  352. */
  353. }
  354. /**
  355. * @}
  356. */
  357. /** @defgroup RTC_Exported_Functions_Group2 RTC Time and Date functions
  358. * @brief RTC Time and Date functions
  359. *
  360. @verbatim
  361. ===============================================================================
  362. ##### RTC Time and Date functions #####
  363. ===============================================================================
  364. [..] This section provides functions allowing to configure Time and Date features
  365. @endverbatim
  366. * @{
  367. */
  368. /**
  369. * @brief Sets RTC current time.
  370. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  371. * the configuration information for RTC.
  372. * @param sTime Pointer to Time structure
  373. * @param Format Specifies the format of the entered parameters.
  374. * This parameter can be one of the following values:
  375. * @arg RTC_FORMAT_BIN: Binary data format
  376. * @arg RTC_FORMAT_BCD: BCD data format
  377. * @retval HAL status
  378. */
  379. HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
  380. {
  381. uint32_t tmpreg = 0U;
  382. /* Check the parameters */
  383. assert_param(IS_RTC_FORMAT(Format));
  384. assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
  385. assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
  386. /* Process Locked */
  387. __HAL_LOCK(hrtc);
  388. hrtc->State = HAL_RTC_STATE_BUSY;
  389. if(Format == RTC_FORMAT_BIN)
  390. {
  391. if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
  392. {
  393. assert_param(IS_RTC_HOUR12(sTime->Hours));
  394. assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
  395. }
  396. else
  397. {
  398. sTime->TimeFormat = 0x00U;
  399. assert_param(IS_RTC_HOUR24(sTime->Hours));
  400. }
  401. assert_param(IS_RTC_MINUTES(sTime->Minutes));
  402. assert_param(IS_RTC_SECONDS(sTime->Seconds));
  403. tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << 16U) | \
  404. ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << 8U) | \
  405. ((uint32_t)RTC_ByteToBcd2(sTime->Seconds)) | \
  406. (((uint32_t)sTime->TimeFormat) << 16U));
  407. }
  408. else
  409. {
  410. if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
  411. {
  412. tmpreg = RTC_Bcd2ToByte(sTime->Hours);
  413. assert_param(IS_RTC_HOUR12(tmpreg));
  414. assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
  415. }
  416. else
  417. {
  418. sTime->TimeFormat = 0x00U;
  419. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
  420. }
  421. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
  422. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
  423. tmpreg = (((uint32_t)(sTime->Hours) << 16U) | \
  424. ((uint32_t)(sTime->Minutes) << 8U) | \
  425. ((uint32_t)sTime->Seconds) | \
  426. ((uint32_t)(sTime->TimeFormat) << 16U));
  427. }
  428. /* Disable the write protection for RTC registers */
  429. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  430. /* Set Initialization mode */
  431. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  432. {
  433. /* Enable the write protection for RTC registers */
  434. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  435. /* Set RTC state */
  436. hrtc->State = HAL_RTC_STATE_ERROR;
  437. /* Process Unlocked */
  438. __HAL_UNLOCK(hrtc);
  439. return HAL_ERROR;
  440. }
  441. else
  442. {
  443. /* Set the RTC_TR register */
  444. hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
  445. /* Clear the bits to be configured */
  446. hrtc->Instance->CR &= (uint32_t)~RTC_CR_BCK;
  447. /* Configure the RTC_CR register */
  448. hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
  449. /* Exit Initialization mode */
  450. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  451. /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  452. if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
  453. {
  454. if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  455. {
  456. /* Enable the write protection for RTC registers */
  457. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  458. hrtc->State = HAL_RTC_STATE_ERROR;
  459. /* Process Unlocked */
  460. __HAL_UNLOCK(hrtc);
  461. return HAL_ERROR;
  462. }
  463. }
  464. /* Enable the write protection for RTC registers */
  465. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  466. hrtc->State = HAL_RTC_STATE_READY;
  467. __HAL_UNLOCK(hrtc);
  468. return HAL_OK;
  469. }
  470. }
  471. /**
  472. * @brief Gets RTC current time.
  473. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  474. * the configuration information for RTC.
  475. * @param sTime Pointer to Time structure
  476. * @param Format Specifies the format of the entered parameters.
  477. * This parameter can be one of the following values:
  478. * @arg RTC_FORMAT_BIN: Binary data format
  479. * @arg RTC_FORMAT_BCD: BCD data format
  480. * @note You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds
  481. * value in second fraction ratio with time unit following generic formula:
  482. * Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit
  483. * This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
  484. * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
  485. * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
  486. * Reading RTC current time locks the values in calendar shadow registers until current date is read.
  487. * @retval HAL status
  488. */
  489. HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
  490. {
  491. uint32_t tmpreg = 0U;
  492. /* Check the parameters */
  493. assert_param(IS_RTC_FORMAT(Format));
  494. /* Get subseconds structure field from the corresponding register */
  495. sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR);
  496. /* Get SecondFraction structure field from the corresponding register field*/
  497. sTime->SecondFraction = (uint32_t)(hrtc->Instance->PRER & RTC_PRER_PREDIV_S);
  498. /* Get the TR register */
  499. tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);
  500. /* Fill the structure fields with the read parameters */
  501. sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16U);
  502. sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> 8U);
  503. sTime->Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
  504. sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16U);
  505. /* Check the input parameters format */
  506. if(Format == RTC_FORMAT_BIN)
  507. {
  508. /* Convert the time structure parameters to Binary format */
  509. sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
  510. sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
  511. sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
  512. }
  513. return HAL_OK;
  514. }
  515. /**
  516. * @brief Sets RTC current date.
  517. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  518. * the configuration information for RTC.
  519. * @param sDate Pointer to date structure
  520. * @param Format specifies the format of the entered parameters.
  521. * This parameter can be one of the following values:
  522. * @arg RTC_FORMAT_BIN: Binary data format
  523. * @arg RTC_FORMAT_BCD: BCD data format
  524. * @retval HAL status
  525. */
  526. HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
  527. {
  528. uint32_t datetmpreg = 0U;
  529. /* Check the parameters */
  530. assert_param(IS_RTC_FORMAT(Format));
  531. /* Process Locked */
  532. __HAL_LOCK(hrtc);
  533. hrtc->State = HAL_RTC_STATE_BUSY;
  534. if((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U))
  535. {
  536. sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU);
  537. }
  538. assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
  539. if(Format == RTC_FORMAT_BIN)
  540. {
  541. assert_param(IS_RTC_YEAR(sDate->Year));
  542. assert_param(IS_RTC_MONTH(sDate->Month));
  543. assert_param(IS_RTC_DATE(sDate->Date));
  544. datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << 16U) | \
  545. ((uint32_t)RTC_ByteToBcd2(sDate->Month) << 8U) | \
  546. ((uint32_t)RTC_ByteToBcd2(sDate->Date)) | \
  547. ((uint32_t)sDate->WeekDay << 13U));
  548. }
  549. else
  550. {
  551. assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
  552. assert_param(IS_RTC_MONTH(datetmpreg));
  553. assert_param(IS_RTC_DATE(datetmpreg));
  554. datetmpreg = ((((uint32_t)sDate->Year) << 16U) | \
  555. (((uint32_t)sDate->Month) << 8U) | \
  556. ((uint32_t)sDate->Date) | \
  557. (((uint32_t)sDate->WeekDay) << 13U));
  558. }
  559. /* Disable the write protection for RTC registers */
  560. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  561. /* Set Initialization mode */
  562. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  563. {
  564. /* Enable the write protection for RTC registers */
  565. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  566. /* Set RTC state*/
  567. hrtc->State = HAL_RTC_STATE_ERROR;
  568. /* Process Unlocked */
  569. __HAL_UNLOCK(hrtc);
  570. return HAL_ERROR;
  571. }
  572. else
  573. {
  574. /* Set the RTC_DR register */
  575. hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
  576. /* Exit Initialization mode */
  577. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  578. /* If CR_BYPSHAD bit = 0, wait for synchro else this check is not needed */
  579. if((hrtc->Instance->CR & RTC_CR_BYPSHAD) == RESET)
  580. {
  581. if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  582. {
  583. /* Enable the write protection for RTC registers */
  584. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  585. hrtc->State = HAL_RTC_STATE_ERROR;
  586. /* Process Unlocked */
  587. __HAL_UNLOCK(hrtc);
  588. return HAL_ERROR;
  589. }
  590. }
  591. /* Enable the write protection for RTC registers */
  592. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  593. hrtc->State = HAL_RTC_STATE_READY ;
  594. /* Process Unlocked */
  595. __HAL_UNLOCK(hrtc);
  596. return HAL_OK;
  597. }
  598. }
  599. /**
  600. * @brief Gets RTC current date.
  601. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  602. * the configuration information for RTC.
  603. * @param sDate Pointer to Date structure
  604. * @param Format Specifies the format of the entered parameters.
  605. * This parameter can be one of the following values:
  606. * @arg RTC_FORMAT_BIN: Binary data format
  607. * @arg RTC_FORMAT_BCD: BCD data format
  608. * @note You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values
  609. * in the higher-order calendar shadow registers to ensure consistency between the time and date values.
  610. * Reading RTC current time locks the values in calendar shadow registers until Current date is read.
  611. * @retval HAL status
  612. */
  613. HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
  614. {
  615. uint32_t datetmpreg = 0U;
  616. /* Check the parameters */
  617. assert_param(IS_RTC_FORMAT(Format));
  618. /* Get the DR register */
  619. datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
  620. /* Fill the structure fields with the read parameters */
  621. sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16U);
  622. sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8U);
  623. sDate->Date = (uint8_t)(datetmpreg & (RTC_DR_DT | RTC_DR_DU));
  624. sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> 13U);
  625. /* Check the input parameters format */
  626. if(Format == RTC_FORMAT_BIN)
  627. {
  628. /* Convert the date structure parameters to Binary format */
  629. sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
  630. sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
  631. sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
  632. }
  633. return HAL_OK;
  634. }
  635. /**
  636. * @}
  637. */
  638. /** @defgroup RTC_Exported_Functions_Group3 RTC Alarm functions
  639. * @brief RTC Alarm functions
  640. *
  641. @verbatim
  642. ===============================================================================
  643. ##### RTC Alarm functions #####
  644. ===============================================================================
  645. [..] This section provides functions allowing to configure Alarm feature
  646. @endverbatim
  647. * @{
  648. */
  649. /**
  650. * @brief Sets the specified RTC Alarm.
  651. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  652. * the configuration information for RTC.
  653. * @param sAlarm Pointer to Alarm structure
  654. * @param Format Specifies the format of the entered parameters.
  655. * This parameter can be one of the following values:
  656. * @arg RTC_FORMAT_BIN: Binary data format
  657. * @arg RTC_FORMAT_BCD: BCD data format
  658. * @retval HAL status
  659. */
  660. HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
  661. {
  662. uint32_t tickstart = 0U;
  663. uint32_t tmpreg = 0U, subsecondtmpreg = 0U;
  664. /* Check the parameters */
  665. assert_param(IS_RTC_FORMAT(Format));
  666. assert_param(IS_RTC_ALARM(sAlarm->Alarm));
  667. assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
  668. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
  669. assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
  670. assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
  671. /* Process Locked */
  672. __HAL_LOCK(hrtc);
  673. hrtc->State = HAL_RTC_STATE_BUSY;
  674. if(Format == RTC_FORMAT_BIN)
  675. {
  676. if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
  677. {
  678. assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
  679. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  680. }
  681. else
  682. {
  683. sAlarm->AlarmTime.TimeFormat = 0x00U;
  684. assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
  685. }
  686. assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
  687. assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
  688. if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  689. {
  690. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
  691. }
  692. else
  693. {
  694. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
  695. }
  696. tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16U) | \
  697. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8U) | \
  698. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
  699. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \
  700. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24U) | \
  701. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  702. ((uint32_t)sAlarm->AlarmMask));
  703. }
  704. else
  705. {
  706. if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
  707. {
  708. tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
  709. assert_param(IS_RTC_HOUR12(tmpreg));
  710. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  711. }
  712. else
  713. {
  714. sAlarm->AlarmTime.TimeFormat = 0x00U;
  715. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  716. }
  717. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
  718. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
  719. if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  720. {
  721. tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
  722. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
  723. }
  724. else
  725. {
  726. tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
  727. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
  728. }
  729. tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16U) | \
  730. ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8U) | \
  731. ((uint32_t) sAlarm->AlarmTime.Seconds) | \
  732. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \
  733. ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24U) | \
  734. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  735. ((uint32_t)sAlarm->AlarmMask));
  736. }
  737. /* Configure the Alarm A or Alarm B Sub Second registers */
  738. subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
  739. /* Disable the write protection for RTC registers */
  740. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  741. /* Configure the Alarm register */
  742. if(sAlarm->Alarm == RTC_ALARM_A)
  743. {
  744. /* Disable the Alarm A interrupt */
  745. __HAL_RTC_ALARMA_DISABLE(hrtc);
  746. /* In case of interrupt mode is used, the interrupt source must disabled */
  747. __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
  748. /* Get tick */
  749. tickstart = HAL_GetTick();
  750. /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
  751. while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
  752. {
  753. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  754. {
  755. /* Enable the write protection for RTC registers */
  756. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  757. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  758. /* Process Unlocked */
  759. __HAL_UNLOCK(hrtc);
  760. return HAL_TIMEOUT;
  761. }
  762. }
  763. hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
  764. /* Configure the Alarm A Sub Second register */
  765. hrtc->Instance->ALRMASSR = subsecondtmpreg;
  766. /* Configure the Alarm state: Enable Alarm */
  767. __HAL_RTC_ALARMA_ENABLE(hrtc);
  768. }
  769. else
  770. {
  771. /* Disable the Alarm B interrupt */
  772. __HAL_RTC_ALARMB_DISABLE(hrtc);
  773. /* In case of interrupt mode is used, the interrupt source must disabled */
  774. __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
  775. /* Get tick */
  776. tickstart = HAL_GetTick();
  777. /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
  778. while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
  779. {
  780. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  781. {
  782. /* Enable the write protection for RTC registers */
  783. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  784. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  785. /* Process Unlocked */
  786. __HAL_UNLOCK(hrtc);
  787. return HAL_TIMEOUT;
  788. }
  789. }
  790. hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
  791. /* Configure the Alarm B Sub Second register */
  792. hrtc->Instance->ALRMBSSR = subsecondtmpreg;
  793. /* Configure the Alarm state: Enable Alarm */
  794. __HAL_RTC_ALARMB_ENABLE(hrtc);
  795. }
  796. /* Enable the write protection for RTC registers */
  797. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  798. /* Change RTC state */
  799. hrtc->State = HAL_RTC_STATE_READY;
  800. /* Process Unlocked */
  801. __HAL_UNLOCK(hrtc);
  802. return HAL_OK;
  803. }
  804. /**
  805. * @brief Sets the specified RTC Alarm with Interrupt
  806. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  807. * the configuration information for RTC.
  808. * @param sAlarm Pointer to Alarm structure
  809. * @param Format Specifies the format of the entered parameters.
  810. * This parameter can be one of the following values:
  811. * @arg RTC_FORMAT_BIN: Binary data format
  812. * @arg RTC_FORMAT_BCD: BCD data format
  813. * @retval HAL status
  814. */
  815. HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
  816. {
  817. uint32_t tmpreg = 0U, subsecondtmpreg = 0U;
  818. __IO uint32_t count = RTC_TIMEOUT_VALUE * (SystemCoreClock / 32U / 1000U) ;
  819. /* Check the parameters */
  820. assert_param(IS_RTC_FORMAT(Format));
  821. assert_param(IS_RTC_ALARM(sAlarm->Alarm));
  822. assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
  823. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
  824. assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
  825. assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
  826. /* Process Locked */
  827. __HAL_LOCK(hrtc);
  828. hrtc->State = HAL_RTC_STATE_BUSY;
  829. if(Format == RTC_FORMAT_BIN)
  830. {
  831. if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
  832. {
  833. assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
  834. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  835. }
  836. else
  837. {
  838. sAlarm->AlarmTime.TimeFormat = 0x00U;
  839. assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
  840. }
  841. assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
  842. assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
  843. if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  844. {
  845. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
  846. }
  847. else
  848. {
  849. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
  850. }
  851. tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << 16U) | \
  852. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << 8U) | \
  853. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds)) | \
  854. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \
  855. ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << 24U) | \
  856. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  857. ((uint32_t)sAlarm->AlarmMask));
  858. }
  859. else
  860. {
  861. if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
  862. {
  863. tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
  864. assert_param(IS_RTC_HOUR12(tmpreg));
  865. assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
  866. }
  867. else
  868. {
  869. sAlarm->AlarmTime.TimeFormat = 0x00U;
  870. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  871. }
  872. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
  873. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
  874. if(sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
  875. {
  876. tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
  877. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(tmpreg));
  878. }
  879. else
  880. {
  881. tmpreg = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
  882. assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(tmpreg));
  883. }
  884. tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << 16U) | \
  885. ((uint32_t)(sAlarm->AlarmTime.Minutes) << 8U) | \
  886. ((uint32_t) sAlarm->AlarmTime.Seconds) | \
  887. ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << 16U) | \
  888. ((uint32_t)(sAlarm->AlarmDateWeekDay) << 24U) | \
  889. ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
  890. ((uint32_t)sAlarm->AlarmMask));
  891. }
  892. /* Configure the Alarm A or Alarm B Sub Second registers */
  893. subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
  894. /* Disable the write protection for RTC registers */
  895. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  896. /* Configure the Alarm register */
  897. if(sAlarm->Alarm == RTC_ALARM_A)
  898. {
  899. /* Disable the Alarm A interrupt */
  900. __HAL_RTC_ALARMA_DISABLE(hrtc);
  901. /* Clear flag alarm A */
  902. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
  903. /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
  904. do
  905. {
  906. if (count-- == 0U)
  907. {
  908. /* Enable the write protection for RTC registers */
  909. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  910. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  911. /* Process Unlocked */
  912. __HAL_UNLOCK(hrtc);
  913. return HAL_TIMEOUT;
  914. }
  915. }
  916. while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET);
  917. hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
  918. /* Configure the Alarm A Sub Second register */
  919. hrtc->Instance->ALRMASSR = subsecondtmpreg;
  920. /* Configure the Alarm state: Enable Alarm */
  921. __HAL_RTC_ALARMA_ENABLE(hrtc);
  922. /* Configure the Alarm interrupt */
  923. __HAL_RTC_ALARM_ENABLE_IT(hrtc,RTC_IT_ALRA);
  924. }
  925. else
  926. {
  927. /* Disable the Alarm B interrupt */
  928. __HAL_RTC_ALARMB_DISABLE(hrtc);
  929. /* Clear flag alarm B */
  930. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
  931. /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
  932. do
  933. {
  934. if (count-- == 0U)
  935. {
  936. /* Enable the write protection for RTC registers */
  937. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  938. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  939. /* Process Unlocked */
  940. __HAL_UNLOCK(hrtc);
  941. return HAL_TIMEOUT;
  942. }
  943. }
  944. while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET);
  945. hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
  946. /* Configure the Alarm B Sub Second register */
  947. hrtc->Instance->ALRMBSSR = subsecondtmpreg;
  948. /* Configure the Alarm state: Enable Alarm */
  949. __HAL_RTC_ALARMB_ENABLE(hrtc);
  950. /* Configure the Alarm interrupt */
  951. __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRB);
  952. }
  953. /* RTC Alarm Interrupt Configuration: EXTI configuration */
  954. __HAL_RTC_ALARM_EXTI_ENABLE_IT();
  955. EXTI->RTSR |= RTC_EXTI_LINE_ALARM_EVENT;
  956. /* Enable the write protection for RTC registers */
  957. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  958. hrtc->State = HAL_RTC_STATE_READY;
  959. /* Process Unlocked */
  960. __HAL_UNLOCK(hrtc);
  961. return HAL_OK;
  962. }
  963. /**
  964. * @brief Deactivate the specified RTC Alarm
  965. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  966. * the configuration information for RTC.
  967. * @param Alarm Specifies the Alarm.
  968. * This parameter can be one of the following values:
  969. * @arg RTC_ALARM_A: AlarmA
  970. * @arg RTC_ALARM_B: AlarmB
  971. * @retval HAL status
  972. */
  973. HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
  974. {
  975. uint32_t tickstart = 0U;
  976. /* Check the parameters */
  977. assert_param(IS_RTC_ALARM(Alarm));
  978. /* Process Locked */
  979. __HAL_LOCK(hrtc);
  980. hrtc->State = HAL_RTC_STATE_BUSY;
  981. /* Disable the write protection for RTC registers */
  982. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  983. if(Alarm == RTC_ALARM_A)
  984. {
  985. /* AlarmA */
  986. __HAL_RTC_ALARMA_DISABLE(hrtc);
  987. /* In case of interrupt mode is used, the interrupt source must disabled */
  988. __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
  989. /* Get tick */
  990. tickstart = HAL_GetTick();
  991. /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
  992. while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
  993. {
  994. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  995. {
  996. /* Enable the write protection for RTC registers */
  997. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  998. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  999. /* Process Unlocked */
  1000. __HAL_UNLOCK(hrtc);
  1001. return HAL_TIMEOUT;
  1002. }
  1003. }
  1004. }
  1005. else
  1006. {
  1007. /* AlarmB */
  1008. __HAL_RTC_ALARMB_DISABLE(hrtc);
  1009. /* In case of interrupt mode is used, the interrupt source must disabled */
  1010. __HAL_RTC_ALARM_DISABLE_IT(hrtc,RTC_IT_ALRB);
  1011. /* Get tick */
  1012. tickstart = HAL_GetTick();
  1013. /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
  1014. while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
  1015. {
  1016. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  1017. {
  1018. /* Enable the write protection for RTC registers */
  1019. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1020. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1021. /* Process Unlocked */
  1022. __HAL_UNLOCK(hrtc);
  1023. return HAL_TIMEOUT;
  1024. }
  1025. }
  1026. }
  1027. /* Enable the write protection for RTC registers */
  1028. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1029. hrtc->State = HAL_RTC_STATE_READY;
  1030. /* Process Unlocked */
  1031. __HAL_UNLOCK(hrtc);
  1032. return HAL_OK;
  1033. }
  1034. /**
  1035. * @brief Gets the RTC Alarm value and masks.
  1036. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1037. * the configuration information for RTC.
  1038. * @param sAlarm Pointer to Date structure
  1039. * @param Alarm Specifies the Alarm.
  1040. * This parameter can be one of the following values:
  1041. * @arg RTC_ALARM_A: AlarmA
  1042. * @arg RTC_ALARM_B: AlarmB
  1043. * @param Format Specifies the format of the entered parameters.
  1044. * This parameter can be one of the following values:
  1045. * @arg RTC_FORMAT_BIN: Binary data format
  1046. * @arg RTC_FORMAT_BCD: BCD data format
  1047. * @retval HAL status
  1048. */
  1049. HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
  1050. {
  1051. uint32_t tmpreg = 0U, subsecondtmpreg = 0U;
  1052. /* Check the parameters */
  1053. assert_param(IS_RTC_FORMAT(Format));
  1054. assert_param(IS_RTC_ALARM(Alarm));
  1055. if(Alarm == RTC_ALARM_A)
  1056. {
  1057. /* AlarmA */
  1058. sAlarm->Alarm = RTC_ALARM_A;
  1059. tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
  1060. subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR ) & RTC_ALRMASSR_SS);
  1061. }
  1062. else
  1063. {
  1064. sAlarm->Alarm = RTC_ALARM_B;
  1065. tmpreg = (uint32_t)(hrtc->Instance->ALRMBR);
  1066. subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS);
  1067. }
  1068. /* Fill the structure with the read parameters */
  1069. sAlarm->AlarmTime.Hours = (uint32_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> 16U);
  1070. sAlarm->AlarmTime.Minutes = (uint32_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> 8U);
  1071. sAlarm->AlarmTime.Seconds = (uint32_t)(tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU));
  1072. sAlarm->AlarmTime.TimeFormat = (uint32_t)((tmpreg & RTC_ALRMAR_PM) >> 16U);
  1073. sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
  1074. sAlarm->AlarmDateWeekDay = (uint32_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> 24U);
  1075. sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
  1076. sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
  1077. if(Format == RTC_FORMAT_BIN)
  1078. {
  1079. sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
  1080. sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
  1081. sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
  1082. sAlarm->AlarmDateWeekDay = RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay);
  1083. }
  1084. return HAL_OK;
  1085. }
  1086. /**
  1087. * @brief This function handles Alarm interrupt request.
  1088. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1089. * the configuration information for RTC.
  1090. * @retval None
  1091. */
  1092. void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef* hrtc)
  1093. {
  1094. if(__HAL_RTC_ALARM_GET_IT(hrtc, RTC_IT_ALRA))
  1095. {
  1096. /* Get the status of the Interrupt */
  1097. if((uint32_t)(hrtc->Instance->CR & RTC_IT_ALRA) != (uint32_t)RESET)
  1098. {
  1099. /* AlarmA callback */
  1100. HAL_RTC_AlarmAEventCallback(hrtc);
  1101. /* Clear the Alarm interrupt pending bit */
  1102. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRAF);
  1103. }
  1104. }
  1105. if(__HAL_RTC_ALARM_GET_IT(hrtc, RTC_IT_ALRB))
  1106. {
  1107. /* Get the status of the Interrupt */
  1108. if((uint32_t)(hrtc->Instance->CR & RTC_IT_ALRB) != (uint32_t)RESET)
  1109. {
  1110. /* AlarmB callback */
  1111. HAL_RTCEx_AlarmBEventCallback(hrtc);
  1112. /* Clear the Alarm interrupt pending bit */
  1113. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRBF);
  1114. }
  1115. }
  1116. /* Clear the EXTI's line Flag for RTC Alarm */
  1117. __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
  1118. /* Change RTC state */
  1119. hrtc->State = HAL_RTC_STATE_READY;
  1120. }
  1121. /**
  1122. * @brief Alarm A callback.
  1123. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1124. * the configuration information for RTC.
  1125. * @retval None
  1126. */
  1127. __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
  1128. {
  1129. /* Prevent unused argument(s) compilation warning */
  1130. UNUSED(hrtc);
  1131. /* NOTE : This function Should not be modified, when the callback is needed,
  1132. the HAL_RTC_AlarmAEventCallback could be implemented in the user file
  1133. */
  1134. }
  1135. /**
  1136. * @brief This function handles AlarmA Polling request.
  1137. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1138. * the configuration information for RTC.
  1139. * @param Timeout Timeout duration
  1140. * @retval HAL status
  1141. */
  1142. HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  1143. {
  1144. uint32_t tickstart = 0U;
  1145. /* Get tick */
  1146. tickstart = HAL_GetTick();
  1147. while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == RESET)
  1148. {
  1149. if(Timeout != HAL_MAX_DELAY)
  1150. {
  1151. if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))
  1152. {
  1153. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1154. return HAL_TIMEOUT;
  1155. }
  1156. }
  1157. }
  1158. /* Clear the Alarm interrupt pending bit */
  1159. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
  1160. /* Change RTC state */
  1161. hrtc->State = HAL_RTC_STATE_READY;
  1162. return HAL_OK;
  1163. }
  1164. /**
  1165. * @}
  1166. */
  1167. /** @defgroup RTC_Exported_Functions_Group4 Peripheral Control functions
  1168. * @brief Peripheral Control functions
  1169. *
  1170. @verbatim
  1171. ===============================================================================
  1172. ##### Peripheral Control functions #####
  1173. ===============================================================================
  1174. [..]
  1175. This subsection provides functions allowing to
  1176. (+) Wait for RTC Time and Date Synchronization
  1177. @endverbatim
  1178. * @{
  1179. */
  1180. /**
  1181. * @brief Waits until the RTC Time and Date registers (RTC_TR and RTC_DR) are
  1182. * synchronized with RTC APB clock.
  1183. * @note The RTC Resynchronization mode is write protected, use the
  1184. * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
  1185. * @note To read the calendar through the shadow registers after Calendar
  1186. * initialization, calendar update or after wake-up from low power modes
  1187. * the software must first clear the RSF flag.
  1188. * The software must then wait until it is set again before reading
  1189. * the calendar, which means that the calendar registers have been
  1190. * correctly copied into the RTC_TR and RTC_DR shadow registers.
  1191. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1192. * the configuration information for RTC.
  1193. * @retval HAL status
  1194. */
  1195. HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef* hrtc)
  1196. {
  1197. uint32_t tickstart = 0U;
  1198. /* Clear RSF flag */
  1199. hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK;
  1200. /* Get tick */
  1201. tickstart = HAL_GetTick();
  1202. /* Wait the registers to be synchronised */
  1203. while((hrtc->Instance->ISR & RTC_ISR_RSF) == (uint32_t)RESET)
  1204. {
  1205. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  1206. {
  1207. return HAL_TIMEOUT;
  1208. }
  1209. }
  1210. return HAL_OK;
  1211. }
  1212. /**
  1213. * @}
  1214. */
  1215. /** @defgroup RTC_Exported_Functions_Group5 Peripheral State functions
  1216. * @brief Peripheral State functions
  1217. *
  1218. @verbatim
  1219. ===============================================================================
  1220. ##### Peripheral State functions #####
  1221. ===============================================================================
  1222. [..]
  1223. This subsection provides functions allowing to
  1224. (+) Get RTC state
  1225. @endverbatim
  1226. * @{
  1227. */
  1228. /**
  1229. * @brief Returns the RTC state.
  1230. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1231. * the configuration information for RTC.
  1232. * @retval HAL state
  1233. */
  1234. HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef* hrtc)
  1235. {
  1236. return hrtc->State;
  1237. }
  1238. /**
  1239. * @}
  1240. */
  1241. /**
  1242. * @brief Enters the RTC Initialization mode.
  1243. * @note The RTC Initialization mode is write protected, use the
  1244. * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
  1245. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1246. * the configuration information for RTC.
  1247. * @retval HAL status
  1248. */
  1249. HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef* hrtc)
  1250. {
  1251. uint32_t tickstart = 0U;
  1252. /* Check if the Initialization mode is set */
  1253. if((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
  1254. {
  1255. /* Set the Initialization mode */
  1256. hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
  1257. /* Get tick */
  1258. tickstart = HAL_GetTick();
  1259. /* Wait till RTC is in INIT state and if Time out is reached exit */
  1260. while((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
  1261. {
  1262. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  1263. {
  1264. return HAL_TIMEOUT;
  1265. }
  1266. }
  1267. }
  1268. return HAL_OK;
  1269. }
  1270. /**
  1271. * @brief Converts a 2 digit decimal to BCD format.
  1272. * @param Value Byte to be converted
  1273. * @retval Converted byte
  1274. */
  1275. uint8_t RTC_ByteToBcd2(uint8_t Value)
  1276. {
  1277. uint32_t bcdhigh = 0U;
  1278. while(Value >= 10U)
  1279. {
  1280. bcdhigh++;
  1281. Value -= 10U;
  1282. }
  1283. return ((uint8_t)(bcdhigh << 4U) | Value);
  1284. }
  1285. /**
  1286. * @brief Converts from 2 digit BCD to Binary.
  1287. * @param Value BCD value to be converted
  1288. * @retval Converted word
  1289. */
  1290. uint8_t RTC_Bcd2ToByte(uint8_t Value)
  1291. {
  1292. uint32_t tmp = 0U;
  1293. tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
  1294. return (tmp + (Value & (uint8_t)0x0F));
  1295. }
  1296. /**
  1297. * @}
  1298. */
  1299. #endif /* HAL_RTC_MODULE_ENABLED */
  1300. /**
  1301. * @}
  1302. */
  1303. /**
  1304. * @}
  1305. */
  1306. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/