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