cpuport.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * File : cpuport.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, 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. */
  18. #include <rtthread.h>
  19. struct exception_stack_frame
  20. {
  21. rt_uint32_t r0;
  22. rt_uint32_t r1;
  23. rt_uint32_t r2;
  24. rt_uint32_t r3;
  25. rt_uint32_t r12;
  26. rt_uint32_t lr;
  27. rt_uint32_t pc;
  28. rt_uint32_t psr;
  29. };
  30. struct stack_frame
  31. {
  32. /* r4 ~ r11 register */
  33. rt_uint32_t r4;
  34. rt_uint32_t r5;
  35. rt_uint32_t r6;
  36. rt_uint32_t r7;
  37. rt_uint32_t r8;
  38. rt_uint32_t r9;
  39. rt_uint32_t r10;
  40. rt_uint32_t r11;
  41. struct exception_stack_frame exception_stack_frame;
  42. };
  43. /* flag in interrupt handling */
  44. rt_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread;
  45. rt_uint32_t rt_thread_switch_interrupt_flag;
  46. /* exception hook */
  47. static rt_err_t (*rt_exception_hook)(void *context) = RT_NULL;
  48. /**
  49. * This function will initialize thread stack
  50. *
  51. * @param tentry the entry of thread
  52. * @param parameter the parameter of entry
  53. * @param stack_addr the beginning stack address
  54. * @param texit the function will be called when thread exit
  55. *
  56. * @return stack address
  57. */
  58. rt_uint8_t *rt_hw_stack_init(void *tentry,
  59. void *parameter,
  60. rt_uint8_t *stack_addr,
  61. void *texit)
  62. {
  63. struct stack_frame *stack_frame;
  64. rt_uint8_t *stk;
  65. unsigned long i;
  66. stk = stack_addr + sizeof(rt_uint32_t);
  67. stk = (rt_uint8_t *)RT_ALIGN_DOWN((rt_uint32_t)stk, 8);
  68. stk -= sizeof(struct stack_frame);
  69. stack_frame = (struct stack_frame *)stk;
  70. /* init all register */
  71. for (i = 0; i < sizeof(struct stack_frame) / sizeof(rt_uint32_t); i ++)
  72. {
  73. ((rt_uint32_t *)stack_frame)[i] = 0xdeadbeef;
  74. }
  75. stack_frame->exception_stack_frame.r0 = (unsigned long)parameter; /* r0 : argument */
  76. stack_frame->exception_stack_frame.r1 = 0; /* r1 */
  77. stack_frame->exception_stack_frame.r2 = 0; /* r2 */
  78. stack_frame->exception_stack_frame.r3 = 0; /* r3 */
  79. stack_frame->exception_stack_frame.r12 = 0; /* r12 */
  80. stack_frame->exception_stack_frame.lr = (unsigned long)texit; /* lr */
  81. stack_frame->exception_stack_frame.pc = (unsigned long)tentry; /* entry point, pc */
  82. stack_frame->exception_stack_frame.psr = 0x01000000L; /* PSR */
  83. /* return task's current stack address */
  84. return stk;
  85. }
  86. /**
  87. * This function set the hook, which is invoked on fault exception handling.
  88. *
  89. * @param exception_handle the exception handling hook function.
  90. */
  91. void rt_hw_exception_install(rt_err_t (*exception_handle)(void* context))
  92. {
  93. rt_exception_hook = exception_handle;
  94. }
  95. /*
  96. * fault exception handler
  97. */
  98. void rt_hw_hard_fault_exception(struct exception_stack_frame* context)
  99. {
  100. extern long list_thread(void);
  101. if (rt_exception_hook != RT_NULL)
  102. {
  103. rt_err_t result;
  104. result = rt_exception_hook(context);
  105. if (result == RT_EOK) return;
  106. }
  107. rt_kprintf("psr: 0x%08x\n", context->psr);
  108. rt_kprintf(" pc: 0x%08x\n", context->pc);
  109. rt_kprintf(" lr: 0x%08x\n", context->lr);
  110. rt_kprintf("r12: 0x%08x\n", context->r12);
  111. rt_kprintf("r03: 0x%08x\n", context->r3);
  112. rt_kprintf("r02: 0x%08x\n", context->r2);
  113. rt_kprintf("r01: 0x%08x\n", context->r1);
  114. rt_kprintf("r00: 0x%08x\n", context->r0);
  115. rt_kprintf("hard fault on thread: %s\n", rt_thread_self()->name);
  116. #ifdef RT_USING_FINSH
  117. list_thread();
  118. #endif
  119. while (1);
  120. }
  121. /**
  122. * shutdown CPU
  123. */
  124. void rt_hw_cpu_shutdown(void)
  125. {
  126. rt_kprintf("shutdown...\n");
  127. RT_ASSERT(0);
  128. }
  129. #ifdef RT_USING_CPU_FFS
  130. /**
  131. * This function finds the first bit set (beginning with the least significant bit)
  132. * in value and return the index of that bit.
  133. *
  134. * Bits are numbered starting at 1 (the least significant bit). A return value of
  135. * zero from any of these functions means that the argument was zero.
  136. *
  137. * @return return the index of the first bit set. If value is 0, then this function
  138. * shall return 0.
  139. */
  140. #if defined(__CC_ARM)
  141. __asm int __rt_ffs(int value)
  142. {
  143. CMP r0, #0x00
  144. BEQ exit
  145. RBIT r0, r0
  146. CLZ r0, r0
  147. ADDS r0, r0, #0x01
  148. exit
  149. BX lr
  150. }
  151. #elif defined(__IAR_SYSTEMS_ICC__)
  152. int __rt_ffs(int value)
  153. {
  154. if (value == 0) return value;
  155. __ASM("RBIT r0, r0");
  156. __ASM("CLZ r0, r0");
  157. __ASM("ADDS r0, r0, #0x01");
  158. }
  159. #elif defined(__GNUC__)
  160. int __rt_ffs(int value)
  161. {
  162. return __builtin_ffs(value);
  163. }
  164. #endif
  165. #endif