cpuport.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * File : cpuport.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009 - 2011, 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. * 2011-02-23 Bernard the first version
  13. * 2012-03-03 xuzhenglim modify for rx62N
  14. */
  15. #include <rthw.h>
  16. #include <rtthread.h>
  17. #include "cpuconfig.h"
  18. #include "machine.h"
  19. #include "iorx62n.h"
  20. #define ENTER_INTERRUPT() ICU.SWINTR.BIT.SWINT = 1;
  21. extern volatile rt_uint8_t rt_interrupt_nest;
  22. /* switch flag on interrupt and thread pointer to save switch record */
  23. rt_uint32_t rt_interrupt_from_thread;
  24. rt_uint32_t rt_interrupt_to_thread;
  25. rt_uint32_t rt_thread_switch_interrupt_flag;
  26. /* stack frame*/
  27. struct stack_frame
  28. {
  29. rt_uint32_t ACCLO;
  30. rt_uint32_t ACCHI;
  31. rt_uint32_t FPSW;
  32. rt_uint32_t R1;
  33. rt_uint32_t R2;
  34. rt_uint32_t R3;
  35. rt_uint32_t R4;
  36. rt_uint32_t R5;
  37. rt_uint32_t R6;
  38. rt_uint32_t R7;
  39. rt_uint32_t R8;
  40. rt_uint32_t R9;
  41. rt_uint32_t R10;
  42. rt_uint32_t R11;
  43. rt_uint32_t R12;
  44. rt_uint32_t R13;
  45. rt_uint32_t R14;
  46. rt_uint32_t R15;
  47. //there is not R0 register,it is special for stack pointer
  48. rt_uint32_t PC;
  49. rt_uint32_t PSW;
  50. };
  51. /**
  52. * Initilial the thread stack.
  53. *
  54. * @author LXZ (2014/11/8)
  55. *
  56. * @param void* tentry
  57. * @param void* parameter
  58. * @param rt_uint8_t* stack_addr
  59. * @param void* texit
  60. *
  61. * @return rt_uint8_t*
  62. */
  63. rt_uint8_t *rt_hw_stack_init(void *tentry, void *parameter,
  64. rt_uint8_t *stack_addr, void *texit)
  65. {
  66. unsigned long *stk;
  67. struct stack_frame *stack_frame;
  68. unsigned long i;
  69. stk = (unsigned long *)stack_addr;
  70. *(stk) = (unsigned long)texit;
  71. stack_frame = (struct stack_frame *)(stack_addr - sizeof(struct stack_frame)) ;
  72. //Initilial all register
  73. for (i = 0; i < sizeof(struct stack_frame) / sizeof(rt_uint32_t); i ++)
  74. {
  75. ((rt_uint32_t *)stack_frame)[i] = 0xdeadbeef;
  76. }
  77. stack_frame->PSW = (unsigned long)0x00030000 ; /* psw */
  78. stack_frame->PC = (unsigned long)tentry; /* thread entery*/
  79. stack_frame->R1 = (unsigned long )parameter; /* r1 : parameter */
  80. stack_frame->FPSW = 0x00000100; /* fpsw */
  81. return(rt_uint8_t *)stack_frame;
  82. }
  83. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  84. extern void list_thread(void);
  85. #endif
  86. extern rt_thread_t rt_current_thread;
  87. /**
  88. * deal exception
  89. *
  90. * @author LXZ (2014/11/8)
  91. *
  92. * @param struct stack_frame* exception_contex
  93. */
  94. void rt_hw_hard_fault_exception(struct stack_frame* exception_contex)
  95. {
  96. if (exception_contex != RT_NULL) {
  97. rt_kprintf("psw: 0x%08x\n", exception_contex->PSW);
  98. rt_kprintf("pc: 0x%08x\n", exception_contex->PC);
  99. rt_kprintf("r0: 0x%08x\n", exception_contex->R1);
  100. rt_kprintf("r0: 0x%08x\n", exception_contex->R2);
  101. rt_kprintf("r0: 0x%08x\n", exception_contex->R3);
  102. rt_kprintf("r0: 0x%08x\n", exception_contex->R4);
  103. rt_kprintf("r0: 0x%08x\n", exception_contex->R5);
  104. rt_kprintf("r0: 0x%08x\n", exception_contex->R6);
  105. rt_kprintf("r0: 0x%08x\n", exception_contex->R7);
  106. rt_kprintf("r0: 0x%08x\n", exception_contex->R8);
  107. rt_kprintf("r0: 0x%08x\n", exception_contex->R9);
  108. rt_kprintf("r0: 0x%08x\n", exception_contex->R10);
  109. rt_kprintf("r0: 0x%08x\n", exception_contex->R11);
  110. rt_kprintf("r0: 0x%08x\n", exception_contex->R12);
  111. rt_kprintf("r0: 0x%08x\n", exception_contex->R13);
  112. rt_kprintf("r0: 0x%08x\n", exception_contex->R14);
  113. rt_kprintf("r0: 0x%08x\n", exception_contex->R15);
  114. rt_kprintf("fpsw: 0x%08x\n", exception_contex->FPSW);
  115. rt_kprintf("acchi: 0x%08x\n", exception_contex->ACCHI);
  116. rt_kprintf("acclo: 0x%08x\n", exception_contex->ACCLO);
  117. }
  118. rt_kprintf("hard fault on thread: %s\n", rt_current_thread->name);
  119. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  120. list_thread();
  121. #endif
  122. while (1);
  123. }
  124. /**
  125. * switch thread in interrupt
  126. *
  127. * @author LXZ (2014/11/8)
  128. *
  129. * @param rt_uint32_t from
  130. * @param rt_uint32_t to
  131. */
  132. void rt_hw_context_switch(rt_uint32_t from, rt_uint32_t to)
  133. {
  134. if (rt_thread_switch_interrupt_flag == 0)
  135. {
  136. rt_thread_switch_interrupt_flag = 1;
  137. rt_interrupt_from_thread = from;
  138. }
  139. rt_interrupt_to_thread = to;
  140. ENTER_INTERRUPT();
  141. }
  142. /**
  143. * switch thread out the interrupt
  144. *
  145. * @author LXZ (2014/11/8)
  146. *
  147. * @param rt_uint32_t from
  148. * @param rt_uint32_t to
  149. */
  150. void rt_hw_context_switch_interrupt(rt_uint32_t from, rt_uint32_t to)
  151. {
  152. if (rt_thread_switch_interrupt_flag == 0)
  153. {
  154. rt_thread_switch_interrupt_flag = 1;
  155. rt_interrupt_from_thread = from;
  156. }
  157. rt_interrupt_to_thread = to;
  158. ENTER_INTERRUPT();
  159. }
  160. /**
  161. * shut down the chip
  162. *
  163. * @author LXZ (2014/11/8)
  164. */
  165. RT_WEAK void rt_hw_cpu_shutdown(void)
  166. {
  167. rt_kprintf("shutdown...\n");
  168. RT_ASSERT(0);
  169. }
  170. /**
  171. * switch to the first thread,it just call one time
  172. *
  173. * @author LXZ (2014/11/8)
  174. *
  175. * @param rt_uint32_t to
  176. */
  177. void rt_hw_context_switch_to(rt_uint32_t to)
  178. {
  179. rt_interrupt_from_thread = 0;
  180. rt_interrupt_to_thread = to;
  181. rt_thread_switch_interrupt_flag = 1;
  182. /* enable interrupt*/
  183. _IEN( _ICU_SWINT ) = 1;
  184. /*clear the interrupt flag*/
  185. _IR( _ICU_SWINT ) = 0;
  186. _IPR( _ICU_SWINT ) = MAX_SYSCALL_INTERRUPT_PRIORITY + 1;
  187. /*touch the software interrupt*/
  188. ENTER_INTERRUPT();
  189. /*wait for first thread start up*/
  190. while(1);
  191. }