硬汉嵌入式论坛

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

[μCOS-III] 编写UCOS III 中断服务程序(ISR)的疑问

[复制链接]

2

主题

4

回帖

10

积分

新手上路

积分
10
发表于 2023-3-11 14:16:15 | 显示全部楼层 |阅读模式
为什么官方的OS_CPU_SysTickHandler没有
if (OSIntNestingCtr == 1) {                                 
        OSTCBCurPtr->StkPtr = Current task's CPU stack pointer register value;
}
D1~RL7QVEY80E`NGL@}U`MJ.png
GM6)3_OYM}JBLOE`LL$X00O.png
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106660
QQ
发表于 2023-3-11 16:38:19 | 显示全部楼层
实际上这个是uCOS的特色,而且是早期的一种玩法,中断嵌套计数OSIntNestingCtr主要是为了实现发生中断嵌套的话,保证退出到最后一级中断后才可以执行任务切换。

现在到了M内核后,基本已经不需要这个东西了,M内核有强大的NVIC中断优先级管理机制,用户仅需将PendSV设置为最低优先级,即可保证发生中断嵌套的话,它是最后执行的。
回复

使用道具 举报

19

主题

371

回帖

428

积分

高级会员

积分
428
发表于 2023-3-11 16:47:34 | 显示全部楼层
cortex-m系列的U,都是硬件保证了,不用手动操作
回复

使用道具 举报

2

主题

4

回帖

10

积分

新手上路

积分
10
 楼主| 发表于 2023-3-12 12:19:40 | 显示全部楼层
eric2013 发表于 2023-3-11 16:38
实际上这个是uCOS的特色,而且是早期的一种玩法,中断嵌套计数OSIntNestingCtr主要是为了实现发生中断嵌套 ...

重看了一下权威内核指南,是不是利用了咬尾中断,然后在OS_CPU_PendSVHandler里实现了
OSTCBCurPtr->StkPtr = Current task's CPU stack pointer register value(由于OS_CPU_PendSVHandler是最低级)
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106660
QQ
发表于 2023-3-12 12:23:17 | 显示全部楼层
BF109 发表于 2023-3-12 12:19
重看了一下权威内核指南,是不是利用了咬尾中断,然后在OS_CPU_PendSVHandler里实现了
OSTCBCurPtr->Stk ...

咬不咬尾不影响,主要是中断嵌套机制处理
回复

使用道具 举报

2

主题

4

回帖

10

积分

新手上路

积分
10
 楼主| 发表于 2023-3-13 17:50:34 | 显示全部楼层
eric2013 发表于 2023-3-12 12:23
咬不咬尾不影响,主要是中断嵌套机制处理

还有一种不用通知内核的
FAST Interrupt Service Routine
MyShortISR:                                                      (1)
    Save enough registers as needed by the ISR;    (2)
    Clear interrupting device;                                (3)
    DO NOT re-enable interrupts;                          (4)
    Call user ISR;                                                 (5)
    Restore the saved CPU registers;                      (6)
    Return from interrupt;                                     (7)
这里有一个DO NOT re-enable interrupts;可是进入中断后,中断一直是使能的啊。
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106660
QQ
发表于 2023-3-13 17:58:00 | 显示全部楼层
BF109 发表于 2023-3-13 17:50
还有一种不用通知内核的
FAST Interrupt Service Routine
MyShortISR:                                ...

中断就要一直开着,而且关闭的时间越短越好,这个中断严重影响其它高优先级中断的及时响应

以uCOS自己的Systick和PendSV中断,里面关键的地方都做了开关中断处理。

比如PendSV

[Asm] 纯文本查看 复制代码
;********************************************************************************************************
;                                       HANDLE PendSV EXCEPTION
;                                   void OS_CPU_PendSVHandler(void)
;
; Note(s) : 1) PendSV is used to cause a context switch.  This is a recommended method for performing
;              context switches with Cortex-M.  This is because the Cortex-M auto-saves half of the
;              processor context on any exception, and restores same on return from exception.  So only
;              saving of R4-R11 & R14 is required and fixing up the stack pointers. Using the PendSV exception
;              this way means that context saving and restoring is identical whether it is initiated from
;              a thread or occurs due to an interrupt or exception.
;
;           2) Pseudo-code is:
;              a) Get the process SP
;              b) Save remaining regs r4-r11 & r14 on process stack;
;              c) Save the process SP in its TCB, OSTCBCurPtr->OSTCBStkPtr = SP;
;              d) Call OSTaskSwHook();
;              e) Get current high priority, OSPrioCur = OSPrioHighRdy;
;              f) Get current ready thread TCB, OSTCBCurPtr = OSTCBHighRdyPtr;
;              g) Get new process SP from TCB, SP = OSTCBHighRdyPtr->OSTCBStkPtr;
;              h) Restore R4-R11 and R14 from new process stack;
;              i) Perform exception return which will restore remaining context.
;
;           3) On entry into PendSV handler:
;              a) The following have been saved on the process stack (by processor):
;                 xPSR, PC, LR, R12, R0-R3
;              b) Processor mode is switched to Handler mode (from Thread mode)
;              c) Stack is Main stack (switched from Process stack)
;              d) OSTCBCurPtr      points to the OS_TCB of the task to suspend
;                 OSTCBHighRdyPtr  points to the OS_TCB of the task to resume
;
;           4) Since PendSV is set to lowest priority in the system (by OSStartHighRdy() above), we
;              know that it will only be run when no other exception or interrupt is active, and
;              therefore safe to assume that context being switched out was using the process stack (PSP).
;
;           5) Increasing priority using a write to BASEPRI does not take effect immediately.
;              (a) IMPLICATION  This erratum means that the instruction after an MSR to boost BASEPRI
;                  might incorrectly be preempted by an insufficient high priority exception.
;
;              (b) WORKAROUND  The MSR to boost BASEPRI can be replaced by the following code sequence:
;
;                  CPSID i
;                  MSR to BASEPRI
;                  DSB
;                  ISB
;                  CPSIE i
;********************************************************************************************************

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
    IF {FPU} != "SoftVFP"
                                                                ; Push high vfp registers if the task is using the FPU context
    TST       R14, #0x10
    IT        EQ
    VSTMDBEQ  R0!, {S16-S31}
    ENDIF

    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

    IF {FPU} != "SoftVFP"
                                                                ; Pop the high vfp registers if the next task is using the FPU context
    TST       R14, #0x10
    IT        EQ
    VLDMIAEQ  R0!, {S16-S31}
    ENDIF

    MSR     PSP, R0                                             ; Load PSP with new process SP

    MOV32   R2, #0                                              ; Restore BASEPRI priority level to 0
    CPSID   I                                                   ; Cortex-M7 errata notice. See Note #5
    MSR     BASEPRI, R2
    DSB
    ISB
    CPSIE   I
    BX      LR                                                  ; Exception return will restore remaining context

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

    END





回复

使用道具 举报

2

主题

4

回帖

10

积分

新手上路

积分
10
 楼主| 发表于 2023-3-13 18:07:02 | 显示全部楼层
eric2013 发表于 2023-3-13 17:58
中断就要一直开着,而且关闭的时间越短越好,这个中断严重影响其它高优先级中断的及时响应

以uCOS自己 ...

我的意思是,既然进入中断是开启状态,手册里为什么还要写一个”DO NOT re-enable interrupts“这个是ucos手册里一种中断的写法。
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106660
QQ
发表于 2023-3-13 18:11:45 | 显示全部楼层
BF109 发表于 2023-3-13 18:07
我的意思是,既然进入中断是开启状态,手册里为什么还要写一个”DO NOT re-enable interrupts“这个是uco ...

手册描述有点老,大家看着费劲,看这个,一目了然,一看就懂


回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-29 06:42 , Processed in 0.184648 second(s), 28 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

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