cpuport.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2011-02-23 Bernard the first version
  9. * 2012-09-23 lgnq set the texit to R31
  10. */
  11. #include <rtthread.h>
  12. extern volatile rt_uint8_t rt_interrupt_nest;
  13. /* switch flag on interrupt and thread pointer to save switch record */
  14. rt_uint32_t rt_interrupt_from_thread;
  15. rt_uint32_t rt_interrupt_to_thread;
  16. rt_uint32_t rt_thread_switch_interrupt_flag;
  17. /**
  18. * This function will initialize hardware interrupt
  19. */
  20. void rt_hw_interrupt_init(void)
  21. {
  22. /* init interrupt nest, and context in thread sp */
  23. rt_interrupt_nest = 0;
  24. rt_interrupt_from_thread = 0;
  25. rt_interrupt_to_thread = 0;
  26. rt_thread_switch_interrupt_flag = 0;
  27. }
  28. /**
  29. * This function will initialize thread stack
  30. *
  31. * @param tentry the entry of thread
  32. * @param parameter the parameter of entry
  33. * @param stack_addr the beginning stack address
  34. * @param texit the function will be called when thread exit
  35. *
  36. * @return stack address
  37. */
  38. rt_uint8_t *rt_hw_stack_init(void *tentry,
  39. void *parameter,
  40. rt_uint8_t *stack_addr,
  41. 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)texit; /* 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. }