STM32调用OS_EXIT_CRITICAL()时遇到HardFault

When calling OS_EXIT_CRITICAL(), STM32 runs into HardFault

很奇怪

OSInit();
OSTimeDly(10);
OSTaskCreate(start_task,(void *)0,(OS_STK *)&START_TASK_STK[TASK_STK_SIZE-1],START_TASK_PRIO);
OSStart();

OSTimeDly(10)中有OS_Enter_CRITICAL()和OS_Exit_Critical(),当代码运行变成OS_Exit_Critical()时,HardFault会发生了。

void  OSTimeDly (INT32U ticks)
{
INT8U      y;
#if OS_CRITICAL_METHOD == 3u                     /* Allocate storage for CPU     status register           */
OS_CPU_SR  cpu_sr = 0u;
#endif



if (OSIntNesting > 0u) {                     /* See if trying to call from an ISR                  */
    return;
}
if (OSLockNesting > 0u) {                    /* See if called with scheduler locked                */
    return;
}
if (ticks > 0u) {                            /* 0 means no delay!                                  */
    OS_ENTER_CRITICAL();
    y            =  OSTCBCur->OSTCBY;        /* Delay current task                                 */
    OSRdyTbl[y] &= (OS_PRIO)~OSTCBCur->OSTCBBitX;
    if (OSRdyTbl[y] == 0u) {
        OSRdyGrp &= (OS_PRIO)~OSTCBCur->OSTCBBitY;
    }
    OSTCBCur->OSTCBDly = ticks;              /* Load ticks in TCB                                  */
    OS_EXIT_CRITICAL();
    OS_Sched();                              /* Find next task to run!                             */
    }
}

但是,如果我将代码更改为:

#if OS_CRITICAL_METHOD == 3u                     /* Allocate storage for CPU status register           */
OS_CPU_SR  cpu_sr = 0u;
#endif
INT8U y;
...

OSInit();
OS_ENTER_CRITICAL();
y            =  OSTCBCur->OSTCBY;        /* Delay current task   */
OSRdyTbl[y] &= (OS_PRIO)~OSTCBCur->OSTCBBitX;
if (OSRdyTbl[y] == 0u) {
    OSRdyGrp &= (OS_PRIO)~OSTCBCur->OSTCBBitY;
}
OS_EXIT_CRITICAL();
//OSTimeDly(10);
OSTaskCreate(start_task,(void *)0,(OS_STK *)&START_TASK_STK[TASK_STK_SIZE-1],START_TASK_PRIO);
OSStart();

还有OS_ENTER_CRITICAL()和OS_EXIT_CRITICAL(),但是当代码运行变成OS_EXIT_CRITICAL()时,没有HardFault happens.Why ?我认为没有什么不同。

您在启动操作系统之前调用 OSTimeDly()

如果你想在启动 OS(通过 OSStart())之前 delay/pause/wait,你将不得不使用带有 volatile 的 "dummy loop",或者使用硬件定时器(更好)等