1
0

stack.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2020, Shenzhen Academy of Aerospace Technology
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-10-16 Dystopia the first version
  9. */
  10. #include <rtthread.h>
  11. /**
  12. * This function will initialize thread stack
  13. *
  14. * @param tentry the entry of thread
  15. * @param parameter the parameter of entry
  16. * @param stack_addr the beginning stack address
  17. * @param texit the function will be called when thread exit
  18. *
  19. * @return stack address
  20. */
  21. rt_uint8_t *rt_hw_stack_init(void *tentry, void *parameter,
  22. rt_uint8_t *stack_addr, void *texit)
  23. {
  24. rt_uint32_t *stk;
  25. int window_index;
  26. int register_index;
  27. stack_addr += sizeof(rt_uint32_t);
  28. stack_addr = (rt_uint8_t *)RT_ALIGN_DOWN((rt_uint32_t)stack_addr, 8);
  29. stk = (rt_uint32_t *)stack_addr;
  30. stk -= 24;
  31. stk -= 8;
  32. for (register_index = 0; register_index != 8; register_index++)
  33. stk[register_index] = 0xdeadbeef;
  34. for (window_index = 0; window_index != 8; window_index++)
  35. {
  36. stk -= 16;
  37. for (register_index = 0; register_index != 16; register_index++)
  38. stk[register_index] = 0xdeadbeef;
  39. if (window_index == 0)
  40. {
  41. stk[8] = (rt_uint32_t)parameter;
  42. stk[15] = (rt_uint32_t)texit - 8;
  43. }
  44. }
  45. stk -= 34;
  46. for (register_index = 0; register_index != 34; register_index++)
  47. stk[register_index] = 0;
  48. stk -= 4;
  49. stk[0] = (rt_uint32_t)tentry; //pc
  50. stk[1] = (rt_uint32_t)tentry + 4; //npc
  51. stk[2] = 0x10C7; //psr
  52. stk[3] = 0x2; //wim
  53. /* return task's current stack address */
  54. return (rt_uint8_t *)stk;
  55. }