cpuport.c 3.8 KB

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