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