chore: convert RTC to HAL (#2866)
* chore: switch RTC to HAL * chore: remove unneeded files * fix simu/simulator
This commit is contained in:
@@ -45,7 +45,6 @@
|
||||
#include "stm32f4xx_tim.h"
|
||||
#include "stm32f4xx_adc.h"
|
||||
#include "stm32f4xx_spi.h"
|
||||
#include "stm32f4xx_rtc.h"
|
||||
#include "stm32f4xx_pwr.h"
|
||||
#include "stm32f4xx_dma.h"
|
||||
#include "stm32f4xx_usart.h"
|
||||
@@ -61,7 +60,6 @@
|
||||
#include "stm32f2xx_tim.h"
|
||||
#include "stm32f2xx_adc.h"
|
||||
#include "stm32f2xx_spi.h"
|
||||
#include "stm32f2xx_rtc.h"
|
||||
#include "stm32f2xx_pwr.h"
|
||||
#include "stm32f2xx_dma.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_exti.c
|
||||
# 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_rtc.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_spi.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_usart.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_exti.c
|
||||
# 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_rtc.c
|
||||
${STM32CUBE_DIR}/Src/stm32f4xx_hal_i2c.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_spi.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_usart.c
|
||||
${STM32_STDPERIPH_DIR}/src/stm32f4xx_adc.c
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -25,8 +25,14 @@ extern "C" {
|
||||
#if defined(STM32F4)
|
||||
#include "CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h"
|
||||
#include "stm32f4xx_hal.h"
|
||||
#include "stm32f4xx_hal_rcc.h"
|
||||
#include "stm32f4xx_hal_rtc.h"
|
||||
#include "stm32f4xx_hal_pwr.h"
|
||||
#elif defined(STM32F2)
|
||||
#include "CMSIS/Device/ST/STM32F2xx/Include/stm32f2xx.h"
|
||||
#include "stm32f2xx_hal.h"
|
||||
#include "stm32f2xx_hal_rcc.h"
|
||||
#include "stm32f2xx_hal_rtc.h"
|
||||
#include "stm32f2xx_hal_pwr.h"
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -58,10 +58,10 @@
|
||||
#define HAL_I2C_MODULE_ENABLED
|
||||
// #define HAL_I2S_MODULE_ENABLED
|
||||
// #define HAL_IWDG_MODULE_ENABLED
|
||||
// #define HAL_PWR_MODULE_ENABLED
|
||||
#define HAL_PWR_MODULE_ENABLED
|
||||
#define HAL_RCC_MODULE_ENABLED
|
||||
// #define HAL_RNG_MODULE_ENABLED
|
||||
// #define HAL_RTC_MODULE_ENABLED
|
||||
#define HAL_RTC_MODULE_ENABLED
|
||||
// #define HAL_SD_MODULE_ENABLED
|
||||
// #define HAL_SPI_MODULE_ENABLED
|
||||
// #define HAL_TIM_MODULE_ENABLED
|
||||
|
||||
@@ -64,11 +64,11 @@
|
||||
// #define HAL_IWDG_MODULE_ENABLED
|
||||
// #define HAL_LTDC_MODULE_ENABLED
|
||||
// #define HAL_DSI_MODULE_ENABLED
|
||||
// #define HAL_PWR_MODULE_ENABLED
|
||||
#define HAL_PWR_MODULE_ENABLED
|
||||
// #define HAL_QSPI_MODULE_ENABLED
|
||||
#define HAL_RCC_MODULE_ENABLED
|
||||
// #define HAL_RNG_MODULE_ENABLED
|
||||
// #define HAL_RTC_MODULE_ENABLED
|
||||
#define HAL_RTC_MODULE_ENABLED
|
||||
// #define HAL_SAI_MODULE_ENABLED
|
||||
// #define HAL_SD_MODULE_ENABLED
|
||||
// #define HAL_SPI_MODULE_ENABLED
|
||||
|
||||
@@ -614,33 +614,6 @@ ITStatus EXTI_GetITStatus(uint32_t EXTI_Line) { return RESET; }
|
||||
void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct) { }
|
||||
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()
|
||||
{
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user