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