stack.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * File : stack.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2013, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2011-09-23 Bernard the first version
  13. * 2011-10-05 Bernard add thumb mode
  14. * 2013-07-15 Bernard add Cortex-A8 support.
  15. */
  16. #include <rtthread.h>
  17. #include "zynq7000.h"
  18. /**
  19. * This function will initialize thread stack
  20. *
  21. * @param tentry the entry of thread
  22. * @param parameter the parameter of entry
  23. * @param stack_addr the beginning stack address
  24. * @param texit the function will be called when thread exit
  25. *
  26. * @return stack address
  27. */
  28. rt_uint8_t *rt_hw_stack_init(void *tentry, void *parameter,
  29. rt_uint8_t *stack_addr, void *texit)
  30. {
  31. rt_uint32_t *stk;
  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. *(--stk) = (rt_uint32_t)tentry; /* entry point */
  36. *(--stk) = (rt_uint32_t)texit; /* lr */
  37. *(--stk) = 0xdeadbeef; /* r12 */
  38. *(--stk) = 0xdeadbeef; /* r11 */
  39. *(--stk) = 0xdeadbeef; /* r10 */
  40. *(--stk) = 0xdeadbeef; /* r9 */
  41. *(--stk) = 0xdeadbeef; /* r8 */
  42. *(--stk) = 0xdeadbeef; /* r7 */
  43. *(--stk) = 0xdeadbeef; /* r6 */
  44. *(--stk) = 0xdeadbeef; /* r5 */
  45. *(--stk) = 0xdeadbeef; /* r4 */
  46. *(--stk) = 0xdeadbeef; /* r3 */
  47. *(--stk) = 0xdeadbeef; /* r2 */
  48. *(--stk) = 0xdeadbeef; /* r1 */
  49. *(--stk) = (rt_uint32_t)parameter; /* r0 : argument */
  50. /* cpsr */
  51. if ((rt_uint32_t)tentry & 0x01)
  52. *(--stk) = SVCMODE | 0x20; /* thumb mode */
  53. else
  54. *(--stk) = SVCMODE; /* arm mode */
  55. /* return task's current stack address */
  56. return (rt_uint8_t *)stk;
  57. }