cmb_port.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * This file is part of the CmBacktrace Library.
  3. *
  4. * Copyright (c) 2016-2018, zylx, <1346773219@qq.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: Initialize function and other general function.
  26. * Created on: 2016-12-15
  27. */
  28. #include <rtthread.h>
  29. #include <rthw.h>
  30. #include <cm_backtrace.h>
  31. #include <string.h>
  32. #if defined(__CC_ARM)
  33. #pragma O1
  34. #elif defined(__ICCARM__)
  35. #pragma optimize=none
  36. #elif defined(__GNUC__)
  37. #pragma GCC optimize ("O0")
  38. #endif
  39. #if defined(__CC_ARM)
  40. static __inline __asm void cmb_set_psp(uint32_t psp) {
  41. msr psp, r0
  42. bx lr
  43. }
  44. #elif defined(__ICCARM__)
  45. /* IAR iccarm specific functions */
  46. /* Close Raw Asm Code Warning */
  47. #pragma diag_suppress=Pe940
  48. static void cmb_set_psp(uint32_t psp)
  49. {
  50. __asm("msr psp, r0");
  51. __asm("bx lr");
  52. }
  53. #pragma diag_default=Pe940
  54. #elif defined(__GNUC__)
  55. __attribute__( ( always_inline ) ) static inline void cmb_set_psp(uint32_t psp) {
  56. __asm volatile ("MSR psp, %0\n\t" :: "r" (psp) );
  57. }
  58. #else
  59. #error "not supported compiler"
  60. #endif
  61. RT_WEAK rt_err_t exception_hook(void *context) {
  62. volatile uint8_t _continue = 1;
  63. uint8_t lr_offset = 0;
  64. uint32_t lr;
  65. #define CMB_LR_WORD_OFFSET_START 6
  66. #define CMB_LR_WORD_OFFSET_END 20
  67. #define CMB_SP_WORD_OFFSET (lr_offset + 1)
  68. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M0) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M3)
  69. #define EXC_RETURN_MASK 0x0000000F // Bits[31:4]
  70. #else
  71. #define EXC_RETURN_MASK 0x0000000F // Bits[31:5]
  72. #endif
  73. rt_enter_critical();
  74. #ifdef RT_USING_FINSH
  75. extern long list_thread(void);
  76. list_thread();
  77. #endif
  78. /* the PSP is changed by RT-Thread HardFault_Handler, so restore it to HardFault context */
  79. #if (defined (__VFP_FP__) && !defined(__SOFTFP__)) || (defined (__ARMVFP__)) || (defined(__ARM_PCS_VFP) || defined(__TARGET_FPU_VFP))
  80. cmb_set_psp(cmb_get_psp() + 4 * 10);
  81. #else
  82. cmb_set_psp(cmb_get_psp() + 4 * 9);
  83. #endif
  84. /* auto calculate the LR offset */
  85. for (lr_offset = CMB_LR_WORD_OFFSET_START; lr_offset <= CMB_LR_WORD_OFFSET_END; lr_offset ++)
  86. {
  87. lr = *((uint32_t *)(cmb_get_sp() + sizeof(uint32_t) * lr_offset));
  88. /*
  89. * Cortex-M0: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0497a/Babefdjc.html
  90. * Cortex-M3: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0552a/Babefdjc.html
  91. * Cortex-M4: http://infocenter.arm.com/help/topic/com.arm.doc.dui0553b/DUI0553.pdf P41
  92. * Cortex-M7: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0646c/Babefdjc.html
  93. */
  94. if ((lr == 0xFFFFFFF1) || (lr == 0xFFFFFFF9) || (lr == 0xFFFFFFFD)
  95. #if (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M4) || (CMB_CPU_PLATFORM_TYPE == CMB_CPU_ARM_CORTEX_M3)
  96. || (lr == 0xFFFFFFE1) || (lr == 0xFFFFFFE9) || (lr == 0xFFFFFFED)
  97. #endif
  98. )
  99. {
  100. break;
  101. }
  102. }
  103. cm_backtrace_fault(lr, cmb_get_sp() + sizeof(uint32_t) * CMB_SP_WORD_OFFSET);
  104. cmb_println("Current system tick: %ld", rt_tick_get());
  105. while (_continue == 1);
  106. return RT_EOK;
  107. }
  108. RT_WEAK void assert_hook(const char* ex, const char* func, rt_size_t line) {
  109. volatile uint8_t _continue = 1;
  110. rt_enter_critical();
  111. #ifdef RT_USING_FINSH
  112. extern long list_thread(void);
  113. list_thread();
  114. #endif
  115. cmb_println("");
  116. cmb_println("(%s) has assert failed at %s:%ld.", ex, func, line);
  117. cm_backtrace_assert(cmb_get_sp());
  118. cmb_println("Current system tick: %ld", rt_tick_get());
  119. while (_continue == 1);
  120. }
  121. int rt_cm_backtrace_init(void) {
  122. cm_backtrace_init("luatos","1.0","V0007");
  123. rt_hw_exception_install(exception_hook);
  124. rt_assert_set_hook(assert_hook);
  125. return 0;
  126. }
  127. INIT_DEVICE_EXPORT(rt_cm_backtrace_init);
  128. long cmb_test(int argc, char **argv) {
  129. volatile int * SCB_CCR = (volatile int *) 0xE000ED14; // SCB->CCR
  130. int x, y, z;
  131. if (argc < 2)
  132. {
  133. rt_kprintf("Please input 'cmb_test <DIVBYZERO|UNALIGNED>' \n");
  134. return 0;
  135. }
  136. if (!strcmp(argv[1], "DIVBYZERO"))
  137. {
  138. *SCB_CCR |= (1 << 4); /* bit4: DIV_0_TRP. */
  139. x = 10;
  140. y = rt_strlen("");
  141. z = x / y;
  142. rt_kprintf("z:%d\n", z);
  143. return 0;
  144. }
  145. else if (!strcmp(argv[1], "UNALIGNED"))
  146. {
  147. volatile int * p;
  148. volatile int value;
  149. *SCB_CCR |= (1 << 3); /* bit3: UNALIGN_TRP. */
  150. p = (int *) 0x00;
  151. value = *p;
  152. rt_kprintf("addr:0x%02X value:0x%08X\r\n", (int) p, value);
  153. p = (int *) 0x04;
  154. value = *p;
  155. rt_kprintf("addr:0x%02X value:0x%08X\r\n", (int) p, value);
  156. p = (int *) 0x03;
  157. value = *p;
  158. rt_kprintf("addr:0x%02X value:0x%08X\r\n", (int) p, value);
  159. return 0;
  160. }
  161. return 0;
  162. }
  163. MSH_CMD_EXPORT(cmb_test, cm_backtrace_test: cmb_test <DIVBYZERO|UNALIGNED> );