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