Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions libcpu/arm/cortex-m4/cpuport.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@

#include <rtthread.h>

#define DBG_TAG "cortex.m4"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

#if /* ARMCC */ ( (defined ( __CC_ARM ) && defined ( __TARGET_FPU_VFP )) \
/* Clang */ || (defined ( __clang__ ) && defined ( __VFP_FP__ ) && !defined(__SOFTFP__)) \
/* IAR */ || (defined ( __ICCARM__ ) && defined ( __ARMVFP__ )) \
Expand Down Expand Up @@ -436,6 +440,107 @@ void rt_hw_cpu_reset(void)
SCB_AIRCR = SCB_RESET_VALUE;
}

/**
\brief Get IPSR Register
\details Returns the content of the IPSR Register.
\return IPSR Register value
*/
rt_inline rt_uint32_t __get_IPSR(void)
{
#if defined(__CC_ARM)
register uint32_t __regIPSR __asm("ipsr");
return(__regIPSR);
Comment on lines +448 to +452
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[best_practices/类别]: Avoid defining __get_IPSR (reserved/CMSIS name) in RT-Thread code / 避免在 RT-Thread 代码中定义 __get_IPSR(保留标识符/CMSIS 重名)

English: __get_IPSR is commonly provided by CMSIS and the leading __ identifier is reserved by the C standard. Defining it here risks redefinition conflicts depending on BSP/CMSIS include order. Prefer using the CMSIS intrinsic (include the proper CMSIS header) or rename this helper to an RT-Thread-specific name.
中文:__get_IPSR 通常由 CMSIS 提供,且以 __ 开头的标识符属于 C 标准保留名称。在此处定义可能因 BSP/CMSIS 头文件包含顺序导致重定义冲突。建议直接使用 CMSIS 的实现(包含对应头文件),或将本地辅助函数改为 RT-Thread 风格命名。

Copilot uses AI. Check for mistakes.
#elif defined(__clang__)
uint32_t result;
Comment on lines +450 to +454
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[bug/类别]: __get_IPSR() lacks a final #else/#error or return path / __get_IPSR() 缺少最终 #else/#error 或返回路径

English: This #if/#elif chain has no final #else branch. If none of the toolchain macros match, the function may compile without a return (UB) or fail under Werror. Please add a final #else with #error (preferred) or a safe fallback return.
中文:该 #if/#elif 分支没有最终的 #else。若工具链宏都不匹配,可能出现“非 void 函数未返回”的未定义行为,或在 Werror 下编译失败。建议补充最终 #else:给出 #error(推荐)或安全兜底返回值。

Copilot uses AI. Check for mistakes.
__asm volatile ("MRS %0, ipsr" : "=r" (result) );
return(result);
#elif defined(__IAR_SYSTEMS_ICC__)
return __iar_builtin_rsr("IPSR");
#elif defined ( __GNUC__ )
uint32_t result;
__asm volatile ("MRS %0, ipsr" : "=r" (result) );
return(result);
#endif
}

/**
* @brief This function will be invoked by BSP, when enter interrupt service routine
*
* @note Please don't invoke this routine in application
*
* @see rt_interrupt_leave
*/
void rt_interrupt_enter(void)
{
extern void (*rt_interrupt_enter_hook)(void);
RT_OBJECT_HOOK_CALL(rt_interrupt_enter_hook,());
LOG_D("irq has come...");
Comment on lines +473 to +477
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[bug/类别]: rt_interrupt_enter() override does not increment interrupt nest counter / 覆盖实现的 rt_interrupt_enter() 未增加中断嵌套计数

English: The generic src/irq.c implementation increments rt_interrupt_nest before calling the enter hook. This Cortex-M4 override only calls the hook/log, so the nesting counter will never increase on Cortex-M4, breaking code/tests that rely on nesting depth. Please keep rt_interrupt_nest maintenance consistent (or avoid overriding if not needed).
中文:通用实现(src/irq.c)会在调用 enter hook 前递增 rt_interrupt_nest。当前 Cortex-M4 覆盖实现仅调用 hook/日志,导致 Cortex-M4 上嵌套计数不会增加,从而影响依赖嵌套深度的代码/测试。建议保持 rt_interrupt_nest 的维护语义一致(或如果无必要则不要覆盖该函数)。

Copilot uses AI. Check for mistakes.
}

