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