backtrace.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * This file is part of the CmBacktrace Library.
  3. *
  4. * Copyright (c) 2016-2020, Armink, <armink.ztl@gmail.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * 'Software'), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * Function: It is the macro definition head file for this library.
  26. * Created on: 2016-12-15
  27. */
  28. #ifndef _CMB_DEF_H_
  29. #define _CMB_DEF_H_
  30. #if defined (GD32F30X_HD) || defined (GD32F30X_XD) || defined (GD32F30X_CL)
  31. #include "gd32f30x.h"
  32. #elif defined (GD32E10X)
  33. #include "gd32e10x.h"
  34. #endif
  35. #include <stdbool.h>
  36. #include <stdint.h>
  37. #include <stdlib.h>
  38. /* library software version number */
  39. #define CMB_SW_VERSION "1.4.0"
  40. #define CMB_CPU_ARM_CORTEX_M0 0
  41. #define CMB_CPU_ARM_CORTEX_M3 1
  42. #define CMB_CPU_ARM_CORTEX_M4 2
  43. #define CMB_CPU_ARM_CORTEX_M7 3
  44. #define CMB_OS_PLATFORM_RTT 0
  45. #define CMB_OS_PLATFORM_UCOSII 1
  46. #define CMB_OS_PLATFORM_UCOSIII 2
  47. #define CMB_OS_PLATFORM_FREERTOS 3
  48. #define CMB_PRINT_LANGUAGE_ENGLISH 0
  49. #define CMB_PRINT_LANGUAGE_CHINESE 1
  50. /* name max length, default size: 32 */
  51. #ifndef CMB_NAME_MAX
  52. #define CMB_NAME_MAX 32
  53. #endif
  54. /* print information language, default is English */
  55. #ifndef CMB_PRINT_LANGUAGE
  56. #define CMB_PRINT_LANGUAGE CMB_PRINT_LANGUAGE_ENGLISH
  57. #endif
  58. #if defined(__CC_ARM)
  59. /* C stack block name, default is STACK */
  60. #ifndef CMB_CSTACK_BLOCK_NAME
  61. #define CMB_CSTACK_BLOCK_NAME STACK
  62. #endif
  63. /* code section name, default is ER_IROM1 */
  64. #ifndef CMB_CODE_SECTION_NAME
  65. #define CMB_CODE_SECTION_NAME ER_IROM1
  66. #endif
  67. #elif defined(__ICCARM__)
  68. /* C stack block name, default is 'CSTACK' */
  69. #ifndef CMB_CSTACK_BLOCK_NAME
  70. #define CMB_CSTACK_BLOCK_NAME "CSTACK"
  71. #endif
  72. /* code section name, default is '.text' */
  73. #ifndef CMB_CODE_SECTION_NAME
  74. #define CMB_CODE_SECTION_NAME ".text"
  75. #endif
  76. #elif defined(__GNUC__)
  77. /* C stack block start address, defined on linker script file, default is _sstack */
  78. #ifndef CMB_CSTACK_BLOCK_START
  79. #define CMB_CSTACK_BLOCK_START _sstack
  80. #endif
  81. /* C stack block end address, defined on linker script file, default is _estack */
  82. #ifndef CMB_CSTACK_BLOCK_END
  83. #define CMB_CSTACK_BLOCK_END _estack
  84. #endif
  85. /* code section start address, defined on linker script file, default is _stext */
  86. #ifndef CMB_CODE_SECTION_START
  87. #define CMB_CODE_SECTION_START _stext
  88. #endif
  89. /* code section end address, defined on linker script file, default is _etext */
  90. #ifndef CMB_CODE_SECTION_END
  91. #define CMB_CODE_SECTION_END _etext
  92. #endif
  93. #else
  94. #error "not supported compiler"
  95. #endif
  96. /* supported function call stack max depth, default is 16 */
  97. #ifndef CMB_CALL_STACK_MAX_DEPTH
  98. #define CMB_CALL_STACK_MAX_DEPTH 16
  99. #endif
  100. /* system handler control and state register */
  101. #ifndef CMB_SYSHND_CTRL
  102. #define CMB_SYSHND_CTRL (*(volatile unsigned int*) (0xE000ED24u))
  103. #endif
  104. /* memory management fault status register */
  105. #ifndef CMB_NVIC_MFSR
  106. #define CMB_NVIC_MFSR (*(volatile unsigned char*) (0xE000ED28u))
  107. #endif
  108. /* bus fault status register */
  109. #ifndef CMB_NVIC_BFSR
  110. #define CMB_NVIC_BFSR (*(volatile unsigned char*) (0xE000ED29u))
  111. #endif
  112. /* usage fault status register */
  113. #ifndef CMB_NVIC_UFSR
  114. #define CMB_NVIC_UFSR (*(volatile unsigned short*)(0xE000ED2Au))
  115. #endif
  116. /* hard fault status register */
  117. #ifndef CMB_NVIC_HFSR
  118. #define CMB_NVIC_HFSR (*(volatile unsigned int*) (0xE000ED2Cu))
  119. #endif
  120. /* debug fault status register */
  121. #ifndef CMB_NVIC_DFSR
  122. #define CMB_NVIC_DFSR (*(volatile unsigned short*)(0xE000ED30u))
  123. #endif
  124. /* memory management fault address register */
  125. #ifndef CMB_NVIC_MMAR
  126. #define CMB_NVIC_MMAR (*(volatile unsigned int*) (0xE000ED34u))
  127. #endif
  128. /* bus fault manage address register */
  129. #ifndef CMB_NVIC_BFAR
  130. #define CMB_NVIC_BFAR (*(volatile unsigned int*) (0xE000ED38u))
  131. #endif
  132. /* auxiliary fault status register */
  133. #ifndef CMB_NVIC_AFSR
  134. #define CMB_NVIC_AFSR (*(volatile unsigned short*)(0xE000ED3Cu))
  135. #endif
  136. /**
  137. * Cortex-M fault registers
  138. */
  139. struct cmb_hard_fault_regs{
  140. struct {
  141. unsigned int r0; // Register R0
  142. unsigned int r1; // Register R1
  143. unsigned int r2; // Register R2
  144. unsigned int r3; // Register R3
  145. unsigned int r12; // Register R12
  146. unsigned int lr; // Link register
  147. unsigned int pc; // Program counter
  148. union {
  149. unsigned int value;
  150. struct {
  151. unsigned int IPSR : 8; // Interrupt Program Status register (IPSR)
  152. unsigned int EPSR : 19; // Execution Program Status register (EPSR)
  153. unsigned int APSR : 5; // Application Program Status register (APSR)
  154. } bits;
  155. } psr; // Program status register.
  156. } saved;
  157. union {
  158. unsigned int value;
  159. struct {
  160. unsigned int MEMFAULTACT : 1; // Read as 1 if memory management fault is active
  161. unsigned int BUSFAULTACT : 1; // Read as 1 if bus fault exception is active
  162. unsigned int UnusedBits1 : 1;
  163. unsigned int USGFAULTACT : 1; // Read as 1 if usage fault exception is active
  164. unsigned int UnusedBits2 : 3;
  165. unsigned int SVCALLACT : 1; // Read as 1 if SVC exception is active
  166. unsigned int MONITORACT : 1; // Read as 1 if debug monitor exception is active
  167. unsigned int UnusedBits3 : 1;
  168. unsigned int PENDSVACT : 1; // Read as 1 if PendSV exception is active
  169. unsigned int SYSTICKACT : 1; // Read as 1 if SYSTICK exception is active
  170. unsigned int USGFAULTPENDED : 1; // Usage fault pended; usage fault started but was replaced by a higher-priority exception
  171. unsigned int MEMFAULTPENDED : 1; // Memory management fault pended; memory management fault started but was replaced by a higher-priority exception
  172. unsigned int BUSFAULTPENDED : 1; // Bus fault pended; bus fault handler was started but was replaced by a higher-priority exception
  173. unsigned int SVCALLPENDED : 1; // SVC pended; SVC was started but was replaced by a higher-priority exception
  174. unsigned int MEMFAULTENA : 1; // Memory management fault handler enable
  175. unsigned int BUSFAULTENA : 1; // Bus fault handler enable
  176. unsigned int USGFAULTENA : 1; // Usage fault handler enable
  177. } bits;
  178. } syshndctrl; // System Handler Control and State Register (0xE000ED24)
  179. union {
  180. unsigned char value;
  181. struct {
  182. unsigned char IACCVIOL : 1; // Instruction access violation
  183. unsigned char DACCVIOL : 1; // Data access violation
  184. unsigned char UnusedBits : 1;
  185. unsigned char MUNSTKERR : 1; // Unstacking error
  186. unsigned char MSTKERR : 1; // Stacking error
  187. unsigned char MLSPERR : 1; // Floating-point lazy state preservation (M4/M7)
  188. unsigned char UnusedBits2 : 1;
  189. unsigned char MMARVALID : 1; // Indicates the MMAR is valid
  190. } bits;
  191. } mfsr; // Memory Management Fault Status Register (0xE000ED28)
  192. unsigned int mmar; // Memory Management Fault Address Register (0xE000ED34)
  193. union {
  194. unsigned char value;
  195. struct {
  196. unsigned char IBUSERR : 1; // Instruction access violation
  197. unsigned char PRECISERR : 1; // Precise data access violation
  198. unsigned char IMPREISERR : 1; // Imprecise data access violation
  199. unsigned char UNSTKERR : 1; // Unstacking error
  200. unsigned char STKERR : 1; // Stacking error
  201. unsigned char LSPERR : 1; // Floating-point lazy state preservation (M4/M7)
  202. unsigned char UnusedBits : 1;
  203. unsigned char BFARVALID : 1; // Indicates BFAR is valid
  204. } bits;
  205. } bfsr; // Bus Fault Status Register (0xE000ED29)
  206. unsigned int bfar; // Bus Fault Manage Address Register (0xE000ED38)
  207. union {
  208. unsigned short value;
  209. struct {
  210. unsigned short UNDEFINSTR : 1; // Attempts to execute an undefined instruction
  211. unsigned short INVSTATE : 1; // Attempts to switch to an invalid state (e.g., ARM)
  212. unsigned short INVPC : 1; // Attempts to do an exception with a bad value in the EXC_RETURN number
  213. unsigned short NOCP : 1; // Attempts to execute a coprocessor instruction
  214. unsigned short UnusedBits : 4;
  215. unsigned short UNALIGNED : 1; // Indicates that an unaligned access fault has taken place
  216. unsigned short DIVBYZERO0 : 1; // Indicates a divide by zero has taken place (can be set only if DIV_0_TRP is set)
  217. } bits;
  218. } ufsr; // Usage Fault Status Register (0xE000ED2A)
  219. union {
  220. unsigned int value;
  221. struct {
  222. unsigned int UnusedBits : 1;
  223. unsigned int VECTBL : 1; // Indicates hard fault is caused by failed vector fetch
  224. unsigned int UnusedBits2 : 28;
  225. unsigned int FORCED : 1; // Indicates hard fault is taken because of bus fault/memory management fault/usage fault
  226. unsigned int DEBUGEVT : 1; // Indicates hard fault is triggered by debug event
  227. } bits;
  228. } hfsr; // Hard Fault Status Register (0xE000ED2C)
  229. union {
  230. unsigned int value;
  231. struct {
  232. unsigned int HALTED : 1; // Halt requested in NVIC
  233. unsigned int BKPT : 1; // BKPT instruction executed
  234. unsigned int DWTTRAP : 1; // DWT match occurred
  235. unsigned int VCATCH : 1; // Vector fetch occurred
  236. unsigned int EXTERNAL : 1; // EDBGRQ signal asserted
  237. } bits;
  238. } dfsr; // Debug Fault Status Register (0xE000ED30)
  239. unsigned int afsr; // Auxiliary Fault Status Register (0xE000ED3C), Vendor controlled (optional)
  240. };
  241. static __inline __asm uint32_t cmb_get_msp(void) {
  242. mrs r0, msp
  243. bx lr
  244. }
  245. static __inline __asm uint32_t cmb_get_psp(void) {
  246. mrs r0, psp
  247. bx lr
  248. }
  249. static __inline __asm uint32_t cmb_get_sp(void) {
  250. mov r0, sp
  251. bx lr
  252. }
  253. #endif /* _CMB_DEF_H_ */