]> git.sur5r.net Git - freertos/blob - Source/portable/MPLAB/PIC18F/port.c
Ready for V5.2.0 release.
[freertos] / Source / portable / MPLAB / PIC18F / port.c
1 /*\r
2         FreeRTOS.org V5.2.0 - 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 \r
10         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
11         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or \r
12         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for \r
13         more details.\r
14 \r
15         You should have received a copy of the GNU General Public License along \r
16         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59 \r
17         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
18 \r
19         A special exception to the GPL is included to allow you to distribute a \r
20         combined work that includes FreeRTOS.org without being obliged to provide\r
21         the source code for any proprietary components.  See the licensing section\r
22         of http://www.FreeRTOS.org for full details.\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 between V1.2.4 and V1.2.5\r
54 \r
55         + Introduced portGLOBAL_INTERRUPT_FLAG definition to test the global \r
56           interrupt flag setting.  Using the two bits defined within\r
57           portINITAL_INTERRUPT_STATE was causing the w register to get clobbered\r
58           before the test was performed.\r
59 \r
60 Changes from V1.2.5\r
61 \r
62         + Set the interrupt vector address to 0x08.  Previously it was at the\r
63           incorrect address for compatibility mode of 0x18.\r
64 \r
65 Changes from V2.1.1\r
66 \r
67         + PCLATU and PCLATH are now saved as part of the context.  This allows\r
68           function pointers to be used within tasks.  Thanks to Javier Espeche\r
69           for the enhancement. \r
70 \r
71 Changes from V2.3.1\r
72 \r
73         + TABLAT is now saved as part of the task context.\r
74         \r
75 Changes from V3.2.0\r
76 \r
77         + TBLPTRU is now initialised to zero as the MPLAB compiler expects this\r
78           value and does not write to the register.\r
79 */\r
80 \r
81 /* Scheduler include files. */\r
82 #include "FreeRTOS.h"\r
83 #include "task.h"\r
84 \r
85 /* MPLAB library include file. */\r
86 #include "timers.h"\r
87 \r
88 /*-----------------------------------------------------------\r
89  * Implementation of functions defined in portable.h for the PIC port.\r
90  *----------------------------------------------------------*/\r
91 \r
92 /* Hardware setup for tick. */\r
93 #define portTIMER_FOSC_SCALE                    ( ( unsigned portLONG ) 4 )\r
94 \r
95 /* Initial interrupt enable state for newly created tasks.  This value is\r
96 copied into INTCON when a task switches in for the first time. */\r
97 #define portINITAL_INTERRUPT_STATE                      0xc0\r
98 \r
99 /* Just the bit within INTCON for the global interrupt flag. */\r
100 #define portGLOBAL_INTERRUPT_FLAG                       0x80\r
101 \r
102 /* Constant used for context switch macro when we require the interrupt \r
103 enable state to be unchanged when the interrupted task is switched back in. */\r
104 #define portINTERRUPTS_UNCHANGED                        0x00\r
105 \r
106 /* Some memory areas get saved as part of the task context.  These memory\r
107 area's get used by the compiler for temporary storage, especially when \r
108 performing mathematical operations, or when using 32bit data types.  This\r
109 constant defines the size of memory area which must be saved. */\r
110 #define portCOMPILER_MANAGED_MEMORY_SIZE        ( ( unsigned portCHAR ) 0x13 )\r
111 \r
112 /* We require the address of the pxCurrentTCB variable, but don't want to know\r
113 any details of its type. */\r
114 typedef void tskTCB;\r
115 extern volatile tskTCB * volatile pxCurrentTCB;\r
116 \r
117 /* IO port constants. */\r
118 #define portBIT_SET             ( ( unsigned portCHAR ) 1 )\r
119 #define portBIT_CLEAR   ( ( unsigned portCHAR ) 0 )\r
120 \r
121 /*\r
122  * The serial port ISR's are defined in serial.c, but are called from portable\r
123  * as they use the same vector as the tick ISR.\r
124  */\r
125 void vSerialTxISR( void );\r
126 void vSerialRxISR( void );\r
127 \r
128 /*\r
129  * Perform hardware setup to enable ticks.\r
130  */\r
131 static void prvSetupTimerInterrupt( void );\r
132 \r
133 /* \r
134  * ISR to maintain the tick, and perform tick context switches if the\r
135  * preemptive scheduler is being used.\r
136  */\r
137 static void prvTickISR( void );\r
138 \r
139 /*\r
140  * ISR placed on the low priority vector.  This calls the appropriate ISR for\r
141  * the actual interrupt.\r
142  */\r
143 static void prvLowInterrupt( void );\r
144 \r
145 /* \r
146  * Macro that pushes all the registers that make up the context of a task onto\r
147  * the stack, then saves the new top of stack into the TCB.\r
148  * \r
149  * If this is called from an ISR then the interrupt enable bits must have been \r
150  * set for the ISR to ever get called.  Therefore we want to save the INTCON\r
151  * register with the enable bits forced to be set - and ucForcedInterruptFlags \r
152  * must contain these bit settings.  This means the interrupts will again be\r
153  * enabled when the interrupted task is switched back in.\r
154  *\r
155  * If this is called from a manual context switch (i.e. from a call to yield),\r
156  * then we want to save the INTCON so it is restored with its current state,\r
157  * and ucForcedInterruptFlags must be 0.  This allows a yield from within\r
158  * a critical section.\r
159  *\r
160  * The compiler uses some locations at the bottom of the memory for temporary\r
161  * storage during math and other computations.  This is especially true if\r
162  * 32bit data types are utilised (as they are by the scheduler).  The .tmpdata\r
163  * and MATH_DATA sections have to be stored in there entirety as part of a task\r
164  * context.  This macro stores from data address 0x00 to \r
165  * portCOMPILER_MANAGED_MEMORY_SIZE.  This is sufficient for the demo \r
166  * applications but you should check the map file for your project to ensure \r
167  * this is sufficient for your needs.  It is not clear whether this size is \r
168  * fixed for all compilations or has the potential to be program specific.\r
169  */\r
170 #define portSAVE_CONTEXT( ucForcedInterruptFlags )                                                              \\r
171 {                                                                                                                                                               \\r
172         _asm                                                                                                                                            \\r
173                 /* Save the status and WREG registers first, as these will get modified \\r
174                 by the operations below. */                                                                                             \\r
175                 MOVFF   WREG, PREINC1                                                                                                   \\r
176                 MOVFF   STATUS, PREINC1                                                                                                 \\r
177                 /* Save the INTCON register with the appropriate bits forced if                 \\r
178                 necessary - as described above. */                                                                              \\r
179                 MOVFF   INTCON, WREG                                                                                                    \\r
180                 IORLW   ucForcedInterruptFlags                                                                                  \\r
181                 MOVFF   WREG, PREINC1                                                                                                   \\r
182         _endasm                                                                                                                                         \\r
183                                                                                                                                                                 \\r
184         portDISABLE_INTERRUPTS();                                                                                                       \\r
185                                                                                                                                                                 \\r
186         _asm                                                                                                                                            \\r
187                 /* Store the necessary registers to the stack. */                                               \\r
188                 MOVFF   BSR, PREINC1                                                                                                    \\r
189                 MOVFF   FSR2L, PREINC1                                                                                                  \\r
190                 MOVFF   FSR2H, PREINC1                                                                                                  \\r
191                 MOVFF   FSR0L, PREINC1                                                                                                  \\r
192                 MOVFF   FSR0H, PREINC1                                                                                                  \\r
193                 MOVFF   TABLAT, PREINC1                                                                                                 \\r
194                 MOVFF   TBLPTRU, PREINC1                                                                                                \\r
195                 MOVFF   TBLPTRH, PREINC1                                                                                                \\r
196                 MOVFF   TBLPTRL, PREINC1                                                                                                \\r
197                 MOVFF   PRODH, PREINC1                                                                                                  \\r
198                 MOVFF   PRODL, PREINC1                                                                                                  \\r
199                 MOVFF   PCLATU, PREINC1                                                                                                 \\r
200                 MOVFF   PCLATH, PREINC1                                                                                                 \\r
201                 /* Store the .tempdata and MATH_DATA areas as described above. */               \\r
202                 CLRF    FSR0L, 0                                                                                                                \\r
203                 CLRF    FSR0H, 0                                                                                                                \\r
204                 MOVFF   POSTINC0, PREINC1                                                                                               \\r
205                 MOVFF   POSTINC0, PREINC1                                                                                               \\r
206                 MOVFF   POSTINC0, PREINC1                                                                                               \\r
207                 MOVFF   POSTINC0, PREINC1                                                                                               \\r
208                 MOVFF   POSTINC0, PREINC1                                                                                               \\r
209                 MOVFF   POSTINC0, PREINC1                                                                                               \\r
210                 MOVFF   POSTINC0, PREINC1                                                                                               \\r
211                 MOVFF   POSTINC0, PREINC1                                                                                               \\r
212                 MOVFF   POSTINC0, PREINC1                                                                                               \\r
213                 MOVFF   POSTINC0, PREINC1                                                                                               \\r
214                 MOVFF   POSTINC0, PREINC1                                                                                               \\r
215                 MOVFF   POSTINC0, PREINC1                                                                                               \\r
216                 MOVFF   POSTINC0, PREINC1                                                                                               \\r
217                 MOVFF   POSTINC0, PREINC1                                                                                               \\r
218                 MOVFF   POSTINC0, PREINC1                                                                                               \\r
219                 MOVFF   POSTINC0, PREINC1                                                                                               \\r
220                 MOVFF   POSTINC0, PREINC1                                                                                               \\r
221                 MOVFF   POSTINC0, PREINC1                                                                                               \\r
222                 MOVFF   POSTINC0, PREINC1                                                                                               \\r
223                 MOVFF   INDF0, PREINC1                                                                                                  \\r
224                 MOVFF   FSR0L, PREINC1                                                                                                  \\r
225                 MOVFF   FSR0H, PREINC1                                                                                                  \\r
226                 /* Store the hardware stack pointer in a temp register before we                \\r
227                 modify it. */                                                                                                                   \\r
228                 MOVFF   STKPTR, FSR0L                                                                                                   \\r
229         _endasm                                                                                                                                         \\r
230                                                                                                                                                                 \\r
231                 /* Store each address from the hardware stack. */                                               \\r
232                 while( STKPTR > ( unsigned portCHAR ) 0 )                                                               \\r
233                 {                                                                                                                                               \\r
234                         _asm                                                                                                                            \\r
235                                 MOVFF   TOSL, PREINC1                                                                                   \\r
236                                 MOVFF   TOSH, PREINC1                                                                                   \\r
237                                 MOVFF   TOSU, PREINC1                                                                                   \\r
238                                 POP                                                                                                                             \\r
239                         _endasm                                                                                                                         \\r
240                 }                                                                                                                                               \\r
241                                                                                                                                                                 \\r
242         _asm                                                                                                                                            \\r
243                 /* Store the number of addresses on the hardware stack (from the                \\r
244                 temporary register). */                                                                                                 \\r
245                 MOVFF   FSR0L, PREINC1                                                                                                  \\r
246                 MOVF    PREINC1, 1, 0                                                                                                   \\r
247         _endasm                                                                                                                                         \\r
248                                                                                                                                                                 \\r
249         /* Save the new top of the software stack in the TCB. */                                        \\r
250         _asm                                                                                                                                            \\r
251                 MOVFF   pxCurrentTCB, FSR0L                                                                                             \\r
252                 MOVFF   pxCurrentTCB + 1, FSR0H                                                                                 \\r
253                 MOVFF   FSR1L, POSTINC0                                                                                                 \\r
254                 MOVFF   FSR1H, POSTINC0                                                                                                 \\r
255         _endasm                                                                                                                                         \\r
256 }\r
257 /*-----------------------------------------------------------*/\r
258 \r
259 /*\r
260  * This is the reverse of portSAVE_CONTEXT.  See portSAVE_CONTEXT for more\r
261  * details.\r
262  */\r
263 #define portRESTORE_CONTEXT()                                                                                                   \\r
264 {                                                                                                                                                               \\r
265         _asm                                                                                                                                            \\r
266                 /* Set FSR0 to point to pxCurrentTCB->pxTopOfStack. */                                  \\r
267                 MOVFF   pxCurrentTCB, FSR0L                                                                                             \\r
268                 MOVFF   pxCurrentTCB + 1, FSR0H                                                                                 \\r
269                                                                                                                                                                 \\r
270                 /* De-reference FSR0 to set the address it holds into FSR1.                             \\r
271                 (i.e. *( pxCurrentTCB->pxTopOfStack ) ). */                                                             \\r
272                 MOVFF   POSTINC0, FSR1L                                                                                                 \\r
273                 MOVFF   POSTINC0, FSR1H                                                                                                 \\r
274                                                                                                                                                                 \\r
275                 /* How many return addresses are there on the hardware stack?  Discard  \\r
276                 the first byte as we are pointing to the next free space. */                    \\r
277                 MOVFF   POSTDEC1, FSR0L                                                                                                 \\r
278                 MOVFF   POSTDEC1, FSR0L                                                                                                 \\r
279         _endasm                                                                                                                                         \\r
280                                                                                                                                                                 \\r
281         /* Fill the hardware stack from our software stack. */                                          \\r
282         STKPTR = 0;                                                                                                                                     \\r
283                                                                                                                                                                 \\r
284         while( STKPTR < FSR0L )                                                                                                         \\r
285         {                                                                                                                                                       \\r
286                 _asm                                                                                                                                    \\r
287                         PUSH                                                                                                                            \\r
288                         MOVF    POSTDEC1, 0, 0                                                                                          \\r
289                         MOVWF   TOSU, 0                                                                                                         \\r
290                         MOVF    POSTDEC1, 0, 0                                                                                          \\r
291                         MOVWF   TOSH, 0                                                                                                         \\r
292                         MOVF    POSTDEC1, 0, 0                                                                                          \\r
293                         MOVWF   TOSL, 0                                                                                                         \\r
294                 _endasm                                                                                                                                 \\r
295         }                                                                                                                                                       \\r
296                                                                                                                                                                 \\r
297         _asm                                                                                                                                            \\r
298                 /* Restore the .tmpdata and MATH_DATA memory. */                                                \\r
299                 MOVFF   POSTDEC1, FSR0H                                                                                                 \\r
300                 MOVFF   POSTDEC1, FSR0L                                                                                                 \\r
301                 MOVFF   POSTDEC1, POSTDEC0                                                                                              \\r
302                 MOVFF   POSTDEC1, POSTDEC0                                                                                              \\r
303                 MOVFF   POSTDEC1, POSTDEC0                                                                                              \\r
304                 MOVFF   POSTDEC1, POSTDEC0                                                                                              \\r
305                 MOVFF   POSTDEC1, POSTDEC0                                                                                              \\r
306                 MOVFF   POSTDEC1, POSTDEC0                                                                                              \\r
307                 MOVFF   POSTDEC1, POSTDEC0                                                                                              \\r
308                 MOVFF   POSTDEC1, POSTDEC0                                                                                              \\r
309                 MOVFF   POSTDEC1, POSTDEC0                                                                                              \\r
310                 MOVFF   POSTDEC1, POSTDEC0                                                                                              \\r
311                 MOVFF   POSTDEC1, POSTDEC0                                                                                              \\r
312                 MOVFF   POSTDEC1, POSTDEC0                                                                                              \\r
313                 MOVFF   POSTDEC1, POSTDEC0                                                                                              \\r
314                 MOVFF   POSTDEC1, POSTDEC0                                                                                              \\r
315                 MOVFF   POSTDEC1, POSTDEC0                                                                                              \\r
316                 MOVFF   POSTDEC1, POSTDEC0                                                                                              \\r
317                 MOVFF   POSTDEC1, POSTDEC0                                                                                              \\r
318                 MOVFF   POSTDEC1, POSTDEC0                                                                                              \\r
319                 MOVFF   POSTDEC1, POSTDEC0                                                                                              \\r
320                 MOVFF   POSTDEC1, INDF0                                                                                                 \\r
321                 /* Restore the other registers forming the tasks context. */                    \\r
322                 MOVFF   POSTDEC1, PCLATH                                                                                                \\r
323                 MOVFF   POSTDEC1, PCLATU                                                                                                \\r
324                 MOVFF   POSTDEC1, PRODL                                                                                                 \\r
325                 MOVFF   POSTDEC1, PRODH                                                                                                 \\r
326                 MOVFF   POSTDEC1, TBLPTRL                                                                                               \\r
327                 MOVFF   POSTDEC1, TBLPTRH                                                                                               \\r
328                 MOVFF   POSTDEC1, TBLPTRU                                                                                               \\r
329                 MOVFF   POSTDEC1, TABLAT                                                                                                \\r
330                 MOVFF   POSTDEC1, FSR0H                                                                                                 \\r
331                 MOVFF   POSTDEC1, FSR0L                                                                                                 \\r
332                 MOVFF   POSTDEC1, FSR2H                                                                                                 \\r
333                 MOVFF   POSTDEC1, FSR2L                                                                                                 \\r
334                 MOVFF   POSTDEC1, BSR                                                                                                   \\r
335                 /* The next byte is the INTCON register.  Read this into WREG as some   \\r
336                 manipulation is required. */                                                                                    \\r
337                 MOVFF   POSTDEC1, WREG                                                                                                  \\r
338         _endasm                                                                                                                                         \\r
339                                                                                                                                                                 \\r
340         /* From the INTCON register, only the interrupt enable bits form part           \\r
341         of the tasks context.  It is perfectly legitimate for another task to           \\r
342         have modified any other bits.  We therefore only restore the top two bits.      \\r
343         */                                                                                                                                                      \\r
344         if( WREG & portGLOBAL_INTERRUPT_FLAG )                                                                          \\r
345         {                                                                                                                                                       \\r
346                 _asm                                                                                                                                    \\r
347                         MOVFF   POSTDEC1, STATUS                                                                                        \\r
348                         MOVFF   POSTDEC1, WREG                                                                                          \\r
349                         /* Return enabling interrupts. */                                                                       \\r
350                         RETFIE  0                                                                                                                       \\r
351                 _endasm                                                                                                                                 \\r
352         }                                                                                                                                                       \\r
353         else                                                                                                                                            \\r
354         {                                                                                                                                                       \\r
355                 _asm                                                                                                                                    \\r
356                         MOVFF   POSTDEC1, STATUS                                                                                        \\r
357                         MOVFF   POSTDEC1, WREG                                                                                          \\r
358                         /* Return without effecting interrupts.  The context may have           \\r
359                         been saved from a critical region. */                                                           \\r
360                         RETURN  0                                                                                                                       \\r
361                 _endasm                                                                                                                                 \\r
362         }                                                                                                                                                       \\r
363 }\r
364 /*-----------------------------------------------------------*/\r
365 \r
366 /* \r
367  * See header file for description. \r
368  */\r
369 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
370 {\r
371 unsigned portLONG ulAddress;\r
372 unsigned portCHAR ucBlock;\r
373 \r
374         /* Place a few bytes of known values on the bottom of the stack. \r
375         This is just useful for debugging. */\r
376 \r
377         *pxTopOfStack = 0x11;\r
378         pxTopOfStack++;\r
379         *pxTopOfStack = 0x22;\r
380         pxTopOfStack++;\r
381         *pxTopOfStack = 0x33;\r
382         pxTopOfStack++;\r
383 \r
384 \r
385         /* Simulate how the stack would look after a call to vPortYield() generated\r
386         by the compiler. \r
387 \r
388         First store the function parameters.  This is where the task will expect to\r
389         find them when it starts running. */\r
390         ulAddress = ( unsigned portLONG ) pvParameters;\r
391         *pxTopOfStack = ( portSTACK_TYPE ) ( ulAddress & ( unsigned portLONG ) 0x00ff );\r
392         pxTopOfStack++;\r
393 \r
394         ulAddress >>= 8;\r
395         *pxTopOfStack = ( portSTACK_TYPE ) ( ulAddress & ( unsigned portLONG ) 0x00ff );\r
396         pxTopOfStack++;\r
397 \r
398         /* Next we just leave a space.  When a context is saved the stack pointer\r
399         is incremented before it is used so as not to corrupt whatever the stack\r
400         pointer is actually pointing to.  This is especially necessary during \r
401         function epilogue code generated by the compiler. */\r
402         *pxTopOfStack = 0x44;\r
403         pxTopOfStack++;\r
404 \r
405         /* Next are all the registers that form part of the task context. */\r
406         \r
407         *pxTopOfStack = ( portSTACK_TYPE ) 0x66; /* WREG. */\r
408         pxTopOfStack++;\r
409 \r
410         *pxTopOfStack = ( portSTACK_TYPE ) 0xcc; /* Status. */\r
411         pxTopOfStack++;\r
412 \r
413         /* INTCON is saved with interrupts enabled. */\r
414         *pxTopOfStack = ( portSTACK_TYPE ) portINITAL_INTERRUPT_STATE; /* INTCON */\r
415         pxTopOfStack++;\r
416 \r
417         *pxTopOfStack = ( portSTACK_TYPE ) 0x11; /* BSR. */\r
418         pxTopOfStack++;\r
419 \r
420         *pxTopOfStack = ( portSTACK_TYPE ) 0x22; /* FSR2L. */\r
421         pxTopOfStack++;\r
422 \r
423         *pxTopOfStack = ( portSTACK_TYPE ) 0x33; /* FSR2H. */\r
424         pxTopOfStack++;\r
425 \r
426         *pxTopOfStack = ( portSTACK_TYPE ) 0x44; /* FSR0L. */\r
427         pxTopOfStack++;\r
428 \r
429         *pxTopOfStack = ( portSTACK_TYPE ) 0x55; /* FSR0H. */\r
430         pxTopOfStack++;\r
431 \r
432         *pxTopOfStack = ( portSTACK_TYPE ) 0x66; /* TABLAT. */\r
433         pxTopOfStack++;\r
434 \r
435         *pxTopOfStack = ( portSTACK_TYPE ) 0x00; /* TBLPTRU. */\r
436         pxTopOfStack++;\r
437 \r
438         *pxTopOfStack = ( portSTACK_TYPE ) 0x88; /* TBLPTRUH. */\r
439         pxTopOfStack++;\r
440 \r
441         *pxTopOfStack = ( portSTACK_TYPE ) 0x99; /* TBLPTRUL. */\r
442         pxTopOfStack++;\r
443 \r
444         *pxTopOfStack = ( portSTACK_TYPE ) 0xaa; /* PRODH. */\r
445         pxTopOfStack++;\r
446 \r
447         *pxTopOfStack = ( portSTACK_TYPE ) 0xbb; /* PRODL. */\r
448         pxTopOfStack++;\r
449 \r
450         *pxTopOfStack = ( portSTACK_TYPE ) 0x00; /* PCLATU. */\r
451         pxTopOfStack++;\r
452 \r
453         *pxTopOfStack = ( portSTACK_TYPE ) 0x00; /* PCLATH. */\r
454         pxTopOfStack++;\r
455 \r
456         /* Next the .tmpdata and MATH_DATA sections. */\r
457         for( ucBlock = 0; ucBlock <= portCOMPILER_MANAGED_MEMORY_SIZE; ucBlock++ )\r
458         {\r
459                 *pxTopOfStack = ( portSTACK_TYPE ) ucBlock;\r
460                 *pxTopOfStack++;\r
461         }\r
462 \r
463         /* Store the top of the global data section. */\r
464         *pxTopOfStack = ( portSTACK_TYPE ) portCOMPILER_MANAGED_MEMORY_SIZE; /* Low. */\r
465         pxTopOfStack++;\r
466 \r
467         *pxTopOfStack = ( portSTACK_TYPE ) 0x00; /* High. */\r
468         pxTopOfStack++;\r
469 \r
470         /* The only function return address so far is the address of the \r
471         task. */\r
472         ulAddress = ( unsigned portLONG ) pxCode;\r
473 \r
474         /* TOS low. */\r
475         *pxTopOfStack = ( portSTACK_TYPE ) ( ulAddress & ( unsigned portLONG ) 0x00ff );\r
476         pxTopOfStack++;\r
477         ulAddress >>= 8;\r
478 \r
479         /* TOS high. */\r
480         *pxTopOfStack = ( portSTACK_TYPE ) ( ulAddress & ( unsigned portLONG ) 0x00ff );\r
481         pxTopOfStack++;\r
482         ulAddress >>= 8;\r
483 \r
484         /* TOS even higher. */\r
485         *pxTopOfStack = ( portSTACK_TYPE ) ( ulAddress & ( unsigned portLONG ) 0x00ff );\r
486         pxTopOfStack++;\r
487 \r
488         /* Store the number of return addresses on the hardware stack - so far only\r
489         the address of the task entry point. */\r
490         *pxTopOfStack = ( portSTACK_TYPE ) 1;\r
491         pxTopOfStack++;\r
492 \r
493         return pxTopOfStack;\r
494 }\r
495 /*-----------------------------------------------------------*/\r
496 \r
497 portBASE_TYPE xPortStartScheduler( void )\r
498 {\r
499         /* Setup a timer for the tick ISR is using the preemptive scheduler. */\r
500         prvSetupTimerInterrupt(); \r
501 \r
502         /* Restore the context of the first task to run. */\r
503         portRESTORE_CONTEXT();\r
504 \r
505         /* Should not get here.  Use the function name to stop compiler warnings. */\r
506         ( void ) prvLowInterrupt;\r
507         ( void ) prvTickISR;\r
508 \r
509         return pdTRUE;\r
510 }\r
511 /*-----------------------------------------------------------*/\r
512 \r
513 void vPortEndScheduler( void )\r
514 {\r
515         /* It is unlikely that the scheduler for the PIC port will get stopped\r
516         once running.  If required disable the tick interrupt here, then return \r
517         to xPortStartScheduler(). */\r
518 }\r
519 /*-----------------------------------------------------------*/\r
520 \r
521 /*\r
522  * Manual context switch.  This is similar to the tick context switch,\r
523  * but does not increment the tick count.  It must be identical to the\r
524  * tick context switch in how it stores the stack of a task.\r
525  */\r
526 void vPortYield( void )\r
527 {\r
528         /* This can get called with interrupts either enabled or disabled.  We\r
529         will save the INTCON register with the interrupt enable bits unmodified. */\r
530         portSAVE_CONTEXT( portINTERRUPTS_UNCHANGED );\r
531 \r
532         /* Switch to the highest priority task that is ready to run. */\r
533         vTaskSwitchContext();\r
534 \r
535         /* Start executing the task we have just switched to. */\r
536         portRESTORE_CONTEXT();\r
537 }\r
538 /*-----------------------------------------------------------*/\r
539 \r
540 /*\r
541  * Vector for ISR.  Nothing here must alter any registers!\r
542  */\r
543 #pragma code high_vector=0x08\r
544 static void prvLowInterrupt( void )\r
545 {\r
546         /* Was the interrupt the tick? */\r
547         if( PIR1bits.CCP1IF )\r
548         {               \r
549                 _asm\r
550                         goto prvTickISR\r
551                 _endasm\r
552         }\r
553 \r
554         /* Was the interrupt a byte being received? */\r
555         if( PIR1bits.RCIF )\r
556         {\r
557                 _asm\r
558                         goto vSerialRxISR\r
559                 _endasm\r
560         }\r
561 \r
562         /* Was the interrupt the Tx register becoming empty? */\r
563         if( PIR1bits.TXIF )\r
564         {\r
565                 if( PIE1bits.TXIE )\r
566                 {\r
567                         _asm\r
568                                 goto vSerialTxISR\r
569                         _endasm\r
570                 }\r
571         }\r
572 }\r
573 #pragma code\r
574 \r
575 /*-----------------------------------------------------------*/\r
576 \r
577 /*\r
578  * ISR for the tick.\r
579  * This increments the tick count and, if using the preemptive scheduler, \r
580  * performs a context switch.  This must be identical to the manual \r
581  * context switch in how it stores the context of a task. \r
582  */\r
583 static void prvTickISR( void )\r
584 {\r
585         /* Interrupts must have been enabled for the ISR to fire, so we have to \r
586         save the context with interrupts enabled. */\r
587         portSAVE_CONTEXT( portGLOBAL_INTERRUPT_FLAG );\r
588         PIR1bits.CCP1IF = 0;\r
589 \r
590         /* Maintain the tick count. */\r
591         vTaskIncrementTick();\r
592 \r
593         #if configUSE_PREEMPTION == 1\r
594         {\r
595                 /* Switch to the highest priority task that is ready to run. */\r
596                 vTaskSwitchContext();\r
597         }\r
598         #endif\r
599 \r
600         portRESTORE_CONTEXT();\r
601 }\r
602 /*-----------------------------------------------------------*/\r
603 \r
604 /*\r
605  * Setup a timer for a regular tick.\r
606  */\r
607 static void prvSetupTimerInterrupt( void )\r
608 {\r
609 const unsigned portLONG ulConstCompareValue = ( ( configCPU_CLOCK_HZ / portTIMER_FOSC_SCALE ) / configTICK_RATE_HZ );\r
610 unsigned portLONG ulCompareValue;\r
611 unsigned portCHAR ucByte;\r
612 \r
613         /* Interrupts are disabled when this function is called.\r
614 \r
615         Setup CCP1 to provide the tick interrupt using a compare match on timer\r
616         1.\r
617 \r
618         Clear the time count then setup timer. */\r
619         TMR1H = ( unsigned portCHAR ) 0x00;\r
620         TMR1L = ( unsigned portCHAR ) 0x00;\r
621 \r
622         /* Set the compare match value. */\r
623         ulCompareValue = ulConstCompareValue;\r
624         CCPR1L = ( unsigned portCHAR ) ( ulCompareValue & ( unsigned portLONG ) 0xff );\r
625         ulCompareValue >>= ( unsigned portLONG ) 8;\r
626         CCPR1H = ( unsigned portCHAR ) ( ulCompareValue & ( unsigned portLONG ) 0xff ); \r
627 \r
628         CCP1CONbits.CCP1M0 = portBIT_SET;       /*< Compare match mode. */\r
629         CCP1CONbits.CCP1M1 = portBIT_SET;       /*< Compare match mode. */\r
630         CCP1CONbits.CCP1M2 = portBIT_CLEAR;     /*< Compare match mode. */\r
631         CCP1CONbits.CCP1M3 = portBIT_SET;       /*< Compare match mode. */\r
632         PIE1bits.CCP1IE = portBIT_SET;          /*< Interrupt enable. */\r
633 \r
634         /* We are only going to use the global interrupt bit, so set the peripheral\r
635         bit to true. */\r
636         INTCONbits.GIEL = portBIT_SET;\r
637 \r
638         /* Provided library function for setting up the timer that will produce the\r
639         tick. */\r
640         OpenTimer1( T1_16BIT_RW & T1_SOURCE_INT & T1_PS_1_1 & T1_CCP1_T3_CCP2 );\r
641 }\r
642 \r