From b96033aef21ec905686065983fa0d4e7e779430c Mon Sep 17 00:00:00 2001 From: dongly Date: Thu, 30 Oct 2025 20:43:25 +0800 Subject: [PATCH 1/7] =?UTF-8?q?[soft=5Frtc]=E4=BF=AE=E6=AD=A3GET=5FTIMESPE?= =?UTF-8?q?C/GET=5FTIMEVAL=20=E8=8E=B7=E5=8F=96=20ns/us=20=E7=9A=84?= =?UTF-8?q?=E9=94=99=E8=AF=AF;=20=E4=BC=98=E5=8C=96soft=5Frtc=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/rtc/dev_soft_rtc.c | 192 ++++++++++++++++---------- 1 file changed, 117 insertions(+), 75 deletions(-) diff --git a/components/drivers/rtc/dev_soft_rtc.c b/components/drivers/rtc/dev_soft_rtc.c index cd5a37ee027..7cd13f792a1 100644 --- a/components/drivers/rtc/dev_soft_rtc.c +++ b/components/drivers/rtc/dev_soft_rtc.c @@ -6,10 +6,10 @@ * Change Logs: * Date Author Notes * 2018-01-30 armink the first version + * 2025-10-30 dongly fix timespec/timeval error, optimize soft_rtc implementation */ #include -#include #include #include @@ -20,11 +20,11 @@ #ifdef RT_USING_SOFT_RTC /* 2018-01-30 14:44:50 = RTC_TIME_INIT(2018, 1, 30, 14, 44, 50) */ -#define RTC_TIME_INIT(year, month, day, hour, minute, second) \ - {.tm_year = year - 1900, .tm_mon = month - 1, .tm_mday = day, .tm_hour = hour, .tm_min = minute, .tm_sec = second} +#define RTC_TIME_INIT(year, month, day, hour, minute, second) \ + { .tm_year = year - 1900, .tm_mon = month - 1, .tm_mday = day, .tm_hour = hour, .tm_min = minute, .tm_sec = second } #ifndef SOFT_RTC_TIME_DEFAULT -#define SOFT_RTC_TIME_DEFAULT RTC_TIME_INIT(2018, 1, 1, 0, 0 ,0) +#define SOFT_RTC_TIME_DEFAULT RTC_TIME_INIT(2018, 1, 1, 0, 0, 0) #endif #ifndef RTC_AUTO_SYNC_FIRST_DELAY @@ -37,19 +37,20 @@ static struct rt_work rtc_sync_work; static struct rt_device soft_rtc_dev; -static rt_tick_t init_tick; -static time_t init_time; -static struct timeval init_tv = {0}; - #ifdef RT_USING_KTIME -static struct timespec init_ts = {0}; +static struct timespec init_ktime_ts = { 0 }; +#else +static rt_tick_t init_tick; #endif +static struct timespec init_ts = { 0 }; #ifdef RT_USING_ALARM static struct rt_rtc_wkalarm wkalarm; static struct rt_timer alarm_time; +static RT_DEFINE_SPINLOCK(_spinlock); + static void alarm_timeout(void *param) { rt_alarm_update(param, 1); @@ -73,17 +74,61 @@ static void soft_rtc_alarm_update(struct rt_rtc_wkalarm *palarm) #endif -static void set_rtc_time(time_t t) +static void set_rtc_time(struct timespec *ts) { - init_time = t - (rt_tick_get() - init_tick) / RT_TICK_PER_SECOND; + rt_base_t level = rt_spin_lock_irqsave(&_spinlock); + init_ts.tv_sec = ts->tv_sec; + init_ts.tv_nsec = ts->tv_nsec; +#ifdef RT_USING_KTIME + rt_ktime_boottime_get_ns(&init_ktime_ts); +#else + init_tick = rt_tick_get(); +#endif + rt_spin_unlock_irqrestore(&_spinlock, level); #ifdef RT_USING_ALARM soft_rtc_alarm_update(&wkalarm); #endif } +static void get_rtc_time(struct timespec *ts) +{ + rt_base_t level; + + if (!ts) + return; + + level = rt_spin_lock_irqsave(&_spinlock); +#ifdef RT_USING_KTIME + struct timespec current_ts; + rt_ktime_boottime_get_ns(¤t_ts); + + /* Calculate time difference */ + ts->tv_sec = init_ktime_ts.tv_sec + (current_ts.tv_sec - init_ktime_ts.tv_sec); + ts->tv_nsec = init_ktime_ts.tv_nsec + (current_ts.tv_nsec - init_ktime_ts.tv_nsec); +#else + rt_tick_t tick = rt_tick_get_delta(init_tick); + ts->tv_sec = init_ts.tv_sec + tick / RT_TICK_PER_SECOND; + ts->tv_nsec = init_ts.tv_nsec + ((tick % RT_TICK_PER_SECOND) * (1000000000UL / RT_TICK_PER_SECOND)); +#endif + /* Handle nanosecond overflow */ + if (ts->tv_nsec >= 1000000000L) + { + ts->tv_sec++; + ts->tv_nsec -= 1000000000L; + } + /* Handle nanosecond underflow */ + if (ts->tv_nsec < 0) + { + ts->tv_sec--; + ts->tv_nsec += 1000000000L; + } + rt_spin_unlock_irqrestore(&_spinlock, level); +} + static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args) { time_t *t; + rt_base_t level; struct tm time_temp; RT_ASSERT(dev != RT_NULL); @@ -93,94 +138,87 @@ static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args) { case RT_DEVICE_CTRL_RTC_GET_TIME: { - t = (time_t *) args; - *t = init_time + (rt_tick_get() - init_tick) / RT_TICK_PER_SECOND; + if (!args) + return -RT_EINVAL; + t = (time_t *)args; + struct timespec ts; + get_rtc_time(&ts); + *t = ts.tv_sec; break; } case RT_DEVICE_CTRL_RTC_SET_TIME: { - t = (time_t *) args; - set_rtc_time(*t); + if (!args) + return -RT_EINVAL; + t = (time_t *)args; + struct timespec ts = { *t, 0 }; + set_rtc_time(&ts); break; } #ifdef RT_USING_ALARM case RT_DEVICE_CTRL_RTC_GET_ALARM: + if (!args) + return -RT_EINVAL; *((struct rt_rtc_wkalarm *)args) = wkalarm; break; case RT_DEVICE_CTRL_RTC_SET_ALARM: + if (!args) + return -RT_EINVAL; wkalarm = *((struct rt_rtc_wkalarm *)args); soft_rtc_alarm_update(&wkalarm); break; #endif -#ifdef RT_USING_KTIME case RT_DEVICE_CTRL_RTC_GET_TIMEVAL: { - struct timeval _tv; + if (!args) + return -RT_EINVAL; struct timeval *tv = (struct timeval *)args; - rt_ktime_boottime_get_us(&_tv); - tv->tv_sec = init_time + _tv.tv_sec; - tv->tv_usec = init_tv.tv_usec + _tv.tv_usec; + struct timespec ts; + get_rtc_time(&ts); + tv->tv_sec = ts.tv_sec; + tv->tv_usec = ts.tv_nsec / 1000; break; } case RT_DEVICE_CTRL_RTC_SET_TIMEVAL: { - struct timeval _tv; + if (!args) + return -RT_EINVAL; struct timeval *tv = (struct timeval *)args; - rt_ktime_boottime_get_us(&_tv); - set_rtc_time(tv->tv_sec); - init_tv.tv_usec = tv->tv_usec - _tv.tv_usec; + struct timespec ts = { tv->tv_sec, tv->tv_usec * 1000 }; + set_rtc_time(&ts); break; } case RT_DEVICE_CTRL_RTC_GET_TIMESPEC: { - struct timespec _ts; + if (!args) + return -RT_EINVAL; struct timespec *ts = (struct timespec *)args; - rt_ktime_boottime_get_ns(&_ts); - ts->tv_sec = init_time + _ts.tv_sec; - ts->tv_nsec = init_ts.tv_nsec + _ts.tv_nsec; + get_rtc_time(ts); break; } case RT_DEVICE_CTRL_RTC_SET_TIMESPEC: { - struct timespec _ts; + if (!args) + return -RT_EINVAL; struct timespec *ts = (struct timespec *)args; - rt_ktime_boottime_get_ns(&_ts); - set_rtc_time(ts->tv_sec); - init_ts.tv_nsec = ts->tv_nsec - _ts.tv_nsec; + set_rtc_time(ts); break; } case RT_DEVICE_CTRL_RTC_GET_TIMERES: { + if (!args) + return -RT_EINVAL; struct timespec *ts = (struct timespec *)args; - ts->tv_sec = 0; + level = rt_spin_lock_irqsave(&_spinlock); + ts->tv_sec = 0; +#ifdef RT_USING_KTIME ts->tv_nsec = (rt_ktime_cputimer_getres() / RT_KTIME_RESMUL); - break; - } #else - case RT_DEVICE_CTRL_RTC_GET_TIMEVAL: - { - struct timeval *tv = (struct timeval *)args; - rt_tick_t tick = rt_tick_get() - init_tick; - tv->tv_sec = init_time + tick / RT_TICK_PER_SECOND; - tv->tv_usec = init_tv.tv_usec + ((tick % RT_TICK_PER_SECOND) * (1000000 / RT_TICK_PER_SECOND)); - break; - } - case RT_DEVICE_CTRL_RTC_SET_TIMEVAL: - { - struct timeval *tv = (struct timeval *)args; - rt_tick_t tick = rt_tick_get() - init_tick; - set_rtc_time(tv->tv_sec); - init_tv.tv_usec = tv->tv_usec - ((tick % RT_TICK_PER_SECOND) * (1000000 / RT_TICK_PER_SECOND)); - break; - } - case RT_DEVICE_CTRL_RTC_GET_TIMERES: - { - struct timespec *ts = (struct timespec *)args; - ts->tv_sec = 0; ts->tv_nsec = (1000UL * 1000 * 1000) / RT_TICK_PER_SECOND; +#endif + rt_spin_unlock_irqrestore(&_spinlock, level); break; } -#endif /* RT_USING_KTIME */ default: return -RT_EINVAL; } @@ -189,8 +227,7 @@ static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args) } #ifdef RT_USING_DEVICE_OPS -const static struct rt_device_ops soft_rtc_ops = -{ +const static struct rt_device_ops soft_rtc_ops = { RT_NULL, RT_NULL, RT_NULL, @@ -209,7 +246,7 @@ static int rt_soft_rtc_init(void) { return 0; } - /* make sure only one 'rtc' device */ + /* Make sure only one 'rtc' device */ #if defined(RT_USING_SOFT_RTC) && defined(BSP_USING_ONCHIP_RTC) #warning "Please note: Currently only one RTC device is allowed in the system, and the name is "rtc"." #endif @@ -221,27 +258,31 @@ static int rt_soft_rtc_init(void) alarm_timeout, &soft_rtc_dev, 0, - RT_TIMER_FLAG_SOFT_TIMER|RT_TIMER_FLAG_ONE_SHOT); + RT_TIMER_FLAG_SOFT_TIMER | RT_TIMER_FLAG_ONE_SHOT); #endif +#ifdef RT_USING_KTIME + rt_ktime_boottime_get_ns(&init_ktime_ts); +#else init_tick = rt_tick_get(); - init_time = timegm(&time_new); +#endif + init_ts.tv_sec = timegm(&time_new); - soft_rtc_dev.type = RT_Device_Class_RTC; + soft_rtc_dev.type = RT_Device_Class_RTC; - /* register rtc device */ + /* Register RTC device */ #ifdef RT_USING_DEVICE_OPS - soft_rtc_dev.ops = &soft_rtc_ops; + soft_rtc_dev.ops = &soft_rtc_ops; #else - soft_rtc_dev.init = RT_NULL; - soft_rtc_dev.open = RT_NULL; - soft_rtc_dev.close = RT_NULL; - soft_rtc_dev.read = RT_NULL; - soft_rtc_dev.write = RT_NULL; + soft_rtc_dev.init = RT_NULL; + soft_rtc_dev.open = RT_NULL; + soft_rtc_dev.close = RT_NULL; + soft_rtc_dev.read = RT_NULL; + soft_rtc_dev.write = RT_NULL; soft_rtc_dev.control = soft_rtc_control; #endif - /* no private */ + /* No private data */ soft_rtc_dev.user_data = RT_NULL; rt_device_register(&soft_rtc_dev, "rtc", RT_DEVICE_FLAG_RDWR); @@ -259,7 +300,8 @@ rt_err_t rt_soft_rtc_sync(void) time_t time = 0; rt_device_control(&soft_rtc_dev, RT_DEVICE_CTRL_RTC_GET_TIME, &time); - set_rtc_time(time); + struct timespec ts = { time, 0 }; + set_rtc_time(&ts); return RT_EOK; } @@ -284,15 +326,15 @@ rt_err_t rt_soft_rtc_set_source(const char *name) #include static void cmd_rtc_sync(int argc, char **argv) { - struct timeval tv = {0}; - struct timezone tz = {0}; - time_t now = (time_t)0; + struct timeval tv = { 0 }; + struct timezone tz = { 0 }; + time_t now = (time_t)0; rt_soft_rtc_sync(); gettimeofday(&tv, &tz); now = tv.tv_sec; - /* output current time */ + /* Output current time */ rt_kprintf("local time: %.*s", 25, ctime(&now)); rt_kprintf("timestamps: %ld\n", (long)tv.tv_sec); } From 05ee7e4bb12e5de4e67c8a4691b925de9fbf206b Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 30 Oct 2025 12:47:58 +0000 Subject: [PATCH 2/7] style: format code with clang-format [skip ci] --- components/drivers/rtc/dev_soft_rtc.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/components/drivers/rtc/dev_soft_rtc.c b/components/drivers/rtc/dev_soft_rtc.c index 7cd13f792a1..69184f2fcb5 100644 --- a/components/drivers/rtc/dev_soft_rtc.c +++ b/components/drivers/rtc/dev_soft_rtc.c @@ -20,8 +20,10 @@ #ifdef RT_USING_SOFT_RTC /* 2018-01-30 14:44:50 = RTC_TIME_INIT(2018, 1, 30, 14, 44, 50) */ -#define RTC_TIME_INIT(year, month, day, hour, minute, second) \ - { .tm_year = year - 1900, .tm_mon = month - 1, .tm_mday = day, .tm_hour = hour, .tm_min = minute, .tm_sec = second } +#define RTC_TIME_INIT(year, month, day, hour, minute, second) \ + { \ + .tm_year = year - 1900, .tm_mon = month - 1, .tm_mday = day, .tm_hour = hour, .tm_min = minute, .tm_sec = second \ + } #ifndef SOFT_RTC_TIME_DEFAULT #define SOFT_RTC_TIME_DEFAULT RTC_TIME_INIT(2018, 1, 1, 0, 0, 0) From 76c1384292c6e82362a5941ac510d202a545c490 Mon Sep 17 00:00:00 2001 From: dongly Date: Fri, 31 Oct 2025 11:57:20 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/rtc/dev_soft_rtc.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/components/drivers/rtc/dev_soft_rtc.c b/components/drivers/rtc/dev_soft_rtc.c index 69184f2fcb5..1d4662c1f45 100644 --- a/components/drivers/rtc/dev_soft_rtc.c +++ b/components/drivers/rtc/dev_soft_rtc.c @@ -37,22 +37,19 @@ #endif static struct rt_work rtc_sync_work; - static struct rt_device soft_rtc_dev; +static struct timespec init_ts = { 0 }; +static RT_DEFINE_SPINLOCK(_spinlock); #ifdef RT_USING_KTIME static struct timespec init_ktime_ts = { 0 }; #else static rt_tick_t init_tick; #endif -static struct timespec init_ts = { 0 }; #ifdef RT_USING_ALARM - static struct rt_rtc_wkalarm wkalarm; static struct rt_timer alarm_time; -static RT_DEFINE_SPINLOCK(_spinlock); - static void alarm_timeout(void *param) { rt_alarm_update(param, 1); From bc97852d913a291e915c9799cfd9f159982bee59 Mon Sep 17 00:00:00 2001 From: dongly Date: Fri, 31 Oct 2025 12:09:02 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/rtc/dev_soft_rtc.c | 109 ++++++++++++++++++++++++-- 1 file changed, 102 insertions(+), 7 deletions(-) diff --git a/components/drivers/rtc/dev_soft_rtc.c b/components/drivers/rtc/dev_soft_rtc.c index 1d4662c1f45..30c9f678baa 100644 --- a/components/drivers/rtc/dev_soft_rtc.c +++ b/components/drivers/rtc/dev_soft_rtc.c @@ -38,8 +38,9 @@ static struct rt_work rtc_sync_work; static struct rt_device soft_rtc_dev; -static struct timespec init_ts = { 0 }; static RT_DEFINE_SPINLOCK(_spinlock); +/* RTC time baseline for calculation */ +static struct timespec init_ts = { 0 }; #ifdef RT_USING_KTIME static struct timespec init_ktime_ts = { 0 }; #else @@ -50,11 +51,28 @@ static rt_tick_t init_tick; static struct rt_rtc_wkalarm wkalarm; static struct rt_timer alarm_time; +/** + * @brief Alarm timeout callback function + * @param param Pointer to RTC device + * @return None + * + * This function is called when the alarm timer expires and updates + * the alarm system. + */ static void alarm_timeout(void *param) { rt_alarm_update(param, 1); } +/** + * @brief Update soft RTC alarm status + * @param palarm Pointer to alarm configuration structure + * @return None + * + * This function updates the alarm timer based on the alarm enable status. + * When enabled, it starts a 1-second period timer for alarm detection. + * When disabled, it stops the timer. + */ static void soft_rtc_alarm_update(struct rt_rtc_wkalarm *palarm) { rt_tick_t next_tick; @@ -73,6 +91,16 @@ static void soft_rtc_alarm_update(struct rt_rtc_wkalarm *palarm) #endif +/** + * @brief Set RTC time baseline + * @param ts Pointer to timestamp to set as baseline + * @return None + * + * This function sets a new time baseline for the soft RTC. All subsequent + * time calculations will be based on this baseline. It records both the + * time value and the corresponding system tick or high-precision time. + * Also updates alarm status if alarms are enabled. + */ static void set_rtc_time(struct timespec *ts) { rt_base_t level = rt_spin_lock_irqsave(&_spinlock); @@ -89,6 +117,16 @@ static void set_rtc_time(struct timespec *ts) #endif } +/** + * @brief Get current RTC time + * @param ts Output parameter to store the retrieved timestamp + * @return None + * + * This function calculates the current time based on the stored baseline + * and the elapsed system tick or high-precision time. It handles both + * nanosecond overflow and underflow to ensure accurate time representation. + * The calculation is thread-safe using spinlock protection. + */ static void get_rtc_time(struct timespec *ts) { rt_base_t level; @@ -101,7 +139,6 @@ static void get_rtc_time(struct timespec *ts) struct timespec current_ts; rt_ktime_boottime_get_ns(¤t_ts); - /* Calculate time difference */ ts->tv_sec = init_ktime_ts.tv_sec + (current_ts.tv_sec - init_ktime_ts.tv_sec); ts->tv_nsec = init_ktime_ts.tv_nsec + (current_ts.tv_nsec - init_ktime_ts.tv_nsec); #else @@ -109,13 +146,12 @@ static void get_rtc_time(struct timespec *ts) ts->tv_sec = init_ts.tv_sec + tick / RT_TICK_PER_SECOND; ts->tv_nsec = init_ts.tv_nsec + ((tick % RT_TICK_PER_SECOND) * (1000000000UL / RT_TICK_PER_SECOND)); #endif - /* Handle nanosecond overflow */ + /* Handle nanosecond overflow/underflow */ if (ts->tv_nsec >= 1000000000L) { ts->tv_sec++; ts->tv_nsec -= 1000000000L; } - /* Handle nanosecond underflow */ if (ts->tv_nsec < 0) { ts->tv_sec--; @@ -124,6 +160,24 @@ static void get_rtc_time(struct timespec *ts) rt_spin_unlock_irqrestore(&_spinlock, level); } +/** + * @brief RTC device control function + * @param dev Pointer to RTC device + * @param cmd Control command (RT_DEVICE_CTRL_RTC_*) + * @param args Command arguments (varies by command) + * @return rt_err_t RT_EOK on success, -RT_EINVAL on error + * + * This function handles various RTC control commands including: + * - RT_DEVICE_CTRL_RTC_GET_TIME: Get current time as time_t + * - RT_DEVICE_CTRL_RTC_SET_TIME: Set time from time_t + * - RT_DEVICE_CTRL_RTC_GET_ALARM: Get alarm configuration + * - RT_DEVICE_CTRL_RTC_SET_ALARM: Set alarm configuration + * - RT_DEVICE_CTRL_RTC_GET_TIMEVAL: Get time as timeval + * - RT_DEVICE_CTRL_RTC_SET_TIMEVAL: Set time from timeval + * - RT_DEVICE_CTRL_RTC_GET_TIMESPEC: Get time as timespec + * - RT_DEVICE_CTRL_RTC_SET_TIMESPEC: Set time from timespec + * - RT_DEVICE_CTRL_RTC_GET_TIMERES: Get timer resolution + */ static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args) { time_t *t; @@ -236,6 +290,15 @@ const static struct rt_device_ops soft_rtc_ops = { }; #endif +/** + * @brief Soft RTC device initialization + * @return int 0 on success + * + * This function initializes the soft RTC device, registers it to the system, + * and sets the default time. It ensures only one RTC device named "rtc" + * exists in the system and configures the device operations. + * The initialization is performed only once. + */ static int rt_soft_rtc_init(void) { static rt_bool_t init_ok = RT_FALSE; @@ -245,7 +308,6 @@ static int rt_soft_rtc_init(void) { return 0; } - /* Make sure only one 'rtc' device */ #if defined(RT_USING_SOFT_RTC) && defined(BSP_USING_ONCHIP_RTC) #warning "Please note: Currently only one RTC device is allowed in the system, and the name is "rtc"." #endif @@ -294,6 +356,13 @@ INIT_DEVICE_EXPORT(rt_soft_rtc_init); #ifdef RT_USING_SYSTEM_WORKQUEUE +/** + * @brief Soft RTC time synchronization + * @return rt_err_t RT_EOK on success + * + * This function retrieves the current RTC time and resets the time baseline. + * It's used to synchronize the soft RTC time with an external time source. + */ rt_err_t rt_soft_rtc_sync(void) { time_t time = 0; @@ -304,16 +373,34 @@ rt_err_t rt_soft_rtc_sync(void) return RT_EOK; } +/** + * @brief RTC sync work function + * @param work Pointer to work item + * @param work_data Work data (unused) + * @return None + * + * This function is executed periodically to maintain soft RTC time accuracy. + * It performs synchronization and schedules the next sync task. + */ static void rtc_sync_work_func(struct rt_work *work, void *work_data) { rt_soft_rtc_sync(); rt_work_submit(work, rt_tick_from_millisecond(RTC_AUTO_SYNC_PERIOD * 1000)); } +/** + * @brief Set soft RTC time source + * @param name Name of the time source device + * @return rt_err_t RT_EOK on success + * + * This function configures the soft RTC to use a specific time source + * and starts the periodic synchronization mechanism. The time source + * device must exist before calling this function. + */ rt_err_t rt_soft_rtc_set_source(const char *name) { RT_ASSERT(name != RT_NULL); - RT_ASSERT(rt_device_find(name)); /* make sure source is exist*/ + RT_ASSERT(rt_device_find(name)); rt_work_init(&rtc_sync_work, rtc_sync_work_func, RT_NULL); rt_work_submit(&rtc_sync_work, rt_tick_from_millisecond(RTC_AUTO_SYNC_FIRST_DELAY * 1000)); @@ -323,6 +410,15 @@ rt_err_t rt_soft_rtc_set_source(const char *name) #ifdef FINSH_USING_MSH #include +/** + * @brief RTC sync command handler + * @param argc Argument count + * @param argv Argument array + * @return None + * + * MSH command that manually triggers RTC time synchronization and displays + * the current time information. Usage: rtc_sync + */ static void cmd_rtc_sync(int argc, char **argv) { struct timeval tv = { 0 }; @@ -333,7 +429,6 @@ static void cmd_rtc_sync(int argc, char **argv) gettimeofday(&tv, &tz); now = tv.tv_sec; - /* Output current time */ rt_kprintf("local time: %.*s", 25, ctime(&now)); rt_kprintf("timestamps: %ld\n", (long)tv.tv_sec); } From 21b0cd1c1bd8a8fa7c86ec816bb0a255ed775acc Mon Sep 17 00:00:00 2001 From: dongly Date: Fri, 31 Oct 2025 12:34:36 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E4=BC=98=E5=8C=96=20RTC=5FTIME=5FINIT=20?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/rtc/dev_soft_rtc.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/components/drivers/rtc/dev_soft_rtc.c b/components/drivers/rtc/dev_soft_rtc.c index 30c9f678baa..874b87a266c 100644 --- a/components/drivers/rtc/dev_soft_rtc.c +++ b/components/drivers/rtc/dev_soft_rtc.c @@ -20,9 +20,14 @@ #ifdef RT_USING_SOFT_RTC /* 2018-01-30 14:44:50 = RTC_TIME_INIT(2018, 1, 30, 14, 44, 50) */ -#define RTC_TIME_INIT(year, month, day, hour, minute, second) \ - { \ - .tm_year = year - 1900, .tm_mon = month - 1, .tm_mday = day, .tm_hour = hour, .tm_min = minute, .tm_sec = second \ +#define RTC_TIME_INIT(year, month, day, hour, minute, second) \ + { \ + .tm_year = year - 1900, \ + .tm_mon = month - 1, \ + .tm_mday = day, \ + .tm_hour = hour, \ + .tm_min = minute, \ + .tm_sec = second, \ } #ifndef SOFT_RTC_TIME_DEFAULT From 82bc8981759f1330ffc3b3aeea8fc546d84fcbb4 Mon Sep 17 00:00:00 2001 From: dongly Date: Fri, 31 Oct 2025 12:40:24 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/rtc/dev_soft_rtc.c | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/components/drivers/rtc/dev_soft_rtc.c b/components/drivers/rtc/dev_soft_rtc.c index 874b87a266c..0acab4e6e6b 100644 --- a/components/drivers/rtc/dev_soft_rtc.c +++ b/components/drivers/rtc/dev_soft_rtc.c @@ -190,14 +190,15 @@ static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args) struct tm time_temp; RT_ASSERT(dev != RT_NULL); + if (!args) + return -RT_EINVAL; + rt_memset(&time_temp, 0, sizeof(struct tm)); switch (cmd) { case RT_DEVICE_CTRL_RTC_GET_TIME: { - if (!args) - return -RT_EINVAL; t = (time_t *)args; struct timespec ts; get_rtc_time(&ts); @@ -206,8 +207,6 @@ static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args) } case RT_DEVICE_CTRL_RTC_SET_TIME: { - if (!args) - return -RT_EINVAL; t = (time_t *)args; struct timespec ts = { *t, 0 }; set_rtc_time(&ts); @@ -215,21 +214,15 @@ static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args) } #ifdef RT_USING_ALARM case RT_DEVICE_CTRL_RTC_GET_ALARM: - if (!args) - return -RT_EINVAL; *((struct rt_rtc_wkalarm *)args) = wkalarm; break; case RT_DEVICE_CTRL_RTC_SET_ALARM: - if (!args) - return -RT_EINVAL; wkalarm = *((struct rt_rtc_wkalarm *)args); soft_rtc_alarm_update(&wkalarm); break; #endif case RT_DEVICE_CTRL_RTC_GET_TIMEVAL: { - if (!args) - return -RT_EINVAL; struct timeval *tv = (struct timeval *)args; struct timespec ts; get_rtc_time(&ts); @@ -239,8 +232,6 @@ static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args) } case RT_DEVICE_CTRL_RTC_SET_TIMEVAL: { - if (!args) - return -RT_EINVAL; struct timeval *tv = (struct timeval *)args; struct timespec ts = { tv->tv_sec, tv->tv_usec * 1000 }; set_rtc_time(&ts); @@ -248,24 +239,18 @@ static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args) } case RT_DEVICE_CTRL_RTC_GET_TIMESPEC: { - if (!args) - return -RT_EINVAL; struct timespec *ts = (struct timespec *)args; get_rtc_time(ts); break; } case RT_DEVICE_CTRL_RTC_SET_TIMESPEC: { - if (!args) - return -RT_EINVAL; struct timespec *ts = (struct timespec *)args; set_rtc_time(ts); break; } case RT_DEVICE_CTRL_RTC_GET_TIMERES: { - if (!args) - return -RT_EINVAL; struct timespec *ts = (struct timespec *)args; level = rt_spin_lock_irqsave(&_spinlock); ts->tv_sec = 0; From 1d02f4231ff9871696c867943da6ad56a96e3fe8 Mon Sep 17 00:00:00 2001 From: dongly Date: Fri, 31 Oct 2025 12:58:09 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/drivers/rtc/dev_soft_rtc.c | 38 ++++++++++++++------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/components/drivers/rtc/dev_soft_rtc.c b/components/drivers/rtc/dev_soft_rtc.c index 0acab4e6e6b..12a9e6b2e73 100644 --- a/components/drivers/rtc/dev_soft_rtc.c +++ b/components/drivers/rtc/dev_soft_rtc.c @@ -186,30 +186,32 @@ static void get_rtc_time(struct timespec *ts) static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args) { time_t *t; + struct timeval *tv; + struct timespec *ts; + struct timespec ts_temp; rt_base_t level; - struct tm time_temp; RT_ASSERT(dev != RT_NULL); + if (!args) return -RT_EINVAL; - rt_memset(&time_temp, 0, sizeof(struct tm)); + rt_memset(&ts_temp, 0, sizeof(ts_temp)); switch (cmd) { case RT_DEVICE_CTRL_RTC_GET_TIME: { t = (time_t *)args; - struct timespec ts; - get_rtc_time(&ts); - *t = ts.tv_sec; + get_rtc_time(&ts_temp); + *t = ts_temp.tv_sec; break; } case RT_DEVICE_CTRL_RTC_SET_TIME: { t = (time_t *)args; - struct timespec ts = { *t, 0 }; - set_rtc_time(&ts); + ts_temp.tv_sec = *t; + set_rtc_time(&ts_temp); break; } #ifdef RT_USING_ALARM @@ -223,35 +225,35 @@ static rt_err_t soft_rtc_control(rt_device_t dev, int cmd, void *args) #endif case RT_DEVICE_CTRL_RTC_GET_TIMEVAL: { - struct timeval *tv = (struct timeval *)args; - struct timespec ts; - get_rtc_time(&ts); - tv->tv_sec = ts.tv_sec; - tv->tv_usec = ts.tv_nsec / 1000; + tv = (struct timeval *)args; + get_rtc_time(&ts_temp); + tv->tv_sec = ts_temp.tv_sec; + tv->tv_usec = ts_temp.tv_nsec / 1000; break; } case RT_DEVICE_CTRL_RTC_SET_TIMEVAL: { - struct timeval *tv = (struct timeval *)args; - struct timespec ts = { tv->tv_sec, tv->tv_usec * 1000 }; - set_rtc_time(&ts); + tv = (struct timeval *)args; + ts_temp.tv_sec = tv->tv_sec; + ts_temp.tv_nsec = tv->tv_usec * 1000; + set_rtc_time(&ts_temp); break; } case RT_DEVICE_CTRL_RTC_GET_TIMESPEC: { - struct timespec *ts = (struct timespec *)args; + ts = (struct timespec *)args; get_rtc_time(ts); break; } case RT_DEVICE_CTRL_RTC_SET_TIMESPEC: { - struct timespec *ts = (struct timespec *)args; + ts = (struct timespec *)args; set_rtc_time(ts); break; } case RT_DEVICE_CTRL_RTC_GET_TIMERES: { - struct timespec *ts = (struct timespec *)args; + ts = (struct timespec *)args; level = rt_spin_lock_irqsave(&_spinlock); ts->tv_sec = 0; #ifdef RT_USING_KTIME