stack.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * File : stack.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009, 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. * 2010-04-09 fify the first version
  13. *
  14. * For : Renesas M16C
  15. * Toolchain : IAR's EW for M16C v3.401
  16. */
  17. #include <rtthread.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_uint16_t *pstk16;
  32. rt_uint16_t flag;
  33. flag = 0x0040;
  34. pstk16 = (rt_uint16_t *)stack_addr;
  35. pstk16--;
  36. /* Simulate ISR entry */
  37. *pstk16-- = (flag & 0x00FF) /* ... The lowest byte of the FLAG register */
  38. | (((rt_uint32_t)tentry >> 8) & 0x00000F00) /* ... The highest nibble of the PC register */
  39. | ((flag << 4) & 0xF000); /* ... The highest nibble of the FLAG register */
  40. *pstk16-- = (((rt_uint32_t)tentry ) & 0x0000FFFF); /* ... The lowest bytes of the PC register */
  41. /* Save registers onto stack frame */
  42. *pstk16-- = (rt_uint16_t)0xFBFB; /* ... FB register */
  43. *pstk16-- = (rt_uint16_t)0x3B3B; /* ... SB register */
  44. *pstk16-- = (rt_uint16_t)0xA1A1; /* ... A1 register */
  45. *pstk16-- = (rt_uint16_t)0xA0A0; /* ... A0 register */
  46. *pstk16-- = (rt_uint16_t)0x3333; /* ... R3 register */
  47. *pstk16-- = (rt_uint32_t)parameter >> 16L; /* ... Pass argument in R2 register */
  48. *pstk16-- = (rt_uint32_t)parameter & 0x0000FFFFL; /* ... Pass argument in R1 register */
  49. *pstk16 = (rt_uint16_t)0x0000; /* ... R0 register */
  50. /* return task's current stack address */
  51. return (rt_uint8_t *)pstk16;
  52. }