]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/WizC/PIC18/port.c
Prepare for V7.2.0 release.
[freertos] / FreeRTOS / Source / portable / WizC / PIC18 / port.c
1 /*\r
2     FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\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
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \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
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43     \r
44     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \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
65 */\r
66 \r
67 /*\r
68 Changes from V3.2.1\r
69         + CallReturn Depth increased from 8 to 10 levels to accomodate wizC/fedC V12.\r
70         \r
71 Changes from V3.2.0\r
72         + TBLPTRU is now initialised to zero during the initial stack creation of a new task. This solves\r
73         an error on devices with more than 64kB ROM.\r
74 \r
75 Changes from V3.0.0\r
76         + ucCriticalNesting is now initialised to 0x7F to prevent interrupts from being\r
77           handled before the scheduler is started.\r
78 \r
79 Changes from V3.0.1\r
80 */\r
81 \r
82 /* Scheduler include files. */\r
83 #include <FreeRTOS.h>\r
84 #include <task.h>\r
85 \r
86 #include <malloc.h>\r
87 \r
88 /*---------------------------------------------------------------------------\r
89  * Implementation of functions defined in portable.h for the WizC PIC18 port.\r
90  *---------------------------------------------------------------------------*/\r
91 \r
92 /*\r
93  * We require the address of the pxCurrentTCB variable, but don't want to\r
94  * know any details of its type.\r
95  */\r
96 typedef void tskTCB;\r
97 extern volatile tskTCB * volatile pxCurrentTCB;\r
98 \r
99 /*\r
100  * Define minimal-stack constants\r
101  * -----\r
102  * FSR's:\r
103  *              STATUS, WREG, BSR, PRODH, PRODL, FSR0H, FSR0L,\r
104  *              FSR1H, FSR1L,TABLAT, (TBLPTRU), TBLPTRH, TBLPTRL,\r
105  *              (PCLATU), PCLATH\r
106  *              sfr's within parenthesis only on devices > 64kB\r
107  * -----\r
108  * Call/Return stack:\r
109  *               2 bytes per entry on devices <= 64kB\r
110  *               3 bytes per entry on devices >  64kB\r
111  * -----\r
112  * Other bytes:\r
113  *               2 bytes: FunctionParameter for initial taskcode\r
114  *               1 byte : Number of entries on call/return stack\r
115  *               1 byte : ucCriticalNesting\r
116  *              16 bytes: Free space on stack\r
117  */\r
118 #if _ROMSIZE > 0x8000\r
119         #define portSTACK_FSR_BYTES                             ( 15 )\r
120         #define portSTACK_CALLRETURN_ENTRY_SIZE (  3 )\r
121 #else\r
122         #define portSTACK_FSR_BYTES                             ( 13 )\r
123         #define portSTACK_CALLRETURN_ENTRY_SIZE (  2 )\r
124 #endif\r
125 \r
126 #define portSTACK_MINIMAL_CALLRETURN_DEPTH      ( 10 )\r
127 #define portSTACK_OTHER_BYTES                           ( 20 )\r
128 \r
129 unsigned short usCalcMinStackSize               = 0;\r
130 \r
131 /*-----------------------------------------------------------*/\r
132 \r
133 /*\r
134  * We initialise ucCriticalNesting to the middle value an \r
135  * unsigned char can contain. This way portENTER_CRITICAL()\r
136  * and portEXIT_CRITICAL() can be called without interrupts\r
137  * being enabled before the scheduler starts.\r
138  */\r
139 register unsigned char ucCriticalNesting = 0x7F;\r
140 \r
141 /*-----------------------------------------------------------*/\r
142 \r
143 /* \r
144  * Initialise the stack of a new task.\r
145  * See portSAVE_CONTEXT macro for description. \r
146  */\r
147 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
148 {\r
149 unsigned char ucScratch;\r
150         /*\r
151          * Get the size of the RAMarea in page 0 used by the compiler\r
152          * We do this here already to avoid W-register conflicts.\r
153          */\r
154         _Pragma("asm")\r
155                 movlw   OVERHEADPAGE0-LOCOPTSIZE+MAXLOCOPTSIZE\r
156                 movwf   PRODL,ACCESS            ; PRODL is used as temp register\r
157         _Pragma("asmend")\r
158         ucScratch = PRODL;\r
159 \r
160         /*\r
161          * Place a few bytes of known values on the bottom of the stack. \r
162          * This is just useful for debugging.\r
163          */\r
164 //      *pxTopOfStack-- = 0x11;\r
165 //      *pxTopOfStack-- = 0x22;\r
166 //      *pxTopOfStack-- = 0x33;\r
167 \r
168         /*\r
169          * Simulate how the stack would look after a call to vPortYield()\r
170          * generated by the compiler.\r
171          */\r
172 \r
173         /*\r
174          * First store the function parameters.  This is where the task expects\r
175          * to find them when it starts running.\r
176          */\r
177         *pxTopOfStack-- = ( portSTACK_TYPE ) ( (( unsigned short ) pvParameters >> 8) & 0x00ff );\r
178         *pxTopOfStack-- = ( portSTACK_TYPE ) (  ( unsigned short ) pvParameters       & 0x00ff );\r
179 \r
180         /*\r
181          * Next are all the registers that form part of the task context.\r
182          */\r
183         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x11; /* STATUS. */\r
184         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x22; /* WREG. */\r
185         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x33; /* BSR. */\r
186         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x44; /* PRODH. */\r
187         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x55; /* PRODL. */\r
188         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x66; /* FSR0H. */\r
189         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x77; /* FSR0L. */\r
190         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x88; /* FSR1H. */\r
191         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x99; /* FSR1L. */\r
192         *pxTopOfStack-- = ( portSTACK_TYPE ) 0xAA; /* TABLAT. */\r
193 #if _ROMSIZE > 0x8000\r
194         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x00; /* TBLPTRU. */\r
195 #endif\r
196         *pxTopOfStack-- = ( portSTACK_TYPE ) 0xCC; /* TBLPTRH. */\r
197         *pxTopOfStack-- = ( portSTACK_TYPE ) 0xDD; /* TBLPTRL. */\r
198 #if _ROMSIZE > 0x8000\r
199         *pxTopOfStack-- = ( portSTACK_TYPE ) 0xEE; /* PCLATU. */\r
200 #endif\r
201         *pxTopOfStack-- = ( portSTACK_TYPE ) 0xFF; /* PCLATH. */\r
202 \r
203         /*\r
204          * Next the compiler's scratchspace.\r
205          */\r
206         while(ucScratch-- > 0)\r
207         {\r
208                 *pxTopOfStack-- = ( portSTACK_TYPE ) 0;\r
209         }\r
210         \r
211         /*\r
212          * The only function return address so far is the address of the task entry.\r
213          * The order is TOSU/TOSH/TOSL. For devices > 64kB, TOSU is put on the \r
214          * stack, too. TOSU is always written as zero here because wizC does not allow\r
215          * functionpointers to point above 64kB in ROM.\r
216          */\r
217 #if _ROMSIZE > 0x8000\r
218         *pxTopOfStack-- = ( portSTACK_TYPE ) 0;\r
219 #endif\r
220         *pxTopOfStack-- = ( portSTACK_TYPE ) ( ( ( unsigned short ) pxCode >> 8 ) & 0x00ff );\r
221         *pxTopOfStack-- = ( portSTACK_TYPE ) ( (   unsigned short ) pxCode        & 0x00ff );\r
222 \r
223         /*\r
224          * Store the number of return addresses on the hardware stack.\r
225          * So far only the address of the task entry point.\r
226          */\r
227         *pxTopOfStack-- = ( portSTACK_TYPE ) 1;\r
228 \r
229         /*\r
230          * The code generated by wizC does not maintain separate\r
231          * stack and frame pointers. Therefore the portENTER_CRITICAL macro cannot \r
232          * use the stack as per other ports.  Instead a variable is used to keep\r
233          * track of the critical section nesting.  This variable has to be stored\r
234          * as part of the task context and is initially set to zero.\r
235          */\r
236         *pxTopOfStack-- = ( portSTACK_TYPE ) portNO_CRITICAL_SECTION_NESTING;   \r
237 \r
238         return pxTopOfStack;\r
239 }\r
240 /*-----------------------------------------------------------*/\r
241 \r
242 unsigned short usPortCALCULATE_MINIMAL_STACK_SIZE( void )\r
243 {\r
244         /*\r
245          * Fetch the size of compiler's scratchspace.\r
246          */\r
247         _Pragma("asm")\r
248                 movlw   OVERHEADPAGE0-LOCOPTSIZE+MAXLOCOPTSIZE\r
249                 movlb   usCalcMinStackSize>>8\r
250                 movwf   usCalcMinStackSize,BANKED\r
251         _Pragma("asmend")\r
252 \r
253         /*\r
254          * Add minimum needed stackspace\r
255          */\r
256         usCalcMinStackSize      +=      ( portSTACK_FSR_BYTES )\r
257                 +       ( portSTACK_MINIMAL_CALLRETURN_DEPTH * portSTACK_CALLRETURN_ENTRY_SIZE )\r
258                 +       ( portSTACK_OTHER_BYTES );\r
259 \r
260         return(usCalcMinStackSize);\r
261 }\r
262 \r
263 /*-----------------------------------------------------------*/\r
264 \r
265 portBASE_TYPE xPortStartScheduler( void )\r
266 {\r
267         extern void portSetupTick( void );\r
268 \r
269         /*\r
270          * Setup a timer for the tick ISR for the preemptive scheduler.\r
271          */\r
272         portSetupTick(); \r
273 \r
274         /*\r
275          * Restore the context of the first task to run.\r
276          */\r
277         portRESTORE_CONTEXT();\r
278 \r
279         /*\r
280          * This point should never be reached during execution.\r
281          */\r
282         return pdTRUE;\r
283 }\r
284 \r
285 /*-----------------------------------------------------------*/\r
286 \r
287 void vPortEndScheduler( void )\r
288 {\r
289         /*\r
290          * It is unlikely that the scheduler for the PIC port will get stopped\r
291          * once running. When called a reset is done which is probably the\r
292          * most valid action.\r
293          */\r
294         _Pragma(asmline reset);\r
295 }\r
296 \r
297 /*-----------------------------------------------------------*/\r
298 \r
299 /*\r
300  * Manual context switch.  This is similar to the tick context switch,\r
301  * but does not increment the tick count.  It must be identical to the\r
302  * tick context switch in how it stores the stack of a task.\r
303  */\r
304 void vPortYield( void )\r
305 {\r
306         /*\r
307          * Save the context of the current task.\r
308          */\r
309         portSAVE_CONTEXT( portINTERRUPTS_UNCHANGED );\r
310 \r
311         /*\r
312          * Switch to the highest priority task that is ready to run.\r
313          */\r
314         vTaskSwitchContext();\r
315 \r
316         /*\r
317          * Start executing the task we have just switched to.\r
318          */\r
319         portRESTORE_CONTEXT();\r
320 }\r
321 \r
322 /*-----------------------------------------------------------*/\r
323 \r
324 void *pvPortMalloc( unsigned short usWantedSize )\r
325 {\r
326 void *pvReturn;\r
327 \r
328         vTaskSuspendAll();\r
329         {\r
330                 pvReturn = malloc( ( malloc_t ) usWantedSize );\r
331         }\r
332         xTaskResumeAll();\r
333 \r
334         return pvReturn;\r
335 }\r
336 \r
337 void vPortFree( void *pv )\r
338 {\r
339         if( pv )\r
340         {\r
341                 vTaskSuspendAll();\r
342                 {\r
343                         free( pv );\r
344                 }\r
345                 xTaskResumeAll();\r
346         }\r
347 }\r