2 FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\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 modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
70 + Introduced the configKERNEL_INTERRUPT_PRIORITY definition.
\r
73 /*-----------------------------------------------------------
\r
74 * Implementation of functions defined in portable.h for the PIC24 port.
\r
75 *----------------------------------------------------------*/
\r
77 /* Scheduler include files. */
\r
78 #include "FreeRTOS.h"
\r
81 /* Hardware specifics. */
\r
82 #define portBIT_SET 1
\r
83 #define portTIMER_PRESCALE 8
\r
84 #define portINITIAL_SR 0
\r
86 /* Defined for backward compatability with project created prior to
\r
87 FreeRTOS.org V4.3.0. */
\r
88 #ifndef configKERNEL_INTERRUPT_PRIORITY
\r
89 #define configKERNEL_INTERRUPT_PRIORITY 1
\r
92 /* The program counter is only 23 bits. */
\r
93 #define portUNUSED_PR_BITS 0x7f
\r
95 /* Records the nesting depth of calls to portENTER_CRITICAL(). */
\r
96 unsigned portBASE_TYPE uxCriticalNesting = 0xef;
\r
98 #if configKERNEL_INTERRUPT_PRIORITY != 1
\r
99 #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
102 #ifdef MPLAB_PIC24_PORT
\r
104 #define portRESTORE_CONTEXT() \
\r
105 asm volatile( "MOV _pxCurrentTCB, W0 \n" /* Restore the stack pointer for the task. */ \
\r
106 "MOV [W0], W15 \n" \
\r
107 "POP W0 \n" /* Restore the critical nesting counter for the task. */ \
\r
108 "MOV W0, _uxCriticalNesting \n" \
\r
112 "POP RCOUNT \n" /* Restore the registers from the stack. */ \
\r
123 #endif /* MPLAB_PIC24_PORT */
\r
125 #ifdef MPLAB_DSPIC_PORT
\r
127 #define portRESTORE_CONTEXT() \
\r
128 asm volatile( "MOV _pxCurrentTCB, W0 \n" /* Restore the stack pointer for the task. */ \
\r
129 "MOV [W0], W15 \n" \
\r
130 "POP W0 \n" /* Restore the critical nesting counter for the task. */ \
\r
131 "MOV W0, _uxCriticalNesting \n" \
\r
136 "POP DOSTARTH \n" \
\r
137 "POP DOSTARTL \n" \
\r
146 "POP RCOUNT \n" /* Restore the registers from the stack. */ \
\r
157 #endif /* MPLAB_DSPIC_PORT */
\r
160 * Setup the timer used to generate the tick interrupt.
\r
162 static void prvSetupTimerInterrupt( void );
\r
165 * See header file for description.
\r
167 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
169 unsigned short usCode;
\r
172 const portSTACK_TYPE xInitialStack[] =
\r
188 0xcdce, /* RCOUNT */
\r
189 0xabac, /* TBLPAG */
\r
191 /* dsPIC specific registers. */
\r
192 #ifdef MPLAB_DSPIC_PORT
\r
193 0x0202, /* ACCAL */
\r
194 0x0303, /* ACCAH */
\r
195 0x0404, /* ACCAU */
\r
196 0x0505, /* ACCBL */
\r
197 0x0606, /* ACCBH */
\r
198 0x0707, /* ACCBU */
\r
199 0x0808, /* DCOUNT */
\r
200 0x090a, /* DOSTARTL */
\r
201 0x1010, /* DOSTARTH */
\r
202 0x1110, /* DOENDL */
\r
203 0x1212, /* DOENDH */
\r
207 /* Setup the stack as if a yield had occurred.
\r
209 Save the low bytes of the program counter. */
\r
210 usCode = ( unsigned short ) pxCode;
\r
211 *pxTopOfStack = ( portSTACK_TYPE ) usCode;
\r
214 /* Save the high byte of the program counter. This will always be zero
\r
215 here as it is passed in a 16bit pointer. If the address is greater than
\r
216 16 bits then the pointer will point to a jump table. */
\r
217 *pxTopOfStack = ( portSTACK_TYPE ) 0;
\r
220 /* Status register with interrupts enabled. */
\r
221 *pxTopOfStack = portINITIAL_SR;
\r
224 /* Parameters are passed in W0. */
\r
225 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;
\r
228 for( i = 0; i < ( sizeof( xInitialStack ) / sizeof( portSTACK_TYPE ) ); i++ )
\r
230 *pxTopOfStack = xInitialStack[ i ];
\r
234 *pxTopOfStack = CORCON;
\r
236 *pxTopOfStack = PSVPAG;
\r
239 /* Finally the critical nesting depth. */
\r
240 *pxTopOfStack = 0x00;
\r
243 return pxTopOfStack;
\r
245 /*-----------------------------------------------------------*/
\r
247 portBASE_TYPE xPortStartScheduler( void )
\r
249 /* Setup a timer for the tick ISR. */
\r
250 prvSetupTimerInterrupt();
\r
252 /* Restore the context of the first task to run. */
\r
253 portRESTORE_CONTEXT();
\r
255 /* Simulate the end of the yield function. */
\r
256 asm volatile ( "return" );
\r
258 /* Should not reach here. */
\r
261 /*-----------------------------------------------------------*/
\r
263 void vPortEndScheduler( void )
\r
265 /* It is unlikely that the scheduler for the PIC port will get stopped
\r
266 once running. If required disable the tick interrupt here, then return
\r
267 to xPortStartScheduler(). */
\r
269 /*-----------------------------------------------------------*/
\r
272 * Setup a timer for a regular tick.
\r
274 static void prvSetupTimerInterrupt( void )
\r
276 const unsigned long ulCompareMatch = ( ( configCPU_CLOCK_HZ / portTIMER_PRESCALE ) / configTICK_RATE_HZ ) - 1;
\r
278 /* Prescale of 8. */
\r
282 PR1 = ( unsigned short ) ulCompareMatch;
\r
284 /* Setup timer 1 interrupt priority. */
\r
285 IPC0bits.T1IP = configKERNEL_INTERRUPT_PRIORITY;
\r
287 /* Clear the interrupt as a starting condition. */
\r
290 /* Enable the interrupt. */
\r
293 /* Setup the prescale value. */
\r
294 T1CONbits.TCKPS0 = 1;
\r
295 T1CONbits.TCKPS1 = 0;
\r
297 /* Start the timer. */
\r
300 /*-----------------------------------------------------------*/
\r
302 void vPortEnterCritical( void )
\r
304 portDISABLE_INTERRUPTS();
\r
305 uxCriticalNesting++;
\r
307 /*-----------------------------------------------------------*/
\r
309 void vPortExitCritical( void )
\r
311 uxCriticalNesting--;
\r
312 if( uxCriticalNesting == 0 )
\r
314 portENABLE_INTERRUPTS();
\r
317 /*-----------------------------------------------------------*/
\r
319 void __attribute__((__interrupt__, auto_psv)) _T1Interrupt( void )
\r
321 /* Clear the timer interrupt. */
\r
324 vTaskIncrementTick();
\r
326 #if configUSE_PREEMPTION == 1
\r