2 FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 Changes between V1.2.4 and V1.2.5
\r
69 + Introduced portGLOBAL_INTERRUPT_FLAG definition to test the global
\r
70 interrupt flag setting. Using the two bits defined within
\r
71 portINITAL_INTERRUPT_STATE was causing the w register to get clobbered
\r
72 before the test was performed.
\r
76 + Set the interrupt vector address to 0x08. Previously it was at the
\r
77 incorrect address for compatibility mode of 0x18.
\r
81 + PCLATU and PCLATH are now saved as part of the context. This allows
\r
82 function pointers to be used within tasks. Thanks to Javier Espeche
\r
83 for the enhancement.
\r
87 + TABLAT is now saved as part of the task context.
\r
91 + TBLPTRU is now initialised to zero as the MPLAB compiler expects this
\r
92 value and does not write to the register.
\r
95 /* Scheduler include files. */
\r
96 #include "FreeRTOS.h"
\r
99 /* MPLAB library include file. */
\r
100 #include "timers.h"
\r
102 /*-----------------------------------------------------------
\r
103 * Implementation of functions defined in portable.h for the PIC port.
\r
104 *----------------------------------------------------------*/
\r
106 /* Hardware setup for tick. */
\r
107 #define portTIMER_FOSC_SCALE ( ( uint32_t ) 4 )
\r
109 /* Initial interrupt enable state for newly created tasks. This value is
\r
110 copied into INTCON when a task switches in for the first time. */
\r
111 #define portINITAL_INTERRUPT_STATE 0xc0
\r
113 /* Just the bit within INTCON for the global interrupt flag. */
\r
114 #define portGLOBAL_INTERRUPT_FLAG 0x80
\r
116 /* Constant used for context switch macro when we require the interrupt
\r
117 enable state to be unchanged when the interrupted task is switched back in. */
\r
118 #define portINTERRUPTS_UNCHANGED 0x00
\r
120 /* Some memory areas get saved as part of the task context. These memory
\r
121 area's get used by the compiler for temporary storage, especially when
\r
122 performing mathematical operations, or when using 32bit data types. This
\r
123 constant defines the size of memory area which must be saved. */
\r
124 #define portCOMPILER_MANAGED_MEMORY_SIZE ( ( uint8_t ) 0x13 )
\r
126 /* We require the address of the pxCurrentTCB variable, but don't want to know
\r
127 any details of its type. */
\r
128 typedef void TCB_t;
\r
129 extern volatile TCB_t * volatile pxCurrentTCB;
\r
131 /* IO port constants. */
\r
132 #define portBIT_SET ( ( uint8_t ) 1 )
\r
133 #define portBIT_CLEAR ( ( uint8_t ) 0 )
\r
136 * The serial port ISR's are defined in serial.c, but are called from portable
\r
137 * as they use the same vector as the tick ISR.
\r
139 void vSerialTxISR( void );
\r
140 void vSerialRxISR( void );
\r
143 * Perform hardware setup to enable ticks.
\r
145 static void prvSetupTimerInterrupt( void );
\r
148 * ISR to maintain the tick, and perform tick context switches if the
\r
149 * preemptive scheduler is being used.
\r
151 static void prvTickISR( void );
\r
154 * ISR placed on the low priority vector. This calls the appropriate ISR for
\r
155 * the actual interrupt.
\r
157 static void prvLowInterrupt( void );
\r
160 * Macro that pushes all the registers that make up the context of a task onto
\r
161 * the stack, then saves the new top of stack into the TCB.
\r
163 * If this is called from an ISR then the interrupt enable bits must have been
\r
164 * set for the ISR to ever get called. Therefore we want to save the INTCON
\r
165 * register with the enable bits forced to be set - and ucForcedInterruptFlags
\r
166 * must contain these bit settings. This means the interrupts will again be
\r
167 * enabled when the interrupted task is switched back in.
\r
169 * If this is called from a manual context switch (i.e. from a call to yield),
\r
170 * then we want to save the INTCON so it is restored with its current state,
\r
171 * and ucForcedInterruptFlags must be 0. This allows a yield from within
\r
172 * a critical section.
\r
174 * The compiler uses some locations at the bottom of the memory for temporary
\r
175 * storage during math and other computations. This is especially true if
\r
176 * 32bit data types are utilised (as they are by the scheduler). The .tmpdata
\r
177 * and MATH_DATA sections have to be stored in there entirety as part of a task
\r
178 * context. This macro stores from data address 0x00 to
\r
179 * portCOMPILER_MANAGED_MEMORY_SIZE. This is sufficient for the demo
\r
180 * applications but you should check the map file for your project to ensure
\r
181 * this is sufficient for your needs. It is not clear whether this size is
\r
182 * fixed for all compilations or has the potential to be program specific.
\r
184 #define portSAVE_CONTEXT( ucForcedInterruptFlags ) \
\r
187 /* Save the status and WREG registers first, as these will get modified \
\r
188 by the operations below. */ \
\r
189 MOVFF WREG, PREINC1 \
\r
190 MOVFF STATUS, PREINC1 \
\r
191 /* Save the INTCON register with the appropriate bits forced if \
\r
192 necessary - as described above. */ \
\r
193 MOVFF INTCON, WREG \
\r
194 IORLW ucForcedInterruptFlags \
\r
195 MOVFF WREG, PREINC1 \
\r
198 portDISABLE_INTERRUPTS(); \
\r
201 /* Store the necessary registers to the stack. */ \
\r
202 MOVFF BSR, PREINC1 \
\r
203 MOVFF FSR2L, PREINC1 \
\r
204 MOVFF FSR2H, PREINC1 \
\r
205 MOVFF FSR0L, PREINC1 \
\r
206 MOVFF FSR0H, PREINC1 \
\r
207 MOVFF TABLAT, PREINC1 \
\r
208 MOVFF TBLPTRU, PREINC1 \
\r
209 MOVFF TBLPTRH, PREINC1 \
\r
210 MOVFF TBLPTRL, PREINC1 \
\r
211 MOVFF PRODH, PREINC1 \
\r
212 MOVFF PRODL, PREINC1 \
\r
213 MOVFF PCLATU, PREINC1 \
\r
214 MOVFF PCLATH, PREINC1 \
\r
215 /* Store the .tempdata and MATH_DATA areas as described above. */ \
\r
218 MOVFF POSTINC0, PREINC1 \
\r
219 MOVFF POSTINC0, PREINC1 \
\r
220 MOVFF POSTINC0, PREINC1 \
\r
221 MOVFF POSTINC0, PREINC1 \
\r
222 MOVFF POSTINC0, PREINC1 \
\r
223 MOVFF POSTINC0, PREINC1 \
\r
224 MOVFF POSTINC0, PREINC1 \
\r
225 MOVFF POSTINC0, PREINC1 \
\r
226 MOVFF POSTINC0, PREINC1 \
\r
227 MOVFF POSTINC0, PREINC1 \
\r
228 MOVFF POSTINC0, PREINC1 \
\r
229 MOVFF POSTINC0, PREINC1 \
\r
230 MOVFF POSTINC0, PREINC1 \
\r
231 MOVFF POSTINC0, PREINC1 \
\r
232 MOVFF POSTINC0, PREINC1 \
\r
233 MOVFF POSTINC0, PREINC1 \
\r
234 MOVFF POSTINC0, PREINC1 \
\r
235 MOVFF POSTINC0, PREINC1 \
\r
236 MOVFF POSTINC0, PREINC1 \
\r
237 MOVFF INDF0, PREINC1 \
\r
238 MOVFF FSR0L, PREINC1 \
\r
239 MOVFF FSR0H, PREINC1 \
\r
240 /* Store the hardware stack pointer in a temp register before we \
\r
242 MOVFF STKPTR, FSR0L \
\r
245 /* Store each address from the hardware stack. */ \
\r
246 while( STKPTR > ( uint8_t ) 0 ) \
\r
249 MOVFF TOSL, PREINC1 \
\r
250 MOVFF TOSH, PREINC1 \
\r
251 MOVFF TOSU, PREINC1 \
\r
257 /* Store the number of addresses on the hardware stack (from the \
\r
258 temporary register). */ \
\r
259 MOVFF FSR0L, PREINC1 \
\r
260 MOVF PREINC1, 1, 0 \
\r
263 /* Save the new top of the software stack in the TCB. */ \
\r
265 MOVFF pxCurrentTCB, FSR0L \
\r
266 MOVFF pxCurrentTCB + 1, FSR0H \
\r
267 MOVFF FSR1L, POSTINC0 \
\r
268 MOVFF FSR1H, POSTINC0 \
\r
271 /*-----------------------------------------------------------*/
\r
274 * This is the reverse of portSAVE_CONTEXT. See portSAVE_CONTEXT for more
\r
277 #define portRESTORE_CONTEXT() \
\r
280 /* Set FSR0 to point to pxCurrentTCB->pxTopOfStack. */ \
\r
281 MOVFF pxCurrentTCB, FSR0L \
\r
282 MOVFF pxCurrentTCB + 1, FSR0H \
\r
284 /* De-reference FSR0 to set the address it holds into FSR1. \
\r
285 (i.e. *( pxCurrentTCB->pxTopOfStack ) ). */ \
\r
286 MOVFF POSTINC0, FSR1L \
\r
287 MOVFF POSTINC0, FSR1H \
\r
289 /* How many return addresses are there on the hardware stack? Discard \
\r
290 the first byte as we are pointing to the next free space. */ \
\r
291 MOVFF POSTDEC1, FSR0L \
\r
292 MOVFF POSTDEC1, FSR0L \
\r
295 /* Fill the hardware stack from our software stack. */ \
\r
298 while( STKPTR < FSR0L ) \
\r
302 MOVF POSTDEC1, 0, 0 \
\r
304 MOVF POSTDEC1, 0, 0 \
\r
306 MOVF POSTDEC1, 0, 0 \
\r
312 /* Restore the .tmpdata and MATH_DATA memory. */ \
\r
313 MOVFF POSTDEC1, FSR0H \
\r
314 MOVFF POSTDEC1, FSR0L \
\r
315 MOVFF POSTDEC1, POSTDEC0 \
\r
316 MOVFF POSTDEC1, POSTDEC0 \
\r
317 MOVFF POSTDEC1, POSTDEC0 \
\r
318 MOVFF POSTDEC1, POSTDEC0 \
\r
319 MOVFF POSTDEC1, POSTDEC0 \
\r
320 MOVFF POSTDEC1, POSTDEC0 \
\r
321 MOVFF POSTDEC1, POSTDEC0 \
\r
322 MOVFF POSTDEC1, POSTDEC0 \
\r
323 MOVFF POSTDEC1, POSTDEC0 \
\r
324 MOVFF POSTDEC1, POSTDEC0 \
\r
325 MOVFF POSTDEC1, POSTDEC0 \
\r
326 MOVFF POSTDEC1, POSTDEC0 \
\r
327 MOVFF POSTDEC1, POSTDEC0 \
\r
328 MOVFF POSTDEC1, POSTDEC0 \
\r
329 MOVFF POSTDEC1, POSTDEC0 \
\r
330 MOVFF POSTDEC1, POSTDEC0 \
\r
331 MOVFF POSTDEC1, POSTDEC0 \
\r
332 MOVFF POSTDEC1, POSTDEC0 \
\r
333 MOVFF POSTDEC1, POSTDEC0 \
\r
334 MOVFF POSTDEC1, INDF0 \
\r
335 /* Restore the other registers forming the tasks context. */ \
\r
336 MOVFF POSTDEC1, PCLATH \
\r
337 MOVFF POSTDEC1, PCLATU \
\r
338 MOVFF POSTDEC1, PRODL \
\r
339 MOVFF POSTDEC1, PRODH \
\r
340 MOVFF POSTDEC1, TBLPTRL \
\r
341 MOVFF POSTDEC1, TBLPTRH \
\r
342 MOVFF POSTDEC1, TBLPTRU \
\r
343 MOVFF POSTDEC1, TABLAT \
\r
344 MOVFF POSTDEC1, FSR0H \
\r
345 MOVFF POSTDEC1, FSR0L \
\r
346 MOVFF POSTDEC1, FSR2H \
\r
347 MOVFF POSTDEC1, FSR2L \
\r
348 MOVFF POSTDEC1, BSR \
\r
349 /* The next byte is the INTCON register. Read this into WREG as some \
\r
350 manipulation is required. */ \
\r
351 MOVFF POSTDEC1, WREG \
\r
354 /* From the INTCON register, only the interrupt enable bits form part \
\r
355 of the tasks context. It is perfectly legitimate for another task to \
\r
356 have modified any other bits. We therefore only restore the top two bits. \
\r
358 if( WREG & portGLOBAL_INTERRUPT_FLAG ) \
\r
361 MOVFF POSTDEC1, STATUS \
\r
362 MOVFF POSTDEC1, WREG \
\r
363 /* Return enabling interrupts. */ \
\r
370 MOVFF POSTDEC1, STATUS \
\r
371 MOVFF POSTDEC1, WREG \
\r
372 /* Return without effecting interrupts. The context may have \
\r
373 been saved from a critical region. */ \
\r
378 /*-----------------------------------------------------------*/
\r
381 * See header file for description.
\r
383 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
385 uint32_t ulAddress;
\r
388 /* Place a few bytes of known values on the bottom of the stack.
\r
389 This is just useful for debugging. */
\r
391 *pxTopOfStack = 0x11;
\r
393 *pxTopOfStack = 0x22;
\r
395 *pxTopOfStack = 0x33;
\r
399 /* Simulate how the stack would look after a call to vPortYield() generated
\r
402 First store the function parameters. This is where the task will expect to
\r
403 find them when it starts running. */
\r
404 ulAddress = ( uint32_t ) pvParameters;
\r
405 *pxTopOfStack = ( StackType_t ) ( ulAddress & ( uint32_t ) 0x00ff );
\r
409 *pxTopOfStack = ( StackType_t ) ( ulAddress & ( uint32_t ) 0x00ff );
\r
412 /* Next we just leave a space. When a context is saved the stack pointer
\r
413 is incremented before it is used so as not to corrupt whatever the stack
\r
414 pointer is actually pointing to. This is especially necessary during
\r
415 function epilogue code generated by the compiler. */
\r
416 *pxTopOfStack = 0x44;
\r
419 /* Next are all the registers that form part of the task context. */
\r
421 *pxTopOfStack = ( StackType_t ) 0x66; /* WREG. */
\r
424 *pxTopOfStack = ( StackType_t ) 0xcc; /* Status. */
\r
427 /* INTCON is saved with interrupts enabled. */
\r
428 *pxTopOfStack = ( StackType_t ) portINITAL_INTERRUPT_STATE; /* INTCON */
\r
431 *pxTopOfStack = ( StackType_t ) 0x11; /* BSR. */
\r
434 *pxTopOfStack = ( StackType_t ) 0x22; /* FSR2L. */
\r
437 *pxTopOfStack = ( StackType_t ) 0x33; /* FSR2H. */
\r
440 *pxTopOfStack = ( StackType_t ) 0x44; /* FSR0L. */
\r
443 *pxTopOfStack = ( StackType_t ) 0x55; /* FSR0H. */
\r
446 *pxTopOfStack = ( StackType_t ) 0x66; /* TABLAT. */
\r
449 *pxTopOfStack = ( StackType_t ) 0x00; /* TBLPTRU. */
\r
452 *pxTopOfStack = ( StackType_t ) 0x88; /* TBLPTRUH. */
\r
455 *pxTopOfStack = ( StackType_t ) 0x99; /* TBLPTRUL. */
\r
458 *pxTopOfStack = ( StackType_t ) 0xaa; /* PRODH. */
\r
461 *pxTopOfStack = ( StackType_t ) 0xbb; /* PRODL. */
\r
464 *pxTopOfStack = ( StackType_t ) 0x00; /* PCLATU. */
\r
467 *pxTopOfStack = ( StackType_t ) 0x00; /* PCLATH. */
\r
470 /* Next the .tmpdata and MATH_DATA sections. */
\r
471 for( ucBlock = 0; ucBlock <= portCOMPILER_MANAGED_MEMORY_SIZE; ucBlock++ )
\r
473 *pxTopOfStack = ( StackType_t ) ucBlock;
\r
477 /* Store the top of the global data section. */
\r
478 *pxTopOfStack = ( StackType_t ) portCOMPILER_MANAGED_MEMORY_SIZE; /* Low. */
\r
481 *pxTopOfStack = ( StackType_t ) 0x00; /* High. */
\r
484 /* The only function return address so far is the address of the
\r
486 ulAddress = ( uint32_t ) pxCode;
\r
489 *pxTopOfStack = ( StackType_t ) ( ulAddress & ( uint32_t ) 0x00ff );
\r
494 *pxTopOfStack = ( StackType_t ) ( ulAddress & ( uint32_t ) 0x00ff );
\r
498 /* TOS even higher. */
\r
499 *pxTopOfStack = ( StackType_t ) ( ulAddress & ( uint32_t ) 0x00ff );
\r
502 /* Store the number of return addresses on the hardware stack - so far only
\r
503 the address of the task entry point. */
\r
504 *pxTopOfStack = ( StackType_t ) 1;
\r
507 return pxTopOfStack;
\r
509 /*-----------------------------------------------------------*/
\r
511 BaseType_t xPortStartScheduler( void )
\r
513 /* Setup a timer for the tick ISR is using the preemptive scheduler. */
\r
514 prvSetupTimerInterrupt();
\r
516 /* Restore the context of the first task to run. */
\r
517 portRESTORE_CONTEXT();
\r
519 /* Should not get here. Use the function name to stop compiler warnings. */
\r
520 ( void ) prvLowInterrupt;
\r
521 ( void ) prvTickISR;
\r
525 /*-----------------------------------------------------------*/
\r
527 void vPortEndScheduler( void )
\r
529 /* It is unlikely that the scheduler for the PIC port will get stopped
\r
530 once running. If required disable the tick interrupt here, then return
\r
531 to xPortStartScheduler(). */
\r
533 /*-----------------------------------------------------------*/
\r
536 * Manual context switch. This is similar to the tick context switch,
\r
537 * but does not increment the tick count. It must be identical to the
\r
538 * tick context switch in how it stores the stack of a task.
\r
540 void vPortYield( void )
\r
542 /* This can get called with interrupts either enabled or disabled. We
\r
543 will save the INTCON register with the interrupt enable bits unmodified. */
\r
544 portSAVE_CONTEXT( portINTERRUPTS_UNCHANGED );
\r
546 /* Switch to the highest priority task that is ready to run. */
\r
547 vTaskSwitchContext();
\r
549 /* Start executing the task we have just switched to. */
\r
550 portRESTORE_CONTEXT();
\r
552 /*-----------------------------------------------------------*/
\r
555 * Vector for ISR. Nothing here must alter any registers!
\r
557 #pragma code high_vector=0x08
\r
558 static void prvLowInterrupt( void )
\r
560 /* Was the interrupt the tick? */
\r
561 if( PIR1bits.CCP1IF )
\r
568 /* Was the interrupt a byte being received? */
\r
569 if( PIR1bits.RCIF )
\r
576 /* Was the interrupt the Tx register becoming empty? */
\r
577 if( PIR1bits.TXIF )
\r
579 if( PIE1bits.TXIE )
\r
589 /*-----------------------------------------------------------*/
\r
592 * ISR for the tick.
\r
593 * This increments the tick count and, if using the preemptive scheduler,
\r
594 * performs a context switch. This must be identical to the manual
\r
595 * context switch in how it stores the context of a task.
\r
597 static void prvTickISR( void )
\r
599 /* Interrupts must have been enabled for the ISR to fire, so we have to
\r
600 save the context with interrupts enabled. */
\r
601 portSAVE_CONTEXT( portGLOBAL_INTERRUPT_FLAG );
\r
602 PIR1bits.CCP1IF = 0;
\r
604 /* Maintain the tick count. */
\r
605 if( xTaskIncrementTick() != pdFALSE )
\r
607 /* Switch to the highest priority task that is ready to run. */
\r
608 vTaskSwitchContext();
\r
611 portRESTORE_CONTEXT();
\r
613 /*-----------------------------------------------------------*/
\r
616 * Setup a timer for a regular tick.
\r
618 static void prvSetupTimerInterrupt( void )
\r
620 const uint32_t ulConstCompareValue = ( ( configCPU_CLOCK_HZ / portTIMER_FOSC_SCALE ) / configTICK_RATE_HZ );
\r
621 uint32_t ulCompareValue;
\r
624 /* Interrupts are disabled when this function is called.
\r
626 Setup CCP1 to provide the tick interrupt using a compare match on timer
\r
629 Clear the time count then setup timer. */
\r
630 TMR1H = ( uint8_t ) 0x00;
\r
631 TMR1L = ( uint8_t ) 0x00;
\r
633 /* Set the compare match value. */
\r
634 ulCompareValue = ulConstCompareValue;
\r
635 CCPR1L = ( uint8_t ) ( ulCompareValue & ( uint32_t ) 0xff );
\r
636 ulCompareValue >>= ( uint32_t ) 8;
\r
637 CCPR1H = ( uint8_t ) ( ulCompareValue & ( uint32_t ) 0xff );
\r
639 CCP1CONbits.CCP1M0 = portBIT_SET; /*< Compare match mode. */
\r
640 CCP1CONbits.CCP1M1 = portBIT_SET; /*< Compare match mode. */
\r
641 CCP1CONbits.CCP1M2 = portBIT_CLEAR; /*< Compare match mode. */
\r
642 CCP1CONbits.CCP1M3 = portBIT_SET; /*< Compare match mode. */
\r
643 PIE1bits.CCP1IE = portBIT_SET; /*< Interrupt enable. */
\r
645 /* We are only going to use the global interrupt bit, so set the peripheral
\r
647 INTCONbits.GIEL = portBIT_SET;
\r
649 /* Provided library function for setting up the timer that will produce the
\r
651 OpenTimer1( T1_16BIT_RW & T1_SOURCE_INT & T1_PS_1_1 & T1_CCP1_T3_CCP2 );
\r