[STM32F1] cortex-m3堆栈的切换

[复制链接]
 楼主| pangb 发表于 2020-11-16 20:24 | 显示全部楼层 |阅读模式
开始是这样的,在移植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
zhanghqi 发表于 2020-11-16 20:29 | 显示全部楼层
楼主详细说说怎么回事啊,这么说太笼统了
huangchui 发表于 2020-11-16 20:33 | 显示全部楼层
这段代码编译的时候报错,Error:r13 not allowed here--'msr psp,sp'。
wyjie 发表于 2020-11-16 20:40 | 显示全部楼层
R13寄存器可以保存两个指针MSP和PSP
jlyuan 发表于 2020-11-16 20:43 | 显示全部楼层
M3有两个等级,一个是用户级和特权级,这个好理解,为了安全嘛
yszong 发表于 2020-11-16 20:47 | 显示全部楼层
M3有两种模式,线程模式和handler模式,线程模式运行主应用程序代码,handler模式运行异常和中断的代码,而且在handler模式下,只能用MSP指针。
dengdc 发表于 2020-11-16 20:51 | 显示全部楼层
错误的原因是不是在handler模式下执行的msr psp,sp这条指令所以报错了
huangchui 发表于 2020-11-16 20:54 | 显示全部楼层
处于什么考虑需要两个堆栈指针,而且要这样应用呢
heweibig 发表于 2020-11-16 21:02 | 显示全部楼层
msr 指令,源操作数不允许是 r13(sp) 和 r15(pc)。
wuhany 发表于 2020-11-16 21:05 | 显示全部楼层
你需要把 sp 转到一个普通寄存器中再 msr 到 psp
jiahy 发表于 2020-11-16 21:09 | 显示全部楼层

mov r0, sp
msr psp, r0
lizye 发表于 2020-11-16 21:12 | 显示全部楼层
汇编指令实现这个功能
huangchui 发表于 2020-11-16 21:15 | 显示全部楼层

果然如此,编译通过了,哈哈。
大神可不可以跟我讲下为什么要有两个堆栈指针呢
zhanghqi 发表于 2020-11-16 21:22 | 显示全部楼层
两个栈指针为了提高 RTOS 的效率而设置。
chenjunt 发表于 2020-11-16 21:25 | 显示全部楼层
应该是语法不对吧,好像没有 msr psp,sp  这样用的
zhenykun 发表于 2020-11-16 21:28 | 显示全部楼层
在handler模式下,只能用MSP指针。
zyf部长 发表于 2020-11-16 21:32 | 显示全部楼层
貌似是这样使用 mrs r0, psp     msr   psp, r0  sp用法不对
xxmmi 发表于 2020-11-16 21:38 | 显示全部楼层
效率应该被降低了吧,栈指针切换需要额外的指令开销,这个目的应该是为了安全
kangzj 发表于 2020-11-16 21:41 | 显示全部楼层
线程模式运行主应用程序代码,handler模式运行异常和中断的代码
gongche 发表于 2020-11-16 21:45 | 显示全部楼层
msr是特殊到通用吧,拿个寄存器中间临时放一下。msr和mrs好像要在特权级才能用
您需要登录后才可以回帖 登录 | 注册

本版积分规则

701

主题

7643

帖子

6

粉丝
快速回复 在线客服 返回列表 返回顶部