2 FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS books - available as PDF or paperback *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
57 + Introduced the configKERNEL_INTERRUPT_PRIORITY definition.
\r
60 /*-----------------------------------------------------------
\r
61 * Implementation of functions defined in portable.h for the PIC24 port.
\r
62 *----------------------------------------------------------*/
\r
64 /* Scheduler include files. */
\r
65 #include "FreeRTOS.h"
\r
68 /* Hardware specifics. */
\r
69 #define portBIT_SET 1
\r
70 #define portTIMER_PRESCALE 8
\r
71 #define portINITIAL_SR 0
\r
73 /* Defined for backward compatability with project created prior to
\r
74 FreeRTOS.org V4.3.0. */
\r
75 #ifndef configKERNEL_INTERRUPT_PRIORITY
\r
76 #define configKERNEL_INTERRUPT_PRIORITY 1
\r
79 /* The program counter is only 23 bits. */
\r
80 #define portUNUSED_PR_BITS 0x7f
\r
82 /* Records the nesting depth of calls to portENTER_CRITICAL(). */
\r
83 unsigned portBASE_TYPE uxCriticalNesting = 0xef;
\r
85 #if configKERNEL_INTERRUPT_PRIORITY != 1
\r
86 #error If configKERNEL_INTERRUPT_PRIORITY is not 1 then the #32 in the following macros needs changing to equal the portINTERRUPT_BITS value, which is ( configKERNEL_INTERRUPT_PRIORITY << 5 )
\r
89 #ifdef MPLAB_PIC24_PORT
\r
91 #define portRESTORE_CONTEXT() \
\r
92 asm volatile( "MOV _pxCurrentTCB, W0 \n" /* Restore the stack pointer for the task. */ \
\r
93 "MOV [W0], W15 \n" \
\r
94 "POP W0 \n" /* Restore the critical nesting counter for the task. */ \
\r
95 "MOV W0, _uxCriticalNesting \n" \
\r
99 "POP RCOUNT \n" /* Restore the registers from the stack. */ \
\r
110 #endif /* MPLAB_PIC24_PORT */
\r
112 #ifdef MPLAB_DSPIC_PORT
\r
114 #define portRESTORE_CONTEXT() \
\r
115 asm volatile( "MOV _pxCurrentTCB, W0 \n" /* Restore the stack pointer for the task. */ \
\r
116 "MOV [W0], W15 \n" \
\r
117 "POP W0 \n" /* Restore the critical nesting counter for the task. */ \
\r
118 "MOV W0, _uxCriticalNesting \n" \
\r
123 "POP DOSTARTH \n" \
\r
124 "POP DOSTARTL \n" \
\r
133 "POP RCOUNT \n" /* Restore the registers from the stack. */ \
\r
144 #endif /* MPLAB_DSPIC_PORT */
\r
147 * Setup the timer used to generate the tick interrupt.
\r
149 static void prvSetupTimerInterrupt( void );
\r
152 * See header file for description.
\r
154 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
156 unsigned short usCode;
\r
159 const portSTACK_TYPE xInitialStack[] =
\r
175 0xcdce, /* RCOUNT */
\r
176 0xabac, /* TBLPAG */
\r
178 /* dsPIC specific registers. */
\r
179 #ifdef MPLAB_DSPIC_PORT
\r
180 0x0202, /* ACCAL */
\r
181 0x0303, /* ACCAH */
\r
182 0x0404, /* ACCAU */
\r
183 0x0505, /* ACCBL */
\r
184 0x0606, /* ACCBH */
\r
185 0x0707, /* ACCBU */
\r
186 0x0808, /* DCOUNT */
\r
187 0x090a, /* DOSTARTL */
\r
188 0x1010, /* DOSTARTH */
\r
189 0x1110, /* DOENDL */
\r
190 0x1212, /* DOENDH */
\r
194 /* Setup the stack as if a yield had occurred.
\r
196 Save the low bytes of the program counter. */
\r
197 usCode = ( unsigned short ) pxCode;
\r
198 *pxTopOfStack = ( portSTACK_TYPE ) usCode;
\r
201 /* Save the high byte of the program counter. This will always be zero
\r
202 here as it is passed in a 16bit pointer. If the address is greater than
\r
203 16 bits then the pointer will point to a jump table. */
\r
204 *pxTopOfStack = ( portSTACK_TYPE ) 0;
\r
207 /* Status register with interrupts enabled. */
\r
208 *pxTopOfStack = portINITIAL_SR;
\r
211 /* Parameters are passed in W0. */
\r
212 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;
\r
215 for( i = 0; i < ( sizeof( xInitialStack ) / sizeof( portSTACK_TYPE ) ); i++ )
\r
217 *pxTopOfStack = xInitialStack[ i ];
\r
221 *pxTopOfStack = CORCON;
\r
223 *pxTopOfStack = PSVPAG;
\r
226 /* Finally the critical nesting depth. */
\r
227 *pxTopOfStack = 0x00;
\r
230 return pxTopOfStack;
\r
232 /*-----------------------------------------------------------*/
\r
234 portBASE_TYPE xPortStartScheduler( void )
\r
236 /* Setup a timer for the tick ISR. */
\r
237 prvSetupTimerInterrupt();
\r
239 /* Restore the context of the first task to run. */
\r
240 portRESTORE_CONTEXT();
\r
242 /* Simulate the end of the yield function. */
\r
243 asm volatile ( "return" );
\r
245 /* Should not reach here. */
\r
248 /*-----------------------------------------------------------*/
\r
250 void vPortEndScheduler( void )
\r
252 /* It is unlikely that the scheduler for the PIC port will get stopped
\r
253 once running. If required disable the tick interrupt here, then return
\r
254 to xPortStartScheduler(). */
\r
256 /*-----------------------------------------------------------*/
\r
259 * Setup a timer for a regular tick.
\r
261 static void prvSetupTimerInterrupt( void )
\r
263 const unsigned long ulCompareMatch = ( ( configCPU_CLOCK_HZ / portTIMER_PRESCALE ) / configTICK_RATE_HZ ) - 1;
\r
265 /* Prescale of 8. */
\r
269 PR1 = ( unsigned short ) ulCompareMatch;
\r
271 /* Setup timer 1 interrupt priority. */
\r
272 IPC0bits.T1IP = configKERNEL_INTERRUPT_PRIORITY;
\r
274 /* Clear the interrupt as a starting condition. */
\r
277 /* Enable the interrupt. */
\r
280 /* Setup the prescale value. */
\r
281 T1CONbits.TCKPS0 = 1;
\r
282 T1CONbits.TCKPS1 = 0;
\r
284 /* Start the timer. */
\r
287 /*-----------------------------------------------------------*/
\r
289 void vPortEnterCritical( void )
\r
291 portDISABLE_INTERRUPTS();
\r
292 uxCriticalNesting++;
\r
294 /*-----------------------------------------------------------*/
\r
296 void vPortExitCritical( void )
\r
298 uxCriticalNesting--;
\r
299 if( uxCriticalNesting == 0 )
\r
301 portENABLE_INTERRUPTS();
\r
304 /*-----------------------------------------------------------*/
\r
306 void __attribute__((__interrupt__, auto_psv)) _T1Interrupt( void )
\r
308 /* Clear the timer interrupt. */
\r
311 vTaskIncrementTick();
\r
313 #if configUSE_PREEMPTION == 1
\r