chore: convert RTC to HAL (#2866)

* chore: switch RTC to HAL

* chore: remove unneeded files

* fix simu/simulator
This commit is contained in:
Malte Langermann
2022-12-23 15:38:37 +10:00
committed by GitHub
parent f9bac7ba9b
commit a1eabf11a5
8 changed files with 64 additions and 80 deletions
@@ -45,7 +45,6 @@
#include "stm32f4xx_tim.h" #include "stm32f4xx_tim.h"
#include "stm32f4xx_adc.h" #include "stm32f4xx_adc.h"
#include "stm32f4xx_spi.h" #include "stm32f4xx_spi.h"
#include "stm32f4xx_rtc.h"
#include "stm32f4xx_pwr.h" #include "stm32f4xx_pwr.h"
#include "stm32f4xx_dma.h" #include "stm32f4xx_dma.h"
#include "stm32f4xx_usart.h" #include "stm32f4xx_usart.h"
@@ -61,7 +60,6 @@
#include "stm32f2xx_tim.h" #include "stm32f2xx_tim.h"
#include "stm32f2xx_adc.h" #include "stm32f2xx_adc.h"
#include "stm32f2xx_spi.h" #include "stm32f2xx_spi.h"
#include "stm32f2xx_rtc.h"
#include "stm32f2xx_pwr.h" #include "stm32f2xx_pwr.h"
#include "stm32f2xx_dma.h" #include "stm32f2xx_dma.h"
#include "stm32f2xx_usart.h" #include "stm32f2xx_usart.h"
@@ -16,7 +16,11 @@ if(NOT NATIVE_BUILD)
${STM32CUBE_DIR}/Src/stm32f2xx_ll_rcc.c ${STM32CUBE_DIR}/Src/stm32f2xx_ll_rcc.c
${STM32CUBE_DIR}/Src/stm32f2xx_ll_exti.c ${STM32CUBE_DIR}/Src/stm32f2xx_ll_exti.c
# HAL drivers # HAL drivers
${STM32CUBE_DIR}/Src/stm32f2xx_hal.c
${STM32CUBE_DIR}/Src/stm32f2xx_hal_pwr.c
${STM32CUBE_DIR}/Src/stm32f2xx_hal_pwr_ex.c
${STM32CUBE_DIR}/Src/stm32f2xx_hal_rcc.c ${STM32CUBE_DIR}/Src/stm32f2xx_hal_rcc.c
${STM32CUBE_DIR}/Src/stm32f2xx_hal_rtc.c
${STM32CUBE_DIR}/Src/stm32f2xx_hal_i2c.c ${STM32CUBE_DIR}/Src/stm32f2xx_hal_i2c.c
) )
@@ -49,7 +53,6 @@ add_library(stm32_stdperiph OBJECT EXCLUDE_FROM_ALL
${STM32_STDPERIPH_DIR}/src/stm32f2xx_syscfg.c ${STM32_STDPERIPH_DIR}/src/stm32f2xx_syscfg.c
${STM32_STDPERIPH_DIR}/src/stm32f2xx_spi.c ${STM32_STDPERIPH_DIR}/src/stm32f2xx_spi.c
${STM32_STDPERIPH_DIR}/src/stm32f2xx_dma.c ${STM32_STDPERIPH_DIR}/src/stm32f2xx_dma.c
${STM32_STDPERIPH_DIR}/src/stm32f2xx_rtc.c
${STM32_STDPERIPH_DIR}/src/stm32f2xx_pwr.c ${STM32_STDPERIPH_DIR}/src/stm32f2xx_pwr.c
${STM32_STDPERIPH_DIR}/src/stm32f2xx_usart.c ${STM32_STDPERIPH_DIR}/src/stm32f2xx_usart.c
${STM32_STDPERIPH_DIR}/src/stm32f2xx_adc.c ${STM32_STDPERIPH_DIR}/src/stm32f2xx_adc.c
@@ -15,7 +15,11 @@ if(NOT NATIVE_BUILD)
${STM32CUBE_DIR}/Src/stm32f4xx_ll_rcc.c ${STM32CUBE_DIR}/Src/stm32f4xx_ll_rcc.c
${STM32CUBE_DIR}/Src/stm32f4xx_ll_exti.c ${STM32CUBE_DIR}/Src/stm32f4xx_ll_exti.c
# HAL drivers # HAL drivers
${STM32CUBE_DIR}/Src/stm32f4xx_hal.c
${STM32CUBE_DIR}/Src/stm32f4xx_hal_pwr.c
${STM32CUBE_DIR}/Src/stm32f4xx_hal_pwr_ex.c
${STM32CUBE_DIR}/Src/stm32f4xx_hal_rcc.c ${STM32CUBE_DIR}/Src/stm32f4xx_hal_rcc.c
${STM32CUBE_DIR}/Src/stm32f4xx_hal_rtc.c
${STM32CUBE_DIR}/Src/stm32f4xx_hal_i2c.c ${STM32CUBE_DIR}/Src/stm32f4xx_hal_i2c.c
${STM32CUBE_DIR}/Src/stm32f4xx_hal_i2c_ex.c ${STM32CUBE_DIR}/Src/stm32f4xx_hal_i2c_ex.c
) )
@@ -48,7 +52,6 @@ set(STM32_STDPERIPH_SRC
${STM32_STDPERIPH_DIR}/src/stm32f4xx_syscfg.c ${STM32_STDPERIPH_DIR}/src/stm32f4xx_syscfg.c
${STM32_STDPERIPH_DIR}/src/stm32f4xx_spi.c ${STM32_STDPERIPH_DIR}/src/stm32f4xx_spi.c
${STM32_STDPERIPH_DIR}/src/stm32f4xx_dma.c ${STM32_STDPERIPH_DIR}/src/stm32f4xx_dma.c
${STM32_STDPERIPH_DIR}/src/stm32f4xx_rtc.c
${STM32_STDPERIPH_DIR}/src/stm32f4xx_pwr.c ${STM32_STDPERIPH_DIR}/src/stm32f4xx_pwr.c
${STM32_STDPERIPH_DIR}/src/stm32f4xx_usart.c ${STM32_STDPERIPH_DIR}/src/stm32f4xx_usart.c
${STM32_STDPERIPH_DIR}/src/stm32f4xx_adc.c ${STM32_STDPERIPH_DIR}/src/stm32f4xx_adc.c
@@ -19,28 +19,26 @@
* GNU General Public License for more details. * GNU General Public License for more details.
*/ */
#include "board.h" #include "stm32_hal.h"
#include "rtc.h" #include "rtc.h"
RTC_HandleTypeDef rtc = {};
void rtcSetTime(const struct gtm * t) void rtcSetTime(const struct gtm * t)
{ {
g_ms100 = 0; // start of next second begins now g_ms100 = 0; // start of next second begins now
RTC_TimeTypeDef RTC_TimeStruct; RTC_TimeTypeDef RTC_TimeStruct = {};
RTC_DateTypeDef RTC_DateStruct; RTC_DateTypeDef RTC_DateStruct = {};
RTC_TimeStructInit(&RTC_TimeStruct); RTC_TimeStruct.Hours = t->tm_hour;
RTC_DateStructInit(&RTC_DateStruct); RTC_TimeStruct.Minutes = t->tm_min;
RTC_TimeStruct.Seconds = t->tm_sec;
RTC_TimeStruct.RTC_Hours = t->tm_hour; RTC_DateStruct.Year = t->tm_year - 100;
RTC_TimeStruct.RTC_Minutes = t->tm_min; RTC_DateStruct.Month = t->tm_mon + 1;
RTC_TimeStruct.RTC_Seconds = t->tm_sec; RTC_DateStruct.Date = t->tm_mday;
RTC_DateStruct.RTC_Year = t->tm_year - 100; HAL_RTC_SetTime(&rtc, &RTC_TimeStruct, RTC_FORMAT_BIN);
RTC_DateStruct.RTC_Month = t->tm_mon + 1; HAL_RTC_SetDate(&rtc, &RTC_DateStruct, RTC_FORMAT_BIN);
RTC_DateStruct.RTC_Date = t->tm_mday;
RTC_SetTime(RTC_Format_BIN, &RTC_TimeStruct);
RTC_SetDate(RTC_Format_BIN, &RTC_DateStruct);
} }
void rtcGetTime(struct gtm * t) void rtcGetTime(struct gtm * t)
@@ -48,50 +46,53 @@ void rtcGetTime(struct gtm * t)
RTC_TimeTypeDef RTC_TimeStruct; RTC_TimeTypeDef RTC_TimeStruct;
RTC_DateTypeDef RTC_DateStruct; RTC_DateTypeDef RTC_DateStruct;
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStruct); HAL_RTC_GetTime(&rtc, &RTC_TimeStruct, RTC_FORMAT_BIN);
RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct); HAL_RTC_GetDate(&rtc, &RTC_DateStruct, RTC_FORMAT_BIN);
t->tm_hour = RTC_TimeStruct.RTC_Hours; t->tm_hour = RTC_TimeStruct.Hours;
t->tm_min = RTC_TimeStruct.RTC_Minutes; t->tm_min = RTC_TimeStruct.Minutes;
t->tm_sec = RTC_TimeStruct.RTC_Seconds; t->tm_sec = RTC_TimeStruct.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_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.RTC_Month - 1; t->tm_mon = RTC_DateStruct.Month - 1;
t->tm_mday = RTC_DateStruct.RTC_Date; t->tm_mday = RTC_DateStruct.Date;
} }
void rtcInit() void rtcInit()
{ {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); rtc.Instance = RTC;
PWR_BackupAccessCmd(ENABLE); __HAL_RCC_PWR_CLK_ENABLE();
RCC_LSEConfig(RCC_LSE_ON); HAL_PWR_EnableBkUpAccess();
// Prevent lockup in case of 32kHz oscillator failure // Enable LSE Oscillator
uint32_t i = 0; RCC_OscInitTypeDef RCC_OscInitStruct;
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) { RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
if (++i > 1000000) RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
return; 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); __HAL_RCC_RTC_ENABLE();
RCC_RTCCLKCmd(ENABLE); HAL_RTC_WaitForSynchro(&rtc);
RTC_WaitForSynchro();
#if !defined(BOOT) #if !defined(BOOT)
RTC_InitTypeDef RTC_InitStruct;
// RTC time base = LSE / ((AsynchPrediv+1) * (SynchPrediv+1)) = 1 Hz*/ // RTC time base = LSE / ((AsynchPrediv+1) * (SynchPrediv+1)) = 1 Hz*/
RTC_InitStruct.RTC_HourFormat = RTC_HourFormat_24; rtc.Init.HourFormat = RTC_HOURFORMAT_24;
RTC_InitStruct.RTC_AsynchPrediv = 127; rtc.Init.AsynchPrediv = 127;
RTC_InitStruct.RTC_SynchPrediv = 255; rtc.Init.SynchPrediv = 255;
RTC_Init(&RTC_InitStruct); 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; struct gtm utm;
rtcGetTime(&utm); rtcGetTime(&utm);
g_rtcTime = gmktime(&utm); g_rtcTime = gmktime(&utm);
#endif #endif
#if defined(RTC_BACKUP_RAM) && !defined(BOOT) #if defined(RTC_BACKUP_RAM) && !defined(BOOT)
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE); __HAL_RCC_BKPSRAM_CLK_ENABLE();
PWR_BackupRegulatorCmd(ENABLE); HAL_PWREx_EnableBkUpReg();
#endif #endif
} }
@@ -25,8 +25,14 @@ extern "C" {
#if defined(STM32F4) #if defined(STM32F4)
#include "CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h" #include "CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h"
#include "stm32f4xx_hal.h" #include "stm32f4xx_hal.h"
#include "stm32f4xx_hal_rcc.h"
#include "stm32f4xx_hal_rtc.h"
#include "stm32f4xx_hal_pwr.h"
#elif defined(STM32F2) #elif defined(STM32F2)
#include "CMSIS/Device/ST/STM32F2xx/Include/stm32f2xx.h" #include "CMSIS/Device/ST/STM32F2xx/Include/stm32f2xx.h"
#include "stm32f2xx_hal.h" #include "stm32f2xx_hal.h"
#include "stm32f2xx_hal_rcc.h"
#include "stm32f2xx_hal_rtc.h"
#include "stm32f2xx_hal_pwr.h"
#endif #endif
} }
@@ -58,10 +58,10 @@
#define HAL_I2C_MODULE_ENABLED #define HAL_I2C_MODULE_ENABLED
// #define HAL_I2S_MODULE_ENABLED // #define HAL_I2S_MODULE_ENABLED
// #define HAL_IWDG_MODULE_ENABLED // #define HAL_IWDG_MODULE_ENABLED
// #define HAL_PWR_MODULE_ENABLED #define HAL_PWR_MODULE_ENABLED
#define HAL_RCC_MODULE_ENABLED #define HAL_RCC_MODULE_ENABLED
// #define HAL_RNG_MODULE_ENABLED // #define HAL_RNG_MODULE_ENABLED
// #define HAL_RTC_MODULE_ENABLED #define HAL_RTC_MODULE_ENABLED
// #define HAL_SD_MODULE_ENABLED // #define HAL_SD_MODULE_ENABLED
// #define HAL_SPI_MODULE_ENABLED // #define HAL_SPI_MODULE_ENABLED
// #define HAL_TIM_MODULE_ENABLED // #define HAL_TIM_MODULE_ENABLED
@@ -64,11 +64,11 @@
// #define HAL_IWDG_MODULE_ENABLED // #define HAL_IWDG_MODULE_ENABLED
// #define HAL_LTDC_MODULE_ENABLED // #define HAL_LTDC_MODULE_ENABLED
// #define HAL_DSI_MODULE_ENABLED // #define HAL_DSI_MODULE_ENABLED
// #define HAL_PWR_MODULE_ENABLED #define HAL_PWR_MODULE_ENABLED
// #define HAL_QSPI_MODULE_ENABLED // #define HAL_QSPI_MODULE_ENABLED
#define HAL_RCC_MODULE_ENABLED #define HAL_RCC_MODULE_ENABLED
// #define HAL_RNG_MODULE_ENABLED // #define HAL_RNG_MODULE_ENABLED
// #define HAL_RTC_MODULE_ENABLED #define HAL_RTC_MODULE_ENABLED
// #define HAL_SAI_MODULE_ENABLED // #define HAL_SAI_MODULE_ENABLED
// #define HAL_SD_MODULE_ENABLED // #define HAL_SD_MODULE_ENABLED
// #define HAL_SPI_MODULE_ENABLED // #define HAL_SPI_MODULE_ENABLED
-27
View File
@@ -614,33 +614,6 @@ ITStatus EXTI_GetITStatus(uint32_t EXTI_Line) { return RESET; }
void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct) { } void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct) { }
void EXTI_ClearITPendingBit(uint32_t EXTI_Line) { } void EXTI_ClearITPendingBit(uint32_t EXTI_Line) { }
// RTC fake functions
ErrorStatus RTC_Init(RTC_InitTypeDef* RTC_InitStruct) { return SUCCESS; }
void RTC_TimeStructInit(RTC_TimeTypeDef* RTC_TimeStruct) { }
void RTC_DateStructInit(RTC_DateTypeDef* RTC_DateStruct) { }
ErrorStatus RTC_WaitForSynchro(void) { return SUCCESS; }
ErrorStatus RTC_SetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct) { return SUCCESS; }
ErrorStatus RTC_SetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct) { return SUCCESS; }
void RTC_GetTime(uint32_t RTC_Format, RTC_TimeTypeDef * RTC_TimeStruct)
{
time_t tme;
time(&tme);
struct tm * timeinfo = localtime(&tme);
RTC_TimeStruct->RTC_Hours = timeinfo->tm_hour;
RTC_TimeStruct->RTC_Minutes = timeinfo->tm_min;
RTC_TimeStruct->RTC_Seconds = timeinfo->tm_sec;
}
void RTC_GetDate(uint32_t RTC_Format, RTC_DateTypeDef * RTC_DateStruct)
{
time_t tme;
time(&tme);
struct tm * timeinfo = localtime(&tme);
RTC_DateStruct->RTC_Year = timeinfo->tm_year - 100; // STM32 year is two decimals only (so base is currently 2000), tm is based on number of years since 1900
RTC_DateStruct->RTC_Month = timeinfo->tm_mon + 1;
RTC_DateStruct->RTC_Date = timeinfo->tm_mday;
}
void unlockFlash() void unlockFlash()
{ {
} }