context_ccs.asm 7.1 KB
Newer Older
G
Grissiom 已提交
1
;/*
2
; * File      : context_ccs.asm
G
Grissiom 已提交
3 4 5 6 7 8 9 10 11 12 13 14
; * This file is part of RT-Thread RTOS
; * COPYRIGHT (C) 2006, RT-Thread Development Team
; *
; * The license and distribution terms for this file may be
; * found in the file LICENSE in this distribution or at
; * http://www.rt-thread.org/license/LICENSE
; *
; * Change Logs:
; * Date       Author       Notes
; * 2009-01-20     Bernard      first version
; * 2011-07-22     Bernard      added thumb mode porting
; * 2013-05-24     Grissiom     port to CCS
15
; * 2013-05-26     Grissiom     optimize for ARMv7
G
Grissiom 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
; */

   .text
   .arm
   .ref rt_thread_switch_interrupt_flag
   .ref rt_interrupt_from_thread
   .ref rt_interrupt_to_thread
   .ref rt_interrupt_enter
   .ref rt_interrupt_leave
   .ref rt_hw_trap_irq

;/*
; * rt_base_t rt_hw_interrupt_disable();
; */
    .def rt_hw_interrupt_disable
rt_hw_interrupt_disable
    MRS r0, cpsr
33
    CPSID IF
G
Grissiom 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    BX  lr

;/*
; * void rt_hw_interrupt_enable(rt_base_t level);
; */
    .def rt_hw_interrupt_enable
rt_hw_interrupt_enable
    MSR cpsr_c, r0
    BX  lr

;/*
; * void rt_hw_context_switch(rt_uint32 from, rt_uint32 to);
; * r0 --> from
; * r1 --> to
; */
    .def rt_hw_context_switch
rt_hw_context_switch
51 52
    STMDB   sp!, {lr}           ; push pc (lr should be pushed in place of PC)
    STMDB   sp!, {r0-r12, lr}   ; push lr & register file
G
Grissiom 已提交
53 54 55

    MRS     r4, cpsr
    TST     lr, #0x01
G
Grissiom 已提交
56
    ORRNE   r4, r4, #0x20       ; it's thumb code
G
Grissiom 已提交
57

58
    STMDB   sp!, {r4}           ; push cpsr
G
Grissiom 已提交
59

G
Grissiom 已提交
60 61 62 63 64 65 66 67
    .if (__TI_VFP_SUPPORT__)
		VMRS    r4,  fpexc
        TST     r4,  #0x40000000
        BEQ     __no_vfp_frame1
		VSTMDB  sp!, {d0-d15}
        VMRS    r5, fpscr
        ; TODO: add support for Common VFPv3.
        ;       Save registers like FPINST, FPINST2
68
        STMDB   sp!, {r5}
G
Grissiom 已提交
69
__no_vfp_frame1
70
        STMDB   sp!, {r4}
G
Grissiom 已提交
71 72
	.endif

G
Grissiom 已提交
73 74 75
    STR     sp, [r0]            ; store sp in preempted tasks TCB
    LDR     sp, [r1]            ; get new task stack pointer

G
Grissiom 已提交
76
    .if (__TI_VFP_SUPPORT__)
77
        LDMIA   sp!, {r0}       ; get fpexc
G
Grissiom 已提交
78
        VMSR    fpexc,  r0      ; restore fpexc
G
Grissiom 已提交
79 80
        TST     r0,  #0x40000000
        BEQ     __no_vfp_frame2
81
        LDMIA   sp!, {r1}       ; get fpscr
G
Grissiom 已提交
82 83 84 85 86
        VMSR    fpscr, r1
		VLDMIA  sp!, {d0-d15}
__no_vfp_frame2
    .endif

87
    LDMIA   sp!, {r4}           ; pop new task cpsr to spsr
G
Grissiom 已提交
88 89
    MSR     spsr_cxsf, r4

90
    LDMIA   sp!, {r0-r12, lr, pc}^ ; pop new task r0-r12, lr & pc, copy spsr to cpsr
G
Grissiom 已提交
91 92 93 94 95 96 97 98 99

