cancel
Showing results for 
Search instead for 
Did you mean: 

Daylight Saving: The Details?

Lars Beiderbecke
Senior III

This is a spiritual followup to this post: https://community.st.com/s/question/0D50X00009XkYuH/rtc-daylight-saving-time-stm32f3-discovery

So I do get which functions to use for daylight saving, and how to BCK bit remembers if I already changed to DST or not.

But:

  1. Since I have to call either function manually, how do I compute "second Sunday in March" or "last Sunday in March"? If I check the date every (running) minute, how can I count Sundays (in particular if the board was powered off during the first Sunday)? Do I need to revert to time() etc.?
  2. Wouldn't frequent checks drain performance? Is there no better way, e.g., by using callbacks? Should I set alerts for the DST dates?
  3. If the BCK bit has to be set again after each boot, how do I know if it should be set of not? By comparing the current date against the DST dates? But then I could power down the board just before DST start, and power on again after it. Then the BCK bit would be set, but the time would not have been adjusted, which is wrong.

Calling daylight saving functions seems easy enough, but how to implement this efficiently? Note that the F7 HAL libraries don't include daylight savings examples.

16 REPLIES 16

I am sure, there are libraries out there. Look e.g. Gnu C Library "calendar time" . If you do not need to do it for a random date, the STM32 RTC can help you. It has a field day of week. So check for each sunday, check for march or october check for date between 25 and 31 and set or reset the flag perhaps.

That's also a great idea!

Well, I hit an unexpected snag here: Functions RTC_DayLightSavingConfig and GetStoreOperation() are unknown to the STM32F7xx HAL. The constants are all upper-case, but that was easy to fix.

So how do F7s configure Daylight Saving? I noticed that RTC_TimeTypeDef contains a field DayLightSaving, but I'm not if this is more than information.

These are SPL functions. You can simply copy/paste them from 'F4 SPL (AFAIK there is no 'F7 SPL).

JW

/** @defgroup RTC_Group5 Daylight Saving configuration functions
 *  @brief   Daylight Saving configuration functions 
 *
@verbatim   
 ===============================================================================
              ##### Daylight Saving configuration functions #####
 ===============================================================================  
 
 [..] This section provide functions allowing to configure the RTC DayLight Saving.
 
@endverbatim
  * @{
  */
 
/**
  * @brief  Adds or substract one hour from the current time.
  * @param  RTC_DayLightSaveOperation: the value of hour adjustment. 
  *          This parameter can be one of the following values:
  *            @arg RTC_DayLightSaving_SUB1H: Substract one hour (winter time)
  *            @arg RTC_DayLightSaving_ADD1H: Add one hour (summer time)
  * @param  RTC_StoreOperation: Specifies the value to be written in the BCK bit 
  *                            in CR register to store the operation.
  *          This parameter can be one of the following values:
  *            @arg RTC_StoreOperation_Reset: BCK Bit Reset
  *            @arg RTC_StoreOperation_Set: BCK Bit Set
  * @retval None
  */
void RTC_DayLightSavingConfig(uint32_t RTC_DayLightSaving, uint32_t RTC_StoreOperation)
{
  /* Check the parameters */
  assert_param(IS_RTC_DAYLIGHT_SAVING(RTC_DayLightSaving));
  assert_param(IS_RTC_STORE_OPERATION(RTC_StoreOperation));
 
  /* Disable the write protection for RTC registers */
  RTC->WPR = 0xCA;
  RTC->WPR = 0x53;
 
  /* Clear the bits to be configured */
  RTC->CR &= (uint32_t)~(RTC_CR_BCK);
 
  /* Configure the RTC_CR register */
  RTC->CR |= (uint32_t)(RTC_DayLightSaving | RTC_StoreOperation);
 
  /* Enable the write protection for RTC registers */
  RTC->WPR = 0xFF; 
}
 
/**
  * @brief  Returns the RTC Day Light Saving stored operation.
  * @param  None
  * @retval RTC Day Light Saving stored operation.
  *          - RTC_StoreOperation_Reset
  *          - RTC_StoreOperation_Set       
  */
uint32_t RTC_GetStoreOperation(void)
{
  return (RTC->CR & RTC_CR_BCK);
}
 
/**
  * @}
  */
 

 As was discussed in that thread, this only sets and reads the RTC_CR.BCK bit [edit] and RTC_CR.ADD1H/SUB1H bits[/edit].

JW

Thanks, Jan, this is very useful.

While initially I didn't want to make use of these functions, it's actually hard to add 1 hour manually to any date and time .

The only reasonable way is to convert date/time to some epoch (the easiest is to use the <time.h> facilities to convert to-from UNIX epoch), perform any time shift required, and then convert back.

Note, that the logic required to subtract 1 hour, overflowing correctly to date, is missing (SUB1H bit description says, "Setting this bit has no effect when current hour is 0.").

JW