cpuport.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018/10/28 Bernard The unify RISC-V porting code.
  9. * 2020/11/20 BalanceTWK Add FPU support
  10. * 2023/01/04 WangShun Adapt to CH32
  11. */
  12. #include <rthw.h>
  13. #include <rtthread.h>
  14. #include "cpuport.h"
  15. #include "rt_hw_stack_frame.h"
  16. #ifndef RT_USING_SMP
  17. volatile rt_ubase_t rt_interrupt_from_thread = 0;
  18. volatile rt_ubase_t rt_interrupt_to_thread = 0;
  19. volatile rt_uint32_t rt_thread_switch_interrupt_flag = 0;
  20. #endif
  21. /**
  22. * This function will initialize thread stack
  23. *
  24. * @param tentry the entry of thread
  25. * @param parameter the parameter of entry
  26. * @param stack_addr the beginning stack address
  27. * @param texit the function will be called when thread exit
  28. *
  29. * @return stack address
  30. */
  31. rt_uint8_t *rt_hw_stack_init(void *tentry,
  32. void *parameter,
  33. rt_uint8_t *stack_addr,
  34. void *texit)
  35. {
  36. struct rt_hw_stack_frame *frame;
  37. rt_uint8_t *stk;
  38. int i;
  39. stk = stack_addr + sizeof(rt_ubase_t);
  40. stk = (rt_uint8_t *)RT_ALIGN_DOWN((rt_ubase_t)stk, REGBYTES);
  41. stk -= sizeof(struct rt_hw_stack_frame);
  42. frame = (struct rt_hw_stack_frame *)stk;
  43. for (i = 0; i < sizeof(struct rt_hw_stack_frame) / sizeof(rt_ubase_t); i++)
  44. {
  45. ((rt_ubase_t *)frame)[i] = 0xdeadbeef;
  46. }
  47. frame->ra = (rt_ubase_t)texit;
  48. frame->a0 = (rt_ubase_t)parameter;
  49. frame->epc = (rt_ubase_t)tentry;
  50. /* force to machine mode(MPP=11) and set MPIE to 1 */
  51. #ifdef ARCH_RISCV_FPU
  52. frame->mstatus = 0x7880;
  53. #else
  54. frame->mstatus = 0x1880;
  55. #endif
  56. return stk;
  57. }
  58. rt_weak void rt_trigger_software_interrupt(void)
  59. {
  60. while (0);
  61. }
  62. rt_weak void rt_hw_do_after_save_above(void)
  63. {
  64. while (1);
  65. }
  66. /*
  67. * #ifdef RT_USING_SMP
  68. * void rt_hw_context_switch_interrupt(void *context, rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread);
  69. * #else
  70. * void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to);
  71. * #endif
  72. */
  73. #ifndef RT_USING_SMP
  74. rt_weak void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to, rt_thread_t from_thread, rt_thread_t to_thread)
  75. {
  76. if (rt_thread_switch_interrupt_flag == 0)
  77. rt_interrupt_from_thread = from;
  78. rt_interrupt_to_thread = to;
  79. rt_thread_switch_interrupt_flag = 1;
  80. rt_trigger_software_interrupt();
  81. return ;
  82. }
  83. #endif /* end of RT_USING_SMP */