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