From 9636b3c30efe0f2c605fc1e3bcb45480bc0e0d71 Mon Sep 17 00:00:00 2001 From: Pillar Date: Wed, 6 Aug 2025 14:22:30 +0800 Subject: [PATCH 1/2] [bsp][etherkit] fix bug and enable high optimization. --- bsp/renesas/libraries/HAL_Drivers/config/rzt/timer_config.h | 2 +- bsp/renesas/libraries/HAL_Drivers/drv_hwtimer.c | 2 ++ .../rzn/fsp/src/bsp/cmsis/Device/RENESAS/Source/system.c | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/bsp/renesas/libraries/HAL_Drivers/config/rzt/timer_config.h b/bsp/renesas/libraries/HAL_Drivers/config/rzt/timer_config.h index f9f6c80134b..842a3eba59e 100644 --- a/bsp/renesas/libraries/HAL_Drivers/config/rzt/timer_config.h +++ b/bsp/renesas/libraries/HAL_Drivers/config/rzt/timer_config.h @@ -19,7 +19,7 @@ extern "C" { #endif -#define PLCKD_PRESCALER_MAX_SELECT 8 +#define PLCKD_PRESCALER_MAX_SELECT 9 /* RSK-RZN2L: Frequency ratio: PCLKA:PCLKD = 1:N (N = 1/2/4/8/16/32/64) */ #define PLCKD_PRESCALER_400M (BSP_PRV_PCLKGPTL_FREQ_400_MHZ) diff --git a/bsp/renesas/libraries/HAL_Drivers/drv_hwtimer.c b/bsp/renesas/libraries/HAL_Drivers/drv_hwtimer.c index f23a3715874..49f5565579a 100644 --- a/bsp/renesas/libraries/HAL_Drivers/drv_hwtimer.c +++ b/bsp/renesas/libraries/HAL_Drivers/drv_hwtimer.c @@ -38,6 +38,8 @@ const rt_uint32_t PLCKD_FREQ_PRESCALER[PLCKD_PRESCALER_MAX_SELECT] = PLCKD_PRESCALER_3_75M, PLCKD_PRESCALER_1_875M, #elif defined(SOC_SERIES_R9A07G0) + PLCKD_PRESCALER_400M, + PLCKD_PRESCALER_200M, PLCKD_PRESCALER_100M, PLCKD_PRESCALER_50M, PLCKD_PRESCALER_25M, diff --git a/bsp/renesas/rzn2l_etherkit/rzn/fsp/src/bsp/cmsis/Device/RENESAS/Source/system.c b/bsp/renesas/rzn2l_etherkit/rzn/fsp/src/bsp/cmsis/Device/RENESAS/Source/system.c index 66d5fa642e4..682cc53bc5a 100644 --- a/bsp/renesas/rzn2l_etherkit/rzn/fsp/src/bsp/cmsis/Device/RENESAS/Source/system.c +++ b/bsp/renesas/rzn2l_etherkit/rzn/fsp/src/bsp/cmsis/Device/RENESAS/Source/system.c @@ -577,7 +577,7 @@ void bsp_loader_bss_init (void) void bsp_copy_multibyte (uintptr_t * src, uintptr_t * dst, uintptr_t bytesize) { uintptr_t i; - uintptr_t cnt; + volatile uintptr_t cnt; uintptr_t src_mod; uint8_t * src_single_byte; @@ -630,7 +630,7 @@ void bsp_copy_multibyte (uintptr_t * src, uintptr_t * dst, uintptr_t bytesize) void bsp_bss_init_multibyte (uintptr_t * src, uintptr_t bytesize) { uintptr_t i; - uintptr_t cnt; + volatile uintptr_t cnt; uintptr_t zero = 0; uintptr_t src_mod; From ec9f1763572a35fe7dc4489d8c5c00b7c639e5b8 Mon Sep 17 00:00:00 2001 From: Pillar Date: Wed, 6 Aug 2025 15:08:35 +0800 Subject: [PATCH 2/2] [kernel] add UP scheduler critical switch flag. --- src/cpu_up.c | 2 ++ src/scheduler_up.c | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/cpu_up.c b/src/cpu_up.c index 32825461b01..d1720c94596 100644 --- a/src/cpu_up.c +++ b/src/cpu_up.c @@ -40,6 +40,7 @@ void rt_spin_lock(struct rt_spinlock *lock) /** * @brief This function will unlock the spinlock, will unlock the thread scheduler. + * If the scheduling function is called before unlocking, it will be scheduled in this function. * * @param lock is a pointer to the spinlock. */ @@ -72,6 +73,7 @@ rt_base_t rt_spin_lock_irqsave(struct rt_spinlock *lock) /** * @brief This function will unlock the spinlock and then restore current cpu interrupt status, will unlock the thread scheduler. + * If the scheduling function is called before unlocking, it will be scheduled in this function. * * @param lock is a pointer to the spinlock. * diff --git a/src/scheduler_up.c b/src/scheduler_up.c index f8c53addec0..c4c6ec66ec2 100644 --- a/src/scheduler_up.c +++ b/src/scheduler_up.c @@ -30,6 +30,7 @@ * 2022-01-07 Gabriel Moving __on_rt_xxxxx_hook to scheduler.c * 2023-03-27 rose_man Split into scheduler upc and scheduler_mp.c * 2023-10-17 ChuShicheng Modify the timing of clearing RT_THREAD_STAT_YIELD flag bits + * 2025-08-04 Pillar Add rt_scheduler_critical_switch_flag */ #define __RT_IPC_SOURCE__ @@ -51,6 +52,11 @@ extern volatile rt_atomic_t rt_interrupt_nest; static rt_int16_t rt_scheduler_lock_nest; rt_uint8_t rt_current_priority; +static rt_int8_t rt_scheduler_critical_switch_flag; +#define IS_CRITICAL_SWITCH_PEND() (rt_scheduler_critical_switch_flag == 1) +#define SET_CRITICAL_SWITCH_FLAG() (rt_scheduler_critical_switch_flag = 1) +#define CLR_CRITICAL_SWITCH_FLAG() (rt_scheduler_critical_switch_flag = 0) + #if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR) static void (*rt_scheduler_hook)(struct rt_thread *from, struct rt_thread *to); static void (*rt_scheduler_switch_hook)(struct rt_thread *tid); @@ -236,6 +242,9 @@ void rt_system_scheduler_start(void) rt_cpu_self()->current_thread = to_thread; + /* flush critical switch flag */ + CLR_CRITICAL_SWITCH_FLAG(); + rt_sched_remove_thread(to_thread); RT_SCHED_CTX(to_thread).stat = RT_THREAD_RUNNING; @@ -387,6 +396,10 @@ void rt_schedule(void) } } } + else + { + SET_CRITICAL_SWITCH_FLAG(); + } /* enable interrupt */ rt_hw_interrupt_enable(level); @@ -604,6 +617,7 @@ void rt_exit_critical_safe(rt_base_t critical_level) /** * @brief Safely exit critical section (non-debug version) + * If the scheduling function is called before exiting, it will be scheduled in this function. * * @param critical_level The expected critical level (unused in non-debug build) * @@ -657,6 +671,7 @@ RTM_EXPORT(rt_enter_critical); /** * @brief Exit critical section and unlock scheduler + * If the scheduling function is called before exiting, it will be scheduled in this function. * * @details This function: * - Decrements the scheduler lock nesting count @@ -685,9 +700,10 @@ void rt_exit_critical(void) /* enable interrupt */ rt_hw_interrupt_enable(level); - if (rt_current_thread) + if (IS_CRITICAL_SWITCH_PEND()) { - /* if scheduler is started, do a schedule */ + CLR_CRITICAL_SWITCH_FLAG(); + /* if scheduler is started and needs to be scheduled, do a schedule */ rt_schedule(); } }