cpuport.c 4.7 KB

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