stm32f1xx_hal_rtc.c 51 KB

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