2 * FreeRTOS Kernel V10.2.0
\r
3 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 Changes between V1.2.4 and V1.2.5
\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
38 + Set the interrupt vector address to 0x08. Previously it was at the
\r
39 incorrect address for compatibility mode of 0x18.
\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
49 + TABLAT is now saved as part of the task context.
\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
57 /* Scheduler include files. */
\r
58 #include "FreeRTOS.h"
\r
61 /* MPLAB library include file. */
\r
64 /*-----------------------------------------------------------
\r
65 * Implementation of functions defined in portable.h for the PIC port.
\r
66 *----------------------------------------------------------*/
\r
68 /* Hardware setup for tick. */
\r
69 #define portTIMER_FOSC_SCALE ( ( uint32_t ) 4 )
\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
75 /* Just the bit within INTCON for the global interrupt flag. */
\r
76 #define portGLOBAL_INTERRUPT_FLAG 0x80
\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
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
88 /* We require the address of the pxCurrentTCB variable, but don't want to know
\r
89 any details of its type. */
\r
91 extern volatile TCB_t * volatile pxCurrentTCB;
\r
93 /* IO port constants. */
\r
94 #define portBIT_SET ( ( uint8_t ) 1 )
\r
95 #define portBIT_CLEAR ( ( uint8_t ) 0 )
\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
101 void vSerialTxISR( void );
\r
102 void vSerialRxISR( void );
\r
105 * Perform hardware setup to enable ticks.
\r
107 static void prvSetupTimerInterrupt( void );
\r
110 * ISR to maintain the tick, and perform tick context switches if the
\r
111 * preemptive scheduler is being used.
\r
113 static void prvTickISR( void );
\r
116 * ISR placed on the low priority vector. This calls the appropriate ISR for
\r
117 * the actual interrupt.
\r
119 static void prvLowInterrupt( void );
\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
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
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
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
146 #define portSAVE_CONTEXT( ucForcedInterruptFlags ) \
\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
160 portDISABLE_INTERRUPTS(); \
\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
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
204 MOVFF STKPTR, FSR0L \
\r
207 /* Store each address from the hardware stack. */ \
\r
208 while( STKPTR > ( uint8_t ) 0 ) \
\r
211 MOVFF TOSL, PREINC1 \
\r
212 MOVFF TOSH, PREINC1 \
\r
213 MOVFF TOSU, PREINC1 \
\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
225 /* Save the new top of the software stack in the TCB. */ \
\r
227 MOVFF pxCurrentTCB, FSR0L \
\r
228 MOVFF pxCurrentTCB + 1, FSR0H \
\r
229 MOVFF FSR1L, POSTINC0 \
\r
230 MOVFF FSR1H, POSTINC0 \
\r
233 /*-----------------------------------------------------------*/
\r
236 * This is the reverse of portSAVE_CONTEXT. See portSAVE_CONTEXT for more
\r
239 #define portRESTORE_CONTEXT() \
\r
242 /* Set FSR0 to point to pxCurrentTCB->pxTopOfStack. */ \
\r
243 MOVFF pxCurrentTCB, FSR0L \
\r
244 MOVFF pxCurrentTCB + 1, FSR0H \
\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
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
257 /* Fill the hardware stack from our software stack. */ \
\r
260 while( STKPTR < FSR0L ) \
\r
264 MOVF POSTDEC1, 0, 0 \
\r
266 MOVF POSTDEC1, 0, 0 \
\r
268 MOVF POSTDEC1, 0, 0 \
\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
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
320 if( WREG & portGLOBAL_INTERRUPT_FLAG ) \
\r
323 MOVFF POSTDEC1, STATUS \
\r
324 MOVFF POSTDEC1, WREG \
\r
325 /* Return enabling interrupts. */ \
\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
340 /*-----------------------------------------------------------*/
\r
343 * See header file for description.
\r
345 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
347 uint32_t ulAddress;
\r
350 /* Place a few bytes of known values on the bottom of the stack.
\r
351 This is just useful for debugging. */
\r
353 *pxTopOfStack = 0x11;
\r
355 *pxTopOfStack = 0x22;
\r
357 *pxTopOfStack = 0x33;
\r
361 /* Simulate how the stack would look after a call to vPortYield() generated
\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
371 *pxTopOfStack = ( StackType_t ) ( ulAddress & ( uint32_t ) 0x00ff );
\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
381 /* Next are all the registers that form part of the task context. */
\r
383 *pxTopOfStack = ( StackType_t ) 0x66; /* WREG. */
\r
386 *pxTopOfStack = ( StackType_t ) 0xcc; /* Status. */
\r
389 /* INTCON is saved with interrupts enabled. */
\r
390 *pxTopOfStack = ( StackType_t ) portINITAL_INTERRUPT_STATE; /* INTCON */
\r
393 *pxTopOfStack = ( StackType_t ) 0x11; /* BSR. */
\r
396 *pxTopOfStack = ( StackType_t ) 0x22; /* FSR2L. */
\r
399 *pxTopOfStack = ( StackType_t ) 0x33; /* FSR2H. */
\r
402 *pxTopOfStack = ( StackType_t ) 0x44; /* FSR0L. */
\r
405 *pxTopOfStack = ( StackType_t ) 0x55; /* FSR0H. */
\r
408 *pxTopOfStack = ( StackType_t ) 0x66; /* TABLAT. */
\r
411 *pxTopOfStack = ( StackType_t ) 0x00; /* TBLPTRU. */
\r
414 *pxTopOfStack = ( StackType_t ) 0x88; /* TBLPTRUH. */
\r
417 *pxTopOfStack = ( StackType_t ) 0x99; /* TBLPTRUL. */
\r
420 *pxTopOfStack = ( StackType_t ) 0xaa; /* PRODH. */
\r
423 *pxTopOfStack = ( StackType_t ) 0xbb; /* PRODL. */
\r
426 *pxTopOfStack = ( StackType_t ) 0x00; /* PCLATU. */
\r
429 *pxTopOfStack = ( StackType_t ) 0x00; /* PCLATH. */
\r
432 /* Next the .tmpdata and MATH_DATA sections. */
\r
433 for( ucBlock = 0; ucBlock <= portCOMPILER_MANAGED_MEMORY_SIZE; ucBlock++ )
\r
435 *pxTopOfStack = ( StackType_t ) ucBlock;
\r
439 /* Store the top of the global data section. */
\r
440 *pxTopOfStack = ( StackType_t ) portCOMPILER_MANAGED_MEMORY_SIZE; /* Low. */
\r
443 *pxTopOfStack = ( StackType_t ) 0x00; /* High. */
\r
446 /* The only function return address so far is the address of the
\r
448 ulAddress = ( uint32_t ) pxCode;
\r
451 *pxTopOfStack = ( StackType_t ) ( ulAddress & ( uint32_t ) 0x00ff );
\r
456 *pxTopOfStack = ( StackType_t ) ( ulAddress & ( uint32_t ) 0x00ff );
\r
460 /* TOS even higher. */
\r
461 *pxTopOfStack = ( StackType_t ) ( ulAddress & ( uint32_t ) 0x00ff );
\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
469 return pxTopOfStack;
\r
471 /*-----------------------------------------------------------*/
\r
473 BaseType_t xPortStartScheduler( void )
\r
475 /* Setup a timer for the tick ISR is using the preemptive scheduler. */
\r
476 prvSetupTimerInterrupt();
\r
478 /* Restore the context of the first task to run. */
\r
479 portRESTORE_CONTEXT();
\r
481 /* Should not get here. Use the function name to stop compiler warnings. */
\r
482 ( void ) prvLowInterrupt;
\r
483 ( void ) prvTickISR;
\r
487 /*-----------------------------------------------------------*/
\r
489 void vPortEndScheduler( void )
\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
495 /*-----------------------------------------------------------*/
\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
502 void vPortYield( void )
\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
508 /* Switch to the highest priority task that is ready to run. */
\r
509 vTaskSwitchContext();
\r
511 /* Start executing the task we have just switched to. */
\r
512 portRESTORE_CONTEXT();
\r
514 /*-----------------------------------------------------------*/
\r
517 * Vector for ISR. Nothing here must alter any registers!
\r
519 #pragma code high_vector=0x08
\r
520 static void prvLowInterrupt( void )
\r
522 /* Was the interrupt the tick? */
\r
523 if( PIR1bits.CCP1IF )
\r
530 /* Was the interrupt a byte being received? */
\r
531 if( PIR1bits.RCIF )
\r
538 /* Was the interrupt the Tx register becoming empty? */
\r
539 if( PIR1bits.TXIF )
\r
541 if( PIE1bits.TXIE )
\r
551 /*-----------------------------------------------------------*/
\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
559 static void prvTickISR( void )
\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
566 /* Maintain the tick count. */
\r
567 if( xTaskIncrementTick() != pdFALSE )
\r
569 /* Switch to the highest priority task that is ready to run. */
\r
570 vTaskSwitchContext();
\r
573 portRESTORE_CONTEXT();
\r
575 /*-----------------------------------------------------------*/
\r
578 * Setup a timer for a regular tick.
\r
580 static void prvSetupTimerInterrupt( void )
\r
582 const uint32_t ulConstCompareValue = ( ( configCPU_CLOCK_HZ / portTIMER_FOSC_SCALE ) / configTICK_RATE_HZ );
\r
583 uint32_t ulCompareValue;
\r
586 /* Interrupts are disabled when this function is called.
\r
588 Setup CCP1 to provide the tick interrupt using a compare match on timer
\r
591 Clear the time count then setup timer. */
\r
592 TMR1H = ( uint8_t ) 0x00;
\r
593 TMR1L = ( uint8_t ) 0x00;
\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
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
607 /* We are only going to use the global interrupt bit, so set the peripheral
\r
609 INTCONbits.GIEL = portBIT_SET;
\r
611 /* Provided library function for setting up the timer that will produce the
\r
613 OpenTimer1( T1_16BIT_RW & T1_SOURCE_INT & T1_PS_1_1 & T1_CCP1_T3_CCP2 );
\r