HardFault_Handler|HardFault_Handler 输出日志信息
之前文章说了原理,这里把最终实现的代码总结:
IAR
文章图片
文章图片
1 void hard_fault_handler_c (unsigned int * hardfault_args) 2 { 3unsigned int stacked_r0; 4unsigned int stacked_r1; 5unsigned int stacked_r2; 6unsigned int stacked_r3; 7unsigned int stacked_r12; 8unsigned int stacked_lr; 9unsigned int stacked_pc; 10unsigned int stacked_psr; 11 12stacked_r0 = ((unsigned long) hardfault_args[0]); 13stacked_r1 = ((unsigned long) hardfault_args[1]); 14stacked_r2 = ((unsigned long) hardfault_args[2]); 15stacked_r3 = ((unsigned long) hardfault_args[3]); 16 17stacked_r12 = ((unsigned long) hardfault_args[4]); 18stacked_lr = ((unsigned long) hardfault_args[5]); 19stacked_pc = ((unsigned long) hardfault_args[6]); 20stacked_psr = ((unsigned long) hardfault_args[7]); 21 22printf ("\r\n[Hard fault handler - all numbers in hex]\r\n"); 23printf ("R0=%x\r\n",stacked_r0); 24printf ("R1=%x\r\n",stacked_r1); 25printf ("R2=%x\r\n",stacked_r2); 26printf ("R3=%x\r\n",stacked_r3); 27printf ("R12=%x\r\n",stacked_r12); 28printf ("LR[R14]=%x subroutine call return address\r\n",stacked_lr); 29printf ("PC[R15]=%x program counter\r\n",stacked_pc); 30printf ("PSR=%x\r\n",stacked_psr); 31printf ("BFAR=%x\r\n",(*((volatile unsigned long *)(0xE000ED38)))); 32printf ("CFSR=%x\r\n",(*((volatile unsigned long *)(0xE000ED28)))); 33printf ("HFSR=%x\r\n",(*((volatile unsigned long *)(0xE000ED2C)))); 34printf ("DFSR=%x\r\n",(*((volatile unsigned long *)(0xE000ED30)))); 35printf ("AFSR=%x\r\n",(*((volatile unsigned long *)(0xE000ED3C)))); 36printf ("SCB_SHCSR=%x\r\n", SCB->SHCSR); 37 38 39while (1); 40 }
hard_fault_handler_c
文章图片
文章图片
1 void HardFault_Handler( void ) 2 { 3 __ASM("TST LR, #4"); 4 __ASM("ITE EQ"); 5 __ASM("MRSEQ R0, MSP"); 6 __ASM("MRSNE R0, PSP"); 7 __ASM("B hard_fault_handler_c"); 8 }
HardFault_Handler 这里的HardFault_Handler里是内联汇编
MDK不能使用内联汇编
文章图片
文章图片
1 __ASM void HardFault_Handler(void) 2 { 3TST lr, #4// Test for MSP or PSP 4ITE EQ 5MRSEQ r0, MSP 6MRSNE r0, PSP 7IMPORT HardFault_Handler_C 8B HardFault_Handler_C 9 }
HardFault_Handler
文章图片
文章图片
1 void HardFault_Handler_C (unsigned int * hardfault_args) 2 { 3unsigned int stacked_r0; 4unsigned int stacked_r1; 5unsigned int stacked_r2; 6unsigned int stacked_r3; 7unsigned int stacked_r12; 8unsigned int stacked_lr; 9unsigned int stacked_pc; 10unsigned int stacked_psr; 11 12stacked_r0 = ((unsigned long) hardfault_args[0]); 13stacked_r1 = ((unsigned long) hardfault_args[1]); 14stacked_r2 = ((unsigned long) hardfault_args[2]); 15stacked_r3 = ((unsigned long) hardfault_args[3]); 16 17stacked_r12 = ((unsigned long) hardfault_args[4]); 18stacked_lr = ((unsigned long) hardfault_args[5]); 19stacked_pc = ((unsigned long) hardfault_args[6]); 20stacked_psr = ((unsigned long) hardfault_args[7]); 21 22printf ("\n\n[Hard fault handler - all numbers in hex]\r\n"); 23printf ("R0 = %x\r\n", stacked_r0); 24printf ("R1 = %x\r\n", stacked_r1); 25printf ("R2 = %x\r\n", stacked_r2); 26printf ("R3 = %x\r\n", stacked_r3); 27printf ("R12 = %x\r\n", stacked_r12); 28printf ("LR [R14] = %xsubroutine call return address\r\n", stacked_lr); 29printf ("PC [R15] = %xprogram counter\r\n", stacked_pc); 30printf ("PSR = %x\r\n", stacked_psr); 31printf ("BFAR = %x\r\n", (*((volatile unsigned long *)(0xE000ED38)))); 32printf ("CFSR = %x\r\n", (*((volatile unsigned long *)(0xE000ED28)))); 33printf ("HFSR = %x\r\n", (*((volatile unsigned long *)(0xE000ED2C)))); 34printf ("DFSR = %x\r\n", (*((volatile unsigned long *)(0xE000ED30)))); 35printf ("AFSR = %x\r\n", (*((volatile unsigned long *)(0xE000ED3C)))); 36printf ("SCB_SHCSR = %x\r\n", SCB->SHCSR); 37 38while (1); 39 }
HardFault_Handler_C
1 [Hard fault handler - all numbers in hex] 2 R0 = 11111000 3 R1 = 8000378 4 R2 = 8000379 5 R3 = c0 6 R12 = 400 7 LR [R14] = 8001759subroutine call return address 8 PC [R15] = 8000340program counter 9 PSR = 1000000 10 BFAR = 11111111 11 CFSR = 8200 12 HFSR = 40000000 13 DFSR = 1 14 AFSR = 0 15 SCB_SHCSR = 0
对于调试很有帮助的,注意PC[R15]的数值是我们需要的
【HardFault_Handler|HardFault_Handler 输出日志信息】转载于:https://www.cnblogs.com/wwjdwy/p/3818354.html
推荐阅读
- 《深度倾听》第5天──「RIA学习力」便签输出第16期
- 《思考,快与慢》第10天──「RIA学习力」便签输出第8期|《思考,快与慢》第10天──「RIA学习力」便签输出第8期 央央
- 选择篇(021)-下面代码的输出是什么?
- 选择篇(022)-下面代码的输出是什么?
- 用输出倒逼输入,加油!
- 童频输出
- python日志重复输出
- 【勇哥创业日记】学会输出,人人都可以成为有影响力的人
- WebAPI|WebAPI 多对多对象输出json异常()
- 2018.8.6阅读输出