]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MPLAB/PIC32MX/port.c
Update FreeRTOS version number to V7.5.3
[freertos] / FreeRTOS / Source / portable / MPLAB / PIC32MX / port.c
1 /*\r
2     FreeRTOS V7.5.3 - Copyright (C) 2013 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 distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! 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 PIC32MX port.\r
68   *----------------------------------------------------------*/\r
69 \r
70 #ifndef __XC\r
71     #error This port is designed to work with XC32.  Please update your C compiler version.\r
72 #endif\r
73 \r
74 /* Scheduler include files. */\r
75 #include "FreeRTOS.h"\r
76 #include "task.h"\r
77 \r
78 /* Hardware specifics. */\r
79 #define portTIMER_PRESCALE      8\r
80 #define portPRESCALE_BITS       1\r
81 \r
82 /* Bits within various registers. */\r
83 #define portIE_BIT                                              ( 0x00000001 )\r
84 #define portEXL_BIT                                             ( 0x00000002 )\r
85 \r
86 /* Bits within the CAUSE register. */\r
87 #define portCORE_SW_0                                   ( 0x00000100 )\r
88 #define portCORE_SW_1                                   ( 0x00000200 )\r
89 \r
90 /* The EXL bit is set to ensure interrupts do not occur while the context of\r
91 the first task is being restored. */\r
92 #define portINITIAL_SR                                  ( portIE_BIT | portEXL_BIT )\r
93 \r
94 #ifndef configTICK_INTERRUPT_VECTOR\r
95         #define configTICK_INTERRUPT_VECTOR _TIMER_1_VECTOR\r
96 #endif\r
97 \r
98 /* Records the interrupt nesting depth.  This starts at one as it will be\r
99 decremented to 0 when the first task starts. */\r
100 volatile unsigned portBASE_TYPE uxInterruptNesting = 0x01;\r
101 \r
102 /* Stores the task stack pointer when a switch is made to use the system stack. */\r
103 unsigned portBASE_TYPE uxSavedTaskStackPointer = 0;\r
104 \r
105 /* The stack used by interrupt service routines that cause a context switch. */\r
106 portSTACK_TYPE xISRStack[ configISR_STACK_SIZE ] = { 0 };\r
107 \r
108 /* The top of stack value ensures there is enough space to store 6 registers on\r
109 the callers stack, as some functions seem to want to do this. */\r
110 const portSTACK_TYPE * const xISRStackTop = &( xISRStack[ configISR_STACK_SIZE - 7 ] );\r
111 \r
112 /*\r
113  * Place the prototype here to ensure the interrupt vector is correctly installed.\r
114  * Note that because the interrupt is written in assembly, the IPL setting in the\r
115  * following line of code has no effect.  The interrupt priority is set by the\r
116  * call to ConfigIntTimer1() in vApplicationSetupTickTimerInterrupt().\r
117  */\r
118 extern void __attribute__( (interrupt(ipl1), vector( configTICK_INTERRUPT_VECTOR ))) vPortTickInterruptHandler( void );\r
119 \r
120 /*\r
121  * The software interrupt handler that performs the yield.  Note that, because\r
122  * the interrupt is written in assembly, the IPL setting in the following line of\r
123  * code has no effect.  The interrupt priority is set by the call to\r
124  * mConfigIntCoreSW0() in xPortStartScheduler().\r
125  */\r
126 void __attribute__( (interrupt(ipl1), vector(_CORE_SOFTWARE_0_VECTOR))) vPortYieldISR( void );\r
127 \r
128 /*-----------------------------------------------------------*/\r
129 \r
130 /*\r
131  * See header file for description.\r
132  */\r
133 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
134 {\r
135         /* Ensure byte alignment is maintained when leaving this function. */\r
136         pxTopOfStack--;\r
137 \r
138         *pxTopOfStack = (portSTACK_TYPE) 0xDEADBEEF;\r
139         pxTopOfStack--;\r
140 \r
141         *pxTopOfStack = (portSTACK_TYPE) 0x12345678;    /* Word to which the stack pointer will be left pointing after context restore. */\r
142         pxTopOfStack--;\r
143 \r
144         *pxTopOfStack = (portSTACK_TYPE) _CP0_GET_CAUSE();\r
145         pxTopOfStack--;\r
146 \r
147         *pxTopOfStack = (portSTACK_TYPE) portINITIAL_SR; /* CP0_STATUS */\r
148         pxTopOfStack--;\r
149 \r
150         *pxTopOfStack = (portSTACK_TYPE) pxCode;                /* CP0_EPC */\r
151         pxTopOfStack--;\r
152 \r
153         *pxTopOfStack = (portSTACK_TYPE) NULL;                  /* ra */\r
154         pxTopOfStack -= 15;\r
155 \r
156         *pxTopOfStack = (portSTACK_TYPE) pvParameters; /* Parameters to pass in */\r
157         pxTopOfStack -= 14;\r
158 \r
159         *pxTopOfStack = (portSTACK_TYPE) 0x00000000;    /* critical nesting level - no longer used. */\r
160         pxTopOfStack--;\r
161 \r
162         return pxTopOfStack;\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 /*\r
167  * Setup a timer for a regular tick.  This function uses peripheral timer 1.\r
168  * The function is declared weak so an application writer can use a different\r
169  * timer by redefining this implementation.  If a different timer is used then\r
170  * configTICK_INTERRUPT_VECTOR must also be defined in FreeRTOSConfig.h to\r
171  * ensure the RTOS provided tick interrupt handler is installed on the correct\r
172  * vector number.  When Timer 1 is used the vector number is defined as\r
173  * _TIMER_1_VECTOR.\r
174  */\r
175 __attribute__(( weak )) void vApplicationSetupTickTimerInterrupt( void )\r
176 {\r
177 const unsigned long ulCompareMatch = ( (configPERIPHERAL_CLOCK_HZ / portTIMER_PRESCALE) / configTICK_RATE_HZ ) - 1;\r
178 \r
179         T1CON = 0x0000;\r
180         T1CONbits.TCKPS = portPRESCALE_BITS;\r
181         PR1 = ulCompareMatch;\r
182         IPC1bits.T1IP = configKERNEL_INTERRUPT_PRIORITY;\r
183 \r
184         /* Clear the interrupt as a starting condition. */\r
185         IFS0bits.T1IF = 0;\r
186 \r
187         /* Enable the interrupt. */\r
188         IEC0bits.T1IE = 1;\r
189 \r
190         /* Start the timer. */\r
191         T1CONbits.TON = 1;\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 void vPortEndScheduler(void)\r
196 {\r
197         /* It is unlikely that the scheduler for the PIC port will get stopped\r
198         once running.  If required disable the tick interrupt here, then return\r
199         to xPortStartScheduler(). */\r
200         for( ;; );\r
201 }\r
202 /*-----------------------------------------------------------*/\r
203 \r
204 portBASE_TYPE xPortStartScheduler( void )\r
205 {\r
206 extern void vPortStartFirstTask( void );\r
207 extern void *pxCurrentTCB;\r
208 \r
209         /* Clear the software interrupt flag. */\r
210         IFS0CLR = _IFS0_CS0IF_MASK;\r
211 \r
212         /* Set software timer priority. */\r
213         IPC0CLR = _IPC0_CS0IP_MASK;\r
214         IPC0SET = ( configKERNEL_INTERRUPT_PRIORITY << _IPC0_CS0IP_POSITION );\r
215 \r
216         /* Enable software interrupt. */\r
217         IEC0CLR = _IEC0_CS0IE_MASK;\r
218         IEC0SET = 1 << _IEC0_CS0IE_POSITION;\r
219 \r
220         /* Setup the timer to generate the tick.  Interrupts will have been\r
221         disabled by the time we get here. */\r
222         vApplicationSetupTickTimerInterrupt();\r
223 \r
224         /* Kick off the highest priority task that has been created so far.\r
225         Its stack location is loaded into uxSavedTaskStackPointer. */\r
226         uxSavedTaskStackPointer = *( unsigned portBASE_TYPE * ) pxCurrentTCB;\r
227         vPortStartFirstTask();\r
228 \r
229         /* Should never get here as the tasks will now be executing. */\r
230         return pdFALSE;\r
231 }\r
232 /*-----------------------------------------------------------*/\r
233 \r
234 void vPortIncrementTick( void )\r
235 {\r
236 unsigned portBASE_TYPE uxSavedStatus;\r
237 \r
238         uxSavedStatus = uxPortSetInterruptMaskFromISR();\r
239         {\r
240                 if( xTaskIncrementTick() != pdFALSE )\r
241                 {\r
242                         /* Pend a context switch. */\r
243                         _CP0_BIS_CAUSE( portCORE_SW_0 );\r
244                 }\r
245         }\r
246         vPortClearInterruptMaskFromISR( uxSavedStatus );\r
247 \r
248         /* Clear timer 1 interrupt. */\r
249         IFS0CLR = _IFS0_T1IF_MASK;\r
250 }\r
251 /*-----------------------------------------------------------*/\r
252 \r
253 unsigned portBASE_TYPE uxPortSetInterruptMaskFromISR( void )\r
254 {\r
255 unsigned portBASE_TYPE uxSavedStatusRegister;\r
256 \r
257         asm volatile ( "di" );\r
258         uxSavedStatusRegister = _CP0_GET_STATUS() | 0x01;\r
259         /* This clears the IPL bits, then sets them to\r
260         configMAX_SYSCALL_INTERRUPT_PRIORITY.  This function should not be called\r
261         from an interrupt that has a priority above\r
262         configMAX_SYSCALL_INTERRUPT_PRIORITY so, when used correctly, the action\r
263         can only result in the IPL being unchanged or raised, and therefore never\r
264         lowered. */\r
265         _CP0_SET_STATUS( ( ( uxSavedStatusRegister & ( ~portALL_IPL_BITS ) ) ) | ( configMAX_SYSCALL_INTERRUPT_PRIORITY << portIPL_SHIFT ) );\r
266 \r
267         return uxSavedStatusRegister;\r
268 }\r
269 /*-----------------------------------------------------------*/\r
270 \r
271 void vPortClearInterruptMaskFromISR( unsigned portBASE_TYPE uxSavedStatusRegister )\r
272 {\r
273         _CP0_SET_STATUS( uxSavedStatusRegister );\r
274 }\r
275 /*-----------------------------------------------------------*/\r
276 \r
277 \r
278 \r
279 \r
280 \r