[STM32F1] cortex-m3堆栈的切换

[复制链接]
1885|22
 楼主| huangchui 发表于 2017-9-25 22:40 | 显示全部楼层 |阅读模式
开始是这样的,在移植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 发表于 2017-9-25 22:41 | 显示全部楼层
楼主详细说说怎么回事啊,这么说太笼统了
 楼主| huangchui 发表于 2017-9-25 22:42 | 显示全部楼层
这段代码编译的时候报错,Error:r13 not allowed here--'msr psp,sp'。
wyjie 发表于 2017-9-25 22:43 | 显示全部楼层

R13寄存器可以保存两个指针MSP和PSP
jlyuan 发表于 2017-9-25 22:46 | 显示全部楼层
M3有两个等级,一个是用户级和特权级,这个好理解,为了安全嘛
yszong 发表于 2017-9-25 22:48 | 显示全部楼层
M3有两种模式,线程模式和handler模式,线程模式运行主应用程序代码,handler模式运行异常和中断的代码,而且在handler模式下,只能用MSP指针。
dengdc 发表于 2017-9-25 22:49 | 显示全部楼层
错误的原因是不是在handler模式下执行的msr psp,sp这条指令所以报错了
 楼主| huangchui 发表于 2017-9-25 22:50 | 显示全部楼层
处于什么考虑需要两个堆栈指针,而且要这样应用呢
heweibig 发表于 2017-9-25 22:51 | 显示全部楼层
msr 指令,源操作数不允许是 r13(sp) 和 r15(pc)。
wuhany 发表于 2017-9-25 22:53 | 显示全部楼层
你需要把 sp 转到一个普通寄存器中再 msr 到 psp
jiahy 发表于 2017-9-25 22:54 | 显示全部楼层

mov r0, sp
msr psp, r0
lizye 发表于 2017-9-25 22:58 | 显示全部楼层
汇编指令实现这个功能
 楼主| huangchui 发表于 2017-9-25 22:59 | 显示全部楼层

果然如此,编译通过了,哈哈。
大神可不可以跟我讲下为什么要有两个堆栈指针呢
zhanghqi 发表于 2017-9-25 23:01 | 显示全部楼层

两个栈指针为了提高 RTOS 的效率而设置。
feelhyq 发表于 2017-9-25 23:01 | 显示全部楼层
本帖最后由 feelhyq 于 2017-9-25 23:19 编辑
dengdc 发表于 2017-9-25 22:49
错误的原因是不是在handler模式下执行的msr psp,sp这条指令所以报错了

应该是语法不对吧,好像没有 msr psp,sp  这样用的
zhenykun 发表于 2017-9-25 23:04 | 显示全部楼层

在handler模式下,只能用MSP指针。
feelhyq 发表于 2017-9-25 23:05 | 显示全部楼层
本帖最后由 feelhyq 于 2017-9-25 23:22 编辑
jiahy 发表于 2017-9-25 22:54
mov r0, sp
msr psp, r0

貌似是这样使用 mrs r0, psp     msr   psp, r0  sp用法不对
feelhyq 发表于 2017-9-25 23:07 | 显示全部楼层

,

本帖最后由 feelhyq 于 2017-9-25 23:23 编辑
zhanghqi 发表于 2017-9-25 23:01
两个栈指针为了提高 RTOS 的效率而设置。

效率应该被降低了吧,栈指针切换需要额外的指令开销,这个目的应该是为了安全
 楼主| huangchui 发表于 2017-9-25 23:10 | 显示全部楼层

结贴了,多谢大家讨论这么多哈,呵呵
zjlol 发表于 2017-9-26 09:06 | 显示全部楼层
学习了!谢谢!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

918

主题

12323

帖子

4

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