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