]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MPLAB/PIC32MZ/port.c
Ensure the PIC32 interrupt stack is 8 byte aligned for all values of configISR_STACK_...
[freertos] / FreeRTOS / Source / portable / MPLAB / PIC32MZ / port.c
1 /*\r
2     FreeRTOS V9.0.1 - Copyright (C) 2017 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \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
12 \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
19 \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
24 \r
25     ***************************************************************************\r
26      *                                                                       *\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
31      *                                                                       *\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
36      *                                                                       *\r
37     ***************************************************************************\r
38 \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
42 \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
46 \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
51 \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
55 \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
58 \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
62 \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
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /*-----------------------------------------------------------\r
71  * Implementation of functions defined in portable.h for the PIC32MZ port.\r
72   *----------------------------------------------------------*/\r
73 \r
74 /* Microchip specific headers. */\r
75 #include <xc.h>\r
76 \r
77 /* Standard headers. */\r
78 #include <string.h>\r
79 \r
80 /* Scheduler include files. */\r
81 #include "FreeRTOS.h"\r
82 #include "task.h"\r
83 \r
84 #if !defined(__PIC32MZ__)\r
85     #error This port is designed to work with XC32 on PIC32MZ MCUs.  Please update your C compiler version or settings.\r
86 #endif\r
87 \r
88 #if( ( configMAX_SYSCALL_INTERRUPT_PRIORITY >= 0x7 ) || ( configMAX_SYSCALL_INTERRUPT_PRIORITY == 0 ) )\r
89         #error configMAX_SYSCALL_INTERRUPT_PRIORITY must be less than 7 and greater than 0\r
90 #endif\r
91 \r
92 /* Hardware specifics. */\r
93 #define portTIMER_PRESCALE      8\r
94 #define portPRESCALE_BITS       1\r
95 \r
96 /* Bits within various registers. */\r
97 #define portIE_BIT                                      ( 0x00000001 )\r
98 #define portEXL_BIT                                     ( 0x00000002 )\r
99 #define portMX_BIT                                      ( 0x01000000 ) /* Allow access to DSP instructions. */\r
100 #define portCU1_BIT                                     ( 0x20000000 ) /* enable CP1 for parts with hardware. */\r
101 #define portFR_BIT                                      ( 0x04000000 ) /* Enable 64 bit floating point registers. */\r
102 \r
103 /* Bits within the CAUSE register. */\r
104 #define portCORE_SW_0                           ( 0x00000100 )\r
105 #define portCORE_SW_1                           ( 0x00000200 )\r
106 \r
107 /* The EXL bit is set to ensure interrupts do not occur while the context of\r
108 the first task is being restored. */\r
109 #if ( __mips_hard_float == 1 )\r
110     #define portINITIAL_SR                      ( portIE_BIT | portEXL_BIT | portMX_BIT | portFR_BIT | portCU1_BIT )\r
111 #else\r
112     #define portINITIAL_SR                      ( portIE_BIT | portEXL_BIT | portMX_BIT )\r
113 #endif\r
114 \r
115 /* The initial value to store into the FPU status and control register. This is\r
116  only used on parts that support a hardware FPU. */\r
117 #define portINITIAL_FPSCR                       (0x1000000) /* High perf on denormal ops */\r
118 \r
119 \r
120 /*\r
121 By default port.c generates its tick interrupt from TIMER1.  The user can\r
122 override this behaviour by:\r
123         1: Providing their own implementation of vApplicationSetupTickTimerInterrupt(),\r
124            which is the function that configures the timer.  The function is defined\r
125            as a weak symbol in this file so if the same function name is used in the\r
126            application code then the version in the application code will be linked\r
127            into the application in preference to the version defined in this file.\r
128         2: Define configTICK_INTERRUPT_VECTOR to the vector number of the timer used\r
129            to generate the tick interrupt.  For example, when timer 1 is used then\r
130            configTICK_INTERRUPT_VECTOR is set to _TIMER_1_VECTOR.\r
131            configTICK_INTERRUPT_VECTOR should be defined in FreeRTOSConfig.h.\r
132         3: Define configCLEAR_TICK_TIMER_INTERRUPT() to clear the interrupt in the\r
133            timer used to generate the tick interrupt.  For example, when timer 1 is\r
134            used configCLEAR_TICK_TIMER_INTERRUPT() is defined to\r
135            IFS0CLR = _IFS0_T1IF_MASK.\r
136 */\r
137 #ifndef configTICK_INTERRUPT_VECTOR\r
138         #define configTICK_INTERRUPT_VECTOR _TIMER_1_VECTOR\r
139         #define configCLEAR_TICK_TIMER_INTERRUPT() IFS0CLR = _IFS0_T1IF_MASK\r
140 #else\r
141         #ifndef configCLEAR_TICK_TIMER_INTERRUPT\r
142                 #error If configTICK_INTERRUPT_VECTOR is defined in application code then configCLEAR_TICK_TIMER_INTERRUPT must also be defined in application code.\r
143         #endif\r
144 #endif\r
145 \r
146 /* Let the user override the pre-loading of the initial RA with the address of\r
147 prvTaskExitError() in case it messes up unwinding of the stack in the\r
148 debugger - in which case configTASK_RETURN_ADDRESS can be defined as 0 (NULL). */\r
149 #ifdef configTASK_RETURN_ADDRESS\r
150         #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS\r
151 #else\r
152         #define portTASK_RETURN_ADDRESS prvTaskExitError\r
153 #endif\r
154 \r
155 /* Set configCHECK_FOR_STACK_OVERFLOW to 3 to add ISR stack checking to task\r
156 stack checking.  A problem in the ISR stack will trigger an assert, not call the\r
157 stack overflow hook function (because the stack overflow hook is specific to a\r
158 task stack, not the ISR stack). */\r
159 #if( configCHECK_FOR_STACK_OVERFLOW > 2 )\r
160 \r
161         /* Don't use 0xa5 as the stack fill bytes as that is used by the kernerl for\r
162         the task stacks, and so will legitimately appear in many positions within\r
163         the ISR stack. */\r
164         #define portISR_STACK_FILL_BYTE 0xee\r
165 \r
166         static const uint8_t ucExpectedStackBytes[] = {\r
167                                                                         portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE,             \\r
168                                                                         portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE,             \\r
169                                                                         portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE,             \\r
170                                                                         portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE,             \\r
171                                                                         portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE };   \\r
172 \r
173         #define portCHECK_ISR_STACK() configASSERT( ( memcmp( ( void * ) xISRStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) == 0 ) )\r
174 #else\r
175         /* Define the function away. */\r
176         #define portCHECK_ISR_STACK()\r
177 #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */\r
178 \r
179 /*-----------------------------------------------------------*/\r
180 \r
181 /*\r
182  * Used to catch tasks that attempt to return from their implementing function.\r
183  */\r
184 static void prvTaskExitError( void );\r
185 \r
186 /*-----------------------------------------------------------*/\r
187 \r
188 /* Records the interrupt nesting depth.  This is initialised to one as it is\r
189 decremented to 0 when the first task starts. */\r
190 volatile UBaseType_t uxInterruptNesting = 0x01;\r
191 \r
192 /* Stores the task stack pointer when a switch is made to use the system stack. */\r
193 UBaseType_t uxSavedTaskStackPointer = 0;\r
194 \r
195 /* The stack used by interrupt service routines that cause a context switch. */\r
196 __attribute__ ((aligned(8))) StackType_t xISRStack[ configISR_STACK_SIZE ] = { 0 };\r
197 \r
198 /* The top of stack value ensures there is enough space to store 6 registers on\r
199 the callers stack, as some functions seem to want to do this.  8 byte alignment\r
200 is required to allow double word floating point stack pushes generated by the\r
201 compiler. */\r
202 const StackType_t * const xISRStackTop = &( xISRStack[ ( configISR_STACK_SIZE & ~portBYTE_ALIGNMENT_MASK ) - 8 ] );\r
203 \r
204 /* Saved as part of the task context. Set to pdFALSE if the task does not\r
205  require an FPU context. */\r
206 #if ( __mips_hard_float == 1 ) && ( configUSE_TASK_FPU_SUPPORT == 1 )\r
207         uint32_t ulTaskHasFPUContext = 0;\r
208 #endif\r
209 \r
210 /*-----------------------------------------------------------*/\r
211 \r
212 /*\r
213  * See header file for description.\r
214  */\r
215 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
216 {\r
217         /* Ensure 8 byte alignment is maintained when leaving this function. */\r
218         pxTopOfStack--;\r
219         pxTopOfStack--;\r
220 \r
221         *pxTopOfStack = (StackType_t) 0xDEADBEEF;\r
222         pxTopOfStack--;\r
223 \r
224         *pxTopOfStack = (StackType_t) 0x12345678;       /* Word to which the stack pointer will be left pointing after context restore. */\r
225         pxTopOfStack--;\r
226 \r
227         *pxTopOfStack = (StackType_t) _CP0_GET_CAUSE();\r
228         pxTopOfStack--;\r
229 \r
230         *pxTopOfStack = (StackType_t) portINITIAL_SR;/* CP0_STATUS */\r
231         pxTopOfStack--;\r
232 \r
233         *pxTopOfStack = (StackType_t) pxCode;           /* CP0_EPC */\r
234         pxTopOfStack--;\r
235 \r
236         *pxTopOfStack = (StackType_t) 0x00000000;       /* DSPControl */\r
237         pxTopOfStack -= 7;                                              /* Includes space for AC1 - AC3. */\r
238 \r
239         *pxTopOfStack = (StackType_t) portTASK_RETURN_ADDRESS;  /* ra */\r
240         pxTopOfStack -= 15;\r
241 \r
242         *pxTopOfStack = (StackType_t) pvParameters; /* Parameters to pass in. */\r
243         pxTopOfStack -= 15;\r
244 \r
245         *pxTopOfStack = (StackType_t) pdFALSE; /*by default disable FPU context save on parts with FPU */\r
246 \r
247         return pxTopOfStack;\r
248 }\r
249 /*-----------------------------------------------------------*/\r
250 \r
251 static void prvTaskExitError( void )\r
252 {\r
253         /* A function that implements a task must not exit or attempt to return to\r
254         its caller as there is nothing to return to.  If a task wants to exit it\r
255         should instead call vTaskDelete( NULL ).\r
256 \r
257         Artificially force an assert() to be triggered if configASSERT() is\r
258         defined, then stop here so application writers can catch the error. */\r
259         configASSERT( uxSavedTaskStackPointer == 0UL );\r
260         portDISABLE_INTERRUPTS();\r
261         for( ;; );\r
262 }\r
263 /*-----------------------------------------------------------*/\r
264 \r
265 /*\r
266  * Setup a timer for a regular tick.  This function uses peripheral timer 1.\r
267  * The function is declared weak so an application writer can use a different\r
268  * timer by redefining this implementation.  If a different timer is used then\r
269  * configTICK_INTERRUPT_VECTOR must also be defined in FreeRTOSConfig.h to\r
270  * ensure the RTOS provided tick interrupt handler is installed on the correct\r
271  * vector number.  When Timer 1 is used the vector number is defined as\r
272  * _TIMER_1_VECTOR.\r
273  */\r
274 __attribute__(( weak )) void vApplicationSetupTickTimerInterrupt( void )\r
275 {\r
276 const uint32_t ulCompareMatch = ( (configPERIPHERAL_CLOCK_HZ / portTIMER_PRESCALE) / configTICK_RATE_HZ ) - 1UL;\r
277 \r
278         T1CON = 0x0000;\r
279         T1CONbits.TCKPS = portPRESCALE_BITS;\r
280         PR1 = ulCompareMatch;\r
281         IPC1bits.T1IP = configKERNEL_INTERRUPT_PRIORITY;\r
282 \r
283         /* Clear the interrupt as a starting condition. */\r
284         IFS0bits.T1IF = 0;\r
285 \r
286         /* Enable the interrupt. */\r
287         IEC0bits.T1IE = 1;\r
288 \r
289         /* Start the timer. */\r
290         T1CONbits.TON = 1;\r
291 }\r
292 /*-----------------------------------------------------------*/\r
293 \r
294 void vPortEndScheduler(void)\r
295 {\r
296         /* Not implemented in ports where there is nothing to return to.\r
297         Artificially force an assert. */\r
298         configASSERT( uxInterruptNesting == 1000UL );\r
299 }\r
300 /*-----------------------------------------------------------*/\r
301 \r
302 BaseType_t xPortStartScheduler( void )\r
303 {\r
304 extern void vPortStartFirstTask( void );\r
305 extern void *pxCurrentTCB;\r
306 \r
307         #if ( configCHECK_FOR_STACK_OVERFLOW > 2 )\r
308         {\r
309                 /* Fill the ISR stack to make it easy to asses how much is being used. */\r
310                 memset( ( void * ) xISRStack, portISR_STACK_FILL_BYTE, sizeof( xISRStack ) );\r
311         }\r
312         #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */\r
313 \r
314         /* Clear the software interrupt flag. */\r
315         IFS0CLR = _IFS0_CS0IF_MASK;\r
316 \r
317         /* Set software timer priority. */\r
318         IPC0CLR = _IPC0_CS0IP_MASK;\r
319         IPC0SET = ( configKERNEL_INTERRUPT_PRIORITY << _IPC0_CS0IP_POSITION );\r
320 \r
321         /* Enable software interrupt. */\r
322         IEC0CLR = _IEC0_CS0IE_MASK;\r
323         IEC0SET = 1 << _IEC0_CS0IE_POSITION;\r
324 \r
325         /* Setup the timer to generate the tick.  Interrupts will have been\r
326         disabled by the time we get here. */\r
327         vApplicationSetupTickTimerInterrupt();\r
328 \r
329         /* Kick off the highest priority task that has been created so far.\r
330         Its stack location is loaded into uxSavedTaskStackPointer. */\r
331         uxSavedTaskStackPointer = *( UBaseType_t * ) pxCurrentTCB;\r
332         vPortStartFirstTask();\r
333 \r
334         /* Should never get here as the tasks will now be executing!  Call the task\r
335         exit error function to prevent compiler warnings about a static function\r
336         not being called in the case that the application writer overrides this\r
337         functionality by defining configTASK_RETURN_ADDRESS. */\r
338         prvTaskExitError();\r
339 \r
340         return pdFALSE;\r
341 }\r
342 /*-----------------------------------------------------------*/\r
343 \r
344 void vPortIncrementTick( void )\r
345 {\r
346 UBaseType_t uxSavedStatus;\r
347 \r
348         uxSavedStatus = uxPortSetInterruptMaskFromISR();\r
349         {\r
350                 if( xTaskIncrementTick() != pdFALSE )\r
351                 {\r
352                         /* Pend a context switch. */\r
353                         _CP0_BIS_CAUSE( portCORE_SW_0 );\r
354                 }\r
355         }\r
356         vPortClearInterruptMaskFromISR( uxSavedStatus );\r
357 \r
358         /* Look for the ISR stack getting near or past its limit. */\r
359         portCHECK_ISR_STACK();\r
360 \r
361         /* Clear timer interrupt. */\r
362         configCLEAR_TICK_TIMER_INTERRUPT();\r
363 }\r
364 /*-----------------------------------------------------------*/\r
365 \r
366 UBaseType_t uxPortSetInterruptMaskFromISR( void )\r
367 {\r
368 UBaseType_t uxSavedStatusRegister;\r
369 \r
370         __builtin_disable_interrupts();\r
371         uxSavedStatusRegister = _CP0_GET_STATUS() | 0x01;\r
372         /* This clears the IPL bits, then sets them to\r
373         configMAX_SYSCALL_INTERRUPT_PRIORITY.  This function should not be called\r
374         from an interrupt that has a priority above\r
375         configMAX_SYSCALL_INTERRUPT_PRIORITY so, when used correctly, the action\r
376         can only result in the IPL being unchanged or raised, and therefore never\r
377         lowered. */\r
378         _CP0_SET_STATUS( ( ( uxSavedStatusRegister & ( ~portALL_IPL_BITS ) ) ) | ( configMAX_SYSCALL_INTERRUPT_PRIORITY << portIPL_SHIFT ) );\r
379 \r
380         return uxSavedStatusRegister;\r
381 }\r
382 /*-----------------------------------------------------------*/\r
383 \r
384 void vPortClearInterruptMaskFromISR( UBaseType_t uxSavedStatusRegister )\r
385 {\r
386         _CP0_SET_STATUS( uxSavedStatusRegister );\r
387 }\r
388 /*-----------------------------------------------------------*/\r
389 \r
390 #if ( __mips_hard_float == 1 ) && ( configUSE_TASK_FPU_SUPPORT == 1 )\r
391 \r
392         void vPortTaskUsesFPU(void)\r
393         {\r
394         extern void vPortInitialiseFPSCR( uint32_t uxFPSCRInit );\r
395 \r
396                 portENTER_CRITICAL();\r
397 \r
398                 /* Initialise the floating point status register. */\r
399                 vPortInitialiseFPSCR(portINITIAL_FPSCR);\r
400 \r
401                 /* A task is registering the fact that it needs a FPU context. Set the\r
402                 FPU flag (saved as part of the task context). */\r
403                 ulTaskHasFPUContext = pdTRUE;\r
404 \r
405                 portEXIT_CRITICAL();\r
406         }\r
407 \r
408 #endif /* __mips_hard_float == 1 */\r
409 \r
410 /*-----------------------------------------------------------*/\r
411 \r
412 \r
413 \r
414 \r