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