Skip to content

feat(cortex-m4): 重定向 rt_interrupt_get_nest() 函数优化获取实现 #10614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

wdfk-prog
Copy link
Contributor

  • 新增 __get_IPSR() 函数以获取 IPSR 寄存器的值
  • 重定向 rt_interrupt_get_nest() 函数,用于判断当前是否处于中断上下文

拉取/合并请求描述:(PR description)

[

为什么提交这份PR (why to submit this PR)

  • 根据freeRTOS的cmsis_os2实现中对于获取中断的判断逻辑
  • rt_interrupt_get_nest函数在arm上可以使用__get_IPSR方式获取,避免现在实现方式的中断屏蔽进行优化
  • __get_IPSR方式对于arm应该是通用的,在m系列上应该是这样.
  • 由于无法一一测试,只对于m4架构进行测试通过.
  • 另外仅测试了gcc编译器,其他编译器未进行测试

你的解决方案是什么 (what is your solution)

请提供验证的bsp和config (provide the config and bsp)

  • BSP:
  • .config:
  • action:

]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用formatting 等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

- 新增 __get_IPSR() 函数以获取 IPSR 寄存器的值
- 重定向 rt_interrupt_get_nest() 函数,用于判断当前是否处于中断上下文
@Rbb666 Rbb666 requested a review from Copilot August 19, 2025 13:47
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR optimizes interrupt nesting detection in the Cortex-M4 architecture by implementing a more efficient approach using the IPSR register instead of the current interrupt masking method.

  • Adds __get_IPSR() function to read the IPSR register value across multiple compiler toolchains
  • Redirects rt_interrupt_get_nest() function to use IPSR-based interrupt context detection
  • Provides compiler-specific implementations for ARM Compiler, Clang, IAR, and GCC

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

rt_inline rt_uint32_t __get_IPSR(void)
{
#if defined(__CC_ARM)
register uint32_t __regIPSR __asm("ipsr");
Copy link
Preview

Copilot AI Aug 19, 2025

Choose a reason for hiding this comment

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

Use rt_uint32_t instead of uint32_t to maintain consistency with RT-Thread's type naming conventions used elsewhere in the codebase.

Suggested change
register uint32_t __regIPSR __asm("ipsr");
register rt_uint32_t __regIPSR __asm("ipsr");

Copilot uses AI. Check for mistakes.

#elif defined(__IAR_SYSTEMS_ICC__)
return __iar_builtin_rsr("IPSR");
#elif defined ( __GNUC__ )
uint32_t result;
Copy link
Preview

Copilot AI Aug 19, 2025

Choose a reason for hiding this comment

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

Use rt_uint32_t instead of uint32_t to maintain consistency with RT-Thread's type naming conventions used elsewhere in the codebase.

Suggested change
uint32_t result;
rt_uint32_t result;
__asm volatile ("MRS %0, ipsr" : "=r" (result) );
return(result);
#elif defined(__IAR_SYSTEMS_ICC__)
return __iar_builtin_rsr("IPSR");
#elif defined ( __GNUC__ )
rt_uint32_t result;

Copilot uses AI. Check for mistakes.

#elif defined(__IAR_SYSTEMS_ICC__)
return __iar_builtin_rsr("IPSR");
#elif defined ( __GNUC__ )
uint32_t result;
Copy link
Preview

Copilot AI Aug 19, 2025

Choose a reason for hiding this comment

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

Use rt_uint32_t instead of uint32_t to maintain consistency with RT-Thread's type naming conventions used elsewhere in the codebase.

Suggested change
uint32_t result;
rt_uint32_t result;
__asm volatile ("MRS %0, ipsr" : "=r" (result) );
return(result);
#elif defined(__IAR_SYSTEMS_ICC__)
return __iar_builtin_rsr("IPSR");
#elif defined ( __GNUC__ )
rt_uint32_t result;

Copilot uses AI. Check for mistakes.

*/
rt_uint8_t rt_interrupt_get_nest(void)
{
return (__get_IPSR() != 0);
Copy link
Preview

Copilot AI Aug 19, 2025

Choose a reason for hiding this comment

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

The function return type is rt_uint8_t but this expression returns a boolean (0 or 1). While this works, it changes the semantic meaning from returning the actual nest count to returning a boolean. Consider either changing the return type to rt_bool_t or updating the function documentation to clarify that it returns 1 for interrupt context and 0 for thread context.

Copilot uses AI. Check for mistakes.

Copy link
Member

Choose a reason for hiding this comment

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

@wdfk-prog 这块应该返回__get_IPSR()的数值?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@wdfk-prog 这块应该返回__get_IPSR()的数值?

  • 函数返回值类型我会进行修改
  • 根据手册来看IPSR寄存器的值仅能代表当前是否处于中断,处于哪个中断源.不能识别进入了嵌套中断的层数.

https://developer.arm.com/documentation/dui0646/c/The-Cortex-M7-Processor/Programmers-model/Core-registers

image
  • 我进行全局搜索rt_interrupt_get_nest的用法.并未发现有对嵌套层数的判断.这样修改不会造成影响.
  • 硬件方案没有办法识别到中断嵌套的层数
  • 或许rt_interrupt_get_nest可以改为``rt_is_interrupt_mode`?

Copy link
Member

Choose a reason for hiding this comment

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

rt_interrupt_get_nest 这种用法可能更好一些

我看到ut里面有类似使用 rt_interrupt_get_nest 的代码:

if(rt_interrupt_get_nest() > max_get_nest_count)

Copy link
Contributor Author

@wdfk-prog wdfk-prog Aug 20, 2025

Choose a reason for hiding this comment

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

rt_interrupt_get_nest 这种用法可能更好一些

我看到ut里面有类似使用 rt_interrupt_get_nest 的代码:

if(rt_interrupt_get_nest() > max_get_nest_count)

  • 是,我也有看到.但是这种是实实在在的优化.大部分场景下并没有要获取中断嵌套层数的必要.
  • 有必要的话,也应该是重新定义两个函数,一个用于获取嵌套层数,另一个专门用来获取是否进入中断既可
  • 另外对于只用内核不使用驱动的方式,之前的rt_interrupt_get_nest 大概率失效.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants