Repostiory containing DAPLink source code with Reset Pin workaround for HANI_IOT board.
Upstream: https://github.com/ARMmbed/DAPLink
source/rtos/cm0/HAL_CM1.c@0:01f31e923fe2, 2020-04-07 (annotated)
- Committer:
- Pawel Zarembski
- Date:
- Tue Apr 07 12:55:42 2020 +0200
- Revision:
- 0:01f31e923fe2
hani: DAPLink with reset workaround
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pawel Zarembski |
0:01f31e923fe2 | 1 | /** |
Pawel Zarembski |
0:01f31e923fe2 | 2 | * @file HAL_CM1.c |
Pawel Zarembski |
0:01f31e923fe2 | 3 | * @brief HAL for Cortex-M0(p) |
Pawel Zarembski |
0:01f31e923fe2 | 4 | * |
Pawel Zarembski |
0:01f31e923fe2 | 5 | * DAPLink Interface Firmware |
Pawel Zarembski |
0:01f31e923fe2 | 6 | * Copyright (c) 2009-2016, ARM Limited, All Rights Reserved |
Pawel Zarembski |
0:01f31e923fe2 | 7 | * SPDX-License-Identifier: Apache-2.0 |
Pawel Zarembski |
0:01f31e923fe2 | 8 | * |
Pawel Zarembski |
0:01f31e923fe2 | 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
Pawel Zarembski |
0:01f31e923fe2 | 10 | * not use this file except in compliance with the License. |
Pawel Zarembski |
0:01f31e923fe2 | 11 | * You may obtain a copy of the License at |
Pawel Zarembski |
0:01f31e923fe2 | 12 | * |
Pawel Zarembski |
0:01f31e923fe2 | 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
Pawel Zarembski |
0:01f31e923fe2 | 14 | * |
Pawel Zarembski |
0:01f31e923fe2 | 15 | * Unless required by applicable law or agreed to in writing, software |
Pawel Zarembski |
0:01f31e923fe2 | 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
Pawel Zarembski |
0:01f31e923fe2 | 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Pawel Zarembski |
0:01f31e923fe2 | 18 | * See the License for the specific language governing permissions and |
Pawel Zarembski |
0:01f31e923fe2 | 19 | * limitations under the License. |
Pawel Zarembski |
0:01f31e923fe2 | 20 | */ |
Pawel Zarembski |
0:01f31e923fe2 | 21 | |
Pawel Zarembski |
0:01f31e923fe2 | 22 | #include "rt_TypeDef.h" |
Pawel Zarembski |
0:01f31e923fe2 | 23 | #include "RTX_Config.h" |
Pawel Zarembski |
0:01f31e923fe2 | 24 | #include "rt_System.h" |
Pawel Zarembski |
0:01f31e923fe2 | 25 | #include "rt_HAL_CM.h" |
Pawel Zarembski |
0:01f31e923fe2 | 26 | #include "rt_Task.h" |
Pawel Zarembski |
0:01f31e923fe2 | 27 | #include "rt_List.h" |
Pawel Zarembski |
0:01f31e923fe2 | 28 | #include "rt_MemBox.h" |
Pawel Zarembski |
0:01f31e923fe2 | 29 | |
Pawel Zarembski |
0:01f31e923fe2 | 30 | |
Pawel Zarembski |
0:01f31e923fe2 | 31 | /*---------------------------------------------------------------------------- |
Pawel Zarembski |
0:01f31e923fe2 | 32 | * Functions |
Pawel Zarembski |
0:01f31e923fe2 | 33 | *---------------------------------------------------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 34 | |
Pawel Zarembski |
0:01f31e923fe2 | 35 | |
Pawel Zarembski |
0:01f31e923fe2 | 36 | /*--------------------------- rt_set_PSP ------------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 37 | |
Pawel Zarembski |
0:01f31e923fe2 | 38 | __asm void rt_set_PSP (U32 stack) { |
Pawel Zarembski |
0:01f31e923fe2 | 39 | MSR PSP,R0 |
Pawel Zarembski |
0:01f31e923fe2 | 40 | BX LR |
Pawel Zarembski |
0:01f31e923fe2 | 41 | } |
Pawel Zarembski |
0:01f31e923fe2 | 42 | |
Pawel Zarembski |
0:01f31e923fe2 | 43 | |
Pawel Zarembski |
0:01f31e923fe2 | 44 | /*--------------------------- rt_get_PSP ------------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 45 | |
Pawel Zarembski |
0:01f31e923fe2 | 46 | __asm U32 rt_get_PSP (void) { |
Pawel Zarembski |
0:01f31e923fe2 | 47 | MRS R0,PSP |
Pawel Zarembski |
0:01f31e923fe2 | 48 | BX LR |
Pawel Zarembski |
0:01f31e923fe2 | 49 | } |
Pawel Zarembski |
0:01f31e923fe2 | 50 | |
Pawel Zarembski |
0:01f31e923fe2 | 51 | |
Pawel Zarembski |
0:01f31e923fe2 | 52 | /*--------------------------- os_set_env ------------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 53 | |
Pawel Zarembski |
0:01f31e923fe2 | 54 | __asm void os_set_env (void) { |
Pawel Zarembski |
0:01f31e923fe2 | 55 | /* Switch to Unprivileged/Privileged Thread mode, use PSP. */ |
Pawel Zarembski |
0:01f31e923fe2 | 56 | MOV R0,SP ; PSP = MSP |
Pawel Zarembski |
0:01f31e923fe2 | 57 | MSR PSP,R0 |
Pawel Zarembski |
0:01f31e923fe2 | 58 | LDR R0,=__cpp(&os_flags) |
Pawel Zarembski |
0:01f31e923fe2 | 59 | LDRB R0,[R0] |
Pawel Zarembski |
0:01f31e923fe2 | 60 | LSLS R0,#31 |
Pawel Zarembski |
0:01f31e923fe2 | 61 | BNE PrivilegedE |
Pawel Zarembski |
0:01f31e923fe2 | 62 | MOVS R0,#0x03 ; Unprivileged Thread mode, use PSP |
Pawel Zarembski |
0:01f31e923fe2 | 63 | MSR CONTROL,R0 |
Pawel Zarembski |
0:01f31e923fe2 | 64 | BX LR |
Pawel Zarembski |
0:01f31e923fe2 | 65 | PrivilegedE |
Pawel Zarembski |
0:01f31e923fe2 | 66 | MOVS R0,#0x02 ; Privileged Thread mode, use PSP |
Pawel Zarembski |
0:01f31e923fe2 | 67 | MSR CONTROL,R0 |
Pawel Zarembski |
0:01f31e923fe2 | 68 | BX LR |
Pawel Zarembski |
0:01f31e923fe2 | 69 | |
Pawel Zarembski |
0:01f31e923fe2 | 70 | ALIGN |
Pawel Zarembski |
0:01f31e923fe2 | 71 | } |
Pawel Zarembski |
0:01f31e923fe2 | 72 | |
Pawel Zarembski |
0:01f31e923fe2 | 73 | |
Pawel Zarembski |
0:01f31e923fe2 | 74 | /*--------------------------- _alloc_box ------------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 75 | |
Pawel Zarembski |
0:01f31e923fe2 | 76 | __asm void *_alloc_box (void *box_mem) { |
Pawel Zarembski |
0:01f31e923fe2 | 77 | /* Function wrapper for Unprivileged/Privileged mode. */ |
Pawel Zarembski |
0:01f31e923fe2 | 78 | LDR R3,=__cpp(rt_alloc_box) |
Pawel Zarembski |
0:01f31e923fe2 | 79 | MOV R12,R3 |
Pawel Zarembski |
0:01f31e923fe2 | 80 | MRS R3,IPSR |
Pawel Zarembski |
0:01f31e923fe2 | 81 | LSLS R3,#24 |
Pawel Zarembski |
0:01f31e923fe2 | 82 | BNE PrivilegedA |
Pawel Zarembski |
0:01f31e923fe2 | 83 | MRS R3,CONTROL |
Pawel Zarembski |
0:01f31e923fe2 | 84 | LSLS R3,#31 |
Pawel Zarembski |
0:01f31e923fe2 | 85 | BEQ PrivilegedA |
Pawel Zarembski |
0:01f31e923fe2 | 86 | SVC 0 |
Pawel Zarembski |
0:01f31e923fe2 | 87 | BX LR |
Pawel Zarembski |
0:01f31e923fe2 | 88 | PrivilegedA |
Pawel Zarembski |
0:01f31e923fe2 | 89 | BX R12 |
Pawel Zarembski |
0:01f31e923fe2 | 90 | |
Pawel Zarembski |
0:01f31e923fe2 | 91 | ALIGN |
Pawel Zarembski |
0:01f31e923fe2 | 92 | } |
Pawel Zarembski |
0:01f31e923fe2 | 93 | |
Pawel Zarembski |
0:01f31e923fe2 | 94 | |
Pawel Zarembski |
0:01f31e923fe2 | 95 | /*--------------------------- _free_box -------------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 96 | |
Pawel Zarembski |
0:01f31e923fe2 | 97 | __asm int _free_box (void *box_mem, void *box) { |
Pawel Zarembski |
0:01f31e923fe2 | 98 | /* Function wrapper for Unprivileged/Privileged mode. */ |
Pawel Zarembski |
0:01f31e923fe2 | 99 | LDR R3,=__cpp(rt_free_box) |
Pawel Zarembski |
0:01f31e923fe2 | 100 | MOV R12,R3 |
Pawel Zarembski |
0:01f31e923fe2 | 101 | MRS R3,IPSR |
Pawel Zarembski |
0:01f31e923fe2 | 102 | LSLS R3,#24 |
Pawel Zarembski |
0:01f31e923fe2 | 103 | BNE PrivilegedF |
Pawel Zarembski |
0:01f31e923fe2 | 104 | MRS R3,CONTROL |
Pawel Zarembski |
0:01f31e923fe2 | 105 | LSLS R3,#31 |
Pawel Zarembski |
0:01f31e923fe2 | 106 | BEQ PrivilegedF |
Pawel Zarembski |
0:01f31e923fe2 | 107 | SVC 0 |
Pawel Zarembski |
0:01f31e923fe2 | 108 | BX LR |
Pawel Zarembski |
0:01f31e923fe2 | 109 | PrivilegedF |
Pawel Zarembski |
0:01f31e923fe2 | 110 | BX R12 |
Pawel Zarembski |
0:01f31e923fe2 | 111 | |
Pawel Zarembski |
0:01f31e923fe2 | 112 | ALIGN |
Pawel Zarembski |
0:01f31e923fe2 | 113 | } |
Pawel Zarembski |
0:01f31e923fe2 | 114 | |
Pawel Zarembski |
0:01f31e923fe2 | 115 | |
Pawel Zarembski |
0:01f31e923fe2 | 116 | /*-------------------------- SVC_Handler ------------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 117 | |
Pawel Zarembski |
0:01f31e923fe2 | 118 | __asm void SVC_Handler (void) { |
Pawel Zarembski |
0:01f31e923fe2 | 119 | PRESERVE8 |
Pawel Zarembski |
0:01f31e923fe2 | 120 | |
Pawel Zarembski |
0:01f31e923fe2 | 121 | IMPORT SVC_Count |
Pawel Zarembski |
0:01f31e923fe2 | 122 | IMPORT SVC_Table |
Pawel Zarembski |
0:01f31e923fe2 | 123 | IMPORT rt_stk_check |
Pawel Zarembski |
0:01f31e923fe2 | 124 | |
Pawel Zarembski |
0:01f31e923fe2 | 125 | MRS R0,PSP ; Read PSP |
Pawel Zarembski |
0:01f31e923fe2 | 126 | LDR R1,[R0,#24] ; Read Saved PC from Stack |
Pawel Zarembski |
0:01f31e923fe2 | 127 | SUBS R1,R1,#2 ; Point to SVC Instruction |
Pawel Zarembski |
0:01f31e923fe2 | 128 | LDRB R1,[R1] ; Load SVC Number |
Pawel Zarembski |
0:01f31e923fe2 | 129 | CMP R1,#0 |
Pawel Zarembski |
0:01f31e923fe2 | 130 | BNE SVC_User ; User SVC Number > 0 |
Pawel Zarembski |
0:01f31e923fe2 | 131 | |
Pawel Zarembski |
0:01f31e923fe2 | 132 | MOV LR,R4 |
Pawel Zarembski |
0:01f31e923fe2 | 133 | LDMIA R0,{R0-R3,R4} ; Read R0-R3,R12 from stack |
Pawel Zarembski |
0:01f31e923fe2 | 134 | MOV R12,R4 |
Pawel Zarembski |
0:01f31e923fe2 | 135 | MOV R4,LR |
Pawel Zarembski |
0:01f31e923fe2 | 136 | BLX R12 ; Call SVC Function |
Pawel Zarembski |
0:01f31e923fe2 | 137 | |
Pawel Zarembski |
0:01f31e923fe2 | 138 | LDR R3,=__cpp(&os_tsk) |
Pawel Zarembski |
0:01f31e923fe2 | 139 | LDMIA R3!,{R1,R2} ; os_tsk.run, os_tsk.new |
Pawel Zarembski |
0:01f31e923fe2 | 140 | CMP R1,R2 |
Pawel Zarembski |
0:01f31e923fe2 | 141 | BEQ SVC_Exit ; no task switch |
Pawel Zarembski |
0:01f31e923fe2 | 142 | |
Pawel Zarembski |
0:01f31e923fe2 | 143 | SUBS R3,#8 |
Pawel Zarembski |
0:01f31e923fe2 | 144 | CMP R1,#0 ; Runtask deleted? |
Pawel Zarembski |
0:01f31e923fe2 | 145 | BEQ SVC_Restore |
Pawel Zarembski |
0:01f31e923fe2 | 146 | |
Pawel Zarembski |
0:01f31e923fe2 | 147 | PUSH {R2,R3} |
Pawel Zarembski |
0:01f31e923fe2 | 148 | ADDS R1,#32 ; for STRB offset < 32 |
Pawel Zarembski |
0:01f31e923fe2 | 149 | MOVS R3,#1 |
Pawel Zarembski |
0:01f31e923fe2 | 150 | STRB R3,[R1,#TCB_RETUPD-32] ; os_tsk.run->ret_upd = 1 |
Pawel Zarembski |
0:01f31e923fe2 | 151 | |
Pawel Zarembski |
0:01f31e923fe2 | 152 | MRS R3,PSP ; Read PSP |
Pawel Zarembski |
0:01f31e923fe2 | 153 | SUBS R3,R3,#32 ; Adjust Start Address |
Pawel Zarembski |
0:01f31e923fe2 | 154 | STR R3,[R1,#TCB_TSTACK-32] ; Update os_tsk.run->tsk_stack |
Pawel Zarembski |
0:01f31e923fe2 | 155 | STMIA R3!,{R4-R7} ; Save old context (R4-R7) |
Pawel Zarembski |
0:01f31e923fe2 | 156 | MOV R4,R8 |
Pawel Zarembski |
0:01f31e923fe2 | 157 | MOV R5,R9 |
Pawel Zarembski |
0:01f31e923fe2 | 158 | MOV R6,R10 |
Pawel Zarembski |
0:01f31e923fe2 | 159 | MOV R7,R11 |
Pawel Zarembski |
0:01f31e923fe2 | 160 | STMIA R3!,{R4-R7} ; Save old context (R8-R11) |
Pawel Zarembski |
0:01f31e923fe2 | 161 | BL rt_stk_check ; Check for Stack overflow |
Pawel Zarembski |
0:01f31e923fe2 | 162 | POP {R2,R3} |
Pawel Zarembski |
0:01f31e923fe2 | 163 | |
Pawel Zarembski |
0:01f31e923fe2 | 164 | SVC_Restore |
Pawel Zarembski |
0:01f31e923fe2 | 165 | STR R2,[R3] ; os_tsk.run = os_tsk.new |
Pawel Zarembski |
0:01f31e923fe2 | 166 | |
Pawel Zarembski |
0:01f31e923fe2 | 167 | ADDS R2,#32 ; for STRB offset < 32 |
Pawel Zarembski |
0:01f31e923fe2 | 168 | LDR R3,[R2,#TCB_TSTACK-32] ; os_tsk.new->tsk_stack |
Pawel Zarembski |
0:01f31e923fe2 | 169 | ADDS R3,R3,#16 ; Adjust Start Address |
Pawel Zarembski |
0:01f31e923fe2 | 170 | LDMIA R3!,{R4-R7} ; Restore new Context (R8-R11) |
Pawel Zarembski |
0:01f31e923fe2 | 171 | MOV R8,R4 |
Pawel Zarembski |
0:01f31e923fe2 | 172 | MOV R9,R5 |
Pawel Zarembski |
0:01f31e923fe2 | 173 | MOV R10,R6 |
Pawel Zarembski |
0:01f31e923fe2 | 174 | MOV R11,R7 |
Pawel Zarembski |
0:01f31e923fe2 | 175 | MSR PSP,R3 ; Write PSP |
Pawel Zarembski |
0:01f31e923fe2 | 176 | SUBS R3,R3,#32 ; Adjust Start Address |
Pawel Zarembski |
0:01f31e923fe2 | 177 | LDMIA R3!,{R4-R7} ; Restore new Context (R4-R7) |
Pawel Zarembski |
0:01f31e923fe2 | 178 | |
Pawel Zarembski |
0:01f31e923fe2 | 179 | LDRB R0,[R2,#TCB_RETUPD-32] ; Update ret_val ? |
Pawel Zarembski |
0:01f31e923fe2 | 180 | CMP R0,#0 |
Pawel Zarembski |
0:01f31e923fe2 | 181 | BEQ SVC_Return |
Pawel Zarembski |
0:01f31e923fe2 | 182 | LDRB R0,[R2,#TCB_RETVAL-32] ; Write os_tsk.new->ret_val |
Pawel Zarembski |
0:01f31e923fe2 | 183 | |
Pawel Zarembski |
0:01f31e923fe2 | 184 | SVC_Exit |
Pawel Zarembski |
0:01f31e923fe2 | 185 | MRS R3,PSP ; Read PSP |
Pawel Zarembski |
0:01f31e923fe2 | 186 | STR R0,[R3] ; Function return value |
Pawel Zarembski |
0:01f31e923fe2 | 187 | |
Pawel Zarembski |
0:01f31e923fe2 | 188 | SVC_Return |
Pawel Zarembski |
0:01f31e923fe2 | 189 | MOVS R3,#:NOT:0xFFFFFFFD ; Set EXC_RETURN value |
Pawel Zarembski |
0:01f31e923fe2 | 190 | MVNS R3,R3 |
Pawel Zarembski |
0:01f31e923fe2 | 191 | BX R3 ; RETI to Thread Mode, use PSP |
Pawel Zarembski |
0:01f31e923fe2 | 192 | |
Pawel Zarembski |
0:01f31e923fe2 | 193 | /*------------------- User SVC ------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 194 | |
Pawel Zarembski |
0:01f31e923fe2 | 195 | SVC_User |
Pawel Zarembski |
0:01f31e923fe2 | 196 | PUSH {R4,LR} ; Save Registers |
Pawel Zarembski |
0:01f31e923fe2 | 197 | LDR R2,=SVC_Count |
Pawel Zarembski |
0:01f31e923fe2 | 198 | LDR R2,[R2] |
Pawel Zarembski |
0:01f31e923fe2 | 199 | CMP R1,R2 |
Pawel Zarembski |
0:01f31e923fe2 | 200 | BHI SVC_Done ; Overflow |
Pawel Zarembski |
0:01f31e923fe2 | 201 | |
Pawel Zarembski |
0:01f31e923fe2 | 202 | LDR R4,=SVC_Table-4 |
Pawel Zarembski |
0:01f31e923fe2 | 203 | LSLS R1,R1,#2 |
Pawel Zarembski |
0:01f31e923fe2 | 204 | LDR R4,[R4,R1] ; Load SVC Function Address |
Pawel Zarembski |
0:01f31e923fe2 | 205 | MOV LR,R4 |
Pawel Zarembski |
0:01f31e923fe2 | 206 | |
Pawel Zarembski |
0:01f31e923fe2 | 207 | LDMIA R0,{R0-R3,R4} ; Read R0-R3,R12 from stack |
Pawel Zarembski |
0:01f31e923fe2 | 208 | MOV R12,R4 |
Pawel Zarembski |
0:01f31e923fe2 | 209 | BLX LR ; Call SVC Function |
Pawel Zarembski |
0:01f31e923fe2 | 210 | |
Pawel Zarembski |
0:01f31e923fe2 | 211 | MRS R4,PSP ; Read PSP |
Pawel Zarembski |
0:01f31e923fe2 | 212 | STMIA R4!,{R0-R3} ; Function return values |
Pawel Zarembski |
0:01f31e923fe2 | 213 | SVC_Done |
Pawel Zarembski |
0:01f31e923fe2 | 214 | POP {R4,PC} ; RETI |
Pawel Zarembski |
0:01f31e923fe2 | 215 | |
Pawel Zarembski |
0:01f31e923fe2 | 216 | ALIGN |
Pawel Zarembski |
0:01f31e923fe2 | 217 | } |
Pawel Zarembski |
0:01f31e923fe2 | 218 | |
Pawel Zarembski |
0:01f31e923fe2 | 219 | |
Pawel Zarembski |
0:01f31e923fe2 | 220 | /*-------------------------- PendSV_Handler ---------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 221 | |
Pawel Zarembski |
0:01f31e923fe2 | 222 | __asm void PendSV_Handler (void) { |
Pawel Zarembski |
0:01f31e923fe2 | 223 | PRESERVE8 |
Pawel Zarembski |
0:01f31e923fe2 | 224 | |
Pawel Zarembski |
0:01f31e923fe2 | 225 | BL __cpp(rt_pop_req) |
Pawel Zarembski |
0:01f31e923fe2 | 226 | |
Pawel Zarembski |
0:01f31e923fe2 | 227 | Sys_Switch |
Pawel Zarembski |
0:01f31e923fe2 | 228 | LDR R3,=__cpp(&os_tsk) |
Pawel Zarembski |
0:01f31e923fe2 | 229 | LDMIA R3!,{R1,R2} ; os_tsk.run, os_tsk.new |
Pawel Zarembski |
0:01f31e923fe2 | 230 | CMP R1,R2 |
Pawel Zarembski |
0:01f31e923fe2 | 231 | BEQ Sys_Exit ; no task switch |
Pawel Zarembski |
0:01f31e923fe2 | 232 | |
Pawel Zarembski |
0:01f31e923fe2 | 233 | SUBS R3,#8 |
Pawel Zarembski |
0:01f31e923fe2 | 234 | PUSH {R2,R3} |
Pawel Zarembski |
0:01f31e923fe2 | 235 | ADDS R1,#32 ; for STRB offset < 32 |
Pawel Zarembski |
0:01f31e923fe2 | 236 | |
Pawel Zarembski |
0:01f31e923fe2 | 237 | MOVS R3,#0 |
Pawel Zarembski |
0:01f31e923fe2 | 238 | STRB R3,[R1,#TCB_RETUPD-32] ; os_tsk.run->ret_upd = 0 |
Pawel Zarembski |
0:01f31e923fe2 | 239 | MRS R3,PSP ; Read PSP |
Pawel Zarembski |
0:01f31e923fe2 | 240 | SUBS R3,R3,#32 ; Adjust Start Address |
Pawel Zarembski |
0:01f31e923fe2 | 241 | STR R3,[R1,#TCB_TSTACK-32] ; Update os_tsk.run->tsk_stack |
Pawel Zarembski |
0:01f31e923fe2 | 242 | STMIA R3!,{R4-R7} ; Save old context (R4-R7) |
Pawel Zarembski |
0:01f31e923fe2 | 243 | MOV R4,R8 |
Pawel Zarembski |
0:01f31e923fe2 | 244 | MOV R5,R9 |
Pawel Zarembski |
0:01f31e923fe2 | 245 | MOV R6,R10 |
Pawel Zarembski |
0:01f31e923fe2 | 246 | MOV R7,R11 |
Pawel Zarembski |
0:01f31e923fe2 | 247 | STMIA R3!,{R4-R7} ; Save old context (R8-R11) |
Pawel Zarembski |
0:01f31e923fe2 | 248 | BL rt_stk_check ; Check for Stack overflow |
Pawel Zarembski |
0:01f31e923fe2 | 249 | |
Pawel Zarembski |
0:01f31e923fe2 | 250 | POP {R2,R3} |
Pawel Zarembski |
0:01f31e923fe2 | 251 | STR R2,[R3] ; os_tsk.run = os_tsk.new |
Pawel Zarembski |
0:01f31e923fe2 | 252 | ADDS R2,#32 ; for STRB offset < 32 |
Pawel Zarembski |
0:01f31e923fe2 | 253 | |
Pawel Zarembski |
0:01f31e923fe2 | 254 | LDR R3,[R2,#TCB_TSTACK-32] ; os_tsk.new->tsk_stack |
Pawel Zarembski |
0:01f31e923fe2 | 255 | ADDS R3,R3,#16 ; Adjust Start Address |
Pawel Zarembski |
0:01f31e923fe2 | 256 | LDMIA R3!,{R4-R7} ; Restore new Context (R8-R11) |
Pawel Zarembski |
0:01f31e923fe2 | 257 | MOV R8,R4 |
Pawel Zarembski |
0:01f31e923fe2 | 258 | MOV R9,R5 |
Pawel Zarembski |
0:01f31e923fe2 | 259 | MOV R10,R6 |
Pawel Zarembski |
0:01f31e923fe2 | 260 | MOV R11,R7 |
Pawel Zarembski |
0:01f31e923fe2 | 261 | MSR PSP,R3 ; Write PSP |
Pawel Zarembski |
0:01f31e923fe2 | 262 | SUBS R3,R3,#32 ; Adjust Start Address |
Pawel Zarembski |
0:01f31e923fe2 | 263 | LDMIA R3!,{R4-R7} ; Restore new Context (R4-R7) |
Pawel Zarembski |
0:01f31e923fe2 | 264 | |
Pawel Zarembski |
0:01f31e923fe2 | 265 | LDRB R0,[R2,#TCB_RETUPD-32] ; Update ret_val ? |
Pawel Zarembski |
0:01f31e923fe2 | 266 | CMP R0,#0 |
Pawel Zarembski |
0:01f31e923fe2 | 267 | BEQ Sys_Exit |
Pawel Zarembski |
0:01f31e923fe2 | 268 | LDRB R0,[R2,#TCB_RETVAL-32] ; Write os_tsk.new->ret_val |
Pawel Zarembski |
0:01f31e923fe2 | 269 | STR R0,[R3,#16] |
Pawel Zarembski |
0:01f31e923fe2 | 270 | Sys_Exit |
Pawel Zarembski |
0:01f31e923fe2 | 271 | MOVS R3,#:NOT:0xFFFFFFFD ; Set EXC_RETURN value |
Pawel Zarembski |
0:01f31e923fe2 | 272 | MVNS R3,R3 |
Pawel Zarembski |
0:01f31e923fe2 | 273 | BX R3 ; RETI to Thread Mode, use PSP |
Pawel Zarembski |
0:01f31e923fe2 | 274 | |
Pawel Zarembski |
0:01f31e923fe2 | 275 | ALIGN |
Pawel Zarembski |
0:01f31e923fe2 | 276 | } |
Pawel Zarembski |
0:01f31e923fe2 | 277 | |
Pawel Zarembski |
0:01f31e923fe2 | 278 | |
Pawel Zarembski |
0:01f31e923fe2 | 279 | /*-------------------------- SysTick_Handler --------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 280 | |
Pawel Zarembski |
0:01f31e923fe2 | 281 | __asm void SysTick_Handler (void) { |
Pawel Zarembski |
0:01f31e923fe2 | 282 | PRESERVE8 |
Pawel Zarembski |
0:01f31e923fe2 | 283 | |
Pawel Zarembski |
0:01f31e923fe2 | 284 | BL __cpp(rt_systick) |
Pawel Zarembski |
0:01f31e923fe2 | 285 | B Sys_Switch |
Pawel Zarembski |
0:01f31e923fe2 | 286 | |
Pawel Zarembski |
0:01f31e923fe2 | 287 | ALIGN |
Pawel Zarembski |
0:01f31e923fe2 | 288 | } |
Pawel Zarembski |
0:01f31e923fe2 | 289 | |
Pawel Zarembski |
0:01f31e923fe2 | 290 | |
Pawel Zarembski |
0:01f31e923fe2 | 291 | /*-------------------------- OS_Tick_Handler --------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 292 | |
Pawel Zarembski |
0:01f31e923fe2 | 293 | __asm void OS_Tick_Handler (void) { |
Pawel Zarembski |
0:01f31e923fe2 | 294 | PRESERVE8 |
Pawel Zarembski |
0:01f31e923fe2 | 295 | |
Pawel Zarembski |
0:01f31e923fe2 | 296 | BL __cpp(os_tick_irqack) |
Pawel Zarembski |
0:01f31e923fe2 | 297 | BL __cpp(rt_systick) |
Pawel Zarembski |
0:01f31e923fe2 | 298 | B Sys_Switch |
Pawel Zarembski |
0:01f31e923fe2 | 299 | |
Pawel Zarembski |
0:01f31e923fe2 | 300 | ALIGN |
Pawel Zarembski |
0:01f31e923fe2 | 301 | } |
Pawel Zarembski |
0:01f31e923fe2 | 302 | |
Pawel Zarembski |
0:01f31e923fe2 | 303 | |
Pawel Zarembski |
0:01f31e923fe2 | 304 | /*--------------------------- rt_init_stack ---------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 305 | |
Pawel Zarembski |
0:01f31e923fe2 | 306 | void rt_init_stack (P_TCB p_TCB, FUNCP task_body) { |
Pawel Zarembski |
0:01f31e923fe2 | 307 | /* Prepare TCB and saved context for a first time start of a task. */ |
Pawel Zarembski |
0:01f31e923fe2 | 308 | U32 *stk,i,size; |
Pawel Zarembski |
0:01f31e923fe2 | 309 | |
Pawel Zarembski |
0:01f31e923fe2 | 310 | /* Prepare a complete interrupt frame for first task start */ |
Pawel Zarembski |
0:01f31e923fe2 | 311 | size = p_TCB->priv_stack >> 2; |
Pawel Zarembski |
0:01f31e923fe2 | 312 | if (size == 0) { |
Pawel Zarembski |
0:01f31e923fe2 | 313 | size = (U16)os_stackinfo >> 2; |
Pawel Zarembski |
0:01f31e923fe2 | 314 | } |
Pawel Zarembski |
0:01f31e923fe2 | 315 | |
Pawel Zarembski |
0:01f31e923fe2 | 316 | /* Write to the top of stack. */ |
Pawel Zarembski |
0:01f31e923fe2 | 317 | stk = &p_TCB->stack[size]; |
Pawel Zarembski |
0:01f31e923fe2 | 318 | |
Pawel Zarembski |
0:01f31e923fe2 | 319 | /* Auto correct to 8-byte ARM stack alignment. */ |
Pawel Zarembski |
0:01f31e923fe2 | 320 | if ((U32)stk & 0x04) { |
Pawel Zarembski |
0:01f31e923fe2 | 321 | stk--; |
Pawel Zarembski |
0:01f31e923fe2 | 322 | } |
Pawel Zarembski |
0:01f31e923fe2 | 323 | |
Pawel Zarembski |
0:01f31e923fe2 | 324 | stk -= 16; |
Pawel Zarembski |
0:01f31e923fe2 | 325 | |
Pawel Zarembski |
0:01f31e923fe2 | 326 | /* Default xPSR and initial PC */ |
Pawel Zarembski |
0:01f31e923fe2 | 327 | stk[15] = INITIAL_xPSR; |
Pawel Zarembski |
0:01f31e923fe2 | 328 | stk[14] = (U32)task_body; |
Pawel Zarembski |
0:01f31e923fe2 | 329 | |
Pawel Zarembski |
0:01f31e923fe2 | 330 | /* Clear R1-R12,LR registers. */ |
Pawel Zarembski |
0:01f31e923fe2 | 331 | for (i = 0; i < 14; i++) { |
Pawel Zarembski |
0:01f31e923fe2 | 332 | stk[i] = 0; |
Pawel Zarembski |
0:01f31e923fe2 | 333 | } |
Pawel Zarembski |
0:01f31e923fe2 | 334 | |
Pawel Zarembski |
0:01f31e923fe2 | 335 | /* Assign a void pointer to R0. */ |
Pawel Zarembski |
0:01f31e923fe2 | 336 | stk[8] = (U32)p_TCB->msg; |
Pawel Zarembski |
0:01f31e923fe2 | 337 | |
Pawel Zarembski |
0:01f31e923fe2 | 338 | /* Initial Task stack pointer. */ |
Pawel Zarembski |
0:01f31e923fe2 | 339 | p_TCB->tsk_stack = (U32)stk; |
Pawel Zarembski |
0:01f31e923fe2 | 340 | |
Pawel Zarembski |
0:01f31e923fe2 | 341 | /* Task entry point. */ |
Pawel Zarembski |
0:01f31e923fe2 | 342 | p_TCB->ptask = task_body; |
Pawel Zarembski |
0:01f31e923fe2 | 343 | |
Pawel Zarembski |
0:01f31e923fe2 | 344 | /* Set a magic word for checking of stack overflow. */ |
Pawel Zarembski |
0:01f31e923fe2 | 345 | p_TCB->stack[0] = MAGIC_WORD; |
Pawel Zarembski |
0:01f31e923fe2 | 346 | } |
Pawel Zarembski |
0:01f31e923fe2 | 347 | |
Pawel Zarembski |
0:01f31e923fe2 | 348 | |
Pawel Zarembski |
0:01f31e923fe2 | 349 | /*---------------------------------------------------------------------------- |
Pawel Zarembski |
0:01f31e923fe2 | 350 | * end of file |
Pawel Zarembski |
0:01f31e923fe2 | 351 | *---------------------------------------------------------------------------*/ |
Pawel Zarembski |
0:01f31e923fe2 | 352 |