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