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