Skip to content

Commit ebff1e5

Browse files
author
Cruz Monrreal
authored
Merge pull request #8213 from jeromecoutant/PR_RTC_F1
STM32F1 RTC : save values in register
2 parents 161ca2b + e8d32ca commit ebff1e5

File tree

3 files changed

+21
-47
lines changed

3 files changed

+21
-47
lines changed

targets/TARGET_STM/TARGET_STM32F1/device/stm32f1xx_hal_rtc.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,6 @@
188188
/** @defgroup RTC_Private_Functions RTC Private Functions
189189
* @{
190190
*/
191-
static uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef* hrtc);
192-
static HAL_StatusTypeDef RTC_WriteTimeCounter(RTC_HandleTypeDef* hrtc, uint32_t TimeCounter);
193191
static uint32_t RTC_ReadAlarmCounter(RTC_HandleTypeDef* hrtc);
194192
static HAL_StatusTypeDef RTC_WriteAlarmCounter(RTC_HandleTypeDef* hrtc, uint32_t AlarmCounter);
195193
static HAL_StatusTypeDef RTC_EnterInitMode(RTC_HandleTypeDef* hrtc);
@@ -1355,7 +1353,7 @@ HAL_StatusTypeDef HAL_RTC_WaitForSynchro(RTC_HandleTypeDef* hrtc)
13551353
* the configuration information for RTC.
13561354
* @retval Time counter
13571355
*/
1358-
static uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef* hrtc)
1356+
uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef* hrtc)
13591357
{
13601358
uint16_t high1 = 0U, high2 = 0U, low = 0U;
13611359
uint32_t timecounter = 0U;
@@ -1385,10 +1383,10 @@ static uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef* hrtc)
13851383
* @param TimeCounter: Counter to write in RTC_CNT registers
13861384
* @retval HAL status
13871385
*/
1388-
static HAL_StatusTypeDef RTC_WriteTimeCounter(RTC_HandleTypeDef* hrtc, uint32_t TimeCounter)
1386+
HAL_StatusTypeDef RTC_WriteTimeCounter(RTC_HandleTypeDef* hrtc, uint32_t TimeCounter)
13891387
{
13901388
HAL_StatusTypeDef status = HAL_OK;
1391-
1389+
13921390
/* Set Initialization mode */
13931391
if(RTC_EnterInitMode(hrtc) != HAL_OK)
13941392
{

targets/TARGET_STM/TARGET_STM32F1/device/stm32f1xx_hal_rtc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,9 @@ HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTim
518518
HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format);
519519
HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format);
520520
HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format);
521+
522+
uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef* hrtc);
523+
HAL_StatusTypeDef RTC_WriteTimeCounter(RTC_HandleTypeDef* hrtc, uint32_t TimeCounter);
521524
/**
522525
* @}
523526
*/

targets/TARGET_STM/rtc_api.c

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -141,51 +141,15 @@ Information about STM32F1:
141141
For date, there is no specific register, only a software structure.
142142
It is then not a problem to not use shifts.
143143
*/
144-
#if TARGET_STM32F1
145144
time_t rtc_read(void)
146145
{
147-
RTC_DateTypeDef dateStruct = {0};
148-
RTC_TimeTypeDef timeStruct = {0};
149-
struct tm timeinfo;
146+
#if TARGET_STM32F1
150147

151148
RtcHandle.Instance = RTC;
152-
153-
// Read actual date and time
154-
// Warning: the time must be read first!
155-
HAL_RTC_GetTime(&RtcHandle, &timeStruct, RTC_FORMAT_BIN);
156-
HAL_RTC_GetDate(&RtcHandle, &dateStruct, RTC_FORMAT_BIN);
157-
158-
/* date information is null before first write procedure */
159-
/* set 01/01/1970 as default values */
160-
if (dateStruct.Year == 0) {
161-
dateStruct.Year = 2 ;
162-
dateStruct.Month = 1 ;
163-
dateStruct.Date = 1 ;
164-
}
165-
166-
// Setup a tm structure based on the RTC
167-
/* tm_wday information is ignored by _rtc_maketime */
168-
/* tm_isdst information is ignored by _rtc_maketime */
169-
timeinfo.tm_mon = dateStruct.Month - 1;
170-
timeinfo.tm_mday = dateStruct.Date;
171-
timeinfo.tm_year = dateStruct.Year + 68;
172-
timeinfo.tm_hour = timeStruct.Hours;
173-
timeinfo.tm_min = timeStruct.Minutes;
174-
timeinfo.tm_sec = timeStruct.Seconds;
175-
176-
// Convert to timestamp
177-
time_t t;
178-
if (_rtc_maketime(&timeinfo, &t, RTC_4_YEAR_LEAP_YEAR_SUPPORT) == false) {
179-
return 0;
180-
}
181-
182-
return t;
183-
}
149+
return RTC_ReadTimeCounter(&RtcHandle);
184150

185151
#else /* TARGET_STM32F1 */
186152

187-
time_t rtc_read(void)
188-
{
189153
struct tm timeinfo;
190154

191155
/* Since the shadow registers are bypassed we have to read the time twice and compare them until both times are the same */
@@ -223,12 +187,23 @@ time_t rtc_read(void)
223187
}
224188

225189
return t;
226-
}
227190

228191
#endif /* TARGET_STM32F1 */
192+
}
193+
194+
229195

230196
void rtc_write(time_t t)
231197
{
198+
#if TARGET_STM32F1
199+
200+
RtcHandle.Instance = RTC;
201+
if (RTC_WriteTimeCounter(&RtcHandle, t) != HAL_OK) {
202+
error("RTC_WriteTimeCounter error\n");
203+
}
204+
205+
#else /* TARGET_STM32F1 */
206+
232207
RTC_DateTypeDef dateStruct = {0};
233208
RTC_TimeTypeDef timeStruct = {0};
234209

@@ -253,12 +228,9 @@ void rtc_write(time_t t)
253228
timeStruct.Hours = timeinfo.tm_hour;
254229
timeStruct.Minutes = timeinfo.tm_min;
255230
timeStruct.Seconds = timeinfo.tm_sec;
256-
257-
#if !(TARGET_STM32F1)
258231
timeStruct.TimeFormat = RTC_HOURFORMAT_24;
259232
timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
260233
timeStruct.StoreOperation = RTC_STOREOPERATION_RESET;
261-
#endif /* TARGET_STM32F1 */
262234

263235
#if DEVICE_LPTICKER && !MBED_CONF_TARGET_LPTICKER_LPTIM
264236
/* Before setting the new time, we need to update the LPTICKER_counter value */
@@ -280,6 +252,7 @@ void rtc_write(time_t t)
280252
}
281253

282254
core_util_critical_section_exit();
255+
#endif /* TARGET_STM32F1 */
283256
}
284257

285258
int rtc_isenabled(void)

0 commit comments

Comments
 (0)