2 * FreeRTOS Kernel V10.1.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.
\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
31 /*-----------------------------------------------------------
\r
32 * Implementation of functions defined in portable.h for the 16FX port.
\r
33 *----------------------------------------------------------*/
\r
36 * Get current value of DPR and ADB registers
\r
38 StackType_t xGet_DPR_ADB_bank( void );
\r
41 * Get current value of DTB and PCB registers
\r
43 StackType_t xGet_DTB_PCB_bank( void );
\r
46 * Sets up the periodic ISR used for the RTOS tick. This uses RLT0, but
\r
47 * can be done using any given RLT.
\r
49 static void prvSetupRLT0Interrupt( void );
\r
51 /*-----------------------------------------------------------*/
\r
54 * We require the address of the pxCurrentTCB variable, but don't want to know
\r
55 * any details of its type.
\r
58 extern volatile TCB_t * volatile pxCurrentTCB;
\r
60 /*-----------------------------------------------------------*/
\r
63 * Macro to save a task context to the task stack. This macro copies the
\r
64 * saved context (AH:AL, DPR:ADB, DTB:PCB , PC and PS) from the system
\r
65 * stack to task stack pointed by user stack pointer ( USP for SMALL and
\r
66 * MEDIUM memory model amd USB:USP for COMPACT and LARGE memory model ),
\r
67 * then it pushes the general purpose registers RW0-RW7 on to the task
\r
68 * stack. Finally the resultant stack pointer value is saved into the
\r
69 * task control block so it can be retrieved the next time the task
\r
72 #if( ( configMEMMODEL == portSMALL ) || ( configMEMMODEL == portMEDIUM ) )
\r
74 #define portSAVE_CONTEXT() \
\r
75 { __asm(" POPW A "); \
\r
76 __asm(" AND CCR,#H'DF "); \
\r
77 __asm(" PUSHW A "); \
\r
78 __asm(" OR CCR,#H'20 "); \
\r
79 __asm(" POPW A "); \
\r
80 __asm(" AND CCR,#H'DF "); \
\r
81 __asm(" PUSHW A "); \
\r
82 __asm(" OR CCR,#H'20 "); \
\r
83 __asm(" POPW A "); \
\r
84 __asm(" AND CCR,#H'DF "); \
\r
85 __asm(" PUSHW A "); \
\r
86 __asm(" OR CCR,#H'20 "); \
\r
87 __asm(" POPW A "); \
\r
88 __asm(" AND CCR,#H'DF "); \
\r
89 __asm(" PUSHW A "); \
\r
90 __asm(" OR CCR,#H'20 "); \
\r
91 __asm(" POPW A "); \
\r
92 __asm(" AND CCR,#H'DF "); \
\r
93 __asm(" PUSHW A "); \
\r
94 __asm(" OR CCR,#H'20 "); \
\r
95 __asm(" POPW A "); \
\r
96 __asm(" AND CCR,#H'DF "); \
\r
97 __asm(" PUSHW A "); \
\r
98 __asm(" PUSHW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
99 __asm(" MOVW A, _pxCurrentTCB "); \
\r
100 __asm(" MOVW A, SP "); \
\r
101 __asm(" SWAPW "); \
\r
102 __asm(" MOVW @AL, AH "); \
\r
103 __asm(" OR CCR,#H'20 "); \
\r
107 * Macro to restore a task context from the task stack. This is effecti-
\r
108 * vely the reverse of SAVE_CONTEXT(). First the stack pointer value
\r
109 * (USP for SMALL and MEDIUM memory model amd USB:USP for COMPACT and
\r
110 * LARGE memory model ) is loaded from the task control block. Next the
\r
111 * value of all the general purpose registers RW0-RW7 is retrieved. Fina-
\r
112 * lly it copies of the context ( AH:AL, DPR:ADB, DTB:PCB, PC and PS) of
\r
113 * the task to be executed upon RETI from user stack to system stack.
\r
116 #define portRESTORE_CONTEXT() \
\r
117 { __asm(" MOVW A, _pxCurrentTCB "); \
\r
118 __asm(" MOVW A, @A "); \
\r
119 __asm(" AND CCR,#H'DF "); \
\r
120 __asm(" MOVW SP, A "); \
\r
121 __asm(" POPW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
122 __asm(" POPW A "); \
\r
123 __asm(" OR CCR,#H'20 "); \
\r
124 __asm(" PUSHW A "); \
\r
125 __asm(" AND CCR,#H'DF "); \
\r
126 __asm(" POPW A "); \
\r
127 __asm(" OR CCR,#H'20 "); \
\r
128 __asm(" PUSHW A "); \
\r
129 __asm(" AND CCR,#H'DF "); \
\r
130 __asm(" POPW A "); \
\r
131 __asm(" OR CCR,#H'20 "); \
\r
132 __asm(" PUSHW A "); \
\r
133 __asm(" AND CCR,#H'DF "); \
\r
134 __asm(" POPW A "); \
\r
135 __asm(" OR CCR,#H'20 "); \
\r
136 __asm(" PUSHW A "); \
\r
137 __asm(" AND CCR,#H'DF "); \
\r
138 __asm(" POPW A "); \
\r
139 __asm(" OR CCR,#H'20 "); \
\r
140 __asm(" PUSHW A "); \
\r
141 __asm(" AND CCR,#H'DF "); \
\r
142 __asm(" POPW A "); \
\r
143 __asm(" OR CCR,#H'20 "); \
\r
144 __asm(" PUSHW A "); \
\r
147 #elif( ( configMEMMODEL == portCOMPACT ) || ( configMEMMODEL == portLARGE ) )
\r
149 #define portSAVE_CONTEXT() \
\r
150 { __asm(" POPW A "); \
\r
151 __asm(" AND CCR,#H'DF "); \
\r
152 __asm(" PUSHW A "); \
\r
153 __asm(" OR CCR,#H'20 "); \
\r
154 __asm(" POPW A "); \
\r
155 __asm(" AND CCR,#H'DF "); \
\r
156 __asm(" PUSHW A "); \
\r
157 __asm(" OR CCR,#H'20 "); \
\r
158 __asm(" POPW A "); \
\r
159 __asm(" AND CCR,#H'DF "); \
\r
160 __asm(" PUSHW A "); \
\r
161 __asm(" OR CCR,#H'20 "); \
\r
162 __asm(" POPW A "); \
\r
163 __asm(" AND CCR,#H'DF "); \
\r
164 __asm(" PUSHW A "); \
\r
165 __asm(" OR CCR,#H'20 "); \
\r
166 __asm(" POPW A "); \
\r
167 __asm(" AND CCR,#H'DF "); \
\r
168 __asm(" PUSHW A "); \
\r
169 __asm(" OR CCR,#H'20 "); \
\r
170 __asm(" POPW A "); \
\r
171 __asm(" AND CCR,#H'DF "); \
\r
172 __asm(" PUSHW A "); \
\r
173 __asm(" PUSHW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
174 __asm(" MOVL A, _pxCurrentTCB "); \
\r
175 __asm(" MOVL RL2, A "); \
\r
176 __asm(" MOVW A, SP "); \
\r
177 __asm(" MOVW @RL2+0, A "); \
\r
178 __asm(" MOV A, USB "); \
\r
179 __asm(" MOV @RL2+2, A "); \
\r
182 #define portRESTORE_CONTEXT() \
\r
183 { __asm(" MOVL A, _pxCurrentTCB "); \
\r
184 __asm(" MOVL RL2, A "); \
\r
185 __asm(" MOVW A, @RL2+0 "); \
\r
186 __asm(" AND CCR,#H'DF "); \
\r
187 __asm(" MOVW SP, A "); \
\r
188 __asm(" MOV A, @RL2+2 "); \
\r
189 __asm(" MOV USB, A "); \
\r
190 __asm(" POPW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
191 __asm(" POPW A "); \
\r
192 __asm(" OR CCR,#H'20 "); \
\r
193 __asm(" PUSHW A "); \
\r
194 __asm(" AND CCR,#H'DF "); \
\r
195 __asm(" POPW A "); \
\r
196 __asm(" OR CCR,#H'20 "); \
\r
197 __asm(" PUSHW A "); \
\r
198 __asm(" AND CCR,#H'DF "); \
\r
199 __asm(" POPW A "); \
\r
200 __asm(" OR CCR,#H'20 "); \
\r
201 __asm(" PUSHW A "); \
\r
202 __asm(" AND CCR,#H'DF "); \
\r
203 __asm(" POPW A "); \
\r
204 __asm(" OR CCR,#H'20 "); \
\r
205 __asm(" PUSHW A "); \
\r
206 __asm(" AND CCR,#H'DF "); \
\r
207 __asm(" POPW A "); \
\r
208 __asm(" OR CCR,#H'20 "); \
\r
209 __asm(" PUSHW A "); \
\r
210 __asm(" AND CCR,#H'DF "); \
\r
211 __asm(" POPW A "); \
\r
212 __asm(" OR CCR,#H'20 "); \
\r
213 __asm(" PUSHW A "); \
\r
217 /*-----------------------------------------------------------*/
\r
220 * Functions for obtaining the current value of DPR:ADB, DTB:PCB bank registers
\r
225 .GLOBAL _xGet_DPR_ADB_bank
\r
226 .GLOBAL _xGet_DTB_PCB_bank
\r
227 .SECTION CODE, CODE, ALIGN=1
\r
229 _xGet_DPR_ADB_bank:
\r
235 #if configMEMMODEL == portMEDIUM || configMEMMODEL == portLARGE
\r
237 #elif configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
242 _xGet_DTB_PCB_bank:
\r
248 #if configMEMMODEL == portMEDIUM || configMEMMODEL == portLARGE
\r
250 #elif configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
255 /*-----------------------------------------------------------*/
\r
258 * Initialise the stack of a task to look exactly as if a call to
\r
259 * portSAVE_CONTEXT had been called.
\r
261 * See the header file portable.h.
\r
263 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
265 /* Place a few bytes of known values on the bottom of the stack.
\r
266 This is just useful for debugging. */
\r
267 *pxTopOfStack = 0x1111;
\r
269 *pxTopOfStack = 0x2222;
\r
271 *pxTopOfStack = 0x3333;
\r
274 /* Once the task is called the task would push the pointer to the
\r
275 parameter onto the stack. Hence here the pointer would be copied to the stack
\r
276 first. When using the COMPACT or LARGE memory model the pointer would be 24
\r
277 bits, and when using the SMALL or MEDIUM memory model the pointer would be 16
\r
279 #if( ( configMEMMODEL == portCOMPACT ) || ( configMEMMODEL == portLARGE ) )
\r
281 *pxTopOfStack = ( StackType_t ) ( ( uint32_t ) ( pvParameters ) >> 16 );
\r
286 *pxTopOfStack = ( StackType_t ) ( pvParameters );
\r
289 /* This is redundant push to the stack. This is required in order to introduce
\r
290 an offset so that the task accesses a parameter correctly that is passed on to
\r
292 #if( ( configMEMMODEL == portMEDIUM ) || ( configMEMMODEL == portLARGE ) )
\r
294 *pxTopOfStack = ( xGet_DTB_PCB_bank() & 0xff00 ) | ( ( ( int32_t ) ( pxCode ) >> 16 ) & 0xff );
\r
299 /* This is redundant push to the stack. This is required in order to introduce
\r
300 an offset so the task correctly accesses the parameter passed on the task stack. */
\r
301 *pxTopOfStack = ( StackType_t ) ( pxCode );
\r
304 /* PS - User Mode, ILM=7, RB=0, Interrupts enabled,USP */
\r
305 *pxTopOfStack = 0xE0C0;
\r
309 *pxTopOfStack = ( StackType_t ) ( pxCode );
\r
313 #if configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
315 *pxTopOfStack = xGet_DTB_PCB_bank();
\r
320 /* DTB | PCB, in case of MEDIUM and LARGE memory models, PCB would be used
\r
321 along with PC to indicate the start address of the function. */
\r
322 #if( ( configMEMMODEL == portMEDIUM ) || ( configMEMMODEL == portLARGE ) )
\r
324 *pxTopOfStack = ( xGet_DTB_PCB_bank() & 0xff00 ) | ( ( ( int32_t ) ( pxCode ) >> 16 ) & 0xff );
\r
330 *pxTopOfStack = xGet_DPR_ADB_bank();
\r
334 *pxTopOfStack = ( StackType_t ) 0x9999;
\r
338 *pxTopOfStack = ( StackType_t ) 0xAAAA;
\r
341 /* Next the general purpose registers. */
\r
342 *pxTopOfStack = ( StackType_t ) 0x7777; /* RW7 */
\r
344 *pxTopOfStack = ( StackType_t ) 0x6666; /* RW6 */
\r
346 *pxTopOfStack = ( StackType_t ) 0x5555; /* RW5 */
\r
348 *pxTopOfStack = ( StackType_t ) 0x4444; /* RW4 */
\r
350 *pxTopOfStack = ( StackType_t ) 0x3333; /* RW3 */
\r
352 *pxTopOfStack = ( StackType_t ) 0x2222; /* RW2 */
\r
354 *pxTopOfStack = ( StackType_t ) 0x1111; /* RW1 */
\r
356 *pxTopOfStack = ( StackType_t ) 0x8888; /* RW0 */
\r
358 return pxTopOfStack;
\r
360 /*-----------------------------------------------------------*/
\r
362 static void prvSetupRLT0Interrupt( void )
\r
364 /* The peripheral clock divided by 16 is used by the timer. */
\r
365 const uint16_t usReloadValue = ( uint16_t ) ( ( ( configCLKP1_CLOCK_HZ / configTICK_RATE_HZ ) / 16UL ) - 1UL );
\r
367 /* set reload value = 34999+1, TICK Interrupt after 10 ms @ 56MHz of CLKP1 */
\r
368 TMRLR0 = usReloadValue;
\r
370 /* prescaler 1:16, reload, interrupt enable, count enable, trigger */
\r
373 /*-----------------------------------------------------------*/
\r
375 BaseType_t xPortStartScheduler( void )
\r
377 /* Setup the hardware to generate the tick. */
\r
378 prvSetupRLT0Interrupt();
\r
380 /* Restore the context of the first task that is going to run. */
\r
381 portRESTORE_CONTEXT();
\r
383 /* Simulate a function call end as generated by the compiler. We will now
\r
384 jump to the start of the task the context of which we have just restored. */
\r
388 /* Should not get here. */
\r
391 /*-----------------------------------------------------------*/
\r
393 void vPortEndScheduler( void )
\r
395 /* Not implemented - unlikely to ever be required as there is nothing to
\r
399 /*-----------------------------------------------------------*/
\r
402 * The interrupt service routine used depends on whether the pre-emptive
\r
403 * scheduler is being used or not.
\r
406 #if configUSE_PREEMPTION == 1
\r
409 * Tick ISR for preemptive scheduler. We can use a __nosavereg attribute
\r
410 * as the context is to be saved by the portSAVE_CONTEXT() macro, not the
\r
411 * compiler generated code. The tick count is incremented after the context
\r
414 __nosavereg __interrupt void prvRLT0_TICKISR( void )
\r
416 /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
\r
419 /* Save the context of the interrupted task. */
\r
420 portSAVE_CONTEXT();
\r
422 /* Enable interrupts */
\r
425 /* Clear RLT0 interrupt flag */
\r
428 /* Increment the tick count then switch to the highest priority task
\r
429 that is ready to run. */
\r
430 if( xTaskIncrementTick() != pdFALSE )
\r
432 vTaskSwitchContext();
\r
435 /* Disable interrupts so that portRESTORE_CONTEXT() is not interrupted */
\r
438 /* Restore the context of the new task. */
\r
439 portRESTORE_CONTEXT();
\r
441 /* Enable interrupts */
\r
448 * Tick ISR for the cooperative scheduler. All this does is increment the
\r
449 * tick count. We don't need to switch context, this can only be done by
\r
450 * manual calls to taskYIELD();
\r
452 __interrupt void prvRLT0_TICKISR( void )
\r
454 /* Clear RLT0 interrupt flag */
\r
457 xTaskIncrementTick();
\r
462 /*-----------------------------------------------------------*/
\r
465 * Manual context switch. We can use a __nosavereg attribute as the context
\r
466 * is to be saved by the portSAVE_CONTEXT() macro, not the compiler generated
\r
469 __nosavereg __interrupt void vPortYield( void )
\r
471 /* Save the context of the interrupted task. */
\r
472 portSAVE_CONTEXT();
\r
474 /* Switch to the highest priority task that is ready to run. */
\r
475 vTaskSwitchContext();
\r
477 /* Restore the context of the new task. */
\r
478 portRESTORE_CONTEXT();
\r
480 /*-----------------------------------------------------------*/
\r
482 __nosavereg __interrupt void vPortYieldDelayed( void )
\r
484 /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
\r
487 /* Save the context of the interrupted task. */
\r
488 portSAVE_CONTEXT();
\r
490 /* Enable interrupts */
\r
493 /* Clear delayed interrupt flag */
\r
494 __asm (" CLRB 03A4H:0 ");
\r
496 /* Switch to the highest priority task that is ready to run. */
\r
497 vTaskSwitchContext();
\r
499 /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
\r
502 /* Restore the context of the new task. */
\r
503 portRESTORE_CONTEXT();
\r
505 /* Enable interrupts */
\r
508 /*-----------------------------------------------------------*/
\r