]> begriffs open source - freertos/blob - Source/portable/MPLAB/PIC32MX/port.c
Update to V5.1.2.
[freertos] / Source / portable / MPLAB / PIC32MX / port.c
1 /*\r
2         FreeRTOS.org V5.1.2 - Copyright (C) 2003-2009 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     * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
30         *                                                                         *\r
31         * This is a concise, step by step, 'hands on' guide that describes both   *\r
32         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
33         * explains numerous examples that are written using the FreeRTOS API.     *\r
34         * Full source code for all the examples is provided in an accompanying    *\r
35         * .zip file.                                                              *\r
36     *                                                                         *\r
37     ***************************************************************************\r
38     ***************************************************************************\r
39 \r
40         Please ensure to read the configuration and relevant port sections of the\r
41         online documentation.\r
42 \r
43         http://www.FreeRTOS.org - Documentation, latest information, license and \r
44         contact details.\r
45 \r
46         http://www.SafeRTOS.com - A version that is certified for use in safety \r
47         critical systems.\r
48 \r
49         http://www.OpenRTOS.com - Commercial support, development, porting, \r
50         licensing and training services.\r
51 */\r
52 \r
53 /*-----------------------------------------------------------\r
54  * Implementation of functions defined in portable.h for the PIC32MX port.\r
55   *----------------------------------------------------------*/\r
56 \r
57 /* Scheduler include files. */\r
58 #include "FreeRTOS.h"\r
59 #include "task.h"\r
60 \r
61 /* Hardware specifics. */\r
62 #define portTIMER_PRESCALE 8\r
63 \r
64 /* Bits within various registers. */\r
65 #define portIE_BIT                                      ( 0x00000001 )\r
66 #define portEXL_BIT                                     ( 0x00000002 )\r
67 #define portSW0_ENABLE                          ( 0x00000100 )\r
68 \r
69 /* The EXL bit is set to ensure interrupts do not occur while the context of\r
70 the first task is being restored. */\r
71 #define portINITIAL_SR                          ( portIE_BIT | portEXL_BIT | portSW0_ENABLE )\r
72 \r
73 /* Records the interrupt nesting depth.  This starts at one as it will be\r
74 decremented to 0 when the first task starts. */\r
75 volatile unsigned portBASE_TYPE uxInterruptNesting = 0x01;\r
76 \r
77 /* Stores the task stack pointer when a switch is made to use the system stack. */\r
78 unsigned portBASE_TYPE uxSavedTaskStackPointer = 0;\r
79 \r
80 /* The stack used by interrupt service routines that cause a context switch. */\r
81 portSTACK_TYPE xISRStack[ configISR_STACK_SIZE ] = { 0 };\r
82 \r
83 /* The top of stack value ensures there is enough space to store 6 registers on \r
84 the callers stack, as some functions seem to want to do this. */\r
85 const portBASE_TYPE * const xISRStackTop = &( xISRStack[ configISR_STACK_SIZE - 7 ] );\r
86 \r
87 /* Place the prototype here to ensure the interrupt vector is correctly installed. */\r
88 extern void __attribute__( (interrupt(ipl1), vector(_TIMER_1_VECTOR))) vT1InterruptHandler( void );\r
89 \r
90 /*\r
91  * The software interrupt handler that performs the yield.\r
92  */\r
93 void __attribute__( (interrupt(ipl1), vector(_CORE_SOFTWARE_0_VECTOR))) vPortYieldISR( void );\r
94 \r
95 /*-----------------------------------------------------------*/\r
96 \r
97 /* \r
98  * See header file for description. \r
99  */\r
100 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
101 {\r
102         *pxTopOfStack = (portSTACK_TYPE) 0xDEADBEEF;\r
103         pxTopOfStack--;\r
104 \r
105         *pxTopOfStack = (portSTACK_TYPE) 0x12345678;    /* Word to which the stack pointer will be left pointing after context restore. */\r
106         pxTopOfStack--;\r
107 \r
108         *pxTopOfStack = (portSTACK_TYPE) _CP0_GET_CAUSE();\r
109         pxTopOfStack--;\r
110 \r
111         *pxTopOfStack = (portSTACK_TYPE) portINITIAL_SR; /* CP0_STATUS */\r
112         pxTopOfStack--;\r
113 \r
114         *pxTopOfStack = (portSTACK_TYPE) pxCode;                /* CP0_EPC */\r
115         pxTopOfStack--;\r
116 \r
117         *pxTopOfStack = (portSTACK_TYPE) NULL;                  /* ra */\r
118         pxTopOfStack -= 15;\r
119 \r
120         *pxTopOfStack = (portSTACK_TYPE) pvParameters; /* Parameters to pass in */\r
121         pxTopOfStack -= 14;\r
122 \r
123         *pxTopOfStack = (portSTACK_TYPE) 0x00000000;    /* critical nesting level - no longer used. */\r
124         pxTopOfStack--;\r
125         \r
126         return pxTopOfStack;\r
127 }\r
128 /*-----------------------------------------------------------*/\r
129 \r
130 /*\r
131  * Setup a timer for a regular tick.\r
132  */\r
133 void prvSetupTimerInterrupt( void )\r
134 {\r
135 const unsigned portLONG ulCompareMatch = ( (configPERIPHERAL_CLOCK_HZ / portTIMER_PRESCALE) / configTICK_RATE_HZ ) - 1;\r
136 \r
137         OpenTimer1( ( T1_ON | T1_PS_1_8 | T1_SOURCE_INT ), ulCompareMatch );\r
138         ConfigIntTimer1( T1_INT_ON | configKERNEL_INTERRUPT_PRIORITY );\r
139 }\r
140 /*-----------------------------------------------------------*/\r
141 \r
142 void vPortEndScheduler(void)\r
143 {\r
144         /* It is unlikely that the scheduler for the PIC port will get stopped\r
145         once running.  If required disable the tick interrupt here, then return \r
146         to xPortStartScheduler(). */\r
147         for( ;; );\r
148 }\r
149 /*-----------------------------------------------------------*/\r
150 \r
151 portBASE_TYPE xPortStartScheduler( void )\r
152 {\r
153 extern void vPortStartFirstTask( void );\r
154 extern void *pxCurrentTCB;\r
155 \r
156         /* Setup the software interrupt. */\r
157         mConfigIntCoreSW0( CSW_INT_ON | CSW_INT_PRIOR_1 | CSW_INT_SUB_PRIOR_0 );\r
158 \r
159         /* Setup the timer to generate the tick.  Interrupts will have been \r
160         disabled by the time we get here. */\r
161         prvSetupTimerInterrupt();\r
162 \r
163         /* Kick off the highest priority task that has been created so far. \r
164         Its stack location is loaded into uxSavedTaskStackPointer. */\r
165         uxSavedTaskStackPointer = *( unsigned portBASE_TYPE * ) pxCurrentTCB;\r
166         vPortStartFirstTask();\r
167 \r
168         /* Should never get here as the tasks will now be executing. */\r
169         return pdFALSE;\r
170 }\r
171 /*-----------------------------------------------------------*/\r
172 \r
173 void vPortIncrementTick( void )\r
174 {\r
175 unsigned portBASE_TYPE uxSavedStatus;\r
176 \r
177         uxSavedStatus = uxPortSetInterruptMaskFromISR();\r
178                 vTaskIncrementTick();\r
179         vPortClearInterruptMaskFromISR( uxSavedStatus );\r
180         \r
181         /* If we are using the preemptive scheduler then we might want to select\r
182         a different task to execute. */\r
183         #if configUSE_PREEMPTION == 1\r
184                 SetCoreSW0();\r
185         #endif /* configUSE_PREEMPTION */\r
186 \r
187         /* Clear timer 0 interrupt. */\r
188         mT1ClearIntFlag();\r
189 }\r
190 /*-----------------------------------------------------------*/\r
191 \r
192 unsigned portBASE_TYPE uxPortSetInterruptMaskFromISR( void )\r
193 {\r
194 unsigned portBASE_TYPE uxSavedStatusRegister;\r
195 \r
196         asm volatile ( "di" );\r
197         uxSavedStatusRegister = _CP0_GET_STATUS() | 0x01;\r
198         _CP0_SET_STATUS( ( uxSavedStatusRegister | ( configMAX_SYSCALL_INTERRUPT_PRIORITY << portIPL_SHIFT ) ) );\r
199 \r
200         return uxSavedStatusRegister;\r
201 }\r
202 /*-----------------------------------------------------------*/\r
203 \r
204 void vPortClearInterruptMaskFromISR( unsigned portBASE_TYPE uxSavedStatusRegister )\r
205 {\r
206         _CP0_SET_STATUS( uxSavedStatusRegister );\r
207 }\r
208 /*-----------------------------------------------------------*/\r
209 \r
210 \r
211 \r
212 \r
213 \r