]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MPLAB/PIC32MX/port.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Source / portable / MPLAB / PIC32MX / port.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*-----------------------------------------------------------\r
30  * Implementation of functions defined in portable.h for the PIC32MX port.\r
31   *----------------------------------------------------------*/\r
32 \r
33 #ifndef __XC\r
34     #error This port is designed to work with XC32.  Please update your C compiler version.\r
35 #endif\r
36 \r
37 /* Scheduler include files. */\r
38 #include "FreeRTOS.h"\r
39 #include "task.h"\r
40 \r
41 /* Hardware specifics. */\r
42 #define portTIMER_PRESCALE      8\r
43 #define portPRESCALE_BITS       1\r
44 \r
45 /* Bits within various registers. */\r
46 #define portIE_BIT                                              ( 0x00000001 )\r
47 #define portEXL_BIT                                             ( 0x00000002 )\r
48 \r
49 /* Bits within the CAUSE register. */\r
50 #define portCORE_SW_0                                   ( 0x00000100 )\r
51 #define portCORE_SW_1                                   ( 0x00000200 )\r
52 \r
53 /* The EXL bit is set to ensure interrupts do not occur while the context of\r
54 the first task is being restored. */\r
55 #define portINITIAL_SR                                  ( portIE_BIT | portEXL_BIT )\r
56 \r
57 /*\r
58 By default port.c generates its tick interrupt from TIMER1.  The user can\r
59 override this behaviour by:\r
60         1: Providing their own implementation of vApplicationSetupTickTimerInterrupt(),\r
61            which is the function that configures the timer.  The function is defined\r
62            as a weak symbol in this file so if the same function name is used in the\r
63            application code then the version in the application code will be linked\r
64            into the application in preference to the version defined in this file.\r
65         2: Define configTICK_INTERRUPT_VECTOR to the vector number of the timer used\r
66            to generate the tick interrupt.  For example, when timer 1 is used then\r
67            configTICK_INTERRUPT_VECTOR is set to _TIMER_1_VECTOR.\r
68            configTICK_INTERRUPT_VECTOR should be defined in FreeRTOSConfig.h.\r
69         3: Define configCLEAR_TICK_TIMER_INTERRUPT() to clear the interrupt in the\r
70            timer used to generate the tick interrupt.  For example, when timer 1 is\r
71            used configCLEAR_TICK_TIMER_INTERRUPT() is defined to\r
72            IFS0CLR = _IFS0_T1IF_MASK.\r
73 */\r
74 #ifndef configTICK_INTERRUPT_VECTOR\r
75         #define configTICK_INTERRUPT_VECTOR _TIMER_1_VECTOR\r
76         #define configCLEAR_TICK_TIMER_INTERRUPT() IFS0CLR = _IFS0_T1IF_MASK\r
77 #else\r
78         #ifndef configCLEAR_TICK_TIMER_INTERRUPT\r
79                 #error If configTICK_INTERRUPT_VECTOR is defined in application code then configCLEAR_TICK_TIMER_INTERRUPT must also be defined in application code.\r
80         #endif\r
81 #endif\r
82 \r
83 /* Let the user override the pre-loading of the initial RA with the address of\r
84 prvTaskExitError() in case it messes up unwinding of the stack in the\r
85 debugger - in which case configTASK_RETURN_ADDRESS can be defined as 0 (NULL). */\r
86 #ifdef configTASK_RETURN_ADDRESS\r
87         #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS\r
88 #else\r
89         #define portTASK_RETURN_ADDRESS prvTaskExitError\r
90 #endif\r
91 \r
92 /* Set configCHECK_FOR_STACK_OVERFLOW to 3 to add ISR stack checking to task\r
93 stack checking.  A problem in the ISR stack will trigger an assert, not call the\r
94 stack overflow hook function (because the stack overflow hook is specific to a\r
95 task stack, not the ISR stack). */\r
96 #if( configCHECK_FOR_STACK_OVERFLOW > 2 )\r
97 \r
98         /* Don't use 0xa5 as the stack fill bytes as that is used by the kernerl for\r
99         the task stacks, and so will legitimately appear in many positions within\r
100         the ISR stack. */\r
101         #define portISR_STACK_FILL_BYTE 0xee\r
102 \r
103         static const uint8_t ucExpectedStackBytes[] = {\r
104                                                                         portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE,             \\r
105                                                                         portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE,             \\r
106                                                                         portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE,             \\r
107                                                                         portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE,             \\r
108                                                                         portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE, portISR_STACK_FILL_BYTE };   \\r
109 \r
110         #define portCHECK_ISR_STACK() configASSERT( ( memcmp( ( void * ) xISRStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) == 0 ) )\r
111 #else\r
112         /* Define the function away. */\r
113         #define portCHECK_ISR_STACK()\r
114 #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */\r
115 \r
116 /*-----------------------------------------------------------*/\r
117 \r
118 \r
119 /*\r
120  * Place the prototype here to ensure the interrupt vector is correctly installed.\r
121  * Note that because the interrupt is written in assembly, the IPL setting in the\r
122  * following line of code has no effect.  The interrupt priority is set by the\r
123  * call to ConfigIntTimer1() in vApplicationSetupTickTimerInterrupt().\r
124  */\r
125 extern void __attribute__( (interrupt(IPL1AUTO), vector( configTICK_INTERRUPT_VECTOR ))) vPortTickInterruptHandler( void );\r
126 \r
127 /*\r
128  * The software interrupt handler that performs the yield.  Note that, because\r
129  * the interrupt is written in assembly, the IPL setting in the following line of\r
130  * code has no effect.  The interrupt priority is set by the call to\r
131  * mConfigIntCoreSW0() in xPortStartScheduler().\r
132  */\r
133 void __attribute__( (interrupt(IPL1AUTO), vector(_CORE_SOFTWARE_0_VECTOR))) vPortYieldISR( void );\r
134 \r
135 /*\r
136  * Used to catch tasks that attempt to return from their implementing function.\r
137  */\r
138 static void prvTaskExitError( void );\r
139 \r
140 /*-----------------------------------------------------------*/\r
141 \r
142 /* Records the interrupt nesting depth.  This is initialised to one as it is\r
143 decremented to 0 when the first task starts. */\r
144 volatile UBaseType_t uxInterruptNesting = 0x01;\r
145 \r
146 /* Stores the task stack pointer when a switch is made to use the system stack. */\r
147 UBaseType_t uxSavedTaskStackPointer = 0;\r
148 \r
149 /* The stack used by interrupt service routines that cause a context switch. */\r
150 __attribute__ ((aligned(8))) StackType_t xISRStack[ configISR_STACK_SIZE ] = { 0 };\r
151 \r
152 /* The top of stack value ensures there is enough space to store 6 registers on\r
153 the callers stack, as some functions seem to want to do this. */\r
154 const StackType_t * const xISRStackTop = &( xISRStack[ ( configISR_STACK_SIZE & ~portBYTE_ALIGNMENT_MASK ) - 8 ] );\r
155 \r
156 /*-----------------------------------------------------------*/\r
157 \r
158 /*\r
159  * See header file for description.\r
160  */\r
161 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
162 {\r
163         /* Ensure byte alignment is maintained when leaving this function. */\r
164         pxTopOfStack--;\r
165 \r
166         *pxTopOfStack = (StackType_t) 0xDEADBEEF;\r
167         pxTopOfStack--;\r
168 \r
169         *pxTopOfStack = (StackType_t) 0x12345678;       /* Word to which the stack pointer will be left pointing after context restore. */\r
170         pxTopOfStack--;\r
171 \r
172         *pxTopOfStack = (StackType_t) _CP0_GET_CAUSE();\r
173         pxTopOfStack--;\r
174 \r
175         *pxTopOfStack = (StackType_t) portINITIAL_SR;/* CP0_STATUS */\r
176         pxTopOfStack--;\r
177 \r
178         *pxTopOfStack = (StackType_t) pxCode;           /* CP0_EPC */\r
179         pxTopOfStack--;\r
180 \r
181         *pxTopOfStack = (StackType_t) portTASK_RETURN_ADDRESS;  /* ra */\r
182         pxTopOfStack -= 15;\r
183 \r
184         *pxTopOfStack = (StackType_t) pvParameters; /* Parameters to pass in. */\r
185         pxTopOfStack -= 15;\r
186 \r
187         return pxTopOfStack;\r
188 }\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 static void prvTaskExitError( void )\r
192 {\r
193         /* A function that implements a task must not exit or attempt to return to\r
194         its caller as there is nothing to return to.  If a task wants to exit it\r
195         should instead call vTaskDelete( NULL ).\r
196 \r
197         Artificially force an assert() to be triggered if configASSERT() is\r
198         defined, then stop here so application writers can catch the error. */\r
199         configASSERT( uxSavedTaskStackPointer == 0UL );\r
200         portDISABLE_INTERRUPTS();\r
201         for( ;; );\r
202 }\r
203 /*-----------------------------------------------------------*/\r
204 \r
205 /*\r
206  * Setup a timer for a regular tick.  This function uses peripheral timer 1.\r
207  * The function is declared weak so an application writer can use a different\r
208  * timer by redefining this implementation.  If a different timer is used then\r
209  * configTICK_INTERRUPT_VECTOR must also be defined in FreeRTOSConfig.h to\r
210  * ensure the RTOS provided tick interrupt handler is installed on the correct\r
211  * vector number.  When Timer 1 is used the vector number is defined as\r
212  * _TIMER_1_VECTOR.\r
213  */\r
214 __attribute__(( weak )) void vApplicationSetupTickTimerInterrupt( void )\r
215 {\r
216 const uint32_t ulCompareMatch = ( (configPERIPHERAL_CLOCK_HZ / portTIMER_PRESCALE) / configTICK_RATE_HZ ) - 1;\r
217 \r
218         T1CON = 0x0000;\r
219         T1CONbits.TCKPS = portPRESCALE_BITS;\r
220         PR1 = ulCompareMatch;\r
221         IPC1bits.T1IP = configKERNEL_INTERRUPT_PRIORITY;\r
222 \r
223         /* Clear the interrupt as a starting condition. */\r
224         IFS0bits.T1IF = 0;\r
225 \r
226         /* Enable the interrupt. */\r
227         IEC0bits.T1IE = 1;\r
228 \r
229         /* Start the timer. */\r
230         T1CONbits.TON = 1;\r
231 }\r
232 /*-----------------------------------------------------------*/\r
233 \r
234 void vPortEndScheduler(void)\r
235 {\r
236         /* Not implemented in ports where there is nothing to return to.\r
237         Artificially force an assert. */\r
238         configASSERT( uxInterruptNesting == 1000UL );\r
239 }\r
240 /*-----------------------------------------------------------*/\r
241 \r
242 BaseType_t xPortStartScheduler( void )\r
243 {\r
244 extern void vPortStartFirstTask( void );\r
245 extern void *pxCurrentTCB;\r
246 \r
247         #if ( configCHECK_FOR_STACK_OVERFLOW > 2 )\r
248         {\r
249                 /* Fill the ISR stack to make it easy to asses how much is being used. */\r
250                 memset( ( void * ) xISRStack, portISR_STACK_FILL_BYTE, sizeof( xISRStack ) );\r
251         }\r
252         #endif /* configCHECK_FOR_STACK_OVERFLOW > 2 */\r
253 \r
254         /* Clear the software interrupt flag. */\r
255         IFS0CLR = _IFS0_CS0IF_MASK;\r
256 \r
257         /* Set software timer priority. */\r
258         IPC0CLR = _IPC0_CS0IP_MASK;\r
259         IPC0SET = ( configKERNEL_INTERRUPT_PRIORITY << _IPC0_CS0IP_POSITION );\r
260 \r
261         /* Enable software interrupt. */\r
262         IEC0CLR = _IEC0_CS0IE_MASK;\r
263         IEC0SET = 1 << _IEC0_CS0IE_POSITION;\r
264 \r
265         /* Setup the timer to generate the tick.  Interrupts will have been\r
266         disabled by the time we get here. */\r
267         vApplicationSetupTickTimerInterrupt();\r
268 \r
269         /* Kick off the highest priority task that has been created so far.\r
270         Its stack location is loaded into uxSavedTaskStackPointer. */\r
271         uxSavedTaskStackPointer = *( UBaseType_t * ) pxCurrentTCB;\r
272         vPortStartFirstTask();\r
273 \r
274         /* Should never get here as the tasks will now be executing!  Call the task\r
275         exit error function to prevent compiler warnings about a static function\r
276         not being called in the case that the application writer overrides this\r
277         functionality by defining configTASK_RETURN_ADDRESS. */\r
278         prvTaskExitError();\r
279 \r
280         return pdFALSE;\r
281 }\r
282 /*-----------------------------------------------------------*/\r
283 \r
284 void vPortIncrementTick( void )\r
285 {\r
286 UBaseType_t uxSavedStatus;\r
287 \r
288         uxSavedStatus = uxPortSetInterruptMaskFromISR();\r
289         {\r
290                 if( xTaskIncrementTick() != pdFALSE )\r
291                 {\r
292                         /* Pend a context switch. */\r
293                         _CP0_BIS_CAUSE( portCORE_SW_0 );\r
294                 }\r
295         }\r
296         vPortClearInterruptMaskFromISR( uxSavedStatus );\r
297 \r
298         /* Look for the ISR stack getting near or past its limit. */\r
299         portCHECK_ISR_STACK();\r
300 \r
301         /* Clear timer interrupt. */\r
302         configCLEAR_TICK_TIMER_INTERRUPT();\r
303 }\r
304 /*-----------------------------------------------------------*/\r
305 \r
306 UBaseType_t uxPortSetInterruptMaskFromISR( void )\r
307 {\r
308 UBaseType_t uxSavedStatusRegister;\r
309 \r
310         __builtin_disable_interrupts();\r
311         uxSavedStatusRegister = _CP0_GET_STATUS() | 0x01;\r
312         /* This clears the IPL bits, then sets them to\r
313         configMAX_SYSCALL_INTERRUPT_PRIORITY.  This function should not be called\r
314         from an interrupt that has a priority above\r
315         configMAX_SYSCALL_INTERRUPT_PRIORITY so, when used correctly, the action\r
316         can only result in the IPL being unchanged or raised, and therefore never\r
317         lowered. */\r
318         _CP0_SET_STATUS( ( ( uxSavedStatusRegister & ( ~portALL_IPL_BITS ) ) ) | ( configMAX_SYSCALL_INTERRUPT_PRIORITY << portIPL_SHIFT ) );\r
319 \r
320         return uxSavedStatusRegister;\r
321 }\r
322 /*-----------------------------------------------------------*/\r
323 \r
324 void vPortClearInterruptMaskFromISR( UBaseType_t uxSavedStatusRegister )\r
325 {\r
326         _CP0_SET_STATUS( uxSavedStatusRegister );\r
327 }\r
328 /*-----------------------------------------------------------*/\r
329 \r
330 \r
331 \r
332 \r
333 \r