stm32 Hardfault_Handler 打印堆栈信息

  1 /* Private typedef -----------------------------------------------------------*/
  2 enum { r0, r1, r2, r3, r12, lr, pc, psr};
  3 
  4 /* Private define ------------------------------------------------------------*/
  5 /* Private macro -------------------------------------------------------------*/
  6 /* Private variables ---------------------------------------------------------*/
  7 extern __IO uint16_t ADC_InjectedConvertedValueTab[32];
  8 uint32_t Index = 0;
  9 
 10 /* Private function prototypes -----------------------------------------------*/
 11 void Hard_Fault_Handler(uint32_t stack[]);
 12 
 13 /* Private functions ---------------------------------------------------------*/
 14 
 15 static void printUsageErrorMsg(uint32_t CFSRValue)
 16 {
 17     printf("Usage fault: \r\n");
 18     CFSRValue >>= 16; // right shift to lsb
 19 
 20     if((CFSRValue & (1<<9)) != 0) {
 21         printf("Divide by zero \r\n");
 22     }
 23     if((CFSRValue & (1<<8)) != 0) {
 24         printf("Unaligned access \r\n");
 25     }
 26 }
 27 
 28 static void printBusFaultErrorMsg(uint32_t CFSRValue)
 29 {
 30     printf("Bus fault: \r\n");
 31     CFSRValue = ((CFSRValue & 0x0000FF00) >> 8); // mask and right shift to lsb
 32 }
 33 
 34 static void printMemoryManagementErrorMsg(uint32_t CFSRValue)
 35 {
 36     printf("Memory Management fault: \r\n");
 37     CFSRValue &= 0x000000FF; // mask just mem faults
 38 }
 39 
 40 static void stackDump(uint32_t stack[])
 41 {
 42   static char msg[80];
 43   sprintf(msg, "R0 = 0x%08x\r\n", stack[r0]); printf(msg);
 44   sprintf(msg, "R1 = 0x%08x\r\n", stack[r1]); printf(msg);
 45   sprintf(msg, "R2 = 0x%08x\r\n", stack[r2]); printf(msg);
 46   sprintf(msg, "R3 = 0x%08x\r\n", stack[r3]); printf(msg);
 47   sprintf(msg, "R12 = 0x%08x\r\n", stack[r12]); printf(msg);
 48   sprintf(msg, "LR = 0x%08x\r\n", stack[lr]); printf(msg);
 49   sprintf(msg, "PC = 0x%08x\r\n", stack[pc]); printf(msg);
 50   sprintf(msg, "PSR = 0x%08x\r\n", stack[psr]); printf(msg);
 51 }
 52 
 53 void Hard_Fault_Handler(uint32_t stack[])
 54 {
 55   static char msg[80];
 56   //if((CoreDebug->DHCSR & 0x01) != 0) {
 57     printf("\r\nIn Hard Fault Handler\r\n");
 58     sprintf(msg, "SCB->HFSR = 0x%08x\r\n", SCB->HFSR);
 59     printf(msg);
 60     if ((SCB->HFSR & (1 << 30)) != 0) {
 61       printf("Forced Hard Fault\r\n");
 62       sprintf(msg, "SCB->CFSR = 0x%08x\r\n", SCB->CFSR );
 63       printf(msg);
 64       if((SCB->CFSR & 0xFFFF0000) != 0) {
 65         printUsageErrorMsg(SCB->CFSR);
 66       }
 67       if((SCB->CFSR & 0xFF00) != 0) {
 68         printBusFaultErrorMsg(SCB->CFSR);
 69       }
 70       if((SCB->CFSR & 0xFF) != 0) {
 71         printMemoryManagementErrorMsg(SCB->CFSR);
 72       }
 73     }
 74     stackDump(stack);
 75     __ASM volatile("BKPT #01");
 76   //}
 77   while(1);
 78 }
 79 
 80 
 81 __ASM void HardFault_Handler_a(void)
 82 {
 83   IMPORT Hard_Fault_Handler
 84 
 85   TST lr, #4
 86   ITE EQ
 87   MRSEQ r0, MSP
 88   MRSNE r0, PSP
 89   B Hard_Fault_Handler
 90 }
 91 
 92 /**
 93 * @brief This function handles Hard Fault exception.
 94 * @param None
 95 * @retval None
 96 */
 97 void HardFault_Handler(void)
 98 {
 99   /* Go to infinite loop when Hard Fault exception occurs */
100   HardFault_Handler_a();
101 }

 

posted @ 2017-05-16 09:18  sunowfox  阅读(4884)  评论(0编辑  收藏  举报