cpuport.c 5.2 KB

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