stm32l4xx_hal_rtc.c 51 KB

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