cpuport.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. #include <rt-thread/libcpu/risc-v/hpmicro/cpuport.h>
  3. #include <rt-thread/libcpu/risc-v/hpmicro/riscv-stackframe.h>
  4. * Copyright (c) 2006-2021, RT-Thread Development Team
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. *
  8. * Change Logs:
  9. * Date Author Notes
  10. * 2018/10/28 Bernard The unify RISC-V porting code.
  11. * 2020/11/20 BalanceTWK Add FPU support
  12. */
  13. #include <rthw.h>
  14. #include <rtthread.h>
  15. #include "riscv-stackframe.h"
  16. volatile rt_ubase_t rt_interrupt_from_thread = 0;
  17. volatile rt_ubase_t rt_interrupt_to_thread = 0;
  18. volatile rt_uint32_t rt_thread_switch_interrupt_flag = 0;
  19. /**
  20. * This function will initialize thread stack
  21. *
  22. * @param tentry the entry of thread
  23. * @param parameter the parameter of entry
  24. * @param stack_addr the beginning stack address
  25. * @param texit the function will be called when thread exit
  26. *
  27. * @return stack address
  28. */
  29. rt_uint8_t *rt_hw_stack_init(void *tentry,
  30. void *parameter,
  31. rt_uint8_t *stack_addr,
  32. void *texit)
  33. {
  34. struct rt_hw_stack_frame *frame;
  35. rt_uint8_t *stk;
  36. int i;
  37. stk = stack_addr + sizeof(rt_ubase_t);
  38. stk = (rt_uint8_t *)RT_ALIGN_DOWN((rt_ubase_t)stk, REGBYTES);
  39. stk -= sizeof(struct rt_hw_stack_frame);
  40. frame = (struct rt_hw_stack_frame *)stk;
  41. for (i = 0; i < sizeof(struct rt_hw_stack_frame) / sizeof(rt_ubase_t); i++)
  42. {
  43. ((rt_ubase_t *)frame)[i] = 0xdeadbeef;
  44. }
  45. frame->ra = (rt_ubase_t)texit;
  46. frame->a0 = (rt_ubase_t)parameter;
  47. frame->epc = (rt_ubase_t)tentry;
  48. /* force to machine mode(MPP=11) and set MPIE to 1 */
  49. frame->mstatus = 0x00007880;
  50. return stk;
  51. }
  52. /*
  53. * void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to);
  54. */
  55. void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to)
  56. {
  57. if (rt_thread_switch_interrupt_flag == 0)
  58. rt_interrupt_from_thread = from;
  59. rt_interrupt_to_thread = to;
  60. rt_thread_switch_interrupt_flag = 1;
  61. return ;
  62. }
  63. /** shutdown CPU */
  64. RT_WEAK void rt_hw_cpu_shutdown()
  65. {
  66. rt_uint32_t level;
  67. rt_kprintf("shutdown...\n");
  68. level = rt_hw_interrupt_disable();
  69. while (level)
  70. {
  71. RT_ASSERT(0);
  72. }
  73. }