]> git.sur5r.net Git - freertos/blob - Source/portable/MPLAB/PIC24_dsPIC/port.c
Update to V6.1.1
[freertos] / Source / portable / MPLAB / PIC24_dsPIC / port.c
1 /*\r
2     FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS books - available as PDF or paperback  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\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 exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     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     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*\r
55         Changes from V4.2.1\r
56 \r
57         + Introduced the configKERNEL_INTERRUPT_PRIORITY definition.\r
58 */\r
59 \r
60 /*-----------------------------------------------------------\r
61  * Implementation of functions defined in portable.h for the PIC24 port.\r
62  *----------------------------------------------------------*/\r
63 \r
64 /* Scheduler include files. */\r
65 #include "FreeRTOS.h"\r
66 #include "task.h"\r
67 \r
68 /* Hardware specifics. */\r
69 #define portBIT_SET 1\r
70 #define portTIMER_PRESCALE 8\r
71 #define portINITIAL_SR  0\r
72 \r
73 /* Defined for backward compatability with project created prior to \r
74 FreeRTOS.org V4.3.0. */\r
75 #ifndef configKERNEL_INTERRUPT_PRIORITY\r
76         #define configKERNEL_INTERRUPT_PRIORITY 1\r
77 #endif\r
78 \r
79 /* The program counter is only 23 bits. */\r
80 #define portUNUSED_PR_BITS      0x7f\r
81 \r
82 /* Records the nesting depth of calls to portENTER_CRITICAL(). */\r
83 unsigned portBASE_TYPE uxCriticalNesting = 0xef;\r
84 \r
85 #if configKERNEL_INTERRUPT_PRIORITY != 1\r
86         #error If configKERNEL_INTERRUPT_PRIORITY is not 1 then the #32 in the following macros needs changing to equal the portINTERRUPT_BITS value, which is ( configKERNEL_INTERRUPT_PRIORITY << 5 )\r
87 #endif\r
88 \r
89 #ifdef MPLAB_PIC24_PORT\r
90 \r
91         #define portRESTORE_CONTEXT()                                                                                                                                                                           \\r
92                 asm volatile(   "MOV    _pxCurrentTCB, W0               \n"     /* Restore the stack pointer for the task. */                           \\r
93                                                 "MOV    [W0], W15                               \n"                                                                                                                             \\r
94                                                 "POP    W0                                              \n"     /* Restore the critical nesting counter for the task. */        \\r
95                                                 "MOV    W0, _uxCriticalNesting  \n"                                                                                                                             \\r
96                                                 "POP    PSVPAG                                  \n"                                                                                                                             \\r
97                                                 "POP    CORCON                                  \n"                                                                                                                             \\r
98                                                 "POP    TBLPAG                                  \n"                                                                                                                             \\r
99                                                 "POP    RCOUNT                                  \n"     /* Restore the registers from the stack. */                                     \\r
100                                                 "POP    W14                                             \n"                                                                                                                             \\r
101                                                 "POP.D  W12                                             \n"                                                                                                                             \\r
102                                                 "POP.D  W10                                             \n"                                                                                                                             \\r
103                                                 "POP.D  W8                                              \n"                                                                                                                             \\r
104                                                 "POP.D  W6                                              \n"                                                                                                                             \\r
105                                                 "POP.D  W4                                              \n"                                                                                                                             \\r
106                                                 "POP.D  W2                                              \n"                                                                                                                             \\r
107                                                 "POP.D  W0                                              \n"                                                                                                                             \\r
108                                                 "POP    SR                                                " );\r
109 \r
110 #endif /* MPLAB_PIC24_PORT */\r
111 \r
112 #ifdef MPLAB_DSPIC_PORT\r
113 \r
114         #define portRESTORE_CONTEXT()                                                                                                                                                                           \\r
115                 asm volatile(   "MOV    _pxCurrentTCB, W0               \n"     /* Restore the stack pointer for the task. */                           \\r
116                                                 "MOV    [W0], W15                               \n"                                                                                                                             \\r
117                                                 "POP    W0                                              \n"     /* Restore the critical nesting counter for the task. */        \\r
118                                                 "MOV    W0, _uxCriticalNesting  \n"                                                                                                                             \\r
119                                                 "POP    PSVPAG                                  \n"                                                                                                                             \\r
120                                                 "POP    CORCON                                  \n"                                                                                                                             \\r
121                                                 "POP    DOENDH                                  \n"                                                                                                                             \\r
122                                                 "POP    DOENDL                                  \n"                                                                                                                             \\r
123                                                 "POP    DOSTARTH                                \n"                                                                                                                             \\r
124                                                 "POP    DOSTARTL                                \n"                                                                                                                             \\r
125                                                 "POP    DCOUNT                                  \n"                                                                                                                             \\r
126                                                 "POP    ACCBU                                   \n"                                                                                                                             \\r
127                                                 "POP    ACCBH                                   \n"                                                                                                                             \\r
128                                                 "POP    ACCBL                                   \n"                                                                                                                             \\r
129                                                 "POP    ACCAU                                   \n"                                                                                                                             \\r
130                                                 "POP    ACCAH                                   \n"                                                                                                                             \\r
131                                                 "POP    ACCAL                                   \n"                                                                                                                             \\r
132                                                 "POP    TBLPAG                                  \n"                                                                                                                             \\r
133                                                 "POP    RCOUNT                                  \n"     /* Restore the registers from the stack. */                                     \\r
134                                                 "POP    W14                                             \n"                                                                                                                             \\r
135                                                 "POP.D  W12                                             \n"                                                                                                                             \\r
136                                                 "POP.D  W10                                             \n"                                                                                                                             \\r
137                                                 "POP.D  W8                                              \n"                                                                                                                             \\r
138                                                 "POP.D  W6                                              \n"                                                                                                                             \\r
139                                                 "POP.D  W4                                              \n"                                                                                                                             \\r
140                                                 "POP.D  W2                                              \n"                                                                                                                             \\r
141                                                 "POP.D  W0                                              \n"                                                                                                                             \\r
142                                                 "POP    SR                                                " );\r
143 \r
144 #endif /* MPLAB_DSPIC_PORT */\r
145 \r
146 /*\r
147  * Setup the timer used to generate the tick interrupt.\r
148  */\r
149 static void prvSetupTimerInterrupt( void );\r
150 \r
151 /* \r
152  * See header file for description. \r
153  */\r
154 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
155 {\r
156 unsigned short usCode;\r
157 portBASE_TYPE i;\r
158 \r
159 const portSTACK_TYPE xInitialStack[] = \r
160 {\r
161         0x1111, /* W1 */\r
162         0x2222, /* W2 */\r
163         0x3333, /* W3 */\r
164         0x4444, /* W4 */\r
165         0x5555, /* W5 */\r
166         0x6666, /* W6 */\r
167         0x7777, /* W7 */\r
168         0x8888, /* W8 */\r
169         0x9999, /* W9 */\r
170         0xaaaa, /* W10 */\r
171         0xbbbb, /* W11 */\r
172         0xcccc, /* W12 */\r
173         0xdddd, /* W13 */\r
174         0xeeee, /* W14 */\r
175         0xcdce, /* RCOUNT */\r
176         0xabac, /* TBLPAG */\r
177 \r
178         /* dsPIC specific registers. */\r
179         #ifdef MPLAB_DSPIC_PORT\r
180                 0x0202, /* ACCAL */\r
181                 0x0303, /* ACCAH */\r
182                 0x0404, /* ACCAU */\r
183                 0x0505, /* ACCBL */\r
184                 0x0606, /* ACCBH */\r
185                 0x0707, /* ACCBU */\r
186                 0x0808, /* DCOUNT */\r
187                 0x090a, /* DOSTARTL */\r
188                 0x1010, /* DOSTARTH */\r
189                 0x1110, /* DOENDL */\r
190                 0x1212, /* DOENDH */\r
191         #endif\r
192 };\r
193 \r
194         /* Setup the stack as if a yield had occurred.\r
195 \r
196         Save the low bytes of the program counter. */\r
197         usCode = ( unsigned short ) pxCode;\r
198         *pxTopOfStack = ( portSTACK_TYPE ) usCode;\r
199         pxTopOfStack++;\r
200 \r
201         /* Save the high byte of the program counter.  This will always be zero\r
202         here as it is passed in a 16bit pointer.  If the address is greater than\r
203         16 bits then the pointer will point to a jump table. */\r
204         *pxTopOfStack = ( portSTACK_TYPE ) 0;\r
205         pxTopOfStack++;\r
206 \r
207         /* Status register with interrupts enabled. */\r
208         *pxTopOfStack = portINITIAL_SR;\r
209         pxTopOfStack++;\r
210 \r
211         /* Parameters are passed in W0. */\r
212         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;\r
213         pxTopOfStack++;\r
214 \r
215         for( i = 0; i < ( sizeof( xInitialStack ) / sizeof( portSTACK_TYPE ) ); i++ )\r
216         {\r
217                 *pxTopOfStack = xInitialStack[ i ];\r
218                 pxTopOfStack++;\r
219         }\r
220 \r
221         *pxTopOfStack = CORCON;\r
222         pxTopOfStack++;\r
223         *pxTopOfStack = PSVPAG;\r
224         pxTopOfStack++;\r
225 \r
226         /* Finally the critical nesting depth. */\r
227         *pxTopOfStack = 0x00;\r
228         pxTopOfStack++;\r
229 \r
230         return pxTopOfStack;\r
231 }\r
232 /*-----------------------------------------------------------*/\r
233 \r
234 portBASE_TYPE xPortStartScheduler( void )\r
235 {\r
236         /* Setup a timer for the tick ISR. */\r
237         prvSetupTimerInterrupt(); \r
238 \r
239         /* Restore the context of the first task to run. */\r
240         portRESTORE_CONTEXT();\r
241 \r
242         /* Simulate the end of the yield function. */\r
243         asm volatile ( "return" );\r
244 \r
245         /* Should not reach here. */\r
246         return pdTRUE;\r
247 }\r
248 /*-----------------------------------------------------------*/\r
249 \r
250 void vPortEndScheduler( void )\r
251 {\r
252         /* It is unlikely that the scheduler for the PIC port will get stopped\r
253         once running.  If required disable the tick interrupt here, then return \r
254         to xPortStartScheduler(). */\r
255 }\r
256 /*-----------------------------------------------------------*/\r
257 \r
258 /*\r
259  * Setup a timer for a regular tick.\r
260  */\r
261 static void prvSetupTimerInterrupt( void )\r
262 {\r
263 const unsigned long ulCompareMatch = ( ( configCPU_CLOCK_HZ / portTIMER_PRESCALE ) / configTICK_RATE_HZ ) - 1;\r
264 \r
265         /* Prescale of 8. */\r
266         T1CON = 0;\r
267         TMR1 = 0;\r
268 \r
269         PR1 = ( unsigned short ) ulCompareMatch;\r
270 \r
271         /* Setup timer 1 interrupt priority. */\r
272         IPC0bits.T1IP = configKERNEL_INTERRUPT_PRIORITY;\r
273 \r
274         /* Clear the interrupt as a starting condition. */\r
275         IFS0bits.T1IF = 0;\r
276 \r
277         /* Enable the interrupt. */\r
278         IEC0bits.T1IE = 1;\r
279 \r
280         /* Setup the prescale value. */\r
281         T1CONbits.TCKPS0 = 1;\r
282         T1CONbits.TCKPS1 = 0;\r
283 \r
284         /* Start the timer. */\r
285         T1CONbits.TON = 1;\r
286 }\r
287 /*-----------------------------------------------------------*/\r
288 \r
289 void vPortEnterCritical( void )\r
290 {\r
291         portDISABLE_INTERRUPTS();\r
292         uxCriticalNesting++;\r
293 }\r
294 /*-----------------------------------------------------------*/\r
295 \r
296 void vPortExitCritical( void )\r
297 {\r
298         uxCriticalNesting--;\r
299         if( uxCriticalNesting == 0 )\r
300         {\r
301                 portENABLE_INTERRUPTS();\r
302         }\r
303 }\r
304 /*-----------------------------------------------------------*/\r
305 \r
306 void __attribute__((__interrupt__, auto_psv)) _T1Interrupt( void )\r
307 {\r
308         /* Clear the timer interrupt. */\r
309         IFS0bits.T1IF = 0;\r
310 \r
311         vTaskIncrementTick();\r
312 \r
313         #if configUSE_PREEMPTION == 1\r
314                 portYIELD();\r
315         #endif\r
316 }\r