2 * FreeRTOS Kernel V10.6.2
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
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:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
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.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
33 /*-----------------------------------------------------------*/
35 /* We require the address of the pxCurrentTCB variable, but don't want to know
36 any details of its type. */
38 extern volatile TCB_t * volatile pxCurrentTCB;
40 /*-----------------------------------------------------------*/
44 ORCCR #0x20 ;Switch to user stack
46 STM0 (R7,R6,R5,R4,R3,R2,R1,R0) ;Store R7-R0
47 STM1 (R14,R13,R12,R11,R10,R9,R8) ;Store R14-R8
48 ST MDH, @-R15 ;Store MDH
49 ST MDL, @-R15 ;Store MDL
51 ANDCCR #0xDF ;Switch back to system stack
52 LD @R15+,R0 ;Store PC to R0
53 ORCCR #0x20 ;Switch to user stack
54 ST R0,@-R15 ;Store PC to User stack
56 ANDCCR #0xDF ;Switch back to system stack
57 LD @R15+,R0 ;Store PS to R0
58 ORCCR #0x20 ;Switch to user stack
59 ST R0,@-R15 ;Store PS to User stack
61 LDI #_pxCurrentTCB, R0 ;Get pxCurrentTCB address
62 LD @R0, R0 ;Get the pxCurrentTCB->pxTopOfStack address
63 ST R15,@R0 ;Store USP to pxCurrentTCB->pxTopOfStack
65 ANDCCR #0xDF ;Switch back to system stack for the rest of tick ISR
69 LDI #_pxCurrentTCB, R0 ;Get pxCurrentTCB address
70 LD @R0, R0 ;Get the pxCurrentTCB->pxTopOfStack address
71 ORCCR #0x20 ;Switch to user stack
72 LD @R0, R15 ;Restore USP from pxCurrentTCB->pxTopOfStack
74 LD @R15+,R0 ;Store PS to R0
75 ANDCCR #0xDF ;Switch to system stack
76 ST R0,@-R15 ;Store PS to system stack
78 ORCCR #0x20 ;Switch to user stack
79 LD @R15+,R0 ;Store PC to R0
80 ANDCCR #0xDF ;Switch to system stack
81 ST R0,@-R15 ;Store PC to system stack
83 ORCCR #0x20 ;Switch back to retrieve the remaining context
85 LD @R15+, MDL ;Restore MDL
86 LD @R15+, MDH ;Restore MDH
87 LDM1 (R14,R13,R12,R11,R10,R9,R8) ;Restore R14-R8
88 LDM0 (R7,R6,R5,R4,R3,R2,R1,R0) ;Restore R7-R0
89 LD @R15+, RP ;Restore RP
91 ANDCCR #0xDF ;Switch back to system stack for the rest of tick ISR
95 /*-----------------------------------------------------------*/
98 * Perform hardware setup to enable ticks from timer 1,
100 static void prvSetupTimerInterrupt( void );
101 /*-----------------------------------------------------------*/
104 * Initialise the stack of a task to look exactly as if a call to
105 * portSAVE_CONTEXT had been called.
107 * See the header file portable.h.
109 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
111 /* Place a few bytes of known values on the bottom of the stack.
112 This is just useful for debugging. */
114 *pxTopOfStack = 0x11111111;
116 *pxTopOfStack = 0x22222222;
118 *pxTopOfStack = 0x33333333;
121 /* This is a redundant push to the stack, it may be required if
122 in some implementations of the compiler the parameter to the task
123 is passed on to the stack rather than in R4 register. */
124 *pxTopOfStack = (StackType_t)(pvParameters);
127 *pxTopOfStack = ( StackType_t ) 0x00000000; /* RP */
129 *pxTopOfStack = ( StackType_t ) 0x00007777; /* R7 */
131 *pxTopOfStack = ( StackType_t ) 0x00006666; /* R6 */
133 *pxTopOfStack = ( StackType_t ) 0x00005555; /* R5 */
136 /* In the current implementation of the compiler the first
137 parameter to the task (or function) is passed via R4 parameter
138 to the task, hence the pvParameters pointer is copied into the R4
139 register. See compiler manual section 4.6.2 for more information. */
140 *pxTopOfStack = ( StackType_t ) (pvParameters); /* R4 */
142 *pxTopOfStack = ( StackType_t ) 0x00003333; /* R3 */
144 *pxTopOfStack = ( StackType_t ) 0x00002222; /* R2 */
146 *pxTopOfStack = ( StackType_t ) 0x00001111; /* R1 */
148 *pxTopOfStack = ( StackType_t ) 0x00000001; /* R0 */
150 *pxTopOfStack = ( StackType_t ) 0x0000EEEE; /* R14 */
152 *pxTopOfStack = ( StackType_t ) 0x0000DDDD; /* R13 */
154 *pxTopOfStack = ( StackType_t ) 0x0000CCCC; /* R12 */
156 *pxTopOfStack = ( StackType_t ) 0x0000BBBB; /* R11 */
158 *pxTopOfStack = ( StackType_t ) 0x0000AAAA; /* R10 */
160 *pxTopOfStack = ( StackType_t ) 0x00009999; /* R9 */
162 *pxTopOfStack = ( StackType_t ) 0x00008888; /* R8 */
164 *pxTopOfStack = ( StackType_t ) 0x11110000; /* MDH */
166 *pxTopOfStack = ( StackType_t ) 0x22220000; /* MDL */
169 /* The start of the task code. */
170 *pxTopOfStack = ( StackType_t ) pxCode; /* PC */
173 /* PS - User Mode, USP, ILM=31, Interrupts enabled */
174 *pxTopOfStack = ( StackType_t ) 0x001F0030; /* PS */
178 /*-----------------------------------------------------------*/
180 BaseType_t xPortStartScheduler( void )
182 /* Setup the hardware to generate the tick. */
183 prvSetupTimerInterrupt();
185 /* Restore the context of the first task that is going to run. */
190 /* Simulate a function call end as generated by the compiler. We will now
191 jump to the start of the task the context of which we have just restored. */
194 /* Should not get here. */
197 /*-----------------------------------------------------------*/
199 void vPortEndScheduler( void )
201 /* Not implemented - unlikely to ever be required as there is nothing to
204 /*-----------------------------------------------------------*/
206 static void prvSetupTimerInterrupt( void )
208 /* The peripheral clock divided by 32 is used by the timer. */
209 const uint16_t usReloadValue = ( uint16_t ) ( ( ( configPER_CLOCK_HZ / configTICK_RATE_HZ ) / 32UL ) - 1UL );
211 /* Setup RLT0 to generate a tick interrupt. */
213 TMCSR0_CNTE = 0; /* Count Disable */
214 TMCSR0_CSL = 0x2; /* CLKP/32 */
215 TMCSR0_MOD = 0; /* Software trigger */
216 TMCSR0_RELD = 1; /* Reload */
218 TMCSR0_UF = 0; /* Clear underflow flag */
219 TMRLR0 = usReloadValue;
220 TMCSR0_INTE = 1; /* Interrupt Enable */
221 TMCSR0_CNTE = 1; /* Count Enable */
222 TMCSR0_TRG = 1; /* Trigger */
224 PORTEN = 0x3; /* Port Enable */
226 /*-----------------------------------------------------------*/
228 #if configUSE_PREEMPTION == 1
231 * Tick ISR for preemptive scheduler. The tick count is incremented
232 * after the context is saved. Then the context is switched if required,
233 * and last the context of the task which is to be resumed is restored.
238 .global _ReloadTimer0_IRQHandler
239 _ReloadTimer0_IRQHandler:
241 ANDCCR #0xEF ;Disable Interrupts
242 SaveContext ;Save context
243 ORCCR #0x10 ;Re-enable Interrupts
247 AND R1,@R0 ;Clear RLT0 interrupt flag
249 CALL32 _xTaskIncrementTick,R12 ;Increment Tick
250 CALL32 _vTaskSwitchContext,R12 ;Switch context if required
252 ANDCCR #0xEF ;Disable Interrupts
253 RestoreContext ;Restore context
254 ORCCR #0x10 ;Re-enable Interrupts
263 * Tick ISR for the cooperative scheduler. All this does is increment the
264 * tick count. We don't need to switch context, this can only be done by
265 * manual calls to taskYIELD();
267 __interrupt void ReloadTimer0_IRQHandler( void )
269 /* Clear RLT0 interrupt flag */
271 xTaskIncrementTick();
277 * Manual context switch. We can use a __nosavereg attribute as the context
278 * would be saved by PortSAVE_CONTEXT(). The context is switched and then
279 * the context of the new task is restored saved.
283 .global _vPortYieldDelayed
286 ANDCCR #0xEF ;Disable Interrupts
287 SaveContext ;Save context
288 ORCCR #0x10 ;Re-enable Interrupts
291 BANDL #0x0E, @R0 ;Clear Delayed interrupt flag
293 CALL32 _vTaskSwitchContext,R12 ;Switch context if required
295 ANDCCR #0xEF ;Disable Interrupts
296 RestoreContext ;Restore context
297 ORCCR #0x10 ;Re-enable Interrupts
302 /*-----------------------------------------------------------*/
305 * Manual context switch. We can use a __nosavereg attribute as the context
306 * would be saved by PortSAVE_CONTEXT(). The context is switched and then
307 * the context of the new task is restored saved.
314 SaveContext ;Save context
315 CALL32 _vTaskSwitchContext,R12 ;Switch context if required
316 RestoreContext ;Restore context
321 /*-----------------------------------------------------------*/