完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
rt_thread_switch_interrupt_flag == 1,说明要切换,既然要切换,为什么要忽略from的值呢?flag影响的就是是否忽略from,但是如果进行切换,就不能忽略from的值啊,谢谢。 |
|
相关推荐
1个回答
|
|
直接在此说结论: rt_thread_switch_interrupt_flag是为了配合cortex-M平台属性(切换线程需要借助pendSV中断实现)所设置的标志位。 具体实现 /libcpu/arm/cortex-mx/xxxxx.S: ;/* ; * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to); ; * r0 --> from ; * r1 --> to ; */ rt_hw_context_switch_interrupt EXPORT rt_hw_context_switch_interrupt rt_hw_context_switch PROC EXPORT rt_hw_context_switch ; set rt_thread_switch_interrupt_flag to 1 LDR r2, =rt_thread_switch_interrupt_flag LDR r3, [r2] CMP r3, #1 ;这里先进行了比较,flag之前就等于1时跳转 _reswitch BEQ _reswitch MOVS r3, #0x01 ; 未执行跳转时,就将flag置为1 STR r3, [r2] LDR r2, =rt_interrupt_from_thread ; set rt_interrupt_from_thread STR r0, [r2] _reswitch LDR r2, =rt_interrupt_to_thread ; set rt_interrupt_to_thread STR r1, [r2] LDR r0, =NVIC_INT_CTRL ; trigger the PendSV exception (causes context switch) LDR r1, =NVIC_PENDSVSET STR r1, [r0] BX LR ENDP ;/* ; * void rt_hw_context_switch_to(rt_uint32 to); ; * r0 --> to ; * this fucntion is used to perform the first thread switch ; */ rt_hw_context_switch_to PROC EXPORT rt_hw_context_switch_to ; set to thread LDR r1, =rt_interrupt_to_thread STR r0, [r1] ; set from thread to 0 LDR r1, =rt_interrupt_from_thread MOVS r0, #0x0 STR r0, [r1] ; set interrupt flag to 1 LDR r1, =rt_thread_switch_interrupt_flag MOVS r0, #1 STR r0, [r1] ;这里直接赋值了flag = 1 ; set the PendSV and SysTick exception priority LDR r0, =NVIC_SHPR3 LDR r1, =NVIC_PENDSV_PRI LDR r2, [r0,#0x00] ; read ORRS r1,r1,r2 ; modify STR r1, [r0] ; write-back ; trigger the PendSV exception (causes context switch) LDR r0, =NVIC_INT_CTRL LDR r1, =NVIC_PENDSVSET STR r1, [r0] ; restore MSP LDR r0, =SCB_VTOR LDR r0, [r0] LDR r0, [r0] MSR msp, r0 ; enable interrupts at processor level CPSIE I ; ensure PendSV exception taken place before subsequent operation DSB ISB ; never reach here! ENDP 可以看见: rt_hw_context_switch 函数切换线程前,进行了如你图示的flag比较,据此确定是否带上from线程context。 rt_hw_context_switch_to函数切线程是不需要from线程context的,因为它只在调度器首次启动时被调用,它没有from线程。 图示,一个常见的cortex-m上下文切换模型: 再看pendSV这边: ; r0 --> switch from thread stack ; r1 --> switch to thread stack ; psr, pc, lr, r12, r3, r2, r1, r0 are pushed into [from] stack PendSV_Handler PROC EXPORT PendSV_Handler ; disable interrupt to protect context switch MRS r2, PRIMASK CPSID I ; get rt_thread_switch_interrupt_flag LDR r0, =rt_thread_switch_interrupt_flag LDR r1, [r0] CMP r1, #0x00 ; 如果flag等于0 就直接跳出pendSV, 不执行上下文切换 BEQ pendsv_exit ; pendsv already handled ; clear rt_thread_switch_interrupt_flag to 0 MOVS r1, #0x00 STR r1, [r0] LDR r0, =rt_interrupt_from_thread LDR r1, [r0] CMP r1, #0x00 BEQ switch_to_thread ; skip register save at the first time MRS r1, psp ; get from thread stack pointer SUBS r1, r1, #0x20 ; space for {r4 - r7} and {r8 - r11} LDR r0, [r0] STR r1, [r0] ; update from thread stack pointer STMIA r1!, {r4 - r7} ; push thread {r4 - r7} register to thread stack MOV r4, r8 ; mov thread {r8 - r11} to {r4 - r7} MOV r5, r9 MOV r6, r10 MOV r7, r11 STMIA r1!, {r4 - r7} ; push thread {r8 - r11} high register to thread stack switch_to_thread LDR r1, =rt_interrupt_to_thread LDR r1, [r1] LDR r1, [r1] ; load thread stack pointer LDMIA r1!, {r4 - r7} ; pop thread {r4 - r7} register from thread stack PUSH {r4 - r7} ; push {r4 - r7} to MSP for copy {r8 - r11} LDMIA r1!, {r4 - r7} ; pop thread {r8 - r11} high register from thread stack to {r4 - r7} MOV r8, r4 ; mov {r4 - r7} to {r8 - r11} MOV r9, r5 MOV r10, r6 MOV r11, r7 POP {r4 - r7} ; pop {r4 - r7} from MSP MSR psp, r1 ; update stack pointer pendsv_exit ; restore interrupt MSR PRIMASK, r2 MOVS r0, #0x04 RSBS r0, r0, #0x00 BX r0 ENDP 我们已经明确:cortex-m 需要借助pendSV执行上下文切换,此时问题转变为: pendSV处理函数中,rt_thread_switch_interrupt_flag = 0 时,为何需要直接跳出pendSV,不执行shedule切换? 那答案就呼之欲出了,这个flag本身的意义就在这:需要切换上下文的时候,就置位flag,从而在pendSV中断服务中执行调度。 |
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
830 浏览 0 评论
6169 浏览 0 评论
如何使用python调起UDE STK5.2进行下载自动化下载呢?
2808 浏览 0 评论
开启全新AI时代 智能嵌入式系统快速发展——“第六届国产嵌入式操作系统技术与产业发展论坛”圆满结束
3099 浏览 0 评论
获奖公布!2024 RT-Thread全球巡回线下培训火热来袭!报名提问有奖!
33131 浏览 11 评论
73592 浏览 21 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-2 23:42 , Processed in 0.517700 second(s), Total 74, Slave 55 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号