2 FreeRTOS V7.4.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
78 + Introduced the configKERNEL_INTERRUPT_PRIORITY definition.
\r
81 /*-----------------------------------------------------------
\r
82 * Implementation of functions defined in portable.h for the PIC24 port.
\r
83 *----------------------------------------------------------*/
\r
85 /* Scheduler include files. */
\r
86 #include "FreeRTOS.h"
\r
89 /* Hardware specifics. */
\r
90 #define portBIT_SET 1
\r
91 #define portTIMER_PRESCALE 8
\r
92 #define portINITIAL_SR 0
\r
94 /* Defined for backward compatability with project created prior to
\r
95 FreeRTOS.org V4.3.0. */
\r
96 #ifndef configKERNEL_INTERRUPT_PRIORITY
\r
97 #define configKERNEL_INTERRUPT_PRIORITY 1
\r
100 /* The program counter is only 23 bits. */
\r
101 #define portUNUSED_PR_BITS 0x7f
\r
103 /* Records the nesting depth of calls to portENTER_CRITICAL(). */
\r
104 unsigned portBASE_TYPE uxCriticalNesting = 0xef;
\r
106 #if configKERNEL_INTERRUPT_PRIORITY != 1
\r
107 #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
110 #ifdef MPLAB_PIC24_PORT
\r
113 #define portRESTORE_CONTEXT() \
\r
114 asm volatile( "MOV _pxCurrentTCB, W0 \n" /* Restore the stack pointer for the task. */ \
\r
115 "MOV [W0], W15 \n" \
\r
116 "POP W0 \n" /* Restore the critical nesting counter for the task. */ \
\r
117 "MOV W0, _uxCriticalNesting \n" \
\r
122 "POP RCOUNT \n" /* Restore the registers from the stack. */ \
\r
132 #else /* __HAS_EDS__ */
\r
133 #define portRESTORE_CONTEXT() \
\r
134 asm volatile( "MOV _pxCurrentTCB, W0 \n" /* Restore the stack pointer for the task. */ \
\r
135 "MOV [W0], W15 \n" \
\r
136 "POP W0 \n" /* Restore the critical nesting counter for the task. */ \
\r
137 "MOV W0, _uxCriticalNesting \n" \
\r
141 "POP RCOUNT \n" /* Restore the registers from the stack. */ \
\r
151 #endif /* __HAS_EDS__ */
\r
152 #endif /* MPLAB_PIC24_PORT */
\r
154 #ifdef MPLAB_DSPIC_PORT
\r
156 #define portRESTORE_CONTEXT() \
\r
157 asm volatile( "MOV _pxCurrentTCB, W0 \n" /* Restore the stack pointer for the task. */ \
\r
158 "MOV [W0], W15 \n" \
\r
159 "POP W0 \n" /* Restore the critical nesting counter for the task. */ \
\r
160 "MOV W0, _uxCriticalNesting \n" \
\r
165 "POP DOSTARTH \n" \
\r
166 "POP DOSTARTL \n" \
\r
175 "POP RCOUNT \n" /* Restore the registers from the stack. */ \
\r
186 #endif /* MPLAB_DSPIC_PORT */
\r
189 * Setup the timer used to generate the tick interrupt.
\r
191 static void prvSetupTimerInterrupt( void );
\r
194 * See header file for description.
\r
196 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
198 unsigned short usCode;
\r
201 const portSTACK_TYPE xInitialStack[] =
\r
217 0xcdce, /* RCOUNT */
\r
218 0xabac, /* TBLPAG */
\r
220 /* dsPIC specific registers. */
\r
221 #ifdef MPLAB_DSPIC_PORT
\r
222 0x0202, /* ACCAL */
\r
223 0x0303, /* ACCAH */
\r
224 0x0404, /* ACCAU */
\r
225 0x0505, /* ACCBL */
\r
226 0x0606, /* ACCBH */
\r
227 0x0707, /* ACCBU */
\r
228 0x0808, /* DCOUNT */
\r
229 0x090a, /* DOSTARTL */
\r
230 0x1010, /* DOSTARTH */
\r
231 0x1110, /* DOENDL */
\r
232 0x1212, /* DOENDH */
\r
236 /* Setup the stack as if a yield had occurred.
\r
238 Save the low bytes of the program counter. */
\r
239 usCode = ( unsigned short ) pxCode;
\r
240 *pxTopOfStack = ( portSTACK_TYPE ) usCode;
\r
243 /* Save the high byte of the program counter. This will always be zero
\r
244 here as it is passed in a 16bit pointer. If the address is greater than
\r
245 16 bits then the pointer will point to a jump table. */
\r
246 *pxTopOfStack = ( portSTACK_TYPE ) 0;
\r
249 /* Status register with interrupts enabled. */
\r
250 *pxTopOfStack = portINITIAL_SR;
\r
253 /* Parameters are passed in W0. */
\r
254 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;
\r
257 for( i = 0; i < ( sizeof( xInitialStack ) / sizeof( portSTACK_TYPE ) ); i++ )
\r
259 *pxTopOfStack = xInitialStack[ i ];
\r
263 *pxTopOfStack = CORCON;
\r
266 #if defined(__HAS_EDS__)
\r
267 *pxTopOfStack = DSRPAG;
\r
269 *pxTopOfStack = DSWPAG;
\r
271 #else /* __HAS_EDS__ */
\r
272 *pxTopOfStack = PSVPAG;
\r
274 #endif /* __HAS_EDS__ */
\r
276 /* Finally the critical nesting depth. */
\r
277 *pxTopOfStack = 0x00;
\r
280 return pxTopOfStack;
\r
282 /*-----------------------------------------------------------*/
\r
284 portBASE_TYPE xPortStartScheduler( void )
\r
286 /* Setup a timer for the tick ISR. */
\r
287 prvSetupTimerInterrupt();
\r
289 /* Restore the context of the first task to run. */
\r
290 portRESTORE_CONTEXT();
\r
292 /* Simulate the end of the yield function. */
\r
293 asm volatile ( "return" );
\r
295 /* Should not reach here. */
\r
298 /*-----------------------------------------------------------*/
\r
300 void vPortEndScheduler( void )
\r
302 /* It is unlikely that the scheduler for the PIC port will get stopped
\r
303 once running. If required disable the tick interrupt here, then return
\r
304 to xPortStartScheduler(). */
\r
306 /*-----------------------------------------------------------*/
\r
309 * Setup a timer for a regular tick.
\r
311 static void prvSetupTimerInterrupt( void )
\r
313 const unsigned long ulCompareMatch = ( ( configCPU_CLOCK_HZ / portTIMER_PRESCALE ) / configTICK_RATE_HZ ) - 1;
\r
315 /* Prescale of 8. */
\r
319 PR1 = ( unsigned short ) ulCompareMatch;
\r
321 /* Setup timer 1 interrupt priority. */
\r
322 IPC0bits.T1IP = configKERNEL_INTERRUPT_PRIORITY;
\r
324 /* Clear the interrupt as a starting condition. */
\r
327 /* Enable the interrupt. */
\r
330 /* Setup the prescale value. */
\r
331 T1CONbits.TCKPS0 = 1;
\r
332 T1CONbits.TCKPS1 = 0;
\r
334 /* Start the timer. */
\r
337 /*-----------------------------------------------------------*/
\r
339 void vPortEnterCritical( void )
\r
341 portDISABLE_INTERRUPTS();
\r
342 uxCriticalNesting++;
\r
344 /*-----------------------------------------------------------*/
\r
346 void vPortExitCritical( void )
\r
348 uxCriticalNesting--;
\r
349 if( uxCriticalNesting == 0 )
\r
351 portENABLE_INTERRUPTS();
\r
354 /*-----------------------------------------------------------*/
\r
356 void __attribute__((__interrupt__, auto_psv)) _T1Interrupt( void )
\r
358 /* Clear the timer interrupt. */
\r
361 if( xTaskIncrementTick() != pdFALSE )
\r