2 * FreeRTOS Kernel V10.1.1
\r
3 * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 #include "FreeRTOS.h"
\r
30 #include "mb91467d.h"
\r
32 /*-----------------------------------------------------------*/
\r
34 /* We require the address of the pxCurrentTCB variable, but don't want to know
\r
35 any details of its type. */
\r
37 extern volatile TCB_t * volatile pxCurrentTCB;
\r
39 /*-----------------------------------------------------------*/
\r
43 ORCCR #0x20 ;Switch to user stack
\r
44 ST RP,@-R15 ;Store RP
\r
45 STM0 (R7,R6,R5,R4,R3,R2,R1,R0) ;Store R7-R0
\r
46 STM1 (R14,R13,R12,R11,R10,R9,R8) ;Store R14-R8
\r
47 ST MDH, @-R15 ;Store MDH
\r
48 ST MDL, @-R15 ;Store MDL
\r
50 ANDCCR #0xDF ;Switch back to system stack
\r
51 LD @R15+,R0 ;Store PC to R0
\r
52 ORCCR #0x20 ;Switch to user stack
\r
53 ST R0,@-R15 ;Store PC to User stack
\r
55 ANDCCR #0xDF ;Switch back to system stack
\r
56 LD @R15+,R0 ;Store PS to R0
\r
57 ORCCR #0x20 ;Switch to user stack
\r
58 ST R0,@-R15 ;Store PS to User stack
\r
60 LDI #_pxCurrentTCB, R0 ;Get pxCurrentTCB address
\r
61 LD @R0, R0 ;Get the pxCurrentTCB->pxTopOfStack address
\r
62 ST R15,@R0 ;Store USP to pxCurrentTCB->pxTopOfStack
\r
64 ANDCCR #0xDF ;Switch back to system stack for the rest of tick ISR
\r
67 #macro RestoreContext
\r
68 LDI #_pxCurrentTCB, R0 ;Get pxCurrentTCB address
\r
69 LD @R0, R0 ;Get the pxCurrentTCB->pxTopOfStack address
\r
70 ORCCR #0x20 ;Switch to user stack
\r
71 LD @R0, R15 ;Restore USP from pxCurrentTCB->pxTopOfStack
\r
73 LD @R15+,R0 ;Store PS to R0
\r
74 ANDCCR #0xDF ;Switch to system stack
\r
75 ST R0,@-R15 ;Store PS to system stack
\r
77 ORCCR #0x20 ;Switch to user stack
\r
78 LD @R15+,R0 ;Store PC to R0
\r
79 ANDCCR #0xDF ;Switch to system stack
\r
80 ST R0,@-R15 ;Store PC to system stack
\r
82 ORCCR #0x20 ;Switch back to retrieve the remaining context
\r
84 LD @R15+, MDL ;Restore MDL
\r
85 LD @R15+, MDH ;Restore MDH
\r
86 LDM1 (R14,R13,R12,R11,R10,R9,R8) ;Restore R14-R8
\r
87 LDM0 (R7,R6,R5,R4,R3,R2,R1,R0) ;Restore R7-R0
\r
88 LD @R15+, RP ;Restore RP
\r
90 ANDCCR #0xDF ;Switch back to system stack for the rest of tick ISR
\r
94 /*-----------------------------------------------------------*/
\r
97 * Perform hardware setup to enable ticks from timer 1,
\r
99 static void prvSetupTimerInterrupt( void );
\r
100 /*-----------------------------------------------------------*/
\r
103 * Initialise the stack of a task to look exactly as if a call to
\r
104 * portSAVE_CONTEXT had been called.
\r
106 * See the header file portable.h.
\r
108 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
110 /* Place a few bytes of known values on the bottom of the stack.
\r
111 This is just useful for debugging. */
\r
113 *pxTopOfStack = 0x11111111;
\r
115 *pxTopOfStack = 0x22222222;
\r
117 *pxTopOfStack = 0x33333333;
\r
120 /* This is a redundant push to the stack, it may be required if
\r
121 in some implementations of the compiler the parameter to the task
\r
122 is passed on to the stack rather than in R4 register. */
\r
123 *pxTopOfStack = (StackType_t)(pvParameters);
\r
126 *pxTopOfStack = ( StackType_t ) 0x00000000; /* RP */
\r
128 *pxTopOfStack = ( StackType_t ) 0x00007777; /* R7 */
\r
130 *pxTopOfStack = ( StackType_t ) 0x00006666; /* R6 */
\r
132 *pxTopOfStack = ( StackType_t ) 0x00005555; /* R5 */
\r
135 /* In the current implementation of the compiler the first
\r
136 parameter to the task (or function) is passed via R4 parameter
\r
137 to the task, hence the pvParameters pointer is copied into the R4
\r
138 register. See compiler manual section 4.6.2 for more information. */
\r
139 *pxTopOfStack = ( StackType_t ) (pvParameters); /* R4 */
\r
141 *pxTopOfStack = ( StackType_t ) 0x00003333; /* R3 */
\r
143 *pxTopOfStack = ( StackType_t ) 0x00002222; /* R2 */
\r
145 *pxTopOfStack = ( StackType_t ) 0x00001111; /* R1 */
\r
147 *pxTopOfStack = ( StackType_t ) 0x00000001; /* R0 */
\r
149 *pxTopOfStack = ( StackType_t ) 0x0000EEEE; /* R14 */
\r
151 *pxTopOfStack = ( StackType_t ) 0x0000DDDD; /* R13 */
\r
153 *pxTopOfStack = ( StackType_t ) 0x0000CCCC; /* R12 */
\r
155 *pxTopOfStack = ( StackType_t ) 0x0000BBBB; /* R11 */
\r
157 *pxTopOfStack = ( StackType_t ) 0x0000AAAA; /* R10 */
\r
159 *pxTopOfStack = ( StackType_t ) 0x00009999; /* R9 */
\r
161 *pxTopOfStack = ( StackType_t ) 0x00008888; /* R8 */
\r
163 *pxTopOfStack = ( StackType_t ) 0x11110000; /* MDH */
\r
165 *pxTopOfStack = ( StackType_t ) 0x22220000; /* MDL */
\r
168 /* The start of the task code. */
\r
169 *pxTopOfStack = ( StackType_t ) pxCode; /* PC */
\r
172 /* PS - User Mode, USP, ILM=31, Interrupts enabled */
\r
173 *pxTopOfStack = ( StackType_t ) 0x001F0030; /* PS */
\r
175 return pxTopOfStack;
\r
177 /*-----------------------------------------------------------*/
\r
179 BaseType_t xPortStartScheduler( void )
\r
181 /* Setup the hardware to generate the tick. */
\r
182 prvSetupTimerInterrupt();
\r
184 /* Restore the context of the first task that is going to run. */
\r
189 /* Simulate a function call end as generated by the compiler. We will now
\r
190 jump to the start of the task the context of which we have just restored. */
\r
193 /* Should not get here. */
\r
196 /*-----------------------------------------------------------*/
\r
198 void vPortEndScheduler( void )
\r
200 /* Not implemented - unlikely to ever be required as there is nothing to
\r
203 /*-----------------------------------------------------------*/
\r
205 static void prvSetupTimerInterrupt( void )
\r
207 /* The peripheral clock divided by 32 is used by the timer. */
\r
208 const uint16_t usReloadValue = ( uint16_t ) ( ( ( configPER_CLOCK_HZ / configTICK_RATE_HZ ) / 32UL ) - 1UL );
\r
210 /* Setup RLT0 to generate a tick interrupt. */
\r
212 TMCSR0_CNTE = 0; /* Count Disable */
\r
213 TMCSR0_CSL = 0x2; /* CLKP/32 */
\r
214 TMCSR0_MOD = 0; /* Software trigger */
\r
215 TMCSR0_RELD = 1; /* Reload */
\r
217 TMCSR0_UF = 0; /* Clear underflow flag */
\r
218 TMRLR0 = usReloadValue;
\r
219 TMCSR0_INTE = 1; /* Interrupt Enable */
\r
220 TMCSR0_CNTE = 1; /* Count Enable */
\r
221 TMCSR0_TRG = 1; /* Trigger */
\r
223 PORTEN = 0x3; /* Port Enable */
\r
225 /*-----------------------------------------------------------*/
\r
227 #if configUSE_PREEMPTION == 1
\r
230 * Tick ISR for preemptive scheduler. The tick count is incremented
\r
231 * after the context is saved. Then the context is switched if required,
\r
232 * and last the context of the task which is to be resumed is restored.
\r
237 .global _ReloadTimer0_IRQHandler
\r
238 _ReloadTimer0_IRQHandler:
\r
240 ANDCCR #0xEF ;Disable Interrupts
\r
241 SaveContext ;Save context
\r
242 ORCCR #0x10 ;Re-enable Interrupts
\r
246 AND R1,@R0 ;Clear RLT0 interrupt flag
\r
248 CALL32 _xTaskIncrementTick,R12 ;Increment Tick
\r
249 CALL32 _vTaskSwitchContext,R12 ;Switch context if required
\r
251 ANDCCR #0xEF ;Disable Interrupts
\r
252 RestoreContext ;Restore context
\r
253 ORCCR #0x10 ;Re-enable Interrupts
\r
262 * Tick ISR for the cooperative scheduler. All this does is increment the
\r
263 * tick count. We don't need to switch context, this can only be done by
\r
264 * manual calls to taskYIELD();
\r
266 __interrupt void ReloadTimer0_IRQHandler( void )
\r
268 /* Clear RLT0 interrupt flag */
\r
270 xTaskIncrementTick();
\r
276 * Manual context switch. We can use a __nosavereg attribute as the context
\r
277 * would be saved by PortSAVE_CONTEXT(). The context is switched and then
\r
278 * the context of the new task is restored saved.
\r
282 .global _vPortYieldDelayed
\r
283 _vPortYieldDelayed:
\r
285 ANDCCR #0xEF ;Disable Interrupts
\r
286 SaveContext ;Save context
\r
287 ORCCR #0x10 ;Re-enable Interrupts
\r
290 BANDL #0x0E, @R0 ;Clear Delayed interrupt flag
\r
292 CALL32 _vTaskSwitchContext,R12 ;Switch context if required
\r
294 ANDCCR #0xEF ;Disable Interrupts
\r
295 RestoreContext ;Restore context
\r
296 ORCCR #0x10 ;Re-enable Interrupts
\r
301 /*-----------------------------------------------------------*/
\r
304 * Manual context switch. We can use a __nosavereg attribute as the context
\r
305 * would be saved by PortSAVE_CONTEXT(). The context is switched and then
\r
306 * the context of the new task is restored saved.
\r
310 .global _vPortYield
\r
313 SaveContext ;Save context
\r
314 CALL32 _vTaskSwitchContext,R12 ;Switch context if required
\r
315 RestoreContext ;Restore context
\r
320 /*-----------------------------------------------------------*/
\r