2 FreeRTOS V8.2.2 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
70 /*-----------------------------------------------------------
\r
71 * Implementation of functions defined in portable.h for the PIC32MX port.
\r
72 *----------------------------------------------------------*/
\r
75 #error This port is designed to work with XC32. Please update your C compiler version.
\r
78 /* Scheduler include files. */
\r
79 #include "FreeRTOS.h"
\r
82 /* Hardware specifics. */
\r
83 #define portTIMER_PRESCALE 8
\r
84 #define portPRESCALE_BITS 1
\r
86 /* Bits within various registers. */
\r
87 #define portIE_BIT ( 0x00000001 )
\r
88 #define portEXL_BIT ( 0x00000002 )
\r
90 /* Bits within the CAUSE register. */
\r
91 #define portCORE_SW_0 ( 0x00000100 )
\r
92 #define portCORE_SW_1 ( 0x00000200 )
\r
94 /* The EXL bit is set to ensure interrupts do not occur while the context of
\r
95 the first task is being restored. */
\r
96 #define portINITIAL_SR ( portIE_BIT | portEXL_BIT )
\r
99 By default port.c generates its tick interrupt from TIMER1. The user can
\r
100 override this behaviour by:
\r
101 1: Providing their own implementation of vApplicationSetupTickTimerInterrupt(),
\r
102 which is the function that configures the timer. The function is defined
\r
103 as a weak symbol in this file so if the same function name is used in the
\r
104 application code then the version in the application code will be linked
\r
105 into the application in preference to the version defined in this file.
\r
106 2: Define configTICK_INTERRUPT_VECTOR to the vector number of the timer used
\r
107 to generate the tick interrupt. For example, when timer 1 is used then
\r
108 configTICK_INTERRUPT_VECTOR is set to _TIMER_1_VECTOR.
\r
109 configTICK_INTERRUPT_VECTOR should be defined in FreeRTOSConfig.h.
\r
110 3: Define configCLEAR_TICK_TIMER_INTERRUPT() to clear the interrupt in the
\r
111 timer used to generate the tick interrupt. For example, when timer 1 is
\r
112 used configCLEAR_TICK_TIMER_INTERRUPT() is defined to
\r
113 IFS0CLR = _IFS0_T1IF_MASK.
\r
115 #ifndef configTICK_INTERRUPT_VECTOR
\r
116 #define configTICK_INTERRUPT_VECTOR _TIMER_1_VECTOR
\r
117 #define configCLEAR_TICK_TIMER_INTERRUPT() IFS0CLR = _IFS0_T1IF_MASK
\r
119 #ifndef configCLEAR_TICK_TIMER_INTERRUPT
\r
120 #error If configTICK_INTERRUPT_VECTOR is defined in application code then configCLEAR_TICK_TIMER_INTERRUPT must also be defined in application code.
\r
124 /* Let the user override the pre-loading of the initial RA with the address of
\r
125 prvTaskExitError() in case it messes up unwinding of the stack in the
\r
126 debugger - in which case configTASK_RETURN_ADDRESS can be defined as 0 (NULL). */
\r
127 #ifdef configTASK_RETURN_ADDRESS
\r
128 #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS
\r
130 #define portTASK_RETURN_ADDRESS prvTaskExitError
\r
133 /* Set configCHECK_FOR_STACK_OVERFLOW to 3 to add ISR stack checking to task
\r
134 stack checking. A problem in the ISR stack will trigger an assert, not call the
\r
135 stack overflow hook function (because the stack overflow hook is specific to a
\r
136 task stack, not the ISR stack). */
\r
137 #if( configCHECK_FOR_STACK_OVERFLOW > 2 )
\r
139 /* Don't use 0xa5 as the stack fill bytes as that is used by the kernerl for
\r
140 the task stacks, and so will legitimately appear in many positions within
\r
142 #define portISR_STACK_FILL_BYTE 0xee
\r
144 static const uint8_t ucExpectedStackBytes[] = {
\r
145 portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
\r
146 portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
\r
147 portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
\r
148 portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
\r
149 portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE }; \
\r
151 #define portCHECK_ISR_STACK() configASSERT( ( memcmp( ( void * ) xISRStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) == 0 ) )
\r
153 /* Define the function away. */
\r
154 #define portCHECK_ISR_STACK()
\r
155 #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */
\r
157 /*-----------------------------------------------------------*/
\r
161 * Place the prototype here to ensure the interrupt vector is correctly installed.
\r
162 * Note that because the interrupt is written in assembly, the IPL setting in the
\r
163 * following line of code has no effect. The interrupt priority is set by the
\r
164 * call to ConfigIntTimer1() in vApplicationSetupTickTimerInterrupt().
\r
166 extern void __attribute__( (interrupt(IPL1AUTO), vector( configTICK_INTERRUPT_VECTOR ))) vPortTickInterruptHandler( void );
\r
169 * The software interrupt handler that performs the yield. Note that, because
\r
170 * the interrupt is written in assembly, the IPL setting in the following line of
\r
171 * code has no effect. The interrupt priority is set by the call to
\r
172 * mConfigIntCoreSW0() in xPortStartScheduler().
\r
174 void __attribute__( (interrupt(IPL1AUTO), vector(_CORE_SOFTWARE_0_VECTOR))) vPortYieldISR( void );
\r
177 * Used to catch tasks that attempt to return from their implementing function.
\r
179 static void prvTaskExitError( void );
\r
181 /*-----------------------------------------------------------*/
\r
183 /* Records the interrupt nesting depth. This is initialised to one as it is
\r
184 decremented to 0 when the first task starts. */
\r
185 volatile UBaseType_t uxInterruptNesting = 0x01;
\r
187 /* Stores the task stack pointer when a switch is made to use the system stack. */
\r
188 UBaseType_t uxSavedTaskStackPointer = 0;
\r
190 /* The stack used by interrupt service routines that cause a context switch. */
\r
191 StackType_t xISRStack[ configISR_STACK_SIZE ] = { 0 };
\r
193 /* The top of stack value ensures there is enough space to store 6 registers on
\r
194 the callers stack, as some functions seem to want to do this. */
\r
195 const StackType_t * const xISRStackTop = &( xISRStack[ configISR_STACK_SIZE - 7 ] );
\r
197 /*-----------------------------------------------------------*/
\r
200 * See header file for description.
\r
202 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
204 /* Ensure byte alignment is maintained when leaving this function. */
\r
207 *pxTopOfStack = (StackType_t) 0xDEADBEEF;
\r
210 *pxTopOfStack = (StackType_t) 0x12345678; /* Word to which the stack pointer will be left pointing after context restore. */
\r
213 *pxTopOfStack = (StackType_t) _CP0_GET_CAUSE();
\r
216 *pxTopOfStack = (StackType_t) portINITIAL_SR;/* CP0_STATUS */
\r
219 *pxTopOfStack = (StackType_t) pxCode; /* CP0_EPC */
\r
222 *pxTopOfStack = (StackType_t) portTASK_RETURN_ADDRESS; /* ra */
\r
223 pxTopOfStack -= 15;
\r
225 *pxTopOfStack = (StackType_t) pvParameters; /* Parameters to pass in. */
\r
226 pxTopOfStack -= 15;
\r
228 return pxTopOfStack;
\r
230 /*-----------------------------------------------------------*/
\r
232 static void prvTaskExitError( void )
\r
234 /* A function that implements a task must not exit or attempt to return to
\r
235 its caller as there is nothing to return to. If a task wants to exit it
\r
236 should instead call vTaskDelete( NULL ).
\r
238 Artificially force an assert() to be triggered if configASSERT() is
\r
239 defined, then stop here so application writers can catch the error. */
\r
240 configASSERT( uxSavedTaskStackPointer == 0UL );
\r
241 portDISABLE_INTERRUPTS();
\r
244 /*-----------------------------------------------------------*/
\r
247 * Setup a timer for a regular tick. This function uses peripheral timer 1.
\r
248 * The function is declared weak so an application writer can use a different
\r
249 * timer by redefining this implementation. If a different timer is used then
\r
250 * configTICK_INTERRUPT_VECTOR must also be defined in FreeRTOSConfig.h to
\r
251 * ensure the RTOS provided tick interrupt handler is installed on the correct
\r
252 * vector number. When Timer 1 is used the vector number is defined as
\r
255 __attribute__(( weak )) void vApplicationSetupTickTimerInterrupt( void )
\r
257 const uint32_t ulCompareMatch = ( (configPERIPHERAL_CLOCK_HZ / portTIMER_PRESCALE) / configTICK_RATE_HZ ) - 1;
\r
260 T1CONbits.TCKPS = portPRESCALE_BITS;
\r
261 PR1 = ulCompareMatch;
\r
262 IPC1bits.T1IP = configKERNEL_INTERRUPT_PRIORITY;
\r
264 /* Clear the interrupt as a starting condition. */
\r
267 /* Enable the interrupt. */
\r
270 /* Start the timer. */
\r
273 /*-----------------------------------------------------------*/
\r
275 void vPortEndScheduler(void)
\r
277 /* Not implemented in ports where there is nothing to return to.
\r
278 Artificially force an assert. */
\r
279 configASSERT( uxInterruptNesting == 1000UL );
\r
281 /*-----------------------------------------------------------*/
\r
283 BaseType_t xPortStartScheduler( void )
\r
285 extern void vPortStartFirstTask( void );
\r
286 extern void *pxCurrentTCB;
\r
288 #if ( configCHECK_FOR_STACK_OVERFLOW > 2 )
\r
290 /* Fill the ISR stack to make it easy to asses how much is being used. */
\r
291 memset( ( void * ) xISRStack, portISR_STACK_FILL_BYTE, sizeof( xISRStack ) );
\r
293 #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */
\r
295 /* Clear the software interrupt flag. */
\r
296 IFS0CLR = _IFS0_CS0IF_MASK;
\r
298 /* Set software timer priority. */
\r
299 IPC0CLR = _IPC0_CS0IP_MASK;
\r
300 IPC0SET = ( configKERNEL_INTERRUPT_PRIORITY << _IPC0_CS0IP_POSITION );
\r
302 /* Enable software interrupt. */
\r
303 IEC0CLR = _IEC0_CS0IE_MASK;
\r
304 IEC0SET = 1 << _IEC0_CS0IE_POSITION;
\r
306 /* Setup the timer to generate the tick. Interrupts will have been
\r
307 disabled by the time we get here. */
\r
308 vApplicationSetupTickTimerInterrupt();
\r
310 /* Kick off the highest priority task that has been created so far.
\r
311 Its stack location is loaded into uxSavedTaskStackPointer. */
\r
312 uxSavedTaskStackPointer = *( UBaseType_t * ) pxCurrentTCB;
\r
313 vPortStartFirstTask();
\r
315 /* Should never get here as the tasks will now be executing! Call the task
\r
316 exit error function to prevent compiler warnings about a static function
\r
317 not being called in the case that the application writer overrides this
\r
318 functionality by defining configTASK_RETURN_ADDRESS. */
\r
319 prvTaskExitError();
\r
323 /*-----------------------------------------------------------*/
\r
325 void vPortIncrementTick( void )
\r
327 UBaseType_t uxSavedStatus;
\r
329 uxSavedStatus = uxPortSetInterruptMaskFromISR();
\r
331 if( xTaskIncrementTick() != pdFALSE )
\r
333 /* Pend a context switch. */
\r
334 _CP0_BIS_CAUSE( portCORE_SW_0 );
\r
337 vPortClearInterruptMaskFromISR( uxSavedStatus );
\r
339 /* Look for the ISR stack getting near or past its limit. */
\r
340 portCHECK_ISR_STACK();
\r
342 /* Clear timer interrupt. */
\r
343 configCLEAR_TICK_TIMER_INTERRUPT();
\r
345 /*-----------------------------------------------------------*/
\r
347 UBaseType_t uxPortSetInterruptMaskFromISR( void )
\r
349 UBaseType_t uxSavedStatusRegister;
\r
351 __builtin_disable_interrupts();
\r
352 uxSavedStatusRegister = _CP0_GET_STATUS() | 0x01;
\r
353 /* This clears the IPL bits, then sets them to
\r
354 configMAX_SYSCALL_INTERRUPT_PRIORITY. This function should not be called
\r
355 from an interrupt that has a priority above
\r
356 configMAX_SYSCALL_INTERRUPT_PRIORITY so, when used correctly, the action
\r
357 can only result in the IPL being unchanged or raised, and therefore never
\r
359 _CP0_SET_STATUS( ( ( uxSavedStatusRegister & ( ~portALL_IPL_BITS ) ) ) | ( configMAX_SYSCALL_INTERRUPT_PRIORITY << portIPL_SHIFT ) );
\r
361 return uxSavedStatusRegister;
\r
363 /*-----------------------------------------------------------*/
\r
365 void vPortClearInterruptMaskFromISR( UBaseType_t uxSavedStatusRegister )
\r
367 _CP0_SET_STATUS( uxSavedStatusRegister );
\r
369 /*-----------------------------------------------------------*/
\r