2 FreeRTOS V7.4.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
75 #include "FreeRTOS.h"
\r
78 /*-----------------------------------------------------------
\r
79 * Implementation of functions defined in portable.h for the 16FX port.
\r
80 *----------------------------------------------------------*/
\r
83 * Get current value of DPR and ADB registers
\r
85 portSTACK_TYPE xGet_DPR_ADB_bank( void );
\r
88 * Get current value of DTB and PCB registers
\r
90 portSTACK_TYPE xGet_DTB_PCB_bank( void );
\r
93 * Sets up the periodic ISR used for the RTOS tick. This uses RLT0, but
\r
94 * can be done using any given RLT.
\r
96 static void prvSetupRLT0Interrupt( void );
\r
98 /*-----------------------------------------------------------*/
\r
101 * We require the address of the pxCurrentTCB variable, but don't want to know
\r
102 * any details of its type.
\r
104 typedef void tskTCB;
\r
105 extern volatile tskTCB * volatile pxCurrentTCB;
\r
107 /*-----------------------------------------------------------*/
\r
110 * Macro to save a task context to the task stack. This macro copies the
\r
111 * saved context (AH:AL, DPR:ADB, DTB:PCB , PC and PS) from the system
\r
112 * stack to task stack pointed by user stack pointer ( USP for SMALL and
\r
113 * MEDIUM memory model amd USB:USP for COMPACT and LARGE memory model ),
\r
114 * then it pushes the general purpose registers RW0-RW7 on to the task
\r
115 * stack. Finally the resultant stack pointer value is saved into the
\r
116 * task control block so it can be retrieved the next time the task
\r
119 #if( ( configMEMMODEL == portSMALL ) || ( configMEMMODEL == portMEDIUM ) )
\r
121 #define portSAVE_CONTEXT() \
\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(" OR CCR,#H'20 "); \
\r
138 __asm(" POPW A "); \
\r
139 __asm(" AND CCR,#H'DF "); \
\r
140 __asm(" PUSHW A "); \
\r
141 __asm(" OR CCR,#H'20 "); \
\r
142 __asm(" POPW A "); \
\r
143 __asm(" AND CCR,#H'DF "); \
\r
144 __asm(" PUSHW A "); \
\r
145 __asm(" PUSHW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
146 __asm(" MOVW A, _pxCurrentTCB "); \
\r
147 __asm(" MOVW A, SP "); \
\r
148 __asm(" SWAPW "); \
\r
149 __asm(" MOVW @AL, AH "); \
\r
150 __asm(" OR CCR,#H'20 "); \
\r
154 * Macro to restore a task context from the task stack. This is effecti-
\r
155 * vely the reverse of SAVE_CONTEXT(). First the stack pointer value
\r
156 * (USP for SMALL and MEDIUM memory model amd USB:USP for COMPACT and
\r
157 * LARGE memory model ) is loaded from the task control block. Next the
\r
158 * value of all the general purpose registers RW0-RW7 is retrieved. Fina-
\r
159 * lly it copies of the context ( AH:AL, DPR:ADB, DTB:PCB, PC and PS) of
\r
160 * the task to be executed upon RETI from user stack to system stack.
\r
163 #define portRESTORE_CONTEXT() \
\r
164 { __asm(" MOVW A, _pxCurrentTCB "); \
\r
165 __asm(" MOVW A, @A "); \
\r
166 __asm(" AND CCR,#H'DF "); \
\r
167 __asm(" MOVW SP, A "); \
\r
168 __asm(" POPW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\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
184 __asm(" AND CCR,#H'DF "); \
\r
185 __asm(" POPW A "); \
\r
186 __asm(" OR CCR,#H'20 "); \
\r
187 __asm(" PUSHW A "); \
\r
188 __asm(" AND CCR,#H'DF "); \
\r
189 __asm(" POPW A "); \
\r
190 __asm(" OR CCR,#H'20 "); \
\r
191 __asm(" PUSHW A "); \
\r
194 #elif( ( configMEMMODEL == portCOMPACT ) || ( configMEMMODEL == portLARGE ) )
\r
196 #define portSAVE_CONTEXT() \
\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(" OR CCR,#H'20 "); \
\r
213 __asm(" POPW A "); \
\r
214 __asm(" AND CCR,#H'DF "); \
\r
215 __asm(" PUSHW A "); \
\r
216 __asm(" OR CCR,#H'20 "); \
\r
217 __asm(" POPW A "); \
\r
218 __asm(" AND CCR,#H'DF "); \
\r
219 __asm(" PUSHW A "); \
\r
220 __asm(" PUSHW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\r
221 __asm(" MOVL A, _pxCurrentTCB "); \
\r
222 __asm(" MOVL RL2, A "); \
\r
223 __asm(" MOVW A, SP "); \
\r
224 __asm(" MOVW @RL2+0, A "); \
\r
225 __asm(" MOV A, USB "); \
\r
226 __asm(" MOV @RL2+2, A "); \
\r
229 #define portRESTORE_CONTEXT() \
\r
230 { __asm(" MOVL A, _pxCurrentTCB "); \
\r
231 __asm(" MOVL RL2, A "); \
\r
232 __asm(" MOVW A, @RL2+0 "); \
\r
233 __asm(" AND CCR,#H'DF "); \
\r
234 __asm(" MOVW SP, A "); \
\r
235 __asm(" MOV A, @RL2+2 "); \
\r
236 __asm(" MOV USB, A "); \
\r
237 __asm(" POPW (RW0,RW1,RW2,RW3,RW4,RW5,RW6,RW7) "); \
\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
253 __asm(" AND CCR,#H'DF "); \
\r
254 __asm(" POPW A "); \
\r
255 __asm(" OR CCR,#H'20 "); \
\r
256 __asm(" PUSHW A "); \
\r
257 __asm(" AND CCR,#H'DF "); \
\r
258 __asm(" POPW A "); \
\r
259 __asm(" OR CCR,#H'20 "); \
\r
260 __asm(" PUSHW A "); \
\r
264 /*-----------------------------------------------------------*/
\r
267 * Functions for obtaining the current value of DPR:ADB, DTB:PCB bank registers
\r
272 .GLOBAL _xGet_DPR_ADB_bank
\r
273 .GLOBAL _xGet_DTB_PCB_bank
\r
274 .SECTION CODE, CODE, ALIGN=1
\r
276 _xGet_DPR_ADB_bank:
\r
282 #if configMEMMODEL == portMEDIUM || configMEMMODEL == portLARGE
\r
284 #elif configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
289 _xGet_DTB_PCB_bank:
\r
295 #if configMEMMODEL == portMEDIUM || configMEMMODEL == portLARGE
\r
297 #elif configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
302 /*-----------------------------------------------------------*/
\r
305 * Initialise the stack of a task to look exactly as if a call to
\r
306 * portSAVE_CONTEXT had been called.
\r
308 * See the header file portable.h.
\r
310 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
312 /* Place a few bytes of known values on the bottom of the stack.
\r
313 This is just useful for debugging. */
\r
314 *pxTopOfStack = 0x1111;
\r
316 *pxTopOfStack = 0x2222;
\r
318 *pxTopOfStack = 0x3333;
\r
321 /* Once the task is called the task would push the pointer to the
\r
322 parameter onto the stack. Hence here the pointer would be copied to the stack
\r
323 first. When using the COMPACT or LARGE memory model the pointer would be 24
\r
324 bits, and when using the SMALL or MEDIUM memory model the pointer would be 16
\r
326 #if( ( configMEMMODEL == portCOMPACT ) || ( configMEMMODEL == portLARGE ) )
\r
328 *pxTopOfStack = ( portSTACK_TYPE ) ( ( unsigned long ) ( pvParameters ) >> 16 );
\r
333 *pxTopOfStack = ( portSTACK_TYPE ) ( pvParameters );
\r
336 /* This is redundant push to the stack. This is required in order to introduce
\r
337 an offset so that the task accesses a parameter correctly that is passed on to
\r
339 #if( ( configMEMMODEL == portMEDIUM ) || ( configMEMMODEL == portLARGE ) )
\r
341 *pxTopOfStack = ( xGet_DTB_PCB_bank() & 0xff00 ) | ( ( ( long ) ( pxCode ) >> 16 ) & 0xff );
\r
346 /* This is redundant push to the stack. This is required in order to introduce
\r
347 an offset so the task correctly accesses the parameter passed on the task stack. */
\r
348 *pxTopOfStack = ( portSTACK_TYPE ) ( pxCode );
\r
351 /* PS - User Mode, ILM=7, RB=0, Interrupts enabled,USP */
\r
352 *pxTopOfStack = 0xE0C0;
\r
356 *pxTopOfStack = ( portSTACK_TYPE ) ( pxCode );
\r
360 #if configMEMMODEL == portSMALL || configMEMMODEL == portCOMPACT
\r
362 *pxTopOfStack = xGet_DTB_PCB_bank();
\r
367 /* DTB | PCB, in case of MEDIUM and LARGE memory models, PCB would be used
\r
368 along with PC to indicate the start address of the function. */
\r
369 #if( ( configMEMMODEL == portMEDIUM ) || ( configMEMMODEL == portLARGE ) )
\r
371 *pxTopOfStack = ( xGet_DTB_PCB_bank() & 0xff00 ) | ( ( ( long ) ( pxCode ) >> 16 ) & 0xff );
\r
377 *pxTopOfStack = xGet_DPR_ADB_bank();
\r
381 *pxTopOfStack = ( portSTACK_TYPE ) 0x9999;
\r
385 *pxTopOfStack = ( portSTACK_TYPE ) 0xAAAA;
\r
388 /* Next the general purpose registers. */
\r
389 *pxTopOfStack = ( portSTACK_TYPE ) 0x7777; /* RW7 */
\r
391 *pxTopOfStack = ( portSTACK_TYPE ) 0x6666; /* RW6 */
\r
393 *pxTopOfStack = ( portSTACK_TYPE ) 0x5555; /* RW5 */
\r
395 *pxTopOfStack = ( portSTACK_TYPE ) 0x4444; /* RW4 */
\r
397 *pxTopOfStack = ( portSTACK_TYPE ) 0x3333; /* RW3 */
\r
399 *pxTopOfStack = ( portSTACK_TYPE ) 0x2222; /* RW2 */
\r
401 *pxTopOfStack = ( portSTACK_TYPE ) 0x1111; /* RW1 */
\r
403 *pxTopOfStack = ( portSTACK_TYPE ) 0x8888; /* RW0 */
\r
405 return pxTopOfStack;
\r
407 /*-----------------------------------------------------------*/
\r
409 static void prvSetupRLT0Interrupt( void )
\r
411 /* The peripheral clock divided by 16 is used by the timer. */
\r
412 const unsigned short usReloadValue = ( unsigned short ) ( ( ( configCLKP1_CLOCK_HZ / configTICK_RATE_HZ ) / 16UL ) - 1UL );
\r
414 /* set reload value = 34999+1, TICK Interrupt after 10 ms @ 56MHz of CLKP1 */
\r
415 TMRLR0 = usReloadValue;
\r
417 /* prescaler 1:16, reload, interrupt enable, count enable, trigger */
\r
420 /*-----------------------------------------------------------*/
\r
422 portBASE_TYPE xPortStartScheduler( void )
\r
424 /* Setup the hardware to generate the tick. */
\r
425 prvSetupRLT0Interrupt();
\r
427 /* Restore the context of the first task that is going to run. */
\r
428 portRESTORE_CONTEXT();
\r
430 /* Simulate a function call end as generated by the compiler. We will now
\r
431 jump to the start of the task the context of which we have just restored. */
\r
435 /* Should not get here. */
\r
438 /*-----------------------------------------------------------*/
\r
440 void vPortEndScheduler( void )
\r
442 /* Not implemented - unlikely to ever be required as there is nothing to
\r
446 /*-----------------------------------------------------------*/
\r
449 * The interrupt service routine used depends on whether the pre-emptive
\r
450 * scheduler is being used or not.
\r
453 #if configUSE_PREEMPTION == 1
\r
456 * Tick ISR for preemptive scheduler. We can use a __nosavereg attribute
\r
457 * as the context is to be saved by the portSAVE_CONTEXT() macro, not the
\r
458 * compiler generated code. The tick count is incremented after the context
\r
461 __nosavereg __interrupt void prvRLT0_TICKISR( void )
\r
463 /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
\r
466 /* Save the context of the interrupted task. */
\r
467 portSAVE_CONTEXT();
\r
469 /* Enable interrupts */
\r
472 /* Clear RLT0 interrupt flag */
\r
475 /* Increment the tick count then switch to the highest priority task
\r
476 that is ready to run. */
\r
477 vTaskIncrementTick();
\r
478 vTaskSwitchContext();
\r
480 /* Disable interrupts so that portRESTORE_CONTEXT() is not interrupted */
\r
483 /* Restore the context of the new task. */
\r
484 portRESTORE_CONTEXT();
\r
486 /* Enable interrupts */
\r
493 * Tick ISR for the cooperative scheduler. All this does is increment the
\r
494 * tick count. We don't need to switch context, this can only be done by
\r
495 * manual calls to taskYIELD();
\r
497 __interrupt void prvRLT0_TICKISR( void )
\r
499 /* Clear RLT0 interrupt flag */
\r
502 vTaskIncrementTick();
\r
507 /*-----------------------------------------------------------*/
\r
510 * Manual context switch. We can use a __nosavereg attribute as the context
\r
511 * is to be saved by the portSAVE_CONTEXT() macro, not the compiler generated
\r
514 __nosavereg __interrupt void vPortYield( void )
\r
516 /* Save the context of the interrupted task. */
\r
517 portSAVE_CONTEXT();
\r
519 /* Switch to the highest priority task that is ready to run. */
\r
520 vTaskSwitchContext();
\r
522 /* Restore the context of the new task. */
\r
523 portRESTORE_CONTEXT();
\r
525 /*-----------------------------------------------------------*/
\r
527 __nosavereg __interrupt void vPortYieldDelayed( void )
\r
529 /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
\r
532 /* Save the context of the interrupted task. */
\r
533 portSAVE_CONTEXT();
\r
535 /* Enable interrupts */
\r
538 /* Clear delayed interrupt flag */
\r
539 __asm (" CLRB 03A4H:0 ");
\r
541 /* Switch to the highest priority task that is ready to run. */
\r
542 vTaskSwitchContext();
\r
544 /* Disable interrupts so that portSAVE_CONTEXT() is not interrupted */
\r
547 /* Restore the context of the new task. */
\r
548 portRESTORE_CONTEXT();
\r
550 /* Enable interrupts */
\r
553 /*-----------------------------------------------------------*/
\r