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