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