cpuport.c 10 KB

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