From fe30e445d32147d8e9602da7caf4fcdc1ae9d4b3 Mon Sep 17 00:00:00 2001 From: xiaoxiaolisunny Date: Fri, 10 Jun 2022 22:52:35 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0HC32F460=E7=B3=BB?= =?UTF-8?q?=E5=88=97RTC=E5=A4=96=E8=AE=BE=E9=A9=B1=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bsp/hc32/ev_hc32f460_lqfp100_v2/board/Kconfig | 31 ++++ bsp/hc32/libraries/hc32_drivers/SConscript | 3 + bsp/hc32/libraries/hc32_drivers/drv_rtc.c | 171 ++++++++++++++++++ 3 files changed, 205 insertions(+) create mode 100644 bsp/hc32/libraries/hc32_drivers/drv_rtc.c diff --git a/bsp/hc32/ev_hc32f460_lqfp100_v2/board/Kconfig b/bsp/hc32/ev_hc32f460_lqfp100_v2/board/Kconfig index 5a9f8be13d4..c01f0a8ce3f 100644 --- a/bsp/hc32/ev_hc32f460_lqfp100_v2/board/Kconfig +++ b/bsp/hc32/ev_hc32f460_lqfp100_v2/board/Kconfig @@ -88,6 +88,37 @@ menu "On-chip Peripheral Drivers" bool "using can1" default n endif + + menuconfig BSP_USING_ADC + bool "Enable ADC" + default n + select RT_USING_ADC + if BSP_USING_ADC + config BSP_USING_ADC1 + bool "using adc1" + default n + config BSP_USING_ADC2 + bool "using adc2" + default n + endif + + menuconfig BSP_USING_RTC + bool "Enable RTC" + select RT_USING_RTC + default n + + if BSP_USING_RTC + choice + prompt "Select clock source" + default BSP_RTC_USING_LRC + + config BSP_RTC_USING_XTAL32 + bool "RTC USING XTAL32" + + config BSP_RTC_USING_LRC + bool "RTC USING LRC" + endchoice + endif endmenu diff --git a/bsp/hc32/libraries/hc32_drivers/SConscript b/bsp/hc32/libraries/hc32_drivers/SConscript index 41fc861fcb6..da09695d36c 100644 --- a/bsp/hc32/libraries/hc32_drivers/SConscript +++ b/bsp/hc32/libraries/hc32_drivers/SConscript @@ -29,6 +29,9 @@ if GetDepend(['RT_USING_ADC']): if GetDepend(['RT_USING_CAN']): src += ['drv_can.c'] + +if GetDepend(['RT_USING_RTC']): + src += ['drv_rtc.c'] path = [cwd] diff --git a/bsp/hc32/libraries/hc32_drivers/drv_rtc.c b/bsp/hc32/libraries/hc32_drivers/drv_rtc.c new file mode 100644 index 00000000000..09c2280978a --- /dev/null +++ b/bsp/hc32/libraries/hc32_drivers/drv_rtc.c @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2006-2022, RT-Thread Development Team + * Copyright (c) 2022, xiaoxiaolisunny + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2022-06-10 xiaoxiaolisunny first version + */ + +#include +#include +#include +#include +#include +#include + +#ifdef BSP_USING_RTC + +static rt_rtc_dev_t hc32_rtc_dev; + +static rt_err_t hc32_rtc_get_time_stamp(struct timeval *tv) +{ + stc_rtc_time_t stcRtcTime = {0}; + stc_rtc_date_t stcRtcDate = {0}; + struct tm tm_new = {0}; + + RTC_GetTime(RTC_DATA_FMT_DEC, &stcRtcTime); + RTC_GetDate(RTC_DATA_FMT_DEC, &stcRtcDate); + + tm_new.tm_sec = stcRtcTime.u8Second; + tm_new.tm_min = stcRtcTime.u8Minute; + tm_new.tm_hour = stcRtcTime.u8Hour; + tm_new.tm_mday = stcRtcDate.u8Day + 1; + tm_new.tm_mon = stcRtcDate.u8Month; + tm_new.tm_year = stcRtcDate.u8Year + 100; + + tv->tv_sec = timegm(&tm_new); + + return RT_EOK; +} + +static rt_err_t hc32_rtc_set_time_stamp(time_t time_stamp) +{ + stc_rtc_time_t stcRtcTime = {0}; + stc_rtc_date_t stcRtcDate = {0}; + struct tm *p_tm; + + p_tm = gmtime(&time_stamp); + if (p_tm->tm_year < 100) + { + return -RT_ERROR; + } + + stcRtcTime.u8Second = p_tm->tm_sec ; + stcRtcTime.u8Minute = p_tm->tm_min ; + stcRtcTime.u8Hour = p_tm->tm_hour; + stcRtcDate.u8Day = p_tm->tm_mday + 1; + stcRtcDate.u8Month = p_tm->tm_mon ; + stcRtcDate.u8Year = p_tm->tm_year - 100; + stcRtcDate.u8Weekday = p_tm->tm_wday + 1; + + if (LL_OK != RTC_SetTime(RTC_DATA_FMT_DEC, &stcRtcTime)) + { + return -RT_ERROR; + } + if (LL_OK != RTC_SetDate(RTC_DATA_FMT_DEC, &stcRtcDate)) + { + return -RT_ERROR; + } + + LOG_D("set rtc time."); + return RT_EOK; +} + +static rt_err_t hc32_rtc_init(void) +{ + stc_rtc_init_t stcRtcInit; + +#ifdef BSP_RTC_USING_XTAL32 + stc_clock_xtal32_init_t stcXtal32Init; + /* Xtal32 config */ + stcXtal32Init.u8State = CLK_XTAL32_ON; + stcXtal32Init.u8Drv = CLK_XTAL32_DRV_HIGH; + stcXtal32Init.u8Filter = CLK_XTAL32_FILTER_RUN_MD; + (void)CLK_Xtal32Init(&stcXtal32Init); + /* Waiting for XTAL32 stabilization */ + rt_thread_delay(100); +#endif + + /* RTC stopped */ + if (DISABLE == RTC_GetCounterState()) + { + /* Reset RTC counter */ + if (LL_ERR_TIMEOUT == RTC_DeInit()) + { + return -RT_ERROR; + } + else + { + /* Configure structure initialization */ + (void)RTC_StructInit(&stcRtcInit); + /* Configuration RTC structure */ + #ifdef BSP_RTC_USING_XTAL32 + stcRtcInit.u8ClockSrc = RTC_CLK_SRC_XTAL32; + #else + stcRtcInit.u8ClockSrc = RTC_CLK_SRC_LRC; + #endif + stcRtcInit.u8HourFormat = RTC_HOUR_FMT_24H; + stcRtcInit.u8IntPeriod = RTC_INT_PERIOD_PER_SEC; + (void)RTC_Init(&stcRtcInit); + /* Startup RTC count */ + RTC_Cmd(ENABLE); + } + } + return RT_EOK; +} + +static rt_err_t hc32_rtc_get_secs(time_t *sec) +{ + struct timeval tv; + + hc32_rtc_get_time_stamp(&tv); + *(time_t *) sec = tv.tv_sec; + LOG_D("RTC: get rtc_time %d", *sec); + + return RT_EOK; +} + +static rt_err_t hc32_rtc_set_secs(time_t *sec) +{ + rt_err_t result = RT_EOK; + + if (hc32_rtc_set_time_stamp(*sec)) + { + result = -RT_ERROR; + } + LOG_D("RTC: set rtc_time %d", *sec); + + return result; +} + +const static struct rt_rtc_ops hc32_rtc_ops = +{ + hc32_rtc_init, + hc32_rtc_get_secs, + hc32_rtc_set_secs, + RT_NULL, + RT_NULL, + hc32_rtc_get_time_stamp, + RT_NULL +}; + +int rt_hw_rtc_init(void) +{ + rt_err_t result; + + hc32_rtc_dev.ops = &hc32_rtc_ops; + result = rt_hw_rtc_register(&hc32_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_RTC */ From bd779f30a42b5fbb9be98e9ddc1c094637bf34f0 Mon Sep 17 00:00:00 2001 From: xiaoxiaolisunny Date: Sat, 11 Jun 2022 14:17:38 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E6=97=A5=E6=9C=9F=E5=AE=9E=E9=99=85=E6=AF=94=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E5=A4=9A2=E5=A4=A9bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bsp/hc32/libraries/hc32_drivers/drv_rtc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bsp/hc32/libraries/hc32_drivers/drv_rtc.c b/bsp/hc32/libraries/hc32_drivers/drv_rtc.c index 09c2280978a..d99fcf9c23a 100644 --- a/bsp/hc32/libraries/hc32_drivers/drv_rtc.c +++ b/bsp/hc32/libraries/hc32_drivers/drv_rtc.c @@ -56,7 +56,7 @@ static rt_err_t hc32_rtc_set_time_stamp(time_t time_stamp) stcRtcTime.u8Second = p_tm->tm_sec ; stcRtcTime.u8Minute = p_tm->tm_min ; stcRtcTime.u8Hour = p_tm->tm_hour; - stcRtcDate.u8Day = p_tm->tm_mday + 1; + stcRtcDate.u8Day = p_tm->tm_mday - 1; stcRtcDate.u8Month = p_tm->tm_mon ; stcRtcDate.u8Year = p_tm->tm_year - 100; stcRtcDate.u8Weekday = p_tm->tm_wday + 1; From 18946e5ee1ab6b09e2159017414089d2b6d2ae22 Mon Sep 17 00:00:00 2001 From: xiaoxiaolisunny Date: Sun, 12 Jun 2022 12:26:21 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E5=AE=89=E5=85=A8=E5=87=BD=E6=95=B0gmtime=5Fr=E3=80=82=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9B=BF=E6=8D=A2=E5=90=8E=E8=AE=BE=E7=BD=AE?= =?UTF-8?q?=E6=97=A5=E6=9C=9F=E4=BC=9A=E5=A4=8D=E4=BD=8D=E6=88=96=E8=80=85?= =?UTF-8?q?hardfault=E7=9A=84bug=EF=BC=8C=E5=8E=9F=E5=9B=A0=E6=98=AF?= =?UTF-8?q?=E5=AE=9A=E4=B9=89p=5Ftm=E6=8C=87=E9=92=88=E6=9C=AA=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E3=80=82=20=20=20=20=20struct=20tm=20p=5Ftm?= =?UTF-8?q?=20=3D=20{0};?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit gmtime_r(&time_stamp, &p_tm); --- bsp/hc32/libraries/hc32_drivers/drv_rtc.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/bsp/hc32/libraries/hc32_drivers/drv_rtc.c b/bsp/hc32/libraries/hc32_drivers/drv_rtc.c index d99fcf9c23a..1a2fbd9f9c9 100644 --- a/bsp/hc32/libraries/hc32_drivers/drv_rtc.c +++ b/bsp/hc32/libraries/hc32_drivers/drv_rtc.c @@ -45,21 +45,22 @@ static rt_err_t hc32_rtc_set_time_stamp(time_t time_stamp) { stc_rtc_time_t stcRtcTime = {0}; stc_rtc_date_t stcRtcDate = {0}; - struct tm *p_tm; + struct tm p_tm = {0}; - p_tm = gmtime(&time_stamp); - if (p_tm->tm_year < 100) + gmtime_r(&time_stamp, &p_tm); + + if (p_tm.tm_year < 100) { return -RT_ERROR; } - stcRtcTime.u8Second = p_tm->tm_sec ; - stcRtcTime.u8Minute = p_tm->tm_min ; - stcRtcTime.u8Hour = p_tm->tm_hour; - stcRtcDate.u8Day = p_tm->tm_mday - 1; - stcRtcDate.u8Month = p_tm->tm_mon ; - stcRtcDate.u8Year = p_tm->tm_year - 100; - stcRtcDate.u8Weekday = p_tm->tm_wday + 1; + stcRtcTime.u8Second = p_tm.tm_sec ; + stcRtcTime.u8Minute = p_tm.tm_min ; + stcRtcTime.u8Hour = p_tm.tm_hour; + stcRtcDate.u8Day = p_tm.tm_mday - 1; + stcRtcDate.u8Month = p_tm.tm_mon ; + stcRtcDate.u8Year = p_tm.tm_year - 100; + stcRtcDate.u8Weekday = p_tm.tm_wday + 1; if (LL_OK != RTC_SetTime(RTC_DATA_FMT_DEC, &stcRtcTime)) { From fc73a74e477a9b89b978e5c7121f8d595dc77cd6 Mon Sep 17 00:00:00 2001 From: xiaoxiaolisunny Date: Mon, 13 Jun 2022 15:20:47 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9drv=5Frtc.c=E6=96=87?= =?UTF-8?q?=E4=BB=B6=EF=BC=8CHC32=E5=88=9D=E5=A7=8B=E5=8C=96=E5=90=8E?= =?UTF-8?q?=E8=AF=BB=E5=8F=96=E7=9A=84=E6=97=A5=E6=9C=9F=E9=83=BD=E6=98=AF?= =?UTF-8?q?0=EF=BC=8C=E6=98=AF=E6=97=A0=E6=95=88=E5=80=BC=EF=BC=8C?= =?UTF-8?q?=E8=BF=99=E9=87=8C=E9=9C=80=E8=A6=81=E5=88=A4=E6=96=AD=E5=A4=84?= =?UTF-8?q?=E7=90=86=E3=80=82=20=E8=AE=BE=E7=BD=AE=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E5=90=8E=E8=AF=BB=E5=87=BA=E7=9A=84=E6=89=8D=E6=98=AF=E6=9C=89?= =?UTF-8?q?=E6=95=88=E6=95=B0=E6=8D=AE=EF=BC=8C=E6=97=A51~31=EF=BC=8C?= =?UTF-8?q?=E6=9C=881~12=EF=BC=8C=E4=BF=AE=E6=94=B9=E4=B8=8Etm=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E8=8C=83=E5=9B=B4=E4=B8=80=E8=87=B4=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bsp/hc32/libraries/hc32_drivers/drv_rtc.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/bsp/hc32/libraries/hc32_drivers/drv_rtc.c b/bsp/hc32/libraries/hc32_drivers/drv_rtc.c index 1a2fbd9f9c9..41729340765 100644 --- a/bsp/hc32/libraries/hc32_drivers/drv_rtc.c +++ b/bsp/hc32/libraries/hc32_drivers/drv_rtc.c @@ -14,7 +14,6 @@ #include #include #include -#include #ifdef BSP_USING_RTC @@ -32,8 +31,16 @@ static rt_err_t hc32_rtc_get_time_stamp(struct timeval *tv) tm_new.tm_sec = stcRtcTime.u8Second; tm_new.tm_min = stcRtcTime.u8Minute; tm_new.tm_hour = stcRtcTime.u8Hour; - tm_new.tm_mday = stcRtcDate.u8Day + 1; - tm_new.tm_mon = stcRtcDate.u8Month; + if(stcRtcDate.u8Month == 0) + { + tm_new.tm_mday = stcRtcDate.u8Day + 1; + tm_new.tm_mon = stcRtcDate.u8Month; + } + else + { + tm_new.tm_mday = stcRtcDate.u8Day ; + tm_new.tm_mon = stcRtcDate.u8Month - 1; + } tm_new.tm_year = stcRtcDate.u8Year + 100; tv->tv_sec = timegm(&tm_new); @@ -57,8 +64,8 @@ static rt_err_t hc32_rtc_set_time_stamp(time_t time_stamp) stcRtcTime.u8Second = p_tm.tm_sec ; stcRtcTime.u8Minute = p_tm.tm_min ; stcRtcTime.u8Hour = p_tm.tm_hour; - stcRtcDate.u8Day = p_tm.tm_mday - 1; - stcRtcDate.u8Month = p_tm.tm_mon ; + stcRtcDate.u8Day = p_tm.tm_mday; + stcRtcDate.u8Month = p_tm.tm_mon + 1; stcRtcDate.u8Year = p_tm.tm_year - 100; stcRtcDate.u8Weekday = p_tm.tm_wday + 1; From d7d6785bf4d7655bd54c01741972c4b008b91c8e Mon Sep 17 00:00:00 2001 From: xiaoxiaolisunny Date: Mon, 13 Jun 2022 17:19:20 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9drv=5Frtc.c,Weekday?= =?UTF-8?q?=E5=8E=BB=E6=8E=89+1=EF=BC=8C=E5=8E=BB=E6=8E=89IntPeriod?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bsp/hc32/libraries/hc32_drivers/drv_rtc.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bsp/hc32/libraries/hc32_drivers/drv_rtc.c b/bsp/hc32/libraries/hc32_drivers/drv_rtc.c index 41729340765..adf2eb7e1bb 100644 --- a/bsp/hc32/libraries/hc32_drivers/drv_rtc.c +++ b/bsp/hc32/libraries/hc32_drivers/drv_rtc.c @@ -67,7 +67,7 @@ static rt_err_t hc32_rtc_set_time_stamp(time_t time_stamp) stcRtcDate.u8Day = p_tm.tm_mday; stcRtcDate.u8Month = p_tm.tm_mon + 1; stcRtcDate.u8Year = p_tm.tm_year - 100; - stcRtcDate.u8Weekday = p_tm.tm_wday + 1; + stcRtcDate.u8Weekday = p_tm.tm_wday; if (LL_OK != RTC_SetTime(RTC_DATA_FMT_DEC, &stcRtcTime)) { @@ -116,7 +116,6 @@ static rt_err_t hc32_rtc_init(void) stcRtcInit.u8ClockSrc = RTC_CLK_SRC_LRC; #endif stcRtcInit.u8HourFormat = RTC_HOUR_FMT_24H; - stcRtcInit.u8IntPeriod = RTC_INT_PERIOD_PER_SEC; (void)RTC_Init(&stcRtcInit); /* Startup RTC count */ RTC_Cmd(ENABLE);