stm32f4xx_hal_rtc.c 53 KB

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