2 * FreeRTOS Kernel V10.4.4
\r
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * SPDX-License-Identifier: MIT
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
8 * this software and associated documentation files (the "Software"), to deal in
\r
9 * the Software without restriction, including without limitation the rights to
\r
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
11 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
12 * subject to the following conditions:
\r
14 * The above copyright notice and this permission notice shall be included in all
\r
15 * copies or substantial portions of the Software.
\r
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
24 * https://www.FreeRTOS.org
\r
25 * https://github.com/FreeRTOS
\r
29 /*-----------------------------------------------------------
\r
30 * Implementation of functions defined in portable.h for the PIC32MX port.
\r
31 *----------------------------------------------------------*/
\r
34 #error This port is designed to work with XC32. Please update your C compiler version.
\r
37 /* Scheduler include files. */
\r
38 #include "FreeRTOS.h"
\r
41 /* Hardware specifics. */
\r
42 #define portTIMER_PRESCALE 8
\r
43 #define portPRESCALE_BITS 1
\r
45 /* Bits within various registers. */
\r
46 #define portIE_BIT ( 0x00000001 )
\r
47 #define portEXL_BIT ( 0x00000002 )
\r
49 /* Bits within the CAUSE register. */
\r
50 #define portCORE_SW_0 ( 0x00000100 )
\r
51 #define portCORE_SW_1 ( 0x00000200 )
\r
53 /* The EXL bit is set to ensure interrupts do not occur while the context of
\r
54 the first task is being restored. */
\r
55 #define portINITIAL_SR ( portIE_BIT | portEXL_BIT )
\r
58 By default port.c generates its tick interrupt from TIMER1. The user can
\r
59 override this behaviour by:
\r
60 1: Providing their own implementation of vApplicationSetupTickTimerInterrupt(),
\r
61 which is the function that configures the timer. The function is defined
\r
62 as a weak symbol in this file so if the same function name is used in the
\r
63 application code then the version in the application code will be linked
\r
64 into the application in preference to the version defined in this file.
\r
65 2: Define configTICK_INTERRUPT_VECTOR to the vector number of the timer used
\r
66 to generate the tick interrupt. For example, when timer 1 is used then
\r
67 configTICK_INTERRUPT_VECTOR is set to _TIMER_1_VECTOR.
\r
68 configTICK_INTERRUPT_VECTOR should be defined in FreeRTOSConfig.h.
\r
69 3: Define configCLEAR_TICK_TIMER_INTERRUPT() to clear the interrupt in the
\r
70 timer used to generate the tick interrupt. For example, when timer 1 is
\r
71 used configCLEAR_TICK_TIMER_INTERRUPT() is defined to
\r
72 IFS0CLR = _IFS0_T1IF_MASK.
\r
74 #ifndef configTICK_INTERRUPT_VECTOR
\r
75 #define configTICK_INTERRUPT_VECTOR _TIMER_1_VECTOR
\r
76 #define configCLEAR_TICK_TIMER_INTERRUPT() IFS0CLR = _IFS0_T1IF_MASK
\r
78 #ifndef configCLEAR_TICK_TIMER_INTERRUPT
\r
79 #error If configTICK_INTERRUPT_VECTOR is defined in application code then configCLEAR_TICK_TIMER_INTERRUPT must also be defined in application code.
\r
83 /* Let the user override the pre-loading of the initial RA with the address of
\r
84 prvTaskExitError() in case it messes up unwinding of the stack in the
\r
85 debugger - in which case configTASK_RETURN_ADDRESS can be defined as 0 (NULL). */
\r
86 #ifdef configTASK_RETURN_ADDRESS
\r
87 #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS
\r
89 #define portTASK_RETURN_ADDRESS prvTaskExitError
\r
92 /* Set configCHECK_FOR_STACK_OVERFLOW to 3 to add ISR stack checking to task
\r
93 stack checking. A problem in the ISR stack will trigger an assert, not call the
\r
94 stack overflow hook function (because the stack overflow hook is specific to a
\r
95 task stack, not the ISR stack). */
\r
96 #if( configCHECK_FOR_STACK_OVERFLOW > 2 )
\r
98 /* Don't use 0xa5 as the stack fill bytes as that is used by the kernerl for
\r
99 the task stacks, and so will legitimately appear in many positions within
\r
101 #define portISR_STACK_FILL_BYTE 0xee
\r
103 static const uint8_t ucExpectedStackBytes[] = {
\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
108 portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE }; \
\r
110 #define portCHECK_ISR_STACK() configASSERT( ( memcmp( ( void * ) xISRStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) == 0 ) )
\r
112 /* Define the function away. */
\r
113 #define portCHECK_ISR_STACK()
\r
114 #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */
\r
116 /*-----------------------------------------------------------*/
\r
120 * Place the prototype here to ensure the interrupt vector is correctly installed.
\r
121 * Note that because the interrupt is written in assembly, the IPL setting in the
\r
122 * following line of code has no effect. The interrupt priority is set by the
\r
123 * call to ConfigIntTimer1() in vApplicationSetupTickTimerInterrupt().
\r
125 extern void __attribute__( (interrupt(IPL1AUTO), vector( configTICK_INTERRUPT_VECTOR ))) vPortTickInterruptHandler( void );
\r
128 * The software interrupt handler that performs the yield. Note that, because
\r
129 * the interrupt is written in assembly, the IPL setting in the following line of
\r
130 * code has no effect. The interrupt priority is set by the call to
\r
131 * mConfigIntCoreSW0() in xPortStartScheduler().
\r
133 void __attribute__( (interrupt(IPL1AUTO), vector(_CORE_SOFTWARE_0_VECTOR))) vPortYieldISR( void );
\r
136 * Used to catch tasks that attempt to return from their implementing function.
\r
138 static void prvTaskExitError( void );
\r
140 /*-----------------------------------------------------------*/
\r
142 /* Records the interrupt nesting depth. This is initialised to one as it is
\r
143 decremented to 0 when the first task starts. */
\r
144 volatile UBaseType_t uxInterruptNesting = 0x01;
\r
146 /* Stores the task stack pointer when a switch is made to use the system stack. */
\r
147 UBaseType_t uxSavedTaskStackPointer = 0;
\r
149 /* The stack used by interrupt service routines that cause a context switch. */
\r
150 __attribute__ ((aligned(8))) StackType_t xISRStack[ configISR_STACK_SIZE ] = { 0 };
\r
152 /* The top of stack value ensures there is enough space to store 6 registers on
\r
153 the callers stack, as some functions seem to want to do this. */
\r
154 const StackType_t * const xISRStackTop = &( xISRStack[ ( configISR_STACK_SIZE & ~portBYTE_ALIGNMENT_MASK ) - 8 ] );
\r
156 /*-----------------------------------------------------------*/
\r
159 * See header file for description.
\r
161 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
163 /* Ensure 8 byte alignment is maintained when the context is popped from
\r
164 * stack. The size of the context is 33 words (132 bytes). */
\r
168 *pxTopOfStack = (StackType_t) 0xDEADBEEF;
\r
171 *pxTopOfStack = (StackType_t) 0x12345678; /* Word to which the stack pointer will be left pointing after context restore. */
\r
174 *pxTopOfStack = (StackType_t) _CP0_GET_CAUSE();
\r
177 *pxTopOfStack = (StackType_t) portINITIAL_SR;/* CP0_STATUS */
\r
180 *pxTopOfStack = (StackType_t) pxCode; /* CP0_EPC */
\r
183 *pxTopOfStack = (StackType_t) portTASK_RETURN_ADDRESS; /* ra */
\r
184 pxTopOfStack -= 15;
\r
186 *pxTopOfStack = (StackType_t) pvParameters; /* Parameters to pass in. */
\r
187 pxTopOfStack -= 15;
\r
189 return pxTopOfStack;
\r
191 /*-----------------------------------------------------------*/
\r
193 static void prvTaskExitError( void )
\r
195 /* A function that implements a task must not exit or attempt to return to
\r
196 its caller as there is nothing to return to. If a task wants to exit it
\r
197 should instead call vTaskDelete( NULL ).
\r
199 Artificially force an assert() to be triggered if configASSERT() is
\r
200 defined, then stop here so application writers can catch the error. */
\r
201 configASSERT( uxSavedTaskStackPointer == 0UL );
\r
202 portDISABLE_INTERRUPTS();
\r
205 /*-----------------------------------------------------------*/
\r
208 * Setup a timer for a regular tick. This function uses peripheral timer 1.
\r
209 * The function is declared weak so an application writer can use a different
\r
210 * timer by redefining this implementation. If a different timer is used then
\r
211 * configTICK_INTERRUPT_VECTOR must also be defined in FreeRTOSConfig.h to
\r
212 * ensure the RTOS provided tick interrupt handler is installed on the correct
\r
213 * vector number. When Timer 1 is used the vector number is defined as
\r
216 __attribute__(( weak )) void vApplicationSetupTickTimerInterrupt( void )
\r
218 const uint32_t ulCompareMatch = ( (configPERIPHERAL_CLOCK_HZ / portTIMER_PRESCALE) / configTICK_RATE_HZ ) - 1;
\r
221 T1CONbits.TCKPS = portPRESCALE_BITS;
\r
222 PR1 = ulCompareMatch;
\r
223 IPC1bits.T1IP = configKERNEL_INTERRUPT_PRIORITY;
\r
225 /* Clear the interrupt as a starting condition. */
\r
228 /* Enable the interrupt. */
\r
231 /* Start the timer. */
\r
234 /*-----------------------------------------------------------*/
\r
236 void vPortEndScheduler(void)
\r
238 /* Not implemented in ports where there is nothing to return to.
\r
239 Artificially force an assert. */
\r
240 configASSERT( uxInterruptNesting == 1000UL );
\r
242 /*-----------------------------------------------------------*/
\r
244 BaseType_t xPortStartScheduler( void )
\r
246 extern void vPortStartFirstTask( void );
\r
247 extern void *pxCurrentTCB;
\r
249 #if ( configCHECK_FOR_STACK_OVERFLOW > 2 )
\r
251 /* Fill the ISR stack to make it easy to asses how much is being used. */
\r
252 memset( ( void * ) xISRStack, portISR_STACK_FILL_BYTE, sizeof( xISRStack ) );
\r
254 #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */
\r
256 /* Clear the software interrupt flag. */
\r
257 IFS0CLR = _IFS0_CS0IF_MASK;
\r
259 /* Set software timer priority. */
\r
260 IPC0CLR = _IPC0_CS0IP_MASK;
\r
261 IPC0SET = ( configKERNEL_INTERRUPT_PRIORITY << _IPC0_CS0IP_POSITION );
\r
263 /* Enable software interrupt. */
\r
264 IEC0CLR = _IEC0_CS0IE_MASK;
\r
265 IEC0SET = 1 << _IEC0_CS0IE_POSITION;
\r
267 /* Setup the timer to generate the tick. Interrupts will have been
\r
268 disabled by the time we get here. */
\r
269 vApplicationSetupTickTimerInterrupt();
\r
271 /* Kick off the highest priority task that has been created so far.
\r
272 Its stack location is loaded into uxSavedTaskStackPointer. */
\r
273 uxSavedTaskStackPointer = *( UBaseType_t * ) pxCurrentTCB;
\r
274 vPortStartFirstTask();
\r
276 /* Should never get here as the tasks will now be executing! Call the task
\r
277 exit error function to prevent compiler warnings about a static function
\r
278 not being called in the case that the application writer overrides this
\r
279 functionality by defining configTASK_RETURN_ADDRESS. */
\r
280 prvTaskExitError();
\r
284 /*-----------------------------------------------------------*/
\r
286 void vPortIncrementTick( void )
\r
288 UBaseType_t uxSavedStatus;
\r
290 uxSavedStatus = uxPortSetInterruptMaskFromISR();
\r
292 if( xTaskIncrementTick() != pdFALSE )
\r
294 /* Pend a context switch. */
\r
295 _CP0_BIS_CAUSE( portCORE_SW_0 );
\r
298 vPortClearInterruptMaskFromISR( uxSavedStatus );
\r
300 /* Look for the ISR stack getting near or past its limit. */
\r
301 portCHECK_ISR_STACK();
\r
303 /* Clear timer interrupt. */
\r
304 configCLEAR_TICK_TIMER_INTERRUPT();
\r
306 /*-----------------------------------------------------------*/
\r
308 UBaseType_t uxPortSetInterruptMaskFromISR( void )
\r
310 UBaseType_t uxSavedStatusRegister;
\r
312 __builtin_disable_interrupts();
\r
313 uxSavedStatusRegister = _CP0_GET_STATUS() | 0x01;
\r
314 /* This clears the IPL bits, then sets them to
\r
315 configMAX_SYSCALL_INTERRUPT_PRIORITY. This function should not be called
\r
316 from an interrupt that has a priority above
\r
317 configMAX_SYSCALL_INTERRUPT_PRIORITY so, when used correctly, the action
\r
318 can only result in the IPL being unchanged or raised, and therefore never
\r
320 _CP0_SET_STATUS( ( ( uxSavedStatusRegister & ( ~portALL_IPL_BITS ) ) ) | ( configMAX_SYSCALL_INTERRUPT_PRIORITY << portIPL_SHIFT ) );
\r
322 return uxSavedStatusRegister;
\r
324 /*-----------------------------------------------------------*/
\r
326 void vPortClearInterruptMaskFromISR( UBaseType_t uxSavedStatusRegister )
\r
328 _CP0_SET_STATUS( uxSavedStatusRegister );
\r
330 /*-----------------------------------------------------------*/
\r