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