]> git.sur5r.net Git - freertos/blob - Source/portable/WizC/PIC18/port.c
9c82529aaf5c478a8d344169cdf106e68ba096dc
[freertos] / Source / portable / WizC / PIC18 / port.c
1 /*\r
2         FreeRTOS.org V4.2.1 - Copyright (C) 2003-2007 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         See http://www.FreeRTOS.org for documentation, latest information, license \r
28         and contact details.  Please ensure to read the configuration and relevant \r
29         port sections of the online documentation.\r
30 \r
31         Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along\r
32         with commercial development and support options.\r
33         ***************************************************************************\r
34 */\r
35 \r
36 /*\r
37 Changes from V4.2.1\r
38         + CallReturn Depth increased from 10 to 12 levels to accomodate wizC/fedC V14.\r
39         +CodeOptions added to disable the wizC/fedC optimiser within asm\r
40 \r
41 Changes from V3.2.1\r
42         + CallReturn Depth increased from 8 to 10 levels to accomodate wizC/fedC V12.\r
43         \r
44 Changes from V3.2.0\r
45         + TBLPTRU is now initialised to zero during the initial stack creation of a new task. This solves\r
46         an error on devices with more than 64kB ROM.\r
47 \r
48 Changes from V3.0.0\r
49         + ucCriticalNesting is now initialised to 0x7F to prevent interrupts from being\r
50           handled before the scheduler is started.\r
51 \r
52 Changes from V3.0.1\r
53 */\r
54 \r
55 /* Scheduler include files. */\r
56 #include <FreeRTOS.h>\r
57 #include <task.h>\r
58 \r
59 #include <malloc.h>\r
60 \r
61 /*---------------------------------------------------------------------------\r
62  * Implementation of functions defined in portable.h for the WizC PIC18 port.\r
63  *---------------------------------------------------------------------------*/\r
64 \r
65 /*\r
66  * We require the address of the pxCurrentTCB variable, but don't want to\r
67  * know any details of its type.\r
68  */\r
69 typedef void tskTCB;\r
70 extern volatile tskTCB * volatile pxCurrentTCB;\r
71 \r
72 /*\r
73  * Define minimal-stack constants\r
74  * -----\r
75  * FSR's:\r
76  *              STATUS, WREG, BSR, PRODH, PRODL, FSR0H, FSR0L,\r
77  *              FSR1H, FSR1L,TABLAT, (TBLPTRU), TBLPTRH, TBLPTRL,\r
78  *              (PCLATU), PCLATH\r
79  *              sfr's within parenthesis only on devices > 64kB\r
80  * -----\r
81  * Call/Return stack:\r
82  *               2 bytes per entry on devices <= 64kB\r
83  *               3 bytes per entry on devices >  64kB\r
84  * -----\r
85  * Other bytes:\r
86  *               2 bytes: FunctionParameter for initial taskcode\r
87  *               1 byte : Number of entries on call/return stack\r
88  *               1 byte : ucCriticalNesting\r
89  *              16 bytes: Free space on stack\r
90  */\r
91 #if _ROMSIZE > 0x8000\r
92         #define portSTACK_FSR_BYTES                             ( 15 )\r
93         #define portSTACK_CALLRETURN_ENTRY_SIZE (  3 )\r
94 #else\r
95         #define portSTACK_FSR_BYTES                             ( 13 )\r
96         #define portSTACK_CALLRETURN_ENTRY_SIZE (  2 )\r
97 #endif\r
98 \r
99 #define portSTACK_MINIMAL_CALLRETURN_DEPTH      ( 12 )\r
100 #define portSTACK_OTHER_BYTES                           ( 20 )\r
101 \r
102 unsigned portSHORT usCalcMinStackSize           = 0;\r
103 \r
104 /*-----------------------------------------------------------*/\r
105 \r
106 /*\r
107  * We initialise ucCriticalNesting to the middle value an \r
108  * unsigned char can contain. This way portENTER_CRITICAL()\r
109  * and portEXIT_CRITICAL() can be called without interrupts\r
110  * being enabled before the scheduler starts.\r
111  */\r
112 register unsigned portCHAR ucCriticalNesting = 0x7F;\r
113 \r
114 /*-----------------------------------------------------------*/\r
115 \r
116 /* \r
117  * Initialise the stack of a new task.\r
118  * See portSAVE_CONTEXT macro for description. \r
119  */\r
120 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
121 {\r
122 unsigned portCHAR ucScratch;\r
123         /*\r
124          * Get the size of the RAMarea in page 0 used by the compiler\r
125          * We do this here already to avoid W-register conflicts.\r
126          */\r
127         _Pragma("asm")\r
128                 dupmodoff\r
129                 movlw   OVERHEADPAGE0-LOCOPTSIZE+MAXLOCOPTSIZE\r
130                 movwf   PRODL,ACCESS            ; PRODL is used as temp register\r
131                 dupmodon\r
132         _Pragma("asmend")\r
133         ucScratch = PRODL;\r
134 \r
135         /*\r
136          * Place a few bytes of known values on the bottom of the stack. \r
137          * This is just useful for debugging.\r
138          */\r
139 //      *pxTopOfStack-- = 0x11;\r
140 //      *pxTopOfStack-- = 0x22;\r
141 //      *pxTopOfStack-- = 0x33;\r
142 \r
143         /*\r
144          * Simulate how the stack would look after a call to vPortYield()\r
145          * generated by the compiler.\r
146          */\r
147 \r
148         /*\r
149          * First store the function parameters.  This is where the task expects\r
150          * to find them when it starts running.\r
151          */\r
152         *pxTopOfStack-- = ( portSTACK_TYPE ) ( (( unsigned portSHORT ) pvParameters >> 8) & 0x00ff );\r
153         *pxTopOfStack-- = ( portSTACK_TYPE ) (  ( unsigned portSHORT ) pvParameters       & 0x00ff );\r
154 \r
155         /*\r
156          * Next are all the registers that form part of the task context.\r
157          */\r
158         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x11; /* STATUS. */\r
159         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x22; /* WREG. */\r
160         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x33; /* BSR. */\r
161         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x44; /* PRODH. */\r
162         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x55; /* PRODL. */\r
163         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x66; /* FSR0H. */\r
164         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x77; /* FSR0L. */\r
165         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x88; /* FSR1H. */\r
166         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x99; /* FSR1L. */\r
167         *pxTopOfStack-- = ( portSTACK_TYPE ) 0xAA; /* TABLAT. */\r
168 #if _ROMSIZE > 0x8000\r
169         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x00; /* TBLPTRU. */\r
170 #endif\r
171         *pxTopOfStack-- = ( portSTACK_TYPE ) 0xCC; /* TBLPTRH. */\r
172         *pxTopOfStack-- = ( portSTACK_TYPE ) 0xDD; /* TBLPTRL. */\r
173 #if _ROMSIZE > 0x8000\r
174         *pxTopOfStack-- = ( portSTACK_TYPE ) 0xEE; /* PCLATU. */\r
175 #endif\r
176         *pxTopOfStack-- = ( portSTACK_TYPE ) 0xFF; /* PCLATH. */\r
177 \r
178         /*\r
179          * Next the compiler's scratchspace.\r
180          */\r
181         while(ucScratch-- > 0)\r
182         {\r
183                 *pxTopOfStack-- = ( portSTACK_TYPE ) 0;\r
184         }\r
185         \r
186         /*\r
187          * The only function return address so far is the address of the task entry.\r
188          * The order is TOSU/TOSH/TOSL. For devices > 64kB, TOSU is put on the \r
189          * stack, too. TOSU is always written as zero here because wizC does not allow\r
190          * functionpointers to point above 64kB in ROM.\r
191          */\r
192 #if _ROMSIZE > 0x8000\r
193         *pxTopOfStack-- = ( portSTACK_TYPE ) 0;\r
194 #endif\r
195         *pxTopOfStack-- = ( portSTACK_TYPE ) ( ( ( unsigned portSHORT ) pxCode >> 8 ) & 0x00ff );\r
196         *pxTopOfStack-- = ( portSTACK_TYPE ) ( (   unsigned portSHORT ) pxCode        & 0x00ff );\r
197 \r
198         /*\r
199          * Store the number of return addresses on the hardware stack.\r
200          * So far only the address of the task entry point.\r
201          */\r
202         *pxTopOfStack-- = ( portSTACK_TYPE ) 1;\r
203 \r
204         /*\r
205          * The code generated by wizC does not maintain separate\r
206          * stack and frame pointers. Therefore the portENTER_CRITICAL macro cannot \r
207          * use the stack as per other ports.  Instead a variable is used to keep\r
208          * track of the critical section nesting.  This variable has to be stored\r
209          * as part of the task context and is initially set to zero.\r
210          */\r
211         *pxTopOfStack-- = ( portSTACK_TYPE ) portNO_CRITICAL_SECTION_NESTING;   \r
212 \r
213         return pxTopOfStack;\r
214 }\r
215 /*-----------------------------------------------------------*/\r
216 \r
217 unsigned portSHORT usPortCALCULATE_MINIMAL_STACK_SIZE( void )\r
218 {\r
219         /*\r
220          * Fetch the size of compiler's scratchspace.\r
221          */\r
222         _Pragma("asm")\r
223                 dupmodoff\r
224                 movlw   OVERHEADPAGE0-LOCOPTSIZE+MAXLOCOPTSIZE\r
225                 movlb   usCalcMinStackSize>>8\r
226                 movwf   usCalcMinStackSize,BANKED\r
227                 dupmodon\r
228         _Pragma("asmend")\r
229 \r
230         /*\r
231          * Add minimum needed stackspace\r
232          */\r
233         usCalcMinStackSize      +=      ( portSTACK_FSR_BYTES )\r
234                 +       ( portSTACK_MINIMAL_CALLRETURN_DEPTH * portSTACK_CALLRETURN_ENTRY_SIZE )\r
235                 +       ( portSTACK_OTHER_BYTES );\r
236 \r
237         return(usCalcMinStackSize);\r
238 }\r
239 \r
240 /*-----------------------------------------------------------*/\r
241 \r
242 portBASE_TYPE xPortStartScheduler( void )\r
243 {\r
244         extern void portSetupTick( void );\r
245 \r
246         /*\r
247          * Setup a timer for the tick ISR for the preemptive scheduler.\r
248          */\r
249         portSetupTick(); \r
250 \r
251         /*\r
252          * Restore the context of the first task to run.\r
253          */\r
254         portRESTORE_CONTEXT();\r
255 \r
256         /*\r
257          * This point should never be reached during execution.\r
258          */\r
259         return pdTRUE;\r
260 }\r
261 \r
262 /*-----------------------------------------------------------*/\r
263 \r
264 void vPortEndScheduler( void )\r
265 {\r
266         /*\r
267          * It is unlikely that the scheduler for the PIC port will get stopped\r
268          * once running. When called a reset is done which is probably the\r
269          * most valid action.\r
270          */\r
271         _Pragma(asmline reset);\r
272 }\r
273 \r
274 /*-----------------------------------------------------------*/\r
275 \r
276 /*\r
277  * Manual context switch.  This is similar to the tick context switch,\r
278  * but does not increment the tick count.  It must be identical to the\r
279  * tick context switch in how it stores the stack of a task.\r
280  */\r
281 void vPortYield( void )\r
282 {\r
283         /*\r
284          * Save the context of the current task.\r
285          */\r
286         portSAVE_CONTEXT( portINTERRUPTS_UNCHANGED );\r
287 \r
288         /*\r
289          * Switch to the highest priority task that is ready to run.\r
290          */\r
291         vTaskSwitchContext();\r
292 \r
293         /*\r
294          * Start executing the task we have just switched to.\r
295          */\r
296         portRESTORE_CONTEXT();\r
297 }\r
298 \r
299 /*-----------------------------------------------------------*/\r
300 \r
301 void *pvPortMalloc( unsigned portSHORT usWantedSize )\r
302 {\r
303 void *pvReturn;\r
304 \r
305         vTaskSuspendAll();\r
306         {\r
307                 pvReturn = malloc( ( malloc_t ) usWantedSize );\r
308         }\r
309         xTaskResumeAll();\r
310 \r
311         return pvReturn;\r
312 }\r
313 \r
314 void vPortFree( void *pv )\r
315 {\r
316         if( pv )\r
317         {\r
318                 vTaskSuspendAll();\r
319                 {\r
320                         free( pv );\r
321                 }\r
322                 xTaskResumeAll();\r
323         }\r
324 }\r