cpuport.c 4.8 KB

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