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