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