2 FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 /*-----------------------------------------------------------
\r
67 * Implementation of functions defined in portable.h for the PIC32MZ port.
\r
68 *----------------------------------------------------------*/
\r
72 /* Scheduler include files. */
\r
73 #include "FreeRTOS.h"
\r
76 #if !defined(__PIC32MZ__)
\r
77 #error This port is designed to work with XC32 on PIC32MZ MCUs. Please update your C compiler version or settings.
\r
80 #if( ( configMAX_SYSCALL_INTERRUPT_PRIORITY >= 0x7 ) || ( configMAX_SYSCALL_INTERRUPT_PRIORITY == 0 ) )
\r
81 #error configMAX_SYSCALL_INTERRUPT_PRIORITY must be less than 7 and greater than 0
\r
84 /* Hardware specifics. */
\r
85 #define portTIMER_PRESCALE 8
\r
86 #define portPRESCALE_BITS 1
\r
88 /* Bits within various registers. */
\r
89 #define portIE_BIT ( 0x00000001 )
\r
90 #define portEXL_BIT ( 0x00000002 )
\r
91 #define portMX_BIT ( 0x01000000 ) /* Allow access to DSP instructions. */
\r
93 /* Bits within the CAUSE register. */
\r
94 #define portCORE_SW_0 ( 0x00000100 )
\r
95 #define portCORE_SW_1 ( 0x00000200 )
\r
97 /* The EXL bit is set to ensure interrupts do not occur while the context of
\r
98 the first task is being restored. */
\r
99 #define portINITIAL_SR ( portIE_BIT | portEXL_BIT | portMX_BIT )
\r
102 By default port.c generates its tick interrupt from TIMER1. The user can
\r
103 override this behaviour by:
\r
104 1: Providing their own implementation of vApplicationSetupTickTimerInterrupt(),
\r
105 which is the function that configures the timer. The function is defined
\r
106 as a weak symbol in this file so if the same function name is used in the
\r
107 application code then the version in the application code will be linked
\r
108 into the application in preference to the version defined in this file.
\r
109 2: Define configTICK_INTERRUPT_VECTOR to the vector number of the timer used
\r
110 to generate the tick interrupt. For example, when timer 1 is used then
\r
111 configTICK_INTERRUPT_VECTOR is set to _TIMER_1_VECTOR.
\r
112 configTICK_INTERRUPT_VECTOR should be defined in FreeRTOSConfig.h.
\r
113 3: Define configCLEAR_TICK_TIMER_INTERRUPT() to clear the interrupt in the
\r
114 timer used to generate the tick interrupt. For example, when timer 1 is
\r
115 used configCLEAR_TICK_TIMER_INTERRUPT() is defined to
\r
116 IFS0CLR = _IFS0_T1IF_MASK.
\r
118 #ifndef configTICK_INTERRUPT_VECTOR
\r
119 #define configTICK_INTERRUPT_VECTOR _TIMER_1_VECTOR
\r
120 #define configCLEAR_TICK_TIMER_INTERRUPT() IFS0CLR = _IFS0_T1IF_MASK
\r
122 #ifndef configCLEAR_TICK_TIMER_INTERRUPT
\r
123 #error If configTICK_INTERRUPT_VECTOR is defined in application code then configCLEAR_TICK_TIMER_INTERRUPT must also be defined in application code.
\r
127 /* Let the user override the pre-loading of the initial RA with the address of
\r
128 prvTaskExitError() in case is messes up unwinding of the stack in the
\r
129 debugger - in which case configTASK_RETURN_ADDRESS can be defined as 0 (NULL). */
\r
130 #ifdef configTASK_RETURN_ADDRESS
\r
131 #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS
\r
133 #define portTASK_RETURN_ADDRESS prvTaskExitError
\r
136 /* Set configCHECK_FOR_STACK_OVERFLOW to 3 to add ISR stack checking to task
\r
137 stack checking. A problem in the ISR stack will trigger an assert, not call the
\r
138 stack overflow hook function (because the stack overflow hook is specific to a
\r
139 task stack, not the ISR stack). */
\r
140 #if( configCHECK_FOR_STACK_OVERFLOW > 2 )
\r
142 /* Don't use 0xa5 as the stack fill bytes as that is used by the kernerl for
\r
143 the task stacks, and so will legitimately appear in many positions within
\r
145 #define portISR_STACK_FILL_BYTE 0xee
\r
147 static const uint8_t ucExpectedStackBytes[] = {
\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
150 portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
\r
151 portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
\r
152 portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE }; \
\r
154 #define portCHECK_ISR_STACK() configASSERT( ( memcmp( ( void * ) xISRStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) == 0 ) )
\r
156 /* Define the function away. */
\r
157 #define portCHECK_ISR_STACK()
\r
158 #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */
\r
160 /*-----------------------------------------------------------*/
\r
163 * Place the prototype here to ensure the interrupt vector is correctly installed.
\r
164 * Note that because the interrupt is written in assembly, the IPL setting in the
\r
165 * following line of code has no effect. The interrupt priority is set by the
\r
166 * call to ConfigIntTimer1() in vApplicationSetupTickTimerInterrupt().
\r
168 extern void __attribute__( (interrupt(ipl1), vector( configTICK_INTERRUPT_VECTOR ))) vPortTickInterruptHandler( void );
\r
171 * The software interrupt handler that performs the yield. Note that, because
\r
172 * the interrupt is written in assembly, the IPL setting in the following line of
\r
173 * code has no effect. The interrupt priority is set by the call to
\r
174 * mConfigIntCoreSW0() in xPortStartScheduler().
\r
176 void __attribute__( (interrupt(ipl1), vector(_CORE_SOFTWARE_0_VECTOR))) vPortYieldISR( void );
\r
179 * Used to catch tasks that attempt to return from their implementing function.
\r
181 static void prvTaskExitError( void );
\r
183 /*-----------------------------------------------------------*/
\r
185 /* Records the interrupt nesting depth. This is initialised to one as it is
\r
186 decremented to 0 when the first task starts. */
\r
187 volatile UBaseType_t uxInterruptNesting = 0x01;
\r
189 /* Stores the task stack pointer when a switch is made to use the system stack. */
\r
190 UBaseType_t uxSavedTaskStackPointer = 0;
\r
192 /* The stack used by interrupt service routines that cause a context switch. */
\r
193 StackType_t xISRStack[ configISR_STACK_SIZE ] = { 0 };
\r
195 /* The top of stack value ensures there is enough space to store 6 registers on
\r
196 the callers stack, as some functions seem to want to do this. */
\r
197 const StackType_t * const xISRStackTop = &( xISRStack[ configISR_STACK_SIZE - 7 ] );
\r
199 /*-----------------------------------------------------------*/
\r
202 * See header file for description.
\r
204 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
206 /* Ensure byte alignment is maintained when leaving this function. */
\r
209 *pxTopOfStack = (StackType_t) 0xDEADBEEF;
\r
212 *pxTopOfStack = (StackType_t) 0x12345678; /* Word to which the stack pointer will be left pointing after context restore. */
\r
215 *pxTopOfStack = (StackType_t) _CP0_GET_CAUSE();
\r
218 *pxTopOfStack = (StackType_t) portINITIAL_SR;/* CP0_STATUS */
\r
221 *pxTopOfStack = (StackType_t) pxCode; /* CP0_EPC */
\r
222 pxTopOfStack -= 7; /* Includes space for AC1 - AC3. */
\r
224 *pxTopOfStack = (StackType_t) 0x00000000; /* DSPControl */
\r
227 *pxTopOfStack = (StackType_t) portTASK_RETURN_ADDRESS; /* ra */
\r
228 pxTopOfStack -= 15;
\r
230 *pxTopOfStack = (StackType_t) pvParameters; /* Parameters to pass in. */
\r
231 pxTopOfStack -= 15;
\r
233 return pxTopOfStack;
\r
235 /*-----------------------------------------------------------*/
\r
237 static void prvTaskExitError( void )
\r
239 /* A function that implements a task must not exit or attempt to return to
\r
240 its caller as there is nothing to return to. If a task wants to exit it
\r
241 should instead call vTaskDelete( NULL ).
\r
243 Artificially force an assert() to be triggered if configASSERT() is
\r
244 defined, then stop here so application writers can catch the error. */
\r
245 configASSERT( uxSavedTaskStackPointer == 0UL );
\r
246 portDISABLE_INTERRUPTS();
\r
249 /*-----------------------------------------------------------*/
\r
252 * Setup a timer for a regular tick. This function uses peripheral timer 1.
\r
253 * The function is declared weak so an application writer can use a different
\r
254 * timer by redefining this implementation. If a different timer is used then
\r
255 * configTICK_INTERRUPT_VECTOR must also be defined in FreeRTOSConfig.h to
\r
256 * ensure the RTOS provided tick interrupt handler is installed on the correct
\r
257 * vector number. When Timer 1 is used the vector number is defined as
\r
260 __attribute__(( weak )) void vApplicationSetupTickTimerInterrupt( void )
\r
262 const uint32_t ulCompareMatch = ( (configPERIPHERAL_CLOCK_HZ / portTIMER_PRESCALE) / configTICK_RATE_HZ ) - 1;
\r
265 T1CONbits.TCKPS = portPRESCALE_BITS;
\r
266 PR1 = ulCompareMatch;
\r
267 IPC1bits.T1IP = configKERNEL_INTERRUPT_PRIORITY;
\r
269 /* Clear the interrupt as a starting condition. */
\r
272 /* Enable the interrupt. */
\r
275 /* Start the timer. */
\r
278 /*-----------------------------------------------------------*/
\r
280 void vPortEndScheduler(void)
\r
282 /* Not implemented in ports where there is nothing to return to.
\r
283 Artificially force an assert. */
\r
284 configASSERT( uxInterruptNesting == 1000UL );
\r
286 /*-----------------------------------------------------------*/
\r
288 BaseType_t xPortStartScheduler( void )
\r
290 extern void vPortStartFirstTask( void );
\r
291 extern void *pxCurrentTCB;
\r
293 #if ( configCHECK_FOR_STACK_OVERFLOW > 2 )
\r
295 /* Fill the ISR stack to make it easy to asses how much is being used. */
\r
296 memset( ( void * ) xISRStack, portISR_STACK_FILL_BYTE, sizeof( xISRStack ) );
\r
298 #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */
\r
300 /* Clear the software interrupt flag. */
\r
301 IFS0CLR = _IFS0_CS0IF_MASK;
\r
303 /* Set software timer priority. */
\r
304 IPC0CLR = _IPC0_CS0IP_MASK;
\r
305 IPC0SET = ( configKERNEL_INTERRUPT_PRIORITY << _IPC0_CS0IP_POSITION );
\r
307 /* Enable software interrupt. */
\r
308 IEC0CLR = _IEC0_CS0IE_MASK;
\r
309 IEC0SET = 1 << _IEC0_CS0IE_POSITION;
\r
311 /* Setup the timer to generate the tick. Interrupts will have been
\r
312 disabled by the time we get here. */
\r
313 vApplicationSetupTickTimerInterrupt();
\r
315 /* Kick off the highest priority task that has been created so far.
\r
316 Its stack location is loaded into uxSavedTaskStackPointer. */
\r
317 uxSavedTaskStackPointer = *( UBaseType_t * ) pxCurrentTCB;
\r
318 vPortStartFirstTask();
\r
320 /* Should never get here as the tasks will now be executing! Call the task
\r
321 exit error function to prevent compiler warnings about a static function
\r
322 not being called in the case that the application writer overrides this
\r
323 functionality by defining configTASK_RETURN_ADDRESS. */
\r
324 prvTaskExitError();
\r
328 /*-----------------------------------------------------------*/
\r
330 void vPortIncrementTick( void )
\r
332 UBaseType_t uxSavedStatus;
\r
334 uxSavedStatus = uxPortSetInterruptMaskFromISR();
\r
336 if( xTaskIncrementTick() != pdFALSE )
\r
338 /* Pend a context switch. */
\r
339 _CP0_BIS_CAUSE( portCORE_SW_0 );
\r
342 vPortClearInterruptMaskFromISR( uxSavedStatus );
\r
344 /* Look for the ISR stack getting near or past its limit. */
\r
345 portCHECK_ISR_STACK();
\r
347 /* Clear timer interrupt. */
\r
348 configCLEAR_TICK_TIMER_INTERRUPT();
\r
350 /*-----------------------------------------------------------*/
\r
352 UBaseType_t uxPortSetInterruptMaskFromISR( void )
\r
354 UBaseType_t uxSavedStatusRegister;
\r
356 __builtin_disable_interrupts();
\r
357 uxSavedStatusRegister = _CP0_GET_STATUS() | 0x01;
\r
358 /* This clears the IPL bits, then sets them to
\r
359 configMAX_SYSCALL_INTERRUPT_PRIORITY. This function should not be called
\r
360 from an interrupt that has a priority above
\r
361 configMAX_SYSCALL_INTERRUPT_PRIORITY so, when used correctly, the action
\r
362 can only result in the IPL being unchanged or raised, and therefore never
\r
364 _CP0_SET_STATUS( ( ( uxSavedStatusRegister & ( ~portALL_IPL_BITS ) ) ) | ( configMAX_SYSCALL_INTERRUPT_PRIORITY << portIPL_SHIFT ) );
\r
366 return uxSavedStatusRegister;
\r
368 /*-----------------------------------------------------------*/
\r
370 void vPortClearInterruptMaskFromISR( UBaseType_t uxSavedStatusRegister )
\r
372 _CP0_SET_STATUS( uxSavedStatusRegister );
\r
374 /*-----------------------------------------------------------*/
\r