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