1
0

stack.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * File : stack.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2011, 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. */
  15. #include <rtthread.h>
  16. #include <board.h>
  17. #include <armv7.h>
  18. /**
  19. * @addtogroup AM33xx
  20. */
  21. /*@{*/
  22. /**
  23. * This function will initialize thread stack
  24. *
  25. * @param tentry the entry of thread
  26. * @param parameter the parameter of entry
  27. * @param stack_addr the beginning stack address
  28. * @param texit the function will be called when thread exit
  29. *
  30. * @return stack address
  31. */
  32. rt_uint8_t *rt_hw_stack_init(void *tentry, void *parameter,
  33. rt_uint8_t *stack_addr, void *texit)
  34. {
  35. rt_uint32_t *stk;
  36. stack_addr += sizeof(rt_uint32_t);
  37. stack_addr = (rt_uint8_t *)RT_ALIGN_DOWN((rt_uint32_t)stack_addr, 8);
  38. stk = (rt_uint32_t *)stack_addr;
  39. *(--stk) = (rt_uint32_t)tentry; /* entry point */
  40. *(--stk) = (rt_uint32_t)texit; /* lr */
  41. *(--stk) = 0xdeadbeef; /* r12 */
  42. *(--stk) = 0xdeadbeef; /* r11 */
  43. *(--stk) = 0xdeadbeef; /* r10 */
  44. *(--stk) = 0xdeadbeef; /* r9 */
  45. *(--stk) = 0xdeadbeef; /* r8 */
  46. *(--stk) = 0xdeadbeef; /* r7 */
  47. *(--stk) = 0xdeadbeef; /* r6 */
  48. *(--stk) = 0xdeadbeef; /* r5 */
  49. *(--stk) = 0xdeadbeef; /* r4 */
  50. *(--stk) = 0xdeadbeef; /* r3 */
  51. *(--stk) = 0xdeadbeef; /* r2 */
  52. *(--stk) = 0xdeadbeef; /* r1 */
  53. *(--stk) = (rt_uint32_t)parameter; /* r0 : argument */
  54. /* cpsr */
  55. if ((rt_uint32_t)tentry & 0x01)
  56. *(--stk) = SVCMODE | 0x20; /* thumb mode */
  57. else
  58. *(--stk) = SVCMODE; /* arm mode */
  59. /* return task's current stack address */
  60. return (rt_uint8_t *)stk;
  61. }
  62. /*@}*/