2 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS eBook *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\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 exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 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 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
56 + CallReturn Depth increased from 8 to 10 levels to accomodate wizC/fedC V12.
\r
59 + TBLPTRU is now initialised to zero during the initial stack creation of a new task. This solves
\r
60 an error on devices with more than 64kB ROM.
\r
63 + ucCriticalNesting is now initialised to 0x7F to prevent interrupts from being
\r
64 handled before the scheduler is started.
\r
69 /* Scheduler include files. */
\r
70 #include <FreeRTOS.h>
\r
75 /*---------------------------------------------------------------------------
\r
76 * Implementation of functions defined in portable.h for the WizC PIC18 port.
\r
77 *---------------------------------------------------------------------------*/
\r
80 * We require the address of the pxCurrentTCB variable, but don't want to
\r
81 * know any details of its type.
\r
83 typedef void tskTCB;
\r
84 extern volatile tskTCB * volatile pxCurrentTCB;
\r
87 * Define minimal-stack constants
\r
90 * STATUS, WREG, BSR, PRODH, PRODL, FSR0H, FSR0L,
\r
91 * FSR1H, FSR1L,TABLAT, (TBLPTRU), TBLPTRH, TBLPTRL,
\r
93 * sfr's within parenthesis only on devices > 64kB
\r
95 * Call/Return stack:
\r
96 * 2 bytes per entry on devices <= 64kB
\r
97 * 3 bytes per entry on devices > 64kB
\r
100 * 2 bytes: FunctionParameter for initial taskcode
\r
101 * 1 byte : Number of entries on call/return stack
\r
102 * 1 byte : ucCriticalNesting
\r
103 * 16 bytes: Free space on stack
\r
105 #if _ROMSIZE > 0x8000
\r
106 #define portSTACK_FSR_BYTES ( 15 )
\r
107 #define portSTACK_CALLRETURN_ENTRY_SIZE ( 3 )
\r
109 #define portSTACK_FSR_BYTES ( 13 )
\r
110 #define portSTACK_CALLRETURN_ENTRY_SIZE ( 2 )
\r
113 #define portSTACK_MINIMAL_CALLRETURN_DEPTH ( 10 )
\r
114 #define portSTACK_OTHER_BYTES ( 20 )
\r
116 unsigned short usCalcMinStackSize = 0;
\r
118 /*-----------------------------------------------------------*/
\r
121 * We initialise ucCriticalNesting to the middle value an
\r
122 * unsigned char can contain. This way portENTER_CRITICAL()
\r
123 * and portEXIT_CRITICAL() can be called without interrupts
\r
124 * being enabled before the scheduler starts.
\r
126 register unsigned char ucCriticalNesting = 0x7F;
\r
128 /*-----------------------------------------------------------*/
\r
131 * Initialise the stack of a new task.
\r
132 * See portSAVE_CONTEXT macro for description.
\r
134 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
136 unsigned char ucScratch;
\r
138 * Get the size of the RAMarea in page 0 used by the compiler
\r
139 * We do this here already to avoid W-register conflicts.
\r
142 movlw OVERHEADPAGE0-LOCOPTSIZE+MAXLOCOPTSIZE
\r
143 movwf PRODL,ACCESS ; PRODL is used as temp register
\r
148 * Place a few bytes of known values on the bottom of the stack.
\r
149 * This is just useful for debugging.
\r
151 // *pxTopOfStack-- = 0x11;
\r
152 // *pxTopOfStack-- = 0x22;
\r
153 // *pxTopOfStack-- = 0x33;
\r
156 * Simulate how the stack would look after a call to vPortYield()
\r
157 * generated by the compiler.
\r
161 * First store the function parameters. This is where the task expects
\r
162 * to find them when it starts running.
\r
164 *pxTopOfStack-- = ( portSTACK_TYPE ) ( (( unsigned short ) pvParameters >> 8) & 0x00ff );
\r
165 *pxTopOfStack-- = ( portSTACK_TYPE ) ( ( unsigned short ) pvParameters & 0x00ff );
\r
168 * Next are all the registers that form part of the task context.
\r
170 *pxTopOfStack-- = ( portSTACK_TYPE ) 0x11; /* STATUS. */
\r
171 *pxTopOfStack-- = ( portSTACK_TYPE ) 0x22; /* WREG. */
\r
172 *pxTopOfStack-- = ( portSTACK_TYPE ) 0x33; /* BSR. */
\r
173 *pxTopOfStack-- = ( portSTACK_TYPE ) 0x44; /* PRODH. */
\r
174 *pxTopOfStack-- = ( portSTACK_TYPE ) 0x55; /* PRODL. */
\r
175 *pxTopOfStack-- = ( portSTACK_TYPE ) 0x66; /* FSR0H. */
\r
176 *pxTopOfStack-- = ( portSTACK_TYPE ) 0x77; /* FSR0L. */
\r
177 *pxTopOfStack-- = ( portSTACK_TYPE ) 0x88; /* FSR1H. */
\r
178 *pxTopOfStack-- = ( portSTACK_TYPE ) 0x99; /* FSR1L. */
\r
179 *pxTopOfStack-- = ( portSTACK_TYPE ) 0xAA; /* TABLAT. */
\r
180 #if _ROMSIZE > 0x8000
\r
181 *pxTopOfStack-- = ( portSTACK_TYPE ) 0x00; /* TBLPTRU. */
\r
183 *pxTopOfStack-- = ( portSTACK_TYPE ) 0xCC; /* TBLPTRH. */
\r
184 *pxTopOfStack-- = ( portSTACK_TYPE ) 0xDD; /* TBLPTRL. */
\r
185 #if _ROMSIZE > 0x8000
\r
186 *pxTopOfStack-- = ( portSTACK_TYPE ) 0xEE; /* PCLATU. */
\r
188 *pxTopOfStack-- = ( portSTACK_TYPE ) 0xFF; /* PCLATH. */
\r
191 * Next the compiler's scratchspace.
\r
193 while(ucScratch-- > 0)
\r
195 *pxTopOfStack-- = ( portSTACK_TYPE ) 0;
\r
199 * The only function return address so far is the address of the task entry.
\r
200 * The order is TOSU/TOSH/TOSL. For devices > 64kB, TOSU is put on the
\r
201 * stack, too. TOSU is always written as zero here because wizC does not allow
\r
202 * functionpointers to point above 64kB in ROM.
\r
204 #if _ROMSIZE > 0x8000
\r
205 *pxTopOfStack-- = ( portSTACK_TYPE ) 0;
\r
207 *pxTopOfStack-- = ( portSTACK_TYPE ) ( ( ( unsigned short ) pxCode >> 8 ) & 0x00ff );
\r
208 *pxTopOfStack-- = ( portSTACK_TYPE ) ( ( unsigned short ) pxCode & 0x00ff );
\r
211 * Store the number of return addresses on the hardware stack.
\r
212 * So far only the address of the task entry point.
\r
214 *pxTopOfStack-- = ( portSTACK_TYPE ) 1;
\r
217 * The code generated by wizC does not maintain separate
\r
218 * stack and frame pointers. Therefore the portENTER_CRITICAL macro cannot
\r
219 * use the stack as per other ports. Instead a variable is used to keep
\r
220 * track of the critical section nesting. This variable has to be stored
\r
221 * as part of the task context and is initially set to zero.
\r
223 *pxTopOfStack-- = ( portSTACK_TYPE ) portNO_CRITICAL_SECTION_NESTING;
\r
225 return pxTopOfStack;
\r
227 /*-----------------------------------------------------------*/
\r
229 unsigned short usPortCALCULATE_MINIMAL_STACK_SIZE( void )
\r
232 * Fetch the size of compiler's scratchspace.
\r
235 movlw OVERHEADPAGE0-LOCOPTSIZE+MAXLOCOPTSIZE
\r
236 movlb usCalcMinStackSize>>8
\r
237 movwf usCalcMinStackSize,BANKED
\r
241 * Add minimum needed stackspace
\r
243 usCalcMinStackSize += ( portSTACK_FSR_BYTES )
\r
244 + ( portSTACK_MINIMAL_CALLRETURN_DEPTH * portSTACK_CALLRETURN_ENTRY_SIZE )
\r
245 + ( portSTACK_OTHER_BYTES );
\r
247 return(usCalcMinStackSize);
\r
250 /*-----------------------------------------------------------*/
\r
252 portBASE_TYPE xPortStartScheduler( void )
\r
254 extern void portSetupTick( void );
\r
257 * Setup a timer for the tick ISR for the preemptive scheduler.
\r
262 * Restore the context of the first task to run.
\r
264 portRESTORE_CONTEXT();
\r
267 * This point should never be reached during execution.
\r
272 /*-----------------------------------------------------------*/
\r
274 void vPortEndScheduler( void )
\r
277 * It is unlikely that the scheduler for the PIC port will get stopped
\r
278 * once running. When called a reset is done which is probably the
\r
279 * most valid action.
\r
281 _Pragma(asmline reset);
\r
284 /*-----------------------------------------------------------*/
\r
287 * Manual context switch. This is similar to the tick context switch,
\r
288 * but does not increment the tick count. It must be identical to the
\r
289 * tick context switch in how it stores the stack of a task.
\r
291 void vPortYield( void )
\r
294 * Save the context of the current task.
\r
296 portSAVE_CONTEXT( portINTERRUPTS_UNCHANGED );
\r
299 * Switch to the highest priority task that is ready to run.
\r
301 vTaskSwitchContext();
\r
304 * Start executing the task we have just switched to.
\r
306 portRESTORE_CONTEXT();
\r
309 /*-----------------------------------------------------------*/
\r
311 void *pvPortMalloc( unsigned short usWantedSize )
\r
317 pvReturn = malloc( ( malloc_t ) usWantedSize );
\r
324 void vPortFree( void *pv )
\r