cpuport.c 4.9 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. * 2018-09-01 xuzhuoyi the first version.
  9. * 2019-07-03 zhaoxiaowei add support for __rt_ffs.
  10. * 2019-12-05 xiaolifan add support for hardware fpu32
  11. * 2022-10-17 guyunjie add support for hardware fpu64 and vcrc
  12. */
  13. #include <rthw.h>
  14. #define DBG_TAG "kernel.irq"
  15. #ifdef RT_DEBUG_IRQ
  16. #define DBG_LVL DBG_LOG
  17. #else
  18. #define DBG_LVL DBG_WARNING
  19. #endif /* defined (RT_DEBUG_IRQ) */
  20. #include <rtdbg.h>
  21. extern volatile rt_uint8_t rt_interrupt_nest;
  22. /* exception and interrupt handler table */
  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. /* exception hook */
  27. static rt_err_t (*rt_exception_hook)(void *context) = RT_NULL;
  28. extern rt_uint16_t rt_hw_get_st0(void);
  29. extern rt_uint16_t rt_hw_get_st1(void);
  30. extern int rt_hw_calc_csb(int value);
  31. struct exception_stack_frame
  32. {
  33. rt_uint32_t t_st0;
  34. rt_uint32_t acc;
  35. rt_uint32_t p;
  36. rt_uint32_t ar1_ar0;
  37. rt_uint32_t dp_st1;
  38. rt_uint32_t dbgstat_ier;
  39. rt_uint32_t return_address;
  40. };
  41. struct stack_frame
  42. {
  43. struct exception_stack_frame exception_stack_frame;
  44. /* r4 ~ r11 register */
  45. rt_uint16_t ar0h;
  46. rt_uint16_t ar1h;
  47. rt_uint32_t xar2;
  48. rt_uint32_t xar3;
  49. rt_uint32_t xar4;
  50. rt_uint32_t xar5;
  51. rt_uint32_t xar6;
  52. rt_uint32_t xar7;
  53. rt_uint32_t xt;
  54. rt_uint32_t rpc;
  55. #ifdef __TMS320C28XX_FPU32__
  56. rt_uint32_t rb;
  57. rt_uint32_t stf;
  58. rt_uint32_t r0h;
  59. rt_uint32_t r1h;
  60. rt_uint32_t r2h;
  61. rt_uint32_t r3h;
  62. rt_uint32_t r4h;
  63. rt_uint32_t r5h;
  64. rt_uint32_t r6h;
  65. rt_uint32_t r7h;
  66. #endif
  67. #ifdef __TMS320C28XX_FPU64__
  68. rt_uint32_t r0l;
  69. rt_uint32_t r1l;
  70. rt_uint32_t r2l;
  71. rt_uint32_t r3l;
  72. rt_uint32_t r4l;
  73. rt_uint32_t r5l;
  74. rt_uint32_t r6l;
  75. rt_uint32_t r7l;
  76. #endif
  77. #ifdef __TMS320C28XX_VCRC__
  78. rt_uint32_t vcrc;
  79. rt_uint32_t vstatus;
  80. rt_uint32_t vcrcpoly;
  81. rt_uint32_t vcrcsize;
  82. #endif
  83. };
  84. rt_uint8_t *rt_hw_stack_init(void *tentry,
  85. void *parameter,
  86. rt_uint8_t *stack_addr,
  87. void *texit)
  88. {
  89. struct stack_frame *stack_frame;
  90. rt_uint8_t *stk;
  91. unsigned long i;
  92. stk = stack_addr;
  93. stk = (rt_uint8_t *)RT_ALIGN((rt_uint32_t)stk, 2);
  94. stk += 1; /*to work around the stack alignment*/
  95. stack_frame = (struct stack_frame *)stk;
  96. /* zero all registers */
  97. for (i = 0; i < sizeof(struct stack_frame) / sizeof(rt_uint32_t); i ++)
  98. {
  99. ((rt_uint32_t *)stack_frame)[i] = 0;
  100. }
  101. /* configure special registers*/
  102. stack_frame->exception_stack_frame.dp_st1 = 0x00000A08;
  103. stack_frame->xar4 = (rt_uint32_t)parameter;
  104. stack_frame->exception_stack_frame.return_address = (rt_uint32_t)tentry;
  105. stack_frame->rpc = (rt_uint32_t)texit;
  106. #ifdef __TMS320C28XX_FPU32__
  107. stack_frame->stf = 0x00000200;
  108. stack_frame->rb = 0;
  109. #endif
  110. /* return task's current stack address */
  111. return stk + sizeof(struct stack_frame);
  112. }
  113. /**
  114. * This function set the hook, which is invoked on fault exception handling.
  115. *
  116. * @param exception_handle the exception handling hook function.
  117. */
  118. void rt_hw_exception_install(rt_err_t (*exception_handle)(void *context))
  119. {
  120. rt_exception_hook = exception_handle;
  121. }
  122. struct exception_info
  123. {
  124. rt_uint32_t exc_return;
  125. struct stack_frame stack_frame;
  126. };
  127. #ifdef RT_USING_CPU_FFS
  128. /*
  129. * This function called rt_hw_calc_csb to finds the first bit set in value.
  130. * rt_hw_calc_csb is a native assembly program that use "CSB" instruction in C28x.
  131. * When you use this function, remember that "int" is only 16-bit in C28x's C compiler.
  132. * If value is a number bigger that 0xFFFF, trouble may be caused.
  133. * Maybe change "int __rt_ffs(int value)" to "rt_int32_t __rt_ffs(rt_int32_t value)" will be better.
  134. */
  135. int __rt_ffs(int value)
  136. {
  137. return rt_hw_calc_csb(value);
  138. }
  139. #endif
  140. /**
  141. * shutdown CPU
  142. */
  143. rt_weak void rt_hw_cpu_shutdown(void)
  144. {
  145. rt_kprintf("shutdown...\n");
  146. RT_ASSERT(0);
  147. }
  148. void rt_interrupt_enter(void)
  149. {
  150. rt_base_t level;
  151. __asm(" EINT");
  152. level = rt_hw_interrupt_disable();
  153. rt_interrupt_nest ++;
  154. RT_OBJECT_HOOK_CALL(rt_interrupt_enter_hook,());
  155. rt_hw_interrupt_enable(level);
  156. LOG_D("irq has come..., irq current nest:%d",
  157. (rt_int32_t)rt_interrupt_nest);
  158. }
  159. void rt_interrupt_leave(void)
  160. {
  161. LOG_D("irq is going to leave, irq current nest:%d",
  162. (rt_int32_t)rt_interrupt_nest);
  163. rt_hw_interrupt_disable();
  164. RT_OBJECT_HOOK_CALL(rt_interrupt_leave_hook,());
  165. rt_interrupt_nest --;
  166. if(rt_thread_switch_interrupt_flag && !rt_interrupt_nest)
  167. {
  168. __asm(" OR IFR, #0x8000"); /* trigger rtos int */
  169. }
  170. /* rt_hw_interrupt_enable auto done by hardware on IRET */
  171. }