]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MPLAB/PIC32MZ/port.c
Multiple tidy up, documentation corrections and typo corrections highlighted by Tamas...
[freertos] / FreeRTOS / Source / portable / MPLAB / PIC32MZ / port.c
1 /*\r
2     FreeRTOS V7.6.0 - Copyright (C) 2013 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     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! kernel.\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /*-----------------------------------------------------------\r
67  * Implementation of functions defined in portable.h for the PIC32MZ port.\r
68   *----------------------------------------------------------*/\r
69 \r
70 #include <xc.h>\r
71 \r
72 /* Scheduler include files. */\r
73 #include "FreeRTOS.h"\r
74 #include "task.h"\r
75 \r
76 #if !defined(__PIC32MZ__)\r
77     #error This port is designed to work with XC32 on PIC32MZ MCUs.  Please update your C compiler version or settings.\r
78 #endif\r
79 \r
80 #if( ( configMAX_SYSCALL_INTERRUPT_PRIORITY >= 0x7 ) || ( configMAX_SYSCALL_INTERRUPT_PRIORITY == 0 ) )\r
81         #error configMAX_SYSCALL_INTERRUPT_PRIORITY must be less than 7 and greater than 0\r
82 #endif\r
83 \r
84 /* Hardware specifics. */\r
85 #define portTIMER_PRESCALE      8\r
86 #define portPRESCALE_BITS       1\r
87 \r
88 /* Bits within various registers. */\r
89 #define portIE_BIT                                      ( 0x00000001 )\r
90 #define portEXL_BIT                                     ( 0x00000002 )\r
91 #define portMX_BIT                                      ( 0x01000000 ) /* Allow access to DSP instructions. */\r
92 \r
93 /* Bits within the CAUSE register. */\r
94 #define portCORE_SW_0                           ( 0x00000100 )\r
95 #define portCORE_SW_1                           ( 0x00000200 )\r
96 \r
97 /* The EXL bit is set to ensure interrupts do not occur while the context of\r
98 the first task is being restored. */\r
99 #define portINITIAL_SR                          ( portIE_BIT | portEXL_BIT | portMX_BIT )\r
100 \r
101 /*\r
102 By default port.c generates its tick interrupt from TIMER1.  The user can\r
103 override this behaviour by:\r
104         1: Providing their own implementation of vApplicationSetupTickTimerInterrupt(),\r
105            which is the function that configures the timer.  The function is defined\r
106            as a weak symbol in this file so if the same function name is used in the\r
107            application code then the version in the application code will be linked\r
108            into the application in preference to the version defined in this file.\r
109         2: Define configTICK_INTERRUPT_VECTOR to the vector number of the timer used\r
110            to generate the tick interrupt.  For example, when timer 1 is used then\r
111            configTICK_INTERRUPT_VECTOR is set to _TIMER_1_VECTOR.\r
112            configTICK_INTERRUPT_VECTOR should be defined in FreeRTOSConfig.h.\r
113         3: Define configCLEAR_TICK_TIMER_INTERRUPT() to clear the interrupt in the\r
114            timer used to generate the tick interrupt.  For example, when timer 1 is\r
115            used configCLEAR_TICK_TIMER_INTERRUPT() is defined to\r
116            IFS0CLR = _IFS0_T1IF_MASK.\r
117 */\r
118 #ifndef configTICK_INTERRUPT_VECTOR\r
119         #define configTICK_INTERRUPT_VECTOR _TIMER_1_VECTOR\r
120         #define configCLEAR_TICK_TIMER_INTERRUPT() IFS0CLR = _IFS0_T1IF_MASK\r
121 #else\r
122         #ifndef configCLEAR_TICK_TIMER_INTERRUPT\r
123                 #error If configTICK_INTERRUPT_VECTOR is defined in application code then configCLEAR_TICK_TIMER_INTERRUPT must also be defined in application code.\r
124         #endif\r
125 #endif\r
126 \r
127 /* Let the user override the pre-loading of the initial RA with the address of\r
128 prvTaskExitError() in case is messes up unwinding of the stack in the\r
129 debugger - in which case configTASK_RETURN_ADDRESS can be defined as 0 (NULL). */\r
130 #ifdef configTASK_RETURN_ADDRESS\r
131         #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS\r
132 #else\r
133         #define portTASK_RETURN_ADDRESS prvTaskExitError\r
134 #endif\r
135 \r
136 /* Set configCHECK_FOR_STACK_OVERFLOW to 3 to add ISR stack checking to task\r
137 stack checking.  A problem in the ISR stack will trigger an assert, not call the\r
138 stack overflow hook function (because the stack overflow hook is specific to a\r
139 task stack, not the ISR stack). */\r
140 #if( configCHECK_FOR_STACK_OVERFLOW > 2 )\r
141 \r
142         /* Don't use 0xa5 as the stack fill bytes as that is used by the kernerl for\r
143         the task stacks, and so will legitimately appear in many positions within\r
144         the ISR stack. */\r
145         #define portISR_STACK_FILL_BYTE 0xee\r
146 \r
147         static const unsigned char ucExpectedStackBytes[] = {\r
148                                                                         portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE,             \\r
149                                                                         portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE,             \\r
150                                                                         portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE,             \\r
151                                                                         portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE,             \\r
152                                                                         portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE };   \\r
153 \r
154         #define portCHECK_ISR_STACK() configASSERT( ( memcmp( ( void * ) xISRStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) == 0 ) )\r
155 #else\r
156         /* Define the function away. */\r
157         #define portCHECK_ISR_STACK()\r
158 #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */\r
159 \r
160 /*-----------------------------------------------------------*/\r
161 \r
162 /*\r
163  * Place the prototype here to ensure the interrupt vector is correctly installed.\r
164  * Note that because the interrupt is written in assembly, the IPL setting in the\r
165  * following line of code has no effect.  The interrupt priority is set by the\r
166  * call to ConfigIntTimer1() in vApplicationSetupTickTimerInterrupt().\r
167  */\r
168 extern void __attribute__( (interrupt(ipl1), vector( configTICK_INTERRUPT_VECTOR ))) vPortTickInterruptHandler( void );\r
169 \r
170 /*\r
171  * The software interrupt handler that performs the yield.  Note that, because\r
172  * the interrupt is written in assembly, the IPL setting in the following line of\r
173  * code has no effect.  The interrupt priority is set by the call to\r
174  * mConfigIntCoreSW0() in xPortStartScheduler().\r
175  */\r
176 void __attribute__( (interrupt(ipl1), vector(_CORE_SOFTWARE_0_VECTOR))) vPortYieldISR( void );\r
177 \r
178 /*\r
179  * Used to catch tasks that attempt to return from their implementing function.\r
180  */\r
181 static void prvTaskExitError( void );\r
182 \r
183 /*-----------------------------------------------------------*/\r
184 \r
185 /* Records the interrupt nesting depth.  This is initialised to one as it is\r
186 decremented to 0 when the first task starts. */\r
187 volatile unsigned portBASE_TYPE uxInterruptNesting = 0x01;\r
188 \r
189 /* Stores the task stack pointer when a switch is made to use the system stack. */\r
190 unsigned portBASE_TYPE uxSavedTaskStackPointer = 0;\r
191 \r
192 /* The stack used by interrupt service routines that cause a context switch. */\r
193 portSTACK_TYPE xISRStack[ configISR_STACK_SIZE ] = { 0 };\r
194 \r
195 /* The top of stack value ensures there is enough space to store 6 registers on\r
196 the callers stack, as some functions seem to want to do this. */\r
197 const portSTACK_TYPE * const xISRStackTop = &( xISRStack[ configISR_STACK_SIZE - 7 ] );\r
198 \r
199 /*-----------------------------------------------------------*/\r
200 \r
201 /*\r
202  * See header file for description.\r
203  */\r
204 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
205 {\r
206         /* Ensure byte alignment is maintained when leaving this function. */\r
207         pxTopOfStack--;\r
208 \r
209         *pxTopOfStack = (portSTACK_TYPE) 0xDEADBEEF;\r
210         pxTopOfStack--;\r
211 \r
212         *pxTopOfStack = (portSTACK_TYPE) 0x12345678;    /* Word to which the stack pointer will be left pointing after context restore. */\r
213         pxTopOfStack--;\r
214 \r
215         *pxTopOfStack = (portSTACK_TYPE) _CP0_GET_CAUSE();\r
216         pxTopOfStack--;\r
217 \r
218         *pxTopOfStack = (portSTACK_TYPE) portINITIAL_SR;/* CP0_STATUS */\r
219         pxTopOfStack--;\r
220 \r
221         *pxTopOfStack = (portSTACK_TYPE) pxCode;                /* CP0_EPC */\r
222         pxTopOfStack -= 7;                                                      /* Includes space for AC1 - AC3. */\r
223 \r
224         *pxTopOfStack = (portSTACK_TYPE) 0x00000000;    /* DSPControl */\r
225         pxTopOfStack--;\r
226 \r
227         *pxTopOfStack = (portSTACK_TYPE) portTASK_RETURN_ADDRESS;       /* ra */\r
228         pxTopOfStack -= 15;\r
229 \r
230         *pxTopOfStack = (portSTACK_TYPE) pvParameters; /* Parameters to pass in. */\r
231         pxTopOfStack -= 15;\r
232 \r
233         return pxTopOfStack;\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 static void prvTaskExitError( void )\r
238 {\r
239         /* A function that implements a task must not exit or attempt to return to\r
240         its caller as there is nothing to return to.  If a task wants to exit it\r
241         should instead call vTaskDelete( NULL ).\r
242 \r
243         Artificially force an assert() to be triggered if configASSERT() is\r
244         defined, then stop here so application writers can catch the error. */\r
245         configASSERT( uxSavedTaskStackPointer == 0UL );\r
246         portDISABLE_INTERRUPTS();\r
247         for( ;; );\r
248 }\r
249 /*-----------------------------------------------------------*/\r
250 \r
251 /*\r
252  * Setup a timer for a regular tick.  This function uses peripheral timer 1.\r
253  * The function is declared weak so an application writer can use a different\r
254  * timer by redefining this implementation.  If a different timer is used then\r
255  * configTICK_INTERRUPT_VECTOR must also be defined in FreeRTOSConfig.h to\r
256  * ensure the RTOS provided tick interrupt handler is installed on the correct\r
257  * vector number.  When Timer 1 is used the vector number is defined as\r
258  * _TIMER_1_VECTOR.\r
259  */\r
260 __attribute__(( weak )) void vApplicationSetupTickTimerInterrupt( void )\r
261 {\r
262 const unsigned long ulCompareMatch = ( (configPERIPHERAL_CLOCK_HZ / portTIMER_PRESCALE) / configTICK_RATE_HZ ) - 1;\r
263 \r
264         T1CON = 0x0000;\r
265         T1CONbits.TCKPS = portPRESCALE_BITS;\r
266         PR1 = ulCompareMatch;\r
267         IPC1bits.T1IP = configKERNEL_INTERRUPT_PRIORITY;\r
268 \r
269         /* Clear the interrupt as a starting condition. */\r
270         IFS0bits.T1IF = 0;\r
271 \r
272         /* Enable the interrupt. */\r
273         IEC0bits.T1IE = 1;\r
274 \r
275         /* Start the timer. */\r
276         T1CONbits.TON = 1;\r
277 }\r
278 /*-----------------------------------------------------------*/\r
279 \r
280 void vPortEndScheduler(void)\r
281 {\r
282         /* Not implemented in ports where there is nothing to return to.\r
283         Artificially force an assert. */\r
284         configASSERT( uxInterruptNesting == 1000UL );\r
285 }\r
286 /*-----------------------------------------------------------*/\r
287 \r
288 portBASE_TYPE xPortStartScheduler( void )\r
289 {\r
290 extern void vPortStartFirstTask( void );\r
291 extern void *pxCurrentTCB;\r
292 \r
293         #if ( configCHECK_FOR_STACK_OVERFLOW > 2 )\r
294         {\r
295                 /* Fill the ISR stack to make it easy to asses how much is being used. */\r
296                 memset( ( void * ) xISRStack, portISR_STACK_FILL_BYTE, sizeof( xISRStack ) );\r
297         }\r
298         #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */\r
299 \r
300         /* Clear the software interrupt flag. */\r
301         IFS0CLR = _IFS0_CS0IF_MASK;\r
302 \r
303         /* Set software timer priority. */\r
304         IPC0CLR = _IPC0_CS0IP_MASK;\r
305         IPC0SET = ( configKERNEL_INTERRUPT_PRIORITY << _IPC0_CS0IP_POSITION );\r
306 \r
307         /* Enable software interrupt. */\r
308         IEC0CLR = _IEC0_CS0IE_MASK;\r
309         IEC0SET = 1 << _IEC0_CS0IE_POSITION;\r
310 \r
311         /* Setup the timer to generate the tick.  Interrupts will have been\r
312         disabled by the time we get here. */\r
313         vApplicationSetupTickTimerInterrupt();\r
314 \r
315         /* Kick off the highest priority task that has been created so far.\r
316         Its stack location is loaded into uxSavedTaskStackPointer. */\r
317         uxSavedTaskStackPointer = *( unsigned portBASE_TYPE * ) pxCurrentTCB;\r
318         vPortStartFirstTask();\r
319 \r
320         /* Should never get here as the tasks will now be executing!  Call the task\r
321         exit error function to prevent compiler warnings about a static function\r
322         not being called in the case that the application writer overrides this\r
323         functionality by defining configTASK_RETURN_ADDRESS. */\r
324         prvTaskExitError();\r
325 \r
326         return pdFALSE;\r
327 }\r
328 /*-----------------------------------------------------------*/\r
329 \r
330 void vPortIncrementTick( void )\r
331 {\r
332 unsigned portBASE_TYPE uxSavedStatus;\r
333 \r
334         uxSavedStatus = uxPortSetInterruptMaskFromISR();\r
335         {\r
336                 if( xTaskIncrementTick() != pdFALSE )\r
337                 {\r
338                         /* Pend a context switch. */\r
339                         _CP0_BIS_CAUSE( portCORE_SW_0 );\r
340                 }\r
341         }\r
342         vPortClearInterruptMaskFromISR( uxSavedStatus );\r
343 \r
344         /* Look for the ISR stack getting near or past its limit. */\r
345         portCHECK_ISR_STACK();\r
346 \r
347         /* Clear timer interrupt. */\r
348         configCLEAR_TICK_TIMER_INTERRUPT();\r
349 }\r
350 /*-----------------------------------------------------------*/\r
351 \r
352 unsigned portBASE_TYPE uxPortSetInterruptMaskFromISR( void )\r
353 {\r
354 unsigned portBASE_TYPE uxSavedStatusRegister;\r
355 \r
356         __builtin_disable_interrupts();\r
357         uxSavedStatusRegister = _CP0_GET_STATUS() | 0x01;\r
358         /* This clears the IPL bits, then sets them to\r
359         configMAX_SYSCALL_INTERRUPT_PRIORITY.  This function should not be called\r
360         from an interrupt that has a priority above\r
361         configMAX_SYSCALL_INTERRUPT_PRIORITY so, when used correctly, the action\r
362         can only result in the IPL being unchanged or raised, and therefore never\r
363         lowered. */\r
364         _CP0_SET_STATUS( ( ( uxSavedStatusRegister & ( ~portALL_IPL_BITS ) ) ) | ( configMAX_SYSCALL_INTERRUPT_PRIORITY << portIPL_SHIFT ) );\r
365 \r
366         return uxSavedStatusRegister;\r
367 }\r
368 /*-----------------------------------------------------------*/\r
369 \r
370 void vPortClearInterruptMaskFromISR( unsigned portBASE_TYPE uxSavedStatusRegister )\r
371 {\r
372         _CP0_SET_STATUS( uxSavedStatusRegister );\r
373 }\r
374 /*-----------------------------------------------------------*/\r
375 \r
376 \r
377 \r
378 \r
379 \r