[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
|
* 2018-12-04 balanceTWK first version
|
||||||
* 2020-10-14 Dozingfiretruck Porting for stm32wbxx
|
* 2020-10-14 Dozingfiretruck Porting for stm32wbxx
|
||||||
* 2021-02-05 Meco Man fix the problem of mixing local time and UTC time
|
* 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"
|
#include "board.h"
|
||||||
|
@ -25,8 +26,6 @@
|
||||||
|
|
||||||
#define BKUP_REG_DATA 0xA5A5
|
#define BKUP_REG_DATA 0xA5A5
|
||||||
|
|
||||||
static struct rt_device rtc;
|
|
||||||
|
|
||||||
static RTC_HandleTypeDef RTC_Handler;
|
static RTC_HandleTypeDef RTC_Handler;
|
||||||
|
|
||||||
RT_WEAK uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister)
|
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;
|
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
|
#ifdef SOC_SERIES_STM32F1
|
||||||
/* update RTC_BKP_DRx*/
|
/* update RTC_BKP_DRx*/
|
||||||
static void rt_rtc_f1_bkp_update(void)
|
static void rt_rtc_f1_bkp_update(void)
|
||||||
|
@ -160,7 +131,7 @@ static void rt_rtc_f1_bkp_update(void)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static rt_err_t rt_rtc_config(struct rt_device *dev)
|
static rt_err_t rt_rtc_config(void)
|
||||||
{
|
{
|
||||||
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
|
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
|
||||||
|
|
||||||
|
@ -234,81 +205,90 @@ static rt_err_t rt_rtc_config(struct rt_device *dev)
|
||||||
return RT_EOK;
|
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)
|
||||||
{
|
{
|
||||||
rt_err_t result = RT_EOK;
|
#if !defined(SOC_SERIES_STM32H7) && !defined(SOC_SERIES_STM32WL) && !defined(SOC_SERIES_STM32WB)
|
||||||
RT_ASSERT(dev != RT_NULL);
|
__HAL_RCC_PWR_CLK_ENABLE();
|
||||||
switch (cmd)
|
#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)
|
||||||
{
|
{
|
||||||
case RT_DEVICE_CTRL_RTC_GET_TIME:
|
|
||||||
*(rt_uint32_t *)args = get_rtc_timestamp();
|
*(rt_uint32_t *)args = get_rtc_timestamp();
|
||||||
LOG_D("RTC: get rtc_time %x\n", *(rt_uint32_t *)args);
|
LOG_D("RTC: get rtc_time %x\n", *(rt_uint32_t *)args);
|
||||||
break;
|
|
||||||
|
|
||||||
case RT_DEVICE_CTRL_RTC_SET_TIME:
|
return RT_EOK;
|
||||||
|
}
|
||||||
|
|
||||||
|
static rt_err_t stm32_rtc_set_secs(void *args)
|
||||||
|
{
|
||||||
|
rt_err_t result = RT_EOK;
|
||||||
|
|
||||||
if (set_rtc_time_stamp(*(rt_uint32_t *)args))
|
if (set_rtc_time_stamp(*(rt_uint32_t *)args))
|
||||||
{
|
{
|
||||||
result = -RT_ERROR;
|
result = -RT_ERROR;
|
||||||
}
|
}
|
||||||
LOG_D("RTC: set rtc_time %x\n", *(rt_uint32_t *)args);
|
LOG_D("RTC: set rtc_time %x\n", *(rt_uint32_t *)args);
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef RT_USING_DEVICE_OPS
|
static const struct rt_rtc_ops stm32_rtc_ops =
|
||||||
const static struct rt_device_ops 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_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)
|
static rt_rtc_dev_t stm32_rtc_dev;
|
||||||
{
|
|
||||||
RT_ASSERT(device != RT_NULL);
|
|
||||||
|
|
||||||
rt_rtc_init();
|
static int rt_hw_rtc_init(void)
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
rt_err_t result;
|
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)
|
if (result != RT_EOK)
|
||||||
{
|
{
|
||||||
LOG_E("rtc register err code: %d", result);
|
LOG_E("rtc register err code: %d", result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
LOG_D("rtc init success");
|
LOG_D("rtc init success");
|
||||||
|
|
||||||
return RT_EOK;
|
return RT_EOK;
|
||||||
}
|
}
|
||||||
INIT_DEVICE_EXPORT(rt_hw_rtc_init);
|
INIT_DEVICE_EXPORT(rt_hw_rtc_init);
|
||||||
|
|
||||||
#endif /* BSP_USING_ONCHIP_RTC */
|
#endif /* BSP_USING_ONCHIP_RTC */
|
||||||
|
|
Loading…
Reference in New Issue