From d2e6719a0e4a8e2133674d08e54160f5345da56d Mon Sep 17 00:00:00 2001 From: wuzheng Date: Thu, 2 Feb 2023 18:01:24 +0800 Subject: [PATCH] adapt rtc driver for hc32f4a0 --- .../Applications/app_test/test_rtc.c | 6 +- Ubiquitous/XiZi_IIoT/board/hc32f4a0/board.c | 7 + .../board/hc32f4a0/third_party_driver/Kconfig | 8 + .../hc32f4a0/third_party_driver/Makefile | 4 + .../common/hc32_ll_driver/src/Makefile | 4 + .../third_party_driver/include/connect_rtc.h | 37 ++++ .../hc32f4a0/third_party_driver/rtc/Kconfig | 11 + .../hc32f4a0/third_party_driver/rtc/Makefile | 2 + .../third_party_driver/rtc/connect_rtc.c | 193 ++++++++++++++++++ 9 files changed, 271 insertions(+), 1 deletion(-) create mode 100644 Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/include/connect_rtc.h create mode 100644 Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/rtc/Kconfig create mode 100644 Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/rtc/Makefile create mode 100644 Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/rtc/connect_rtc.c diff --git a/APP_Framework/Applications/app_test/test_rtc.c b/APP_Framework/Applications/app_test/test_rtc.c index 3a95c6cea..18cd9b121 100644 --- a/APP_Framework/Applications/app_test/test_rtc.c +++ b/APP_Framework/Applications/app_test/test_rtc.c @@ -14,13 +14,17 @@ void TestRTC(int argc,char *argv[]) int times = atoi(argv[1]); printf("Time will be printf %d times\n",times); + struct RtcDrvConfigureParam rtc_para; + time_t my_time=0; + rtc_para.rtc_operation_cmd = OPER_RTC_SET_TIME; - *(rtc_para.time) = 0; + rtc_para.time = &my_time; struct PrivIoctlCfg ioctl_cfg; ioctl_cfg.ioctl_driver_type = RTC_TYPE; ioctl_cfg.args = (void *)&rtc_para; + PrivIoctl(rtc_fd,0,&ioctl_cfg); rtc_para.rtc_operation_cmd = OPER_RTC_GET_TIME; diff --git a/Ubiquitous/XiZi_IIoT/board/hc32f4a0/board.c b/Ubiquitous/XiZi_IIoT/board/hc32f4a0/board.c index aea6acb02..9966f98a2 100644 --- a/Ubiquitous/XiZi_IIoT/board/hc32f4a0/board.c +++ b/Ubiquitous/XiZi_IIoT/board/hc32f4a0/board.c @@ -54,6 +54,10 @@ Modification: #include #endif +#ifdef BSP_USING_RTC +#include +#endif + extern void entry(void); extern int HwUsartInit(); @@ -167,6 +171,9 @@ struct InitSequenceDesc _board_init[] = #endif #ifdef BSP_USING_USB { "usb", HwUsbHostInit }, +#endif +#ifdef BSP_USING_RTC + { "usb", HwRtcInit }, #endif { " NONE ", NONE }, }; diff --git a/Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/Kconfig b/Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/Kconfig index 53f197836..38f0e3afa 100644 --- a/Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/Kconfig +++ b/Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/Kconfig @@ -53,3 +53,11 @@ menuconfig BSP_USING_USB if BSP_USING_USB source "$BSP_DIR/third_party_driver/usb/Kconfig" endif + +menuconfig BSP_USING_RTC + bool "Using RTC device" + default n + select RESOURCES_RTC + if BSP_USING_RTC + source "$BSP_DIR/third_party_driver/rtc/Kconfig" + endif diff --git a/Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/Makefile b/Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/Makefile index ac78bbe64..d36b846fb 100644 --- a/Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/Makefile +++ b/Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/Makefile @@ -28,4 +28,8 @@ ifeq ($(CONFIG_BSP_USING_USB),y) SRC_DIR += usb endif +ifeq ($(CONFIG_BSP_USING_RTC),y) + SRC_DIR += rtc +endif + include $(KERNEL_ROOT)/compiler.mk diff --git a/Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/common/hc32_ll_driver/src/Makefile b/Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/common/hc32_ll_driver/src/Makefile index c23f8548a..984a4d543 100644 --- a/Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/common/hc32_ll_driver/src/Makefile +++ b/Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/common/hc32_ll_driver/src/Makefile @@ -24,4 +24,8 @@ ifeq ($(CONFIG_BSP_USING_USB),y) SRC_FILES += hc32_ll_usb.c endif +ifeq ($(CONFIG_BSP_USING_RTC),y) + SRC_FILES += hc32_ll_rtc.c +endif + include $(KERNEL_ROOT)/compiler.mk diff --git a/Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/include/connect_rtc.h b/Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/include/connect_rtc.h new file mode 100644 index 000000000..16ba33d31 --- /dev/null +++ b/Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/include/connect_rtc.h @@ -0,0 +1,37 @@ +/* +* Copyright (c) 2020 AIIT XUOS Lab +* XiUOS is licensed under Mulan PSL v2. +* You can use this software according to the terms and conditions of the Mulan PSL v2. +* You may obtain a copy of Mulan PSL v2 at: +* http://license.coscl.org.cn/MulanPSL2 +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +* See the Mulan PSL v2 for more details. +*/ + +/** +* @file connect_rtc.h +* @brief define hc32f4a0-board i2c function and struct +* @version 3.0 +* @author AIIT XUOS Lab +* @date 2023-02-02 +*/ + +#ifndef CONNECT_I2C_H +#define CONNECT_I2C_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +int HwRtcInit(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/rtc/Kconfig b/Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/rtc/Kconfig new file mode 100644 index 000000000..e853dd40a --- /dev/null +++ b/Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/rtc/Kconfig @@ -0,0 +1,11 @@ +if BSP_USING_RTC + config RTC_BUS_NAME + string "rtc bus name" + default "rtc" + config RTC_DRV_NAME + string "rtc bus driver name" + default "rtc_drv" + config RTC_DEVICE_NAME + string "rtc bus device name" + default "rtc_dev" +endif diff --git a/Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/rtc/Makefile b/Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/rtc/Makefile new file mode 100644 index 000000000..d30b05a1a --- /dev/null +++ b/Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/rtc/Makefile @@ -0,0 +1,2 @@ +SRC_FILES := connect_rtc.c +include $(KERNEL_ROOT)/compiler.mk diff --git a/Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/rtc/connect_rtc.c b/Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/rtc/connect_rtc.c new file mode 100644 index 000000000..9a627d1a0 --- /dev/null +++ b/Ubiquitous/XiZi_IIoT/board/hc32f4a0/third_party_driver/rtc/connect_rtc.c @@ -0,0 +1,193 @@ +/* +* Copyright (c) 2020 AIIT XUOS Lab +* XiUOS is licensed under Mulan PSL v2. +* You can use this software according to the terms and conditions of the Mulan PSL v2. +* You may obtain a copy of Mulan PSL v2 at: +* http://license.coscl.org.cn/MulanPSL2 +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +* See the Mulan PSL v2 for more details. +*/ + +/** +* @file connect_rtc.c +* @brief support aiit-riscv64-board rtc function and register to bus framework +* @version 1.0 +* @author AIIT XUOS Lab +* @date 2023-02-02 +*/ + +#include +#include +#include +#include + +// static int GetWeekDay(int year, int month, int day) +// { +// /* Magic method to get weekday */ +// int weekday = (day += month < 3 ? year-- : year - 2, +// 23 * month / 9 + day + 4 + year / 4 - year / 100 + year / 400) % 7; +// return weekday; +// } + +static uint32 RtcConfigure(void *drv, struct BusConfigureInfo *configure_info) +{ + NULL_PARAM_CHECK(drv); + + struct RtcDriver *rtc_drv = (struct RtcDriver *)drv; + struct RtcDrvConfigureParam *drv_param = (struct RtcDrvConfigureParam *)configure_info->private_data; + + int cmd = drv_param->rtc_operation_cmd; + time_t *time = drv_param->time; + + switch (cmd) + { + case OPER_RTC_GET_TIME: + { + struct tm ct; + stc_rtc_date_t rtc_date; + stc_rtc_time_t rtc_time; + + // rtc_timer_get(&year, &month, &day, &hour, &minute, &second); + RTC_GetDate(RTC_DATA_FMT_DEC, &rtc_date); + RTC_GetTime(RTC_DATA_FMT_DEC, &rtc_time); + + ct.tm_year = rtc_date.u8Year ; + ct.tm_mon = rtc_date.u8Month ; + ct.tm_mday = rtc_date.u8Day; + ct.tm_wday = rtc_date.u8Weekday; + + ct.tm_hour = rtc_time.u8Hour; + ct.tm_min = rtc_time.u8Minute; + ct.tm_sec = rtc_time.u8Second; + + *time = mktime(&ct); + } + break; + case OPER_RTC_SET_TIME: + { + struct tm *ct; + stc_rtc_date_t rtc_date; + stc_rtc_time_t rtc_time; + x_base lock; + + lock = CriticalAreaLock(); + ct = localtime(time); + rtc_date.u8Year = ct->tm_year ; + rtc_date.u8Month = ct->tm_mon ; + rtc_date.u8Day = ct->tm_mday; + rtc_date.u8Weekday = ct->tm_wday; + rtc_time.u8Hour = ct->tm_hour; + rtc_time.u8Minute = ct->tm_min; + rtc_time.u8Second = ct->tm_sec; + CriticalAreaUnLock(lock); + + RTC_SetDate(RTC_DATA_FMT_DEC, &rtc_date); + RTC_SetTime(RTC_DATA_FMT_DEC, &rtc_time); + } + break; + } + return EOK; +} + +/*manage the rtc device operations*/ +static const struct RtcDevDone dev_done = +{ + .open = NONE, + .close = NONE, + .write = NONE, + .read = NONE, +}; + +static int BoardRtcBusInit(struct RtcBus *rtc_bus, struct RtcDriver *rtc_driver) +{ + x_err_t ret = EOK; + + /*Init the rtc bus */ + ret = RtcBusInit(rtc_bus, RTC_BUS_NAME); + if (EOK != ret) { + KPrintf("HwRtcInit RtcBusInit error %d\n", ret); + return ERROR; + } + + /*Init the rtc driver*/ + ret = RtcDriverInit(rtc_driver, RTC_DRV_NAME); + if (EOK != ret) { + KPrintf("HwRtcInit RtcDriverInit error %d\n", ret); + return ERROR; + } + + /*Attach the rtc driver to the rtc bus*/ + ret = RtcDriverAttachToBus(RTC_DRV_NAME, RTC_BUS_NAME); + if (EOK != ret) { + KPrintf("HwRtcInit RtcDriverAttachToBus error %d\n", ret); + return ERROR; + } + + return ret; +} + +/*Attach the rtc device to the rtc bus*/ +static int BoardRtcDevBend(void) +{ + x_err_t ret = EOK; + + static struct RtcHardwareDevice rtc_device; + memset(&rtc_device, 0, sizeof(struct RtcHardwareDevice)); + + rtc_device.dev_done = &(dev_done); + + ret = RtcDeviceRegister(&rtc_device, NONE, RTC_DEVICE_NAME); + if (EOK != ret) { + KPrintf("HwRtcInit RtcDeviceInit device %s error %d\n", RTC_DEVICE_NAME, ret); + return ERROR; + } + + ret = RtcDeviceAttachToBus(RTC_DEVICE_NAME, RTC_BUS_NAME); + if (EOK != ret) { + KPrintf("HwRtcInit RtcDeviceAttachToBus device %s error %d\n", RTC_DEVICE_NAME, ret); + return ERROR; + } + + return ret; +} + + +int HwRtcInit(void) +{ + x_err_t ret = EOK; + + static struct RtcBus rtc_bus; + memset(&rtc_bus, 0, sizeof(struct RtcBus)); + + static struct RtcDriver rtc_driver; + memset(&rtc_driver, 0, sizeof(struct RtcDriver)); + + rtc_driver.configure = &(RtcConfigure); + + ret = BoardRtcBusInit(&rtc_bus, &rtc_driver); + if (EOK != ret) { + KPrintf("HwRtcInit error ret %u\n", ret); + return ERROR; + } + + ret = BoardRtcDevBend(); + if (EOK != ret) { + KPrintf("HwRtcInit error ret %u\n", ret); + } + + stc_rtc_init_t stcRtcInit; + /* Configure structure initialization */ + (void)RTC_StructInit(&stcRtcInit); + + /* Configuration RTC structure */ + stcRtcInit.u8ClockSrc = RTC_CLK_SRC_XTAL32; + stcRtcInit.u8HourFormat = RTC_HOUR_FMT_24H; + stcRtcInit.u8IntPeriod = RTC_INT_PERIOD_PER_SEC; + (void)RTC_Init(&stcRtcInit); + + RTC_Cmd(LL_RTC_ENABLE); + + return ret; +}