stm32l1xx_hal_rtc.c 30 KB

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