context_gcc.S 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * File : context.S
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2013, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2013-07-05 Bernard the first version
  23. */
  24. /*
  25. * rt_base_t rt_hw_interrupt_disable();
  26. */
  27. .globl rt_hw_interrupt_disable
  28. rt_hw_interrupt_disable:
  29. mrs r0, cpsr
  30. cpsid if
  31. bx lr
  32. /*
  33. * void rt_hw_interrupt_enable(rt_base_t level);
  34. */
  35. .globl rt_hw_interrupt_enable
  36. rt_hw_interrupt_enable:
  37. msr cpsr_c, r0
  38. bx lr
  39. /*
  40. * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
  41. * r0 --> from
  42. * r1 --> to
  43. */
  44. .globl rt_hw_context_switch
  45. rt_hw_context_switch:
  46. stmfd sp!, {lr} @ push pc (lr should be pushed in place of PC)
  47. stmfd sp!, {r0-r12, lr} @ push lr & register file
  48. mrs r4, cpsr
  49. tst lr, #0x01
  50. orrne r4, r4, #0x20 @ it's thumb code
  51. stmfd sp!, {r4} @ push cpsr
  52. str sp, [r0] @ store sp in preempted tasks TCB
  53. ldr sp, [r1] @ get new task stack pointer
  54. ldmfd sp!, {r4} @ pop new task cpsr to spsr
  55. msr spsr_cxsf, r4
  56. _do_switch:
  57. ldmfd sp!, {r0-r12, lr, pc}^ @ pop new task r0-r12, lr & pc, copy spsr to cpsr
  58. /*
  59. * void rt_hw_context_switch_to(rt_uint32 to);
  60. * r0 --> to
  61. */
  62. .globl rt_hw_context_switch_to
  63. rt_hw_context_switch_to:
  64. ldr sp, [r0] @ get new task stack pointer
  65. ldmfd sp!, {r4} @ pop new task spsr
  66. msr spsr_cxsf, r4
  67. bic r4, r4, #0x20 @ must be ARM mode
  68. msr cpsr_cxsf, r4
  69. ldmfd sp!, {r0-r12, lr, pc}^ @ pop new task r0-r12, lr & pc
  70. /*
  71. * void rt_hw_context_switch_interrupt(rt_uint32 from, rt_uint32 to);
  72. */
  73. .globl rt_thread_switch_interrupt_flag
  74. .globl rt_interrupt_from_thread
  75. .globl rt_interrupt_to_thread
  76. .globl rt_hw_context_switch_interrupt
  77. rt_hw_context_switch_interrupt:
  78. ldr r2, =rt_thread_switch_interrupt_flag
  79. ldr r3, [r2]
  80. cmp r3, #1
  81. beq _reswitch
  82. mov r3, #1 @ set rt_thread_switch_interrupt_flag to 1
  83. str r3, [r2]
  84. ldr r2, =rt_interrupt_from_thread @ set rt_interrupt_from_thread
  85. str r0, [r2]
  86. _reswitch:
  87. ldr r2, =rt_interrupt_to_thread @ set rt_interrupt_to_thread
  88. str r1, [r2]
  89. bx lr