feat(cortex-m4): 重定向 rt_interrupt_get_nest() 函数优化获取实现#10614
feat(cortex-m4): 重定向 rt_interrupt_get_nest() 函数优化获取实现#10614wdfk-prog wants to merge 1 commit intoRT-Thread:masterfrom
Conversation
- 新增 __get_IPSR() 函数以获取 IPSR 寄存器的值 - 重定向 rt_interrupt_get_nest() 函数,用于判断当前是否处于中断上下文
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
Use rt_uint32_t instead of uint32_t to maintain consistency with RT-Thread's type naming conventions used elsewhere in the codebase.
| register uint32_t __regIPSR __asm("ipsr"); | |
| register rt_uint32_t __regIPSR __asm("ipsr"); |
| #elif defined(__IAR_SYSTEMS_ICC__) | ||
| return __iar_builtin_rsr("IPSR"); | ||
| #elif defined ( __GNUC__ ) | ||
| uint32_t result; |
There was a problem hiding this comment.
Use rt_uint32_t instead of uint32_t to maintain consistency with RT-Thread's type naming conventions used elsewhere in the codebase.
| 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; |
| #elif defined(__IAR_SYSTEMS_ICC__) | ||
| return __iar_builtin_rsr("IPSR"); | ||
| #elif defined ( __GNUC__ ) | ||
| uint32_t result; |
There was a problem hiding this comment.
Use rt_uint32_t instead of uint32_t to maintain consistency with RT-Thread's type naming conventions used elsewhere in the codebase.
| 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; |
| */ | ||
| rt_uint8_t rt_interrupt_get_nest(void) | ||
| { | ||
| return (__get_IPSR() != 0); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
@wdfk-prog 这块应该返回__get_IPSR()的数值?
- 函数返回值类型我会进行修改
- 根据手册来看IPSR寄存器的值仅能代表当前是否处于中断,处于哪个中断源.不能识别进入了嵌套中断的层数.
- 我进行全局搜索
rt_interrupt_get_nest的用法.并未发现有对嵌套层数的判断.这样修改不会造成影响. - 硬件方案没有办法识别到中断嵌套的层数
- 或许
rt_interrupt_get_nest可以改为``rt_is_interrupt_mode`?
There was a problem hiding this comment.
rt_interrupt_get_nest 这种用法可能更好一些
我看到ut里面有类似使用 rt_interrupt_get_nest 的代码:
There was a problem hiding this comment.
rt_interrupt_get_nest 这种用法可能更好一些
我看到ut里面有类似使用 rt_interrupt_get_nest 的代码:
- 是,我也有看到.但是这种是实实在在的优化.大部分场景下并没有要获取中断嵌套层数的必要.
- 有必要的话,也应该是重新定义两个函数,一个用于获取嵌套层数,另一个专门用来获取是否进入中断既可
- 另外对于只用内核不使用驱动的方式,之前的
rt_interrupt_get_nest大概率失效.
拉取/合并请求描述:(PR description)
[
为什么提交这份PR (why to submit this PR)
你的解决方案是什么 (what is your solution)
请提供验证的bsp和config (provide the config and bsp)
]
当前拉取/合并请求的状态 Intent for your PR
必须选择一项 Choose one (Mandatory):
代码质量 Code Quality:
我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:
#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up