Skip to content

Commit 4e8f85c

Browse files
committed
STM32F1 RTC : save values in register
1 parent 082ba15 commit 4e8f85c

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
@@ -149,51 +149,15 @@ Information about STM32F1:
149149
For date, there is no specific register, only a software structure.
150150
It is then not a problem to not use shifts.
151151
*/
152-
#if TARGET_STM32F1
153152
time_t rtc_read(void)
154153
{
155-
RTC_DateTypeDef dateStruct = {0};
156-
RTC_TimeTypeDef timeStruct = {0};
157-
struct tm timeinfo;
154+
#if TARGET_STM32F1
158155

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

193159
#else /* TARGET_STM32F1 */
194160

195-
time_t rtc_read(void)
196-
{
197161
struct tm timeinfo;
198162

199163
/* Since the shadow registers are bypassed we have to read the time twice and compare them until both times are the same */
@@ -231,12 +195,23 @@ time_t rtc_read(void)
231195
}
232196

233197
return t;
234-
}
235198

236199
#endif /* TARGET_STM32F1 */
200+
}
201+
202+
237203

238204
void rtc_write(time_t t)
239205
{
206+
#if TARGET_STM32F1
207+
208+
RtcHandle.Instance = RTC;
209+
if (RTC_WriteTimeCounter(&RtcHandle, t) != HAL_OK) {
210+
error("RTC_WriteTimeCounter error\n");
211+
}
212+
213+
#else /* TARGET_STM32F1 */
214+
240215
RTC_DateTypeDef dateStruct = {0};
241216
RTC_TimeTypeDef timeStruct = {0};
242217

@@ -261,12 +236,9 @@ void rtc_write(time_t t)
261236
timeStruct.Hours = timeinfo.tm_hour;
262237
timeStruct.Minutes = timeinfo.tm_min;
263238
timeStruct.Seconds = timeinfo.tm_sec;
264-
265-
#if !(TARGET_STM32F1)
266239
timeStruct.TimeFormat = RTC_HOURFORMAT_24;
267240
timeStruct.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
268241
timeStruct.StoreOperation = RTC_STOREOPERATION_RESET;
269-
#endif /* TARGET_STM32F1 */
270242

271243
#if DEVICE_LPTICKER && !MBED_CONF_TARGET_LPTICKER_LPTIM
272244
/* Need to update LP_continuous_time value before new RTC time */
@@ -293,6 +265,7 @@ void rtc_write(time_t t)
293265
#endif /* DEVICE_LPTICKER && !MBED_CONF_TARGET_LPTICKER_LPTIM */
294266

295267
core_util_critical_section_exit();
268+
#endif /* TARGET_STM32F1 */
296269
}
297270

298271
int rtc_isenabled(void)

0 commit comments

Comments
 (0)