rt-thread-official/bsp/nuclei/libraries/gd32vf103/HAL_Drivers/drv_rtc.c

161 lines
3.5 KiB
C
Raw Normal View History

bsp/nuclei: Add more drivers support for gd32vf103_rvstar board Squashed commit of the following: commit 32dd349ccff85cdcad81021b5185f8f7347786db Author: Huaqi Fang <578567190@qq.com> Date: Thu Jun 11 16:08:08 2020 +0800 bsp/nuclei: Add more documentation about how to use RT-Thread in RV-STAR Signed-off-by: Huaqi Fang <578567190@qq.com> commit c9474c20a558e9d7934a3d72cc9bb17a98c20871 Author: Huaqi Fang <578567190@qq.com> Date: Thu Jun 11 14:06:42 2020 +0800 bsp/nuclei: remove comments in rt_hw_*_drvinit Signed-off-by: Huaqi Fang <578567190@qq.com> commit 89b5caa2c3e55dc352b13f8e33229f03351b7049 Author: Huaqi Fang <578567190@qq.com> Date: Thu Jun 11 13:52:12 2020 +0800 bsp/nuclei: Add a entry README.md for nuclei bsp Signed-off-by: Huaqi Fang <578567190@qq.com> commit ae50d6e9bdc92ca4767c5ac7d0ded7252314a42e Author: Huaqi Fang <578567190@qq.com> Date: Thu Jun 11 11:52:52 2020 +0800 bsp/nuclei: update README.md cover more pinmux section Signed-off-by: Huaqi Fang <578567190@qq.com> commit 1a42d85dfe5bc7b009f057784249d54d0c82de0f Author: Huaqi Fang <578567190@qq.com> Date: Thu Jun 11 08:52:06 2020 +0800 bsp/nuclei: clean up unused code in drv_spi.c Signed-off-by: Huaqi Fang <578567190@qq.com> commit b40edcdf59c96ef261f65a0c023a24ac78cabb39 Author: Huaqi Fang <578567190@qq.com> Date: Wed Jun 10 14:09:15 2020 +0800 bsp/nuclei: Format the code comments for hw pinmux init Signed-off-by: Huaqi Fang <578567190@qq.com> commit c8ae9fdfdb989bbff85d911fee3124abd9d145db Author: Huaqi Fang <578567190@qq.com> Date: Mon Jun 8 17:12:43 2020 +0800 bsp/nuclei: Add more drivers support for gd32vf103 * More drivers are supported for rvstar board, see README.md * If user want to use these supported drivers, menuconfig is required * User also need to setup pinmux for its coresponding driver in board/board.c Signed-off-by: Huaqi Fang <578567190@qq.com> Signed-off-by: Huaqi Fang <578567190@qq.com>
2020-06-16 09:35:58 +08:00
/*
* Copyright (c) 2006-2020, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-06-08 hqfang first implementation.
*/
#include "drv_rtc.h"
#ifdef BSP_USING_RTC
static time_t get_timestamp(void)
{
return (time_t)rtc_counter_get();
}
static int set_timestamp(time_t timestamp)
{
/* wait until last write operation on RTC registers has finished */
rtc_lwoff_wait();
/* change the current time */
rtc_counter_set((uint32_t)timestamp);
/* wait until last write operation on RTC registers has finished */
rtc_lwoff_wait();
return RT_EOK;
}
static void rtc_configuration(void)
{
/* enable PMU and BKPI clocks */
rcu_periph_clock_enable(RCU_BKPI);
rcu_periph_clock_enable(RCU_PMU);
/* allow access to BKP domain */
pmu_backup_write_enable();
/* reset backup domain */
bkp_deinit();
/* enable LXTAL */
rcu_osci_on(RCU_LXTAL);
/* wait till LXTAL is ready */
rcu_osci_stab_wait(RCU_LXTAL);
/* select RCU_LXTAL as RTC clock source */
rcu_rtc_clock_config(RCU_RTCSRC_LXTAL);
/* enable RTC Clock */
rcu_periph_clock_enable(RCU_RTC);
/* wait for RTC registers synchronization */
rtc_register_sync_wait();
/* wait until last write operation on RTC registers has finished */
rtc_lwoff_wait();
/* wait until last write operation on RTC registers has finished */
rtc_lwoff_wait();
/* set RTC prescaler: set RTC period to 1s */
rtc_prescaler_set(32767);
/* wait until last write operation on RTC registers has finished */
rtc_lwoff_wait();
}
static rt_err_t gd32_rtc_init(rt_device_t dev)
{
if (bkp_data_read(BKP_DATA_0) != 0xA5A5)
{
rtc_configuration();
bkp_data_write(BKP_DATA_0, 0xA5A5);
}
else
{
/* allow access to BKP domain */
rcu_periph_clock_enable(RCU_PMU);
pmu_backup_write_enable();
/* wait for RTC registers synchronization */
rtc_register_sync_wait();
/* wait until last write operation on RTC registers has finished */
rtc_lwoff_wait();
}
return RT_EOK;
}
static rt_err_t gd32_rtc_open(rt_device_t dev, rt_uint16_t oflag)
{
return RT_EOK;
}
static rt_err_t gd32_rtc_close(rt_device_t dev)
{
return RT_EOK;
}
static rt_size_t gd32_rtc_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
{
return RT_EOK;
}
static rt_size_t gd32_rtc_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
return RT_EOK;
}
static rt_err_t gd32_rtc_control(rt_device_t dev, int cmd, void *args)
{
RT_ASSERT(dev != RT_NULL);
switch (cmd)
{
case RT_DEVICE_CTRL_RTC_GET_TIME:
{
*(uint32_t *)args = get_timestamp();
}
break;
case RT_DEVICE_CTRL_RTC_SET_TIME:
{
set_timestamp(*(time_t *)args);
}
break;
default:
return RT_EINVAL;
}
return RT_EOK;
}
static struct rt_device rtc_device =
{
.type = RT_Device_Class_RTC,
.init = gd32_rtc_init,
.open = gd32_rtc_open,
.close = gd32_rtc_close,
.read = gd32_rtc_read,
.write = gd32_rtc_write,
.control = gd32_rtc_control,
};
int rt_hw_rtc_init(void)
{
rt_err_t ret = RT_EOK;
ret = rt_device_register(&rtc_device, "rtc", RT_DEVICE_FLAG_RDWR);
rt_device_open(&rtc_device, RT_DEVICE_OFLAG_RDWR);
return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_hw_rtc_init);
#endif /* BSP_USING_RTC */