stm32l4xx_hal_rtc.c 52 KB

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