2 FreeRTOS.org V4.3.0 - Copyright (C) 2003-2007 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
31 Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along
\r
32 with commercial development and support options.
\r
33 ***************************************************************************
\r
39 + Introduced the configKERNEL_INTERRUPT_PRIORITY definition.
\r
42 /*-----------------------------------------------------------
\r
43 * Implementation of functions defined in portable.h for the PIC24 port.
\r
44 *----------------------------------------------------------*/
\r
46 /* Scheduler include files. */
\r
47 #include "FreeRTOS.h"
\r
50 /* Hardware specifics. */
\r
51 #define portBIT_SET 1
\r
52 #define portTIMER_PRESCALE 8
\r
53 #define portINITIAL_SR 0
\r
55 /* Defined for backward compatability with project created prior to
\r
56 FreeRTOS.org V4.3.0. */
\r
57 #ifndef configKERNEL_INTERRUPT_PRIORITY
\r
58 #define configKERNEL_INTERRUPT_PRIORITY 1
\r
61 /* The program counter is only 23 bits. */
\r
62 #define portUNUSED_PR_BITS 0x7f
\r
64 /* Records the nesting depth of calls to portENTER_CRITICAL(). */
\r
65 unsigned portBASE_TYPE uxCriticalNesting = 0xef;
\r
67 #if configKERNEL_INTERRUPT_PRIORITY != 1
\r
68 #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
71 #ifdef MPLAB_PIC24_PORT
\r
73 #define portRESTORE_CONTEXT() \
\r
74 asm volatile( "MOV _pxCurrentTCB, W0 \n" /* Restore the stack pointer for the task. */ \
\r
75 "MOV [W0], W15 \n" \
\r
76 "POP W0 \n" /* Restore the critical nesting counter for the task. */ \
\r
77 "MOV W0, _uxCriticalNesting \n" \
\r
81 "POP RCOUNT \n" /* Restore the registers from the stack. */ \
\r
93 #define portSAVE_CONTEXT() \
\r
94 asm volatile( "PUSH SR \n" /* Save the SR used by the task.... */ \
\r
95 "PUSH W0 \n" /* ....then disable interrupts. */ \
\r
98 "PUSH W1 \n" /* Save registers to the stack. */ \
\r
110 "MOV _uxCriticalNesting, W0 \n" /* Save the critical nesting counter for the task. */ \
\r
112 "MOV _pxCurrentTCB, W0 \n" /* Save the new top of stack into the TCB. */ \
\r
115 #endif /* MPLAB_PIC24_PORT */
\r
117 #ifdef MPLAB_DSPIC_PORT
\r
119 #define portRESTORE_CONTEXT() \
\r
120 asm volatile( "MOV _pxCurrentTCB, W0 \n" /* Restore the stack pointer for the task. */ \
\r
121 "MOV [W0], W15 \n" \
\r
122 "POP W0 \n" /* Restore the critical nesting counter for the task. */ \
\r
123 "MOV W0, _uxCriticalNesting \n" \
\r
128 "POP DOSTARTH \n" \
\r
129 "POP DOSTARTL \n" \
\r
138 "POP RCOUNT \n" /* Restore the registers from the stack. */ \
\r
150 #define portSAVE_CONTEXT() \
\r
151 asm volatile( "PUSH SR \n" /* Save the SR used by the task.... */ \
\r
152 "PUSH W0 \n" /* ....then disable interrupts. */ \
\r
155 "PUSH W1 \n" /* Save registers to the stack. */ \
\r
172 "PUSH DOSTARTL \n" \
\r
173 "PUSH DOSTARTH \n" \
\r
178 "MOV _uxCriticalNesting, W0 \n" /* Save the critical nesting counter for the task. */ \
\r
180 "MOV _pxCurrentTCB, W0 \n" /* Save the new top of stack into the TCB. */ \
\r
181 "MOV W15, [W0] " );
\r
183 #endif /* MPLAB_DSPIC_PORT */
\r
186 * Setup the timer used to generate the tick interrupt.
\r
188 static void prvSetupTimerInterrupt( void );
\r
191 * See header file for description.
\r
193 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
195 unsigned portSHORT usCode;
\r
198 const portSTACK_TYPE xInitialStack[] =
\r
214 0xcdce, /* RCOUNT */
\r
215 0xabac, /* TBLPAG */
\r
217 /* dsPIC specific registers. */
\r
218 #ifdef MPLAB_DSPIC_PORT
\r
219 0x0202, /* ACCAL */
\r
220 0x0303, /* ACCAH */
\r
221 0x0404, /* ACCAU */
\r
222 0x0505, /* ACCBL */
\r
223 0x0606, /* ACCBH */
\r
224 0x0707, /* ACCBU */
\r
225 0x0808, /* DCOUNT */
\r
226 0x090a, /* DOSTARTL */
\r
227 0x1010, /* DOSTARTH */
\r
228 0x1110, /* DOENDL */
\r
229 0x1212, /* DOENDH */
\r
233 /* Setup the stack as if a yield had occurred.
\r
235 Save the low bytes of the program counter. */
\r
236 usCode = ( unsigned portSHORT ) pxCode;
\r
237 *pxTopOfStack = ( portSTACK_TYPE ) usCode;
\r
240 /* Save the high byte of the program counter. This will always be zero
\r
241 here as it is passed in a 16bit pointer. If the address is greater than
\r
242 16 bits then the pointer will point to a jump table. */
\r
243 *pxTopOfStack = ( portSTACK_TYPE ) 0;
\r
246 /* Status register with interrupts enabled. */
\r
247 *pxTopOfStack = portINITIAL_SR;
\r
250 /* Parameters are passed in W0. */
\r
251 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;
\r
254 for( i = 0; i < ( sizeof( xInitialStack ) / sizeof( portSTACK_TYPE ) ); i++ )
\r
256 *pxTopOfStack = xInitialStack[ i ];
\r
260 *pxTopOfStack = CORCON;
\r
262 *pxTopOfStack = PSVPAG;
\r
265 /* Finally the critical nesting depth. */
\r
266 *pxTopOfStack = 0x00;
\r
269 return pxTopOfStack;
\r
271 /*-----------------------------------------------------------*/
\r
273 portBASE_TYPE xPortStartScheduler( void )
\r
275 /* Setup a timer for the tick ISR. */
\r
276 prvSetupTimerInterrupt();
\r
278 /* Restore the context of the first task to run. */
\r
279 portRESTORE_CONTEXT();
\r
281 /* Simulate the end of the yield function. */
\r
282 asm volatile ( "return" );
\r
284 /* Should not reach here. */
\r
287 /*-----------------------------------------------------------*/
\r
289 void vPortEndScheduler( void )
\r
291 /* It is unlikely that the scheduler for the PIC port will get stopped
\r
292 once running. If required disable the tick interrupt here, then return
\r
293 to xPortStartScheduler(). */
\r
295 /*-----------------------------------------------------------*/
\r
298 * Manual context switch. This is similar to the tick context switch,
\r
299 * but does not increment the tick count. It must be identical to the
\r
300 * tick context switch in how it stores the stack of a task.
\r
302 void vPortYield( void )
\r
304 portSAVE_CONTEXT();
\r
305 vTaskSwitchContext();
\r
306 portRESTORE_CONTEXT();
\r
308 /*-----------------------------------------------------------*/
\r
311 * Setup a timer for a regular tick.
\r
313 static void prvSetupTimerInterrupt( void )
\r
315 const unsigned portLONG ulCompareMatch = ( configCPU_CLOCK_HZ / portTIMER_PRESCALE ) / configTICK_RATE_HZ;
\r
317 /* Prescale of 8. */
\r
321 PR1 = ( unsigned portSHORT ) ulCompareMatch;
\r
323 /* Setup timer 1 interrupt priority. */
\r
324 IPC0bits.T1IP = configKERNEL_INTERRUPT_PRIORITY;
\r
326 /* Clear the interrupt as a starting condition. */
\r
329 /* Enable the interrupt. */
\r
332 /* Setup the prescale value. */
\r
333 T1CONbits.TCKPS0 = 1;
\r
334 T1CONbits.TCKPS1 = 0;
\r
336 /* Start the timer. */
\r
339 /*-----------------------------------------------------------*/
\r
341 void vPortEnterCritical( void )
\r
343 portDISABLE_INTERRUPTS();
\r
344 uxCriticalNesting++;
\r
346 /*-----------------------------------------------------------*/
\r
348 void vPortExitCritical( void )
\r
350 uxCriticalNesting--;
\r
351 if( uxCriticalNesting == 0 )
\r
353 portENABLE_INTERRUPTS();
\r
356 /*-----------------------------------------------------------*/
\r
358 void __attribute__((__interrupt__, auto_psv)) _T1Interrupt( void )
\r
360 vTaskIncrementTick();
\r
362 /* Clear the timer interrupt. */
\r
365 #if configUSE_PREEMPTION == 1
\r