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