STM32L4xx_HAL_Driver  1.14.0
RTC Time and Date functions

RTC Time and Date functions. More...

Functions

HAL_StatusTypeDef HAL_RTC_SetTime (RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
 Set RTC current time. More...
 
HAL_StatusTypeDef HAL_RTC_GetTime (RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
 Get RTC current time. More...
 
HAL_StatusTypeDef HAL_RTC_SetDate (RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
 Set RTC current date. More...
 
HAL_StatusTypeDef HAL_RTC_GetDate (RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
 Get RTC current date. More...
 

Detailed Description

RTC Time and Date functions.

 ===============================================================================
                 ##### RTC Time and Date functions #####
 ===============================================================================

 [..] This section provides functions allowing to configure Time and Date features

Function Documentation

◆ HAL_RTC_GetDate()

HAL_StatusTypeDef HAL_RTC_GetDate ( RTC_HandleTypeDef hrtc,
RTC_DateTypeDef sDate,
uint32_t  Format 
)

Get RTC current date.

Note
You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values in the higher-order calendar shadow registers to ensure consistency between the time and date values. Reading RTC current time locks the values in calendar shadow registers until Current date is read.
Parameters
hrtcRTC handle
sDatePointer to Date structure
FormatSpecifies the format of the entered parameters. This parameter can be one of the following values:
  • RTC_FORMAT_BIN: Binary data format
  • RTC_FORMAT_BCD: BCD data format
Return values
HALstatus

Definition at line 946 of file stm32l4xx_hal_rtc.c.

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 }
return HAL_OK
uint8_t RTC_Bcd2ToByte(uint8_t Value)
Convert from 2 digit BCD to Binary.
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))

◆ HAL_RTC_GetTime()

HAL_StatusTypeDef HAL_RTC_GetTime ( RTC_HandleTypeDef hrtc,
RTC_TimeTypeDef sTime,
uint32_t  Format 
)

Get RTC current time.

Note
You can use SubSeconds and SecondFraction (sTime structure fields returned) to convert SubSeconds value in second fraction ratio with time unit following generic formula: Second fraction ratio * time_unit= [(SecondFraction-SubSeconds)/(SecondFraction+1)] * time_unit This conversion can be performed only if no shift operation is pending (ie. SHFP=0) when PREDIV_S >= SS
You must call HAL_RTC_GetDate() after HAL_RTC_GetTime() to unlock the values in the higher-order calendar shadow registers to ensure consistency between the time and date values. Reading RTC current time locks the values in calendar shadow registers until Current date is read to ensure consistency between the time and date values.
Parameters
hrtcRTC handle
sTimePointer to Time structure with Hours, Minutes and Seconds fields returned with input format (BIN or BCD), also SubSeconds field returning the RTC_SSR register content and SecondFraction field the Synchronous pre-scaler factor to be used for second fraction ratio computation.
FormatSpecifies the format of the entered parameters. This parameter can be one of the following values:
  • RTC_FORMAT_BIN: Binary data format
  • RTC_FORMAT_BCD: BCD data format
Return values
HALstatus

Definition at line 818 of file stm32l4xx_hal_rtc.c.

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 }
return HAL_OK
uint8_t RTC_Bcd2ToByte(uint8_t Value)
Convert from 2 digit BCD to Binary.
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))

◆ HAL_RTC_SetDate()

HAL_StatusTypeDef HAL_RTC_SetDate ( RTC_HandleTypeDef hrtc,
RTC_DateTypeDef sDate,
uint32_t  Format 
)

Set RTC current date.

Parameters
hrtcRTC handle
sDatePointer to date structure
Formatspecifies the format of the entered parameters. This parameter can be one of the following values:
  • RTC_FORMAT_BIN: Binary data format
  • RTC_FORMAT_BCD: BCD data format
Return values
HALstatus

Definition at line 862 of file stm32l4xx_hal_rtc.c.

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 }
__IO HAL_RTCStateTypeDef State
__HAL_RTC_WRITEPROTECTION_DISABLE(hrtc)
__HAL_UNLOCK(hrtc)
uint8_t RTC_ByteToBcd2(uint8_t Value)
Convert a 2 digit decimal to BCD format.
__HAL_LOCK(hrtc)
return HAL_OK
uint8_t RTC_Bcd2ToByte(uint8_t Value)
Convert from 2 digit BCD to Binary.
__HAL_RTC_WRITEPROTECTION_ENABLE(hrtc)
HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef *hrtc)
Exit the RTC Initialization mode.
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))
HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
Enter the RTC Initialization mode.

◆ HAL_RTC_SetTime()

HAL_StatusTypeDef HAL_RTC_SetTime ( RTC_HandleTypeDef hrtc,
RTC_TimeTypeDef sTime,
uint32_t  Format 
)

Set RTC current time.

Parameters
hrtcRTC handle
sTimePointer to Time structure
FormatSpecifies the format of the entered parameters. This parameter can be one of the following values:
  • RTC_FORMAT_BIN: Binary data format
  • RTC_FORMAT_BCD: BCD data format
Return values
HALstatus

Definition at line 708 of file stm32l4xx_hal_rtc.c.

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 }
__IO HAL_RTCStateTypeDef State
__HAL_RTC_WRITEPROTECTION_DISABLE(hrtc)
__HAL_UNLOCK(hrtc)
uint8_t RTC_ByteToBcd2(uint8_t Value)
Convert a 2 digit decimal to BCD format.
__HAL_LOCK(hrtc)
return HAL_OK
uint8_t RTC_Bcd2ToByte(uint8_t Value)
Convert from 2 digit BCD to Binary.
__HAL_RTC_WRITEPROTECTION_ENABLE(hrtc)
HAL_StatusTypeDef RTC_ExitInitMode(RTC_HandleTypeDef *hrtc)
Exit the RTC Initialization mode.
assert_param(IS_RTC_WAKEUP_CLOCK(WakeUpClock))
HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef *hrtc)
Enter the RTC Initialization mode.