stack.c 2.1 KB

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