cmb_def.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * This file is part of the CmBacktrace Library.
  3. *
  4. * Copyright (c) 2016-2018, 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. #include <cmb_cfg.h>
  31. #include <stdint.h>
  32. #include <stdlib.h>
  33. /* library software version number */
  34. #define CMB_SW_VERSION "1.2.1"
  35. #define CMB_CPU_ARM_CORTEX_M0 0
  36. #define CMB_CPU_ARM_CORTEX_M3 1
  37. #define CMB_CPU_ARM_CORTEX_M4 2
  38. #define CMB_CPU_ARM_CORTEX_M7 3
  39. #define CMB_OS_PLATFORM_RTT 0
  40. #define CMB_OS_PLATFORM_UCOSII 1
  41. #define CMB_OS_PLATFORM_UCOSIII 2
  42. #define CMB_OS_PLATFORM_FREERTOS 3
  43. #define CMB_PRINT_LANGUAGE_ENGLISH 0
  44. #define CMB_PRINT_LANUUAGE_CHINESE 1
  45. /* name max length, default size: 32 */
  46. #ifndef CMB_NAME_MAX
  47. #define CMB_NAME_MAX 32
  48. #endif
  49. /* print information language, default is English */
  50. #ifndef CMB_PRINT_LANGUAGE
  51. #define CMB_PRINT_LANGUAGE CMB_PRINT_LANGUAGE_ENGLISH
  52. #endif
  53. #if defined(__CC_ARM)
  54. /* C stack block name, default is STACK */
  55. #ifndef CMB_CSTACK_BLOCK_NAME
  56. #define CMB_CSTACK_BLOCK_NAME STACK
  57. #endif
  58. /* code section name, default is ER_IROM1 */
  59. #ifndef CMB_CODE_SECTION_NAME
  60. #define CMB_CODE_SECTION_NAME ER_IROM1
  61. #endif
  62. #elif defined(__ICCARM__)
  63. /* C stack block name, default is 'CSTACK' */
  64. #ifndef CMB_CSTACK_BLOCK_NAME
  65. #define CMB_CSTACK_BLOCK_NAME "CSTACK"
  66. #endif
  67. /* code section name, default is '.text' */
  68. #ifndef CMB_CODE_SECTION_NAME
  69. #define CMB_CODE_SECTION_NAME ".text"
  70. #endif
  71. #elif defined(__GNUC__)
  72. /* C stack block start address, defined on linker script file, default is _sstack */
  73. #ifndef CMB_CSTACK_BLOCK_START
  74. #define CMB_CSTACK_BLOCK_START _sstack
  75. #endif
  76. /* C stack block end address, defined on linker script file, default is _estack */
  77. #ifndef CMB_CSTACK_BLOCK_END
  78. #define CMB_CSTACK_BLOCK_END _estack
  79. #endif
  80. /* code section start address, defined on linker script file, default is _stext */
  81. #ifndef CMB_CODE_SECTION_START
  82. #define CMB_CODE_SECTION_START _stext
  83. #endif
  84. /* code section end address, defined on linker script file, default is _etext */
  85. #ifndef CMB_CODE_SECTION_END
  86. #define CMB_CODE_SECTION_END _etext
  87. #endif
  88. #else
  89. #error "not supported compiler"
  90. #endif
  91. /* supported function call stack max depth, default is 16 */
  92. #ifndef CMB_CALL_STACK_MAX_DEPTH
  93. #define CMB_CALL_STACK_MAX_DEPTH 16
  94. #endif
  95. /* system handler control and state register */
  96. #ifndef CMB_SYSHND_CTRL
  97. #define CMB_SYSHND_CTRL (*(volatile unsigned int*) (0xE000ED24u))
  98. #endif
  99. /* memory management fault status register */
  100. #ifndef CMB_NVIC_MFSR
  101. #define CMB_NVIC_MFSR (*(volatile unsigned char*) (0xE000ED28u))
  102. #endif
  103. /* bus fault status register */
  104. #ifndef CMB_NVIC_BFSR
  105. #define CMB_NVIC_BFSR (*(volatile unsigned char*) (0xE000ED29u))
  106. #endif
  107. /* usage fault status register */
  108. #ifndef CMB_NVIC_UFSR
  109. #define CMB_NVIC_UFSR (*(volatile unsigned short*)(0xE000ED2Au))
  110. #endif
  111. /* hard fault status register */
  112. #ifndef CMB_NVIC_HFSR
  113. #define CMB_NVIC_HFSR (*(volatile unsigned int*) (0xE000ED2Cu))
  114. #endif
  115. /* debug fault status register */
  116. #ifndef CMB_NVIC_DFSR
  117. #define CMB_NVIC_DFSR (*(volatile unsigned short*)(0xE000ED30u))
  118. #endif
  119. /* memory management fault address register */
  120. #ifndef CMB_NVIC_MMAR
  121. #define CMB_NVIC_MMAR (*(volatile unsigned int*) (0xE000ED34u))
  122. #endif
  123. /* bus fault manage address register */
  124. #ifndef CMB_NVIC_BFAR
  125. #define CMB_NVIC_BFAR (*(volatile unsigned int*) (0xE000ED38u))
  126. #endif
  127. /* auxiliary fault status register */
  128. #ifndef CMB_NVIC_AFSR
  129. #define CMB_NVIC_AFSR (*(volatile unsigned short*)(0xE000ED3Cu))
  130. #endif
  131. /**
  132. * Cortex-M fault registers
  133. */
  134. struct cmb_hard_fault_regs{
  135. struct {
  136. unsigned int r0; // Register R0
  137. unsigned int r1; // Register R1
  138. unsigned int r2; // Register R2
  139. unsigned int r3; // Register R3
  140. unsigned int r12; // Register R12
  141. unsigned int lr; // Link register
  142. unsigned int pc; // Program counter
  143. union {
  144. unsigned int value;
  145. struct {
  146. unsigned int IPSR : 8; // Interrupt Program Status register (IPSR)
  147. unsigned int EPSR : 19; // Execution Program Status register (EPSR)
  148. unsigned int APSR : 5; // Application Program Status register (APSR)
  149. } bits;
  150. } psr; // Program status register.
  151. } saved;
  152. union {
  153. unsigned int value;
  154. struct {
  155. unsigned int MEMFAULTACT : 1; // Read as 1 if memory management fault is active
  156. unsigned int BUSFAULTACT : 1; // Read as 1 if bus fault exception is active
  157. unsigned int UnusedBits1 : 1;
  158. unsigned int USGFAULTACT : 1; // Read as 1 if usage fault exception is active
  159. unsigned int UnusedBits2 : 3;
  160. unsigned int SVCALLACT : 1; // Read as 1 if SVC exception is active
  161. unsigned int MONITORACT : 1; // Read as 1 if debug monitor exception is active
  162. unsigned int UnusedBits3 : 1;
  163. unsigned int PENDSVACT : 1; // Read as 1 if PendSV exception is active
  164. unsigned int SYSTICKACT : 1; // Read as 1 if SYSTICK exception is active
  165. unsigned int USGFAULTPENDED : 1; // Usage fault pended; usage fault started but was replaced by a higher-priority exception
  166. unsigned int MEMFAULTPENDED : 1; // Memory management fault pended; memory management fault started but was replaced by a higher-priority exception
  167. unsigned int BUSFAULTPENDED : 1; // Bus fault pended; bus fault handler was started but was replaced by a higher-priority exception
  168. unsigned int SVCALLPENDED : 1; // SVC pended; SVC was started but was replaced by a higher-priority exception
  169. unsigned int MEMFAULTENA : 1; // Memory management fault handler enable
  170. unsigned int BUSFAULTENA : 1; // Bus fault handler enable
  171. unsigned int USGFAULTENA : 1; // Usage fault handler enable
  172. } bits;
  173. } syshndctrl; // System Handler Control and State Register (0xE000ED24)
  174. union {
  175. unsigned char value;
  176. struct {
  177. unsigned char IACCVIOL : 1; // Instruction access violation
  178. unsigned char DACCVIOL : 1; // Data access violation
  179. unsigned char UnusedBits : 1;
  180. unsigned char MUNSTKERR : 1; // Unstacking error
  181. unsigned char MSTKERR : 1; // Stacking error
  182. unsigned char MLSPERR : 1; // Floating-point lazy state preservation (M4/M7)
  183. unsigned char UnusedBits2 : 1;
  184. unsigned char MMARVALID : 1; // Indicates the MMAR is valid
  185. } bits;
  186. } mfsr; // Memory Management Fault Status Register (0xE000ED28)
  187. unsigned int mmar; // Memory Management Fault Address Register (0xE000ED34)
  188. union {
  189. unsigned char value;
  190. struct {
  191. unsigned char IBUSERR : 1; // Instruction access violation
  192. unsigned char PRECISERR : 1; // Precise data access violation
  193. unsigned char IMPREISERR : 1; // Imprecise data access violation
  194. unsigned char UNSTKERR : 1; // Unstacking error
  195. unsigned char STKERR : 1; // Stacking error
  196. unsigned char LSPERR : 1; // Floating-point lazy state preservation (M4/M7)
  197. unsigned char UnusedBits : 1;
  198. unsigned char BFARVALID : 1; // Indicates BFAR is valid
  199. } bits;
  200. } bfsr; // Bus Fault Status Register (0xE000ED29)
  201. unsigned int bfar; // Bus Fault Manage Address Register (0xE000ED38)
  202. union {
  203. unsigned short value;
  204. struct {
  205. unsigned short UNDEFINSTR : 1; // Attempts to execute an undefined instruction
  206. unsigned short INVSTATE : 1; // Attempts to switch to an invalid state (e.g., ARM)
  207. unsigned short INVPC : 1; // Attempts to do an exception with a bad value in the EXC_RETURN number
  208. unsigned short NOCP : 1; // Attempts to execute a coprocessor instruction
  209. unsigned short UnusedBits : 4;
  210. unsigned short UNALIGNED : 1; // Indicates that an unaligned access fault has taken place
  211. unsigned short DIVBYZERO0 : 1; // Indicates a divide by zero has taken place (can be set only if DIV_0_TRP is set)
  212. } bits;
  213. } ufsr; // Usage Fault Status Register (0xE000ED2A)
  214. union {
  215. unsigned int value;
  216. struct {
  217. unsigned int UnusedBits : 1;
  218. unsigned int VECTBL : 1; // Indicates hard fault is caused by failed vector fetch
  219. unsigned int UnusedBits2 : 28;
  220. unsigned int FORCED : 1; // Indicates hard fault is taken because of bus fault/memory management fault/usage fault
  221. unsigned int DEBUGEVT : 1; // Indicates hard fault is triggered by debug event
  222. } bits;
  223. } hfsr; // Hard Fault Status Register (0xE000ED2C)
  224. union {
  225. unsigned int value;
  226. struct {
  227. unsigned int HALTED : 1; // Halt requested in NVIC
  228. unsigned int BKPT : 1; // BKPT instruction executed
  229. unsigned int DWTTRAP : 1; // DWT match occurred
  230. unsigned int VCATCH : 1; // Vector fetch occurred
  231. unsigned int EXTERNAL : 1; // EDBGRQ signal asserted
  232. } bits;
  233. } dfsr; // Debug Fault Status Register (0xE000ED30)
  234. unsigned int afsr; // Auxiliary Fault Status Register (0xE000ED3C), Vendor controlled (optional)
  235. };
  236. /* assert for developer. */
  237. #define CMB_ASSERT(EXPR) \
  238. if (!(EXPR)) \
  239. { \
  240. cmb_println("(%s) has assert failed at %s.", #EXPR, __FUNCTION__); \
  241. while (1); \
  242. }
  243. /* ELF(Executable and Linking Format) file extension name for each compiler */
  244. #if defined(__CC_ARM)
  245. #define CMB_ELF_FILE_EXTENSION_NAME ".axf"
  246. #elif defined(__ICCARM__)
  247. #define CMB_ELF_FILE_EXTENSION_NAME ".out"
  248. #elif defined(__GNUC__)
  249. #define CMB_ELF_FILE_EXTENSION_NAME ".elf"
  250. #else
  251. #error "not supported compiler"
  252. #endif
  253. #ifndef cmb_println
  254. #error "cmb_println isn't defined in 'cmb_cfg.h'"
  255. #endif
  256. #ifndef CMB_CPU_PLATFORM_TYPE
  257. #error "CMB_CPU_PLATFORM_TYPE isn't defined in 'cmb_cfg.h'"
  258. #endif
  259. #if (defined(CMB_USING_BARE_METAL_PLATFORM) && defined(CMB_USING_OS_PLATFORM))
  260. #error "CMB_USING_BARE_METAL_PLATFORM and CMB_USING_OS_PLATFORM only one of them can be used"
  261. #elif defined(CMB_USING_OS_PLATFORM)
  262. #if !defined(CMB_OS_PLATFORM_TYPE)
  263. #error "CMB_OS_PLATFORM_TYPE isn't defined in 'cmb_cfg.h'"
  264. #endif /* !defined(CMB_OS_PLATFORM_TYPE) */
  265. #if (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_RTT)
  266. #include <rtthread.h>
  267. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_UCOSII)
  268. #include <ucos_ii.h>
  269. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_UCOSIII)
  270. #include <os.h>
  271. #elif (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_FREERTOS)
  272. #include <FreeRTOS.h>
  273. extern uint32_t *vTaskStackAddr(void);/* need to modify the FreeRTOS/tasks source code */
  274. extern uint32_t vTaskStackSize(void);
  275. extern char * vTaskName(void);
  276. #else
  277. #error "not supported OS type"
  278. #endif /* (CMB_OS_PLATFORM_TYPE == CMB_OS_PLATFORM_RTT) */
  279. #endif /* (defined(CMB_USING_BARE_METAL_PLATFORM) && defined(CMB_USING_OS_PLATFORM)) */
  280. /* include or export for supported cmb_get_msp, cmb_get_psp and cmb_get_sp function */
  281. #if defined(__CC_ARM)
  282. static __inline __asm uint32_t cmb_get_msp(void) {
  283. mrs r0, msp
  284. bx lr
  285. }
  286. static __inline __asm uint32_t cmb_get_psp(void) {
  287. mrs r0, psp
  288. bx lr
  289. }
  290. static __inline __asm uint32_t cmb_get_sp(void) {
  291. mov r0, sp
  292. bx lr
  293. }
  294. #elif defined(__ICCARM__)
  295. /* IAR iccarm specific functions */
  296. /* Close Raw Asm Code Warning */
  297. #pragma diag_suppress=Pe940
  298. static uint32_t cmb_get_msp(void)
  299. {
  300. __asm("mrs r0, msp");
  301. __asm("bx lr");
  302. }
  303. static uint32_t cmb_get_psp(void)
  304. {
  305. __asm("mrs r0, psp");
  306. __asm("bx lr");
  307. }
  308. static uint32_t cmb_get_sp(void)
  309. {
  310. __asm("mov r0, sp");
  311. __asm("bx lr");
  312. }
  313. #pragma diag_default=Pe940
  314. #elif defined(__GNUC__)
  315. __attribute__( ( always_inline ) ) static inline uint32_t cmb_get_msp(void) {
  316. register uint32_t result;
  317. __asm volatile ("MRS %0, msp\n" : "=r" (result) );
  318. return(result);
  319. }
  320. __attribute__( ( always_inline ) ) static inline uint32_t cmb_get_psp(void) {
  321. register uint32_t result;
  322. __asm volatile ("MRS %0, psp\n" : "=r" (result) );
  323. return(result);
  324. }
  325. __attribute__( ( always_inline ) ) static inline uint32_t cmb_get_sp(void) {
  326. register uint32_t result;
  327. __asm volatile ("MOV %0, sp\n" : "=r" (result) );
  328. return(result);
  329. }
  330. #else
  331. #error "not supported compiler"
  332. #endif
  333. #endif /* _CMB_DEF_H_ */