stm32f1xx_hal_rtc.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_rtc.c
  4. * @author MCD Application Team
  5. * @version V1.1.1
  6. * @date 12-May-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 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. ##### How to use this driver #####
  19. ==================================================================
  20. [..]
  21. (+) Enable the RTC domain access (see description in the section above).
  22. (+) Configure the RTC Prescaler (Asynchronous prescaler to generate RTC 1Hz time base)
  23. using the HAL_RTC_Init() function.
  24. *** Time and Date configuration ***
  25. ===================================
  26. [..]
  27. (+) To configure the RTC Calendar (Time and Date) use the HAL_RTC_SetTime()
  28. and HAL_RTC_SetDate() functions.
  29. (+) To read the RTC Calendar, use the HAL_RTC_GetTime() and HAL_RTC_GetDate() functions.
  30. *** Alarm configuration ***
  31. ===========================
  32. [..]
  33. (+) To configure the RTC Alarm use the HAL_RTC_SetAlarm() function.
  34. You can also configure the RTC Alarm with interrupt mode using the HAL_RTC_SetAlarm_IT() function.
  35. (+) To read the RTC Alarm, use the HAL_RTC_GetAlarm() function.
  36. *** Tamper configuration ***
  37. ============================
  38. [..]
  39. (+) Enable the RTC Tamper and configure the Tamper Level using the
  40. HAL_RTCEx_SetTamper() function. You can configure RTC Tamper with interrupt
  41. mode using HAL_RTCEx_SetTamper_IT() function.
  42. (+) The TAMPER1 alternate function can be mapped to PC13
  43. *** Backup Data Registers configuration ***
  44. ===========================================
  45. [..]
  46. (+) To write to the RTC Backup Data registers, use the HAL_RTCEx_BKUPWrite()
  47. function.
  48. (+) To read the RTC Backup Data registers, use the HAL_RTCEx_BKUPRead()
  49. function.
  50. ##### WARNING: Drivers Restrictions #####
  51. ==================================================================
  52. [..] RTC version used on STM32F1 families is version V1. All the features supported by V2
  53. (other families) will be not supported on F1.
  54. [..] As on V2, main RTC features are managed by HW. But on F1, date feature is completely
  55. managed by SW.
  56. [..] Then, there are some restrictions compared to other families:
  57. (+) Only format 24 hours supported in HAL (format 12 hours not supported)
  58. (+) Date is saved in SRAM. Then, when MCU is in STOP or STANDBY mode, date will be lost.
  59. User should implement a way to save date before entering in low power mode (an
  60. example is provided with firmware package based on backup registers)
  61. (+) Date is automatically updated each time a HAL_RTC_GetTime or HAL_RTC_GetDate is called.
  62. (+) Alarm detection is limited to 1 day. It will expire only 1 time (no alarm repetition, need
  63. to program a new alarm)
  64. ##### Backup Domain Operating Condition #####
  65. ==============================================================================
  66. [..] The real-time clock (RTC) and the RTC backup registers can be powered
  67. from the VBAT voltage when the main VDD supply is powered off.
  68. To retain the content of the RTC backup registers and supply the RTC
  69. when VDD is turned off, VBAT pin can be connected to an optional
  70. standby voltage supplied by a battery or by another source.
  71. [..] To allow the RTC operating even when the main digital supply (VDD) is turned
  72. off, the VBAT pin powers the following blocks:
  73. (+) The RTC
  74. (+) The LSE oscillator
  75. (+) PC13 I/O
  76. [..] When the backup domain is supplied by VDD (analog switch connected to VDD),
  77. the following pins are available:
  78. (+) PC13 can be used as a Tamper pin
  79. [..] When the backup domain is supplied by VBAT (analog switch connected to VBAT
  80. because VDD is not present), the following pins are available:
  81. (+) PC13 can be used as the Tamper pin
  82. ##### Backup Domain Reset #####
  83. ==================================================================
  84. [..] The backup domain reset sets all RTC registers and the RCC_BDCR register
  85. to their reset values.
  86. [..] A backup domain reset is generated when one of the following events occurs:
  87. (#) Software reset, triggered by setting the BDRST bit in the
  88. RCC Backup domain control register (RCC_BDCR).
  89. (#) VDD or VBAT power on, if both supplies have previously been powered off.
  90. (#) Tamper detection event resets all data backup registers.
  91. ##### Backup Domain Access #####
  92. ==================================================================
  93. [..] After reset, the backup domain (RTC registers, RTC backup data
  94. registers and backup SRAM) is protected against possible unwanted write
  95. accesses.
  96. [..] To enable access to the RTC Domain and RTC registers, proceed as follows:
  97. (+) Call the function HAL_RCCEx_PeriphCLKConfig in using RCC_PERIPHCLK_RTC for
  98. PeriphClockSelection and select RTCClockSelection (LSE, LSI or HSE)
  99. (+) Enable the BKP clock in using __HAL_RCC_BKP_CLK_ENABLE()
  100. ##### RTC and low power modes #####
  101. ==================================================================
  102. [..] The MCU can be woken up from a low power mode by an RTC alternate
  103. function.
  104. [..] The RTC alternate functions are the RTC alarms (Alarm A),
  105. and RTC tamper event detection.
  106. These RTC alternate functions can wake up the system from the Stop and
  107. Standby low power modes.
  108. [..] The system can also wake up from low power modes without depending
  109. on an external interrupt (Auto-wakeup mode), by using the RTC alarm.
  110. @endverbatim
  111. ******************************************************************************
  112. * @attention
  113. *
  114. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  115. *
  116. * Redistribution and use in source and binary forms, with or without modification,
  117. * are permitted provided that the following conditions are met:
  118. * 1. Redistributions of source code must retain the above copyright notice,
  119. * this list of conditions and the following disclaimer.
  120. * 2. Redistributions in binary form must reproduce the above copyright notice,
  121. * this list of conditions and the following disclaimer in the documentation
  122. * and/or other materials provided with the distribution.
  123. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  124. * may be used to endorse or promote products derived from this software
  125. * without specific prior written permission.
  126. *
  127. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  128. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  129. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  130. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  131. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  132. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  133. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  134. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  135. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  136. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  137. *
  138. ******************************************************************************
  139. */
  140. /* Includes ------------------------------------------------------------------*/
  141. #include "stm32f1xx_hal.h"
  142. /** @addtogroup STM32F1xx_HAL_Driver
  143. * @{
  144. */
  145. /** @defgroup RTC RTC
  146. * @brief RTC HAL module driver
  147. * @{
  148. */
  149. #ifdef HAL_RTC_MODULE_ENABLED
  150. /* Private typedef -----------------------------------------------------------*/
  151. /* Private define ------------------------------------------------------------*/
  152. /** @defgroup RTC_Private_Constants RTC Private Constants
  153. * @{
  154. */
  155. #define RTC_ALARM_RESETVALUE_REGISTER (uint16_t)0xFFFF
  156. #define RTC_ALARM_RESETVALUE 0xFFFFFFFFU
  157. /**
  158. * @}
  159. */
  160. /* Private macro -------------------------------------------------------------*/
  161. /** @defgroup RTC_Private_Macros RTC Private Macros
  162. * @{
  163. */
  164. /**
  165. * @}
  166. */
  167. /* Private variables ---------------------------------------------------------*/
  168. /* Private function prototypes -----------------------------------------------*/
  169. /** @defgroup RTC_Private_Functions RTC Private Functions
  170. * @{
  171. */
  172. static uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef* hrtc);
  173. static HAL_StatusTypeDef RTC_WriteTimeCounter(RTC_HandleTypeDef* hrtc, uint32_t TimeCounter);
  174. static uint32_t RTC_ReadAlarmCounter(RTC_HandleTypeDef* hrtc);
  175. static HAL_StatusTypeDef RTC_WriteAlarmCounter(RTC_HandleTypeDef* hrtc, uint32_t AlarmCounter);
  176. static HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef* hrtc);
  177. static HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef* hrtc);
  178. static uint8_t RTC_ByteToBcd2(uint8_t Value);
  179. static uint8_t RTC_Bcd2ToByte(uint8_t Value);
  180. static uint8_t RTC_IsLeapYear(uint16_t nYear);
  181. static void RTC_DateUpdate(RTC_HandleTypeDef* hrtc, uint32_t DayElapsed);
  182. static uint8_t RTC_WeekDayNum(uint32_t nYear, uint8_t nMonth, uint8_t nDay);
  183. /**
  184. * @}
  185. */
  186. /* Private functions ---------------------------------------------------------*/
  187. /** @defgroup RTC_Exported_Functions RTC Exported Functions
  188. * @{
  189. */
  190. /** @defgroup RTC_Exported_Functions_Group1 Initialization and de-initialization functions
  191. * @brief Initialization and Configuration functions
  192. *
  193. @verbatim
  194. ===============================================================================
  195. ##### Initialization and de-initialization functions #####
  196. ===============================================================================
  197. [..] This section provides functions allowing to initialize and configure the
  198. RTC Prescaler (Asynchronous), disable RTC registers Write protection,
  199. enter and exit the RTC initialization mode,
  200. RTC registers synchronization check and reference clock detection enable.
  201. (#) The RTC Prescaler should be programmed to generate the RTC 1Hz time base.
  202. (#) All RTC registers are Write protected. Writing to the RTC registers
  203. is enabled by setting the CNF bit in the RTC_CRL register.
  204. (#) To read the calendar after wakeup from low power modes (Standby or Stop)
  205. the software must first wait for the RSF bit (Register Synchronized Flag)
  206. in the RTC_CRL register to be set by hardware.
  207. The HAL_RTC_WaitForSynchro() function implements the above software
  208. sequence (RSF clear and RSF check).
  209. @endverbatim
  210. * @{
  211. */
  212. /**
  213. * @brief Initializes the RTC peripheral
  214. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  215. * the configuration information for RTC.
  216. * @retval HAL status
  217. */
  218. HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
  219. {
  220. uint32_t prescaler = 0U;
  221. /* Check input parameters */
  222. if(hrtc == NULL)
  223. {
  224. return HAL_ERROR;
  225. }
  226. /* Check the parameters */
  227. assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
  228. assert_param(IS_RTC_CALIB_OUTPUT(hrtc->Init.OutPut));
  229. assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
  230. if(hrtc->State == HAL_RTC_STATE_RESET)
  231. {
  232. /* Allocate lock resource and initialize it */
  233. hrtc->Lock = HAL_UNLOCKED;
  234. /* Initialize RTC MSP */
  235. HAL_RTC_MspInit(hrtc);
  236. }
  237. /* Set RTC state */
  238. hrtc->State = HAL_RTC_STATE_BUSY;
  239. /* Waiting for synchro */
  240. if(HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
  241. {
  242. /* Set RTC state */
  243. hrtc->State = HAL_RTC_STATE_ERROR;
  244. return HAL_ERROR;
  245. }
  246. /* Set Initialization mode */
  247. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  248. {
  249. /* Set RTC state */
  250. hrtc->State = HAL_RTC_STATE_ERROR;
  251. return HAL_ERROR;
  252. }
  253. else
  254. {
  255. /* Clear Flags Bits */
  256. CLEAR_BIT(hrtc->Instance->CRL, (RTC_FLAG_OW | RTC_FLAG_ALRAF | RTC_FLAG_SEC));
  257. if(hrtc->Init.OutPut != RTC_OUTPUTSOURCE_NONE)
  258. {
  259. /* Disable the selected Tamper pin */
  260. CLEAR_BIT(BKP->CR, BKP_CR_TPE);
  261. }
  262. /* Set the signal which will be routed to RTC Tamper pin*/
  263. MODIFY_REG(BKP->RTCCR, (BKP_RTCCR_CCO | BKP_RTCCR_ASOE | BKP_RTCCR_ASOS), hrtc->Init.OutPut);
  264. if (hrtc->Init.AsynchPrediv != RTC_AUTO_1_SECOND)
  265. {
  266. /* RTC Prescaler provided directly by end-user*/
  267. prescaler = hrtc->Init.AsynchPrediv;
  268. }
  269. else
  270. {
  271. /* RTC Prescaler will be automatically calculated to get 1 second timebase */
  272. /* Get the RTCCLK frequency */
  273. prescaler = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_RTC);
  274. /* Check that RTC clock is enabled*/
  275. if (prescaler == 0U)
  276. {
  277. /* Should not happen. Frequency is not available*/
  278. hrtc->State = HAL_RTC_STATE_ERROR;
  279. return HAL_ERROR;
  280. }
  281. else
  282. {
  283. /* RTC period = RTCCLK/(RTC_PR + 1) */
  284. prescaler = prescaler - 1U;
  285. }
  286. }
  287. /* Configure the RTC_PRLH / RTC_PRLL */
  288. MODIFY_REG(hrtc->Instance->PRLH, RTC_PRLH_PRL, (prescaler >> 16U));
  289. MODIFY_REG(hrtc->Instance->PRLL, RTC_PRLL_PRL, (prescaler & RTC_PRLL_PRL));
  290. /* Wait for synchro */
  291. if(RTC_ExitInitMode(hrtc) != HAL_OK)
  292. {
  293. hrtc->State = HAL_RTC_STATE_ERROR;
  294. return HAL_ERROR;
  295. }
  296. /* Initialize date to 1st of January 2000 */
  297. hrtc->DateToUpdate.Year = 0x00U;
  298. hrtc->DateToUpdate.Month = RTC_MONTH_JANUARY;
  299. hrtc->DateToUpdate.Date = 0x01U;
  300. /* Set RTC state */
  301. hrtc->State = HAL_RTC_STATE_READY;
  302. return HAL_OK;
  303. }
  304. }
  305. /**
  306. * @brief DeInitializes the RTC peripheral
  307. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  308. * the configuration information for RTC.
  309. * @note This function does not reset the RTC Backup Data registers.
  310. * @retval HAL status
  311. */
  312. HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
  313. {
  314. /* Check input parameters */
  315. if(hrtc == NULL)
  316. {
  317. return HAL_ERROR;
  318. }
  319. /* Check the parameters */
  320. assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
  321. /* Set RTC state */
  322. hrtc->State = HAL_RTC_STATE_BUSY;
  323. /* Set Initialization mode */
  324. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  325. {
  326. /* Set RTC state */
  327. hrtc->State = HAL_RTC_STATE_ERROR;
  328. /* Release Lock */
  329. __HAL_UNLOCK(hrtc);
  330. return HAL_ERROR;
  331. }
  332. else
  333. {
  334. CLEAR_REG(hrtc->Instance->CNTL);
  335. CLEAR_REG(hrtc->Instance->CNTH);
  336. WRITE_REG(hrtc->Instance->PRLL, 0x00008000U);
  337. CLEAR_REG(hrtc->Instance->PRLH);
  338. /* Reset All CRH/CRL bits */
  339. CLEAR_REG(hrtc->Instance->CRH);
  340. CLEAR_REG(hrtc->Instance->CRL);
  341. if(RTC_ExitInitMode(hrtc) != HAL_OK)
  342. {
  343. hrtc->State = HAL_RTC_STATE_ERROR;
  344. /* Process Unlocked */
  345. __HAL_UNLOCK(hrtc);
  346. return HAL_ERROR;
  347. }
  348. }
  349. /* Wait for synchro*/
  350. HAL_RTC_WaitForSynchro(hrtc);
  351. /* Clear RSF flag */
  352. CLEAR_BIT(hrtc->Instance->CRL, RTC_FLAG_RSF);
  353. /* De-Initialize RTC MSP */
  354. HAL_RTC_MspDeInit(hrtc);
  355. hrtc->State = HAL_RTC_STATE_RESET;
  356. /* Release Lock */
  357. __HAL_UNLOCK(hrtc);
  358. return HAL_OK;
  359. }
  360. /**
  361. * @brief Initializes the RTC MSP.
  362. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  363. * the configuration information for RTC.
  364. * @retval None
  365. */
  366. __weak void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
  367. {
  368. /* Prevent unused argument(s) compilation warning */
  369. UNUSED(hrtc);
  370. /* NOTE : This function Should not be modified, when the callback is needed,
  371. the HAL_RTC_MspInit could be implemented in the user file
  372. */
  373. }
  374. /**
  375. * @brief DeInitializes the RTC MSP.
  376. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  377. * the configuration information for RTC.
  378. * @retval None
  379. */
  380. __weak void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
  381. {
  382. /* Prevent unused argument(s) compilation warning */
  383. UNUSED(hrtc);
  384. /* NOTE : This function Should not be modified, when the callback is needed,
  385. the HAL_RTC_MspDeInit could be implemented in the user file
  386. */
  387. }
  388. /**
  389. * @}
  390. */
  391. /** @defgroup RTC_Exported_Functions_Group2 Time and Date functions
  392. * @brief RTC Time and Date functions
  393. *
  394. @verbatim
  395. ===============================================================================
  396. ##### RTC Time and Date functions #####
  397. ===============================================================================
  398. [..] This section provides functions allowing to configure Time and Date features
  399. @endverbatim
  400. * @{
  401. */
  402. /**
  403. * @brief Sets RTC current time.
  404. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  405. * the configuration information for RTC.
  406. * @param sTime: Pointer to Time structure
  407. * @param Format: Specifies the format of the entered parameters.
  408. * This parameter can be one of the following values:
  409. * @arg RTC_FORMAT_BIN: Binary data format
  410. * @arg RTC_FORMAT_BCD: BCD data format
  411. * @retval HAL status
  412. */
  413. HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
  414. {
  415. uint32_t counter_time = 0U, counter_alarm = 0U;
  416. /* Check input parameters */
  417. if((hrtc == NULL) || (sTime == NULL))
  418. {
  419. return HAL_ERROR;
  420. }
  421. /* Check the parameters */
  422. assert_param(IS_RTC_FORMAT(Format));
  423. /* Process Locked */
  424. __HAL_LOCK(hrtc);
  425. hrtc->State = HAL_RTC_STATE_BUSY;
  426. if(Format == RTC_FORMAT_BIN)
  427. {
  428. assert_param(IS_RTC_HOUR24(sTime->Hours));
  429. assert_param(IS_RTC_MINUTES(sTime->Minutes));
  430. assert_param(IS_RTC_SECONDS(sTime->Seconds));
  431. counter_time = (uint32_t)(((uint32_t)sTime->Hours * 3600U) + \
  432. ((uint32_t)sTime->Minutes * 60U) + \
  433. ((uint32_t)sTime->Seconds));
  434. }
  435. else
  436. {
  437. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
  438. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
  439. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
  440. counter_time = (((uint32_t)(RTC_Bcd2ToByte(sTime->Hours)) * 3600U) + \
  441. ((uint32_t)(RTC_Bcd2ToByte(sTime->Minutes)) * 60U) + \
  442. ((uint32_t)(RTC_Bcd2ToByte(sTime->Seconds))));
  443. }
  444. /* Write time counter in RTC registers */
  445. if (RTC_WriteTimeCounter(hrtc, counter_time) != HAL_OK)
  446. {
  447. /* Set RTC state */
  448. hrtc->State = HAL_RTC_STATE_ERROR;
  449. /* Process Unlocked */
  450. __HAL_UNLOCK(hrtc);
  451. return HAL_ERROR;
  452. }
  453. else
  454. {
  455. /* Clear Second and overflow flags */
  456. CLEAR_BIT(hrtc->Instance->CRL, (RTC_FLAG_SEC | RTC_FLAG_OW));
  457. /* Read current Alarm counter in RTC registers */
  458. counter_alarm = RTC_ReadAlarmCounter(hrtc);
  459. /* Set again alarm to match with new time if enabled */
  460. if (counter_alarm != RTC_ALARM_RESETVALUE)
  461. {
  462. if(counter_alarm < counter_time)
  463. {
  464. /* Add 1 day to alarm counter*/
  465. counter_alarm += (uint32_t)(24U * 3600U);
  466. /* Write new Alarm counter in RTC registers */
  467. if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
  468. {
  469. /* Set RTC state */
  470. hrtc->State = HAL_RTC_STATE_ERROR;
  471. /* Process Unlocked */
  472. __HAL_UNLOCK(hrtc);
  473. return HAL_ERROR;
  474. }
  475. }
  476. }
  477. hrtc->State = HAL_RTC_STATE_READY;
  478. __HAL_UNLOCK(hrtc);
  479. return HAL_OK;
  480. }
  481. }
  482. /**
  483. * @brief Gets RTC current time.
  484. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  485. * the configuration information for RTC.
  486. * @param sTime: Pointer to Time structure
  487. * @param Format: Specifies the format of the entered parameters.
  488. * This parameter can be one of the following values:
  489. * @arg RTC_FORMAT_BIN: Binary data format
  490. * @arg RTC_FORMAT_BCD: BCD data format
  491. * @retval HAL status
  492. */
  493. HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
  494. {
  495. uint32_t counter_time = 0U, counter_alarm = 0U, days_elapsed = 0U, hours = 0U;
  496. /* Check input parameters */
  497. if((hrtc == NULL) || (sTime == NULL))
  498. {
  499. return HAL_ERROR;
  500. }
  501. /* Check the parameters */
  502. assert_param(IS_RTC_FORMAT(Format));
  503. /* Check if counter overflow occurred */
  504. if (__HAL_RTC_OVERFLOW_GET_FLAG(hrtc, RTC_FLAG_OW))
  505. {
  506. return HAL_ERROR;
  507. }
  508. /* Read the time counter*/
  509. counter_time = RTC_ReadTimeCounter(hrtc);
  510. /* Fill the structure fields with the read parameters */
  511. hours = counter_time / 3600U;
  512. sTime->Minutes = (uint8_t)((counter_time % 3600U) / 60U);
  513. sTime->Seconds = (uint8_t)((counter_time % 3600U) % 60U);
  514. if (hours >= 24U)
  515. {
  516. /* Get number of days elapsed from last calculation */
  517. days_elapsed = (hours / 24U);
  518. /* Set Hours in RTC_TimeTypeDef structure*/
  519. sTime->Hours = (hours % 24U);
  520. /* Read Alarm counter in RTC registers */
  521. counter_alarm = RTC_ReadAlarmCounter(hrtc);
  522. /* Calculate remaining time to reach alarm (only if set and not yet expired)*/
  523. if ((counter_alarm != RTC_ALARM_RESETVALUE) && (counter_alarm > counter_time))
  524. {
  525. counter_alarm -= counter_time;
  526. }
  527. else
  528. {
  529. /* In case of counter_alarm < counter_time */
  530. /* Alarm expiration already occurred but alarm not deactivated */
  531. counter_alarm = RTC_ALARM_RESETVALUE;
  532. }
  533. /* Set updated time in decreasing counter by number of days elapsed */
  534. counter_time -= (days_elapsed * 24U * 3600U);
  535. /* Write time counter in RTC registers */
  536. if (RTC_WriteTimeCounter(hrtc, counter_time) != HAL_OK)
  537. {
  538. return HAL_ERROR;
  539. }
  540. /* Set updated alarm to be set */
  541. if (counter_alarm != RTC_ALARM_RESETVALUE)
  542. {
  543. counter_alarm += counter_time;
  544. /* Write time counter in RTC registers */
  545. if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
  546. {
  547. return HAL_ERROR;
  548. }
  549. }
  550. else
  551. {
  552. /* Alarm already occurred. Set it to reset values to avoid unexpected expiration */
  553. if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
  554. {
  555. return HAL_ERROR;
  556. }
  557. }
  558. /* Update date */
  559. RTC_DateUpdate(hrtc, days_elapsed);
  560. }
  561. else
  562. {
  563. sTime->Hours = hours;
  564. }
  565. /* Check the input parameters format */
  566. if(Format != RTC_FORMAT_BIN)
  567. {
  568. /* Convert the time structure parameters to BCD format */
  569. sTime->Hours = (uint8_t)RTC_ByteToBcd2(sTime->Hours);
  570. sTime->Minutes = (uint8_t)RTC_ByteToBcd2(sTime->Minutes);
  571. sTime->Seconds = (uint8_t)RTC_ByteToBcd2(sTime->Seconds);
  572. }
  573. return HAL_OK;
  574. }
  575. /**
  576. * @brief Sets RTC current date.
  577. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  578. * the configuration information for RTC.
  579. * @param sDate: Pointer to date structure
  580. * @param Format: specifies the format of the entered parameters.
  581. * This parameter can be one of the following values:
  582. * @arg RTC_FORMAT_BIN: Binary data format
  583. * @arg RTC_FORMAT_BCD: BCD data format
  584. * @retval HAL status
  585. */
  586. HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
  587. {
  588. uint32_t counter_time = 0U, counter_alarm = 0U, hours = 0U;
  589. /* Check input parameters */
  590. if((hrtc == NULL) || (sDate == NULL))
  591. {
  592. return HAL_ERROR;
  593. }
  594. /* Check the parameters */
  595. assert_param(IS_RTC_FORMAT(Format));
  596. /* Process Locked */
  597. __HAL_LOCK(hrtc);
  598. hrtc->State = HAL_RTC_STATE_BUSY;
  599. if(Format == RTC_FORMAT_BIN)
  600. {
  601. assert_param(IS_RTC_YEAR(sDate->Year));
  602. assert_param(IS_RTC_MONTH(sDate->Month));
  603. assert_param(IS_RTC_DATE(sDate->Date));
  604. /* Change the current date */
  605. hrtc->DateToUpdate.Year = sDate->Year;
  606. hrtc->DateToUpdate.Month = sDate->Month;
  607. hrtc->DateToUpdate.Date = sDate->Date;
  608. }
  609. else
  610. {
  611. assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
  612. assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
  613. assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));
  614. /* Change the current date */
  615. hrtc->DateToUpdate.Year = RTC_Bcd2ToByte(sDate->Year);
  616. hrtc->DateToUpdate.Month = RTC_Bcd2ToByte(sDate->Month);
  617. hrtc->DateToUpdate.Date = RTC_Bcd2ToByte(sDate->Date);
  618. }
  619. /* WeekDay set by user can be ignored because automatically calculated */
  620. hrtc->DateToUpdate.WeekDay = RTC_WeekDayNum(hrtc->DateToUpdate.Year, hrtc->DateToUpdate.Month, hrtc->DateToUpdate.Date);
  621. sDate->WeekDay = hrtc->DateToUpdate.WeekDay;
  622. /* Reset time to be aligned on the same day */
  623. /* Read the time counter*/
  624. counter_time = RTC_ReadTimeCounter(hrtc);
  625. /* Fill the structure fields with the read parameters */
  626. hours = counter_time / 3600U;
  627. if (hours > 24U)
  628. {
  629. /* Set updated time in decreasing counter by number of days elapsed */
  630. counter_time -= ((hours / 24U) * 24U * 3600U);
  631. /* Write time counter in RTC registers */
  632. if (RTC_WriteTimeCounter(hrtc, counter_time) != HAL_OK)
  633. {
  634. /* Set RTC state */
  635. hrtc->State = HAL_RTC_STATE_ERROR;
  636. /* Process Unlocked */
  637. __HAL_UNLOCK(hrtc);
  638. return HAL_ERROR;
  639. }
  640. /* Read current Alarm counter in RTC registers */
  641. counter_alarm = RTC_ReadAlarmCounter(hrtc);
  642. /* Set again alarm to match with new time if enabled */
  643. if (counter_alarm != RTC_ALARM_RESETVALUE)
  644. {
  645. if(counter_alarm < counter_time)
  646. {
  647. /* Add 1 day to alarm counter*/
  648. counter_alarm += (uint32_t)(24U * 3600U);
  649. /* Write new Alarm counter in RTC registers */
  650. if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
  651. {
  652. /* Set RTC state */
  653. hrtc->State = HAL_RTC_STATE_ERROR;
  654. /* Process Unlocked */
  655. __HAL_UNLOCK(hrtc);
  656. return HAL_ERROR;
  657. }
  658. }
  659. }
  660. }
  661. hrtc->State = HAL_RTC_STATE_READY ;
  662. /* Process Unlocked */
  663. __HAL_UNLOCK(hrtc);
  664. return HAL_OK;
  665. }
  666. /**
  667. * @brief Gets RTC current date.
  668. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  669. * the configuration information for RTC.
  670. * @param sDate: Pointer to Date structure
  671. * @param Format: Specifies the format of the entered parameters.
  672. * This parameter can be one of the following values:
  673. * @arg RTC_FORMAT_BIN: Binary data format
  674. * @arg RTC_FORMAT_BCD: BCD data format
  675. * @retval HAL status
  676. */
  677. HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
  678. {
  679. RTC_TimeTypeDef stime = {0U};
  680. /* Check input parameters */
  681. if((hrtc == NULL) || (sDate == NULL))
  682. {
  683. return HAL_ERROR;
  684. }
  685. /* Check the parameters */
  686. assert_param(IS_RTC_FORMAT(Format));
  687. /* Call HAL_RTC_GetTime function to update date if counter higher than 24 hours */
  688. if (HAL_RTC_GetTime(hrtc, &stime, RTC_FORMAT_BIN) != HAL_OK)
  689. {
  690. return HAL_ERROR;
  691. }
  692. /* Fill the structure fields with the read parameters */
  693. sDate->WeekDay = hrtc->DateToUpdate.WeekDay;
  694. sDate->Year = hrtc->DateToUpdate.Year;
  695. sDate->Month = hrtc->DateToUpdate.Month;
  696. sDate->Date = hrtc->DateToUpdate.Date;
  697. /* Check the input parameters format */
  698. if(Format != RTC_FORMAT_BIN)
  699. {
  700. /* Convert the date structure parameters to BCD format */
  701. sDate->Year = (uint8_t)RTC_ByteToBcd2(sDate->Year);
  702. sDate->Month = (uint8_t)RTC_ByteToBcd2(sDate->Month);
  703. sDate->Date = (uint8_t)RTC_ByteToBcd2(sDate->Date);
  704. }
  705. return HAL_OK;
  706. }
  707. /**
  708. * @}
  709. */
  710. /** @defgroup RTC_Exported_Functions_Group3 Alarm functions
  711. * @brief RTC Alarm functions
  712. *
  713. @verbatim
  714. ===============================================================================
  715. ##### RTC Alarm functions #####
  716. ===============================================================================
  717. [..] This section provides functions allowing to configure Alarm feature
  718. @endverbatim
  719. * @{
  720. */
  721. /**
  722. * @brief Sets the specified RTC Alarm.
  723. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  724. * the configuration information for RTC.
  725. * @param sAlarm: Pointer to Alarm structure
  726. * @param Format: Specifies the format of the entered parameters.
  727. * This parameter can be one of the following values:
  728. * @arg RTC_FORMAT_BIN: Binary data format
  729. * @arg RTC_FORMAT_BCD: BCD data format
  730. * @retval HAL status
  731. */
  732. HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
  733. {
  734. uint32_t counter_alarm = 0U, counter_time;
  735. RTC_TimeTypeDef stime = {0U};
  736. /* Check input parameters */
  737. if((hrtc == NULL) || (sAlarm == NULL))
  738. {
  739. return HAL_ERROR;
  740. }
  741. /* Check the parameters */
  742. assert_param(IS_RTC_FORMAT(Format));
  743. assert_param(IS_RTC_ALARM(sAlarm->Alarm));
  744. /* Process Locked */
  745. __HAL_LOCK(hrtc);
  746. hrtc->State = HAL_RTC_STATE_BUSY;
  747. /* Call HAL_RTC_GetTime function to update date if counter higher than 24 hours */
  748. if (HAL_RTC_GetTime(hrtc, &stime, RTC_FORMAT_BIN) != HAL_OK)
  749. {
  750. return HAL_ERROR;
  751. }
  752. /* Convert time in seconds */
  753. counter_time = (uint32_t)(((uint32_t)stime.Hours * 3600U) + \
  754. ((uint32_t)stime.Minutes * 60U) + \
  755. ((uint32_t)stime.Seconds));
  756. if(Format == RTC_FORMAT_BIN)
  757. {
  758. assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
  759. assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
  760. assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
  761. counter_alarm = (uint32_t)(((uint32_t)sAlarm->AlarmTime.Hours * 3600U) + \
  762. ((uint32_t)sAlarm->AlarmTime.Minutes * 60U) + \
  763. ((uint32_t)sAlarm->AlarmTime.Seconds));
  764. }
  765. else
  766. {
  767. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  768. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
  769. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
  770. counter_alarm = (((uint32_t)(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)) * 3600U) + \
  771. ((uint32_t)(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)) * 60U) + \
  772. ((uint32_t)RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
  773. }
  774. /* Check that requested alarm should expire in the same day (otherwise add 1 day) */
  775. if (counter_alarm < counter_time)
  776. {
  777. /* Add 1 day to alarm counter*/
  778. counter_alarm += (uint32_t)(24U * 3600U);
  779. }
  780. /* Write Alarm counter in RTC registers */
  781. if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
  782. {
  783. /* Set RTC state */
  784. hrtc->State = HAL_RTC_STATE_ERROR;
  785. /* Process Unlocked */
  786. __HAL_UNLOCK(hrtc);
  787. return HAL_ERROR;
  788. }
  789. else
  790. {
  791. hrtc->State = HAL_RTC_STATE_READY;
  792. __HAL_UNLOCK(hrtc);
  793. return HAL_OK;
  794. }
  795. }
  796. /**
  797. * @brief Sets the specified RTC Alarm with Interrupt
  798. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  799. * the configuration information for RTC.
  800. * @param sAlarm: Pointer to Alarm structure
  801. * @param Format: Specifies the format of the entered parameters.
  802. * This parameter can be one of the following values:
  803. * @arg RTC_FORMAT_BIN: Binary data format
  804. * @arg RTC_FORMAT_BCD: BCD data format
  805. * @note The HAL_RTC_SetTime() must be called before enabling the Alarm feature.
  806. * @retval HAL status
  807. */
  808. HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
  809. {
  810. uint32_t counter_alarm = 0U, counter_time;
  811. RTC_TimeTypeDef stime = {0U};
  812. /* Check input parameters */
  813. if((hrtc == NULL) || (sAlarm == NULL))
  814. {
  815. return HAL_ERROR;
  816. }
  817. /* Check the parameters */
  818. assert_param(IS_RTC_FORMAT(Format));
  819. assert_param(IS_RTC_ALARM(sAlarm->Alarm));
  820. /* Process Locked */
  821. __HAL_LOCK(hrtc);
  822. hrtc->State = HAL_RTC_STATE_BUSY;
  823. /* Call HAL_RTC_GetTime function to update date if counter higher than 24 hours */
  824. if (HAL_RTC_GetTime(hrtc, &stime, RTC_FORMAT_BIN) != HAL_OK)
  825. {
  826. return HAL_ERROR;
  827. }
  828. /* Convert time in seconds */
  829. counter_time = (uint32_t)(((uint32_t)stime.Hours * 3600U) + \
  830. ((uint32_t)stime.Minutes * 60U) + \
  831. ((uint32_t)stime.Seconds));
  832. if(Format == RTC_FORMAT_BIN)
  833. {
  834. assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
  835. assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
  836. assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
  837. counter_alarm = (uint32_t)(((uint32_t)sAlarm->AlarmTime.Hours * 3600U) + \
  838. ((uint32_t)sAlarm->AlarmTime.Minutes * 60U) + \
  839. ((uint32_t)sAlarm->AlarmTime.Seconds));
  840. }
  841. else
  842. {
  843. assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
  844. assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
  845. assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
  846. counter_alarm = (((uint32_t)(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)) * 3600U) + \
  847. ((uint32_t)(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)) * 60U) + \
  848. ((uint32_t)RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
  849. }
  850. /* Check that requested alarm should expire in the same day (otherwise add 1 day) */
  851. if (counter_alarm < counter_time)
  852. {
  853. /* Add 1 day to alarm counter*/
  854. counter_alarm += (uint32_t)(24U * 3600U);
  855. }
  856. /* Write alarm counter in RTC registers */
  857. if (RTC_WriteAlarmCounter(hrtc, counter_alarm) != HAL_OK)
  858. {
  859. /* Set RTC state */
  860. hrtc->State = HAL_RTC_STATE_ERROR;
  861. /* Process Unlocked */
  862. __HAL_UNLOCK(hrtc);
  863. return HAL_ERROR;
  864. }
  865. else
  866. {
  867. /* Clear flag alarm A */
  868. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
  869. /* Configure the Alarm interrupt */
  870. __HAL_RTC_ALARM_ENABLE_IT(hrtc,RTC_IT_ALRA);
  871. /* RTC Alarm Interrupt Configuration: EXTI configuration */
  872. __HAL_RTC_ALARM_EXTI_ENABLE_IT();
  873. __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
  874. hrtc->State = HAL_RTC_STATE_READY;
  875. __HAL_UNLOCK(hrtc);
  876. return HAL_OK;
  877. }
  878. }
  879. /**
  880. * @brief Gets the RTC Alarm value and masks.
  881. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  882. * the configuration information for RTC.
  883. * @param sAlarm: Pointer to Date structure
  884. * @param Alarm: Specifies the Alarm.
  885. * This parameter can be one of the following values:
  886. * @arg RTC_ALARM_A: Alarm
  887. * @param Format: Specifies the format of the entered parameters.
  888. * This parameter can be one of the following values:
  889. * @arg RTC_FORMAT_BIN: Binary data format
  890. * @arg RTC_FORMAT_BCD: BCD data format
  891. * @retval HAL status
  892. */
  893. HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
  894. {
  895. uint32_t counter_alarm = 0U;
  896. /* Prevent unused argument(s) compilation warning */
  897. UNUSED(Alarm);
  898. /* Check input parameters */
  899. if((hrtc == NULL) || (sAlarm == NULL))
  900. {
  901. return HAL_ERROR;
  902. }
  903. /* Check the parameters */
  904. assert_param(IS_RTC_FORMAT(Format));
  905. assert_param(IS_RTC_ALARM(Alarm));
  906. /* Read Alarm counter in RTC registers */
  907. counter_alarm = RTC_ReadAlarmCounter(hrtc);
  908. /* Fill the structure with the read parameters */
  909. /* Set hours in a day range (between 0 to 24)*/
  910. sAlarm->AlarmTime.Hours = (uint32_t)((counter_alarm / 3600U) % 24U);
  911. sAlarm->AlarmTime.Minutes = (uint32_t)((counter_alarm % 3600U) / 60U);
  912. sAlarm->AlarmTime.Seconds = (uint32_t)((counter_alarm % 3600U) % 60U);
  913. if(Format != RTC_FORMAT_BIN)
  914. {
  915. sAlarm->AlarmTime.Hours = RTC_ByteToBcd2(sAlarm->AlarmTime.Hours);
  916. sAlarm->AlarmTime.Minutes = RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes);
  917. sAlarm->AlarmTime.Seconds = RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds);
  918. }
  919. return HAL_OK;
  920. }
  921. /**
  922. * @brief Deactive the specified RTC Alarm
  923. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  924. * the configuration information for RTC.
  925. * @param Alarm: Specifies the Alarm.
  926. * This parameter can be one of the following values:
  927. * @arg RTC_ALARM_A: AlarmA
  928. * @retval HAL status
  929. */
  930. HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
  931. {
  932. /* Prevent unused argument(s) compilation warning */
  933. UNUSED(Alarm);
  934. /* Check the parameters */
  935. assert_param(IS_RTC_ALARM(Alarm));
  936. /* Check input parameters */
  937. if(hrtc == NULL)
  938. {
  939. return HAL_ERROR;
  940. }
  941. /* Process Locked */
  942. __HAL_LOCK(hrtc);
  943. hrtc->State = HAL_RTC_STATE_BUSY;
  944. /* In case of interrupt mode is used, the interrupt source must disabled */
  945. __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
  946. /* Set Initialization mode */
  947. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  948. {
  949. /* Set RTC state */
  950. hrtc->State = HAL_RTC_STATE_ERROR;
  951. /* Process Unlocked */
  952. __HAL_UNLOCK(hrtc);
  953. return HAL_ERROR;
  954. }
  955. else
  956. {
  957. /* Clear flag alarm A */
  958. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
  959. /* Set to default values ALRH & ALRL registers */
  960. WRITE_REG(hrtc->Instance->ALRH, RTC_ALARM_RESETVALUE_REGISTER);
  961. WRITE_REG(hrtc->Instance->ALRL, RTC_ALARM_RESETVALUE_REGISTER);
  962. /* RTC Alarm Interrupt Configuration: Disable EXTI configuration */
  963. __HAL_RTC_ALARM_EXTI_DISABLE_IT();
  964. /* Wait for synchro */
  965. if(RTC_ExitInitMode(hrtc) != HAL_OK)
  966. {
  967. hrtc->State = HAL_RTC_STATE_ERROR;
  968. /* Process Unlocked */
  969. __HAL_UNLOCK(hrtc);
  970. return HAL_ERROR;
  971. }
  972. }
  973. hrtc->State = HAL_RTC_STATE_READY;
  974. /* Process Unlocked */
  975. __HAL_UNLOCK(hrtc);
  976. return HAL_OK;
  977. }
  978. /**
  979. * @brief This function handles Alarm interrupt request.
  980. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  981. * the configuration information for RTC.
  982. * @retval None
  983. */
  984. void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef* hrtc)
  985. {
  986. if(__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRA))
  987. {
  988. /* Get the status of the Interrupt */
  989. if(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) != (uint32_t)RESET)
  990. {
  991. /* AlarmA callback */
  992. HAL_RTC_AlarmAEventCallback(hrtc);
  993. /* Clear the Alarm interrupt pending bit */
  994. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc,RTC_FLAG_ALRAF);
  995. }
  996. }
  997. /* Clear the EXTI's line Flag for RTC Alarm */
  998. __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
  999. /* Change RTC state */
  1000. hrtc->State = HAL_RTC_STATE_READY;
  1001. }
  1002. /**
  1003. * @brief Alarm A callback.
  1004. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1005. * the configuration information for RTC.
  1006. * @retval None
  1007. */
  1008. __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
  1009. {
  1010. /* Prevent unused argument(s) compilation warning */
  1011. UNUSED(hrtc);
  1012. /* NOTE : This function Should not be modified, when the callback is needed,
  1013. the HAL_RTC_AlarmAEventCallback could be implemented in the user file
  1014. */
  1015. }
  1016. /**
  1017. * @brief This function handles AlarmA Polling request.
  1018. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1019. * the configuration information for RTC.
  1020. * @param Timeout: Timeout duration
  1021. * @retval HAL status
  1022. */
  1023. HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
  1024. {
  1025. uint32_t tickstart = HAL_GetTick();
  1026. /* Check input parameters */
  1027. if(hrtc == NULL)
  1028. {
  1029. return HAL_ERROR;
  1030. }
  1031. while(__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == RESET)
  1032. {
  1033. if(Timeout != HAL_MAX_DELAY)
  1034. {
  1035. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1036. {
  1037. hrtc->State = HAL_RTC_STATE_TIMEOUT;
  1038. return HAL_TIMEOUT;
  1039. }
  1040. }
  1041. }
  1042. /* Clear the Alarm interrupt pending bit */
  1043. __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
  1044. /* Change RTC state */
  1045. hrtc->State = HAL_RTC_STATE_READY;
  1046. return HAL_OK;
  1047. }
  1048. /**
  1049. * @}
  1050. */
  1051. /** @defgroup RTC_Exported_Functions_Group4 Peripheral State functions
  1052. * @brief Peripheral State functions
  1053. *
  1054. @verbatim
  1055. ===============================================================================
  1056. ##### Peripheral State functions #####
  1057. ===============================================================================
  1058. [..]
  1059. This subsection provides functions allowing to
  1060. (+) Get RTC state
  1061. @endverbatim
  1062. * @{
  1063. */
  1064. /**
  1065. * @brief Returns the RTC state.
  1066. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1067. * the configuration information for RTC.
  1068. * @retval HAL state
  1069. */
  1070. HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef* hrtc)
  1071. {
  1072. return hrtc->State;
  1073. }
  1074. /**
  1075. * @}
  1076. */
  1077. /** @defgroup RTC_Exported_Functions_Group5 Peripheral Control functions
  1078. * @brief Peripheral Control functions
  1079. *
  1080. @verbatim
  1081. ===============================================================================
  1082. ##### Peripheral Control functions #####
  1083. ===============================================================================
  1084. [..]
  1085. This subsection provides functions allowing to
  1086. (+) Wait for RTC Time and Date Synchronization
  1087. @endverbatim
  1088. * @{
  1089. */
  1090. /**
  1091. * @brief Waits until the RTC registers (RTC_CNT, RTC_ALR and RTC_PRL)
  1092. * are synchronized with RTC APB clock.
  1093. * @note This function must be called before any read operation after an APB reset
  1094. * or an APB clock stop.
  1095. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1096. * the configuration information for RTC.
  1097. * @retval HAL status
  1098. */
  1099. HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef* hrtc)
  1100. {
  1101. uint32_t tickstart = 0U;
  1102. /* Check input parameters */
  1103. if(hrtc == NULL)
  1104. {
  1105. return HAL_ERROR;
  1106. }
  1107. /* Clear RSF flag */
  1108. CLEAR_BIT(hrtc->Instance->CRL, RTC_FLAG_RSF);
  1109. tickstart = HAL_GetTick();
  1110. /* Wait the registers to be synchronised */
  1111. while((hrtc->Instance->CRL & RTC_FLAG_RSF) == (uint32_t)RESET)
  1112. {
  1113. if((HAL_GetTick() - tickstart ) > RTC_TIMEOUT_VALUE)
  1114. {
  1115. return HAL_TIMEOUT;
  1116. }
  1117. }
  1118. return HAL_OK;
  1119. }
  1120. /**
  1121. * @}
  1122. */
  1123. /**
  1124. * @}
  1125. */
  1126. /** @addtogroup RTC_Private_Functions
  1127. * @{
  1128. */
  1129. /**
  1130. * @brief Read the time counter available in RTC_CNT registers.
  1131. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1132. * the configuration information for RTC.
  1133. * @retval Time counter
  1134. */
  1135. static uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef* hrtc)
  1136. {
  1137. uint16_t high1 = 0U, high2 = 0U, low = 0U;
  1138. uint32_t timecounter = 0U;
  1139. high1 = READ_REG(hrtc->Instance->CNTH & RTC_CNTH_RTC_CNT);
  1140. low = READ_REG(hrtc->Instance->CNTL & RTC_CNTL_RTC_CNT);
  1141. high2 = READ_REG(hrtc->Instance->CNTH & RTC_CNTH_RTC_CNT);
  1142. if (high1 != high2)
  1143. { /* In this case the counter roll over during reading of CNTL and CNTH registers,
  1144. read again CNTL register then return the counter value */
  1145. timecounter = (((uint32_t) high2 << 16U) | READ_REG(hrtc->Instance->CNTL & RTC_CNTL_RTC_CNT));
  1146. }
  1147. else
  1148. { /* No counter roll over during reading of CNTL and CNTH registers, counter
  1149. value is equal to first value of CNTL and CNTH */
  1150. timecounter = (((uint32_t) high1 << 16U) | low);
  1151. }
  1152. return timecounter;
  1153. }
  1154. /**
  1155. * @brief Write the time counter in RTC_CNT registers.
  1156. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1157. * the configuration information for RTC.
  1158. * @param TimeCounter: Counter to write in RTC_CNT registers
  1159. * @retval HAL status
  1160. */
  1161. static HAL_StatusTypeDef RTC_WriteTimeCounter(RTC_HandleTypeDef* hrtc, uint32_t TimeCounter)
  1162. {
  1163. HAL_StatusTypeDef status = HAL_OK;
  1164. /* Set Initialization mode */
  1165. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  1166. {
  1167. status = HAL_ERROR;
  1168. }
  1169. else
  1170. {
  1171. /* Set RTC COUNTER MSB word */
  1172. WRITE_REG(hrtc->Instance->CNTH, (TimeCounter >> 16U));
  1173. /* Set RTC COUNTER LSB word */
  1174. WRITE_REG(hrtc->Instance->CNTL, (TimeCounter & RTC_CNTL_RTC_CNT));
  1175. /* Wait for synchro */
  1176. if(RTC_ExitInitMode(hrtc) != HAL_OK)
  1177. {
  1178. status = HAL_ERROR;
  1179. }
  1180. }
  1181. return status;
  1182. }
  1183. /**
  1184. * @brief Read the time counter available in RTC_ALR registers.
  1185. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1186. * the configuration information for RTC.
  1187. * @retval Time counter
  1188. */
  1189. static uint32_t RTC_ReadAlarmCounter(RTC_HandleTypeDef* hrtc)
  1190. {
  1191. uint16_t high1 = 0U, low = 0U;
  1192. high1 = READ_REG(hrtc->Instance->ALRH & RTC_CNTH_RTC_CNT);
  1193. low = READ_REG(hrtc->Instance->ALRL & RTC_CNTL_RTC_CNT);
  1194. return (((uint32_t) high1 << 16U) | low);
  1195. }
  1196. /**
  1197. * @brief Write the time counter in RTC_ALR registers.
  1198. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1199. * the configuration information for RTC.
  1200. * @param AlarmCounter: Counter to write in RTC_ALR registers
  1201. * @retval HAL status
  1202. */
  1203. static HAL_StatusTypeDef RTC_WriteAlarmCounter(RTC_HandleTypeDef* hrtc, uint32_t AlarmCounter)
  1204. {
  1205. HAL_StatusTypeDef status = HAL_OK;
  1206. /* Set Initialization mode */
  1207. if(RTC_EnterInitMode(hrtc) != HAL_OK)
  1208. {
  1209. status = HAL_ERROR;
  1210. }
  1211. else
  1212. {
  1213. /* Set RTC COUNTER MSB word */
  1214. WRITE_REG(hrtc->Instance->ALRH, (AlarmCounter >> 16U));
  1215. /* Set RTC COUNTER LSB word */
  1216. WRITE_REG(hrtc->Instance->ALRL, (AlarmCounter & RTC_ALRL_RTC_ALR));
  1217. /* Wait for synchro */
  1218. if(RTC_ExitInitMode(hrtc) != HAL_OK)
  1219. {
  1220. status = HAL_ERROR;
  1221. }
  1222. }
  1223. return status;
  1224. }
  1225. /**
  1226. * @brief Enters the RTC Initialization mode.
  1227. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1228. * the configuration information for RTC.
  1229. * @retval HAL status
  1230. */
  1231. static HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef* hrtc)
  1232. {
  1233. uint32_t tickstart = 0U;
  1234. tickstart = HAL_GetTick();
  1235. /* Wait till RTC is in INIT state and if Time out is reached exit */
  1236. while((hrtc->Instance->CRL & RTC_CRL_RTOFF) == (uint32_t)RESET)
  1237. {
  1238. if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1239. {
  1240. return HAL_TIMEOUT;
  1241. }
  1242. }
  1243. /* Disable the write protection for RTC registers */
  1244. __HAL_RTC_WRITEPROTECTION_DISABLE(hrtc);
  1245. return HAL_OK;
  1246. }
  1247. /**
  1248. * @brief Exit the RTC Initialization mode.
  1249. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1250. * the configuration information for RTC.
  1251. * @retval HAL status
  1252. */
  1253. static HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef* hrtc)
  1254. {
  1255. uint32_t tickstart = 0U;
  1256. /* Disable the write protection for RTC registers */
  1257. __HAL_RTC_WRITEPROTECTION_ENABLE(hrtc);
  1258. tickstart = HAL_GetTick();
  1259. /* Wait till RTC is in INIT state and if Time out is reached exit */
  1260. while((hrtc->Instance->CRL & RTC_CRL_RTOFF) == (uint32_t)RESET)
  1261. {
  1262. if((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
  1263. {
  1264. return HAL_TIMEOUT;
  1265. }
  1266. }
  1267. return HAL_OK;
  1268. }
  1269. /**
  1270. * @brief Converts a 2 digit decimal to BCD format.
  1271. * @param Value: Byte to be converted
  1272. * @retval Converted byte
  1273. */
  1274. static uint8_t RTC_ByteToBcd2(uint8_t Value)
  1275. {
  1276. uint32_t bcdhigh = 0U;
  1277. while(Value >= 10U)
  1278. {
  1279. bcdhigh++;
  1280. Value -= 10U;
  1281. }
  1282. return ((uint8_t)(bcdhigh << 4U) | Value);
  1283. }
  1284. /**
  1285. * @brief Converts from 2 digit BCD to Binary.
  1286. * @param Value: BCD value to be converted
  1287. * @retval Converted word
  1288. */
  1289. static uint8_t RTC_Bcd2ToByte(uint8_t Value)
  1290. {
  1291. uint32_t tmp = 0U;
  1292. tmp = ((uint8_t)(Value & (uint8_t)0xF0) >> (uint8_t)0x4) * 10U;
  1293. return (tmp + (Value & (uint8_t)0x0F));
  1294. }
  1295. /**
  1296. * @brief Updates date when time is 23:59:59.
  1297. * @param hrtc pointer to a RTC_HandleTypeDef structure that contains
  1298. * the configuration information for RTC.
  1299. * @param DayElapsed: Number of days elapsed from last date update
  1300. * @retval None
  1301. */
  1302. static void RTC_DateUpdate(RTC_HandleTypeDef* hrtc, uint32_t DayElapsed)
  1303. {
  1304. uint32_t year = 0U, month = 0U, day = 0U;
  1305. uint32_t loop = 0U;
  1306. /* Get the current year*/
  1307. year = hrtc->DateToUpdate.Year;
  1308. /* Get the current month and day */
  1309. month = hrtc->DateToUpdate.Month;
  1310. day = hrtc->DateToUpdate.Date;
  1311. for (loop = 0U; loop < DayElapsed; loop++)
  1312. {
  1313. if((month == 1U) || (month == 3U) || (month == 5U) || (month == 7U) || \
  1314. (month == 8U) || (month == 10U) || (month == 12U))
  1315. {
  1316. if(day < 31U)
  1317. {
  1318. day++;
  1319. }
  1320. /* Date structure member: day = 31 */
  1321. else
  1322. {
  1323. if(month != 12U)
  1324. {
  1325. month++;
  1326. day = 1U;
  1327. }
  1328. /* Date structure member: day = 31 & month =12 */
  1329. else
  1330. {
  1331. month = 1U;
  1332. day = 1U;
  1333. year++;
  1334. }
  1335. }
  1336. }
  1337. else if((month == 4U) || (month == 6U) || (month == 9U) || (month == 11U))
  1338. {
  1339. if(day < 30U)
  1340. {
  1341. day++;
  1342. }
  1343. /* Date structure member: day = 30 */
  1344. else
  1345. {
  1346. month++;
  1347. day = 1U;
  1348. }
  1349. }
  1350. else if(month == 2U)
  1351. {
  1352. if(day < 28U)
  1353. {
  1354. day++;
  1355. }
  1356. else if(day == 28U)
  1357. {
  1358. /* Leap year */
  1359. if(RTC_IsLeapYear(year))
  1360. {
  1361. day++;
  1362. }
  1363. else
  1364. {
  1365. month++;
  1366. day = 1U;
  1367. }
  1368. }
  1369. else if(day == 29U)
  1370. {
  1371. month++;
  1372. day = 1U;
  1373. }
  1374. }
  1375. }
  1376. /* Update year */
  1377. hrtc->DateToUpdate.Year = year;
  1378. /* Update day and month */
  1379. hrtc->DateToUpdate.Month = month;
  1380. hrtc->DateToUpdate.Date = day;
  1381. /* Update day of the week */
  1382. hrtc->DateToUpdate.WeekDay = RTC_WeekDayNum(year, month, day);
  1383. }
  1384. /**
  1385. * @brief Check whether the passed year is Leap or not.
  1386. * @param nYear year to check
  1387. * @retval 1: leap year
  1388. * 0: not leap year
  1389. */
  1390. static uint8_t RTC_IsLeapYear(uint16_t nYear)
  1391. {
  1392. if((nYear % 4U) != 0U)
  1393. {
  1394. return 0U;
  1395. }
  1396. if((nYear % 100U) != 0U)
  1397. {
  1398. return 1U;
  1399. }
  1400. if((nYear % 400U) == 0U)
  1401. {
  1402. return 1U;
  1403. }
  1404. else
  1405. {
  1406. return 0U;
  1407. }
  1408. }
  1409. /**
  1410. * @brief Determines the week number, the day number and the week day number.
  1411. * @param nYear year to check
  1412. * @param nMonth Month to check
  1413. * @param nDay Day to check
  1414. * @note Day is calculated with hypothesis that year > 2000
  1415. * @retval Value which can take one of the following parameters:
  1416. * @arg RTC_WEEKDAY_MONDAY
  1417. * @arg RTC_WEEKDAY_TUESDAY
  1418. * @arg RTC_WEEKDAY_WEDNESDAY
  1419. * @arg RTC_WEEKDAY_THURSDAY
  1420. * @arg RTC_WEEKDAY_FRIDAY
  1421. * @arg RTC_WEEKDAY_SATURDAY
  1422. * @arg RTC_WEEKDAY_SUNDAY
  1423. */
  1424. static uint8_t RTC_WeekDayNum(uint32_t nYear, uint8_t nMonth, uint8_t nDay)
  1425. {
  1426. uint32_t year = 0U, weekday = 0U;
  1427. year = 2000U + nYear;
  1428. if(nMonth < 3U)
  1429. {
  1430. /*D = { [(23 x month)/9] + day + 4 + year + [(year-1)/4] - [(year-1)/100] + [(year-1)/400] } mod 7*/
  1431. weekday = (((23U * nMonth)/9U) + nDay + 4U + year + ((year-1U)/4U) - ((year-1U)/100U) + ((year-1U)/400U)) % 7U;
  1432. }
  1433. else
  1434. {
  1435. /*D = { [(23 x month)/9] + day + 4 + year + [year/4] - [year/100] + [year/400] - 2 } mod 7*/
  1436. weekday = (((23U * nMonth)/9U) + nDay + 4U + year + (year/4U) - (year/100U) + (year/400U) - 2U ) % 7U;
  1437. }
  1438. return (uint8_t)weekday;
  1439. }
  1440. /**
  1441. * @}
  1442. */
  1443. #endif /* HAL_RTC_MODULE_ENABLED */
  1444. /**
  1445. * @}
  1446. */
  1447. /**
  1448. * @}
  1449. */
  1450. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/