chore: convert RTC to HAL (#2866)
* chore: switch RTC to HAL * chore: remove unneeded files * fix simu/simulator
This commit is contained in:
@@ -19,28 +19,26 @@
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#include "board.h"
|
||||
#include "stm32_hal.h"
|
||||
#include "rtc.h"
|
||||
|
||||
RTC_HandleTypeDef rtc = {};
|
||||
|
||||
void rtcSetTime(const struct gtm * t)
|
||||
{
|
||||
g_ms100 = 0; // start of next second begins now
|
||||
|
||||
RTC_TimeTypeDef RTC_TimeStruct;
|
||||
RTC_DateTypeDef RTC_DateStruct;
|
||||
RTC_TimeTypeDef RTC_TimeStruct = {};
|
||||
RTC_DateTypeDef RTC_DateStruct = {};
|
||||
|
||||
RTC_TimeStructInit(&RTC_TimeStruct);
|
||||
RTC_DateStructInit(&RTC_DateStruct);
|
||||
|
||||
RTC_TimeStruct.RTC_Hours = t->tm_hour;
|
||||
RTC_TimeStruct.RTC_Minutes = t->tm_min;
|
||||
RTC_TimeStruct.RTC_Seconds = t->tm_sec;
|
||||
RTC_DateStruct.RTC_Year = t->tm_year - 100;
|
||||
RTC_DateStruct.RTC_Month = t->tm_mon + 1;
|
||||
RTC_DateStruct.RTC_Date = t->tm_mday;
|
||||
|
||||
RTC_SetTime(RTC_Format_BIN, &RTC_TimeStruct);
|
||||
RTC_SetDate(RTC_Format_BIN, &RTC_DateStruct);
|
||||
RTC_TimeStruct.Hours = t->tm_hour;
|
||||
RTC_TimeStruct.Minutes = t->tm_min;
|
||||
RTC_TimeStruct.Seconds = t->tm_sec;
|
||||
RTC_DateStruct.Year = t->tm_year - 100;
|
||||
RTC_DateStruct.Month = t->tm_mon + 1;
|
||||
RTC_DateStruct.Date = t->tm_mday;
|
||||
HAL_RTC_SetTime(&rtc, &RTC_TimeStruct, RTC_FORMAT_BIN);
|
||||
HAL_RTC_SetDate(&rtc, &RTC_DateStruct, RTC_FORMAT_BIN);
|
||||
}
|
||||
|
||||
void rtcGetTime(struct gtm * t)
|
||||
@@ -48,50 +46,53 @@ void rtcGetTime(struct gtm * t)
|
||||
RTC_TimeTypeDef RTC_TimeStruct;
|
||||
RTC_DateTypeDef RTC_DateStruct;
|
||||
|
||||
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStruct);
|
||||
RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct);
|
||||
HAL_RTC_GetTime(&rtc, &RTC_TimeStruct, RTC_FORMAT_BIN);
|
||||
HAL_RTC_GetDate(&rtc, &RTC_DateStruct, RTC_FORMAT_BIN);
|
||||
|
||||
t->tm_hour = RTC_TimeStruct.RTC_Hours;
|
||||
t->tm_min = RTC_TimeStruct.RTC_Minutes;
|
||||
t->tm_sec = RTC_TimeStruct.RTC_Seconds;
|
||||
t->tm_year = RTC_DateStruct.RTC_Year + 100; // STM32 year is two decimals only (so base is currently 2000), gtm is based on number of years since 1900
|
||||
t->tm_mon = RTC_DateStruct.RTC_Month - 1;
|
||||
t->tm_mday = RTC_DateStruct.RTC_Date;
|
||||
t->tm_hour = RTC_TimeStruct.Hours;
|
||||
t->tm_min = RTC_TimeStruct.Minutes;
|
||||
t->tm_sec = RTC_TimeStruct.Seconds;
|
||||
t->tm_year = RTC_DateStruct.Year + 100; // STM32 year is two decimals only (so base is currently 2000), gtm is based on number of years since 1900
|
||||
t->tm_mon = RTC_DateStruct.Month - 1;
|
||||
t->tm_mday = RTC_DateStruct.Date;
|
||||
}
|
||||
|
||||
void rtcInit()
|
||||
{
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
|
||||
PWR_BackupAccessCmd(ENABLE);
|
||||
RCC_LSEConfig(RCC_LSE_ON);
|
||||
|
||||
// Prevent lockup in case of 32kHz oscillator failure
|
||||
uint32_t i = 0;
|
||||
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {
|
||||
if (++i > 1000000)
|
||||
return;
|
||||
rtc.Instance = RTC;
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
HAL_PWR_EnableBkUpAccess();
|
||||
|
||||
// Enable LSE Oscillator
|
||||
RCC_OscInitTypeDef RCC_OscInitStruct;
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
|
||||
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
|
||||
|
||||
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) == HAL_OK) {
|
||||
__HAL_RCC_RTC_CLKPRESCALER(RCC_RTCCLKSOURCE_LSE);
|
||||
__HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
|
||||
}
|
||||
|
||||
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
|
||||
RCC_RTCCLKCmd(ENABLE);
|
||||
RTC_WaitForSynchro();
|
||||
|
||||
__HAL_RCC_RTC_ENABLE();
|
||||
HAL_RTC_WaitForSynchro(&rtc);
|
||||
|
||||
#if !defined(BOOT)
|
||||
RTC_InitTypeDef RTC_InitStruct;
|
||||
|
||||
// RTC time base = LSE / ((AsynchPrediv+1) * (SynchPrediv+1)) = 1 Hz*/
|
||||
RTC_InitStruct.RTC_HourFormat = RTC_HourFormat_24;
|
||||
RTC_InitStruct.RTC_AsynchPrediv = 127;
|
||||
RTC_InitStruct.RTC_SynchPrediv = 255;
|
||||
RTC_Init(&RTC_InitStruct);
|
||||
rtc.Init.HourFormat = RTC_HOURFORMAT_24;
|
||||
rtc.Init.AsynchPrediv = 127;
|
||||
rtc.Init.SynchPrediv = 255;
|
||||
rtc.Init.OutPut = RTC_OUTPUT_DISABLE;
|
||||
rtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
|
||||
rtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
|
||||
HAL_RTC_Init(&rtc);
|
||||
|
||||
struct gtm utm;
|
||||
rtcGetTime(&utm);
|
||||
g_rtcTime = gmktime(&utm);
|
||||
#endif
|
||||
|
||||
#if defined(RTC_BACKUP_RAM) && !defined(BOOT)
|
||||
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE);
|
||||
PWR_BackupRegulatorCmd(ENABLE);
|
||||
__HAL_RCC_BKPSRAM_CLK_ENABLE();
|
||||
HAL_PWREx_EnableBkUpReg();
|
||||
#endif
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user