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