cpuport.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * File : cpuport.c
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Change Logs:
  18. * Date Author Notes
  19. * 2017-08-25 Archer first version
  20. */
  21. #include <rtthread.h>
  22. #include "nds32.h"
  23. /*
  24. * Initialise the stack of a task to look exactly as if a call to
  25. * SAVE_CONTEXT had been called.
  26. *
  27. * See header file for description.
  28. *
  29. *
  30. * Stack Layout:
  31. * High |-----------------|
  32. * | $R5 |
  33. * |-----------------|
  34. * | . |
  35. * | . |
  36. * |-----------------|
  37. * | $R0 |
  38. * |-----------------|
  39. * | $R30 (LP) |
  40. * |-----------------|
  41. * | $R29 (GP) |
  42. * |-----------------|
  43. * | $R28 (FP) |
  44. * |-----------------|
  45. * | $R15 $R27 |
  46. * |-----------------|
  47. * | $R10 $R26 |
  48. * |-----------------|
  49. * | . |
  50. * | . |
  51. * |-----------------|
  52. * | $R6 |
  53. * |-----------------|
  54. * | $IFC_LP | (Option)
  55. * |-----------------|
  56. * | $LC/$LE/$LB | (Option)
  57. * | (ZOL) |
  58. * |-----------------|
  59. * | $IPSW |
  60. * |-----------------|
  61. * | $IPC |
  62. * |-----------------|
  63. * | Dummy word | (Option, only exist when IFC & ZOL both configured)
  64. * |-----------------|
  65. * | $FPU | (Option)
  66. * |-----------------|
  67. * Low
  68. *
  69. */
  70. struct stack_frame
  71. {
  72. rt_uint32_t topOfStack[34];
  73. };
  74. /* flag in interrupt handling */
  75. rt_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread;
  76. rt_uint32_t rt_thread_switch_interrupt_flag;
  77. /* exception hook */
  78. static rt_err_t (*rt_exception_hook)(void *context) = RT_NULL;
  79. rt_base_t rt_hw_interrupt_disable(void)
  80. {
  81. rt_base_t level = __nds32__mfsr(NDS32_SR_PSW);
  82. GIE_DISABLE();
  83. return level;
  84. }
  85. void rt_hw_interrupt_enable(rt_base_t level)
  86. {
  87. if (level & PSW_mskGIE)
  88. GIE_ENABLE();
  89. }
  90. /* For relax support, must initial $gp at task init*/
  91. extern uint32_t _SDA_BASE_ __attribute__ ((weak));
  92. /**************************************************************
  93. * This function will initialize thread stack
  94. *
  95. * @param tentry the entry of thread
  96. * @param parameter the parameter of entry
  97. * @param stack_addr the beginning stack address
  98. * @param texit the function will be called when thread exit
  99. *
  100. * @return stack address
  101. **************************************************************/
  102. rt_uint8_t *rt_hw_stack_init(void *tentry,
  103. void *parameter,
  104. rt_uint8_t *stack_addr,
  105. void *texit)
  106. {
  107. rt_int32_t i;
  108. rt_uint32_t *pxTopOfStack;
  109. pxTopOfStack = (rt_uint32_t *)RT_ALIGN_DOWN((rt_uint32_t)stack_addr, 4);
  110. /* Simulate the stack frame as it would be created by a context switch */
  111. /* R0 ~ R5 registers */
  112. for (i = 5; i >= 1; i--) /* R5, R4, R3, R2 and R1. */
  113. *--pxTopOfStack = (rt_uint32_t)0x01010101UL * i;
  114. *--pxTopOfStack = (rt_uint32_t)parameter; /* R0 : Argument */
  115. /* R6 ~ R30 registers */
  116. *--pxTopOfStack = (rt_uint32_t)texit; /* R30: $LP */
  117. *--pxTopOfStack = (rt_uint32_t)&_SDA_BASE_; /* R29: $GP */
  118. *--pxTopOfStack = (rt_uint32_t)0x2828282828; /* R28: $FP */
  119. #ifdef __NDS32_REDUCE_REGS__
  120. *--pxTopOfStack = (rt_uint32_t)0x1515151515; /* R15 */
  121. for (i = 10; i >= 6; i--) /* R10 ~ R6 */
  122. *--pxTopOfStack = (rt_uint32_t)0x01010101UL * i;
  123. #else
  124. for (i = 27; i >= 6; i--) /* R27 ~ R6 */
  125. *--pxTopOfStack = (rt_uint32_t)0x01010101UL * i;
  126. #endif
  127. /* IFC system register */
  128. #ifdef __TARGET_IFC_EXT
  129. *--pxTopOfStack = (rt_uint32_t)0x0; /* $IFC_LP */
  130. #endif
  131. /* ZOL system registers */
  132. #ifdef __TARGET_ZOL_EXT
  133. *--pxTopOfStack = (rt_uint32_t)0x0; /* $LC */
  134. *--pxTopOfStack = (rt_uint32_t)0x0; /* $LE */
  135. *--pxTopOfStack = (rt_uint32_t)0x0; /* $LB */
  136. #endif
  137. /* IPSW and IPC system registers */
  138. /* Default IPSW: enable GIE, set CPL to 7, clear IFCON */
  139. i = (__nds32__mfsr(NDS32_SR_PSW) | PSW_mskGIE | PSW_mskCPL) & ~PSW_mskIFCON;
  140. *--pxTopOfStack = (rt_uint32_t)i; /* $IPSW */
  141. *--pxTopOfStack = (rt_uint32_t)tentry; /* $IPC */
  142. /* Dummy word for 8-byte stack alignment */
  143. #if defined(__TARGET_IFC_EXT) && defined(__TARGET_ZOL_EXT)
  144. *--pxTopOfStack = (rt_uint32_t)0xFFFFFFFF; /* Dummy */
  145. #endif
  146. /* FPU registers */
  147. #ifdef __TARGET_FPU_EXT
  148. for (i = 0; i < FPU_REGS; i++)
  149. *--pxTopOfStack = (rt_uint32_t)0x0; /* FPU */
  150. #endif
  151. return (rt_uint8_t *)pxTopOfStack;
  152. }
  153. /**
  154. * This function set the hook, which is invoked on fault exception handling.
  155. *
  156. * @param exception_handle the exception handling hook function.
  157. */
  158. void rt_hw_exception_install(rt_err_t (*exception_handle)(void* context))
  159. {
  160. rt_exception_hook = exception_handle;
  161. }
  162. #ifdef RT_USING_CPU_FFS
  163. /**
  164. * This function finds the first bit set (beginning with the least significant bit)
  165. * in value and return the index of that bit.
  166. *
  167. * Bits are numbered starting at 1 (the least significant bit). A return value of
  168. * zero from any of these functions means that the argument was zero.
  169. *
  170. * @return return the index of the first bit set. If value is 0, then this function
  171. * shall return 0.
  172. */
  173. #if defined(__GNUC__)
  174. int __rt_ffs(int value)
  175. {
  176. return __builtin_ffs(value);
  177. }
  178. #endif
  179. #endif