当前位置: 代码迷 >> 驱动开发 >> 关于ucos_ii任务切换优先级有关问题
  详细解决方案

关于ucos_ii任务切换优先级有关问题

热度:73   发布时间:2016-04-28 11:07:56.0
求助:关于ucos_ii任务切换优先级问题
最近在移植ucos_ii到stm32中去,历经挫折……(废话就不多说了)
眼看成功在即,发现任务切换问题总是会跳到HardFault_Handler中去。

环境:我在现在创建两个任务,任务A(优先级10)和任务B(优先级11),利用信号量事件驱动任务切换,初始化时把A的信号量初始值为1,B任务信号量为0。A任务先开始运行,执行完以后发信号给任务B,B收到信号,OSSemPend对B进行任务切换;B运行完,发信号给A,A收到信号,OSSemped切换任务……

现象:两个任务切换前十数次成功,跑一会以后会跳到HardFault_Handler中去。

大概原因:
1、单步调试时我发现,要单步较长时间才会产生中断;
2、任务优先级OSPrioHighRdy发生未知变化,不再是10或11而是其它值;
3、LR寄存器地址为不存在的0xFFFFFFF5
4、因为LR寄存器地址不存在,产生中断。



问题来了,怎么解决这个错误?是不是我分析错了?
跪求大牛莅临指导。

------解决方案--------------------
任务切换的时候要关闭中断。。。。
------解决方案--------------------
HardFault_Handler 一般是有数据溢出引起的 ,比如数据越界
给你个查看问题的思路吧:
*.s
HardFault_Handler\
PROC
; EXPORT HardFault_Handler [WEAK]
; B .

IMPORT hard_fault_handler_c
TST LR, #4
ITE EQ
MRSEQ R0, MSP
MRSNE R0, PSP
B hard_fault_handler_c
ENDP 


*.c
void hard_fault_handler_c(unsigned int * hardfault_args) 
{
/* Go to infinite loop when Hard Fault exception occurs */

unsigned int stacked_r0;
unsigned int stacked_r1;
unsigned int stacked_r2;
unsigned int stacked_r3;
unsigned int stacked_r12;
unsigned int stacked_lr;
unsigned int stacked_pc;
unsigned int stacked_psr;

stacked_r0 = ((unsigned long) hardfault_args[0]);
stacked_r1 = ((unsigned long) hardfault_args[1]);
stacked_r2 = ((unsigned long) hardfault_args[2]);
stacked_r3 = ((unsigned long) hardfault_args[3]);

stacked_r12 = ((unsigned long) hardfault_args[4]);
stacked_lr = ((unsigned long) hardfault_args[5]);
stacked_pc = ((unsigned long) hardfault_args[6]);
stacked_psr = ((unsigned long) hardfault_args[7]);

printf ("[Hard fault handler]\r\n");
printf("R0 = %x\r\n", stacked_r0);
printf ("R1 = %x\r\n", stacked_r1);
printf ("R2 = %x\r\n", stacked_r2);
printf ("R3 = %x\r\n", stacked_r3);
printf ("R12 = %x\r\n", stacked_r12);
printf ("LR = %x\r\n", stacked_lr);
printf ("PC = %x\r\n", stacked_pc);
printf ("PSR = %x\r\n", stacked_psr);
printf ("BFAR = %x\r\n", (*((volatile unsigned long *)(0xE000ED38))));
printf ("CFSR = %x\r\n", (*((volatile unsigned long *)(0xE000ED28))));
printf ("HFSR = %x\r\n", (*((volatile unsigned long *)(0xE000ED2C))));
printf("DFSR = %x\r\n", (*((volatile unsigned long *)(0xE000ED30))));
printf("AFSR = %x\r\n", (*((volatile unsigned long *)(0xE000ED3C)))); 
while (1)
{
}
}

这样把出错前的寄存器打印出来 看从那进入的
  相关解决方案