stack.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2021, Shenzhen Academy of Aerospace Technology
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-11-16 Dystopia the first version
  9. */
  10. #include <rtthread.h>
  11. #include <c66xx.h>
  12. extern rt_uint32_t rt_hw_get_current_dp(void);
  13. /**
  14. * @addtogroup C66xx
  15. */
  16. /*@{*/
  17. /**
  18. * This function will initialize thread stack
  19. *
  20. * @param tentry the entry of thread
  21. * @param parameter the parameter of entry
  22. * @param stack_addr the beginning stack address
  23. * @param texit the function will be called when thread exit
  24. *
  25. * @return stack address
  26. */
  27. rt_uint8_t *rt_hw_stack_init(void *tentry, void *parameter,
  28. rt_uint8_t *stack_addr, void *texit)
  29. {
  30. rt_hw_thread_stack_register *thread_context = RT_NULL;
  31. rt_uint32_t stk = 0;
  32. stack_addr += sizeof(rt_uint32_t);
  33. stack_addr = (rt_uint8_t *)RT_ALIGN_DOWN((rt_uint32_t)stack_addr, 8);
  34. stk = (rt_uint32_t)stack_addr;
  35. thread_context = (rt_hw_thread_stack_register *)(stk - sizeof(rt_hw_thread_stack_register));
  36. thread_context->pc = (rt_uint32_t)tentry;
  37. thread_context->csr = 0x0103;
  38. thread_context->tsr = 0x3;
  39. thread_context->orig_a4 = 1;
  40. thread_context->ilc = 0;
  41. thread_context->rilc = 0;
  42. thread_context->hw_register.b17 = 0xB17;
  43. thread_context->hw_register.b16 = 0xB16;
  44. thread_context->hw_register.b19 = 0xB19;
  45. thread_context->hw_register.b18 = 0xB18;
  46. thread_context->hw_register.b21 = 0xB21;
  47. thread_context->hw_register.b20 = 0xB20;
  48. thread_context->hw_register.b23 = 0xB23;
  49. thread_context->hw_register.b22 = 0xB22;
  50. thread_context->hw_register.b25 = 0xB25;
  51. thread_context->hw_register.b24 = 0xB24;
  52. thread_context->hw_register.b27 = 0xB27;
  53. thread_context->hw_register.b26 = 0xB26;
  54. thread_context->hw_register.b29 = 0xB29;
  55. thread_context->hw_register.b28 = 0xB28;
  56. thread_context->hw_register.b31 = 0xB31;
  57. thread_context->hw_register.b30 = 0xB30;
  58. thread_context->hw_register.b1 = 0xB01;
  59. thread_context->hw_register.b0 = 0xB00;
  60. thread_context->hw_register.b3 = (rt_uint32_t)texit;
  61. thread_context->hw_register.b2 = 0xB02;
  62. thread_context->hw_register.b5 = 0xB05;
  63. thread_context->hw_register.b4 = 0xB04;
  64. thread_context->hw_register.b7 = 0xB07;
  65. thread_context->hw_register.b6 = 0xB06;
  66. thread_context->hw_register.b9 = 0xB09;
  67. thread_context->hw_register.b8 = 0xB08;
  68. thread_context->hw_register.b11 = 0xB11;
  69. thread_context->hw_register.b10 = 0xB10;
  70. thread_context->hw_register.b13 = 0xB13;
  71. thread_context->hw_register.b12 = 0xB12;
  72. thread_context->hw_register.a17 = 0xA17;
  73. thread_context->hw_register.a16 = 0xA16;
  74. thread_context->hw_register.a19 = 0xA19;
  75. thread_context->hw_register.a18 = 0xA18;
  76. thread_context->hw_register.a21 = 0xA21;
  77. thread_context->hw_register.a20 = 0xA20;
  78. thread_context->hw_register.a23 = 0xA23;
  79. thread_context->hw_register.a22 = 0xA22;
  80. thread_context->hw_register.a25 = 0xA25;
  81. thread_context->hw_register.a24 = 0xA24;
  82. thread_context->hw_register.a27 = 0xA27;
  83. thread_context->hw_register.a26 = 0xA26;
  84. thread_context->hw_register.a29 = 0xA29;
  85. thread_context->hw_register.a28 = 0xA28;
  86. thread_context->hw_register.a31 = 0xA31;
  87. thread_context->hw_register.a30 = 0xA30;
  88. thread_context->hw_register.a1 = 0xA01;
  89. thread_context->hw_register.a0 = 0xA00;
  90. thread_context->hw_register.a3 = 0xA03;
  91. thread_context->hw_register.a2 = 0xA02;
  92. thread_context->hw_register.a5 = 0xA05;
  93. thread_context->hw_register.a4 = (rt_uint32_t)parameter;
  94. thread_context->hw_register.a7 = 0xA07;
  95. thread_context->hw_register.a6 = 0xA06;
  96. thread_context->hw_register.a9 = 0xA09;
  97. thread_context->hw_register.a8 = 0xA08;
  98. thread_context->hw_register.a11 = 0xA11;
  99. thread_context->hw_register.a10 = 0xA10;
  100. thread_context->hw_register.a13 = 0xA13;
  101. thread_context->hw_register.a12 = 0xA12;
  102. thread_context->hw_register.a15 = 0xA15;
  103. thread_context->hw_register.a14 = 0xA14;
  104. thread_context->hw_register.dp = rt_hw_get_current_dp();
  105. thread_context->hw_register.sp = (rt_uint32_t)stk;
  106. /* return task's current stack address */
  107. return (rt_uint8_t *)thread_context - 8;
  108. }