cmb_def.h 17 KB

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