]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MPLAB/PIC24_dsPIC/port.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Source / portable / MPLAB / PIC24_dsPIC / port.c
1 /*\r
2     FreeRTOS V7.1.1 - 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         Changes from V4.2.1\r
69 \r
70         + Introduced the configKERNEL_INTERRUPT_PRIORITY definition.\r
71 */\r
72 \r
73 /*-----------------------------------------------------------\r
74  * Implementation of functions defined in portable.h for the PIC24 port.\r
75  *----------------------------------------------------------*/\r
76 \r
77 /* Scheduler include files. */\r
78 #include "FreeRTOS.h"\r
79 #include "task.h"\r
80 \r
81 /* Hardware specifics. */\r
82 #define portBIT_SET 1\r
83 #define portTIMER_PRESCALE 8\r
84 #define portINITIAL_SR  0\r
85 \r
86 /* Defined for backward compatability with project created prior to \r
87 FreeRTOS.org V4.3.0. */\r
88 #ifndef configKERNEL_INTERRUPT_PRIORITY\r
89         #define configKERNEL_INTERRUPT_PRIORITY 1\r
90 #endif\r
91 \r
92 /* The program counter is only 23 bits. */\r
93 #define portUNUSED_PR_BITS      0x7f\r
94 \r
95 /* Records the nesting depth of calls to portENTER_CRITICAL(). */\r
96 unsigned portBASE_TYPE uxCriticalNesting = 0xef;\r
97 \r
98 #if configKERNEL_INTERRUPT_PRIORITY != 1\r
99         #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
100 #endif\r
101 \r
102 #ifdef MPLAB_PIC24_PORT\r
103 \r
104         #define portRESTORE_CONTEXT()                                                                                                                                                                           \\r
105                 asm volatile(   "MOV    _pxCurrentTCB, W0               \n"     /* Restore the stack pointer for the task. */                           \\r
106                                                 "MOV    [W0], W15                               \n"                                                                                                                             \\r
107                                                 "POP    W0                                              \n"     /* Restore the critical nesting counter for the task. */        \\r
108                                                 "MOV    W0, _uxCriticalNesting  \n"                                                                                                                             \\r
109                                                 "POP    PSVPAG                                  \n"                                                                                                                             \\r
110                                                 "POP    CORCON                                  \n"                                                                                                                             \\r
111                                                 "POP    TBLPAG                                  \n"                                                                                                                             \\r
112                                                 "POP    RCOUNT                                  \n"     /* Restore the registers from the stack. */                                     \\r
113                                                 "POP    W14                                             \n"                                                                                                                             \\r
114                                                 "POP.D  W12                                             \n"                                                                                                                             \\r
115                                                 "POP.D  W10                                             \n"                                                                                                                             \\r
116                                                 "POP.D  W8                                              \n"                                                                                                                             \\r
117                                                 "POP.D  W6                                              \n"                                                                                                                             \\r
118                                                 "POP.D  W4                                              \n"                                                                                                                             \\r
119                                                 "POP.D  W2                                              \n"                                                                                                                             \\r
120                                                 "POP.D  W0                                              \n"                                                                                                                             \\r
121                                                 "POP    SR                                                " );\r
122 \r
123 #endif /* MPLAB_PIC24_PORT */\r
124 \r
125 #ifdef MPLAB_DSPIC_PORT\r
126 \r
127         #define portRESTORE_CONTEXT()                                                                                                                                                                           \\r
128                 asm volatile(   "MOV    _pxCurrentTCB, W0               \n"     /* Restore the stack pointer for the task. */                           \\r
129                                                 "MOV    [W0], W15                               \n"                                                                                                                             \\r
130                                                 "POP    W0                                              \n"     /* Restore the critical nesting counter for the task. */        \\r
131                                                 "MOV    W0, _uxCriticalNesting  \n"                                                                                                                             \\r
132                                                 "POP    PSVPAG                                  \n"                                                                                                                             \\r
133                                                 "POP    CORCON                                  \n"                                                                                                                             \\r
134                                                 "POP    DOENDH                                  \n"                                                                                                                             \\r
135                                                 "POP    DOENDL                                  \n"                                                                                                                             \\r
136                                                 "POP    DOSTARTH                                \n"                                                                                                                             \\r
137                                                 "POP    DOSTARTL                                \n"                                                                                                                             \\r
138                                                 "POP    DCOUNT                                  \n"                                                                                                                             \\r
139                                                 "POP    ACCBU                                   \n"                                                                                                                             \\r
140                                                 "POP    ACCBH                                   \n"                                                                                                                             \\r
141                                                 "POP    ACCBL                                   \n"                                                                                                                             \\r
142                                                 "POP    ACCAU                                   \n"                                                                                                                             \\r
143                                                 "POP    ACCAH                                   \n"                                                                                                                             \\r
144                                                 "POP    ACCAL                                   \n"                                                                                                                             \\r
145                                                 "POP    TBLPAG                                  \n"                                                                                                                             \\r
146                                                 "POP    RCOUNT                                  \n"     /* Restore the registers from the stack. */                                     \\r
147                                                 "POP    W14                                             \n"                                                                                                                             \\r
148                                                 "POP.D  W12                                             \n"                                                                                                                             \\r
149                                                 "POP.D  W10                                             \n"                                                                                                                             \\r
150                                                 "POP.D  W8                                              \n"                                                                                                                             \\r
151                                                 "POP.D  W6                                              \n"                                                                                                                             \\r
152                                                 "POP.D  W4                                              \n"                                                                                                                             \\r
153                                                 "POP.D  W2                                              \n"                                                                                                                             \\r
154                                                 "POP.D  W0                                              \n"                                                                                                                             \\r
155                                                 "POP    SR                                                " );\r
156 \r
157 #endif /* MPLAB_DSPIC_PORT */\r
158 \r
159 /*\r
160  * Setup the timer used to generate the tick interrupt.\r
161  */\r
162 static void prvSetupTimerInterrupt( void );\r
163 \r
164 /* \r
165  * See header file for description. \r
166  */\r
167 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
168 {\r
169 unsigned short usCode;\r
170 portBASE_TYPE i;\r
171 \r
172 const portSTACK_TYPE xInitialStack[] = \r
173 {\r
174         0x1111, /* W1 */\r
175         0x2222, /* W2 */\r
176         0x3333, /* W3 */\r
177         0x4444, /* W4 */\r
178         0x5555, /* W5 */\r
179         0x6666, /* W6 */\r
180         0x7777, /* W7 */\r
181         0x8888, /* W8 */\r
182         0x9999, /* W9 */\r
183         0xaaaa, /* W10 */\r
184         0xbbbb, /* W11 */\r
185         0xcccc, /* W12 */\r
186         0xdddd, /* W13 */\r
187         0xeeee, /* W14 */\r
188         0xcdce, /* RCOUNT */\r
189         0xabac, /* TBLPAG */\r
190 \r
191         /* dsPIC specific registers. */\r
192         #ifdef MPLAB_DSPIC_PORT\r
193                 0x0202, /* ACCAL */\r
194                 0x0303, /* ACCAH */\r
195                 0x0404, /* ACCAU */\r
196                 0x0505, /* ACCBL */\r
197                 0x0606, /* ACCBH */\r
198                 0x0707, /* ACCBU */\r
199                 0x0808, /* DCOUNT */\r
200                 0x090a, /* DOSTARTL */\r
201                 0x1010, /* DOSTARTH */\r
202                 0x1110, /* DOENDL */\r
203                 0x1212, /* DOENDH */\r
204         #endif\r
205 };\r
206 \r
207         /* Setup the stack as if a yield had occurred.\r
208 \r
209         Save the low bytes of the program counter. */\r
210         usCode = ( unsigned short ) pxCode;\r
211         *pxTopOfStack = ( portSTACK_TYPE ) usCode;\r
212         pxTopOfStack++;\r
213 \r
214         /* Save the high byte of the program counter.  This will always be zero\r
215         here as it is passed in a 16bit pointer.  If the address is greater than\r
216         16 bits then the pointer will point to a jump table. */\r
217         *pxTopOfStack = ( portSTACK_TYPE ) 0;\r
218         pxTopOfStack++;\r
219 \r
220         /* Status register with interrupts enabled. */\r
221         *pxTopOfStack = portINITIAL_SR;\r
222         pxTopOfStack++;\r
223 \r
224         /* Parameters are passed in W0. */\r
225         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;\r
226         pxTopOfStack++;\r
227 \r
228         for( i = 0; i < ( sizeof( xInitialStack ) / sizeof( portSTACK_TYPE ) ); i++ )\r
229         {\r
230                 *pxTopOfStack = xInitialStack[ i ];\r
231                 pxTopOfStack++;\r
232         }\r
233 \r
234         *pxTopOfStack = CORCON;\r
235         pxTopOfStack++;\r
236         *pxTopOfStack = PSVPAG;\r
237         pxTopOfStack++;\r
238 \r
239         /* Finally the critical nesting depth. */\r
240         *pxTopOfStack = 0x00;\r
241         pxTopOfStack++;\r
242 \r
243         return pxTopOfStack;\r
244 }\r
245 /*-----------------------------------------------------------*/\r
246 \r
247 portBASE_TYPE xPortStartScheduler( void )\r
248 {\r
249         /* Setup a timer for the tick ISR. */\r
250         prvSetupTimerInterrupt(); \r
251 \r
252         /* Restore the context of the first task to run. */\r
253         portRESTORE_CONTEXT();\r
254 \r
255         /* Simulate the end of the yield function. */\r
256         asm volatile ( "return" );\r
257 \r
258         /* Should not reach here. */\r
259         return pdTRUE;\r
260 }\r
261 /*-----------------------------------------------------------*/\r
262 \r
263 void vPortEndScheduler( void )\r
264 {\r
265         /* It is unlikely that the scheduler for the PIC port will get stopped\r
266         once running.  If required disable the tick interrupt here, then return \r
267         to xPortStartScheduler(). */\r
268 }\r
269 /*-----------------------------------------------------------*/\r
270 \r
271 /*\r
272  * Setup a timer for a regular tick.\r
273  */\r
274 static void prvSetupTimerInterrupt( void )\r
275 {\r
276 const unsigned long ulCompareMatch = ( ( configCPU_CLOCK_HZ / portTIMER_PRESCALE ) / configTICK_RATE_HZ ) - 1;\r
277 \r
278         /* Prescale of 8. */\r
279         T1CON = 0;\r
280         TMR1 = 0;\r
281 \r
282         PR1 = ( unsigned short ) ulCompareMatch;\r
283 \r
284         /* Setup timer 1 interrupt priority. */\r
285         IPC0bits.T1IP = configKERNEL_INTERRUPT_PRIORITY;\r
286 \r
287         /* Clear the interrupt as a starting condition. */\r
288         IFS0bits.T1IF = 0;\r
289 \r
290         /* Enable the interrupt. */\r
291         IEC0bits.T1IE = 1;\r
292 \r
293         /* Setup the prescale value. */\r
294         T1CONbits.TCKPS0 = 1;\r
295         T1CONbits.TCKPS1 = 0;\r
296 \r
297         /* Start the timer. */\r
298         T1CONbits.TON = 1;\r
299 }\r
300 /*-----------------------------------------------------------*/\r
301 \r
302 void vPortEnterCritical( void )\r
303 {\r
304         portDISABLE_INTERRUPTS();\r
305         uxCriticalNesting++;\r
306 }\r
307 /*-----------------------------------------------------------*/\r
308 \r
309 void vPortExitCritical( void )\r
310 {\r
311         uxCriticalNesting--;\r
312         if( uxCriticalNesting == 0 )\r
313         {\r
314                 portENABLE_INTERRUPTS();\r
315         }\r
316 }\r
317 /*-----------------------------------------------------------*/\r
318 \r
319 void __attribute__((__interrupt__, auto_psv)) _T1Interrupt( void )\r
320 {\r
321         /* Clear the timer interrupt. */\r
322         IFS0bits.T1IF = 0;\r
323 \r
324         vTaskIncrementTick();\r
325 \r
326         #if configUSE_PREEMPTION == 1\r
327                 portYIELD();\r
328         #endif\r
329 }\r