stm32f7xx_hal_rtc.c 53 KB

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