;/*
; * void rt_hw_context_switch_to(rt_uint32 to);
; * r0 --> to
; */
    .def rt_hw_context_switch_to
rt_hw_context_switch_to
    LDR     sp, [r0]            ; get new task stack pointer

G
Grissiom 已提交
100
    .if (__TI_VFP_SUPPORT__)
101
        LDMIA   sp!, {r0}       ; get fpexc
G
Grissiom 已提交
102
        VMSR    fpexc, r0
G
Grissiom 已提交
103 104
        TST     r0,  #0x40000000
        BEQ     __no_vfp_frame_to
105
        LDMIA   sp!, {r1}       ; get fpscr
G
Grissiom 已提交
106 107 108 109 110
        VMSR    fpscr, r1
		VLDMIA  sp!, {d0-d15}
__no_vfp_frame_to
    .endif

111
    LDMIA   sp!, {r4}           ; pop new task cpsr to spsr
G
Grissiom 已提交
112 113
    MSR     spsr_cxsf, r4

114
    LDMIA   sp!, {r0-r12, lr, pc}^ ; pop new task r0-r12, lr & pc, copy spsr to cpsr
G
Grissiom 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

;/*
; * void rt_hw_context_switch_interrupt(rt_uint32 from, rt_uint32 to);
; */

    .def rt_hw_context_switch_interrupt
rt_hw_context_switch_interrupt
    LDR r2, pintflag
    LDR r3, [r2]
    CMP r3, #1
    BEQ _reswitch
    MOV r3, #1              ; set rt_thread_switch_interrupt_flag to 1
    STR r3, [r2]
    LDR r2, pfromthread     ; set rt_interrupt_from_thread
    STR r0, [r2]
_reswitch
    LDR r2, ptothread       ; set rt_interrupt_to_thread
    STR r1, [r2]
    BX  lr

    .def IRQ_Handler
IRQ_Handler
137
    STMDB   sp!, {r0-r12,lr}
G
Grissiom 已提交
138 139 140 141 142 143 144 145 146

    .if (__TI_VFP_SUPPORT__)
		VMRS    r0,  fpexc
        TST     r0,  #0x40000000
        BEQ     __no_vfp_frame_str_irq
		VSTMDB  sp!, {d0-d15}
        VMRS    r1, fpscr
        ; TODO: add support for Common VFPv3.
        ;       Save registers like FPINST, FPINST2
147
        STMDB   sp!, {r1}
G
Grissiom 已提交
148
__no_vfp_frame_str_irq
149
        STMDB   sp!, {r0}
G
Grissiom 已提交
150 151
	.endif

G
Grissiom 已提交
152 153 154 155 156 157 158 159 160 161 162
    BL  rt_interrupt_enter
    BL  rt_hw_trap_irq
    BL  rt_interrupt_leave

    ; if rt_thread_switch_interrupt_flag set, jump to
    ; rt_hw_context_switch_interrupt_do and don't return
    LDR r0, pintflag
    LDR r1, [r0]
    CMP r1, #1
    BEQ rt_hw_context_switch_interrupt_do

G
Grissiom 已提交
163
    .if (__TI_VFP_SUPPORT__)
164
        LDMIA   sp!, {r0}       ; get fpexc
G
Grissiom 已提交
165
        VMSR    fpexc, r0
G
Grissiom 已提交
166 167
        TST     r0,  #0x40000000
        BEQ     __no_vfp_frame_ldr_irq
168
        LDMIA   sp!, {r1}       ; get fpscr
G
Grissiom 已提交
169 170 171 172 173
        VMSR    fpscr, r1
		VLDMIA  sp!, {d0-d15}
__no_vfp_frame_ldr_irq
    .endif

174
    LDMIA   sp!, {r0-r12,lr}
G
Grissiom 已提交
175 176 177 178 179 180 181 182 183 184
    SUBS    pc, lr, #4

; /*
; * void rt_hw_context_switch_interrupt_do(rt_base_t flag)
; */
    .def rt_hw_context_switch_interrupt_do
