stm32l1xx_hal_rtc.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_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) and the RTC backup registers can be powered
  19. from the VBAT voltage when the main VDD supply is powered off.
  20. To retain the content of the RTC backup registers and supply the RTC
  21. when VDD is turned off, VBAT pin can be connected to an optional
  22. standby voltage supplied by a battery or by another source.
  23. [..] To allow the RTC operating even when the main digital supply (VDD) is turned
  24. off, the VBAT pin powers the following blocks:
  25. (#) The RTC
  26. (#) The LSE oscillator
  27. (#) PC13 to PC15 I/Os (when available)
  28. [..] When the backup domain is supplied by VDD (analog switch connected to VDD),
  29. the following pins are available:
  30. (#) PC14 and PC15 can be used as either GPIO or LSE pins
  31. (#) PC13 can be used as a GPIO or as the RTC_AF1 pin
  32. [..] When the backup domain is supplied by VBAT (analog switch connected to VBAT
  33. because VDD is not present), the following pins are available:
  34. (#) PC14 and PC15 can be used as LSE pins only
  35. (#) PC13 can be used as the RTC_AF1 pin
  36. ##### Backup Domain Reset #####
  37. ==================================================================
  38. [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
  39. to their reset values.
  40. [..] A backup domain reset is generated when one of the following events occurs:
  41. (#) Software reset, triggered by setting the BDRST bit in the
  42. RCC Backup domain control register (RCC_BDCR).
  43. (#) VDD or VBAT power on, if both supplies have previously been powered off.
  44. ##### Backup Domain Access #####
  45. ==================================================================
  46. [..] After reset, the backup domain (RTC registers, RTC backup data
  47. registers and backup SRAM) is protected against possible unwanted write
  48. accesses.
  49. [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
  50. (+) Enable the Power Controller (PWR) APB1 interface clock using the
  51. __HAL_RCC_PWR_CLK_ENABLE() function.
  52. (+) Enable access to RTC domain using the HAL_PWR_EnableBkUpAccess() function.
  53. (+) Select the RTC clock source using the __HAL_RCC_RTC_CONFIG() function.
  54. (+) Enable RTC Clock using the __HAL_RCC_RTC_ENABLE() function.
  55. ##### How to use this driver #####
  56. ==================================================================
  57. [..]
  58. (+) Enable the RTC domain access (see description in the section above).
  59. (+) Configure the RTC Prescaler (Asynchronous and Synchronous) and RTC hour
  60. format using the HAL_RTC_Init() function.
  61. *** Time and Date configuration ***
  62. ===================================
  63. [..]
  64. (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
  65. and HAL_RTC_SetDate() functions.
  66. (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
  67. *** Alarm configuration ***
  68. ===========================
  69. [..]
  70. (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
  71. You can also configure the RTC Alarm with interrupt mode using the HAL_RTC_SetAlarm_IT() function.
  72. (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
  73. ##### RTC and low power modes #####
  74. ==================================================================
  75. [..] The MCU can be woken up from a low power mode by an RTC alternate
  76. function.
  77. [..] The RTC alternate functions are the RTC alarms (Alarm A and Alarm B),
  78. RTC wakeup, RTC tamper event detection and RTC time stamp event detection.
  79. These RTC alternate functions can wake up the system from the Stop and
  80. Standby low power modes.
  81. [..] The system can also wake up from low power modes without depending
  82. on an external interrupt (Auto-wakeup mode), by using the RTC alarm
  83. or the RTC wakeup events.
  84. [..] The RTC provides a programmable time base for waking up from the
  85. Stop or Standby mode at regular intervals.
  86. Wakeup from STOP and STANDBY modes is possible only when the RTC clock source
  87. is LSE or LSI.
  88. @endverbatim
  89. ******************************************************************************
  90. * @attention
  91. *
  92. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  93. *
  94. * Redistribution and use in source and binary forms, with or without modification,
  95. * are permitted provided that the following conditions are met:
  96. * 1. Redistributions of source code must retain the above copyright notice,
  97. * this list of conditions and the following disclaimer.
  98. * 2. Redistributions in binary form must reproduce the above copyright notice,
  99. * this list of conditions and the following disclaimer in the documentation
  100. * and/or other materials provided with the distribution.
  101. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  102. * may be used to endorse or promote products derived from this software
  103. * without specific prior written permission.
  104. *
  105. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  106. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  107. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  108. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  109. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  110. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  111. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  112. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  113. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  114. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  115. *
  116. ******************************************************************************
  117. */
  118. /* Includes ------------------------------------------------------------------*/
  119. #include "stm32l1xx_hal.h"
  120. /** @addtogroup STM32L1xx_HAL_Driver
  121. * @{
  122. */
  123. /** @defgroup RTC RTC
  124. * @brief RTC HAL module driver
  125. * @{
  126. */
  127. #ifdef HAL_RTC_MODULE_ENABLED
  128. /* Private typedef -----------------------------------------------------------*/
  129. /* Private define ------------------------------------------------------------*/
  130. /* Private macro -------------------------------------------------------------*/
  131. /* Private variables ---------------------------------------------------------*/
  132. /* Private function prototypes -----------------------------------------------*/
  133. /* Private functions ---------------------------------------------------------*/
  134. /** @defgroup RTC_Exported_Functions RTC Exported Functions
  135. * @{
  136. */
  137. /** @defgroup RTC_Exported_Functions_Group1 Initialization and de-initialization functions
  138. * @brief Initialization and Configuration functions
  139. *
  140. @verbatim
  141. ===============================================================================
  142. ##### Initialization and de-initialization functions #####
  143. ===============================================================================
  144. [..] This section provides functions allowing to initialize and configure the
  145. RTC Prescaler (Synchronous and Asynchronous), RTC Hour format, disable
  146. RTC registers Write protection, enter and exit the RTC initialization mode,
  147. RTC registers synchronization check and reference clock detection enable.
  148. (#) The RTC Prescaler is programmed to generate the RTC 1Hz time base.
  149. It is split into 2 programmable prescalers to minimize power consumption.
  150. (++) A 7-bit asynchronous prescaler and a 13-bit synchronous prescaler.
  151. (++) When both prescalers are used, it is recommended to configure the
  152. asynchronous prescaler to a high value to minimize power consumption.
  153. (#) All RTC registers are Write protected. Writing to the RTC registers
  154. is enabled by writing a key into the Write Protection register, RTC_WPR.
  155. (#) To configure the RTC Calendar, user application should enter
  156. initialization mode. In this mode, the calendar counter is stopped
  157. and its value can be updated. When the initialization sequence is
  158. complete, the calendar restarts counting after 4 RTCCLK cycles.
  159. (#) To read the calendar through the shadow registers after Calendar
  160. initialization, calendar update or after wakeup from low power modes
  161. the software must first clear the RSF flag. The software must then
  162. wait until it is set again before reading the calendar, which means
  163. that the calendar registers have been correctly copied into the
  164. RTC_TR and RTC_DR shadow registers.The HAL_RTC_WaitForSynchro() function
  165. implements the above software sequence (RSF clear and RSF check).
  166. @endverbatim
  167. * @{
  168. */
  169. /**
  170. * @brief Initializes the RTC peripheral
  171. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  172. * the configuration information for RTC.
  173. * @retval HAL status
  174. */
  175. HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
  176. {
  177. /* Check the RTC peripheral state */
  178. if(hrtc == NULL)
  179. {
  180. return HAL_ERROR;
  181. }
  182. /* Check the parameters */
  183. assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
  184. assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
  185. assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
  186. assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
  187. assert_param(IS_RTC_OUTPUT(hrtc->Init.OutPut));
  188. assert_param(IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
  189. assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
  190. if(hrtc->State == HAL_RTC_STATE_RESET)
  191. {
  192. /* Allocate lock resource and initialize it */
  193. hrtc->Lock = HAL_UNLOCKED;
  194. /* Initialize RTC MSP */
  195. HAL_RTC_MspInit(hrtc);
  196. }
  197. /* Set RTC state */
  198. hrtc->State = HAL_RTC_STATE_BUSY;
  199. /* Disable the write protection for RTC registers */
  200. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  201. /* Set Initialization mode */
  202. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  203. {
  204. /* Enable the write protection for RTC registers */
  205. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  206. /* Set RTC state */
  207. hrtc->State = HAL_RTC_STATE_ERROR;
  208. return HAL_ERROR;
  209. }
  210. else
  211. {
  212. /* Clear RTC_CR FMT, OSEL and POL Bits */
  213. hrtc->Instance->CR &= ((uint32_t)~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL));
  214. /* Set RTC_CR register */
  215. hrtc->Instance->CR |= (uint32_t)(hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity);
  216. /* Configure the RTC PRER */
  217. hrtc->Instance->PRER = (uint32_t)(hrtc->Init.SynchPrediv);
  218. hrtc->Instance->PRER |= (uint32_t)(hrtc->Init.AsynchPrediv << 16);
  219. /* Exit Initialization mode */
  220. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  221. hrtc->Instance->TAFCR &= (uint32_t)~RTC_TAFCR_ALARMOUTTYPE;
  222. hrtc->Instance->TAFCR |= (uint32_t)(hrtc->Init.OutPutType);
  223. /* Enable the write protection for RTC registers */
  224. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  225. /* Set RTC state */
  226. hrtc->State = HAL_RTC_STATE_READY;
  227. return HAL_OK;
  228. }
  229. }
  230. /**
  231. * @brief DeInitializes the RTC peripheral
  232. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  233. * the configuration information for RTC.
  234. * @note This function doesn't reset the RTC Backup Data registers.
  235. * @retval HAL status
  236. */
  237. __weak HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
  238. {
  239. /* Prevent unused argument(s) compilation warning */
  240. UNUSED(hrtc);
  241. /* Note : This function is defined into this file for library reference. */
  242. /* Function content is located into file stm32l1xx_hal_rtc_ex.c */
  243. /* Return function status */
  244. return HAL_ERROR;
  245. }
  246. /**
  247. * @brief Initializes the RTC MSP.
  248. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  249. * the configuration information for RTC.
  250. * @retval None
  251. */
  252. __weak void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
  253. {
  254. /* Prevent unused argument(s) compilation warning */
  255. UNUSED(hrtc);
  256. /* NOTE : This function Should not be modified, when the callback is needed,
  257. the HAL_RTC_MspInit could be implemented in the user file
  258. */
  259. }
  260. /**
  261. * @brief DeInitializes the RTC MSP.
  262. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  263. * the configuration information for RTC.
  264. * @retval None
  265. */
  266. __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
  267. {
  268. /* Prevent unused argument(s) compilation warning */
  269. UNUSED(hrtc);
  270. /* NOTE : This function Should not be modified, when the callback is needed,
  271. the HAL_RTC_MspDeInit could be implemented in the user file
  272. */
  273. }
  274. /**
  275. * @}
  276. */
  277. /** @defgroup RTC_Exported_Functions_Group2 RTC Time and Date functions
  278. * @brief RTC Time and Date functions
  279. *
  280. @verbatim
  281. ===============================================================================
  282. ##### RTC Time and Date functions #####
  283. ===============================================================================
  284. [..] This section provides functions allowing to configure Time and Date features
  285. @endverbatim
  286. * @{
  287. */
  288. /**
  289. * @brief Sets RTC current time.
  290. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  291. * the configuration information for RTC.
  292. * @param sTime: Pointer to Time structure
  293. * @param Format: Specifies the format of the entered parameters.
  294. * This parameter can be one of the following values:
  295. * @arg RTC_FORMAT_BIN: Binary data format
  296. * @arg RTC_FORMAT_BCD: BCD data format
  297. * @retval HAL status
  298. */
  299. HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
  300. {
  301. uint32_t tmpreg = 0;
  302. /* Check the parameters */
  303. assert_param(IS_RTC_FORMAT(Format));
  304. assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
  305. assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
  306. /* Process Locked */
  307. __HAL_LOCK(hrtc);
  308. hrtc->State = HAL_RTC_STATE_BUSY;
  309. if(Format == RTC_FORMAT_BIN)
  310. {
  311. if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
  312. {
  313. assert_param(IS_RTC_HOUR12(sTime->Hours));
  314. assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
  315. }
  316. else
  317. {
  318. sTime->TimeFormat = 0x00;
  319. assert_param(IS_RTC_HOUR24(sTime->Hours));
  320. }
  321. assert_param(IS_RTC_MINUTES(sTime->Minutes));
  322. assert_param(IS_RTC_SECONDS(sTime->Seconds));
  323. tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << 16) | \
  324. ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << 8) | \
  325. ((uint32_t)RTC_ByteToBcd2(sTime->Seconds)) | \
  326. (((uint32_t)sTime->TimeFormat) << 16));
  327. }
  328. else
  329. {
  330. if((hrtc->Instance->CR & RTC_CR_FMT) != (uint32_t)RESET)
  331. {
  332. tmpreg = RTC_Bcd2ToByte(sTime->Hours);
  333. assert_param(IS_RTC_HOUR12(tmpreg));
  334. assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
  335. }
  336. else
  337. {
  338. sTime->TimeFormat = 0x00;
  339. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
  340. }
  341. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
  342. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
  343. tmpreg = (((uint32_t)(sTime->Hours) << 16) | \
  344. ((uint32_t)(sTime->Minutes) << 8) | \
  345. ((uint32_t)sTime->Seconds) | \
  346. ((uint32_t)(sTime->TimeFormat) << 16));
  347. }
  348. /* Disable the write protection for RTC registers */
  349. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  350. /* Set Initialization mode */
  351. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  352. {
  353. /* Enable the write protection for RTC registers */
  354. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  355. /* Set RTC state */
  356. hrtc->State = HAL_RTC_STATE_ERROR;
  357. /* Process Unlocked */
  358. __HAL_UNLOCK(hrtc);
  359. return HAL_ERROR;
  360. }
  361. else
  362. {
  363. /* Set the RTC_TR register */
  364. hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
  365. /* Clear the bits to be configured */
  366. hrtc->Instance->CR &= (uint32_t)~RTC_CR_BKP;
  367. /* Configure the RTC_CR register */
  368. hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
  369. /* Exit Initialization mode */
  370. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  371. /* Wait for synchro */
  372. if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  373. {
  374. /* Enable the write protection for RTC registers */
  375. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  376. hrtc->State = HAL_RTC_STATE_ERROR;
  377. /* Process Unlocked */
  378. __HAL_UNLOCK(hrtc);
  379. return HAL_ERROR;
  380. }
  381. /* Enable the write protection for RTC registers */
  382. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  383. hrtc->State = HAL_RTC_STATE_READY;
  384. __HAL_UNLOCK(hrtc);
  385. return HAL_OK;
  386. }
  387. }
  388. /**
  389. * @brief Sets RTC current date.
  390. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  391. * the configuration information for RTC.
  392. * @param sDate: Pointer to date structure
  393. * @param Format: specifies the format of the entered parameters.
  394. * This parameter can be one of the following values:
  395. * @arg RTC_FORMAT_BIN: Binary data format
  396. * @arg RTC_FORMAT_BCD: BCD data format
  397. * @retval HAL status
  398. */
  399. HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
  400. {
  401. uint32_t datetmpreg = 0;
  402. /* Check the parameters */
  403. assert_param(IS_RTC_FORMAT(Format));
  404. /* Process Locked */
  405. __HAL_LOCK(hrtc);
  406. hrtc->State = HAL_RTC_STATE_BUSY;
  407. if((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10) == 0x10))
  408. {
  409. sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10)) + (uint8_t)0x0A);
  410. }
  411. assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
  412. if(Format == RTC_FORMAT_BIN)
  413. {
  414. assert_param(IS_RTC_YEAR(sDate->Year));
  415. assert_param(IS_RTC_MONTH(sDate->Month));
  416. assert_param(IS_RTC_DATE(sDate->Date));
  417. datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << 16) | \
  418. ((uint32_t)RTC_ByteToBcd2(sDate->Month) << 8) | \
  419. ((uint32_t)RTC_ByteToBcd2(sDate->Date)) | \
  420. ((uint32_t)sDate->WeekDay << 13));
  421. }
  422. else
  423. {
  424. assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
  425. datetmpreg = RTC_Bcd2ToByte(sDate->Month);
  426. assert_param(IS_RTC_MONTH(datetmpreg));
  427. datetmpreg = RTC_Bcd2ToByte(sDate->Date);
  428. assert_param(IS_RTC_DATE(datetmpreg));
  429. datetmpreg = ((((uint32_t)sDate->Year) << 16) | \
  430. (((uint32_t)sDate->Month) << 8) | \
  431. ((uint32_t)sDate->Date) | \
  432. (((uint32_t)sDate->WeekDay) << 13));
  433. }
  434. /* Disable the write protection for RTC registers */
  435. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  436. /* Set Initialization mode */
  437. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  438. {
  439. /* Enable the write protection for RTC registers */
  440. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  441. /* Set RTC state*/
  442. hrtc->State = HAL_RTC_STATE_ERROR;
  443. /* Process Unlocked */
  444. __HAL_UNLOCK(hrtc);
  445. return HAL_ERROR;
  446. }
  447. else
  448. {
  449. /* Set the RTC_DR register */
  450. hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
  451. /* Exit Initialization mode */
  452. hrtc->Instance->ISR &= (uint32_t)~RTC_ISR_INIT;
  453. /* Wait for synchro */
  454. if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  455. {
  456. /* Enable the write protection for RTC registers */
  457. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  458. hrtc->State = HAL_RTC_STATE_ERROR;
  459. /* Process Unlocked */
  460. __HAL_UNLOCK(hrtc);
  461. return HAL_ERROR;
  462. }
  463. /* Enable the write protection for RTC registers */
  464. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  465. hrtc->State = HAL_RTC_STATE_READY ;
  466. /* Process Unlocked */
  467. __HAL_UNLOCK(hrtc);
  468. return HAL_OK;
  469. }
  470. }
  471. /**
  472. * @brief Gets RTC current date.
  473. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  474. * the configuration information for RTC.
  475. * @param sDate: Pointer to Date structure
  476. * @param Format: Specifies the format of the entered parameters.
  477. * This parameter can be one of the following values:
  478. * @arg RTC_FORMAT_BIN: Binary data format
  479. * @arg RTC_FORMAT_BCD: BCD data format
  480. * @note You 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. * @retval HAL status
  484. */
  485. HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
  486. {
  487. uint32_t datetmpreg = 0;
  488. /* Check the parameters */
  489. assert_param(IS_RTC_FORMAT(Format));
  490. /* Get the DR register */
  491. datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
  492. /* Fill the structure fields with the read parameters */
  493. sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16);
  494. sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8);
  495. sDate->Date = (uint8_t)(datetmpreg & (RTC_DR_DT | RTC_DR_DU));
  496. sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> 13);
  497. /* Check the input parameters format */
  498. if(Format == RTC_FORMAT_BIN)
  499. {
  500. /* Convert the date structure parameters to Binary format */
  501. sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
  502. sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
  503. sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
  504. }
  505. return HAL_OK;
  506. }
  507. /**
  508. * @}
  509. */
  510. /** @defgroup RTC_Exported_Functions_Group3 RTC Alarm functions
  511. * @brief RTC Alarm functions
  512. *
  513. @verbatim
  514. ===============================================================================
  515. ##### RTC Alarm functions #####
  516. ===============================================================================
  517. [..] This section provides functions allowing to configure Alarm feature
  518. @endverbatim
  519. * @{
  520. */
  521. /**
  522. * @brief Deactive the specified RTC Alarm
  523. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  524. * the configuration information for RTC.
  525. * @param Alarm: Specifies the Alarm.
  526. * This parameter can be one of the following values:
  527. * @arg RTC_ALARM_A: AlarmA
  528. * @arg RTC_ALARM_B: AlarmB
  529. * @retval HAL status
  530. */
  531. HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
  532. {
  533. uint32_t tickstart = 0;
  534. /* Check the parameters */
  535. assert_param(IS_RTC_ALARM(Alarm));
  536. /* Process Locked */
  537. __HAL_LOCK(hrtc);
  538. hrtc->State = HAL_RTC_STATE_BUSY;
  539. /* Disable the write protection for RTC registers */
  540. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  541. if(Alarm == RTC_ALARM_A)
  542. {
  543. /* AlarmA */
  544. __HAL_RTC_ALARMA_DISABLE(hrtc);
  545. /* In case of interrupt mode is used, the interrupt source must disabled */
  546. __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
  547. tickstart = HAL_GetTick();
  548. /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
  549. while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == RESET)
  550. {
  551. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  552. {
  553. /* Enable the write protection for RTC registers */
  554. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  555. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  556. /* Process Unlocked */
  557. __HAL_UNLOCK(hrtc);
  558. return HAL_TIMEOUT;
  559. }
  560. }
  561. }
  562. else
  563. {
  564. /* AlarmB */
  565. __HAL_RTC_ALARMB_DISABLE(hrtc);
  566. /* In case of interrupt mode is used, the interrupt source must disabled */
  567. __HAL_RTC_ALARM_DISABLE_IT(hrtc,RTC_IT_ALRB);
  568. tickstart = HAL_GetTick();
  569. /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
  570. while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == RESET)
  571. {
  572. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  573. {
  574. /* Enable the write protection for RTC registers */
  575. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  576. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  577. /* Process Unlocked */
  578. __HAL_UNLOCK(hrtc);
  579. return HAL_TIMEOUT;
  580. }
  581. }
  582. }
  583. /* Enable the write protection for RTC registers */
  584. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  585. hrtc->State = HAL_RTC_STATE_READY;
  586. /* Process Unlocked */
  587. __HAL_UNLOCK(hrtc);
  588. return HAL_OK;
  589. }
  590. /**
  591. * @brief This function handles Alarm interrupt request.
  592. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  593. * the configuration information for RTC.
  594. * @retval None
  595. */
  596. void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef* hrtc)
  597. {
  598. /* Get the AlarmA interrupt source enable status */
  599. if(__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRA) != RESET)
  600. {
  601. /* Get the pending status of the AlarmA Interrupt */
  602. if(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) != RESET)
  603. {
  604. /* AlarmA callback */
  605. HAL_RTC_AlarmAEventCallback(hrtc);
  606. /* Clear the AlarmA interrupt pending bit */
  607. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
  608. }
  609. }
  610. /* Get the AlarmB interrupt source enable status */
  611. if(__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRB) != RESET)
  612. {
  613. /* Get the pending status of the AlarmB Interrupt */
  614. if(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) != RESET)
  615. {
  616. /* AlarmB callback */
  617. HAL_RTCEx_AlarmBEventCallback(hrtc);
  618. /* Clear the AlarmB interrupt pending bit */
  619. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
  620. }
  621. }
  622. /* Clear the EXTI's line Flag for RTC Alarm */
  623. __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
  624. /* Change RTC state */
  625. hrtc->State = HAL_RTC_STATE_READY;
  626. }
  627. /**
  628. * @brief Alarm A callback.
  629. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  630. * the configuration information for RTC.
  631. * @retval None
  632. */
  633. __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
  634. {
  635. /* Prevent unused argument(s) compilation warning */
  636. UNUSED(hrtc);
  637. /* NOTE : This function Should not be modified, when the callback is needed,
  638. the HAL_RTC_AlarmAEventCallback could be implemented in the user file
  639. */
  640. }
  641. /**
  642. * @brief This function handles AlarmA Polling request.
  643. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  644. * the configuration information for RTC.
  645. * @param Timeout: Timeout duration
  646. * @retval HAL status
  647. */
  648. HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  649. {
  650. uint32_t tickstart = HAL_GetTick();
  651. while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == RESET)
  652. {
  653. if(Timeout != HAL_MAX_DELAY)
  654. {
  655. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  656. {
  657. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  658. return HAL_TIMEOUT;
  659. }
  660. }
  661. }
  662. /* Clear the Alarm interrupt pending bit */
  663. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
  664. /* Change RTC state */
  665. hrtc->State = HAL_RTC_STATE_READY;
  666. return HAL_OK;
  667. }
  668. /**
  669. * @}
  670. */
  671. /** @defgroup RTC_Exported_Functions_Group5 Peripheral State functions
  672. * @brief Peripheral State functions
  673. *
  674. @verbatim
  675. ===============================================================================
  676. ##### Peripheral State functions #####
  677. ===============================================================================
  678. [..]
  679. This subsection provides functions allowing to
  680. (+) Get RTC state
  681. @endverbatim
  682. * @{
  683. */
  684. /**
  685. * @brief Returns the RTC state.
  686. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  687. * the configuration information for RTC.
  688. * @retval HAL state
  689. */
  690. HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef* hrtc)
  691. {
  692. return hrtc->State;
  693. }
  694. /**
  695. * @}
  696. */
  697. /**
  698. * @}
  699. */
  700. /** @defgroup RTC_Internal_Functions RTC Internal function
  701. * @{
  702. */
  703. /**
  704. * @brief Enters the RTC Initialization mode.
  705. * @note The RTC Initialization mode is write protected, use the
  706. * __HAL_RTC_WRITEPROTECTION_DISABLE() before calling this function.
  707. * @param hrtc: pointer to a RTC_HandleTypeDef structure that contains
  708. * the configuration information for RTC.
  709. * @retval HAL status
  710. */
  711. HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef* hrtc)
  712. {
  713. uint32_t tickstart = 0;
  714. /* Check if the Initialization mode is set */
  715. if((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
  716. {
  717. /* Set the Initialization mode */
  718. hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
  719. tickstart = HAL_GetTick();
  720. /* Wait till RTC is in INIT state and if Time out is reached exit */
  721. while((hrtc->Instance->ISR & RTC_ISR_INITF) == (uint32_t)RESET)
  722. {
  723. if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  724. {
  725. return HAL_TIMEOUT;
  726. }
  727. }
  728. }
  729. return HAL_OK;
  730. }
  731. /**
  732. * @brief Converts a 2 digit decimal to BCD format.
  733. * @param Value: Byte to be converted
  734. * @retval Converted byte
  735. */
  736. uint8_t RTC_ByteToBcd2(uint8_t Value)
  737. {
  738. uint32_t bcdhigh = 0;
  739. while(Value >= 10)
  740. {
  741. bcdhigh++;
  742. Value -= 10;
  743. }
  744. return ((uint8_t)(bcdhigh << 4) | Value);
  745. }
  746. /**
  747. * @brief Converts from 2 digit BCD to Binary.
  748. * @param Value: BCD value to be converted
  749. * @retval Converted word
  750. */
  751. uint8_t RTC_Bcd2ToByte(uint8_t Value)
  752. {
  753. uint32_t tmp = 0;
  754. tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10;
  755. return (tmp + (Value & (uint8_t)0x0F));
  756. }
  757. /**
  758. * @}
  759. */
  760. #endif /* HAL_RTC_MODULE_ENABLED */
  761. /**
  762. * @}
  763. */
  764. /**
  765. * @}
  766. */
  767. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/