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