1;/* 2; * FreeRTOS Kernel V10.6.2 3; * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4; * 5; * SPDX-License-Identifier: MIT 6; * 7; * Permission is hereby granted, free of charge, to any person obtaining a copy of 8; * this software and associated documentation files (the "Software"), to deal in 9; * the Software without restriction, including without limitation the rights to 10; * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 11; * the Software, and to permit persons to whom the Software is furnished to do so, 12; * subject to the following conditions: 13; * 14; * The above copyright notice and this permission notice shall be included in all 15; * copies or substantial portions of the Software. 16; * 17; * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18; * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 19; * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 20; * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 21; * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 22; * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23; * 24; * https://www.FreeRTOS.org 25; * https://github.com/FreeRTOS 26; * 27; */ 28; Note: Select the correct include files for the device used by the application. 29#include "FreeRTOSConfig.h" 30;------------------------------------------------------------------------------ 31 32; Functions used by scheduler 33;------------------------------------------------------------------------------ 34 EXTERN vTaskSwitchContext 35 EXTERN xTaskIncrementTick 36 37; Variables used by scheduler 38;------------------------------------------------------------------------------ 39 EXTERN pxCurrentTCB 40 EXTERN usCriticalNesting 41 42; Functions implemented in this file 43;------------------------------------------------------------------------------ 44 PUBLIC vPortYield 45 PUBLIC vPortStart 46 47; Security ID definition 48;------------------------------------------------------------------------------ 49#define CG_SECURITY0 0FFH 50#define CG_SECURITY1 0FFH 51#define CG_SECURITY2 0FFH 52#define CG_SECURITY3 0FFH 53#define CG_SECURITY4 0FFH 54#define CG_SECURITY5 0FFH 55#define CG_SECURITY6 0FFH 56#define CG_SECURITY7 0FFH 57#define CG_SECURITY8 0FFH 58#define CG_SECURITY9 0FFH 59 60; Option Byte definitions 61;------------------------------------------------------------------------------ 62#define CG_OPTION7A 0x00 63#define CG_OPTION7B 0x04 64#define OPT7C 0x00 65#define OPT7D 0x00 66#define OPT7E 0x00 67#define OPT7F 0x00 68 69; Tick ISR Prototype 70;------------------------------------------------------------------------------ 71 PUBWEAK `??MD_INTTM0EQ0??INTVEC 608` 72 PUBLIC MD_INTTM0EQ0 73 74MD_INTTM0EQ0 SYMBOL "MD_INTTM0EQ0" 75`??MD_INTTM0EQ0??INTVEC 608` SYMBOL "??INTVEC 608", MD_INTTM0EQ0 76 77;------------------------------------------------------------------------------ 78; portSAVE_CONTEXT MACRO 79; Saves the context of the remaining general purpose registers 80; and the usCriticalNesting Value of the active Task onto the task stack 81; saves stack pointer to the TCB 82;------------------------------------------------------------------------------ 83portSAVE_CONTEXT MACRO 84#if configDATA_MODE == 1 ; Using the Tiny data model 85 prepare {r20,r21,r22,r23,r24,r25,r26,r27,r28,r29,r30},76,sp ; save general purpose registers 86 sst.w r19,72[ep] 87 sst.w r18,68[ep] 88 sst.w r17,64[ep] 89 sst.w r16,60[ep] 90 sst.w r15,56[ep] 91 sst.w r14,52[ep] 92 sst.w r13,48[ep] 93 sst.w r12,44[ep] 94 sst.w r11,40[ep] 95 sst.w r10,36[ep] 96 sst.w r9,32[ep] 97 sst.w r8,28[ep] 98 sst.w r7,24[ep] 99 sst.w r6,20[ep] 100 sst.w r5,16[ep] 101 sst.w r4,12[ep] 102#else ; Using the Small/Large data model 103 prepare {r20,r21,r22,r23,r24,r26,r27,r28,r29,r30},72,sp ; save general purpose registers 104 sst.w r19,68[ep] 105 sst.w r18,64[ep] 106 sst.w r17,60[ep] 107 sst.w r16,56[ep] 108 sst.w r15,52[ep] 109 sst.w r14,48[ep] 110 sst.w r13,44[ep] 111 sst.w r12,40[ep] 112 sst.w r11,36[ep] 113 sst.w r10,32[ep] 114 sst.w r9,28[ep] 115 sst.w r8,24[ep] 116 sst.w r7,20[ep] 117 sst.w r6,16[ep] 118 sst.w r5,12[ep] 119#endif /* configDATA_MODE */ 120 sst.w r2,8[ep] 121 sst.w r1,4[ep] 122 MOVHI hi1(usCriticalNesting),r0,r1 ; save usCriticalNesting value to stack 123 ld.w lw1(usCriticalNesting)[r1],r2 124 sst.w r2,0[ep] 125 MOVHI hi1(pxCurrentTCB),r0,r1 ; save SP to top of current TCB 126 ld.w lw1(pxCurrentTCB)[r1],r2 127 st.w sp,0[r2] 128 ENDM 129;------------------------------------------------------------------------------ 130 131;------------------------------------------------------------------------------ 132; portRESTORE_CONTEXT MACRO 133; Gets stack pointer from the current TCB 134; Restores the context of the usCriticalNesting value and general purpose 135; registers of the selected task from the task stack 136;------------------------------------------------------------------------------ 137portRESTORE_CONTEXT MACRO 138 MOVHI hi1(pxCurrentTCB),r0,r1 ; get Stackpointer address 139 ld.w lw1(pxCurrentTCB)[r1],sp 140 MOV sp,r1 141 ld.w 0[r1],sp ; load stackpointer 142 MOV sp,ep ; set stack pointer to element pointer 143 sld.w 0[ep],r1 ; load usCriticalNesting value from stack 144 MOVHI hi1(usCriticalNesting),r0,r2 145 st.w r1,lw1(usCriticalNesting)[r2] 146 sld.w 4[ep],r1 ; restore general purpose registers 147 sld.w 8[ep],r2 148#if configDATA_MODE == 1 ; Using Tiny data model 149 sld.w 12[ep],r4 150 sld.w 16[ep],r5 151 sld.w 20[ep],r6 152 sld.w 24[ep],r7 153 sld.w 28[ep],r8 154 sld.w 32[ep],r9 155 sld.w 36[ep],r10 156 sld.w 40[ep],r11 157 sld.w 44[ep],r12 158 sld.w 48[ep],r13 159 sld.w 52[ep],r14 160 sld.w 56[ep],r15 161 sld.w 60[ep],r16 162 sld.w 64[ep],r17 163 sld.w 68[ep],r18 164 sld.w 72[ep],r19 165 dispose 76,{r20,r21,r22,r23,r24,r25,r26,r27,r28,r29,r30} 166#else ; Using Small/Large data model 167 sld.w 12[ep],r5 168 sld.w 16[ep],r6 169 sld.w 20[ep],r7 170 sld.w 24[ep],r8 171 sld.w 28[ep],r9 172 sld.w 32[ep],r10 173 sld.w 36[ep],r11 174 sld.w 40[ep],r12 175 sld.w 44[ep],r13 176 sld.w 48[ep],r14 177 sld.w 52[ep],r15 178 sld.w 56[ep],r16 179 sld.w 60[ep],r17 180 sld.w 64[ep],r18 181 sld.w 68[ep],r19 182 dispose 72,{r20,r21,r22,r23,r24,r26,r27,r28,r29,r30} 183#endif /* configDATA_MODE */ 184 ENDM 185;------------------------------------------------------------------------------ 186 187;------------------------------------------------------------------------------ 188; Restore the context of the first task that is going to run. 189; 190; Input: NONE 191; 192; Call: CALL vPortStart 193; 194; Output: NONE 195;------------------------------------------------------------------------------ 196 RSEG CODE:CODE 197vPortStart: 198 portRESTORE_CONTEXT ; Restore the context of whichever task the ... 199 ld.w 0[sp],lp 200 ldsr lp,5 ; restore PSW 201 DI 202 ld.w 4[sp],lp ; restore LP 203 ld.w 8[sp],lp ; restore LP 204 ADD 0x0C,sp ; set SP to right position 205 EI 206 jmp [lp] 207;------------------------------------------------------------------------------ 208 209;------------------------------------------------------------------------------ 210; Port Yield function to check for a Task switch in the cooperative and 211; preemptive mode 212; 213; Input: NONE 214; 215; Call: CALL vPortYield 216; 217; Output: NONE 218;------------------------------------------------------------------------------ 219 220 RSEG CODE:CODE 221vPortYield: 222 223 add -0x0C,sp ; prepare stack to save necessary values 224 st.w lp,8[sp] ; store LP to stack 225 stsr 0,r31 226 st.w lp,4[sp] ; store EIPC to stack 227 stsr 1,lp 228 st.w lp,0[sp] ; store EIPSW to stack 229 portSAVE_CONTEXT ; Save the context of the current task. 230 jarl vTaskSwitchContext,lp ; Call the scheduler. 231 portRESTORE_CONTEXT ; Restore the context of whichever task the ... 232 ; ... scheduler decided should run. 233 ld.w 0[sp],lp ; restore EIPSW from stack 234 ldsr lp,1 235 ld.w 4[sp],lp ; restore EIPC from stack 236 ldsr lp,0 237 ld.w 8[sp],lp ; restore LP from stack 238 add 0x0C,sp ; set SP to right position 239 240 RETI 241 242;------------------------------------------------------------------------------ 243 244;------------------------------------------------------------------------------ 245; Perform the necessary steps of the Tick Count Increment and Task Switch 246; depending on the chosen kernel configuration 247; 248; Input: NONE 249; 250; Call: ISR 251; 252; Output: NONE 253;------------------------------------------------------------------------------ 254#if configUSE_PREEMPTION == 1 ; use preemptive kernel mode 255 256MD_INTTM0EQ0: 257 258 add -0x0C,sp ; prepare stack to save necessary values 259 st.w lp,8[sp] ; store LP to stack 260 stsr 0,r31 261 st.w lp,4[sp] ; store EIPC to stack 262 stsr 1,lp 263 st.w lp,0[sp] ; store EIPSW to stack 264 portSAVE_CONTEXT ; Save the context of the current task. 265 jarl xTaskIncrementTick,lp ; Call the timer tick function. 266 jarl vTaskSwitchContext,lp ; Call the scheduler. 267 portRESTORE_CONTEXT ; Restore the context of whichever task the ... 268 ; ... scheduler decided should run. 269 ld.w 0[sp],lp ; restore EIPSW from stack 270 ldsr lp,1 271 ld.w 4[sp],lp ; restore EIPC from stack 272 ldsr lp,0 273 ld.w 8[sp],lp ; restore LP from stack 274 add 0x0C,sp ; set SP to right position 275 276 RETI 277;------------------------------------------------------------------------------ 278#else ; use cooperative kernel mode 279 280MD_INTTM0EQ0: 281 prepare {lp,ep},8,sp 282 sst.w r1,4[ep] 283 sst.w r5,0[ep] 284 jarl xTaskIncrementTick,lp ; Call the timer tick function. 285 sld.w 0[ep],r5 286 sld.w 4[ep],r1 287 dispose 8,{lp,ep} 288 RETI 289#endif /* configUSE_PREEMPTION */ 290 291;------------------------------------------------------------------------------ 292 COMMON INTVEC:CODE:ROOT(2) 293 ORG 608 294`??MD_INTTM0EQ0??INTVEC 608`: 295 JR MD_INTTM0EQ0 296 297 RSEG NEAR_ID:CONST:SORT:NOROOT(2) 298`?<Initializer for usCriticalNesting>`: 299 DW 10 300 301 COMMON INTVEC:CODE:ROOT(2) 302 ORG 40H 303`??vPortYield??INTVEC 40`: 304 JR vPortYield 305 306;------------------------------------------------------------------------------ 307; set microcontroller security ID 308 309 COMMON INTVEC:CODE:ROOT(2) 310 ORG 70H 311`SECUID`: 312 DB CG_SECURITY0 313 DB CG_SECURITY1 314 DB CG_SECURITY2 315 DB CG_SECURITY3 316 DB CG_SECURITY4 317 DB CG_SECURITY5 318 DB CG_SECURITY6 319 DB CG_SECURITY7 320 DB CG_SECURITY8 321 DB CG_SECURITY9 322 323;------------------------------------------------------------------------------ 324; set microcontroller option bytes 325 326 COMMON INTVEC:CODE:ROOT(2) 327 ORG 7AH 328`OPTBYTES`: 329 DB CG_OPTION7A 330 DB CG_OPTION7B 331 DB OPT7C 332 DB OPT7D 333 DB OPT7E 334 DB OPT7F 335 336 END 337