2 * FreeRTOS Kernel V10.4.3 LTS Patch 3
\r
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * https://www.FreeRTOS.org
\r
23 * https://github.com/FreeRTOS
\r
25 * 1 tab == 4 spaces!
\r
28 /*-----------------------------------------------------------
\r
29 * Implementation of functions defined in portable.h for the PIC32MX port.
\r
30 *----------------------------------------------------------*/
\r
33 #error This port is designed to work with XC32. Please update your C compiler version.
\r
36 /* Scheduler include files. */
\r
37 #include "FreeRTOS.h"
\r
40 /* Hardware specifics. */
\r
41 #define portTIMER_PRESCALE 8
\r
42 #define portPRESCALE_BITS 1
\r
44 /* Bits within various registers. */
\r
45 #define portIE_BIT ( 0x00000001 )
\r
46 #define portEXL_BIT ( 0x00000002 )
\r
48 /* Bits within the CAUSE register. */
\r
49 #define portCORE_SW_0 ( 0x00000100 )
\r
50 #define portCORE_SW_1 ( 0x00000200 )
\r
52 /* The EXL bit is set to ensure interrupts do not occur while the context of
\r
53 the first task is being restored. */
\r
54 #define portINITIAL_SR ( portIE_BIT | portEXL_BIT )
\r
57 By default port.c generates its tick interrupt from TIMER1. The user can
\r
58 override this behaviour by:
\r
59 1: Providing their own implementation of vApplicationSetupTickTimerInterrupt(),
\r
60 which is the function that configures the timer. The function is defined
\r
61 as a weak symbol in this file so if the same function name is used in the
\r
62 application code then the version in the application code will be linked
\r
63 into the application in preference to the version defined in this file.
\r
64 2: Define configTICK_INTERRUPT_VECTOR to the vector number of the timer used
\r
65 to generate the tick interrupt. For example, when timer 1 is used then
\r
66 configTICK_INTERRUPT_VECTOR is set to _TIMER_1_VECTOR.
\r
67 configTICK_INTERRUPT_VECTOR should be defined in FreeRTOSConfig.h.
\r
68 3: Define configCLEAR_TICK_TIMER_INTERRUPT() to clear the interrupt in the
\r
69 timer used to generate the tick interrupt. For example, when timer 1 is
\r
70 used configCLEAR_TICK_TIMER_INTERRUPT() is defined to
\r
71 IFS0CLR = _IFS0_T1IF_MASK.
\r
73 #ifndef configTICK_INTERRUPT_VECTOR
\r
74 #define configTICK_INTERRUPT_VECTOR _TIMER_1_VECTOR
\r
75 #define configCLEAR_TICK_TIMER_INTERRUPT() IFS0CLR = _IFS0_T1IF_MASK
\r
77 #ifndef configCLEAR_TICK_TIMER_INTERRUPT
\r
78 #error If configTICK_INTERRUPT_VECTOR is defined in application code then configCLEAR_TICK_TIMER_INTERRUPT must also be defined in application code.
\r
82 /* Let the user override the pre-loading of the initial RA with the address of
\r
83 prvTaskExitError() in case it messes up unwinding of the stack in the
\r
84 debugger - in which case configTASK_RETURN_ADDRESS can be defined as 0 (NULL). */
\r
85 #ifdef configTASK_RETURN_ADDRESS
\r
86 #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS
\r
88 #define portTASK_RETURN_ADDRESS prvTaskExitError
\r
91 /* Set configCHECK_FOR_STACK_OVERFLOW to 3 to add ISR stack checking to task
\r
92 stack checking. A problem in the ISR stack will trigger an assert, not call the
\r
93 stack overflow hook function (because the stack overflow hook is specific to a
\r
94 task stack, not the ISR stack). */
\r
95 #if( configCHECK_FOR_STACK_OVERFLOW > 2 )
\r
97 /* Don't use 0xa5 as the stack fill bytes as that is used by the kernerl for
\r
98 the task stacks, and so will legitimately appear in many positions within
\r
100 #define portISR_STACK_FILL_BYTE 0xee
\r
102 static const uint8_t ucExpectedStackBytes[] = {
\r
103 portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
\r
104 portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
\r
105 portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
\r
106 portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, \
\r
107 portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE }; \
\r
109 #define portCHECK_ISR_STACK() configASSERT( ( memcmp( ( void * ) xISRStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) == 0 ) )
\r
111 /* Define the function away. */
\r
112 #define portCHECK_ISR_STACK()
\r
113 #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */
\r
115 /*-----------------------------------------------------------*/
\r
119 * Place the prototype here to ensure the interrupt vector is correctly installed.
\r
120 * Note that because the interrupt is written in assembly, the IPL setting in the
\r
121 * following line of code has no effect. The interrupt priority is set by the
\r
122 * call to ConfigIntTimer1() in vApplicationSetupTickTimerInterrupt().
\r
124 extern void __attribute__( (interrupt(IPL1AUTO), vector( configTICK_INTERRUPT_VECTOR ))) vPortTickInterruptHandler( void );
\r
127 * The software interrupt handler that performs the yield. Note that, because
\r
128 * the interrupt is written in assembly, the IPL setting in the following line of
\r
129 * code has no effect. The interrupt priority is set by the call to
\r
130 * mConfigIntCoreSW0() in xPortStartScheduler().
\r
132 void __attribute__( (interrupt(IPL1AUTO), vector(_CORE_SOFTWARE_0_VECTOR))) vPortYieldISR( void );
\r
135 * Used to catch tasks that attempt to return from their implementing function.
\r
137 static void prvTaskExitError( void );
\r
139 /*-----------------------------------------------------------*/
\r
141 /* Records the interrupt nesting depth. This is initialised to one as it is
\r
142 decremented to 0 when the first task starts. */
\r
143 volatile UBaseType_t uxInterruptNesting = 0x01;
\r
145 /* Stores the task stack pointer when a switch is made to use the system stack. */
\r
146 UBaseType_t uxSavedTaskStackPointer = 0;
\r
148 /* The stack used by interrupt service routines that cause a context switch. */
\r
149 __attribute__ ((aligned(8))) StackType_t xISRStack[ configISR_STACK_SIZE ] = { 0 };
\r
151 /* The top of stack value ensures there is enough space to store 6 registers on
\r
152 the callers stack, as some functions seem to want to do this. */
\r
153 const StackType_t * const xISRStackTop = &( xISRStack[ ( configISR_STACK_SIZE & ~portBYTE_ALIGNMENT_MASK ) - 8 ] );
\r
155 /*-----------------------------------------------------------*/
\r
158 * See header file for description.
\r
160 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
162 /* Ensure 8 byte alignment is maintained when the context is popped from
\r
163 * stack. The size of the context is 33 words (132 bytes). */
\r
167 *pxTopOfStack = (StackType_t) 0xDEADBEEF;
\r
170 *pxTopOfStack = (StackType_t) 0x12345678; /* Word to which the stack pointer will be left pointing after context restore. */
\r
173 *pxTopOfStack = (StackType_t) _CP0_GET_CAUSE();
\r
176 *pxTopOfStack = (StackType_t) portINITIAL_SR;/* CP0_STATUS */
\r
179 *pxTopOfStack = (StackType_t) pxCode; /* CP0_EPC */
\r
182 *pxTopOfStack = (StackType_t) portTASK_RETURN_ADDRESS; /* ra */
\r
183 pxTopOfStack -= 15;
\r
185 *pxTopOfStack = (StackType_t) pvParameters; /* Parameters to pass in. */
\r
186 pxTopOfStack -= 15;
\r
188 return pxTopOfStack;
\r
190 /*-----------------------------------------------------------*/
\r
192 static void prvTaskExitError( void )
\r
194 /* A function that implements a task must not exit or attempt to return to
\r
195 its caller as there is nothing to return to. If a task wants to exit it
\r
196 should instead call vTaskDelete( NULL ).
\r
198 Artificially force an assert() to be triggered if configASSERT() is
\r
199 defined, then stop here so application writers can catch the error. */
\r
200 configASSERT( uxSavedTaskStackPointer == 0UL );
\r
201 portDISABLE_INTERRUPTS();
\r
204 /*-----------------------------------------------------------*/
\r
207 * Setup a timer for a regular tick. This function uses peripheral timer 1.
\r
208 * The function is declared weak so an application writer can use a different
\r
209 * timer by redefining this implementation. If a different timer is used then
\r
210 * configTICK_INTERRUPT_VECTOR must also be defined in FreeRTOSConfig.h to
\r
211 * ensure the RTOS provided tick interrupt handler is installed on the correct
\r
212 * vector number. When Timer 1 is used the vector number is defined as
\r
215 __attribute__(( weak )) void vApplicationSetupTickTimerInterrupt( void )
\r
217 const uint32_t ulCompareMatch = ( (configPERIPHERAL_CLOCK_HZ / portTIMER_PRESCALE) / configTICK_RATE_HZ ) - 1;
\r
220 T1CONbits.TCKPS = portPRESCALE_BITS;
\r
221 PR1 = ulCompareMatch;
\r
222 IPC1bits.T1IP = configKERNEL_INTERRUPT_PRIORITY;
\r
224 /* Clear the interrupt as a starting condition. */
\r
227 /* Enable the interrupt. */
\r
230 /* Start the timer. */
\r
233 /*-----------------------------------------------------------*/
\r
235 void vPortEndScheduler(void)
\r
237 /* Not implemented in ports where there is nothing to return to.
\r
238 Artificially force an assert. */
\r
239 configASSERT( uxInterruptNesting == 1000UL );
\r
241 /*-----------------------------------------------------------*/
\r
243 BaseType_t xPortStartScheduler( void )
\r
245 extern void vPortStartFirstTask( void );
\r
246 extern void *pxCurrentTCB;
\r
248 #if ( configCHECK_FOR_STACK_OVERFLOW > 2 )
\r
250 /* Fill the ISR stack to make it easy to asses how much is being used. */
\r
251 memset( ( void * ) xISRStack, portISR_STACK_FILL_BYTE, sizeof( xISRStack ) );
\r
253 #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */
\r
255 /* Clear the software interrupt flag. */
\r
256 IFS0CLR = _IFS0_CS0IF_MASK;
\r
258 /* Set software timer priority. */
\r
259 IPC0CLR = _IPC0_CS0IP_MASK;
\r
260 IPC0SET = ( configKERNEL_INTERRUPT_PRIORITY << _IPC0_CS0IP_POSITION );
\r
262 /* Enable software interrupt. */
\r
263 IEC0CLR = _IEC0_CS0IE_MASK;
\r
264 IEC0SET = 1 << _IEC0_CS0IE_POSITION;
\r
266 /* Setup the timer to generate the tick. Interrupts will have been
\r
267 disabled by the time we get here. */
\r
268 vApplicationSetupTickTimerInterrupt();
\r
270 /* Kick off the highest priority task that has been created so far.
\r
271 Its stack location is loaded into uxSavedTaskStackPointer. */
\r
272 uxSavedTaskStackPointer = *( UBaseType_t * ) pxCurrentTCB;
\r
273 vPortStartFirstTask();
\r
275 /* Should never get here as the tasks will now be executing! Call the task
\r
276 exit error function to prevent compiler warnings about a static function
\r
277 not being called in the case that the application writer overrides this
\r
278 functionality by defining configTASK_RETURN_ADDRESS. */
\r
279 prvTaskExitError();
\r
283 /*-----------------------------------------------------------*/
\r
285 void vPortIncrementTick( void )
\r
287 UBaseType_t uxSavedStatus;
\r
289 uxSavedStatus = uxPortSetInterruptMaskFromISR();
\r
291 if( xTaskIncrementTick() != pdFALSE )
\r
293 /* Pend a context switch. */
\r
294 _CP0_BIS_CAUSE( portCORE_SW_0 );
\r
297 vPortClearInterruptMaskFromISR( uxSavedStatus );
\r
299 /* Look for the ISR stack getting near or past its limit. */
\r
300 portCHECK_ISR_STACK();
\r
302 /* Clear timer interrupt. */
\r
303 configCLEAR_TICK_TIMER_INTERRUPT();
\r
305 /*-----------------------------------------------------------*/
\r
307 UBaseType_t uxPortSetInterruptMaskFromISR( void )
\r
309 UBaseType_t uxSavedStatusRegister;
\r
311 __builtin_disable_interrupts();
\r
312 uxSavedStatusRegister = _CP0_GET_STATUS() | 0x01;
\r
313 /* This clears the IPL bits, then sets them to
\r
314 configMAX_SYSCALL_INTERRUPT_PRIORITY. This function should not be called
\r
315 from an interrupt that has a priority above
\r
316 configMAX_SYSCALL_INTERRUPT_PRIORITY so, when used correctly, the action
\r
317 can only result in the IPL being unchanged or raised, and therefore never
\r
319 _CP0_SET_STATUS( ( ( uxSavedStatusRegister & ( ~portALL_IPL_BITS ) ) ) | ( configMAX_SYSCALL_INTERRUPT_PRIORITY << portIPL_SHIFT ) );
\r
321 return uxSavedStatusRegister;
\r
323 /*-----------------------------------------------------------*/
\r
325 void vPortClearInterruptMaskFromISR( UBaseType_t uxSavedStatusRegister )
\r
327 _CP0_SET_STATUS( uxSavedStatusRegister );
\r
329 /*-----------------------------------------------------------*/
\r