2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 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. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 #include "FreeRTOS.h"
\r
32 /*-----------------------------------------------------------
\r
33 * Implementation of functions defined in portable.h for the 16FX port.
\r
34 *----------------------------------------------------------*/
\r
37 * Get current value of DPR and ADB registers
\r
39 StackType_t xGet_DPR_ADB_bank( void );
\r
42 * Get current value of DTB and PCB registers
\r
44 StackType_t xGet_DTB_PCB_bank( void );
\r
47 * Sets up the periodic ISR used for the RTOS tick. This uses RLT0, but
\r
48 * can be done using any given RLT.
\r
50 static void prvSetupRLT0Interrupt( void );
\r
52 /*-----------------------------------------------------------*/
\r
55 * We require the address of the pxCurrentTCB variable, but don't want to know
\r
56 * any details of its type.
\r
59 extern volatile TCB_t * volatile pxCurrentTCB;
\r
61 /*-----------------------------------------------------------*/
\r
64 * Macro to save a task context to the task stack. This macro copies the
\r
65 * saved context (AH:AL, DPR:ADB, DTB:PCB , PC and PS) from the system
\r
66 * stack to task stack pointed by user stack pointer ( USP for SMALL and
\r
67 * MEDIUM memory model amd USB:USP for COMPACT and LARGE memory model ),
\r
68 * then it pushes the general purpose registers RW0-RW7 on to the task
\r
69 * stack. Finally the resultant stack pointer value is saved into the
\r
70 * task control block so it can be retrieved the next time the task
\r
73 #if( ( configMEMMODEL == portSMALL ) || ( configMEMMODEL == portMEDIUM ) )
\r
75 #define portSAVE_CONTEXT() \
\r
76 { __asm(" POPW A "); \
\r
77 __asm(" AND CCR,#H'DF "); \
\r
78 __asm(" PUSHW A "); \
\r
79 __asm(" OR CCR,#H'20 "); \
\r
80 __asm(" POPW A "); \
\r
81 __asm(" AND CCR,#H'DF "); \
\r
82 __asm(" PUSHW A "); \
\r
83 __asm(" OR CCR,#H'20 "); \
\r
84 __asm(" POPW A "); \
\r
85 __asm(" AND CCR,#H'DF "); \
\r
86 __asm(" PUSHW A "); \
\r
87 __asm(" OR CCR,#H'20 "); \
\r
88 __asm(" POPW A "); \
\r
89 __asm(" AND CCR,#H'DF "); \
\r
90 __asm(" PUSHW A "); \
\r
91 __asm(" OR CCR,#H'20 "); \
\r
92 __asm(" POPW A "); \
\r
93 __asm(" AND CCR,#H'DF "); \
\r
94 __asm(" PUSHW A "); \
\r
95 __asm(" OR CCR,#H'20 "); \
\r
96 __asm(" POPW A "); \
\r
97 __asm(" AND CCR,#H'DF "); \
\r
98 __asm(" PUSHW A "); \
\r
99 __asm(" PUSHW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
100 __asm(" MOVW A, _pxCurrentTCB "); \
\r
101 __asm(" MOVW A, SP "); \
\r
102 __asm(" SWAPW "); \
\r
103 __asm(" MOVW @AL, AH "); \
\r
104 __asm(" OR CCR,#H'20 "); \
\r
108 * Macro to restore a task context from the task stack. This is effecti-
\r
109 * vely the reverse of SAVE_CONTEXT(). First the stack pointer value
\r
110 * (USP for SMALL and MEDIUM memory model amd USB:USP for COMPACT and
\r
111 * LARGE memory model ) is loaded from the task control block. Next the
\r
112 * value of all the general purpose registers RW0-RW7 is retrieved. Fina-
\r
113 * lly it copies of the context ( AH:AL, DPR:ADB, DTB:PCB, PC and PS) of
\r
114 * the task to be executed upon RETI from user stack to system stack.
\r
117 #define portRESTORE_CONTEXT() \
\r
118 { __asm(" MOVW A, _pxCurrentTCB "); \
\r
119 __asm(" MOVW A, @A "); \
\r
120 __asm(" AND CCR,#H'DF "); \
\r
121 __asm(" MOVW SP, A "); \
\r
122 __asm(" POPW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
123 __asm(" POPW A "); \
\r
124 __asm(" OR CCR,#H'20 "); \
\r
125 __asm(" PUSHW A "); \
\r
126 __asm(" AND CCR,#H'DF "); \
\r
127 __asm(" POPW A "); \
\r
128 __asm(" OR CCR,#H'20 "); \
\r
129 __asm(" PUSHW A "); \
\r
130 __asm(" AND CCR,#H'DF "); \
\r
131 __asm(" POPW A "); \
\r
132 __asm(" OR CCR,#H'20 "); \
\r
133 __asm(" PUSHW A "); \
\r
134 __asm(" AND CCR,#H'DF "); \
\r
135 __asm(" POPW A "); \
\r
136 __asm(" OR CCR,#H'20 "); \
\r
137 __asm(" PUSHW A "); \
\r
138 __asm(" AND CCR,#H'DF "); \
\r
139 __asm(" POPW A "); \
\r
140 __asm(" OR CCR,#H'20 "); \
\r
141 __asm(" PUSHW A "); \
\r
142 __asm(" AND CCR,#H'DF "); \
\r
143 __asm(" POPW A "); \
\r
144 __asm(" OR CCR,#H'20 "); \
\r
145 __asm(" PUSHW A "); \
\r
148 #elif( ( configMEMMODEL == portCOMPACT ) || ( configMEMMODEL == portLARGE ) )
\r
150 #define portSAVE_CONTEXT() \
\r
151 { __asm(" POPW A "); \
\r
152 __asm(" AND CCR,#H'DF "); \
\r
153 __asm(" PUSHW A "); \
\r
154 __asm(" OR CCR,#H'20 "); \
\r
155 __asm(" POPW A "); \
\r
156 __asm(" AND CCR,#H'DF "); \
\r
157 __asm(" PUSHW A "); \
\r
158 __asm(" OR CCR,#H'20 "); \
\r
159 __asm(" POPW A "); \
\r
160 __asm(" AND CCR,#H'DF "); \
\r
161 __asm(" PUSHW A "); \
\r
162 __asm(" OR CCR,#H'20 "); \
\r
163 __asm(" POPW A "); \
\r
164 __asm(" AND CCR,#H'DF "); \
\r
165 __asm(" PUSHW A "); \
\r
166 __asm(" OR CCR,#H'20 "); \
\r
167 __asm(" POPW A "); \
\r
168 __asm(" AND CCR,#H'DF "); \
\r
169 __asm(" PUSHW A "); \
\r
170 __asm(" OR CCR,#H'20 "); \
\r
171 __asm(" POPW A "); \
\r
172 __asm(" AND CCR,#H'DF "); \
\r
173 __asm(" PUSHW A "); \
\r
174 __asm(" PUSHW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
175 __asm(" MOVL A, _pxCurrentTCB "); \
\r
176 __asm(" MOVL RL2, A "); \
\r
177 __asm(" MOVW A, SP "); \
\r
178 __asm(" MOVW @RL2+0, A "); \
\r
179 __asm(" MOV A, USB "); \
\r
180 __asm(" MOV @RL2+2, A "); \
\r
183 #define portRESTORE_CONTEXT() \
\r
184 { __asm(" MOVL A, _pxCurrentTCB "); \
\r
185 __asm(" MOVL RL2, A "); \
\r
186 __asm(" MOVW A, @RL2+0 "); \
\r
187 __asm(" AND CCR,#H'DF "); \
\r
188 __asm(" MOVW SP, A "); \
\r
189 __asm(" MOV A, @RL2+2 "); \
\r
190 __asm(" MOV USB, A "); \
\r
191 __asm(" POPW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
192 __asm(" POPW A "); \
\r
193 __asm(" OR CCR,#H'20 "); \
\r
194 __asm(" PUSHW A "); \
\r
195 __asm(" AND CCR,#H'DF "); \
\r
196 __asm(" POPW A "); \
\r
197 __asm(" OR CCR,#H'20 "); \
\r
198 __asm(" PUSHW A "); \
\r
199 __asm(" AND CCR,#H'DF "); \
\r
200 __asm(" POPW A "); \
\r
201 __asm(" OR CCR,#H'20 "); \
\r
202 __asm(" PUSHW A "); \
\r
203 __asm(" AND CCR,#H'DF "); \
\r
204 __asm(" POPW A "); \
\r
205 __asm(" OR CCR,#H'20 "); \
\r
206 __asm(" PUSHW A "); \
\r
207 __asm(" AND CCR,#H'DF "); \
\r
208 __asm(" POPW A "); \
\r
209 __asm(" OR CCR,#H'20 "); \
\r
210 __asm(" PUSHW A "); \
\r
211 __asm(" AND CCR,#H'DF "); \
\r
212 __asm(" POPW A "); \
\r
213 __asm(" OR CCR,#H'20 "); \
\r
214 __asm(" PUSHW A "); \
\r
218 /*-----------------------------------------------------------*/
\r
221 * Functions for obtaining the current value of DPR:ADB, DTB:PCB bank registers
\r
226 .GLOBAL _xGet_DPR_ADB_bank
\r
227 .GLOBAL _xGet_DTB_PCB_bank
\r
228 .SECTION CODE, CODE, ALIGN=1
\r
230 _xGet_DPR_ADB_bank:
\r
236 #if configMEMMODEL == portMEDIUM || configMEMMODEL == portLARGE
\r
238 #elif configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
243 _xGet_DTB_PCB_bank:
\r
249 #if configMEMMODEL == portMEDIUM || configMEMMODEL == portLARGE
\r
251 #elif configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
256 /*-----------------------------------------------------------*/
\r
259 * Initialise the stack of a task to look exactly as if a call to
\r
260 * portSAVE_CONTEXT had been called.
\r
262 * See the header file portable.h.
\r
264 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
266 /* Place a few bytes of known values on the bottom of the stack.
\r
267 This is just useful for debugging. */
\r
268 *pxTopOfStack = 0x1111;
\r
270 *pxTopOfStack = 0x2222;
\r
272 *pxTopOfStack = 0x3333;
\r
275 /* Once the task is called the task would push the pointer to the
\r
276 parameter onto the stack. Hence here the pointer would be copied to the stack
\r
277 first. When using the COMPACT or LARGE memory model the pointer would be 24
\r
278 bits, and when using the SMALL or MEDIUM memory model the pointer would be 16
\r
280 #if( ( configMEMMODEL == portCOMPACT ) || ( configMEMMODEL == portLARGE ) )
\r
282 *pxTopOfStack = ( StackType_t ) ( ( uint32_t ) ( pvParameters ) >> 16 );
\r
287 *pxTopOfStack = ( StackType_t ) ( pvParameters );
\r
290 /* This is redundant push to the stack. This is required in order to introduce
\r
291 an offset so that the task accesses a parameter correctly that is passed on to
\r
293 #if( ( configMEMMODEL == portMEDIUM ) || ( configMEMMODEL == portLARGE ) )
\r
295 *pxTopOfStack = ( xGet_DTB_PCB_bank() & 0xff00 ) | ( ( ( int32_t ) ( pxCode ) >> 16 ) & 0xff );
\r
300 /* This is redundant push to the stack. This is required in order to introduce
\r
301 an offset so the task correctly accesses the parameter passed on the task stack. */
\r
302 *pxTopOfStack = ( StackType_t ) ( pxCode );
\r
305 /* PS - User Mode, ILM=7, RB=0, Interrupts enabled,USP */
\r
306 *pxTopOfStack = 0xE0C0;
\r
310 *pxTopOfStack = ( StackType_t ) ( pxCode );
\r
314 #if configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
316 *pxTopOfStack = xGet_DTB_PCB_bank();
\r
321 /* DTB | PCB, in case of MEDIUM and LARGE memory models, PCB would be used
\r
322 along with PC to indicate the start address of the function. */
\r
323 #if( ( configMEMMODEL == portMEDIUM ) || ( configMEMMODEL == portLARGE ) )
\r
325 *pxTopOfStack = ( xGet_DTB_PCB_bank() & 0xff00 ) | ( ( ( int32_t ) ( pxCode ) >> 16 ) & 0xff );
\r
331 *pxTopOfStack = xGet_DPR_ADB_bank();
\r
335 *pxTopOfStack = ( StackType_t ) 0x9999;
\r
339 *pxTopOfStack = ( StackType_t ) 0xAAAA;
\r
342 /* Next the general purpose registers. */
\r
343 *pxTopOfStack = ( StackType_t ) 0x7777; /* RW7 */
\r
345 *pxTopOfStack = ( StackType_t ) 0x6666; /* RW6 */
\r
347 *pxTopOfStack = ( StackType_t ) 0x5555; /* RW5 */
\r
349 *pxTopOfStack = ( StackType_t ) 0x4444; /* RW4 */
\r
351 *pxTopOfStack = ( StackType_t ) 0x3333; /* RW3 */
\r
353 *pxTopOfStack = ( StackType_t ) 0x2222; /* RW2 */
\r
355 *pxTopOfStack = ( StackType_t ) 0x1111; /* RW1 */
\r
357 *pxTopOfStack = ( StackType_t ) 0x8888; /* RW0 */
\r
359 return pxTopOfStack;
\r
361 /*-----------------------------------------------------------*/
\r
363 static void prvSetupRLT0Interrupt( void )
\r
365 /* The peripheral clock divided by 16 is used by the timer. */
\r
366 const uint16_t usReloadValue = ( uint16_t ) ( ( ( configCLKP1_CLOCK_HZ / configTICK_RATE_HZ ) / 16UL ) - 1UL );
\r
368 /* set reload value = 34999+1, TICK Interrupt after 10 ms @ 56MHz of CLKP1 */
\r
369 TMRLR0 = usReloadValue;
\r
371 /* prescaler 1:16, reload, interrupt enable, count enable, trigger */
\r
374 /*-----------------------------------------------------------*/
\r
376 BaseType_t xPortStartScheduler( void )
\r
378 /* Setup the hardware to generate the tick. */
\r
379 prvSetupRLT0Interrupt();
\r
381 /* Restore the context of the first task that is going to run. */
\r
382 portRESTORE_CONTEXT();
\r
384 /* Simulate a function call end as generated by the compiler. We will now
\r
385 jump to the start of the task the context of which we have just restored. */
\r
389 /* Should not get here. */
\r
392 /*-----------------------------------------------------------*/
\r
394 void vPortEndScheduler( void )
\r
396 /* Not implemented - unlikely to ever be required as there is nothing to
\r
400 /*-----------------------------------------------------------*/
\r
403 * The interrupt service routine used depends on whether the pre-emptive
\r
404 * scheduler is being used or not.
\r
407 #if configUSE_PREEMPTION == 1
\r
410 * Tick ISR for preemptive scheduler. We can use a __nosavereg attribute
\r
411 * as the context is to be saved by the portSAVE_CONTEXT() macro, not the
\r
412 * compiler generated code. The tick count is incremented after the context
\r
415 __nosavereg __interrupt void prvRLT0_TICKISR( void )
\r
417 /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
\r
420 /* Save the context of the interrupted task. */
\r
421 portSAVE_CONTEXT();
\r
423 /* Enable interrupts */
\r
426 /* Clear RLT0 interrupt flag */
\r
429 /* Increment the tick count then switch to the highest priority task
\r
430 that is ready to run. */
\r
431 if( xTaskIncrementTick() != pdFALSE )
\r
433 vTaskSwitchContext();
\r
436 /* Disable interrupts so that portRESTORE_CONTEXT() is not interrupted */
\r
439 /* Restore the context of the new task. */
\r
440 portRESTORE_CONTEXT();
\r
442 /* Enable interrupts */
\r
449 * Tick ISR for the cooperative scheduler. All this does is increment the
\r
450 * tick count. We don't need to switch context, this can only be done by
\r
451 * manual calls to taskYIELD();
\r
453 __interrupt void prvRLT0_TICKISR( void )
\r
455 /* Clear RLT0 interrupt flag */
\r
458 xTaskIncrementTick();
\r
463 /*-----------------------------------------------------------*/
\r
466 * Manual context switch. We can use a __nosavereg attribute as the context
\r
467 * is to be saved by the portSAVE_CONTEXT() macro, not the compiler generated
\r
470 __nosavereg __interrupt void vPortYield( void )
\r
472 /* Save the context of the interrupted task. */
\r
473 portSAVE_CONTEXT();
\r
475 /* Switch to the highest priority task that is ready to run. */
\r
476 vTaskSwitchContext();
\r
478 /* Restore the context of the new task. */
\r
479 portRESTORE_CONTEXT();
\r
481 /*-----------------------------------------------------------*/
\r
483 __nosavereg __interrupt void vPortYieldDelayed( void )
\r
485 /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
\r
488 /* Save the context of the interrupted task. */
\r
489 portSAVE_CONTEXT();
\r
491 /* Enable interrupts */
\r
494 /* Clear delayed interrupt flag */
\r
495 __asm (" CLRB 03A4H:0 ");
\r
497 /* Switch to the highest priority task that is ready to run. */
\r
498 vTaskSwitchContext();
\r
500 /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
\r
503 /* Restore the context of the new task. */
\r
504 portRESTORE_CONTEXT();
\r
506 /* Enable interrupts */
\r
509 /*-----------------------------------------------------------*/
\r