cpuport.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2009-01-05 Bernard first version
  9. * 2011-02-14 onelife Modify for EFM32
  10. * 2011-06-17 onelife Merge all of the C source code into cpuport.c
  11. * 2012-12-23 aozima stack addr align to 8byte.
  12. * 2012-12-29 Bernard Add exception hook.
  13. * 2013-07-09 aozima enhancement hard fault exception handler.
  14. */
  15. #include <rtthread.h>
  16. struct exception_stack_frame
  17. {
  18. rt_uint32_t r0;
  19. rt_uint32_t r1;
  20. rt_uint32_t r2;
  21. rt_uint32_t r3;
  22. rt_uint32_t r12;
  23. rt_uint32_t lr;
  24. rt_uint32_t pc;
  25. rt_uint32_t psr;
  26. };
  27. struct stack_frame
  28. {
  29. /* r4 ~ r11 register */
  30. rt_uint32_t r4;
  31. rt_uint32_t r5;
  32. rt_uint32_t r6;
  33. rt_uint32_t r7;
  34. rt_uint32_t r8;
  35. rt_uint32_t r9;
  36. rt_uint32_t r10;
  37. rt_uint32_t r11;
  38. struct exception_stack_frame exception_stack_frame;
  39. };
  40. /* flag in interrupt handling */
  41. rt_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread;
  42. rt_uint32_t rt_thread_switch_interrupt_flag;
  43. /* exception hook */
  44. static rt_err_t (*rt_exception_hook)(void *context) = RT_NULL;
  45. /**
  46. * This function will initialize thread stack
  47. *
  48. * @param tentry the entry of thread
  49. * @param parameter the parameter of entry
  50. * @param stack_addr the beginning stack address
  51. * @param texit the function will be called when thread exit
  52. *
  53. * @return stack address
  54. */
  55. rt_uint8_t *rt_hw_stack_init(void *tentry,
  56. void *parameter,
  57. rt_uint8_t *stack_addr,
  58. void *texit)
  59. {
  60. struct stack_frame *stack_frame;
  61. rt_uint8_t *stk;
  62. unsigned long i;
  63. stk = stack_addr + sizeof(rt_uint32_t);
  64. stk = (rt_uint8_t *)RT_ALIGN_DOWN((rt_uint32_t)stk, 8);
  65. stk -= sizeof(struct stack_frame);
  66. stack_frame = (struct stack_frame *)stk;
  67. /* init all register */
  68. for (i = 0; i < sizeof(struct stack_frame) / sizeof(rt_uint32_t); i ++)
  69. {
  70. ((rt_uint32_t *)stack_frame)[i] = 0xdeadbeef;
  71. }
  72. stack_frame->exception_stack_frame.r0 = (unsigned long)parameter; /* r0 : argument */
  73. stack_frame->exception_stack_frame.r1 = 0; /* r1 */
  74. stack_frame->exception_stack_frame.r2 = 0; /* r2 */
  75. stack_frame->exception_stack_frame.r3 = 0; /* r3 */
  76. stack_frame->exception_stack_frame.r12 = 0; /* r12 */
  77. stack_frame->exception_stack_frame.lr = (unsigned long)texit; /* lr */
  78. stack_frame->exception_stack_frame.pc = (unsigned long)tentry; /* entry point, pc */
  79. stack_frame->exception_stack_frame.psr = 0x01000000L; /* PSR */
  80. /* return task's current stack address */
  81. return stk;
  82. }
  83. /**
  84. * This function set the hook, which is invoked on fault exception handling.
  85. *
  86. * @param exception_handle the exception handling hook function.
  87. */
  88. void rt_hw_exception_install(rt_err_t (*exception_handle)(void* context))
  89. {
  90. rt_exception_hook = exception_handle;
  91. }
  92. #define SCB_CFSR (*(volatile const unsigned *)0xE000ED28) /* Configurable Fault Status Register */
  93. #define SCB_HFSR (*(volatile const unsigned *)0xE000ED2C) /* HardFault Status Register */
  94. #define SCB_MMAR (*(volatile const unsigned *)0xE000ED34) /* MemManage Fault Address register */
  95. #define SCB_BFAR (*(volatile const unsigned *)0xE000ED38) /* Bus Fault Address Register */
  96. #define SCB_AIRCR (*(volatile unsigned long *)0xE000ED0C) /* Reset control Address Register */
  97. #define SCB_RESET_VALUE 0x05FA0004 /* Reset value, write to SCB_AIRCR can reset cpu */
  98. #define SCB_CFSR_MFSR (*(volatile const unsigned char*)0xE000ED28) /* Memory-management Fault Status Register */
  99. #define SCB_CFSR_BFSR (*(volatile const unsigned char*)0xE000ED29) /* Bus Fault Status Register */
  100. #define SCB_CFSR_UFSR (*(volatile const unsigned short*)0xE000ED2A) /* Usage Fault Status Register */
  101. #ifdef RT_USING_FINSH
  102. static void usage_fault_track(void)
  103. {
  104. rt_kprintf("usage fault:\n");
  105. rt_kprintf("SCB_CFSR_UFSR:0x%02X ", SCB_CFSR_UFSR);
  106. if(SCB_CFSR_UFSR & (1<<0))
  107. {
  108. /* [0]:UNDEFINSTR */
  109. rt_kprintf("UNDEFINSTR ");
  110. }
  111. if(SCB_CFSR_UFSR & (1<<1))
  112. {
  113. /* [1]:INVSTATE */
  114. rt_kprintf("INVSTATE ");
  115. }
  116. if(SCB_CFSR_UFSR & (1<<2))
  117. {
  118. /* [2]:INVPC */
  119. rt_kprintf("INVPC ");
  120. }
  121. if(SCB_CFSR_UFSR & (1<<3))
  122. {
  123. /* [3]:NOCP */
  124. rt_kprintf("NOCP ");
  125. }
  126. if(SCB_CFSR_UFSR & (1<<8))
  127. {
  128. /* [8]:UNALIGNED */
  129. rt_kprintf("UNALIGNED ");
  130. }
  131. if(SCB_CFSR_UFSR & (1<<9))
  132. {
  133. /* [9]:DIVBYZERO */
  134. rt_kprintf("DIVBYZERO ");
  135. }
  136. rt_kprintf("\n");
  137. }
  138. static void bus_fault_track(void)
  139. {
  140. rt_kprintf("bus fault:\n");
  141. rt_kprintf("SCB_CFSR_BFSR:0x%02X ", SCB_CFSR_BFSR);
  142. if(SCB_CFSR_BFSR & (1<<0))
  143. {
  144. /* [0]:IBUSERR */
  145. rt_kprintf("IBUSERR ");
  146. }
  147. if(SCB_CFSR_BFSR & (1<<1))
  148. {
  149. /* [1]:PRECISERR */
  150. rt_kprintf("PRECISERR ");
  151. }
  152. if(SCB_CFSR_BFSR & (1<<2))
  153. {
  154. /* [2]:IMPRECISERR */
  155. rt_kprintf("IMPRECISERR ");
  156. }
  157. if(SCB_CFSR_BFSR & (1<<3))
  158. {
  159. /* [3]:UNSTKERR */
  160. rt_kprintf("UNSTKERR ");
  161. }
  162. if(SCB_CFSR_BFSR & (1<<4))
  163. {
  164. /* [4]:STKERR */
  165. rt_kprintf("STKERR ");
  166. }
  167. if(SCB_CFSR_BFSR & (1<<7))
  168. {
  169. rt_kprintf("SCB->BFAR:%08X\n", SCB_BFAR);
  170. }
  171. else
  172. {
  173. rt_kprintf("\n");
  174. }
  175. }
  176. static void mem_manage_fault_track(void)
  177. {
  178. rt_kprintf("mem manage fault:\n");
  179. rt_kprintf("SCB_CFSR_MFSR:0x%02X ", SCB_CFSR_MFSR);
  180. if(SCB_CFSR_MFSR & (1<<0))
  181. {
  182. /* [0]:IACCVIOL */
  183. rt_kprintf("IACCVIOL ");
  184. }
  185. if(SCB_CFSR_MFSR & (1<<1))
  186. {
  187. /* [1]:DACCVIOL */
  188. rt_kprintf("DACCVIOL ");
  189. }
  190. if(SCB_CFSR_MFSR & (1<<3))
  191. {
  192. /* [3]:MUNSTKERR */
  193. rt_kprintf("MUNSTKERR ");
  194. }
  195. if(SCB_CFSR_MFSR & (1<<4))
  196. {
  197. /* [4]:MSTKERR */
  198. rt_kprintf("MSTKERR ");
  199. }
  200. if(SCB_CFSR_MFSR & (1<<7))
  201. {
  202. /* [7]:MMARVALID */
  203. rt_kprintf("SCB->MMAR:%08X\n", SCB_MMAR);
  204. }
  205. else
  206. {
  207. rt_kprintf("\n");
  208. }
  209. }
  210. static void hard_fault_track(void)
  211. {
  212. if(SCB_HFSR & (1UL<<1))
  213. {
  214. /* [1]:VECTBL, Indicates hard fault is caused by failed vector fetch. */
  215. rt_kprintf("failed vector fetch\n");
  216. }
  217. if(SCB_HFSR & (1UL<<30))
  218. {
  219. /* [30]:FORCED, Indicates hard fault is taken because of bus fault,
  220. memory management fault, or usage fault. */
  221. if(SCB_CFSR_BFSR)
  222. {
  223. bus_fault_track();
  224. }
  225. if(SCB_CFSR_MFSR)
  226. {
  227. mem_manage_fault_track();
  228. }
  229. if(SCB_CFSR_UFSR)
  230. {
  231. usage_fault_track();
  232. }
  233. }
  234. if(SCB_HFSR & (1UL<<31))
  235. {
  236. /* [31]:DEBUGEVT, Indicates hard fault is triggered by debug event. */
  237. rt_kprintf("debug event\n");
  238. }
  239. }
  240. #endif /* RT_USING_FINSH */
  241. struct exception_info
  242. {
  243. rt_uint32_t exc_return;
  244. struct stack_frame stack_frame;
  245. };
  246. /*
  247. * fault exception handler
  248. */
  249. void rt_hw_hard_fault_exception(struct exception_info * exception_info)
  250. {
  251. extern long list_thread(void);
  252. struct stack_frame* context = &exception_info->stack_frame;
  253. if (rt_exception_hook != RT_NULL)
  254. {
  255. rt_err_t result;
  256. result = rt_exception_hook(exception_info);
  257. if (result == RT_EOK)
  258. return;
  259. }
  260. rt_kprintf("psr: 0x%08x\n", context->exception_stack_frame.psr);
  261. rt_kprintf("r00: 0x%08x\n", context->exception_stack_frame.r0);
  262. rt_kprintf("r01: 0x%08x\n", context->exception_stack_frame.r1);
  263. rt_kprintf("r02: 0x%08x\n", context->exception_stack_frame.r2);
  264. rt_kprintf("r03: 0x%08x\n", context->exception_stack_frame.r3);
  265. rt_kprintf("r04: 0x%08x\n", context->r4);
  266. rt_kprintf("r05: 0x%08x\n", context->r5);
  267. rt_kprintf("r06: 0x%08x\n", context->r6);
  268. rt_kprintf("r07: 0x%08x\n", context->r7);
  269. rt_kprintf("r08: 0x%08x\n", context->r8);
  270. rt_kprintf("r09: 0x%08x\n", context->r9);
  271. rt_kprintf("r10: 0x%08x\n", context->r10);
  272. rt_kprintf("r11: 0x%08x\n", context->r11);
  273. rt_kprintf("r12: 0x%08x\n", context->exception_stack_frame.r12);
  274. rt_kprintf(" lr: 0x%08x\n", context->exception_stack_frame.lr);
  275. rt_kprintf(" pc: 0x%08x\n", context->exception_stack_frame.pc);
  276. if(exception_info->exc_return & (1 << 2) )
  277. {
  278. rt_kprintf("hard fault on thread: %s\r\n\r\n", rt_thread_self()->name);
  279. #ifdef RT_USING_FINSH
  280. list_thread();
  281. #endif /* RT_USING_FINSH */
  282. }
  283. else
  284. {
  285. rt_kprintf("hard fault on handler\r\n\r\n");
  286. }
  287. #ifdef RT_USING_FINSH
  288. hard_fault_track();
  289. #endif /* RT_USING_FINSH */
  290. while (1);
  291. }
  292. /**
  293. * shutdown CPU
  294. */
  295. void rt_hw_cpu_shutdown(void)
  296. {
  297. rt_kprintf("shutdown...\n");
  298. RT_ASSERT(0);
  299. }
  300. /**
  301. * reset CPU
  302. */
  303. RT_WEAK void rt_hw_cpu_reset(void)
  304. {
  305. SCB_AIRCR = SCB_RESET_VALUE;
  306. }
  307. #ifdef RT_USING_CPU_FFS
  308. /**
  309. * This function finds the first bit set (beginning with the least significant bit)
  310. * in value and return the index of that bit.
  311. *
  312. * Bits are numbered starting at 1 (the least significant bit). A return value of
  313. * zero from any of these functions means that the argument was zero.
  314. *
  315. * @return return the index of the first bit set. If value is 0, then this function
  316. * shall return 0.
  317. */
  318. #if defined(__CC_ARM)
  319. __asm int __rt_ffs(int value)
  320. {
  321. CMP r0, #0x00
  322. BEQ exit
  323. RBIT r0, r0
  324. CLZ r0, r0
  325. ADDS r0, r0, #0x01
  326. exit
  327. BX lr
  328. }
  329. #elif defined(__IAR_SYSTEMS_ICC__)
  330. int __rt_ffs(int value)
  331. {
  332. if (value == 0) return value;
  333. asm("RBIT %0, %1" : "=r"(value) : "r"(value));
  334. asm("CLZ %0, %1" : "=r"(value) : "r"(value));
  335. asm("ADDS %0, %1, #0x01" : "=r"(value) : "r"(value));
  336. return value;
  337. }
  338. #elif defined(__GNUC__)
  339. int __rt_ffs(int value)
  340. {
  341. return __builtin_ffs(value);
  342. }
  343. #endif
  344. #endif