]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/MPLAB/PIC24_dsPIC/port.c
Modify the GCC/AVR port to make use of the xTaskIncrementTick return value.
[freertos] / FreeRTOS / Source / portable / MPLAB / PIC24_dsPIC / port.c
1 /*\r
2     FreeRTOS V7.4.2 - Copyright (C) 2013 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 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
67     Integrity Systems, who sell the code with commercial support,\r
68     indemnification and middleware, under the OpenRTOS brand.\r
69 \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
71     engineered and independently SIL3 certified version for use in safety and\r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /*\r
76         Changes from V4.2.1\r
77 \r
78         + Introduced the configKERNEL_INTERRUPT_PRIORITY definition.\r
79 */\r
80 \r
81 /*-----------------------------------------------------------\r
82  * Implementation of functions defined in portable.h for the PIC24 port.\r
83  *----------------------------------------------------------*/\r
84 \r
85 /* Scheduler include files. */\r
86 #include "FreeRTOS.h"\r
87 #include "task.h"\r
88 \r
89 /* Hardware specifics. */\r
90 #define portBIT_SET 1\r
91 #define portTIMER_PRESCALE 8\r
92 #define portINITIAL_SR  0\r
93 \r
94 /* Defined for backward compatability with project created prior to\r
95 FreeRTOS.org V4.3.0. */\r
96 #ifndef configKERNEL_INTERRUPT_PRIORITY\r
97         #define configKERNEL_INTERRUPT_PRIORITY 1\r
98 #endif\r
99 \r
100 /* Use _T1Interrupt as the interrupt handler name if the application writer has\r
101 not provided their own. */\r
102 #ifndef configTICK_INTERRUPT_HANDLER\r
103         #define configTICK_INTERRUPT_HANDLER _T1Interrupt\r
104 #endif /* configTICK_INTERRUPT_HANDLER */\r
105 \r
106 /* The program counter is only 23 bits. */\r
107 #define portUNUSED_PR_BITS      0x7f\r
108 \r
109 /* Records the nesting depth of calls to portENTER_CRITICAL(). */\r
110 unsigned portBASE_TYPE uxCriticalNesting = 0xef;\r
111 \r
112 #if configKERNEL_INTERRUPT_PRIORITY != 1\r
113         #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
114 #endif\r
115 \r
116 #if defined( __PIC24E__ ) || defined ( __PIC24F__ ) || defined( __PIC24FK__ ) || defined( __PIC24H__ )\r
117 \r
118     #ifdef __HAS_EDS__\r
119                 #define portRESTORE_CONTEXT()                                                                                                                                                                           \\r
120                                         asm volatile(   "MOV    _pxCurrentTCB, W0               \n"     /* Restore the stack pointer for the task. */           \\r
121                                                         "MOV    [W0], W15                               \n"                                                                                                                             \\r
122                                                         "POP    W0                                              \n"     /* Restore the critical nesting counter for the task. */        \\r
123                                                         "MOV    W0, _uxCriticalNesting  \n"                                                                                                                             \\r
124                                                         "POP    DSWPAG                                  \n"                                                                                                                             \\r
125                                                         "POP    DSRPAG                                  \n"                                                                                                                             \\r
126                                                         "POP    CORCON                                  \n"                                                                                                                             \\r
127                                                         "POP    TBLPAG                                  \n"                                                                                                                             \\r
128                                                         "POP    RCOUNT                                  \n"     /* Restore the registers from the stack. */                                     \\r
129                                                         "POP    W14                                             \n"                                                                                                                             \\r
130                                                         "POP.D  W12                                             \n"                                                                                                                             \\r
131                                                         "POP.D  W10                                             \n"                                                                                                                             \\r
132                                                         "POP.D  W8                                              \n"                                                                                                                             \\r
133                                                         "POP.D  W6                                              \n"                                                                                                                             \\r
134                                                         "POP.D  W4                                              \n"                                                                                                                             \\r
135                                                         "POP.D  W2                                              \n"                                                                                                                             \\r
136                                                         "POP.D  W0                                              \n"                                                                                                                             \\r
137                                                         "POP    SR                                                " );\r
138         #else /* __HAS_EDS__ */\r
139                 #define portRESTORE_CONTEXT()                                                                                                                                                                           \\r
140                         asm volatile(   "MOV    _pxCurrentTCB, W0               \n"     /* Restore the stack pointer for the task. */                           \\r
141                                                         "MOV    [W0], W15                               \n"                                                                                                                             \\r
142                                                         "POP    W0                                              \n"     /* Restore the critical nesting counter for the task. */        \\r
143                                                         "MOV    W0, _uxCriticalNesting  \n"                                                                                                                             \\r
144                                                         "POP    PSVPAG                                  \n"                                                                                                                             \\r
145                                                         "POP    CORCON                                  \n"                                                                                                                             \\r
146                                                         "POP    TBLPAG                                  \n"                                                                                                                             \\r
147                                                         "POP    RCOUNT                                  \n"     /* Restore the registers from the stack. */                                     \\r
148                                                         "POP    W14                                             \n"                                                                                                                             \\r
149                                                         "POP.D  W12                                             \n"                                                                                                                             \\r
150                                                         "POP.D  W10                                             \n"                                                                                                                             \\r
151                                                         "POP.D  W8                                              \n"                                                                                                                             \\r
152                                                         "POP.D  W6                                              \n"                                                                                                                             \\r
153                                                         "POP.D  W4                                              \n"                                                                                                                             \\r
154                                                         "POP.D  W2                                              \n"                                                                                                                             \\r
155                                                         "POP.D  W0                                              \n"                                                                                                                             \\r
156                                                         "POP    SR                                                " );\r
157                 #endif /* __HAS_EDS__ */\r
158 #endif /* MPLAB_PIC24_PORT */\r
159 \r
160 #if defined( __dsPIC30F__ ) || defined( __dsPIC33F__ )\r
161 \r
162         #define portRESTORE_CONTEXT()                                                                                                                                                                           \\r
163                 asm volatile(   "MOV    _pxCurrentTCB, W0               \n"     /* Restore the stack pointer for the task. */                           \\r
164                                                 "MOV    [W0], W15                               \n"                                                                                                                             \\r
165                                                 "POP    W0                                              \n"     /* Restore the critical nesting counter for the task. */        \\r
166                                                 "MOV    W0, _uxCriticalNesting  \n"                                                                                                                             \\r
167                                                 "POP    PSVPAG                                  \n"                                                                                                                             \\r
168                                                 "POP    CORCON                                  \n"                                                                                                                             \\r
169                                                 "POP    DOENDH                                  \n"                                                                                                                             \\r
170                                                 "POP    DOENDL                                  \n"                                                                                                                             \\r
171                                                 "POP    DOSTARTH                                \n"                                                                                                                             \\r
172                                                 "POP    DOSTARTL                                \n"                                                                                                                             \\r
173                                                 "POP    DCOUNT                                  \n"                                                                                                                             \\r
174                                                 "POP    ACCBU                                   \n"                                                                                                                             \\r
175                                                 "POP    ACCBH                                   \n"                                                                                                                             \\r
176                                                 "POP    ACCBL                                   \n"                                                                                                                             \\r
177                                                 "POP    ACCAU                                   \n"                                                                                                                             \\r
178                                                 "POP    ACCAH                                   \n"                                                                                                                             \\r
179                                                 "POP    ACCAL                                   \n"                                                                                                                             \\r
180                                                 "POP    TBLPAG                                  \n"                                                                                                                             \\r
181                                                 "POP    RCOUNT                                  \n"     /* Restore the registers from the stack. */                                     \\r
182                                                 "POP    W14                                             \n"                                                                                                                             \\r
183                                                 "POP.D  W12                                             \n"                                                                                                                             \\r
184                                                 "POP.D  W10                                             \n"                                                                                                                             \\r
185                                                 "POP.D  W8                                              \n"                                                                                                                             \\r
186                                                 "POP.D  W6                                              \n"                                                                                                                             \\r
187                                                 "POP.D  W4                                              \n"                                                                                                                             \\r
188                                                 "POP.D  W2                                              \n"                                                                                                                             \\r
189                                                 "POP.D  W0                                              \n"                                                                                                                             \\r
190                                                 "POP    SR                                                " );\r
191 \r
192 #endif /* MPLAB_DSPIC_PORT */\r
193 \r
194 #ifndef portRESTORE_CONTEXT\r
195         #error Unrecognised device selected\r
196 #endif\r
197 \r
198 /*\r
199  * Setup the timer used to generate the tick interrupt.\r
200  */\r
201 void vApplicationSetupTickTimerInterrupt( void );\r
202 \r
203 /*\r
204  * See header file for description.\r
205  */\r
206 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
207 {\r
208 unsigned short usCode;\r
209 unsigned portBASE_TYPE i;\r
210 \r
211 const portSTACK_TYPE xInitialStack[] =\r
212 {\r
213         0x1111, /* W1 */\r
214         0x2222, /* W2 */\r
215         0x3333, /* W3 */\r
216         0x4444, /* W4 */\r
217         0x5555, /* W5 */\r
218         0x6666, /* W6 */\r
219         0x7777, /* W7 */\r
220         0x8888, /* W8 */\r
221         0x9999, /* W9 */\r
222         0xaaaa, /* W10 */\r
223         0xbbbb, /* W11 */\r
224         0xcccc, /* W12 */\r
225         0xdddd, /* W13 */\r
226         0xeeee, /* W14 */\r
227         0xcdce, /* RCOUNT */\r
228         0xabac, /* TBLPAG */\r
229 \r
230         /* dsPIC specific registers. */\r
231         #ifdef MPLAB_DSPIC_PORT\r
232                 0x0202, /* ACCAL */\r
233                 0x0303, /* ACCAH */\r
234                 0x0404, /* ACCAU */\r
235                 0x0505, /* ACCBL */\r
236                 0x0606, /* ACCBH */\r
237                 0x0707, /* ACCBU */\r
238                 0x0808, /* DCOUNT */\r
239                 0x090a, /* DOSTARTL */\r
240                 0x1010, /* DOSTARTH */\r
241                 0x1110, /* DOENDL */\r
242                 0x1212, /* DOENDH */\r
243         #endif\r
244 };\r
245 \r
246         /* Setup the stack as if a yield had occurred.\r
247 \r
248         Save the low bytes of the program counter. */\r
249         usCode = ( unsigned short ) pxCode;\r
250         *pxTopOfStack = ( portSTACK_TYPE ) usCode;\r
251         pxTopOfStack++;\r
252 \r
253         /* Save the high byte of the program counter.  This will always be zero\r
254         here as it is passed in a 16bit pointer.  If the address is greater than\r
255         16 bits then the pointer will point to a jump table. */\r
256         *pxTopOfStack = ( portSTACK_TYPE ) 0;\r
257         pxTopOfStack++;\r
258 \r
259         /* Status register with interrupts enabled. */\r
260         *pxTopOfStack = portINITIAL_SR;\r
261         pxTopOfStack++;\r
262 \r
263         /* Parameters are passed in W0. */\r
264         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;\r
265         pxTopOfStack++;\r
266 \r
267         for( i = 0; i < ( sizeof( xInitialStack ) / sizeof( portSTACK_TYPE ) ); i++ )\r
268         {\r
269                 *pxTopOfStack = xInitialStack[ i ];\r
270                 pxTopOfStack++;\r
271         }\r
272 \r
273         *pxTopOfStack = CORCON;\r
274         pxTopOfStack++;\r
275 \r
276         #if defined(__HAS_EDS__)\r
277                 *pxTopOfStack = DSRPAG;\r
278                 pxTopOfStack++;\r
279                 *pxTopOfStack = DSWPAG;\r
280                 pxTopOfStack++;\r
281         #else /* __HAS_EDS__ */\r
282                 *pxTopOfStack = PSVPAG;\r
283                 pxTopOfStack++;\r
284         #endif /* __HAS_EDS__ */\r
285 \r
286         /* Finally the critical nesting depth. */\r
287         *pxTopOfStack = 0x00;\r
288         pxTopOfStack++;\r
289 \r
290         return pxTopOfStack;\r
291 }\r
292 /*-----------------------------------------------------------*/\r
293 \r
294 portBASE_TYPE xPortStartScheduler( void )\r
295 {\r
296         /* Setup a timer for the tick ISR. */\r
297         vApplicationSetupTickTimerInterrupt();\r
298 \r
299         /* Restore the context of the first task to run. */\r
300         portRESTORE_CONTEXT();\r
301 \r
302         /* Simulate the end of the yield function. */\r
303         asm volatile ( "return" );\r
304 \r
305         /* Should not reach here. */\r
306         return pdTRUE;\r
307 }\r
308 /*-----------------------------------------------------------*/\r
309 \r
310 void vPortEndScheduler( void )\r
311 {\r
312         /* It is unlikely that the scheduler for the PIC port will get stopped\r
313         once running.  If required disable the tick interrupt here, then return\r
314         to xPortStartScheduler(). */\r
315 }\r
316 /*-----------------------------------------------------------*/\r
317 \r
318 /*\r
319  * Setup a timer for a regular tick.\r
320  */\r
321 __attribute__(( weak )) void vApplicationSetupTickTimerInterrupt( void )\r
322 {\r
323 const unsigned long ulCompareMatch = ( ( configCPU_CLOCK_HZ / portTIMER_PRESCALE ) / configTICK_RATE_HZ ) - 1;\r
324 \r
325         /* Prescale of 8. */\r
326         T1CON = 0;\r
327         TMR1 = 0;\r
328 \r
329         PR1 = ( unsigned short ) ulCompareMatch;\r
330 \r
331         /* Setup timer 1 interrupt priority. */\r
332         IPC0bits.T1IP = configKERNEL_INTERRUPT_PRIORITY;\r
333 \r
334         /* Clear the interrupt as a starting condition. */\r
335         IFS0bits.T1IF = 0;\r
336 \r
337         /* Enable the interrupt. */\r
338         IEC0bits.T1IE = 1;\r
339 \r
340         /* Setup the prescale value. */\r
341         T1CONbits.TCKPS0 = 1;\r
342         T1CONbits.TCKPS1 = 0;\r
343 \r
344         /* Start the timer. */\r
345         T1CONbits.TON = 1;\r
346 }\r
347 /*-----------------------------------------------------------*/\r
348 \r
349 void vPortEnterCritical( void )\r
350 {\r
351         portDISABLE_INTERRUPTS();\r
352         uxCriticalNesting++;\r
353 }\r
354 /*-----------------------------------------------------------*/\r
355 \r
356 void vPortExitCritical( void )\r
357 {\r
358         uxCriticalNesting--;\r
359         if( uxCriticalNesting == 0 )\r
360         {\r
361                 portENABLE_INTERRUPTS();\r
362         }\r
363 }\r
364 /*-----------------------------------------------------------*/\r
365 \r
366 void __attribute__((__interrupt__, auto_psv)) configTICK_INTERRUPT_HANDLER( void )\r
367 {\r
368         /* Clear the timer interrupt. */\r
369         IFS0bits.T1IF = 0;\r
370 \r
371         if( xTaskIncrementTick() != pdFALSE )\r
372         {\r
373                 portYIELD();\r
374         }\r
375 }\r
376 \r