2021-03-08 01:08 AM
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:
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.
Solved! Go to Solution.
2021-03-08 03:15 AM
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.
2021-03-08 03:26 AM
That's also a great idea!
2021-03-08 06:33 AM
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.
2021-03-08 06:36 AM
These are SPL functions. You can simply copy/paste them from 'F4 SPL (AFAIK there is no 'F7 SPL).
JW
2021-03-08 06:40 AM
/** @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
2021-03-08 09:25 AM
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 .
2021-03-08 10:06 AM
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