[STM32F1] 关于cortex-m3堆栈的切换

[复制链接]
2562|8
 楼主| z87843785 发表于 2015-7-20 16:46 | 显示全部楼层 |阅读模式
我是一个新手。开始是这样的,在移植eCos时遇到一个编译错误,代码如下
  1. //=========================================================================
  2. // State switch VSR
  3. //
  4. // This is called from the init code to switch execution from the main
  5. // stack to the process stack. We also take the opportunity to do some
  6. // other things that are best done in asm code such as disabling interrupts
  7. // and setting the control register.
  8. //
  9. // The adjustment to MSP by 1/2 interrupt stack size allows code to
  10. // throw exceptions without corrupting the execution stack. This is
  11. // only necessary for non-kernel configurations (e.g. RedBoot, Stubs)
  12. // since kernel configurations will switch to a thread stack before
  13. // they should throw an exception.        
  14.         
  15.         .global hal_switch_state_vsr
  16.         .thumb
  17.         .thumb_func
  18.         .type   hal_switch_state_vsr, %function
  19. hal_switch_state_vsr:

  20.         mov     r0,#CYGNUM_HAL_CORTEXM_PRIORITY_MAX       
  21.         msr     basepri,r0
  22.         
  23.         mov     r0,#2                   // Set CONTROL register to 2
  24.         msr     control,r0
  25.         isb                             // Insert a barrier
  26.         
  27.         msr     psp,sp                  // Copy SP to PSP

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

  34. #endif
这段代码编译的时候报错,Error:r13 not allowed here--'msr psp,sp'。
然后我就去找原因了:
1.R13寄存器可以保存两个指针MSP和PSP。
2.M3有两个等级,一个是用户级和特权级,这个好理解,为了安全嘛。
3.M3有两种模式,线程模式和handler模式,线程模式运行主应用程序代码,handler模式运行异常和中断的代码,而且在handler模式下,只能用MSP指针。
然后我就猜想,错误的原因是不是在handler模式下执行的msr psp,sp这条指令所以报错了,然而我并不知道这个想法对不对。
其实我的问题是,处于什么考虑需要两个堆栈指针,而且要这样应用呢,希望大神给我讲讲嘛,感激不尽。如果顺便能帮我找找代码出错的原因俺都愿意以身相许了。
john_lee 发表于 2015-7-20 17:29 | 显示全部楼层
msr 指令,源操作数不允许是 r13(sp) 和 r15(pc)。
你需要把 sp 转到一个普通寄存器中再 msr 到 psp,例如:
  1. mov r0, sp
  2. msr psp, r0
598330983 发表于 2015-7-20 22:04 来自手机 | 显示全部楼层
汇编指令实现这个功能
 楼主| z87843785 发表于 2015-7-21 08:59 | 显示全部楼层
john_lee 发表于 2015-7-20 17:29
msr 指令,源操作数不允许是 r13(sp) 和 r15(pc)。
你需要把 sp 转到一个普通寄存器中再 msr 到 psp,例如 ...

果然如此,编译通过了,哈哈。
大神可不可以跟我讲下为什么要有两个堆栈指针呢
john_lee 发表于 2015-7-21 10:19 | 显示全部楼层
两个栈指针为了提高 RTOS 的效率而设置。
 楼主| z87843785 发表于 2015-7-21 14:05 | 显示全部楼层
额。。。那在以后的实践中慢慢体会了
zhou0214 发表于 2015-7-26 21:18 | 显示全部楼层
汇编命令表示不懂,楼主问题解决了吗??
 楼主| z87843785 发表于 2015-11-23 11:46 | 显示全部楼层
@zhou0214  解决了,二楼版主的方法解决的
可可球 发表于 2015-11-23 12:38 | 显示全部楼层
在handler模式下,只能用MSP指针。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

12

帖子

0

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