]> git.sur5r.net Git - freertos/blob - Source/portable/MPLAB/PIC32MX/port.c
Update ready for V5.1.0 release.
[freertos] / Source / portable / MPLAB / PIC32MX / port.c
1 /*\r
2         FreeRTOS.org V5.1.0 - Copyright (C) 2003-2008 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26     ***************************************************************************\r
27     ***************************************************************************\r
28     *                                                                         *\r
29     * SAVE TIME AND MONEY!  We can port FreeRTOS.org to your own hardware,    *\r
30     * and even write all or part of your application on your behalf.          *\r
31     * See http://www.OpenRTOS.com for details of the services we provide to   *\r
32     * expedite your project.                                                  *\r
33     *                                                                         *\r
34     ***************************************************************************\r
35     ***************************************************************************\r
36 \r
37         Please ensure to read the configuration and relevant port sections of the\r
38         online documentation.\r
39 \r
40         http://www.FreeRTOS.org - Documentation, latest information, license and \r
41         contact details.\r
42 \r
43         http://www.SafeRTOS.com - A version that is certified for use in safety \r
44         critical systems.\r
45 \r
46         http://www.OpenRTOS.com - Commercial support, development, porting, \r
47         licensing and training services.\r
48 */\r
49 \r
50 /*-----------------------------------------------------------\r
51  * Implementation of functions defined in portable.h for the PIC32MX port.\r
52   *----------------------------------------------------------*/\r
53 \r
54 /* Scheduler include files. */\r
55 #include "FreeRTOS.h"\r
56 #include "task.h"\r
57 \r
58 /* Hardware specifics. */\r
59 #define portTIMER_PRESCALE 8\r
60 \r
61 /* Bits within various registers. */\r
62 #define portIE_BIT                                      ( 0x00000001 )\r
63 #define portEXL_BIT                                     ( 0x00000002 )\r
64 #define portSW0_ENABLE                          ( 0x00000100 )\r
65 \r
66 /* The EXL bit is set to ensure interrupts do not occur while the context of\r
67 the first task is being restored. */\r
68 #define portINITIAL_SR                          ( portIE_BIT | portEXL_BIT | portSW0_ENABLE )\r
69 \r
70 /* Records the interrupt nesting depth.  This starts at one as it will be\r
71 decremented to 0 when the first task starts. */\r
72 volatile unsigned portBASE_TYPE uxInterruptNesting = 0x01;\r
73 \r
74 /* Stores the task stack pointer when a switch is made to use the system stack. */\r
75 unsigned portBASE_TYPE uxSavedTaskStackPointer = 0;\r
76 \r
77 /* The stack used by interrupt service routines that cause a context switch. */\r
78 portSTACK_TYPE xISRStack[ configISR_STACK_SIZE ] = { 0 };\r
79 \r
80 /* The top of stack value ensures there is enough space to store 6 registers on \r
81 the callers stack, as some functions seem to want to do this. */\r
82 const portBASE_TYPE * const xISRStackTop = &( xISRStack[ configISR_STACK_SIZE - 7 ] );\r
83 \r
84 /* Place the prototype here to ensure the interrupt vector is correctly installed. */\r
85 extern void __attribute__( (interrupt(ipl1), vector(_TIMER_1_VECTOR))) vT1InterruptHandler( void );\r
86 \r
87 /*\r
88  * The software interrupt handler that performs the yield.\r
89  */\r
90 void __attribute__( (interrupt(ipl1), vector(_CORE_SOFTWARE_0_VECTOR))) vPortYieldISR( void );\r
91 \r
92 /*-----------------------------------------------------------*/\r
93 \r
94 /* \r
95  * See header file for description. \r
96  */\r
97 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
98 {\r
99         *pxTopOfStack = (portSTACK_TYPE) 0xDEADBEEF;\r
100         pxTopOfStack--;\r
101 \r
102         *pxTopOfStack = (portSTACK_TYPE) 0x12345678;    /* Word to which the stack pointer will be left pointing after context restore. */\r
103         pxTopOfStack--;\r
104 \r
105         *pxTopOfStack = (portSTACK_TYPE) _CP0_GET_CAUSE();\r
106         pxTopOfStack--;\r
107 \r
108         *pxTopOfStack = (portSTACK_TYPE) portINITIAL_SR; /* CP0_STATUS */\r
109         pxTopOfStack--;\r
110 \r
111         *pxTopOfStack = (portSTACK_TYPE) pxCode;                /* CP0_EPC */\r
112         pxTopOfStack--;\r
113 \r
114         *pxTopOfStack = (portSTACK_TYPE) NULL;                  /* ra */\r
115         pxTopOfStack -= 15;\r
116 \r
117         *pxTopOfStack = (portSTACK_TYPE) pvParameters; /* Parameters to pass in */\r
118         pxTopOfStack -= 14;\r
119 \r
120         *pxTopOfStack = (portSTACK_TYPE) 0x00000000;    /* critical nesting level - no longer used. */\r
121         pxTopOfStack--;\r
122         \r
123         return pxTopOfStack;\r
124 }\r
125 /*-----------------------------------------------------------*/\r
126 \r
127 /*\r
128  * Setup a timer for a regular tick.\r
129  */\r
130 void prvSetupTimerInterrupt( void )\r
131 {\r
132 const unsigned portLONG ulCompareMatch = ( (configPERIPHERAL_CLOCK_HZ / portTIMER_PRESCALE) / configTICK_RATE_HZ ) - 1;\r
133 \r
134         OpenTimer1( ( T1_ON | T1_PS_1_8 | T1_SOURCE_INT ), ulCompareMatch );\r
135         ConfigIntTimer1( T1_INT_ON | configKERNEL_INTERRUPT_PRIORITY );\r
136 }\r
137 /*-----------------------------------------------------------*/\r
138 \r
139 void vPortEndScheduler(void)\r
140 {\r
141         /* It is unlikely that the scheduler for the PIC port will get stopped\r
142         once running.  If required disable the tick interrupt here, then return \r
143         to xPortStartScheduler(). */\r
144         for( ;; );\r
145 }\r
146 /*-----------------------------------------------------------*/\r
147 \r
148 portBASE_TYPE xPortStartScheduler( void )\r
149 {\r
150 extern void vPortStartFirstTask( void );\r
151 extern void *pxCurrentTCB;\r
152 \r
153         /* Setup the software interrupt. */\r
154         mConfigIntCoreSW0( CSW_INT_ON | CSW_INT_PRIOR_1 | CSW_INT_SUB_PRIOR_0 );\r
155 \r
156         /* Setup the timer to generate the tick.  Interrupts will have been \r
157         disabled by the time we get here. */\r
158         prvSetupTimerInterrupt();\r
159 \r
160         /* Kick off the highest priority task that has been created so far. \r
161         Its stack location is loaded into uxSavedTaskStackPointer. */\r
162         uxSavedTaskStackPointer = *( unsigned portBASE_TYPE * ) pxCurrentTCB;\r
163         vPortStartFirstTask();\r
164 \r
165         /* Should never get here as the tasks will now be executing. */\r
166         return pdFALSE;\r
167 }\r
168 /*-----------------------------------------------------------*/\r
169 \r
170 void vPortIncrementTick( void )\r
171 {\r
172 unsigned portBASE_TYPE uxSavedStatus;\r
173 \r
174         uxSavedStatus = uxPortSetInterruptMaskFromISR();\r
175                 vTaskIncrementTick();\r
176         vPortClearInterruptMaskFromISR( uxSavedStatus );\r
177         \r
178         /* If we are using the preemptive scheduler then we might want to select\r
179         a different task to execute. */\r
180         #if configUSE_PREEMPTION == 1\r
181                 SetCoreSW0();\r
182         #endif /* configUSE_PREEMPTION */\r
183 \r
184         /* Clear timer 0 interrupt. */\r
185         mT1ClearIntFlag();\r
186 }\r
187 /*-----------------------------------------------------------*/\r
188 \r
189 unsigned portBASE_TYPE uxPortSetInterruptMaskFromISR( void )\r
190 {\r
191 unsigned portBASE_TYPE uxSavedStatusRegister;\r
192 \r
193         asm volatile ( "di" );\r
194         uxSavedStatusRegister = _CP0_GET_STATUS() | 0x01;\r
195         _CP0_SET_STATUS( ( uxSavedStatusRegister | ( configMAX_SYSCALL_INTERRUPT_PRIORITY << portIPL_SHIFT ) ) );\r
196 \r
197         return uxSavedStatusRegister;\r
198 }\r
199 /*-----------------------------------------------------------*/\r
200 \r
201 void vPortClearInterruptMaskFromISR( unsigned portBASE_TYPE uxSavedStatusRegister )\r
202 {\r
203         _CP0_SET_STATUS( uxSavedStatusRegister );\r
204 }\r
205 /*-----------------------------------------------------------*/\r
206 \r
207 \r
208 \r
209 \r
210 \r