cpuport.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. * Copyright (c) 2019-Present Nuclei Limited. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020/03/26 Huaqi Nuclei RISC-V Core porting code.
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include <stdio.h>
  14. #include "cpuport.h"
  15. #define SYSTICK_TICK_CONST (SOC_TIMER_FREQ / RT_TICK_PER_SECOND)
  16. #ifndef configKERNEL_INTERRUPT_PRIORITY
  17. #define configKERNEL_INTERRUPT_PRIORITY 0
  18. #endif
  19. #ifndef configMAX_SYSCALL_INTERRUPT_PRIORITY
  20. // See function prvCheckMaxSysCallPrio and prvCalcMaxSysCallMTH
  21. #define configMAX_SYSCALL_INTERRUPT_PRIORITY 255
  22. #endif
  23. #define portINITIAL_MSTATUS ( MSTATUS_MPP | MSTATUS_MPIE | MSTATUS_FS_INITIAL | MSTATUS_VS_INITIAL)
  24. volatile rt_ubase_t rt_interrupt_from_thread = 0;
  25. volatile rt_ubase_t rt_interrupt_to_thread = 0;
  26. volatile rt_ubase_t rt_thread_switch_interrupt_flag = 0;
  27. struct rt_hw_stack_frame {
  28. rt_ubase_t epc; /* epc - epc - program counter */
  29. rt_ubase_t ra; /* x1 - ra - return address for jumps */
  30. rt_ubase_t t0; /* x5 - t0 - temporary register 0 */
  31. rt_ubase_t t1; /* x6 - t1 - temporary register 1 */
  32. rt_ubase_t t2; /* x7 - t2 - temporary register 2 */
  33. rt_ubase_t s0_fp; /* x8 - s0/fp - saved register 0 or frame pointer */
  34. rt_ubase_t s1; /* x9 - s1 - saved register 1 */
  35. rt_ubase_t a0; /* x10 - a0 - return value or function argument 0 */
  36. rt_ubase_t a1; /* x11 - a1 - return value or function argument 1 */
  37. rt_ubase_t a2; /* x12 - a2 - function argument 2 */
  38. rt_ubase_t a3; /* x13 - a3 - function argument 3 */
  39. rt_ubase_t a4; /* x14 - a4 - function argument 4 */
  40. rt_ubase_t a5; /* x15 - a5 - function argument 5 */
  41. #ifndef __riscv_32e
  42. rt_ubase_t a6; /* x16 - a6 - function argument 6 */
  43. rt_ubase_t a7; /* x17 - s7 - function argument 7 */
  44. rt_ubase_t s2; /* x18 - s2 - saved register 2 */
  45. rt_ubase_t s3; /* x19 - s3 - saved register 3 */
  46. rt_ubase_t s4; /* x20 - s4 - saved register 4 */
  47. rt_ubase_t s5; /* x21 - s5 - saved register 5 */
  48. rt_ubase_t s6; /* x22 - s6 - saved register 6 */
  49. rt_ubase_t s7; /* x23 - s7 - saved register 7 */
  50. rt_ubase_t s8; /* x24 - s8 - saved register 8 */
  51. rt_ubase_t s9; /* x25 - s9 - saved register 9 */
  52. rt_ubase_t s10; /* x26 - s10 - saved register 10 */
  53. rt_ubase_t s11; /* x27 - s11 - saved register 11 */
  54. rt_ubase_t t3; /* x28 - t3 - temporary register 3 */
  55. rt_ubase_t t4; /* x29 - t4 - temporary register 4 */
  56. rt_ubase_t t5; /* x30 - t5 - temporary register 5 */
  57. rt_ubase_t t6; /* x31 - t6 - temporary register 6 */
  58. #endif
  59. rt_ubase_t mstatus; /* - machine status register */
  60. };
  61. /**
  62. * This function will initialize thread stack
  63. *
  64. * @param tentry the entry of thread
  65. * @param parameter the parameter of entry
  66. * @param stack_addr the beginning stack address
  67. * @param texit the function will be called when thread exit
  68. *
  69. * @return stack address
  70. */
  71. rt_uint8_t* rt_hw_stack_init(void* tentry,
  72. void* parameter,
  73. rt_uint8_t* stack_addr,
  74. void* texit)
  75. {
  76. struct rt_hw_stack_frame* frame;
  77. rt_uint8_t* stk;
  78. int i;
  79. stk = stack_addr + sizeof(rt_ubase_t);
  80. stk = (rt_uint8_t*)RT_ALIGN_DOWN((rt_ubase_t)stk, REGBYTES);
  81. stk -= sizeof(struct rt_hw_stack_frame);
  82. frame = (struct rt_hw_stack_frame*)stk;
  83. for (i = 0; i < sizeof(struct rt_hw_stack_frame) / sizeof(rt_ubase_t); i++) {
  84. ((rt_ubase_t*)frame)[i] = 0xdeadbeef;
  85. }
  86. frame->ra = (rt_ubase_t)texit;
  87. frame->a0 = (rt_ubase_t)parameter;
  88. frame->epc = (rt_ubase_t)tentry;
  89. frame->mstatus = portINITIAL_MSTATUS;
  90. return stk;
  91. }
  92. /*
  93. * void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to);
  94. */
  95. void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to)
  96. {
  97. if (rt_thread_switch_interrupt_flag == 0) {
  98. rt_interrupt_from_thread = from;
  99. }
  100. rt_interrupt_to_thread = to;
  101. rt_thread_switch_interrupt_flag = 1;
  102. portYIELD();
  103. }
  104. void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to)
  105. {
  106. rt_hw_context_switch_interrupt(from, to);
  107. }
  108. /** shutdown CPU */
  109. void rt_hw_cpu_shutdown()
  110. {
  111. rt_uint32_t level;
  112. rt_kprintf("shutdown...\n");
  113. level = rt_hw_interrupt_disable();
  114. while (level) {
  115. RT_ASSERT(0);
  116. }
  117. }
  118. void xPortTaskSwitch(void)
  119. {
  120. /* Clear Software IRQ, A MUST */
  121. SysTimer_ClearSWIRQ();
  122. rt_thread_switch_interrupt_flag = 0;
  123. // make from thread to be to thread
  124. // If there is another swi interrupt triggered by other harts
  125. // not through rt_hw_context_switch or rt_hw_context_switch_interrupt
  126. // the task switch should just do a same task save and restore
  127. rt_interrupt_from_thread = rt_interrupt_to_thread;
  128. }
  129. void vPortSetupTimerInterrupt(void)
  130. {
  131. uint64_t ticks = SYSTICK_TICK_CONST;
  132. /* Make SWI and SysTick the lowest priority interrupts. */
  133. /* Stop and clear the SysTimer. SysTimer as Non-Vector Interrupt */
  134. SysTick_Config(ticks);
  135. ECLIC_DisableIRQ(SysTimer_IRQn);
  136. ECLIC_SetLevelIRQ(SysTimer_IRQn, configKERNEL_INTERRUPT_PRIORITY);
  137. ECLIC_SetShvIRQ(SysTimer_IRQn, ECLIC_NON_VECTOR_INTERRUPT);
  138. ECLIC_EnableIRQ(SysTimer_IRQn);
  139. /* Set SWI interrupt level to lowest level/priority, SysTimerSW as Vector Interrupt */
  140. ECLIC_SetShvIRQ(SysTimerSW_IRQn, ECLIC_VECTOR_INTERRUPT);
  141. ECLIC_SetLevelIRQ(SysTimerSW_IRQn, configKERNEL_INTERRUPT_PRIORITY);
  142. ECLIC_EnableIRQ(SysTimerSW_IRQn);
  143. }
  144. #if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
  145. #ifndef RT_HEAP_SIZE
  146. #warning RT_HEAP_SIZE is not defined in rtconfig.h, using default 2048
  147. #define RT_HEAP_SIZE 2048
  148. #endif
  149. static uint32_t rt_heap[RT_HEAP_SIZE]; // heap default size: 4K(1024 * 4)
  150. RT_WEAK void* rt_heap_begin_get(void)
  151. {
  152. return rt_heap;
  153. }
  154. RT_WEAK void* rt_heap_end_get(void)
  155. {
  156. return rt_heap + RT_HEAP_SIZE;
  157. }
  158. #endif
  159. /**
  160. * This function will initial your board.
  161. */
  162. void rt_hw_board_init()
  163. {
  164. /* OS Tick Configuration */
  165. vPortSetupTimerInterrupt();
  166. /* Call components board initial (use INIT_BOARD_EXPORT()) */
  167. #ifdef RT_USING_COMPONENTS_INIT
  168. rt_components_board_init();
  169. #endif
  170. #if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
  171. rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
  172. #endif
  173. __disable_irq();
  174. }
  175. #define SysTick_Handler eclic_mtip_handler
  176. /* This is the timer interrupt service routine. */
  177. void SysTick_Handler(void)
  178. {
  179. // Reload timer
  180. SysTick_Reload(SYSTICK_TICK_CONST);
  181. /* enter interrupt */
  182. rt_interrupt_enter();
  183. /* tick increase */
  184. rt_tick_increase();
  185. /* leave interrupt */
  186. rt_interrupt_leave();
  187. }
  188. void rt_hw_console_output(const char* str)
  189. {
  190. rt_size_t size = 0;
  191. size = rt_strlen(str);
  192. for (int i = 0; i < size; i ++) {
  193. putchar(str[i]);
  194. }
  195. }
  196. char rt_hw_console_getchar(void)
  197. {
  198. char ch = -1;
  199. ch = (char)getchar();
  200. return ch;
  201. }
  202. rt_base_t rt_hw_interrupt_disable(void)
  203. {
  204. return __RV_CSR_READ_CLEAR(CSR_MSTATUS, MSTATUS_MIE);
  205. }
  206. void rt_hw_interrupt_enable(rt_base_t level)
  207. {
  208. __RV_CSR_WRITE(CSR_MSTATUS, level);
  209. }