1
0

stack.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. * 2013-05-24 Grissiom port to RM48x50
  14. */
  15. #include <rtthread.h>
  16. #include "armv7.h"
  17. /**
  18. * @addtogroup RM48x50
  19. */
  20. /*@{*/
  21. /**
  22. * This function will initialize thread stack
  23. *
  24. * @param tentry the entry of thread
  25. * @param parameter the parameter of entry
  26. * @param stack_addr the beginning stack address
  27. * @param texit the function will be called when thread exit
  28. *
  29. * @return stack address
  30. */
  31. rt_uint8_t *rt_hw_stack_init(void *tentry, void *parameter,
  32. rt_uint8_t *stack_addr, void *texit)
  33. {
  34. rt_uint32_t *stk;
  35. stack_addr += sizeof(rt_uint32_t);
  36. stack_addr = (rt_uint8_t *)RT_ALIGN_DOWN((rt_uint32_t)stack_addr, 8);
  37. stk = (rt_uint32_t *)stack_addr;
  38. *(--stk) = (rt_uint32_t)tentry; /* entry point */
  39. *(--stk) = (rt_uint32_t)texit; /* lr */
  40. *(--stk) = 0xdeadbeef; /* r12 */
  41. *(--stk) = 0xdeadbeef; /* r11 */
  42. *(--stk) = 0xdeadbeef; /* r10 */
  43. *(--stk) = 0xdeadbeef; /* r9 */
  44. *(--stk) = 0xdeadbeef; /* r8 */
  45. *(--stk) = 0xdeadbeef; /* r7 */
  46. *(--stk) = 0xdeadbeef; /* r6 */
  47. *(--stk) = 0xdeadbeef; /* r5 */
  48. *(--stk) = 0xdeadbeef; /* r4 */
  49. *(--stk) = 0xdeadbeef; /* r3 */
  50. *(--stk) = 0xdeadbeef; /* r2 */
  51. *(--stk) = 0xdeadbeef; /* r1 */
  52. *(--stk) = (rt_uint32_t)parameter; /* r0 : argument */
  53. /* cpsr */
  54. if ((rt_uint32_t)tentry & 0x01)
  55. *(--stk) = SVCMODE | 0x20; /* thumb mode */
  56. else
  57. *(--stk) = SVCMODE; /* arm mode */
  58. #if defined(__TI_VFP_SUPPORT__) || (defined (__VFP_FP__) && !defined(__SOFTFP__))
  59. #ifndef RT_VFP_LAZY_STACKING
  60. {
  61. int i;
  62. for (i = 0; i < VFP_DATA_NR; i++)
  63. {
  64. *(--stk) = 0;
  65. }
  66. /* FPSCR TODO: do we need to set the values other than 0? */
  67. *(--stk) = 0;
  68. /* FPEXC. Enable the FVP if no lazy stacking. */
  69. *(--stk) = 0x40000000;
  70. }
  71. #else
  72. /* FPEXC. Disable the FVP by default. */
  73. *(--stk) = 0x00000000;
  74. #endif
  75. #endif
  76. /* return task's current stack address */
  77. return (rt_uint8_t *)stk;
  78. }
  79. /*@}*/