forked from xuos/xiuos
adapt rtc driver for hc32f4a0
This commit is contained in:
parent
b2de31844a
commit
d2e6719a0e
|
@ -14,13 +14,17 @@ void TestRTC(int argc,char *argv[])
|
||||||
|
|
||||||
int times = atoi(argv[1]);
|
int times = atoi(argv[1]);
|
||||||
printf("Time will be printf %d times\n",times);
|
printf("Time will be printf %d times\n",times);
|
||||||
|
|
||||||
struct RtcDrvConfigureParam rtc_para;
|
struct RtcDrvConfigureParam rtc_para;
|
||||||
|
time_t my_time=0;
|
||||||
|
|
||||||
rtc_para.rtc_operation_cmd = OPER_RTC_SET_TIME;
|
rtc_para.rtc_operation_cmd = OPER_RTC_SET_TIME;
|
||||||
*(rtc_para.time) = 0;
|
rtc_para.time = &my_time;
|
||||||
|
|
||||||
struct PrivIoctlCfg ioctl_cfg;
|
struct PrivIoctlCfg ioctl_cfg;
|
||||||
ioctl_cfg.ioctl_driver_type = RTC_TYPE;
|
ioctl_cfg.ioctl_driver_type = RTC_TYPE;
|
||||||
ioctl_cfg.args = (void *)&rtc_para;
|
ioctl_cfg.args = (void *)&rtc_para;
|
||||||
|
|
||||||
PrivIoctl(rtc_fd,0,&ioctl_cfg);
|
PrivIoctl(rtc_fd,0,&ioctl_cfg);
|
||||||
|
|
||||||
rtc_para.rtc_operation_cmd = OPER_RTC_GET_TIME;
|
rtc_para.rtc_operation_cmd = OPER_RTC_GET_TIME;
|
||||||
|
|
|
@ -54,6 +54,10 @@ Modification:
|
||||||
#include <connect_usb.h>
|
#include <connect_usb.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef BSP_USING_RTC
|
||||||
|
#include <connect_rtc.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
extern void entry(void);
|
extern void entry(void);
|
||||||
extern int HwUsartInit();
|
extern int HwUsartInit();
|
||||||
|
|
||||||
|
@ -167,6 +171,9 @@ struct InitSequenceDesc _board_init[] =
|
||||||
#endif
|
#endif
|
||||||
#ifdef BSP_USING_USB
|
#ifdef BSP_USING_USB
|
||||||
{ "usb", HwUsbHostInit },
|
{ "usb", HwUsbHostInit },
|
||||||
|
#endif
|
||||||
|
#ifdef BSP_USING_RTC
|
||||||
|
{ "usb", HwRtcInit },
|
||||||
#endif
|
#endif
|
||||||
{ " NONE ", NONE },
|
{ " NONE ", NONE },
|
||||||
};
|
};
|
||||||
|
|
|
@ -53,3 +53,11 @@ menuconfig BSP_USING_USB
|
||||||
if BSP_USING_USB
|
if BSP_USING_USB
|
||||||
source "$BSP_DIR/third_party_driver/usb/Kconfig"
|
source "$BSP_DIR/third_party_driver/usb/Kconfig"
|
||||||
endif
|
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
|
||||||
|
|
|
@ -28,4 +28,8 @@ ifeq ($(CONFIG_BSP_USING_USB),y)
|
||||||
SRC_DIR += usb
|
SRC_DIR += usb
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifeq ($(CONFIG_BSP_USING_RTC),y)
|
||||||
|
SRC_DIR += rtc
|
||||||
|
endif
|
||||||
|
|
||||||
include $(KERNEL_ROOT)/compiler.mk
|
include $(KERNEL_ROOT)/compiler.mk
|
||||||
|
|
|
@ -24,4 +24,8 @@ ifeq ($(CONFIG_BSP_USING_USB),y)
|
||||||
SRC_FILES += hc32_ll_usb.c
|
SRC_FILES += hc32_ll_usb.c
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifeq ($(CONFIG_BSP_USING_RTC),y)
|
||||||
|
SRC_FILES += hc32_ll_rtc.c
|
||||||
|
endif
|
||||||
|
|
||||||
include $(KERNEL_ROOT)/compiler.mk
|
include $(KERNEL_ROOT)/compiler.mk
|
||||||
|
|
|
@ -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 <device.h>
|
||||||
|
#include <hc32_ll_rtc.h>
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int HwRtcInit(void);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
|
@ -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
|
|
@ -0,0 +1,2 @@
|
||||||
|
SRC_FILES := connect_rtc.c
|
||||||
|
include $(KERNEL_ROOT)/compiler.mk
|
|
@ -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 <connect_rtc.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
// 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;
|
||||||
|
}
|
Loading…
Reference in New Issue