]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MPLAB/PIC32MX/port.c
Prepare for V7.4.0 release.
[freertos] / FreeRTOS / Source / portable / MPLAB / PIC32MX / port.c
1 /*\r
2     FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not itcan be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /*-----------------------------------------------------------\r
76  * Implementation of functions defined in portable.h for the PIC32MX port.\r
77   *----------------------------------------------------------*/\r
78 \r
79 #ifndef __XC__\r
80     #error This port is designed to work with XC32.  Please update your C compiler version.\r
81 #endif\r
82 \r
83 /* Scheduler include files. */\r
84 #include "FreeRTOS.h"\r
85 #include "task.h"\r
86 \r
87 /* Hardware specifics. */\r
88 #define portTIMER_PRESCALE      8\r
89 #define portPRESCALE_BITS       1\r
90 \r
91 /* Bits within various registers. */\r
92 #define portIE_BIT                                              ( 0x00000001 )\r
93 #define portEXL_BIT                                             ( 0x00000002 )\r
94 \r
95 /* Bits within the CAUSE register. */\r
96 #define portCORE_SW_0                                   ( 0x00000100 )\r
97 #define portCORE_SW_1                                   ( 0x00000200 )\r
98 \r
99 /* The EXL bit is set to ensure interrupts do not occur while the context of\r
100 the first task is being restored. */\r
101 #define portINITIAL_SR                                  ( portIE_BIT | portEXL_BIT )\r
102 \r
103 #ifndef configTICK_INTERRUPT_VECTOR\r
104         #define configTICK_INTERRUPT_VECTOR _TIMER_1_VECTOR\r
105 #endif\r
106 \r
107 /* Records the interrupt nesting depth.  This starts at one as it will be\r
108 decremented to 0 when the first task starts. */\r
109 volatile unsigned portBASE_TYPE uxInterruptNesting = 0x01;\r
110 \r
111 /* Stores the task stack pointer when a switch is made to use the system stack. */\r
112 unsigned portBASE_TYPE uxSavedTaskStackPointer = 0;\r
113 \r
114 /* The stack used by interrupt service routines that cause a context switch. */\r
115 portSTACK_TYPE xISRStack[ configISR_STACK_SIZE ] = { 0 };\r
116 \r
117 /* The top of stack value ensures there is enough space to store 6 registers on\r
118 the callers stack, as some functions seem to want to do this. */\r
119 const portSTACK_TYPE * const xISRStackTop = &( xISRStack[ configISR_STACK_SIZE - 7 ] );\r
120 \r
121 /*\r
122  * Place the prototype here to ensure the interrupt vector is correctly installed.\r
123  * Note that because the interrupt is written in assembly, the IPL setting in the\r
124  * following line of code has no effect.  The interrupt priority is set by the\r
125  * call to ConfigIntTimer1() in vApplicationSetupTickTimerInterrupt().\r
126  */\r
127 extern void __attribute__( (interrupt(ipl1), vector( configTICK_INTERRUPT_VECTOR ))) vPortTickInterruptHandler( void );\r
128 \r
129 /*\r
130  * The software interrupt handler that performs the yield.  Note that, because\r
131  * the interrupt is written in assembly, the IPL setting in the following line of\r
132  * code has no effect.  The interrupt priority is set by the call to\r
133  * mConfigIntCoreSW0() in xPortStartScheduler().\r
134  */\r
135 void __attribute__( (interrupt(ipl1), vector(_CORE_SOFTWARE_0_VECTOR))) vPortYieldISR( void );\r
136 \r
137 /*-----------------------------------------------------------*/\r
138 \r
139 /*\r
140  * See header file for description.\r
141  */\r
142 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
143 {\r
144         /* Ensure byte alignment is maintained when leaving this function. */\r
145         pxTopOfStack--;\r
146 \r
147         *pxTopOfStack = (portSTACK_TYPE) 0xDEADBEEF;\r
148         pxTopOfStack--;\r
149 \r
150         *pxTopOfStack = (portSTACK_TYPE) 0x12345678;    /* Word to which the stack pointer will be left pointing after context restore. */\r
151         pxTopOfStack--;\r
152 \r
153         *pxTopOfStack = (portSTACK_TYPE) _CP0_GET_CAUSE();\r
154         pxTopOfStack--;\r
155 \r
156         *pxTopOfStack = (portSTACK_TYPE) portINITIAL_SR; /* CP0_STATUS */\r
157         pxTopOfStack--;\r
158 \r
159         *pxTopOfStack = (portSTACK_TYPE) pxCode;                /* CP0_EPC */\r
160         pxTopOfStack--;\r
161 \r
162         *pxTopOfStack = (portSTACK_TYPE) NULL;                  /* ra */\r
163         pxTopOfStack -= 15;\r
164 \r
165         *pxTopOfStack = (portSTACK_TYPE) pvParameters; /* Parameters to pass in */\r
166         pxTopOfStack -= 14;\r
167 \r
168         *pxTopOfStack = (portSTACK_TYPE) 0x00000000;    /* critical nesting level - no longer used. */\r
169         pxTopOfStack--;\r
170 \r
171         return pxTopOfStack;\r
172 }\r
173 /*-----------------------------------------------------------*/\r
174 \r
175 /*\r
176  * Setup a timer for a regular tick.  This function uses peripheral timer 1.\r
177  * The function is declared weak so an application writer can use a different\r
178  * timer by redefining this implementation.  If a different timer is used then\r
179  * configTICK_INTERRUPT_VECTOR must also be defined in FreeRTOSConfig.h to\r
180  * ensure the RTOS provided tick interrupt handler is installed on the correct\r
181  * vector number.  When Timer 1 is used the vector number is defined as\r
182  * _TIMER_1_VECTOR.\r
183  */\r
184 __attribute__(( weak )) void vApplicationSetupTickTimerInterrupt( void )\r
185 {\r
186 const unsigned long ulCompareMatch = ( (configPERIPHERAL_CLOCK_HZ / portTIMER_PRESCALE) / configTICK_RATE_HZ ) - 1;\r
187 \r
188         T1CON = 0x0000;\r
189         T1CONbits.TCKPS = portPRESCALE_BITS;\r
190         PR1 = ulCompareMatch;\r
191         IPC1bits.T1IP = configKERNEL_INTERRUPT_PRIORITY;\r
192 \r
193         /* Clear the interrupt as a starting condition. */\r
194         IFS0bits.T1IF = 0;\r
195 \r
196         /* Enable the interrupt. */\r
197         IEC0bits.T1IE = 1;\r
198 \r
199         /* Start the timer. */\r
200         T1CONbits.TON = 1;\r
201 }\r
202 /*-----------------------------------------------------------*/\r
203 \r
204 void vPortEndScheduler(void)\r
205 {\r
206         /* It is unlikely that the scheduler for the PIC port will get stopped\r
207         once running.  If required disable the tick interrupt here, then return\r
208         to xPortStartScheduler(). */\r
209         for( ;; );\r
210 }\r
211 /*-----------------------------------------------------------*/\r
212 \r
213 portBASE_TYPE xPortStartScheduler( void )\r
214 {\r
215 extern void vPortStartFirstTask( void );\r
216 extern void *pxCurrentTCB;\r
217 \r
218         /* Clear the software interrupt flag. */\r
219         IFS0CLR = _IFS0_CS0IF_MASK;\r
220 \r
221         /* Set software timer priority. */\r
222         IPC0CLR = _IPC0_CS0IP_MASK;\r
223         IPC0SET = ( configKERNEL_INTERRUPT_PRIORITY << _IPC0_CS0IP_POSITION );\r
224 \r
225         /* Enable software interrupt. */\r
226         IEC0CLR = _IEC0_CS0IE_MASK;\r
227         IEC0SET = 1 << _IEC0_CS0IE_POSITION;\r
228 \r
229         /* Setup the timer to generate the tick.  Interrupts will have been\r
230         disabled by the time we get here. */\r
231         vApplicationSetupTickTimerInterrupt();\r
232 \r
233         /* Kick off the highest priority task that has been created so far.\r
234         Its stack location is loaded into uxSavedTaskStackPointer. */\r
235         uxSavedTaskStackPointer = *( unsigned portBASE_TYPE * ) pxCurrentTCB;\r
236         vPortStartFirstTask();\r
237 \r
238         /* Should never get here as the tasks will now be executing. */\r
239         return pdFALSE;\r
240 }\r
241 /*-----------------------------------------------------------*/\r
242 \r
243 void vPortIncrementTick( void )\r
244 {\r
245 unsigned portBASE_TYPE uxSavedStatus;\r
246 \r
247         uxSavedStatus = uxPortSetInterruptMaskFromISR();\r
248         vTaskIncrementTick();\r
249         vPortClearInterruptMaskFromISR( uxSavedStatus );\r
250 \r
251         /* If we are using the preemptive scheduler then we might want to select\r
252         a different task to execute. */\r
253         #if configUSE_PREEMPTION == 1\r
254                 _CP0_BIS_CAUSE( portCORE_SW_0 );\r
255         #endif /* configUSE_PREEMPTION */\r
256 \r
257         /* Clear timer 1 interrupt. */\r
258         IFS0CLR = _IFS0_T1IF_MASK;\r
259 }\r
260 /*-----------------------------------------------------------*/\r
261 \r
262 unsigned portBASE_TYPE uxPortSetInterruptMaskFromISR( void )\r
263 {\r
264 unsigned portBASE_TYPE uxSavedStatusRegister;\r
265 \r
266         asm volatile ( "di" );\r
267         uxSavedStatusRegister = _CP0_GET_STATUS() | 0x01;\r
268         /* This clears the IPL bits, then sets them to\r
269         configMAX_SYSCALL_INTERRUPT_PRIORITY.  This function should not be called\r
270         from an interrupt that has a priority above\r
271         configMAX_SYSCALL_INTERRUPT_PRIORITY so, when used correctly, the action\r
272         can only result in the IPL being unchanged or raised, and therefore never\r
273         lowered. */\r
274         _CP0_SET_STATUS( ( ( uxSavedStatusRegister & ( ~portALL_IPL_BITS ) ) ) | ( configMAX_SYSCALL_INTERRUPT_PRIORITY << portIPL_SHIFT ) );\r
275 \r
276         return uxSavedStatusRegister;\r
277 }\r
278 /*-----------------------------------------------------------*/\r
279 \r
280 void vPortClearInterruptMaskFromISR( unsigned portBASE_TYPE uxSavedStatusRegister )\r
281 {\r
282         _CP0_SET_STATUS( uxSavedStatusRegister );\r
283 }\r
284 /*-----------------------------------------------------------*/\r
285 \r
286 \r
287 \r
288 \r
289 \r