stm32h7xx_hal_rtc.c 50 KB

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