Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. #include <cstdarg>
  2. #include <cstdint>
  3. #include <cstdio>
  4. #include "libopencm3/stm32/usart.h"
  5. #include "libopencmsis/core_cm3.h"
  6.  
  7. static void PanicPrintf(const char* fmt, ...)
  8. {
  9. va_list l;
  10. va_start(l, fmt);
  11. char buf[200] = {0};
  12. vsprintf(buf, fmt, l);
  13.  
  14. const char* c = buf;
  15. while (*c != '\0')
  16. {
  17. usart_send_blocking(USART2, *c);
  18. c++;
  19. }
  20.  
  21. va_end(l);
  22. }
  23.  
  24. typedef struct
  25. {
  26. __I uint32_t CPUID; /*!< Offset: 0x000 (R/ ) CPUID Base Register */
  27. __IO uint32_t ICSR; /*!< Offset: 0x004 (R/W) Interrupt Control and State Register */
  28. __IO uint32_t VTOR; /*!< Offset: 0x008 (R/W) Vector Table Offset Register */
  29. __IO uint32_t AIRCR; /*!< Offset: 0x00C (R/W) Application Interrupt and Reset Control Register */
  30. __IO uint32_t SCR; /*!< Offset: 0x010 (R/W) System Control Register */
  31. __IO uint32_t CCR; /*!< Offset: 0x014 (R/W) Configuration Control Register */
  32. __IO uint8_t SHP[12]; /*!< Offset: 0x018 (R/W) System Handlers Priority Registers (4-7, 8-11, 12-15) */
  33. __IO uint32_t SHCSR; /*!< Offset: 0x024 (R/W) System Handler Control and State Register */
  34. __IO uint32_t CFSR; /*!< Offset: 0x028 (R/W) Configurable Fault Status Register */
  35. __IO uint32_t HFSR; /*!< Offset: 0x02C (R/W) HardFault Status Register */
  36. __IO uint32_t DFSR; /*!< Offset: 0x030 (R/W) Debug Fault Status Register */
  37. __IO uint32_t MMFAR; /*!< Offset: 0x034 (R/W) MemManage Fault Address Register */
  38. __IO uint32_t BFAR; /*!< Offset: 0x038 (R/W) BusFault Address Register */
  39. __IO uint32_t AFSR; /*!< Offset: 0x03C (R/W) Auxiliary Fault Status Register */
  40. __I uint32_t PFR[2]; /*!< Offset: 0x040 (R/ ) Processor Feature Register */
  41. __I uint32_t DFR; /*!< Offset: 0x048 (R/ ) Debug Feature Register */
  42. __I uint32_t ADR; /*!< Offset: 0x04C (R/ ) Auxiliary Feature Register */
  43. __I uint32_t MMFR[4]; /*!< Offset: 0x050 (R/ ) Memory Model Feature Register */
  44. __I uint32_t ISAR[5]; /*!< Offset: 0x060 (R/ ) Instruction Set Attributes Register */
  45. uint32_t RESERVED0[5];
  46. __IO uint32_t CPACR; /*!< Offset: 0x088 (R/W) Coprocessor Access Control Register */
  47. } SCB_Type;
  48.  
  49. #define SCB_FULL ((SCB_Type*)SCB_BASE)
  50.  
  51. extern "C" __attribute__((used)) void prvGetRegistersFromStack(uint32_t* pulFaultStackAddress)
  52. {
  53. /* These are volatile to try and prevent the compiler/linker optimising them
  54. away as the variables never actually get used. If the debugger won't show the
  55. values of the variables, make them global my moving their declaration outside
  56. of this function. */
  57.  
  58. volatile uint32_t r0 = pulFaultStackAddress[0];
  59. volatile uint32_t r1 = pulFaultStackAddress[1];
  60. volatile uint32_t r2 = pulFaultStackAddress[2];
  61. volatile uint32_t r3 = pulFaultStackAddress[3];
  62.  
  63. volatile uint32_t r12 = pulFaultStackAddress[4];
  64. volatile uint32_t lr = pulFaultStackAddress[5]; /* Link register. */
  65. volatile uint32_t pc = pulFaultStackAddress[6]; /* Program counter. */
  66. volatile uint32_t psr = pulFaultStackAddress[7]; /* Program status register. */
  67.  
  68. // volatile uint16_t* nvic = *((uint16_t*)0xe000ed04);
  69. uint32_t cfsr = SCB_FULL->CFSR;
  70. uint32_t hfsr = SCB_FULL->HFSR;
  71. uint32_t mmfar = SCB_FULL->MMFAR;
  72. uint32_t bfar = SCB_FULL->BFAR;
  73.  
  74. /* When the following line is hit, the variables contain the register values. */
  75.  
  76. PanicPrintf("CFSR: 0x%X\nHFSR: 0x%X\nMMFAR: 0x%X\nBFAR: 0x%X\nLR: 0x%X\nPC: 0x%X\nPSR: 0x%X", cfsr, hfsr, mmfar, bfar, lr, pc, psr);
  77.  
  78. while (1)
  79. ;
  80. }
  81.  
  82. __attribute__((naked)) void hard_fault_handler()
  83. {
  84. __asm volatile(" tst lr, #4 \n"
  85. " ite eq \n"
  86. " mrseq r0, msp \n"
  87. " mrsne r0, psp \n"
  88. " ldr r1, [r0, #24] \n"
  89. " ldr r2, handler2_address_constHF \n"
  90. " bx r2 \n"
  91. " handler2_address_constHF: .word prvGetRegistersFromStack \n");
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement