打印
[STM32F1]

cortex-m3堆栈的切换

[复制链接]
978|41
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
pangb|  楼主 | 2022-12-10 11:58 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
开始是这样的,在移植eCos时遇到一个编译错误,代码如下
//=========================================================================
// State switch VSR
//
// This is called from the init code to switch execution from the main
// stack to the process stack. We also take the opportunity to do some
// other things that are best done in asm code such as disabling interrupts
// and setting the control register.
//
// The adjustment to MSP by 1/2 interrupt stack size allows code to
// throw exceptions without corrupting the execution stack. This is
// only necessary for non-kernel configurations (e.g. RedBoot, Stubs)
// since kernel configurations will switch to a thread stack before
// they should throw an exception.        
        
        .global hal_switch_state_vsr
        .thumb
        .thumb_func
        .type   hal_switch_state_vsr, %function
hal_switch_state_vsr:

        mov     r0,#CYGNUM_HAL_CORTEXM_PRIORITY_MAX        
        msr     basepri,r0
        
        mov     r0,#2                   // Set CONTROL register to 2
        msr     control,r0
        isb                             // Insert a barrier
        
        msr     psp,sp                  // Copy SP to PSP

#if !defined(CYGPKG_KERNEL)
        sub     sp,#(CYGNUM_HAL_COMMON_INTERRUPTS_STACK_SIZE/2)
#endif        
        
        orr     lr,#0xD                 // Adjust return link
        bx      lr                      // Return to init code on PSP now

#endif

使用特权

评论回复
沙发
huangchui| | 2022-12-10 12:06 | 只看该作者
这段代码编译的时候报错,Error:r13 not allowed here--'msr psp,sp'。

使用特权

评论回复
板凳
pangb|  楼主 | 2022-12-10 12:08 | 只看该作者
R13寄存器可以保存两个指针MSP和PSP

使用特权

评论回复
地板
chuxh| | 2022-12-10 12:32 | 只看该作者
M3有两个等级,一个是用户级和特权级,这个好理解,为了安全嘛

使用特权

评论回复
5
llljh| | 2022-12-10 12:34 | 只看该作者
M3有两种模式,线程模式和handler模式,线程模式运行主应用程序代码,handler模式运行异常和中断的代码,而且在handler模式下,只能用MSP指针。

使用特权

评论回复
6
supernan| | 2022-12-10 12:36 | 只看该作者
错误的原因是不是在handler模式下执行的msr psp,sp这条指令所以报错了

使用特权

评论回复
7
dengdc| | 2022-12-10 12:42 | 只看该作者
处于什么考虑需要两个堆栈指针,而且要这样应用呢

使用特权

评论回复
8
heweibig| | 2022-12-10 12:48 | 只看该作者
msr 指令,源操作数不允许是 r13(sp) 和 r15(pc)。

使用特权

评论回复
9
stly| | 2022-12-10 13:01 | 只看该作者
你需要把 sp 转到一个普通寄存器中再 msr 到 psp

使用特权

评论回复
10
heweibig| | 2022-12-10 13:03 | 只看该作者

mov r0, sp
msr psp, r0

使用特权

评论回复
11
huanghuac| | 2022-12-10 13:06 | 只看该作者
汇编指令实现这个功能

使用特权

评论回复
12
zhuhuis| | 2022-12-10 13:10 | 只看该作者

果然如此,编译通过了,哈哈。
大神可不可以跟我讲下为什么要有两个堆栈指针呢

使用特权

评论回复
13
chuxh| | 2022-12-10 13:14 | 只看该作者
两个栈指针为了提高 RTOS 的效率而设置。

使用特权

评论回复
14
llljh| | 2022-12-10 13:16 | 只看该作者
应该是语法不对吧,好像没有 msr psp,sp  这样用的

使用特权

评论回复
15
huangchui| | 2022-12-10 13:19 | 只看该作者
在handler模式下,只能用MSP指针。

使用特权

评论回复
16
yszong| | 2022-12-10 13:21 | 只看该作者
貌似是这样使用 mrs r0, psp     msr   psp, r0  sp用法不对

使用特权

评论回复
17
xxrs| | 2022-12-10 13:24 | 只看该作者
效率应该被降低了吧,栈指针切换需要额外的指令开销,这个目的应该是为了安全

使用特权

评论回复
18
jiahy| | 2022-12-10 13:28 | 只看该作者

线程模式运行主应用程序代码,handler模式运行异常和中断的代码

使用特权

评论回复
19
zhaoxqi| | 2022-12-10 13:31 | 只看该作者
msr是特殊到通用吧,拿个寄存器中间临时放一下。msr和mrs好像要在特权级才能用

使用特权

评论回复
20
pangb|  楼主 | 2022-12-10 13:33 | 只看该作者
这是为什么,什么原因

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

701

主题

7643

帖子

6

粉丝