]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MPLAB/PIC32MZ/port.c
Update version number to 8.1.1 for patch release that re-enables mutexes to be given...
[freertos] / FreeRTOS / Source / portable / MPLAB / PIC32MZ / port.c
1 /*\r
2     FreeRTOS V8.1.1 - 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 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 uint8_t 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  * Used to catch tasks that attempt to return from their implementing function.\r
164  */\r
165 static void prvTaskExitError( void );\r
166 \r
167 /*-----------------------------------------------------------*/\r
168 \r
169 /* Records the interrupt nesting depth.  This is initialised to one as it is\r
170 decremented to 0 when the first task starts. */\r
171 volatile UBaseType_t uxInterruptNesting = 0x01;\r
172 \r
173 /* Stores the task stack pointer when a switch is made to use the system stack. */\r
174 UBaseType_t uxSavedTaskStackPointer = 0;\r
175 \r
176 /* The stack used by interrupt service routines that cause a context switch. */\r
177 StackType_t xISRStack[ configISR_STACK_SIZE ] = { 0 };\r
178 \r
179 /* The top of stack value ensures there is enough space to store 6 registers on\r
180 the callers stack, as some functions seem to want to do this. */\r
181 const StackType_t * const xISRStackTop = &( xISRStack[ configISR_STACK_SIZE - 7 ] );\r
182 \r
183 /*-----------------------------------------------------------*/\r
184 \r
185 /*\r
186  * See header file for description.\r
187  */\r
188 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
189 {\r
190         /* Ensure byte alignment is maintained when leaving this function. */\r
191         pxTopOfStack--;\r
192 \r
193         *pxTopOfStack = (StackType_t) 0xDEADBEEF;\r
194         pxTopOfStack--;\r
195 \r
196         *pxTopOfStack = (StackType_t) 0x12345678;       /* Word to which the stack pointer will be left pointing after context restore. */\r
197         pxTopOfStack--;\r
198 \r
199         *pxTopOfStack = (StackType_t) _CP0_GET_CAUSE();\r
200         pxTopOfStack--;\r
201 \r
202         *pxTopOfStack = (StackType_t) portINITIAL_SR;/* CP0_STATUS */\r
203         pxTopOfStack--;\r
204 \r
205         *pxTopOfStack = (StackType_t) pxCode;           /* CP0_EPC */\r
206         pxTopOfStack -= 7;                                                      /* Includes space for AC1 - AC3. */\r
207 \r
208         *pxTopOfStack = (StackType_t) 0x00000000;       /* DSPControl */\r
209         pxTopOfStack--;\r
210 \r
211         *pxTopOfStack = (StackType_t) portTASK_RETURN_ADDRESS;  /* ra */\r
212         pxTopOfStack -= 15;\r
213 \r
214         *pxTopOfStack = (StackType_t) pvParameters; /* Parameters to pass in. */\r
215         pxTopOfStack -= 15;\r
216 \r
217         return pxTopOfStack;\r
218 }\r
219 /*-----------------------------------------------------------*/\r
220 \r
221 static void prvTaskExitError( void )\r
222 {\r
223         /* A function that implements a task must not exit or attempt to return to\r
224         its caller as there is nothing to return to.  If a task wants to exit it\r
225         should instead call vTaskDelete( NULL ).\r
226 \r
227         Artificially force an assert() to be triggered if configASSERT() is\r
228         defined, then stop here so application writers can catch the error. */\r
229         configASSERT( uxSavedTaskStackPointer == 0UL );\r
230         portDISABLE_INTERRUPTS();\r
231         for( ;; );\r
232 }\r
233 /*-----------------------------------------------------------*/\r
234 \r
235 /*\r
236  * Setup a timer for a regular tick.  This function uses peripheral timer 1.\r
237  * The function is declared weak so an application writer can use a different\r
238  * timer by redefining this implementation.  If a different timer is used then\r
239  * configTICK_INTERRUPT_VECTOR must also be defined in FreeRTOSConfig.h to\r
240  * ensure the RTOS provided tick interrupt handler is installed on the correct\r
241  * vector number.  When Timer 1 is used the vector number is defined as\r
242  * _TIMER_1_VECTOR.\r
243  */\r
244 __attribute__(( weak )) void vApplicationSetupTickTimerInterrupt( void )\r
245 {\r
246 const uint32_t ulCompareMatch = ( (configPERIPHERAL_CLOCK_HZ / portTIMER_PRESCALE) / configTICK_RATE_HZ ) - 1;\r
247 \r
248         T1CON = 0x0000;\r
249         T1CONbits.TCKPS = portPRESCALE_BITS;\r
250         PR1 = ulCompareMatch;\r
251         IPC1bits.T1IP = configKERNEL_INTERRUPT_PRIORITY;\r
252 \r
253         /* Clear the interrupt as a starting condition. */\r
254         IFS0bits.T1IF = 0;\r
255 \r
256         /* Enable the interrupt. */\r
257         IEC0bits.T1IE = 1;\r
258 \r
259         /* Start the timer. */\r
260         T1CONbits.TON = 1;\r
261 }\r
262 /*-----------------------------------------------------------*/\r
263 \r
264 void vPortEndScheduler(void)\r
265 {\r
266         /* Not implemented in ports where there is nothing to return to.\r
267         Artificially force an assert. */\r
268         configASSERT( uxInterruptNesting == 1000UL );\r
269 }\r
270 /*-----------------------------------------------------------*/\r
271 \r
272 BaseType_t xPortStartScheduler( void )\r
273 {\r
274 extern void vPortStartFirstTask( void );\r
275 extern void *pxCurrentTCB;\r
276 \r
277         #if ( configCHECK_FOR_STACK_OVERFLOW > 2 )\r
278         {\r
279                 /* Fill the ISR stack to make it easy to asses how much is being used. */\r
280                 memset( ( void * ) xISRStack, portISR_STACK_FILL_BYTE, sizeof( xISRStack ) );\r
281         }\r
282         #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */\r
283 \r
284         /* Clear the software interrupt flag. */\r
285         IFS0CLR = _IFS0_CS0IF_MASK;\r
286 \r
287         /* Set software timer priority. */\r
288         IPC0CLR = _IPC0_CS0IP_MASK;\r
289         IPC0SET = ( configKERNEL_INTERRUPT_PRIORITY << _IPC0_CS0IP_POSITION );\r
290 \r
291         /* Enable software interrupt. */\r
292         IEC0CLR = _IEC0_CS0IE_MASK;\r
293         IEC0SET = 1 << _IEC0_CS0IE_POSITION;\r
294 \r
295         /* Setup the timer to generate the tick.  Interrupts will have been\r
296         disabled by the time we get here. */\r
297         vApplicationSetupTickTimerInterrupt();\r
298 \r
299         /* Kick off the highest priority task that has been created so far.\r
300         Its stack location is loaded into uxSavedTaskStackPointer. */\r
301         uxSavedTaskStackPointer = *( UBaseType_t * ) pxCurrentTCB;\r
302         vPortStartFirstTask();\r
303 \r
304         /* Should never get here as the tasks will now be executing!  Call the task\r
305         exit error function to prevent compiler warnings about a static function\r
306         not being called in the case that the application writer overrides this\r
307         functionality by defining configTASK_RETURN_ADDRESS. */\r
308         prvTaskExitError();\r
309 \r
310         return pdFALSE;\r
311 }\r
312 /*-----------------------------------------------------------*/\r
313 \r
314 void vPortIncrementTick( void )\r
315 {\r
316 UBaseType_t uxSavedStatus;\r
317 \r
318         uxSavedStatus = uxPortSetInterruptMaskFromISR();\r
319         {\r
320                 if( xTaskIncrementTick() != pdFALSE )\r
321                 {\r
322                         /* Pend a context switch. */\r
323                         _CP0_BIS_CAUSE( portCORE_SW_0 );\r
324                 }\r
325         }\r
326         vPortClearInterruptMaskFromISR( uxSavedStatus );\r
327 \r
328         /* Look for the ISR stack getting near or past its limit. */\r
329         portCHECK_ISR_STACK();\r
330 \r
331         /* Clear timer interrupt. */\r
332         configCLEAR_TICK_TIMER_INTERRUPT();\r
333 }\r
334 /*-----------------------------------------------------------*/\r
335 \r
336 UBaseType_t uxPortSetInterruptMaskFromISR( void )\r
337 {\r
338 UBaseType_t uxSavedStatusRegister;\r
339 \r
340         __builtin_disable_interrupts();\r
341         uxSavedStatusRegister = _CP0_GET_STATUS() | 0x01;\r
342         /* This clears the IPL bits, then sets them to\r
343         configMAX_SYSCALL_INTERRUPT_PRIORITY.  This function should not be called\r
344         from an interrupt that has a priority above\r
345         configMAX_SYSCALL_INTERRUPT_PRIORITY so, when used correctly, the action\r
346         can only result in the IPL being unchanged or raised, and therefore never\r
347         lowered. */\r
348         _CP0_SET_STATUS( ( ( uxSavedStatusRegister & ( ~portALL_IPL_BITS ) ) ) | ( configMAX_SYSCALL_INTERRUPT_PRIORITY << portIPL_SHIFT ) );\r
349 \r
350         return uxSavedStatusRegister;\r
351 }\r
352 /*-----------------------------------------------------------*/\r
353 \r
354 void vPortClearInterruptMaskFromISR( UBaseType_t uxSavedStatusRegister )\r
355 {\r
356         _CP0_SET_STATUS( uxSavedStatusRegister );\r
357 }\r
358 /*-----------------------------------------------------------*/\r
359 \r
360 \r
361 \r
362 \r
363 \r