硬汉嵌入式论坛

 找回密码
 立即注册
查看: 1217|回复: 3
收起左侧

[技术讨论] PendSV在执行中会被被打断吗

[复制链接]

3

主题

9

回帖

18

积分

新手上路

积分
18
发表于 2024-12-12 20:41:46 | 显示全部楼层 |阅读模式
在学RTOS的时候有这样的疑惑 FreeRTOS只是屏蔽了自己可以管理的优先级的中断 那假设我PendSV正在运行前确实没有中断发生,此时PendSV正在运行时,来了一个优先级较高的中断会打断PendSV的运行吗?---而我在看ucosIII的实现的时候就没有这个疑惑,因为ucosIII上来就关掉了所有的中断(在PendSV中)

freertos

freertos

ucosiii

ucosiii
回复

使用道具 举报

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
117530
QQ
发表于 2024-12-13 00:51:15 | 显示全部楼层
FreeRTOS PendSV开关中断处理位置

__asm void xPortPendSVHandler( void )
{
    extern uxCriticalNesting;
    extern pxCurrentTCB;
    extern vTaskSwitchContext;

/* *INDENT-OFF* */
    PRESERVE8

    mrs r0, psp
    isb
    /* Get the location of the current TCB. */
    ldr r3, =pxCurrentTCB
    ldr r2, [ r3 ]

    /* Is the task using the FPU context?  If so, push high vfp registers. */
    tst r14, #0x10
    it eq
    vstmdbeq r0!, {s16-s31}

    /* Save the core registers. */
    stmdb r0!, {r4-r11, r14}

    /* Save the new top of stack into the first member of the TCB. */
    str r0, [ r2 ]

    stmdb sp!, {r0, r3}
    mov r0, #configMAX_SYSCALL_INTERRUPT_PRIORITY
    msr basepri, r0
    dsb
    isb
    bl vTaskSwitchContext
    mov r0, #0
    msr basepri, r0
    ldmia sp!, {r0, r3}

    /* The first item in pxCurrentTCB is the task top of stack. */
    ldr r1, [ r3 ]
    ldr r0, [ r1 ]

    /* Pop the core registers. */
    ldmia r0!, {r4-r11, r14}

    /* Is the task using the FPU context?  If so, pop the high vfp registers
     * too. */
    tst r14, #0x10
    it eq
    vldmiaeq r0!, {s16-s31}

    msr psp, r0
    isb
    #ifdef WORKAROUND_PMU_CM001 /* XMC4000 specific errata */
        #if WORKAROUND_PMU_CM001 == 1
            push { r14 }
            pop { pc }
            nop
        #endif
    #endif

    bx r14
/* *INDENT-ON* */
}



uCOS-III PendSV开关中断处理位置

OS_CPU_PendSVHandler
    CPSID   I                                                   ; Cortex-M7 errata notice. See Note #5
    MOV32   R2, OS_KA_BASEPRI_Boundary                          ; Set BASEPRI priority level required for exception preemption
    LDR     R1, [R2]
    MSR     BASEPRI, R1
    DSB
    ISB
    CPSIE   I

    MRS     R0, PSP                                             ; PSP is process stack pointer
    STMFD   R0!, {R4-R11, R14}                                  ; Save remaining regs r4-11, R14 on process stack

    MOV32   R5, OSTCBCurPtr                                     ; OSTCBCurPtr->StkPtr = SP;
    LDR     R1, [R5]
    STR     R0, [R1]                                            ; R0 is SP of process being switched out

                                                                ; At this point, entire context of process has been saved
    MOV     R4, LR                                              ; Save LR exc_return value
    BL      OSTaskSwHook                                        ; Call OSTaskSwHook() for FPU Push & Pop

    MOV32   R0, OSPrioCur                                       ; OSPrioCur   = OSPrioHighRdy;
    MOV32   R1, OSPrioHighRdy
    LDRB    R2, [R1]
    STRB    R2, [R0]

    MOV32   R1, OSTCBHighRdyPtr                                 ; OSTCBCurPtr = OSTCBHighRdyPtr;
    LDR     R2, [R1]
    STR     R2, [R5]

    ORR     LR,  R4, #0x04                                      ; Ensure exception return uses process stack
    LDR     R0,  [R2]                                           ; R0 is new process SP; SP = OSTCBHighRdyPtr->StkPtr;
    LDMFD   R0!, {R4-R11, R14}                                  ; Restore r4-11, R14 from new process stack
    MSR     PSP, R0                                             ; Load PSP with new process SP

    MOV32   R2, #0                                              ; Restore BASEPRI priority level to 0
    MSR     BASEPRI, R2
    BX      LR                                                  ; Exception return will restore remaining context

    ALIGN                                                       ; Removes warning[A1581W]: added <no_padbytes> of padding at <address>

    END


回复

使用道具 举报

0

主题

5

回帖

5

积分

新手上路

积分
5
发表于 2024-12-13 09:19:59 | 显示全部楼层
“来了一个优先级较高的中断会打断PendSV的运行吗?”
------ 会,但无须担心。也许你没注意到,在PendSV中,汇编指令操作数力图使用R0~R3寄存器,这是因为在PendSV中发生更高优先级中断时,硬件会将R0~R3寄存器压栈;从高优先级中断退出时,硬件将自动恢复R0~R3寄存器,无需软件参与。所以,即使在PendSV中发生了高优先级中断,也不会破坏R0~R3寄存器的内容。事实上,只要处理好某些细节,一些RTOS内核可以不关中断。
回复

使用道具 举报

3

主题

9

回帖

18

积分

新手上路

积分
18
 楼主| 发表于 2024-12-13 11:00:24 | 显示全部楼层
好的谢谢您的回复
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2025-8-12 18:14 , Processed in 0.044504 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表