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