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