Skip to content

Commit

Permalink
Updated RTC handling to match core
Browse files Browse the repository at this point in the history
  • Loading branch information
dresco committed Dec 28, 2024
1 parent 4b10d8a commit 2ac0ed6
Showing 1 changed file with 38 additions and 21 deletions.
59 changes: 38 additions & 21 deletions Src/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -2395,33 +2395,28 @@ static bool set_rtc_time (struct tm *time)
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef sDate = {0};

if(!rtc_started)
rtc_started = HAL_RTC_Init(&hrtc) == HAL_OK;

if(rtc_started) {

sTime.Hours = time->tm_hour;
sTime.Minutes = time->tm_min;
sTime.Seconds = time->tm_sec;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) == HAL_OK) {
sDate.WeekDay = RTC_WEEKDAY_MONDAY;
sDate.Month = time->tm_mon + 1;
sDate.Date = time->tm_mday;
sDate.Year = time->tm_year - 100;
HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
}
sTime.Hours = time->tm_hour;
sTime.Minutes = time->tm_min;
sTime.Seconds = time->tm_sec;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
if(HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) == HAL_OK) {
sDate.WeekDay = RTC_WEEKDAY_MONDAY;
sDate.Month = time->tm_mon + 1;
sDate.Date = time->tm_mday;
sDate.Year = time->tm_year - 100;
hal.driver_cap.rtc_set = (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) == HAL_OK);
}


return rtc_started;
}

static bool get_rtc_time (struct tm *time)
{
bool ok = false;

if(rtc_started) {
if(hal.driver_cap.rtc_set) {

RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef sDate = {0};
Expand Down Expand Up @@ -2527,7 +2522,7 @@ bool driver_init (void)
hal.info = "STM32H743";
#endif

hal.driver_version = "241217";
hal.driver_version = "241221";
hal.driver_url = "https://github.com/dresco/STM32H7xx";
#ifdef BOARD_NAME
hal.board = BOARD_NAME;
Expand Down Expand Up @@ -2586,8 +2581,30 @@ bool driver_init (void)
hal.periph_port.set_pin_description = setPeriphPinDescription;

#if RTC_ENABLE
hal.rtc.get_datetime = get_rtc_time;
hal.rtc.set_datetime = set_rtc_time;

RCC_OscInitTypeDef OscInitStruct;

HAL_RCC_GetOscConfig(&OscInitStruct);

if(OscInitStruct.OscillatorType & (RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_LSE)) {

RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {
.PeriphClockSelection = RCC_PERIPHCLK_RTC,
.RTCClockSelection = (OscInitStruct.OscillatorType & RCC_OSCILLATORTYPE_LSI) ? RCC_RTCCLKSOURCE_LSI : RCC_RTCCLKSOURCE_LSE
};

if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) == HAL_OK) {

__HAL_RCC_RTC_ENABLE();

if((hal.driver_cap.rtc = HAL_RTC_Init(&hrtc) == HAL_OK)) {
hal.rtc.get_datetime = get_rtc_time;
hal.rtc.set_datetime = set_rtc_time;
// TODO: read the time and set hal.driver_cap.rtc_set if >= core build date?
}
}
}

#endif

serialRegisterStreams();
Expand Down

0 comments on commit 2ac0ed6

Please sign in to comment.