1
0

cpuport.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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-25 lgnq save texit address in to thread stack
  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_uint8_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_uint16_t *pstk16;
  44. rt_uint16_t flag;
  45. flag = 0x0040;
  46. pstk16 = (rt_uint16_t *)stack_addr;
  47. *pstk16-- = (rt_uint32_t)texit >> 16L;
  48. *pstk16-- = (rt_uint32_t)texit & 0x0000FFFFL;
  49. /* Simulate ISR entry */
  50. *pstk16-- = (flag&0x00FF) | /* The lowest byte of the FLAG register */
  51. (((rt_uint32_t)tentry>>8)&0x00000F00) | /* The highest nibble of the PC register */
  52. ((flag<<4)&0xF000); /* The highest nibble of the FLAG register */
  53. *pstk16-- = (((rt_uint32_t)tentry)&0x0000FFFF); /* The lowest bytes of the PC register */
  54. /* Save registers onto stack frame */
  55. *pstk16-- = (rt_uint16_t)0xFBFB; /* FB register */
  56. *pstk16-- = (rt_uint16_t)0x3B3B; /* SB register */
  57. *pstk16-- = (rt_uint16_t)0xA1A1; /* A1 register */
  58. *pstk16-- = (rt_uint16_t)0xA0A0; /* A0 register */
  59. *pstk16-- = (rt_uint16_t)0x3333; /* R3 register */
  60. *pstk16-- = (rt_uint32_t)parameter >> 16L; /* Pass argument in R2 register */
  61. *pstk16-- = (rt_uint32_t)parameter & 0x0000FFFFL; /* Pass argument in R1 register */
  62. *pstk16 = (rt_uint16_t)0x0000; /* R0 register */
  63. /* return task's current stack address */
  64. return (rt_uint8_t *)pstk16;
  65. }
  66. void rt_hw_context_switch(rt_uint32_t from, rt_uint32_t to)
  67. {
  68. rt_interrupt_from_thread = from;
  69. rt_interrupt_to_thread = to;
  70. asm("INT #0");
  71. }
  72. void rt_hw_context_switch_interrupt(rt_uint32_t from, rt_uint32_t to)
  73. {
  74. if (rt_thread_switch_interrupt_flag != 1)
  75. {
  76. rt_thread_switch_interrupt_flag = 1;
  77. rt_interrupt_from_thread = from;
  78. }
  79. rt_interrupt_to_thread = to;
  80. }
  81. #if defined(__GNUC__)
  82. rt_base_t rt_hw_interrupt_disable(void)
  83. {
  84. register rt_uint16_t temp;
  85. asm("STC FLG, %0":"=r" (temp));
  86. asm("FCLR I");
  87. return (rt_base_t)temp;
  88. }
  89. void rt_hw_interrupt_enable(rt_base_t level)
  90. {
  91. register rt_uint16_t temp;
  92. temp = level & 0xffff;
  93. asm("LDC %0, FLG": :"r" (temp));
  94. }
  95. #endif