cmb_def.h 17 KB

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