2 FreeRTOS.org V5.4.0 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
52 #include "FreeRTOS.h"
\r
55 /*-----------------------------------------------------------
\r
56 * Implementation of functions defined in portable.h for the 16FX port.
\r
57 *----------------------------------------------------------*/
\r
60 * Get current value of DPR and ADB registers
\r
62 portSTACK_TYPE xGet_DPR_ADB_bank( void );
\r
65 * Get current value of DTB and PCB registers
\r
67 portSTACK_TYPE xGet_DTB_PCB_bank( void );
\r
70 * Sets up the periodic ISR used for the RTOS tick. This uses RLT0, but
\r
71 * can be done using any given RLT.
\r
73 static void prvSetupRLT0Interrupt( void );
\r
75 /*-----------------------------------------------------------*/
\r
78 * We require the address of the pxCurrentTCB variable, but don't want to know
\r
79 * any details of its type.
\r
81 typedef void tskTCB;
\r
82 extern volatile tskTCB * volatile pxCurrentTCB;
\r
84 /*-----------------------------------------------------------*/
\r
87 * Macro to save a task context to the task stack. This macro copies the
\r
88 * saved context (AH:AL, DPR:ADB, DTB:PCB , PC and PS) from the system
\r
89 * stack to task stack pointed by user stack pointer ( USP for SMALL and
\r
90 * MEDIUM memory model amd USB:USP for COMPACT and LARGE memory model ),
\r
91 * then it pushes the general purpose registers RW0-RW7 on to the task
\r
92 * stack. Finally the resultant stack pointer value is saved into the
\r
93 * task control block so it can be retrieved the next time the task
\r
96 #if( ( configMEMMODEL == portSMALL ) || ( configMEMMODEL == portMEDIUM ) )
\r
98 #define portSAVE_CONTEXT() \
\r
99 { __asm(" POPW A "); \
\r
100 __asm(" AND CCR,#H'DF "); \
\r
101 __asm(" PUSHW A "); \
\r
102 __asm(" OR CCR,#H'20 "); \
\r
103 __asm(" POPW A "); \
\r
104 __asm(" AND CCR,#H'DF "); \
\r
105 __asm(" PUSHW A "); \
\r
106 __asm(" OR CCR,#H'20 "); \
\r
107 __asm(" POPW A "); \
\r
108 __asm(" AND CCR,#H'DF "); \
\r
109 __asm(" PUSHW A "); \
\r
110 __asm(" OR CCR,#H'20 "); \
\r
111 __asm(" POPW A "); \
\r
112 __asm(" AND CCR,#H'DF "); \
\r
113 __asm(" PUSHW A "); \
\r
114 __asm(" OR CCR,#H'20 "); \
\r
115 __asm(" POPW A "); \
\r
116 __asm(" AND CCR,#H'DF "); \
\r
117 __asm(" PUSHW A "); \
\r
118 __asm(" OR CCR,#H'20 "); \
\r
119 __asm(" POPW A "); \
\r
120 __asm(" AND CCR,#H'DF "); \
\r
121 __asm(" PUSHW A "); \
\r
122 __asm(" PUSHW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
123 __asm(" MOVW A, _pxCurrentTCB "); \
\r
124 __asm(" MOVW A, SP "); \
\r
125 __asm(" SWAPW "); \
\r
126 __asm(" MOVW @AL, AH "); \
\r
127 __asm(" OR CCR,#H'20 "); \
\r
131 * Macro to restore a task context from the task stack. This is effecti-
\r
132 * vely the reverse of SAVE_CONTEXT(). First the stack pointer value
\r
133 * (USP for SMALL and MEDIUM memory model amd USB:USP for COMPACT and
\r
134 * LARGE memory model ) is loaded from the task control block. Next the
\r
135 * value of all the general purpose registers RW0-RW7 is retrieved. Fina-
\r
136 * lly it copies of the context ( AH:AL, DPR:ADB, DTB:PCB, PC and PS) of
\r
137 * the task to be executed upon RETI from user stack to system stack.
\r
140 #define portRESTORE_CONTEXT() \
\r
141 { __asm(" MOVW A, _pxCurrentTCB "); \
\r
142 __asm(" MOVW A, @A "); \
\r
143 __asm(" AND CCR,#H'DF "); \
\r
144 __asm(" MOVW SP, A "); \
\r
145 __asm(" POPW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
146 __asm(" POPW A "); \
\r
147 __asm(" OR CCR,#H'20 "); \
\r
148 __asm(" PUSHW A "); \
\r
149 __asm(" AND CCR,#H'DF "); \
\r
150 __asm(" POPW A "); \
\r
151 __asm(" OR CCR,#H'20 "); \
\r
152 __asm(" PUSHW A "); \
\r
153 __asm(" AND CCR,#H'DF "); \
\r
154 __asm(" POPW A "); \
\r
155 __asm(" OR CCR,#H'20 "); \
\r
156 __asm(" PUSHW A "); \
\r
157 __asm(" AND CCR,#H'DF "); \
\r
158 __asm(" POPW A "); \
\r
159 __asm(" OR CCR,#H'20 "); \
\r
160 __asm(" PUSHW A "); \
\r
161 __asm(" AND CCR,#H'DF "); \
\r
162 __asm(" POPW A "); \
\r
163 __asm(" OR CCR,#H'20 "); \
\r
164 __asm(" PUSHW A "); \
\r
165 __asm(" AND CCR,#H'DF "); \
\r
166 __asm(" POPW A "); \
\r
167 __asm(" OR CCR,#H'20 "); \
\r
168 __asm(" PUSHW A "); \
\r
171 #elif( ( configMEMMODEL == portCOMPACT ) || ( configMEMMODEL == portLARGE ) )
\r
173 #define portSAVE_CONTEXT() \
\r
174 { __asm(" POPW A "); \
\r
175 __asm(" AND CCR,#H'DF "); \
\r
176 __asm(" PUSHW A "); \
\r
177 __asm(" OR CCR,#H'20 "); \
\r
178 __asm(" POPW A "); \
\r
179 __asm(" AND CCR,#H'DF "); \
\r
180 __asm(" PUSHW A "); \
\r
181 __asm(" OR CCR,#H'20 "); \
\r
182 __asm(" POPW A "); \
\r
183 __asm(" AND CCR,#H'DF "); \
\r
184 __asm(" PUSHW A "); \
\r
185 __asm(" OR CCR,#H'20 "); \
\r
186 __asm(" POPW A "); \
\r
187 __asm(" AND CCR,#H'DF "); \
\r
188 __asm(" PUSHW A "); \
\r
189 __asm(" OR CCR,#H'20 "); \
\r
190 __asm(" POPW A "); \
\r
191 __asm(" AND CCR,#H'DF "); \
\r
192 __asm(" PUSHW A "); \
\r
193 __asm(" OR CCR,#H'20 "); \
\r
194 __asm(" POPW A "); \
\r
195 __asm(" AND CCR,#H'DF "); \
\r
196 __asm(" PUSHW A "); \
\r
197 __asm(" PUSHW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
198 __asm(" MOVL A, _pxCurrentTCB "); \
\r
199 __asm(" MOVL RL2, A "); \
\r
200 __asm(" MOVW A, SP "); \
\r
201 __asm(" MOVW @RL2+0, A "); \
\r
202 __asm(" MOV A, USB "); \
\r
203 __asm(" MOV @RL2+2, A "); \
\r
206 #define portRESTORE_CONTEXT() \
\r
207 { __asm(" MOVL A, _pxCurrentTCB "); \
\r
208 __asm(" MOVL RL2, A "); \
\r
209 __asm(" MOVW A, @RL2+0 "); \
\r
210 __asm(" AND CCR,#H'DF "); \
\r
211 __asm(" MOVW SP, A "); \
\r
212 __asm(" MOV A, @RL2+2 "); \
\r
213 __asm(" MOV USB, A "); \
\r
214 __asm(" POPW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
215 __asm(" POPW A "); \
\r
216 __asm(" OR CCR,#H'20 "); \
\r
217 __asm(" PUSHW A "); \
\r
218 __asm(" AND CCR,#H'DF "); \
\r
219 __asm(" POPW A "); \
\r
220 __asm(" OR CCR,#H'20 "); \
\r
221 __asm(" PUSHW A "); \
\r
222 __asm(" AND CCR,#H'DF "); \
\r
223 __asm(" POPW A "); \
\r
224 __asm(" OR CCR,#H'20 "); \
\r
225 __asm(" PUSHW A "); \
\r
226 __asm(" AND CCR,#H'DF "); \
\r
227 __asm(" POPW A "); \
\r
228 __asm(" OR CCR,#H'20 "); \
\r
229 __asm(" PUSHW A "); \
\r
230 __asm(" AND CCR,#H'DF "); \
\r
231 __asm(" POPW A "); \
\r
232 __asm(" OR CCR,#H'20 "); \
\r
233 __asm(" PUSHW A "); \
\r
234 __asm(" AND CCR,#H'DF "); \
\r
235 __asm(" POPW A "); \
\r
236 __asm(" OR CCR,#H'20 "); \
\r
237 __asm(" PUSHW A "); \
\r
241 /*-----------------------------------------------------------*/
\r
244 * Functions for obtaining the current value of DPR:ADB, DTB:PCB bank registers
\r
249 .GLOBAL _xGet_DPR_ADB_bank
\r
250 .GLOBAL _xGet_DTB_PCB_bank
\r
251 .SECTION CODE, CODE, ALIGN=1
\r
253 _xGet_DPR_ADB_bank:
\r
259 #if configMEMMODEL == portMEDIUM || configMEMMODEL == portLARGE
\r
261 #elif configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
266 _xGet_DTB_PCB_bank:
\r
272 #if configMEMMODEL == portMEDIUM || configMEMMODEL == portLARGE
\r
274 #elif configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
279 /*-----------------------------------------------------------*/
\r
282 * Initialise the stack of a task to look exactly as if a call to
\r
283 * portSAVE_CONTEXT had been called.
\r
285 * See the header file portable.h.
\r
287 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
289 /* Place a few bytes of known values on the bottom of the stack.
\r
290 This is just useful for debugging. */
\r
291 *pxTopOfStack = 0x1111;
\r
293 *pxTopOfStack = 0x2222;
\r
295 *pxTopOfStack = 0x3333;
\r
298 /* Once the task is called the task would push the pointer to the
\r
299 parameter onto the stack. Hence here the pointer would be copied to the stack
\r
300 first. When using the COMPACT or LARGE memory model the pointer would be 24
\r
301 bits, and when using the SMALL or MEDIUM memory model the pointer would be 16
\r
303 #if( ( configMEMMODEL == portCOMPACT ) || ( configMEMMODEL == portLARGE ) )
\r
305 *pxTopOfStack = ( portSTACK_TYPE ) ( ( unsigned portLONG ) ( pvParameters ) >> 16 );
\r
310 *pxTopOfStack = ( portSTACK_TYPE ) ( pvParameters );
\r
313 /* This is redundant push to the stack. This is required in order to introduce
\r
314 an offset so that the task accesses a parameter correctly that is passed on to
\r
316 #if( ( configMEMMODEL == portMEDIUM ) || ( configMEMMODEL == portLARGE ) )
\r
318 *pxTopOfStack = ( xGet_DTB_PCB_bank() & 0xff00 ) | ( ( ( portLONG ) ( pxCode ) >> 16 ) & 0xff );
\r
323 /* This is redundant push to the stack. This is required in order to introduce
\r
324 an offset so the task correctly accesses the parameter passed on the task stack. */
\r
325 *pxTopOfStack = ( portSTACK_TYPE ) ( pxCode );
\r
328 /* PS - User Mode, ILM=7, RB=0, Interrupts enabled,USP */
\r
329 *pxTopOfStack = 0xE0C0;
\r
333 *pxTopOfStack = ( portSTACK_TYPE ) ( pxCode );
\r
337 #if configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
339 *pxTopOfStack = xGet_DTB_PCB_bank();
\r
344 /* DTB | PCB, in case of MEDIUM and LARGE memory models, PCB would be used
\r
345 along with PC to indicate the start address of the function. */
\r
346 #if( ( configMEMMODEL == portMEDIUM ) || ( configMEMMODEL == portLARGE ) )
\r
348 *pxTopOfStack = ( xGet_DTB_PCB_bank() & 0xff00 ) | ( ( ( portLONG ) ( pxCode ) >> 16 ) & 0xff );
\r
354 *pxTopOfStack = xGet_DPR_ADB_bank();
\r
358 *pxTopOfStack = ( portSTACK_TYPE ) 0x9999;
\r
362 *pxTopOfStack = ( portSTACK_TYPE ) 0xAAAA;
\r
365 /* Next the general purpose registers. */
\r
366 *pxTopOfStack = ( portSTACK_TYPE ) 0x7777; /* RW7 */
\r
368 *pxTopOfStack = ( portSTACK_TYPE ) 0x6666; /* RW6 */
\r
370 *pxTopOfStack = ( portSTACK_TYPE ) 0x5555; /* RW5 */
\r
372 *pxTopOfStack = ( portSTACK_TYPE ) 0x4444; /* RW4 */
\r
374 *pxTopOfStack = ( portSTACK_TYPE ) 0x3333; /* RW3 */
\r
376 *pxTopOfStack = ( portSTACK_TYPE ) 0x2222; /* RW2 */
\r
378 *pxTopOfStack = ( portSTACK_TYPE ) 0x1111; /* RW1 */
\r
380 *pxTopOfStack = ( portSTACK_TYPE ) 0x8888; /* RW0 */
\r
382 return pxTopOfStack;
\r
384 /*-----------------------------------------------------------*/
\r
386 static void prvSetupRLT0Interrupt( void )
\r
388 /* The peripheral clock divided by 16 is used by the timer. */
\r
389 const unsigned portSHORT usReloadValue = ( unsigned portSHORT ) ( ( ( configCLKP1_CLOCK_HZ / configTICK_RATE_HZ ) / 16UL ) - 1UL );
\r
391 /* set reload value = 34999+1, TICK Interrupt after 10 ms @ 56MHz of CLKP1 */
\r
392 TMRLR0 = usReloadValue;
\r
394 /* prescaler 1:16, reload, interrupt enable, count enable, trigger */
\r
397 /*-----------------------------------------------------------*/
\r
399 portBASE_TYPE xPortStartScheduler( void )
\r
401 /* Setup the hardware to generate the tick. */
\r
402 prvSetupRLT0Interrupt();
\r
404 /* Restore the context of the first task that is going to run. */
\r
405 portRESTORE_CONTEXT();
\r
407 /* Simulate a function call end as generated by the compiler. We will now
\r
408 jump to the start of the task the context of which we have just restored. */
\r
412 /* Should not get here. */
\r
415 /*-----------------------------------------------------------*/
\r
417 void vPortEndScheduler( void )
\r
419 /* Not implemented - unlikely to ever be required as there is nothing to
\r
423 /*-----------------------------------------------------------*/
\r
426 * The interrupt service routine used depends on whether the pre-emptive
\r
427 * scheduler is being used or not.
\r
430 #if configUSE_PREEMPTION == 1
\r
433 * Tick ISR for preemptive scheduler. We can use a __nosavereg attribute
\r
434 * as the context is to be saved by the portSAVE_CONTEXT() macro, not the
\r
435 * compiler generated code. The tick count is incremented after the context
\r
438 __nosavereg __interrupt void prvRLT0_TICKISR( void )
\r
440 /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
\r
443 /* Save the context of the interrupted task. */
\r
444 portSAVE_CONTEXT();
\r
446 /* Enable interrupts */
\r
449 /* Clear RLT0 interrupt flag */
\r
452 /* Increment the tick count then switch to the highest priority task
\r
453 that is ready to run. */
\r
454 vTaskIncrementTick();
\r
455 vTaskSwitchContext();
\r
457 /* Disable interrupts so that portRESTORE_CONTEXT() is not interrupted */
\r
460 /* Restore the context of the new task. */
\r
461 portRESTORE_CONTEXT();
\r
463 /* Enable interrupts */
\r
470 * Tick ISR for the cooperative scheduler. All this does is increment the
\r
471 * tick count. We don't need to switch context, this can only be done by
\r
472 * manual calls to taskYIELD();
\r
474 __interrupt void prvRLT0_TICKISR( void )
\r
476 /* Clear RLT0 interrupt flag */
\r
479 vTaskIncrementTick();
\r
484 /*-----------------------------------------------------------*/
\r
487 * Manual context switch. We can use a __nosavereg attribute as the context
\r
488 * is to be saved by the portSAVE_CONTEXT() macro, not the compiler generated
\r
491 __nosavereg __interrupt void vPortYield( void )
\r
493 /* Save the context of the interrupted task. */
\r
494 portSAVE_CONTEXT();
\r
496 /* Switch to the highest priority task that is ready to run. */
\r
497 vTaskSwitchContext();
\r
499 /* Restore the context of the new task. */
\r
500 portRESTORE_CONTEXT();
\r
502 /*-----------------------------------------------------------*/
\r
504 __nosavereg __interrupt void vPortYieldDelayed( void )
\r
506 /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
\r
509 /* Save the context of the interrupted task. */
\r
510 portSAVE_CONTEXT();
\r
512 /* Enable interrupts */
\r
515 /* Clear delayed interrupt flag */
\r
516 __asm (" CLRB 03A4H:0 ");
\r
518 /* Switch to the highest priority task that is ready to run. */
\r
519 vTaskSwitchContext();
\r
521 /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
\r
524 /* Restore the context of the new task. */
\r
525 portRESTORE_CONTEXT();
\r
527 /* Enable interrupts */
\r
530 /*-----------------------------------------------------------*/
\r