打印
[STM32F1]

cortex-m3堆栈的切换

[复制链接]
1471|28
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
pangb|  楼主 | 2019-1-10 17: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
沙发
zhanghqi| | 2019-1-10 18:03 | 只看该作者
楼主详细说说怎么回事啊,这么说太笼统了

使用特权

评论回复
板凳
huangchui| | 2019-1-10 18:07 | 只看该作者
这段代码编译的时候报错,Error:r13 not allowed here--'msr psp,sp'。

使用特权

评论回复
地板
wyjie| | 2019-1-10 18:11 | 只看该作者
R13寄存器可以保存两个指针MSP和PSP

使用特权

评论回复
5
jlyuan| | 2019-1-10 18:14 | 只看该作者
M3有两个等级,一个是用户级和特权级,这个好理解,为了安全嘛

使用特权

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

使用特权

评论回复
7
dengdc| | 2019-1-10 18:43 | 只看该作者

错误的原因是不是在handler模式下执行的msr psp,sp这条指令所以报错了

使用特权

评论回复
8
huangchui| | 2019-1-10 18:47 | 只看该作者
处于什么考虑需要两个堆栈指针,而且要这样应用呢

使用特权

评论回复
9
heweibig| | 2019-1-10 18:50 | 只看该作者
msr 指令,源操作数不允许是 r13(sp) 和 r15(pc)。

使用特权

评论回复
10
wuhany| | 2019-1-10 18:54 | 只看该作者
你需要把 sp 转到一个普通寄存器中再 msr 到 psp

使用特权

评论回复
11
jiahy| | 2019-1-10 18:58 | 只看该作者

mov r0, sp
msr psp, r0

使用特权

评论回复
12
lizye| | 2019-1-10 19:01 | 只看该作者
汇编指令实现这个功能

使用特权

评论回复
13
huangchui| | 2019-1-10 19:06 | 只看该作者


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

使用特权

评论回复
14
zhanghqi| | 2019-1-10 19:08 | 只看该作者
两个栈指针为了提高 RTOS 的效率而设置。

使用特权

评论回复
15
chenjunt| | 2019-1-10 19:11 | 只看该作者
应该是语法不对吧,好像没有 msr psp,sp  这样用的

使用特权

评论回复
16
zhenykun| | 2019-1-10 19:15 | 只看该作者


在handler模式下,只能用MSP指针。

使用特权

评论回复
17
zyf部长| | 2019-1-10 19:19 | 只看该作者
貌似是这样使用 mrs r0, psp     msr   psp, r0  sp用法不对

使用特权

评论回复
18
xxmmi| | 2019-1-10 19:21 | 只看该作者
效率应该被降低了吧,栈指针切换需要额外的指令开销,这个目的应该是为了安全

使用特权

评论回复
19
kangzj| | 2019-1-10 19:25 | 只看该作者

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

使用特权

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

使用特权

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

本版积分规则

701

主题

7643

帖子

6

粉丝