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