cpuport.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * File : cpuport.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009 - 2012, 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-02-23 Bernard the first version
  13. * 2012-09-23 lgnq set the texit to R31
  14. */
  15. #include <rtthread.h>
  16. extern volatile rt_uint8_t rt_interrupt_nest;
  17. /* switch flag on interrupt and thread pointer to save switch record */
  18. rt_uint32_t rt_interrupt_from_thread;
  19. rt_uint32_t rt_interrupt_to_thread;
  20. rt_uint32_t rt_thread_switch_interrupt_flag;
  21. /**
  22. * This function will initialize hardware interrupt
  23. */
  24. void rt_hw_interrupt_init(void)
  25. {
  26. /* init interrupt nest, and context in thread sp */
  27. rt_interrupt_nest = 0;
  28. rt_interrupt_from_thread = 0;
  29. rt_interrupt_to_thread = 0;
  30. rt_thread_switch_interrupt_flag = 0;
  31. }
  32. /**
  33. * This function will initialize thread stack
  34. *
  35. * @param tentry the entry of thread
  36. * @param parameter the parameter of entry
  37. * @param stack_addr the beginning stack address
  38. * @param texit the function will be called when thread exit
  39. *
  40. * @return stack address
  41. */
  42. rt_uint8_t *rt_hw_stack_init(void *tentry,
  43. void *parameter,
  44. rt_uint8_t *stack_addr,
  45. void *texit)
  46. {
  47. rt_uint32_t *stk;
  48. stk = (rt_uint32_t *)stack_addr; /* Load stack pointer */
  49. *(--stk) = (rt_uint32_t)0x23232323; /* r23 */
  50. *(--stk) = (rt_uint32_t)0x24242424; /* r24 */
  51. *(--stk) = (rt_uint32_t)0x25252525; /* r25 */
  52. *(--stk) = (rt_uint32_t)0x26262626; /* r26 */
  53. *(--stk) = (rt_uint32_t)0x27272727; /* r27 */
  54. *(--stk) = (rt_uint32_t)0x28282828; /* r28 */
  55. *(--stk) = (rt_uint32_t)0x29292929; /* r29 */
  56. *(--stk) = (rt_uint32_t)0x30303030; /* r30 */
  57. *(--stk) = (rt_uint32_t)texit; /* r31 */
  58. *(--stk) = (rt_uint32_t)0x00000000; /* Task PSW = Interrupts enabled */
  59. *(--stk) = (rt_uint32_t)tentry; /* Task's PC */
  60. *(--stk) = (rt_uint32_t)0x16161616; /* r16 */
  61. *(--stk) = (rt_uint32_t)0x15151515; /* r15 */
  62. *(--stk) = (rt_uint32_t)0x14141414; /* r14 */
  63. *(--stk) = (rt_uint32_t)0x13131313; /* r13 */
  64. *(--stk) = (rt_uint32_t)0x12121212; /* r12 */
  65. *(--stk) = (rt_uint32_t)0x11111111; /* r11 */
  66. *(--stk) = (rt_uint32_t)0x10101010; /* r10 */
  67. *(--stk) = (rt_uint32_t)0x09090909; /* r9 */
  68. *(--stk) = (rt_uint32_t)0x08080808; /* r8 */
  69. *(--stk) = (rt_uint32_t)0x07070707; /* r7 */
  70. *(--stk) = (rt_uint32_t)0x06060606; /* r6 */
  71. *(--stk) = (rt_uint32_t)0x05050505; /* r5 */
  72. *(--stk) = (rt_uint32_t)0x02020202; /* r2 */
  73. *(--stk) = (rt_uint32_t)parameter; /* r1 */
  74. return ((rt_uint8_t *)stk);
  75. }
  76. void rt_hw_context_switch(rt_uint32_t from, rt_uint32_t to)
  77. {
  78. rt_interrupt_from_thread = from;
  79. rt_interrupt_to_thread = to;
  80. asm("trap 0x10");
  81. }
  82. void rt_hw_context_switch_interrupt(rt_uint32_t from, rt_uint32_t to)
  83. {
  84. if (rt_thread_switch_interrupt_flag != 1)
  85. {
  86. rt_thread_switch_interrupt_flag = 1;
  87. rt_interrupt_from_thread = from;
  88. }
  89. rt_interrupt_to_thread = to;
  90. }