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