rt_hw_context_switch_interrupt_do
    MOV     r1,  #0           ; clear flag
    STR     r1,  [r0]

G
Grissiom 已提交
185
    .if (__TI_VFP_SUPPORT__)
186
        LDMIA   sp!, {r0}       ; get fpexc
G
Grissiom 已提交
187
        VMSR    fpexc, r0
G
Grissiom 已提交
188 189
        TST     r0,  #0x40000000
        BEQ     __no_vfp_frame_do1
190
        LDMIA   sp!, {r1}       ; get fpscr
G
Grissiom 已提交
191 192 193 194 195
        VMSR    fpscr, r1
		VLDMIA  sp!, {d0-d15}
__no_vfp_frame_do1
    .endif

196 197
    LDMIA   sp!, {r0-r12,lr}  ; reload saved registers
    STMDB   sp, {r0-r3}       ; save r0-r3. We will restore r0-r3 in the SVC
198 199
                              ; mode so there is no need to update SP.
    SUB     r1,  sp, #16      ; save the right SP value in r1, so we could restore r0-r3.
G
Grissiom 已提交
200 201 202 203 204
    SUB     r2,  lr, #4       ; save old task's pc to r2

    MRS     r3,  spsr         ; get cpsr of interrupt thread

    ; switch to SVC mode and no interrupt
205
    CPSID   IF, #0x13
G
Grissiom 已提交
206

207 208 209 210
    STMDB   sp!, {r2}         ; push old task's pc
    STMDB   sp!, {r4-r12,lr}  ; push old task's lr,r12-r4
    LDMIA   r1!, {r4-r7}      ; restore r0-r3 of the interrupted thread
    STMDB   sp!, {r4-r7}      ; push old task's r3-r0. We don't need to push/pop them to
211 212
                              ; r0-r3 because we just want to transfer the data and don't
                              ; use them here.
213
    STMDB   sp!, {r3}         ; push old task's cpsr
G
Grissiom 已提交
214

G
Grissiom 已提交
215 216 217 218
    .if (__TI_VFP_SUPPORT__)
		VMRS    r0,  fpexc
        TST     r0,  #0x40000000
        BEQ     __no_vfp_frame_do2
G
Grissiom 已提交
219
        VSTMDB  sp!, {d0-d15}
G
Grissiom 已提交
220 221 222
        VMRS    r1, fpscr
        ; TODO: add support for Common VFPv3.
        ;       Save registers like FPINST, FPINST2
223
        STMDB   sp!, {r1}
G
Grissiom 已提交
224
__no_vfp_frame_do2
225
        STMDB   sp!, {r0}
G
Grissiom 已提交
226 227
	.endif

G
Grissiom 已提交
228 229 230 231 232 233 234 235
    LDR     r4,  pfromthread
    LDR     r5,  [r4]
    STR     sp,  [r5]         ; store sp in preempted tasks's TCB

    LDR     r6,  ptothread
    LDR     r6,  [r6]
    LDR     sp,  [r6]         ; get new task's stack pointer

G
Grissiom 已提交
236
    .if (__TI_VFP_SUPPORT__)
237
        LDMIA   sp!, {r0}       ; get fpexc
G
Grissiom 已提交
238
        VMSR    fpexc, r0
G
Grissiom 已提交
239 240
        TST     r0,  #0x40000000
        BEQ     __no_vfp_frame_do3
241
        LDMIA   sp!, {r1}       ; get fpscr
G
Grissiom 已提交
242 243 244 245 246
        VMSR    fpscr, r1
		VLDMIA  sp!, {d0-d15}
__no_vfp_frame_do3
    .endif

247
    LDMIA   sp!, {r4}         ; pop new task's cpsr to spsr
G
Grissiom 已提交
248 249
    MSR     spsr_cxsf, r4

250
    LDMIA   sp!, {r0-r12,lr,pc}^ ; pop new task's r0-r12,lr & pc, copy spsr to cpsr
G
Grissiom 已提交
251 252 253 254

pintflag     .word   rt_thread_switch_interrupt_flag
pfromthread  .word   rt_interrupt_from_thread
ptothread    .word   rt_interrupt_to_thread