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