2 FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
67 #include "FreeRTOS.h"
\r
70 /*-----------------------------------------------------------
\r
71 * Implementation of functions defined in portable.h for the 16FX port.
\r
72 *----------------------------------------------------------*/
\r
75 * Get current value of DPR and ADB registers
\r
77 portSTACK_TYPE xGet_DPR_ADB_bank( void );
\r
80 * Get current value of DTB and PCB registers
\r
82 portSTACK_TYPE xGet_DTB_PCB_bank( void );
\r
85 * Sets up the periodic ISR used for the RTOS tick. This uses RLT0, but
\r
86 * can be done using any given RLT.
\r
88 static void prvSetupRLT0Interrupt( void );
\r
90 /*-----------------------------------------------------------*/
\r
93 * We require the address of the pxCurrentTCB variable, but don't want to know
\r
94 * any details of its type.
\r
96 typedef void tskTCB;
\r
97 extern volatile tskTCB * volatile pxCurrentTCB;
\r
99 /*-----------------------------------------------------------*/
\r
102 * Macro to save a task context to the task stack. This macro copies the
\r
103 * saved context (AH:AL, DPR:ADB, DTB:PCB , PC and PS) from the system
\r
104 * stack to task stack pointed by user stack pointer ( USP for SMALL and
\r
105 * MEDIUM memory model amd USB:USP for COMPACT and LARGE memory model ),
\r
106 * then it pushes the general purpose registers RW0-RW7 on to the task
\r
107 * stack. Finally the resultant stack pointer value is saved into the
\r
108 * task control block so it can be retrieved the next time the task
\r
111 #if( ( configMEMMODEL == portSMALL ) || ( configMEMMODEL == portMEDIUM ) )
\r
113 #define portSAVE_CONTEXT() \
\r
114 { __asm(" POPW A "); \
\r
115 __asm(" AND CCR,#H'DF "); \
\r
116 __asm(" PUSHW A "); \
\r
117 __asm(" OR CCR,#H'20 "); \
\r
118 __asm(" POPW A "); \
\r
119 __asm(" AND CCR,#H'DF "); \
\r
120 __asm(" PUSHW A "); \
\r
121 __asm(" OR CCR,#H'20 "); \
\r
122 __asm(" POPW A "); \
\r
123 __asm(" AND CCR,#H'DF "); \
\r
124 __asm(" PUSHW A "); \
\r
125 __asm(" OR CCR,#H'20 "); \
\r
126 __asm(" POPW A "); \
\r
127 __asm(" AND CCR,#H'DF "); \
\r
128 __asm(" PUSHW A "); \
\r
129 __asm(" OR CCR,#H'20 "); \
\r
130 __asm(" POPW A "); \
\r
131 __asm(" AND CCR,#H'DF "); \
\r
132 __asm(" PUSHW A "); \
\r
133 __asm(" OR CCR,#H'20 "); \
\r
134 __asm(" POPW A "); \
\r
135 __asm(" AND CCR,#H'DF "); \
\r
136 __asm(" PUSHW A "); \
\r
137 __asm(" PUSHW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
138 __asm(" MOVW A, _pxCurrentTCB "); \
\r
139 __asm(" MOVW A, SP "); \
\r
140 __asm(" SWAPW "); \
\r
141 __asm(" MOVW @AL, AH "); \
\r
142 __asm(" OR CCR,#H'20 "); \
\r
146 * Macro to restore a task context from the task stack. This is effecti-
\r
147 * vely the reverse of SAVE_CONTEXT(). First the stack pointer value
\r
148 * (USP for SMALL and MEDIUM memory model amd USB:USP for COMPACT and
\r
149 * LARGE memory model ) is loaded from the task control block. Next the
\r
150 * value of all the general purpose registers RW0-RW7 is retrieved. Fina-
\r
151 * lly it copies of the context ( AH:AL, DPR:ADB, DTB:PCB, PC and PS) of
\r
152 * the task to be executed upon RETI from user stack to system stack.
\r
155 #define portRESTORE_CONTEXT() \
\r
156 { __asm(" MOVW A, _pxCurrentTCB "); \
\r
157 __asm(" MOVW A, @A "); \
\r
158 __asm(" AND CCR,#H'DF "); \
\r
159 __asm(" MOVW SP, A "); \
\r
160 __asm(" POPW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
161 __asm(" POPW A "); \
\r
162 __asm(" OR CCR,#H'20 "); \
\r
163 __asm(" PUSHW A "); \
\r
164 __asm(" AND CCR,#H'DF "); \
\r
165 __asm(" POPW A "); \
\r
166 __asm(" OR CCR,#H'20 "); \
\r
167 __asm(" PUSHW A "); \
\r
168 __asm(" AND CCR,#H'DF "); \
\r
169 __asm(" POPW A "); \
\r
170 __asm(" OR CCR,#H'20 "); \
\r
171 __asm(" PUSHW A "); \
\r
172 __asm(" AND CCR,#H'DF "); \
\r
173 __asm(" POPW A "); \
\r
174 __asm(" OR CCR,#H'20 "); \
\r
175 __asm(" PUSHW A "); \
\r
176 __asm(" AND CCR,#H'DF "); \
\r
177 __asm(" POPW A "); \
\r
178 __asm(" OR CCR,#H'20 "); \
\r
179 __asm(" PUSHW A "); \
\r
180 __asm(" AND CCR,#H'DF "); \
\r
181 __asm(" POPW A "); \
\r
182 __asm(" OR CCR,#H'20 "); \
\r
183 __asm(" PUSHW A "); \
\r
186 #elif( ( configMEMMODEL == portCOMPACT ) || ( configMEMMODEL == portLARGE ) )
\r
188 #define portSAVE_CONTEXT() \
\r
189 { __asm(" POPW A "); \
\r
190 __asm(" AND CCR,#H'DF "); \
\r
191 __asm(" PUSHW A "); \
\r
192 __asm(" OR CCR,#H'20 "); \
\r
193 __asm(" POPW A "); \
\r
194 __asm(" AND CCR,#H'DF "); \
\r
195 __asm(" PUSHW A "); \
\r
196 __asm(" OR CCR,#H'20 "); \
\r
197 __asm(" POPW A "); \
\r
198 __asm(" AND CCR,#H'DF "); \
\r
199 __asm(" PUSHW A "); \
\r
200 __asm(" OR CCR,#H'20 "); \
\r
201 __asm(" POPW A "); \
\r
202 __asm(" AND CCR,#H'DF "); \
\r
203 __asm(" PUSHW A "); \
\r
204 __asm(" OR CCR,#H'20 "); \
\r
205 __asm(" POPW A "); \
\r
206 __asm(" AND CCR,#H'DF "); \
\r
207 __asm(" PUSHW A "); \
\r
208 __asm(" OR CCR,#H'20 "); \
\r
209 __asm(" POPW A "); \
\r
210 __asm(" AND CCR,#H'DF "); \
\r
211 __asm(" PUSHW A "); \
\r
212 __asm(" PUSHW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
213 __asm(" MOVL A, _pxCurrentTCB "); \
\r
214 __asm(" MOVL RL2, A "); \
\r
215 __asm(" MOVW A, SP "); \
\r
216 __asm(" MOVW @RL2+0, A "); \
\r
217 __asm(" MOV A, USB "); \
\r
218 __asm(" MOV @RL2+2, A "); \
\r
221 #define portRESTORE_CONTEXT() \
\r
222 { __asm(" MOVL A, _pxCurrentTCB "); \
\r
223 __asm(" MOVL RL2, A "); \
\r
224 __asm(" MOVW A, @RL2+0 "); \
\r
225 __asm(" AND CCR,#H'DF "); \
\r
226 __asm(" MOVW SP, A "); \
\r
227 __asm(" MOV A, @RL2+2 "); \
\r
228 __asm(" MOV USB, A "); \
\r
229 __asm(" POPW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
230 __asm(" POPW A "); \
\r
231 __asm(" OR CCR,#H'20 "); \
\r
232 __asm(" PUSHW A "); \
\r
233 __asm(" AND CCR,#H'DF "); \
\r
234 __asm(" POPW A "); \
\r
235 __asm(" OR CCR,#H'20 "); \
\r
236 __asm(" PUSHW A "); \
\r
237 __asm(" AND CCR,#H'DF "); \
\r
238 __asm(" POPW A "); \
\r
239 __asm(" OR CCR,#H'20 "); \
\r
240 __asm(" PUSHW A "); \
\r
241 __asm(" AND CCR,#H'DF "); \
\r
242 __asm(" POPW A "); \
\r
243 __asm(" OR CCR,#H'20 "); \
\r
244 __asm(" PUSHW A "); \
\r
245 __asm(" AND CCR,#H'DF "); \
\r
246 __asm(" POPW A "); \
\r
247 __asm(" OR CCR,#H'20 "); \
\r
248 __asm(" PUSHW A "); \
\r
249 __asm(" AND CCR,#H'DF "); \
\r
250 __asm(" POPW A "); \
\r
251 __asm(" OR CCR,#H'20 "); \
\r
252 __asm(" PUSHW A "); \
\r
256 /*-----------------------------------------------------------*/
\r
259 * Functions for obtaining the current value of DPR:ADB, DTB:PCB bank registers
\r
264 .GLOBAL _xGet_DPR_ADB_bank
\r
265 .GLOBAL _xGet_DTB_PCB_bank
\r
266 .SECTION CODE, CODE, ALIGN=1
\r
268 _xGet_DPR_ADB_bank:
\r
274 #if configMEMMODEL == portMEDIUM || configMEMMODEL == portLARGE
\r
276 #elif configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
281 _xGet_DTB_PCB_bank:
\r
287 #if configMEMMODEL == portMEDIUM || configMEMMODEL == portLARGE
\r
289 #elif configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
294 /*-----------------------------------------------------------*/
\r
297 * Initialise the stack of a task to look exactly as if a call to
\r
298 * portSAVE_CONTEXT had been called.
\r
300 * See the header file portable.h.
\r
302 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
304 /* Place a few bytes of known values on the bottom of the stack.
\r
305 This is just useful for debugging. */
\r
306 *pxTopOfStack = 0x1111;
\r
308 *pxTopOfStack = 0x2222;
\r
310 *pxTopOfStack = 0x3333;
\r
313 /* Once the task is called the task would push the pointer to the
\r
314 parameter onto the stack. Hence here the pointer would be copied to the stack
\r
315 first. When using the COMPACT or LARGE memory model the pointer would be 24
\r
316 bits, and when using the SMALL or MEDIUM memory model the pointer would be 16
\r
318 #if( ( configMEMMODEL == portCOMPACT ) || ( configMEMMODEL == portLARGE ) )
\r
320 *pxTopOfStack = ( portSTACK_TYPE ) ( ( unsigned long ) ( pvParameters ) >> 16 );
\r
325 *pxTopOfStack = ( portSTACK_TYPE ) ( pvParameters );
\r
328 /* This is redundant push to the stack. This is required in order to introduce
\r
329 an offset so that the task accesses a parameter correctly that is passed on to
\r
331 #if( ( configMEMMODEL == portMEDIUM ) || ( configMEMMODEL == portLARGE ) )
\r
333 *pxTopOfStack = ( xGet_DTB_PCB_bank() & 0xff00 ) | ( ( ( long ) ( pxCode ) >> 16 ) & 0xff );
\r
338 /* This is redundant push to the stack. This is required in order to introduce
\r
339 an offset so the task correctly accesses the parameter passed on the task stack. */
\r
340 *pxTopOfStack = ( portSTACK_TYPE ) ( pxCode );
\r
343 /* PS - User Mode, ILM=7, RB=0, Interrupts enabled,USP */
\r
344 *pxTopOfStack = 0xE0C0;
\r
348 *pxTopOfStack = ( portSTACK_TYPE ) ( pxCode );
\r
352 #if configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
354 *pxTopOfStack = xGet_DTB_PCB_bank();
\r
359 /* DTB | PCB, in case of MEDIUM and LARGE memory models, PCB would be used
\r
360 along with PC to indicate the start address of the function. */
\r
361 #if( ( configMEMMODEL == portMEDIUM ) || ( configMEMMODEL == portLARGE ) )
\r
363 *pxTopOfStack = ( xGet_DTB_PCB_bank() & 0xff00 ) | ( ( ( long ) ( pxCode ) >> 16 ) & 0xff );
\r
369 *pxTopOfStack = xGet_DPR_ADB_bank();
\r
373 *pxTopOfStack = ( portSTACK_TYPE ) 0x9999;
\r
377 *pxTopOfStack = ( portSTACK_TYPE ) 0xAAAA;
\r
380 /* Next the general purpose registers. */
\r
381 *pxTopOfStack = ( portSTACK_TYPE ) 0x7777; /* RW7 */
\r
383 *pxTopOfStack = ( portSTACK_TYPE ) 0x6666; /* RW6 */
\r
385 *pxTopOfStack = ( portSTACK_TYPE ) 0x5555; /* RW5 */
\r
387 *pxTopOfStack = ( portSTACK_TYPE ) 0x4444; /* RW4 */
\r
389 *pxTopOfStack = ( portSTACK_TYPE ) 0x3333; /* RW3 */
\r
391 *pxTopOfStack = ( portSTACK_TYPE ) 0x2222; /* RW2 */
\r
393 *pxTopOfStack = ( portSTACK_TYPE ) 0x1111; /* RW1 */
\r
395 *pxTopOfStack = ( portSTACK_TYPE ) 0x8888; /* RW0 */
\r
397 return pxTopOfStack;
\r
399 /*-----------------------------------------------------------*/
\r
401 static void prvSetupRLT0Interrupt( void )
\r
403 /* The peripheral clock divided by 16 is used by the timer. */
\r
404 const unsigned short usReloadValue = ( unsigned short ) ( ( ( configCLKP1_CLOCK_HZ / configTICK_RATE_HZ ) / 16UL ) - 1UL );
\r
406 /* set reload value = 34999+1, TICK Interrupt after 10 ms @ 56MHz of CLKP1 */
\r
407 TMRLR0 = usReloadValue;
\r
409 /* prescaler 1:16, reload, interrupt enable, count enable, trigger */
\r
412 /*-----------------------------------------------------------*/
\r
414 portBASE_TYPE xPortStartScheduler( void )
\r
416 /* Setup the hardware to generate the tick. */
\r
417 prvSetupRLT0Interrupt();
\r
419 /* Restore the context of the first task that is going to run. */
\r
420 portRESTORE_CONTEXT();
\r
422 /* Simulate a function call end as generated by the compiler. We will now
\r
423 jump to the start of the task the context of which we have just restored. */
\r
427 /* Should not get here. */
\r
430 /*-----------------------------------------------------------*/
\r
432 void vPortEndScheduler( void )
\r
434 /* Not implemented - unlikely to ever be required as there is nothing to
\r
438 /*-----------------------------------------------------------*/
\r
441 * The interrupt service routine used depends on whether the pre-emptive
\r
442 * scheduler is being used or not.
\r
445 #if configUSE_PREEMPTION == 1
\r
448 * Tick ISR for preemptive scheduler. We can use a __nosavereg attribute
\r
449 * as the context is to be saved by the portSAVE_CONTEXT() macro, not the
\r
450 * compiler generated code. The tick count is incremented after the context
\r
453 __nosavereg __interrupt void prvRLT0_TICKISR( void )
\r
455 /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
\r
458 /* Save the context of the interrupted task. */
\r
459 portSAVE_CONTEXT();
\r
461 /* Enable interrupts */
\r
464 /* Clear RLT0 interrupt flag */
\r
467 /* Increment the tick count then switch to the highest priority task
\r
468 that is ready to run. */
\r
469 vTaskIncrementTick();
\r
470 vTaskSwitchContext();
\r
472 /* Disable interrupts so that portRESTORE_CONTEXT() is not interrupted */
\r
475 /* Restore the context of the new task. */
\r
476 portRESTORE_CONTEXT();
\r
478 /* Enable interrupts */
\r
485 * Tick ISR for the cooperative scheduler. All this does is increment the
\r
486 * tick count. We don't need to switch context, this can only be done by
\r
487 * manual calls to taskYIELD();
\r
489 __interrupt void prvRLT0_TICKISR( void )
\r
491 /* Clear RLT0 interrupt flag */
\r
494 vTaskIncrementTick();
\r
499 /*-----------------------------------------------------------*/
\r
502 * Manual context switch. We can use a __nosavereg attribute as the context
\r
503 * is to be saved by the portSAVE_CONTEXT() macro, not the compiler generated
\r
506 __nosavereg __interrupt void vPortYield( void )
\r
508 /* Save the context of the interrupted task. */
\r
509 portSAVE_CONTEXT();
\r
511 /* Switch to the highest priority task that is ready to run. */
\r
512 vTaskSwitchContext();
\r
514 /* Restore the context of the new task. */
\r
515 portRESTORE_CONTEXT();
\r
517 /*-----------------------------------------------------------*/
\r
519 __nosavereg __interrupt void vPortYieldDelayed( void )
\r
521 /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
\r
524 /* Save the context of the interrupted task. */
\r
525 portSAVE_CONTEXT();
\r
527 /* Enable interrupts */
\r
530 /* Clear delayed interrupt flag */
\r
531 __asm (" CLRB 03A4H:0 ");
\r
533 /* Switch to the highest priority task that is ready to run. */
\r
534 vTaskSwitchContext();
\r
536 /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
\r
539 /* Restore the context of the new task. */
\r
540 portRESTORE_CONTEXT();
\r
542 /* Enable interrupts */
\r
545 /*-----------------------------------------------------------*/
\r