STM32L4xx_HAL_Driver  1.14.0
stm32l4xx_hal_rtc.c
Go to the documentation of this file.
1 
167 /* Includes ------------------------------------------------------------------*/
168 #include "stm32l4xx_hal.h"
169 
180 #ifdef HAL_RTC_MODULE_ENABLED
181 
182 /* Private typedef -----------------------------------------------------------*/
183 /* Private define ------------------------------------------------------------*/
184 /* Private macro -------------------------------------------------------------*/
185 /* Private variables ---------------------------------------------------------*/
186 /* Private function prototypes -----------------------------------------------*/
187 /* Exported functions --------------------------------------------------------*/
188 
232 HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
233 {
234  HAL_StatusTypeDef status = HAL_ERROR;
235 
236  /* Check the RTC peripheral state */
237  if (hrtc != NULL)
238  {
239  /* Check the parameters */
240  assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
241  assert_param(IS_RTC_HOUR_FORMAT(hrtc->Init.HourFormat));
242  assert_param(IS_RTC_ASYNCH_PREDIV(hrtc->Init.AsynchPrediv));
243  assert_param(IS_RTC_SYNCH_PREDIV(hrtc->Init.SynchPrediv));
244  assert_param(IS_RTC_OUTPUT(hrtc->Init.OutPut));
245  assert_param(IS_RTC_OUTPUT_REMAP(hrtc->Init.OutPutRemap));
246  assert_param(IS_RTC_OUTPUT_POL(hrtc->Init.OutPutPolarity));
247  assert_param(IS_RTC_OUTPUT_TYPE(hrtc->Init.OutPutType));
248 #if defined(STM32L412xx) || defined(STM32L422xx)
249  assert_param(IS_RTC_OUTPUT_PULLUP(hrtc->Init.OutPutPullUp));
250 #endif
251 
252 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
253  if (hrtc->State == HAL_RTC_STATE_RESET)
254  {
255  /* Allocate lock resource and initialize it */
256  hrtc->Lock = HAL_UNLOCKED;
257  hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */
258  hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback; /* Legacy weak AlarmBEventCallback */
259  hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback; /* Legacy weak TimeStampEventCallback */
260  hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
261 #if defined(RTC_TAMPER1_SUPPORT)
262  hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */
263 #endif /* RTC_TAMPER1_SUPPORT */
264  hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback; /* Legacy weak Tamper2EventCallback */
265 #if defined(RTC_TAMPER3_SUPPORT)
266  hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback; /* Legacy weak Tamper3EventCallback */
267 #endif /* RTC_TAMPER3_SUPPORT */
268 
269  if (hrtc->MspInitCallback == NULL)
270  {
272  }
273  /* Init the low level hardware */
274  hrtc->MspInitCallback(hrtc);
275 
276  if (hrtc->MspDeInitCallback == NULL)
277  {
279  }
280  }
281 #else /* #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
282  if (hrtc->State == HAL_RTC_STATE_RESET)
283  {
284  /* Allocate lock resource and initialize it */
285  hrtc->Lock = HAL_UNLOCKED;
286 
287  /* Initialize RTC MSP */
288  HAL_RTC_MspInit(hrtc);
289  }
290 #endif /* #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
291 
292 #if defined(STM32L412xx) || defined(STM32L422xx)
293  /* Process TAMP ip offset from RTC one */
294  hrtc->TampOffset = (TAMP_BASE - RTC_BASE);
295 #endif
296  /* Set RTC state */
297  hrtc->State = HAL_RTC_STATE_BUSY;
298 
299  /* Disable the write protection for RTC registers */
301 
302  /* Enter Initialization mode */
303  status = RTC_EnterInitMode(hrtc);
304 
305  if (status == HAL_OK)
306  {
307 #if defined(STM32L412xx) || defined(STM32L422xx)
308  /* Clear RTC_CR FMT, OSEL, POL and TAMPOE Bits */
309  hrtc->Instance->CR &= ~(RTC_CR_FMT | RTC_CR_POL | RTC_CR_OSEL | RTC_CR_TAMPOE);
310 #else
311  /* Clear RTC_CR FMT, OSEL and POL Bits */
312  hrtc->Instance->CR &= ~(RTC_CR_FMT | RTC_CR_OSEL | RTC_CR_POL);
313 #endif
314  /* Set RTC_CR register */
315  hrtc->Instance->CR |= (hrtc->Init.HourFormat | hrtc->Init.OutPut | hrtc->Init.OutPutPolarity);
316 
317  /* Configure the RTC PRER */
318  hrtc->Instance->PRER = (hrtc->Init.SynchPrediv);
319  hrtc->Instance->PRER |= (hrtc->Init.AsynchPrediv << RTC_PRER_PREDIV_A_Pos);
320 
321  /* Exit Initialization mode */
322  status = RTC_ExitInitMode(hrtc);
323 
324  if (status == HAL_OK)
325  {
326 #if defined(STM32L412xx) || defined(STM32L422xx)
327  hrtc->Instance->CR &= ~(RTC_CR_TAMPALRM_PU | RTC_CR_TAMPALRM_TYPE | RTC_CR_OUT2EN);
328  hrtc->Instance->CR |= (hrtc->Init.OutPutPullUp | hrtc->Init.OutPutType | hrtc->Init.OutPutRemap);
329 #else
330  hrtc->Instance->OR &= ~(RTC_OR_ALARMOUTTYPE | RTC_OR_OUT_RMP);
331  hrtc->Instance->OR |= (hrtc->Init.OutPutType | hrtc->Init.OutPutRemap);
332 #endif
333 
334  /* Enable the write protection for RTC registers */
336 
337  if (status == HAL_OK)
338  {
339  hrtc->State = HAL_RTC_STATE_READY;
340  }
341  }
342  }
343  }
344 
345  return status;
346 }
347 
354 HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
355 {
356  HAL_StatusTypeDef status = HAL_ERROR;
357 
358  /* Check the RTC peripheral state */
359  if (hrtc != NULL)
360  {
361  /* Check the parameters */
362  assert_param(IS_RTC_ALL_INSTANCE(hrtc->Instance));
363 
364  /* Set RTC state */
365  hrtc->State = HAL_RTC_STATE_BUSY;
366 
367  /* Disable the write protection for RTC registers */
369 
370  /* Enter Initialization mode */
371  status = RTC_EnterInitMode(hrtc);
372 
373  if (status == HAL_OK)
374  {
375  /* Reset all RTC CR register bits */
376  hrtc->Instance->TR = 0x00000000U;
377  hrtc->Instance->DR = ((uint32_t)(RTC_DR_WDU_0 | RTC_DR_MU_0 | RTC_DR_DU_0));
378  hrtc->Instance->CR &= 0x00000000U;
379 
380  hrtc->Instance->WUTR = RTC_WUTR_WUT;
381  hrtc->Instance->PRER = ((uint32_t)(RTC_PRER_PREDIV_A | 0x000000FFU));
382  hrtc->Instance->ALRMAR = 0x00000000U;
383  hrtc->Instance->ALRMBR = 0x00000000U;
384  hrtc->Instance->SHIFTR = 0x00000000U;
385  hrtc->Instance->CALR = 0x00000000U;
386  hrtc->Instance->ALRMASSR = 0x00000000U;
387  hrtc->Instance->ALRMBSSR = 0x00000000U;
388 
389  /* Exit initialization mode */
390  status = RTC_ExitInitMode(hrtc);
391 
392 
393  if (status == HAL_OK)
394  {
395 #if defined(STM32L412xx) || defined(STM32L422xx)
396  /* Reset TAMP registers */
397  ((TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset))->CR1 = 0xFFFF0000U;
398  ((TAMP_TypeDef *)((uint32_t)hrtc->Instance + hrtc->TampOffset))->CR2 = 0x00000000U;
399 #else
400  /* Reset Tamper configuration register */
401  hrtc->Instance->TAMPCR = 0x00000000U;
402 
403  /* Reset Option register */
404  hrtc->Instance->OR = 0x00000000U;
405 #endif
406 
407  /* Enable the write protection for RTC registers */
409 
410 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
411  if (hrtc->MspDeInitCallback == NULL)
412  {
414  }
415 
416  /* DeInit the low level hardware: CLOCK, NVIC.*/
417  hrtc->MspDeInitCallback(hrtc);
418 #else
419  /* De-Initialize RTC MSP */
420  HAL_RTC_MspDeInit(hrtc);
421 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS) */
422 
423  hrtc->State = HAL_RTC_STATE_RESET;
424 
425  /* Release Lock */
426  __HAL_UNLOCK(hrtc);
427  }
428  }
429  }
430 
431  return status;
432 }
433 
434 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
435 
454 {
455  HAL_StatusTypeDef status = HAL_OK;
456 
457  if (pCallback == NULL)
458  {
459  return HAL_ERROR;
460  }
461 
462  /* Process locked */
463  __HAL_LOCK(hrtc);
464 
465  if (HAL_RTC_STATE_READY == hrtc->State)
466  {
467  switch (CallbackID)
468  {
470  hrtc->AlarmAEventCallback = pCallback;
471  break;
472 
474  hrtc->AlarmBEventCallback = pCallback;
475  break;
476 
478  hrtc->TimeStampEventCallback = pCallback;
479  break;
480 
482  hrtc->WakeUpTimerEventCallback = pCallback;
483  break;
484 
485 #if defined(RTC_TAMPER1_SUPPORT)
487  hrtc->Tamper1EventCallback = pCallback;
488  break;
489 #endif /* RTC_TAMPER1_SUPPORT */
490 
492  hrtc->Tamper2EventCallback = pCallback;
493  break;
494 
495 #if defined(RTC_TAMPER3_SUPPORT)
497  hrtc->Tamper3EventCallback = pCallback;
498  break;
499 #endif /* RTC_TAMPER3_SUPPORT */
500 
501  case HAL_RTC_MSPINIT_CB_ID :
502  hrtc->MspInitCallback = pCallback;
503  break;
504 
506  hrtc->MspDeInitCallback = pCallback;
507  break;
508 
509  default :
510  /* Return error status */
511  status = HAL_ERROR;
512  break;
513  }
514  }
515  else if (HAL_RTC_STATE_RESET == hrtc->State)
516  {
517  switch (CallbackID)
518  {
519  case HAL_RTC_MSPINIT_CB_ID :
520  hrtc->MspInitCallback = pCallback;
521  break;
522 
524  hrtc->MspDeInitCallback = pCallback;
525  break;
526 
527  default :
528  /* Return error status */
529  status = HAL_ERROR;
530  break;
531  }
532  }
533  else
534  {
535  /* Return error status */
536  status = HAL_ERROR;
537  }
538 
539  /* Release Lock */
540  __HAL_UNLOCK(hrtc);
541 
542  return status;
543 }
544 
563 {
564  HAL_StatusTypeDef status = HAL_OK;
565 
566  /* Process locked */
567  __HAL_LOCK(hrtc);
568 
569  if (HAL_RTC_STATE_READY == hrtc->State)
570  {
571  switch (CallbackID)
572  {
574  hrtc->AlarmAEventCallback = HAL_RTC_AlarmAEventCallback; /* Legacy weak AlarmAEventCallback */
575  break;
576 
578  hrtc->AlarmBEventCallback = HAL_RTCEx_AlarmBEventCallback; /* Legacy weak AlarmBEventCallback */
579  break;
580 
582  hrtc->TimeStampEventCallback = HAL_RTCEx_TimeStampEventCallback; /* Legacy weak TimeStampEventCallback */
583  break;
584 
586  hrtc->WakeUpTimerEventCallback = HAL_RTCEx_WakeUpTimerEventCallback; /* Legacy weak WakeUpTimerEventCallback */
587  break;
588 
589 #if defined(RTC_TAMPER1_SUPPORT)
591  hrtc->Tamper1EventCallback = HAL_RTCEx_Tamper1EventCallback; /* Legacy weak Tamper1EventCallback */
592  break;
593 #endif /* RTC_TAMPER1_SUPPORT */
594 
596  hrtc->Tamper2EventCallback = HAL_RTCEx_Tamper2EventCallback; /* Legacy weak Tamper2EventCallback */
597  break;
598 
599 #if defined(RTC_TAMPER3_SUPPORT)
601  hrtc->Tamper3EventCallback = HAL_RTCEx_Tamper3EventCallback; /* Legacy weak Tamper3EventCallback */
602  break;
603 #endif /* RTC_TAMPER3_SUPPORT */
604 
605  case HAL_RTC_MSPINIT_CB_ID :
607  break;
608 
611  break;
612 
613  default :
614  /* Return error status */
615  status = HAL_ERROR;
616  break;
617  }
618  }
619  else if (HAL_RTC_STATE_RESET == hrtc->State)
620  {
621  switch (CallbackID)
622  {
623  case HAL_RTC_MSPINIT_CB_ID :
625  break;
626 
629  break;
630 
631  default :
632  /* Return error status */
633  status = HAL_ERROR;
634  break;
635  }
636  }
637  else
638  {
639  /* Return error status */
640  status = HAL_ERROR;
641  }
642 
643  /* Release Lock */
644  __HAL_UNLOCK(hrtc);
645 
646  return status;
647 }
648 #endif /* USE_HAL_RTC_REGISTER_CALLBACKS */
649 
656 {
657  /* Prevent unused argument(s) compilation warning */
658  UNUSED(hrtc);
659 
660  /* NOTE : This function should not be modified, when the callback is needed,
661  the HAL_RTC_MspInit could be implemented in the user file
662  */
663 }
664 
671 {
672  /* Prevent unused argument(s) compilation warning */
673  UNUSED(hrtc);
674 
675  /* NOTE : This function should not be modified, when the callback is needed,
676  the HAL_RTC_MspDeInit could be implemented in the user file
677  */
678 }
679 
708 HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
709 {
710  uint32_t tmpreg;
711  HAL_StatusTypeDef status;
712 
713  /* Check the parameters */
714  assert_param(IS_RTC_FORMAT(Format));
715  assert_param(IS_RTC_DAYLIGHT_SAVING(sTime->DayLightSaving));
716  assert_param(IS_RTC_STORE_OPERATION(sTime->StoreOperation));
717 
718  /* Process Locked */
719  __HAL_LOCK(hrtc);
720 
721  hrtc->State = HAL_RTC_STATE_BUSY;
722 
723  /* Disable the write protection for RTC registers */
725 
726  /* Enter Initialization mode */
727  status = RTC_EnterInitMode(hrtc);
728  if (status == HAL_OK)
729  {
730  if (Format == RTC_FORMAT_BIN)
731  {
732  if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
733  {
734  assert_param(IS_RTC_HOUR12(sTime->Hours));
735  assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
736  }
737  else
738  {
739  sTime->TimeFormat = 0x00U;
740  assert_param(IS_RTC_HOUR24(sTime->Hours));
741  }
742  assert_param(IS_RTC_MINUTES(sTime->Minutes));
743  assert_param(IS_RTC_SECONDS(sTime->Seconds));
744 
745  tmpreg = (uint32_t)(((uint32_t)RTC_ByteToBcd2(sTime->Hours) << RTC_TR_HU_Pos) | \
746  ((uint32_t)RTC_ByteToBcd2(sTime->Minutes) << RTC_TR_MNU_Pos) | \
747  ((uint32_t)RTC_ByteToBcd2(sTime->Seconds) << RTC_TR_SU_Pos) | \
748  (((uint32_t)sTime->TimeFormat) << RTC_TR_PM_Pos));
749  }
750  else
751  {
752  if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
753  {
754  assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sTime->Hours)));
755  assert_param(IS_RTC_HOURFORMAT12(sTime->TimeFormat));
756  }
757  else
758  {
759  sTime->TimeFormat = 0x00U;
760  assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sTime->Hours)));
761  }
762  assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sTime->Minutes)));
763  assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sTime->Seconds)));
764  tmpreg = (((uint32_t)(sTime->Hours) << RTC_TR_HU_Pos) | \
765  ((uint32_t)(sTime->Minutes) << RTC_TR_MNU_Pos) | \
766  ((uint32_t)(sTime->Seconds) << RTC_TR_SU_Pos) | \
767  ((uint32_t)(sTime->TimeFormat) << RTC_TR_PM_Pos));
768  }
769 
770  /* Set the RTC_TR register */
771  hrtc->Instance->TR = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
772 
773  /* Clear the bits to be configured */
774  hrtc->Instance->CR &= ((uint32_t)~RTC_CR_BKP);
775 
776  /* Configure the RTC_CR register */
777  hrtc->Instance->CR |= (uint32_t)(sTime->DayLightSaving | sTime->StoreOperation);
778 
779  /* Exit Initialization mode */
780  status = RTC_ExitInitMode(hrtc);
781  }
782 
783  /* Enable the write protection for RTC registers */
785 
786  if (status == HAL_OK)
787  {
788  hrtc->State = HAL_RTC_STATE_READY;
789  }
790 
791  /* Process Unlocked */
792  __HAL_UNLOCK(hrtc);
793 
794  return status;
795 }
796 
818 HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
819 {
820  uint32_t tmpreg;
821 
822  /* Check the parameters */
823  assert_param(IS_RTC_FORMAT(Format));
824 
825  /* Get subseconds structure field from the corresponding register*/
826  sTime->SubSeconds = (uint32_t)(hrtc->Instance->SSR);
827 
828  /* Get SecondFraction structure field from the corresponding register field*/
829  sTime->SecondFraction = (uint32_t)(hrtc->Instance->PRER & RTC_PRER_PREDIV_S);
830 
831  /* Get the TR register */
832  tmpreg = (uint32_t)(hrtc->Instance->TR & RTC_TR_RESERVED_MASK);
833 
834  /* Fill the structure fields with the read parameters */
835  sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> RTC_TR_HU_Pos);
836  sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >> RTC_TR_MNU_Pos);
837  sTime->Seconds = (uint8_t)((tmpreg & (RTC_TR_ST | RTC_TR_SU)) >> RTC_TR_SU_Pos);
838  sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> RTC_TR_PM_Pos);
839 
840  /* Check the input parameters format */
841  if (Format == RTC_FORMAT_BIN)
842  {
843  /* Convert the time structure parameters to Binary format */
844  sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
845  sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
846  sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
847  }
848 
849  return HAL_OK;
850 }
851 
862 HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
863 {
864  uint32_t datetmpreg;
865  HAL_StatusTypeDef status;
866 
867  /* Check the parameters */
868  assert_param(IS_RTC_FORMAT(Format));
869 
870  /* Process Locked */
871  __HAL_LOCK(hrtc);
872 
873  hrtc->State = HAL_RTC_STATE_BUSY;
874 
875  if ((Format == RTC_FORMAT_BIN) && ((sDate->Month & 0x10U) == 0x10U))
876  {
877  sDate->Month = (uint8_t)((sDate->Month & (uint8_t)~(0x10U)) + (uint8_t)0x0AU);
878  }
879 
880  assert_param(IS_RTC_WEEKDAY(sDate->WeekDay));
881 
882  if (Format == RTC_FORMAT_BIN)
883  {
884  assert_param(IS_RTC_YEAR(sDate->Year));
885  assert_param(IS_RTC_MONTH(sDate->Month));
886  assert_param(IS_RTC_DATE(sDate->Date));
887 
888  datetmpreg = (((uint32_t)RTC_ByteToBcd2(sDate->Year) << RTC_DR_YU_Pos) | \
889  ((uint32_t)RTC_ByteToBcd2(sDate->Month) << RTC_DR_MU_Pos) | \
890  ((uint32_t)RTC_ByteToBcd2(sDate->Date) << RTC_DR_DU_Pos) | \
891  ((uint32_t)sDate->WeekDay << RTC_DR_WDU_Pos));
892  }
893  else
894  {
895  assert_param(IS_RTC_YEAR(RTC_Bcd2ToByte(sDate->Year)));
896  assert_param(IS_RTC_MONTH(RTC_Bcd2ToByte(sDate->Month)));
897  assert_param(IS_RTC_DATE(RTC_Bcd2ToByte(sDate->Date)));
898 
899  datetmpreg = ((((uint32_t)sDate->Year) << RTC_DR_YU_Pos) | \
900  (((uint32_t)sDate->Month) << RTC_DR_MU_Pos) | \
901  (((uint32_t)sDate->Date) << RTC_DR_DU_Pos) | \
902  (((uint32_t)sDate->WeekDay) << RTC_DR_WDU_Pos));
903  }
904 
905  /* Disable the write protection for RTC registers */
907 
908  /* Enter Initialization mode */
909  status = RTC_EnterInitMode(hrtc);
910  if (status == HAL_OK)
911  {
912  /* Set the RTC_DR register */
913  hrtc->Instance->DR = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
914 
915  /* Exit Initialization mode */
916  status = RTC_ExitInitMode(hrtc);
917  }
918 
919  /* Enable the write protection for RTC registers */
921 
922  if (status == HAL_OK)
923  {
924  hrtc->State = HAL_RTC_STATE_READY ;
925  }
926 
927  /* Process Unlocked */
928  __HAL_UNLOCK(hrtc);
929 
930  return status;
931 }
932 
946 HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
947 {
948  uint32_t datetmpreg;
949 
950  /* Check the parameters */
951  assert_param(IS_RTC_FORMAT(Format));
952 
953  /* Get the DR register */
954  datetmpreg = (uint32_t)(hrtc->Instance->DR & RTC_DR_RESERVED_MASK);
955 
956  /* Fill the structure fields with the read parameters */
957  sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> RTC_DR_YU_Pos);
958  sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> RTC_DR_MU_Pos);
959  sDate->Date = (uint8_t)((datetmpreg & (RTC_DR_DT | RTC_DR_DU)) >> RTC_DR_DU_Pos);
960  sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> RTC_DR_WDU_Pos);
961 
962  /* Check the input parameters format */
963  if (Format == RTC_FORMAT_BIN)
964  {
965  /* Convert the date structure parameters to Binary format */
966  sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
967  sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
968  sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
969  }
970  return HAL_OK;
971 }
972 
1000 HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1001 {
1002  uint32_t tmpreg, subsecondtmpreg;
1003 
1004  /* Check the parameters */
1005  assert_param(IS_RTC_FORMAT(Format));
1006  assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1007  assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1008  assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1009  assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1010  assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1011 
1012  /* Process Locked */
1013  __HAL_LOCK(hrtc);
1014 
1015  hrtc->State = HAL_RTC_STATE_BUSY;
1016 
1017  if (Format == RTC_FORMAT_BIN)
1018  {
1019  if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1020  {
1021  assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1022  assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1023  }
1024  else
1025  {
1026  sAlarm->AlarmTime.TimeFormat = 0x00U;
1027  assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1028  }
1029  assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1030  assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1031 
1032  if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1033  {
1034  assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1035  }
1036  else
1037  {
1038  assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1039  }
1040  tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1041  ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1042  ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1043  ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1044  ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1045  ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1046  ((uint32_t)sAlarm->AlarmMask));
1047  }
1048  else
1049  {
1050  if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1051  {
1052  assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1053  assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1054  }
1055  else
1056  {
1057  sAlarm->AlarmTime.TimeFormat = 0x00U;
1058  assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1059  }
1060 
1061  assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1062  assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1063 
1064 #ifdef USE_FULL_ASSERT
1065  if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1066  {
1067  assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1068  }
1069  else
1070  {
1071  assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1072  }
1073 
1074 #endif /* USE_FULL_ASSERT */
1075  tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1076  ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1077  ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1078  ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1079  ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1080  ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1081  ((uint32_t)sAlarm->AlarmMask));
1082  }
1083 
1084  /* Configure the Alarm A or Alarm B Sub Second registers */
1085  subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
1086 
1087  /* Disable the write protection for RTC registers */
1089 
1090  /* Configure the Alarm register */
1091  if (sAlarm->Alarm == RTC_ALARM_A)
1092  {
1093  /* Disable the Alarm A interrupt */
1094  __HAL_RTC_ALARMA_DISABLE(hrtc);
1095  /* Clear flag alarm A */
1096  __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1097  /* In case of interrupt mode is used, the interrupt source must disabled */
1098  __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
1099 
1100 #if defined (RTC_FLAG_ALRAWF)
1101  uint32_t tickstart = HAL_GetTick();
1102  /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
1103  while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
1104  {
1105  if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1106  {
1107  /* Enable the write protection for RTC registers */
1109 
1110  hrtc->State = HAL_RTC_STATE_TIMEOUT;
1111 
1112  /* Process Unlocked */
1113  __HAL_UNLOCK(hrtc);
1114 
1115  return HAL_TIMEOUT;
1116  }
1117  }
1118 #endif
1119 
1120  hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
1121  /* Configure the Alarm A Sub Second register */
1122  hrtc->Instance->ALRMASSR = subsecondtmpreg;
1123  /* Configure the Alarm state: Enable Alarm */
1124  __HAL_RTC_ALARMA_ENABLE(hrtc);
1125  }
1126  else
1127  {
1128  /* Disable the Alarm B interrupt */
1129  __HAL_RTC_ALARMB_DISABLE(hrtc);
1130  /* Clear flag alarm B */
1131  __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1132  /* In case of interrupt mode is used, the interrupt source must disabled */
1133  __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
1134 
1135 #if defined (RTC_FLAG_ALRBWF)
1136  uint32_t tickstart = HAL_GetTick();
1137  /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
1138  while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
1139  {
1140  if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1141  {
1142  /* Enable the write protection for RTC registers */
1144 
1145  hrtc->State = HAL_RTC_STATE_TIMEOUT;
1146 
1147  /* Process Unlocked */
1148  __HAL_UNLOCK(hrtc);
1149 
1150  return HAL_TIMEOUT;
1151  }
1152  }
1153 #endif
1154 
1155  hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
1156  /* Configure the Alarm B Sub Second register */
1157  hrtc->Instance->ALRMBSSR = subsecondtmpreg;
1158  /* Configure the Alarm state: Enable Alarm */
1159  __HAL_RTC_ALARMB_ENABLE(hrtc);
1160  }
1161 
1162  /* Enable the write protection for RTC registers */
1164 
1165  /* Change RTC state */
1166  hrtc->State = HAL_RTC_STATE_READY;
1167 
1168  /* Process Unlocked */
1169  __HAL_UNLOCK(hrtc);
1170 
1171  return HAL_OK;
1172 }
1173 
1187 HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1188 {
1189  uint32_t tmpreg, subsecondtmpreg;
1190 
1191  /* Check the parameters */
1192  assert_param(IS_RTC_FORMAT(Format));
1193  assert_param(IS_RTC_ALARM(sAlarm->Alarm));
1194  assert_param(IS_RTC_ALARM_MASK(sAlarm->AlarmMask));
1195  assert_param(IS_RTC_ALARM_DATE_WEEKDAY_SEL(sAlarm->AlarmDateWeekDaySel));
1196  assert_param(IS_RTC_ALARM_SUB_SECOND_VALUE(sAlarm->AlarmTime.SubSeconds));
1197  assert_param(IS_RTC_ALARM_SUB_SECOND_MASK(sAlarm->AlarmSubSecondMask));
1198 
1199  /* Process Locked */
1200  __HAL_LOCK(hrtc);
1201 
1202  hrtc->State = HAL_RTC_STATE_BUSY;
1203 
1204  if (Format == RTC_FORMAT_BIN)
1205  {
1206  if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1207  {
1208  assert_param(IS_RTC_HOUR12(sAlarm->AlarmTime.Hours));
1209  assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1210  }
1211  else
1212  {
1213  sAlarm->AlarmTime.TimeFormat = 0x00U;
1214  assert_param(IS_RTC_HOUR24(sAlarm->AlarmTime.Hours));
1215  }
1216  assert_param(IS_RTC_MINUTES(sAlarm->AlarmTime.Minutes));
1217  assert_param(IS_RTC_SECONDS(sAlarm->AlarmTime.Seconds));
1218 
1219  if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1220  {
1221  assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(sAlarm->AlarmDateWeekDay));
1222  }
1223  else
1224  {
1225  assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(sAlarm->AlarmDateWeekDay));
1226  }
1227 
1228  tmpreg = (((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1229  ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1230  ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1231  ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1232  ((uint32_t)RTC_ByteToBcd2(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1233  ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1234  ((uint32_t)sAlarm->AlarmMask));
1235  }
1236  else
1237  {
1238  if ((hrtc->Instance->CR & RTC_CR_FMT) != 0U)
1239  {
1240  assert_param(IS_RTC_HOUR12(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1241  assert_param(IS_RTC_HOURFORMAT12(sAlarm->AlarmTime.TimeFormat));
1242  }
1243  else
1244  {
1245  sAlarm->AlarmTime.TimeFormat = 0x00U;
1246  assert_param(IS_RTC_HOUR24(RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours)));
1247  }
1248 
1249  assert_param(IS_RTC_MINUTES(RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes)));
1250  assert_param(IS_RTC_SECONDS(RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds)));
1251 
1252 #ifdef USE_FULL_ASSERT
1253  if (sAlarm->AlarmDateWeekDaySel == RTC_ALARMDATEWEEKDAYSEL_DATE)
1254  {
1255  assert_param(IS_RTC_ALARM_DATE_WEEKDAY_DATE(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1256  }
1257  else
1258  {
1259  assert_param(IS_RTC_ALARM_DATE_WEEKDAY_WEEKDAY(RTC_Bcd2ToByte(sAlarm->AlarmDateWeekDay)));
1260  }
1261 
1262 #endif /* USE_FULL_ASSERT */
1263  tmpreg = (((uint32_t)(sAlarm->AlarmTime.Hours) << RTC_ALRMAR_HU_Pos) | \
1264  ((uint32_t)(sAlarm->AlarmTime.Minutes) << RTC_ALRMAR_MNU_Pos) | \
1265  ((uint32_t)(sAlarm->AlarmTime.Seconds) << RTC_ALRMAR_SU_Pos) | \
1266  ((uint32_t)(sAlarm->AlarmTime.TimeFormat) << RTC_ALRMAR_PM_Pos) | \
1267  ((uint32_t)(sAlarm->AlarmDateWeekDay) << RTC_ALRMAR_DU_Pos) | \
1268  ((uint32_t)sAlarm->AlarmDateWeekDaySel) | \
1269  ((uint32_t)sAlarm->AlarmMask));
1270  }
1271  /* Configure the Alarm A or Alarm B Sub Second registers */
1272  subsecondtmpreg = (uint32_t)((uint32_t)(sAlarm->AlarmTime.SubSeconds) | (uint32_t)(sAlarm->AlarmSubSecondMask));
1273 
1274  /* Disable the write protection for RTC registers */
1276 
1277  /* Configure the Alarm register */
1278  if (sAlarm->Alarm == RTC_ALARM_A)
1279  {
1280  /* Disable the Alarm A interrupt */
1281  __HAL_RTC_ALARMA_DISABLE(hrtc);
1282 
1283  /* Clear flag alarm A */
1284  __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1285 
1286 #if defined (RTC_FLAG_ALRAWF)
1287  uint32_t tickstart = HAL_GetTick();
1288  /* Wait till RTC ALRAWF flag is set and if Time out is reached exit */
1289  while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
1290  {
1291  if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1292  {
1293  /* Enable the write protection for RTC registers */
1295 
1296  hrtc->State = HAL_RTC_STATE_TIMEOUT;
1297 
1298  /* Process Unlocked */
1299  __HAL_UNLOCK(hrtc);
1300 
1301  return HAL_TIMEOUT;
1302  }
1303  }
1304 #endif
1305 
1306  hrtc->Instance->ALRMAR = (uint32_t)tmpreg;
1307  /* Configure the Alarm A Sub Second register */
1308  hrtc->Instance->ALRMASSR = subsecondtmpreg;
1309  /* Configure the Alarm state: Enable Alarm */
1310  __HAL_RTC_ALARMA_ENABLE(hrtc);
1311  /* Configure the Alarm interrupt */
1312  __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRA);
1313  }
1314  else
1315  {
1316  /* Disable the Alarm B interrupt */
1317  __HAL_RTC_ALARMB_DISABLE(hrtc);
1318 
1319  /* Clear flag alarm B */
1320  __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1321 
1322 #if defined (RTC_FLAG_ALRBWF)
1323  uint32_t tickstart = HAL_GetTick();
1324  /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
1325  while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
1326  {
1327  if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1328  {
1329  /* Enable the write protection for RTC registers */
1331 
1332  hrtc->State = HAL_RTC_STATE_TIMEOUT;
1333 
1334  /* Process Unlocked */
1335  __HAL_UNLOCK(hrtc);
1336 
1337  return HAL_TIMEOUT;
1338  }
1339  }
1340 #endif
1341 
1342  hrtc->Instance->ALRMBR = (uint32_t)tmpreg;
1343  /* Configure the Alarm B Sub Second register */
1344  hrtc->Instance->ALRMBSSR = subsecondtmpreg;
1345  /* Configure the Alarm state: Enable Alarm */
1346  __HAL_RTC_ALARMB_ENABLE(hrtc);
1347  /* Configure the Alarm interrupt */
1348  __HAL_RTC_ALARM_ENABLE_IT(hrtc, RTC_IT_ALRB);
1349  }
1350 
1351  /* RTC Alarm Interrupt Configuration: EXTI configuration */
1352  __HAL_RTC_ALARM_EXTI_ENABLE_IT();
1353  __HAL_RTC_ALARM_EXTI_ENABLE_RISING_EDGE();
1354 
1355  /* Enable the write protection for RTC registers */
1357 
1358  hrtc->State = HAL_RTC_STATE_READY;
1359 
1360  /* Process Unlocked */
1361  __HAL_UNLOCK(hrtc);
1362 
1363  return HAL_OK;
1364 }
1365 
1375 HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
1376 {
1377  /* Check the parameters */
1378  assert_param(IS_RTC_ALARM(Alarm));
1379 
1380  /* Process Locked */
1381  __HAL_LOCK(hrtc);
1382 
1383  hrtc->State = HAL_RTC_STATE_BUSY;
1384 
1385  /* Disable the write protection for RTC registers */
1387 
1388  if (Alarm == RTC_ALARM_A)
1389  {
1390  /* AlarmA */
1391  __HAL_RTC_ALARMA_DISABLE(hrtc);
1392 
1393  /* In case of interrupt mode is used, the interrupt source must disabled */
1394  __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRA);
1395 
1396 #if defined (RTC_FLAG_ALRAWF)
1397  uint32_t tickstart = HAL_GetTick();
1398  /* Wait till RTC ALRxWF flag is set and if Time out is reached exit */
1399  while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAWF) == 0U)
1400  {
1401  if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1402  {
1403  /* Enable the write protection for RTC registers */
1405 
1406  hrtc->State = HAL_RTC_STATE_TIMEOUT;
1407 
1408  /* Process Unlocked */
1409  __HAL_UNLOCK(hrtc);
1410 
1411  return HAL_TIMEOUT;
1412  }
1413  }
1414 #endif
1415  }
1416  else
1417  {
1418  /* AlarmB */
1419  __HAL_RTC_ALARMB_DISABLE(hrtc);
1420 
1421  /* In case of interrupt mode is used, the interrupt source must disabled */
1422  __HAL_RTC_ALARM_DISABLE_IT(hrtc, RTC_IT_ALRB);
1423 
1424 #if defined (RTC_FLAG_ALRBWF)
1425  uint32_t tickstart = HAL_GetTick();
1426  /* Wait till RTC ALRBWF flag is set and if Time out is reached exit */
1427  while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBWF) == 0U)
1428  {
1429  if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1430  {
1431  /* Enable the write protection for RTC registers */
1433 
1434  hrtc->State = HAL_RTC_STATE_TIMEOUT;
1435 
1436  /* Process Unlocked */
1437  __HAL_UNLOCK(hrtc);
1438 
1439  return HAL_TIMEOUT;
1440  }
1441  }
1442 #endif
1443  }
1444  /* Enable the write protection for RTC registers */
1446 
1447  hrtc->State = HAL_RTC_STATE_READY;
1448 
1449  /* Process Unlocked */
1450  __HAL_UNLOCK(hrtc);
1451 
1452  return HAL_OK;
1453 }
1454 
1469 HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
1470 {
1471  uint32_t tmpreg, subsecondtmpreg;
1472 
1473  /* Check the parameters */
1474  assert_param(IS_RTC_FORMAT(Format));
1475  assert_param(IS_RTC_ALARM(Alarm));
1476 
1477  if (Alarm == RTC_ALARM_A)
1478  {
1479  /* AlarmA */
1480  sAlarm->Alarm = RTC_ALARM_A;
1481 
1482  tmpreg = (uint32_t)(hrtc->Instance->ALRMAR);
1483  subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMASSR) & RTC_ALRMASSR_SS);
1484 
1485  /* Fill the structure with the read parameters */
1486  sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMAR_HT | RTC_ALRMAR_HU)) >> RTC_ALRMAR_HU_Pos);
1487  sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMAR_MNT | RTC_ALRMAR_MNU)) >> RTC_ALRMAR_MNU_Pos);
1488  sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMAR_ST | RTC_ALRMAR_SU)) >> RTC_ALRMAR_SU_Pos);
1489  sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMAR_PM) >> RTC_ALRMAR_PM_Pos);
1490  sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1491  sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMAR_DT | RTC_ALRMAR_DU)) >> RTC_ALRMAR_DU_Pos);
1492  sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMAR_WDSEL);
1493  sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1494  }
1495  else
1496  {
1497  sAlarm->Alarm = RTC_ALARM_B;
1498 
1499  tmpreg = (uint32_t)(hrtc->Instance->ALRMBR);
1500  subsecondtmpreg = (uint32_t)((hrtc->Instance->ALRMBSSR) & RTC_ALRMBSSR_SS);
1501 
1502  /* Fill the structure with the read parameters */
1503  sAlarm->AlarmTime.Hours = (uint8_t)((tmpreg & (RTC_ALRMBR_HT | RTC_ALRMBR_HU)) >> RTC_ALRMBR_HU_Pos);
1504  sAlarm->AlarmTime.Minutes = (uint8_t)((tmpreg & (RTC_ALRMBR_MNT | RTC_ALRMBR_MNU)) >> RTC_ALRMBR_MNU_Pos);
1505  sAlarm->AlarmTime.Seconds = (uint8_t)((tmpreg & (RTC_ALRMBR_ST | RTC_ALRMBR_SU)) >> RTC_ALRMBR_SU_Pos);
1506  sAlarm->AlarmTime.TimeFormat = (uint8_t)((tmpreg & RTC_ALRMBR_PM) >> RTC_ALRMBR_PM_Pos);
1507  sAlarm->AlarmTime.SubSeconds = (uint32_t) subsecondtmpreg;
1508  sAlarm->AlarmDateWeekDay = (uint8_t)((tmpreg & (RTC_ALRMBR_DT | RTC_ALRMBR_DU)) >> RTC_ALRMBR_DU_Pos);
1509  sAlarm->AlarmDateWeekDaySel = (uint32_t)(tmpreg & RTC_ALRMBR_WDSEL);
1510  sAlarm->AlarmMask = (uint32_t)(tmpreg & RTC_ALARMMASK_ALL);
1511  }
1512 
1513  if (Format == RTC_FORMAT_BIN)
1514  {
1515  sAlarm->AlarmTime.Hours = RTC_Bcd2ToByte(sAlarm->AlarmTime.Hours);
1516  sAlarm->AlarmTime.Minutes = RTC_Bcd2ToByte(sAlarm->AlarmTime.Minutes);
1517  sAlarm->AlarmTime.Seconds = RTC_Bcd2ToByte(sAlarm->AlarmTime.Seconds);
1519  }
1520 
1521  return HAL_OK;
1522 }
1523 
1530 {
1531  /* Clear the EXTI's line Flag for RTC Alarm */
1532  __HAL_RTC_ALARM_EXTI_CLEAR_FLAG();
1533 
1534 #if defined(STM32L412xx) || defined(STM32L422xx)
1535  /* Get interrupt status */
1536  uint32_t tmp = hrtc->Instance->MISR;
1537 
1538  if ((tmp & RTC_MISR_ALRAMF) != 0u)
1539  {
1540  /* Clear the AlarmA interrupt pending bit */
1541  hrtc->Instance->SCR = RTC_SCR_CALRAF;
1542 
1543 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1544  /* Call Compare Match registered Callback */
1545  hrtc->AlarmAEventCallback(hrtc);
1546 #else /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1548 #endif /* (USE_HAL_RTC_REGISTER_CALLBACKS == 1) */
1549  }
1550 
1551  if ((tmp & RTC_MISR_ALRBMF) != 0u)
1552  {
1553  /* Clear the AlarmB interrupt pending bit */
1554  hrtc->Instance->SCR = RTC_SCR_CALRBF;
1555 
1556 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1557  /* Call Compare Match registered Callback */
1558  hrtc->AlarmBEventCallback(hrtc);
1559 #else
1561 #endif
1562  }
1563 
1564 #else /* #if defined(STM32L412xx) || defined(STM32L422xx) */
1565 
1566  /* Get the AlarmA interrupt source enable status */
1567  if (__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRA) != 0U)
1568  {
1569  /* Get the pending status of the AlarmA Interrupt */
1570  if (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) != 0U)
1571  {
1572  /* Clear the AlarmA interrupt pending bit */
1573  __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1574 
1575 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1576  hrtc->AlarmAEventCallback(hrtc);
1577 #else
1579 #endif
1580  }
1581  }
1582 
1583  /* Get the AlarmB interrupt source enable status */
1584  if (__HAL_RTC_ALARM_GET_IT_SOURCE(hrtc, RTC_IT_ALRB) != 0U)
1585  {
1586  /* Get the pending status of the AlarmB Interrupt */
1587  if (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRBF) != 0U)
1588  {
1589  /* Clear the AlarmB interrupt pending bit */
1590  __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRBF);
1591 
1592 #if (USE_HAL_RTC_REGISTER_CALLBACKS == 1)
1593  hrtc->AlarmBEventCallback(hrtc);
1594 #else
1596 #endif
1597  }
1598  }
1599 #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) */
1600 
1601  /* Change RTC state */
1602  hrtc->State = HAL_RTC_STATE_READY;
1603 }
1604 
1611 {
1612  /* Prevent unused argument(s) compilation warning */
1613  UNUSED(hrtc);
1614 
1615  /* NOTE : This function should not be modified, when the callback is needed,
1616  the HAL_RTC_AlarmAEventCallback could be implemented in the user file
1617  */
1618 }
1619 
1626 HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
1627 {
1628 
1629  uint32_t tickstart = HAL_GetTick();
1630 
1631  while (__HAL_RTC_ALARM_GET_FLAG(hrtc, RTC_FLAG_ALRAF) == 0U)
1632  {
1633  if (Timeout != HAL_MAX_DELAY)
1634  {
1635  if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
1636  {
1637  hrtc->State = HAL_RTC_STATE_TIMEOUT;
1638  return HAL_TIMEOUT;
1639  }
1640  }
1641  }
1642 
1643  /* Clear the Alarm interrupt pending bit */
1644  __HAL_RTC_ALARM_CLEAR_FLAG(hrtc, RTC_FLAG_ALRAF);
1645 
1646  /* Change RTC state */
1647  hrtc->State = HAL_RTC_STATE_READY;
1648 
1649  return HAL_OK;
1650 }
1651 
1686 {
1687  uint32_t tickstart;
1688 
1689  /* Clear RSF flag */
1690 #if defined(STM32L412xx) || defined(STM32L422xx)
1691  hrtc->Instance->ICSR &= (uint32_t)RTC_RSF_MASK;
1692 #else
1693  hrtc->Instance->ISR &= (uint32_t)RTC_RSF_MASK;
1694 #endif
1695 
1696  tickstart = HAL_GetTick();
1697 
1698  /* Wait the registers to be synchronised */
1699 #if defined(STM32L412xx) || defined(STM32L422xx)
1700  while ((hrtc->Instance->ICSR & RTC_ICSR_RSF) == 0U)
1701 #else
1702  while ((hrtc->Instance->ISR & RTC_ISR_RSF) == 0U)
1703 #endif
1704  {
1705  if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1706  {
1707  return HAL_TIMEOUT;
1708  }
1709  }
1710 
1711  return HAL_OK;
1712 }
1713 
1738 {
1739  /* Return RTC handle state */
1740  return hrtc->State;
1741 }
1742 
1761 HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
1762 {
1763  uint32_t tickstart;
1764  HAL_StatusTypeDef status = HAL_OK;
1765 
1766  /* Check if the Initialization mode is set */
1767 #if defined(STM32L412xx) || defined(STM32L422xx)
1768  if ((hrtc->Instance->ICSR & RTC_ICSR_INITF) == 0U)
1769  {
1770  /* Set the Initialization mode */
1771  SET_BIT(hrtc->Instance->ICSR, RTC_ICSR_INIT);
1772 
1773  tickstart = HAL_GetTick();
1774  /* Wait till RTC is in INIT state and if Time out is reached exit */
1775  while ((READ_BIT(hrtc->Instance->ICSR, RTC_ICSR_INITF) == 0U) && (status != HAL_TIMEOUT))
1776  {
1777  if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1778  {
1779  status = HAL_TIMEOUT;
1780  hrtc->State = HAL_RTC_STATE_TIMEOUT;
1781  }
1782  }
1783  }
1784 #else /* #if defined(STM32L412xx) || defined(STM32L422xx) */
1785  if ((hrtc->Instance->ISR & RTC_ISR_INITF) == 0U)
1786  {
1787  /* Set the Initialization mode */
1788  hrtc->Instance->ISR = (uint32_t)RTC_INIT_MASK;
1789 
1790  tickstart = HAL_GetTick();
1791  /* Wait till RTC is in INIT state and if Time out is reached exit */
1792  while ((READ_BIT(hrtc->Instance->ISR, RTC_ISR_INITF) == 0U) && (status != HAL_TIMEOUT))
1793  {
1794  if ((HAL_GetTick() - tickstart) > RTC_TIMEOUT_VALUE)
1795  {
1796  status = HAL_TIMEOUT;
1797  hrtc->State = HAL_RTC_STATE_TIMEOUT;
1798  }
1799  }
1800  }
1801 #endif /* #if defined(STM32L412xx) || defined(STM32L422xx) */
1802 
1803  return status;
1804 }
1805 
1811 HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef *hrtc)
1812 {
1813  HAL_StatusTypeDef status = HAL_OK;
1814 
1815  /* Exit Initialization mode */
1816 #if defined(STM32L412xx) || defined(STM32L422xx)
1817  CLEAR_BIT(RTC->ICSR, RTC_ICSR_INIT);
1818 #else
1819  /* Exit Initialization mode */
1820  CLEAR_BIT(RTC->ISR, RTC_ISR_INIT);
1821 #endif
1822 
1823  /* If CR_BYPSHAD bit = 0, wait for synchro */
1824  if (READ_BIT(RTC->CR, RTC_CR_BYPSHAD) == 0U)
1825  {
1826  if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
1827  {
1828  hrtc->State = HAL_RTC_STATE_TIMEOUT;
1829  status = HAL_TIMEOUT;
1830  }
1831  }
1832  else /* WA 2.9.6 Calendar initialization may fail in case of consecutive INIT mode entry */
1833  {
1834  /* Clear BYPSHAD bit */
1835  CLEAR_BIT(RTC->CR, RTC_CR_BYPSHAD);
1836  if (HAL_RTC_WaitForSynchro(hrtc) != HAL_OK)
1837  {
1838  hrtc->State = HAL_RTC_STATE_TIMEOUT;
1839  status = HAL_TIMEOUT;
1840  }
1841  /* Restore BYPSHAD bit */
1842  SET_BIT(RTC->CR, RTC_CR_BYPSHAD);
1843  }
1844 
1845  return status;
1846 }
1847 
1848 
1849 
1855 uint8_t RTC_ByteToBcd2(uint8_t Value)
1856 {
1857  uint32_t bcdhigh = 0U;
1858  uint8_t temp = Value;
1859 
1860  while (temp >= 10U)
1861  {
1862  bcdhigh++;
1863  temp -= 10U;
1864  }
1865 
1866  return ((uint8_t)(bcdhigh << 4U) | temp);
1867 }
1868 
1874 uint8_t RTC_Bcd2ToByte(uint8_t Value)
1875 {
1876  uint8_t tmp;
1877  tmp = ((Value & 0xF0U) >> 4U) * 10U;
1878  return (tmp + (Value & 0x0FU));
1879 }
1880 
1885 #endif /* HAL_RTC_MODULE_ENABLED */
1886 
1894 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
HAL_StatusTypeDef HAL_RTC_DeInit(RTC_HandleTypeDef *hrtc)
DeInitialize the RTC peripheral.
HAL_StatusTypeDef HAL_RTC_Init(RTC_HandleTypeDef *hrtc)
Initialize the RTC peripheral.
void(* Tamper3EventCallback)(struct __RTC_HandleTypeDef *hrtc)
RTC Alarm structure definition.
HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
Set RTC current time.
RTC_TimeTypeDef AlarmTime
HAL_StatusTypeDef HAL_RTC_PollForAlarmAEvent(RTC_HandleTypeDef *hrtc, uint32_t Timeout)
Handle AlarmA Polling request.
HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef *hrtc)
Wait until the RTC Time and Date registers (RTC_TR and RTC_DR) are synchronized with RTC APB clock...
This file contains all the functions prototypes for the HAL module driver.
void(* AlarmAEventCallback)(struct __RTC_HandleTypeDef *hrtc)
HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
Get RTC current date.
__IO HAL_RTCStateTypeDef State
void(* Tamper2EventCallback)(struct __RTC_HandleTypeDef *hrtc)
void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
__HAL_RTC_WRITEPROTECTION_DISABLE(hrtc)
uint32_t HAL_GetTick(void)
Provide a tick value in millisecond.
void HAL_RTC_AlarmIRQHandler(RTC_HandleTypeDef *hrtc)
Handle Alarm interrupt request.
__HAL_UNLOCK(hrtc)
void HAL_RTCEx_Tamper1EventCallback(RTC_HandleTypeDef *hrtc)
uint8_t RTC_ByteToBcd2(uint8_t Value)
Convert a 2 digit decimal to BCD format.
CLEAR_BIT(hrtc->Instance->CR, RTC_CR_WUTE)
RTC Time structure definition.
__HAL_LOCK(hrtc)
HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
Set the specified RTC Alarm with Interrupt.
return HAL_OK
void(* MspInitCallback)(struct __RTC_HandleTypeDef *hrtc)
HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
Set the specified RTC Alarm.
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
Alarm A callback.
HAL_RTCStateTypeDef
HAL State structures definition.
HAL_RTC_CallbackIDTypeDef
HAL RTC Callback ID enumeration definition.
HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
Set RTC current date.
void(* pRTC_CallbackTypeDef)(RTC_HandleTypeDef *hrtc)
HAL RTC Callback pointer definition.
void HAL_RTCEx_Tamper2EventCallback(RTC_HandleTypeDef *hrtc)
void(* TimeStampEventCallback)(struct __RTC_HandleTypeDef *hrtc)
HAL_LockTypeDef Lock
void HAL_RTCEx_TimeStampEventCallback(RTC_HandleTypeDef *hrtc)
TimeStamp callback.
void(* Tamper1EventCallback)(struct __RTC_HandleTypeDef *hrtc)
void(* MspDeInitCallback)(struct __RTC_HandleTypeDef *hrtc)
void HAL_RTCEx_Tamper3EventCallback(RTC_HandleTypeDef *hrtc)
RTC_InitTypeDef Init
void(* AlarmBEventCallback)(struct __RTC_HandleTypeDef *hrtc)
void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc)
DeInitialize the RTC MSP.
void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
Initialize the RTC MSP.
HAL_RTCStateTypeDef HAL_RTC_GetState(RTC_HandleTypeDef *hrtc)
Return the RTC handle state.
HAL_StatusTypeDef HAL_RTC_UnRegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID)
Unregister an RTC Callback RTC callback is redirected to the weak predefined callback.
HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
Get RTC current time.
uint8_t RTC_Bcd2ToByte(uint8_t Value)
Convert from 2 digit BCD to Binary.
RTC Date structure definition.
HAL_StatusTypeDef HAL_RTC_GetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Alarm, uint32_t Format)
Get the RTC Alarm value and masks.
__HAL_RTC_WRITEPROTECTION_ENABLE(hrtc)
HAL_StatusTypeDef HAL_RTC_RegisterCallback(RTC_HandleTypeDef *hrtc, HAL_RTC_CallbackIDTypeDef CallbackID, pRTC_CallbackTypeDef pCallback)
Register a User RTC Callback To be used instead of the weak predefined callback.
HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef *hrtc)
Exit the RTC Initialization mode.
void(* WakeUpTimerEventCallback)(struct __RTC_HandleTypeDef *hrtc)
RTC Handle Structure definition.
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))
void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
uint32_t AlarmDateWeekDaySel
HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
Enter the RTC Initialization mode.
HAL_StatusTypeDef HAL_RTC_DeactivateAlarm(RTC_HandleTypeDef *hrtc, uint32_t Alarm)
Deactivate the specified RTC Alarm.