2 FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
72 + CallReturn Depth increased from 8 to 10 levels to accomodate wizC/fedC V12.
\r
75 + TBLPTRU is now initialised to zero during the initial stack creation of a new task. This solves
\r
76 an error on devices with more than 64kB ROM.
\r
79 + ucCriticalNesting is now initialised to 0x7F to prevent interrupts from being
\r
80 handled before the scheduler is started.
\r
85 /* Scheduler include files. */
\r
86 #include <FreeRTOS.h>
\r
91 /*---------------------------------------------------------------------------
\r
92 * Implementation of functions defined in portable.h for the WizC PIC18 port.
\r
93 *---------------------------------------------------------------------------*/
\r
96 * We require the address of the pxCurrentTCB variable, but don't want to
\r
97 * know any details of its type.
\r
100 extern volatile TCB_t * volatile pxCurrentTCB;
\r
103 * Define minimal-stack constants
\r
106 * STATUS, WREG, BSR, PRODH, PRODL, FSR0H, FSR0L,
\r
107 * FSR1H, FSR1L,TABLAT, (TBLPTRU), TBLPTRH, TBLPTRL,
\r
109 * sfr's within parenthesis only on devices > 64kB
\r
111 * Call/Return stack:
\r
112 * 2 bytes per entry on devices <= 64kB
\r
113 * 3 bytes per entry on devices > 64kB
\r
116 * 2 bytes: FunctionParameter for initial taskcode
\r
117 * 1 byte : Number of entries on call/return stack
\r
118 * 1 byte : ucCriticalNesting
\r
119 * 16 bytes: Free space on stack
\r
121 #if _ROMSIZE > 0x8000
\r
122 #define portSTACK_FSR_BYTES ( 15 )
\r
123 #define portSTACK_CALLRETURN_ENTRY_SIZE ( 3 )
\r
125 #define portSTACK_FSR_BYTES ( 13 )
\r
126 #define portSTACK_CALLRETURN_ENTRY_SIZE ( 2 )
\r
129 #define portSTACK_MINIMAL_CALLRETURN_DEPTH ( 10 )
\r
130 #define portSTACK_OTHER_BYTES ( 20 )
\r
132 uint16_t usCalcMinStackSize = 0;
\r
134 /*-----------------------------------------------------------*/
\r
137 * We initialise ucCriticalNesting to the middle value an
\r
138 * uint8_t can contain. This way portENTER_CRITICAL()
\r
139 * and portEXIT_CRITICAL() can be called without interrupts
\r
140 * being enabled before the scheduler starts.
\r
142 register uint8_t ucCriticalNesting = 0x7F;
\r
144 /*-----------------------------------------------------------*/
\r
147 * Initialise the stack of a new task.
\r
148 * See portSAVE_CONTEXT macro for description.
\r
150 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
154 * Get the size of the RAMarea in page 0 used by the compiler
\r
155 * We do this here already to avoid W-register conflicts.
\r
158 movlw OVERHEADPAGE0-LOCOPTSIZE+MAXLOCOPTSIZE
\r
159 movwf PRODL,ACCESS ; PRODL is used as temp register
\r
164 * Place a few bytes of known values on the bottom of the stack.
\r
165 * This is just useful for debugging.
\r
167 // *pxTopOfStack-- = 0x11;
\r
168 // *pxTopOfStack-- = 0x22;
\r
169 // *pxTopOfStack-- = 0x33;
\r
172 * Simulate how the stack would look after a call to vPortYield()
\r
173 * generated by the compiler.
\r
177 * First store the function parameters. This is where the task expects
\r
178 * to find them when it starts running.
\r
180 *pxTopOfStack-- = ( StackType_t ) ( (( uint16_t ) pvParameters >> 8) & 0x00ff );
\r
181 *pxTopOfStack-- = ( StackType_t ) ( ( uint16_t ) pvParameters & 0x00ff );
\r
184 * Next are all the registers that form part of the task context.
\r
186 *pxTopOfStack-- = ( StackType_t ) 0x11; /* STATUS. */
\r
187 *pxTopOfStack-- = ( StackType_t ) 0x22; /* WREG. */
\r
188 *pxTopOfStack-- = ( StackType_t ) 0x33; /* BSR. */
\r
189 *pxTopOfStack-- = ( StackType_t ) 0x44; /* PRODH. */
\r
190 *pxTopOfStack-- = ( StackType_t ) 0x55; /* PRODL. */
\r
191 *pxTopOfStack-- = ( StackType_t ) 0x66; /* FSR0H. */
\r
192 *pxTopOfStack-- = ( StackType_t ) 0x77; /* FSR0L. */
\r
193 *pxTopOfStack-- = ( StackType_t ) 0x88; /* FSR1H. */
\r
194 *pxTopOfStack-- = ( StackType_t ) 0x99; /* FSR1L. */
\r
195 *pxTopOfStack-- = ( StackType_t ) 0xAA; /* TABLAT. */
\r
196 #if _ROMSIZE > 0x8000
\r
197 *pxTopOfStack-- = ( StackType_t ) 0x00; /* TBLPTRU. */
\r
199 *pxTopOfStack-- = ( StackType_t ) 0xCC; /* TBLPTRH. */
\r
200 *pxTopOfStack-- = ( StackType_t ) 0xDD; /* TBLPTRL. */
\r
201 #if _ROMSIZE > 0x8000
\r
202 *pxTopOfStack-- = ( StackType_t ) 0xEE; /* PCLATU. */
\r
204 *pxTopOfStack-- = ( StackType_t ) 0xFF; /* PCLATH. */
\r
207 * Next the compiler's scratchspace.
\r
209 while(ucScratch-- > 0)
\r
211 *pxTopOfStack-- = ( StackType_t ) 0;
\r
215 * The only function return address so far is the address of the task entry.
\r
216 * The order is TOSU/TOSH/TOSL. For devices > 64kB, TOSU is put on the
\r
217 * stack, too. TOSU is always written as zero here because wizC does not allow
\r
218 * functionpointers to point above 64kB in ROM.
\r
220 #if _ROMSIZE > 0x8000
\r
221 *pxTopOfStack-- = ( StackType_t ) 0;
\r
223 *pxTopOfStack-- = ( StackType_t ) ( ( ( uint16_t ) pxCode >> 8 ) & 0x00ff );
\r
224 *pxTopOfStack-- = ( StackType_t ) ( ( uint16_t ) pxCode & 0x00ff );
\r
227 * Store the number of return addresses on the hardware stack.
\r
228 * So far only the address of the task entry point.
\r
230 *pxTopOfStack-- = ( StackType_t ) 1;
\r
233 * The code generated by wizC does not maintain separate
\r
234 * stack and frame pointers. Therefore the portENTER_CRITICAL macro cannot
\r
235 * use the stack as per other ports. Instead a variable is used to keep
\r
236 * track of the critical section nesting. This variable has to be stored
\r
237 * as part of the task context and is initially set to zero.
\r
239 *pxTopOfStack-- = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
\r
241 return pxTopOfStack;
\r
243 /*-----------------------------------------------------------*/
\r
245 uint16_t usPortCALCULATE_MINIMAL_STACK_SIZE( void )
\r
248 * Fetch the size of compiler's scratchspace.
\r
251 movlw OVERHEADPAGE0-LOCOPTSIZE+MAXLOCOPTSIZE
\r
252 movlb usCalcMinStackSize>>8
\r
253 movwf usCalcMinStackSize,BANKED
\r
257 * Add minimum needed stackspace
\r
259 usCalcMinStackSize += ( portSTACK_FSR_BYTES )
\r
260 + ( portSTACK_MINIMAL_CALLRETURN_DEPTH * portSTACK_CALLRETURN_ENTRY_SIZE )
\r
261 + ( portSTACK_OTHER_BYTES );
\r
263 return(usCalcMinStackSize);
\r
266 /*-----------------------------------------------------------*/
\r
268 BaseType_t xPortStartScheduler( void )
\r
270 extern void portSetupTick( void );
\r
273 * Setup a timer for the tick ISR for the preemptive scheduler.
\r
278 * Restore the context of the first task to run.
\r
280 portRESTORE_CONTEXT();
\r
283 * This point should never be reached during execution.
\r
288 /*-----------------------------------------------------------*/
\r
290 void vPortEndScheduler( void )
\r
293 * It is unlikely that the scheduler for the PIC port will get stopped
\r
294 * once running. When called a reset is done which is probably the
\r
295 * most valid action.
\r
297 _Pragma(asmline reset);
\r
300 /*-----------------------------------------------------------*/
\r
303 * Manual context switch. This is similar to the tick context switch,
\r
304 * but does not increment the tick count. It must be identical to the
\r
305 * tick context switch in how it stores the stack of a task.
\r
307 void vPortYield( void )
\r
310 * Save the context of the current task.
\r
312 portSAVE_CONTEXT( portINTERRUPTS_UNCHANGED );
\r
315 * Switch to the highest priority task that is ready to run.
\r
317 vTaskSwitchContext();
\r
320 * Start executing the task we have just switched to.
\r
322 portRESTORE_CONTEXT();
\r
324 /*-----------------------------------------------------------*/
\r
326 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
328 void *pvPortMalloc( uint16_t usWantedSize )
\r
334 pvReturn = malloc( ( malloc_t ) usWantedSize );
\r
341 #endif /* configSUPPORT_STATIC_ALLOCATION */
\r
343 /*-----------------------------------------------------------*/
\r
345 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
\r
347 void vPortFree( void *pv )
\r
359 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */