cpuport.c 3.5 KB

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