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