cpuport.c 3.8 KB

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