/**
* @brief This function will be invoked by BSP, when leave interrupt service routine
*
* @note Please don't invoke this routine in application
*
* @see rt_interrupt_enter
*/
void rt_interrupt_leave(void)
{
extern void (*rt_interrupt_leave_hook)(void);
Comment on lines +488 to +489
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[bug/类别]: rt_interrupt_leave() override does not decrement interrupt nest counter / 覆盖实现的 rt_interrupt_leave() 未减少中断嵌套计数

English: The generic src/irq.c implementation decrements rt_interrupt_nest on leave. This Cortex-M4 override doesn’t update the counter, so rt_interrupt_nest becomes inconsistent on Cortex-M4 builds. Please decrement the nest counter (and keep hook call order consistent with the generic implementation) or avoid overriding.
中文:通用实现(src/irq.c)在 leave 时会递减 rt_interrupt_nest。当前 Cortex-M4 覆盖实现未更新该计数,导致 Cortex-M4 上 rt_interrupt_nest 失真。建议补齐递减逻辑(并保持 hook 调用顺序与通用实现一致),或避免覆盖实现。

Suggested change
{
extern void (*rt_interrupt_leave_hook)(void);
{
extern rt_uint8_t rt_interrupt_nest;
extern void (*rt_interrupt_leave_hook)(void);
rt_interrupt_nest--;

Copilot uses AI. Check for mistakes.
LOG_D("irq is going to leave");
RT_OBJECT_HOOK_CALL(rt_interrupt_leave_hook,());
}

/**
* @brief This function will return the nest of interrupt.
*
* User application can invoke this function to get whether current
* context is interrupt context.
*
* @return the number of nested interrupts.
*/
rt_uint8_t rt_interrupt_get_nest(void)
{
return (__get_IPSR() != 0);
}
Comment on lines +502 to +505
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[bug/类别]: rt_interrupt_get_nest() changed from “nesting depth” to boolean / rt_interrupt_get_nest() 从“嵌套深度”变为布尔语义

English: The API contract is “the number of nested interrupts”, but this implementation returns only 0/1 based on IPSR. This can break callers that expect depth > 1 (e.g., IRQ nesting tests or logic distinguishing nested vs first-level ISR). Please keep rt_interrupt_get_nest() returning the real nesting count; add a separate fast helper for “in ISR context” if needed.
中文:该 API 约定是“中断嵌套次数”,但当前实现仅基于 IPSR 返回 0/1。这样会破坏依赖嵌套深度 > 1 的调用方(例如中断嵌套测试或需要区分是否发生嵌套的逻辑)。建议保持 rt_interrupt_get_nest() 返回真实嵌套计数;若需要快速判断 ISR 上下文,可新增独立接口。

Copilot uses AI. Check for mistakes.

/**
\brief Get PRIMASK Register
\details Returns the content of the PRIMASK Register.
\return PRIMASK Register value
*/
rt_inline rt_uint32_t rt_hw_get_primask_value(void)
{
#if defined(__CC_ARM)
register uint32_t __regPRIMASK __asm("primask");
return (__regPRIMASK);
#elif defined(__clang__)
uint32_t result;
Comment on lines +514 to +518
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[bug/类别]: rt_hw_get_primask_value() lacks a final #else/#error or return path / rt_hw_get_primask_value() 缺少最终 #else/#error 或返回路径

English: Like __get_IPSR(), this helper has no final #else branch. Please add a final #else with #error (preferred) or a safe fallback return to avoid undefined behavior / build failures on unexpected toolchains.
中文:与 __get_IPSR() 类似,该辅助函数也缺少最终 #else 分支。建议补充最终 #else:使用 #error(推荐)或安全兜底返回值,以避免在非预期工具链下出现未定义行为/编译失败。

Copilot uses AI. Check for mistakes.
__asm volatile ("MRS %0, primask" : "=r" (result));
return result;
#elif defined(__IAR_SYSTEMS_ICC__)
return __iar_builtin_rsr("PRIMASK");
#elif defined(__GNUC__)
uint32_t result;
__asm volatile ("MRS %0, primask" : "=r" (result));
return result;
#endif
}

/**
* @brief Check whether maskable interrupts are currently disabled.
*
* @details
* For Cortex-M4, interrupts are considered disabled when either:
* - PRIMASK masks all configurable interrupts.
*
* @return RT_TRUE if interrupts are masked; otherwise RT_FALSE.
*/
rt_bool_t rt_hw_interrupt_is_disabled(void)
{
return ((rt_hw_get_primask_value() & 0x1UL) != 0UL);
}

#ifdef RT_USING_CPU_FFS
/**
* This function finds the first bit set (beginning with the least significant bit)
Expand Down
4 changes: 2 additions & 2 deletions src/irq.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

#if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)

static void (*rt_interrupt_enter_hook)(void);
static void (*rt_interrupt_leave_hook)(void);
void (*rt_interrupt_enter_hook)(void);
void (*rt_interrupt_leave_hook)(void);
Comment on lines +29 to +30
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[maintainability/类别]: Exported hook function pointers lack a public declaration / 导出的 hook 函数指针缺少公开声明

English: rt_interrupt_enter_hook / rt_interrupt_leave_hook are now global symbols but are not declared in any header, forcing other translation units to redeclare them with extern (as done in cpuport.c). To avoid drift and keep types consistent, please add a single authoritative declaration in an appropriate internal header (guarded by RT_USING_HOOK && RT_HOOK_USING_FUNC_PTR), or alternatively keep the pointers private and expose a small rt_interrupt_enter_hook_call()/rt_interrupt_leave_hook_call() helper.
中文:rt_interrupt_enter_hook / rt_interrupt_leave_hook 现在变成了全局符号,但当前没有任何头文件声明它们,导致其他文件只能各自 extern 重复声明(如 cpuport.c)。为避免类型不一致与维护成本,建议在合适的内部头文件中提供唯一声明(并用 RT_USING_HOOK && RT_HOOK_USING_FUNC_PTR 保护),或保持指针私有并提供 rt_interrupt_enter_hook_call()/rt_interrupt_leave_hook_call() 之类的小封装接口。

Suggested change
void (*rt_interrupt_enter_hook)(void);
void (*rt_interrupt_leave_hook)(void);
static void (*rt_interrupt_enter_hook)(void);
static void (*rt_interrupt_leave_hook)(void);

Copilot uses AI. Check for mistakes.

/**
* @ingroup group_hook
Expand Down
5 changes: 2 additions & 3 deletions src/scheduler_up.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ rt_uint32_t rt_thread_ready_priority_group;
rt_uint8_t rt_thread_ready_table[32];
#endif /* RT_THREAD_PRIORITY_MAX > 32 */

extern volatile rt_atomic_t rt_interrupt_nest;
static rt_atomic_t rt_scheduler_lock_nest;
rt_uint8_t rt_current_priority;

Expand Down Expand Up @@ -347,13 +346,13 @@ void rt_schedule(void)
LOG_D("[%d]switch to priority#%d "
"thread:%.*s(sp:0x%08x), "
"from thread:%.*s(sp: 0x%08x)",
rt_interrupt_nest, highest_ready_priority,
rt_interrupt_get_nest(), highest_ready_priority,
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[custom/类别]: 🟡 [PR Title/PR 标题]: Missing or incorrect prefix format / 缺少或错误的前缀格式

English: PR title should follow format: [module][subsystem] Description in lowercase. Current title starts with feat and does not start with the bracketed prefix, which diverges from the repo’s PR title convention.
中文:PR 标题应遵循格式:小写的 [模块][子系统] 描述。当前标题以 feat 开头且未以方括号前缀开头,与仓库约定不一致。

Copilot generated this review using guidance from repository custom instructions.
RT_NAME_MAX, to_thread->parent.name, to_thread->sp,
RT_NAME_MAX, from_thread->parent.name, from_thread->sp);

RT_SCHEDULER_STACK_CHECK(to_thread);
Comment on lines +349 to 353
Copy link

Copilot AI Apr 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[performance/类别]: Avoid calling rt_interrupt_get_nest() multiple times in rt_schedule / 避免在 rt_schedule 中重复调用 rt_interrupt_get_nest()

English: rt_interrupt_get_nest() can be relatively expensive on some ports (e.g., it may mask/restore IRQs to read an atomic counter). Here it’s called for logging; please cache the value in a local variable and reuse it for the subsequent ISR-context check.
中文:在部分架构上 rt_interrupt_get_nest() 可能会关/开中断以读取原子计数,代价不低。此处用于日志打印时已调用一次,建议缓存到局部变量并在后续 ISR 上下文判断时复用。

Copilot uses AI. Check for mistakes.

if (rt_interrupt_nest == 0)
if (rt_interrupt_get_nest() == 0)
{
extern void rt_thread_handle_sig(rt_bool_t clean_state);

Expand Down
Loading