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