[bsp][stm32] Implement stm32 rtc driver to RTC framework V2.0
This commit is contained in:
parent
2bd7e04827
commit
ecc0981d10
|
@ -8,6 +8,7 @@
|
|||
* 2018-12-04 balanceTWK first version
|
||||
* 2020-10-14 Dozingfiretruck Porting for stm32wbxx
|
||||
* 2021-02-05 Meco Man fix the problem of mixing local time and UTC time
|
||||
* 2021-07-05 iysheng implement RTC framework V2.0
|
||||
*/
|
||||
|
||||
#include "board.h"
|
||||
|
@ -25,8 +26,6 @@
|
|||
|
||||
#define BKUP_REG_DATA 0xA5A5
|
||||
|
||||
static struct rt_device rtc;
|
||||
|
||||
static RTC_HandleTypeDef RTC_Handler;
|
||||
|
||||
RT_WEAK uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
|
||||
|
@ -102,34 +101,6 @@ static rt_err_t set_rtc_time_stamp(time_t time_stamp)
|
|||
return RT_EOK;
|
||||
}
|
||||
|
||||
static void rt_rtc_init(void)
|
||||
{
|
||||
#if !defined(SOC_SERIES_STM32H7) && !defined(SOC_SERIES_STM32WL) && !defined(SOC_SERIES_STM32WB)
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
#endif
|
||||
|
||||
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
||||
#ifdef BSP_RTC_USING_LSI
|
||||
#ifdef SOC_SERIES_STM32WB
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI1;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
|
||||
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
|
||||
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
|
||||
#else
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
|
||||
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
|
||||
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
|
||||
#endif
|
||||
#else
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
|
||||
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
|
||||
RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
|
||||
#endif
|
||||
HAL_RCC_OscConfig(&RCC_OscInitStruct);
|
||||
}
|
||||
|
||||
#ifdef SOC_SERIES_STM32F1
|
||||
/* update RTC_BKP_DRx*/
|
||||
static void rt_rtc_f1_bkp_update(void)
|
||||
|
@ -160,7 +131,7 @@ static void rt_rtc_f1_bkp_update(void)
|
|||
}
|
||||
#endif
|
||||
|
||||
static rt_err_t rt_rtc_config(struct rt_device *dev)
|
||||
static rt_err_t rt_rtc_config(void)
|
||||
{
|
||||
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
|
||||
|
||||
|
@ -234,81 +205,90 @@ static rt_err_t rt_rtc_config(struct rt_device *dev)
|
|||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
|
||||
static rt_err_t stm32_rtc_init(void)
|
||||
{
|
||||
#if !defined(SOC_SERIES_STM32H7) && !defined(SOC_SERIES_STM32WL) && !defined(SOC_SERIES_STM32WB)
|
||||
__HAL_RCC_PWR_CLK_ENABLE();
|
||||
#endif
|
||||
|
||||
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
||||
#ifdef BSP_RTC_USING_LSI
|
||||
#ifdef SOC_SERIES_STM32WB
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI1;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
|
||||
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
|
||||
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
|
||||
#else
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
|
||||
RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
|
||||
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
|
||||
#endif
|
||||
#else
|
||||
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
|
||||
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
|
||||
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
|
||||
RCC_OscInitStruct.LSIState = RCC_LSI_OFF;
|
||||
#endif
|
||||
HAL_RCC_OscConfig(&RCC_OscInitStruct);
|
||||
|
||||
if (rt_rtc_config() != RT_EOK)
|
||||
{
|
||||
LOG_E("rtc init failed.");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t stm32_rtc_get_secs(void *args)
|
||||
{
|
||||
*(rt_uint32_t *)args = get_rtc_timestamp();
|
||||
LOG_D("RTC: get rtc_time %x\n", *(rt_uint32_t *)args);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t stm32_rtc_set_secs(void *args)
|
||||
{
|
||||
rt_err_t result = RT_EOK;
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
switch (cmd)
|
||||
{
|
||||
case RT_DEVICE_CTRL_RTC_GET_TIME:
|
||||
*(rt_uint32_t *)args = get_rtc_timestamp();
|
||||
LOG_D("RTC: get rtc_time %x\n", *(rt_uint32_t *)args);
|
||||
break;
|
||||
|
||||
case RT_DEVICE_CTRL_RTC_SET_TIME:
|
||||
if (set_rtc_time_stamp(*(rt_uint32_t *)args))
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
}
|
||||
LOG_D("RTC: set rtc_time %x\n", *(rt_uint32_t *)args);
|
||||
break;
|
||||
if (set_rtc_time_stamp(*(rt_uint32_t *)args))
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
}
|
||||
LOG_D("RTC: set rtc_time %x\n", *(rt_uint32_t *)args);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_DEVICE_OPS
|
||||
const static struct rt_device_ops rtc_ops =
|
||||
static const struct rt_rtc_ops stm32_rtc_ops =
|
||||
{
|
||||
stm32_rtc_init,
|
||||
stm32_rtc_get_secs, /* get_secs */
|
||||
stm32_rtc_set_secs, /* set secs */
|
||||
RT_NULL,
|
||||
RT_NULL,
|
||||
RT_NULL,
|
||||
RT_NULL,
|
||||
RT_NULL,
|
||||
rt_rtc_control
|
||||
};
|
||||
#endif
|
||||
|
||||
static rt_err_t rt_hw_rtc_register(rt_device_t device, const char *name, rt_uint32_t flag)
|
||||
{
|
||||
RT_ASSERT(device != RT_NULL);
|
||||
static rt_rtc_dev_t stm32_rtc_dev;
|
||||
|
||||
rt_rtc_init();
|
||||
if (rt_rtc_config(device) != RT_EOK)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
#ifdef RT_USING_DEVICE_OPS
|
||||
device->ops = &rtc_ops;
|
||||
#else
|
||||
device->init = RT_NULL;
|
||||
device->open = RT_NULL;
|
||||
device->close = RT_NULL;
|
||||
device->read = RT_NULL;
|
||||
device->write = RT_NULL;
|
||||
device->control = rt_rtc_control;
|
||||
#endif
|
||||
device->type = RT_Device_Class_RTC;
|
||||
device->rx_indicate = RT_NULL;
|
||||
device->tx_complete = RT_NULL;
|
||||
device->user_data = RT_NULL;
|
||||
|
||||
/* register a character device */
|
||||
return rt_device_register(device, name, flag);
|
||||
}
|
||||
|
||||
int rt_hw_rtc_init(void)
|
||||
static int rt_hw_rtc_init(void)
|
||||
{
|
||||
rt_err_t result;
|
||||
result = rt_hw_rtc_register(&rtc, "rtc", RT_DEVICE_FLAG_RDWR);
|
||||
|
||||
stm32_rtc_dev.ops = &stm32_rtc_ops;
|
||||
result = rt_rtc_dev_register(&stm32_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR, RT_NULL);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
LOG_E("rtc register err code: %d", result);
|
||||
return result;
|
||||
}
|
||||
LOG_D("rtc init success");
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
INIT_DEVICE_EXPORT(rt_hw_rtc_init);
|
||||
|
||||
#endif /* BSP_USING_ONCHIP_RTC */
|
||||
|
|
Loading…
Reference in New Issue