]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/WizC/PIC18/portmacro.h
Update version numbers in preparation for new release.
[freertos] / FreeRTOS / Source / portable / WizC / PIC18 / portmacro.h
1 /*\r
2     FreeRTOS V8.2.2 - Copyright (C) 2015 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     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /*\r
71 Changes from V3.0.0\r
72 \r
73 Changes from V3.0.1\r
74 */\r
75 #ifndef PORTMACRO_H\r
76 #define PORTMACRO_H\r
77 \r
78 #if !defined(_SERIES) || _SERIES != 18\r
79         #error "WizC supports FreeRTOS on the Microchip PIC18-series only"\r
80 #endif\r
81 \r
82 #if !defined(QUICKCALL) || QUICKCALL != 1\r
83         #error "QuickCall must be enabled (see ProjectOptions/Optimisations)"\r
84 #endif\r
85 \r
86 #include <stddef.h>\r
87 #include <pic.h>\r
88 \r
89 #define portCHAR                char\r
90 #define portFLOAT               float\r
91 #define portDOUBLE              portFLOAT\r
92 #define portLONG                long\r
93 #define portSHORT               short\r
94 #define portSTACK_TYPE  uint8_t\r
95 #define portBASE_TYPE   char\r
96 \r
97 typedef portSTACK_TYPE StackType_t;\r
98 typedef signed char BaseType_t;\r
99 typedef unsigned char UBaseType_t;\r
100 \r
101 \r
102 #if( configUSE_16_BIT_TICKS == 1 )\r
103         typedef uint16_t TickType_t;\r
104         #define portMAX_DELAY ( TickType_t )    ( 0xFFFF )\r
105 #else\r
106         typedef uint32_t TickType_t;\r
107         #define portMAX_DELAY ( TickType_t )    ( 0xFFFFFFFF )\r
108 #endif\r
109 \r
110 #define portBYTE_ALIGNMENT                      1\r
111 \r
112 /*-----------------------------------------------------------*/\r
113 \r
114 /*\r
115  * Constant used for context switch macro when we require the interrupt\r
116  * enable state to be forced when the interrupted task is switched back in.\r
117  */\r
118 #define portINTERRUPTS_FORCED                           (0x01)\r
119 \r
120 /*\r
121  * Constant used for context switch macro when we require the interrupt\r
122  * enable state to be unchanged when the interrupted task is switched back in.\r
123  */\r
124 #define portINTERRUPTS_UNCHANGED                        (0x00)\r
125 \r
126 /* Initial interrupt enable state for newly created tasks.  This value is\r
127  * used when a task switches in for the first time.\r
128  */\r
129 #define portINTERRUPTS_INITIAL_STATE            (portINTERRUPTS_FORCED)\r
130 \r
131 /*\r
132  * Macros to modify the global interrupt enable bit in INTCON.\r
133  */\r
134 #define portDISABLE_INTERRUPTS()        \\r
135         do                                                              \\r
136         {                                                               \\r
137                 bGIE=0;                                         \\r
138         } while(bGIE)   // MicroChip recommends this check!\r
139 \r
140 #define portENABLE_INTERRUPTS()         \\r
141         do                                                              \\r
142         {                                                               \\r
143                 bGIE=1;                                         \\r
144         } while(0)\r
145 \r
146 /*-----------------------------------------------------------*/\r
147 \r
148 /*\r
149  * Critical section macros.\r
150  */\r
151 extern uint8_t ucCriticalNesting;\r
152 \r
153 #define portNO_CRITICAL_SECTION_NESTING         ( ( uint8_t ) 0 )\r
154 \r
155 #define portENTER_CRITICAL()                                                                            \\r
156         do                                                                                                                              \\r
157         {                                                                                                                               \\r
158                 portDISABLE_INTERRUPTS();                                                                       \\r
159                                                                                                                                         \\r
160                 /*                                                                                                                      \\r
161                  * Now interrupts are disabled ucCriticalNesting                        \\r
162                  * can be accessed directly. Increment                                          \\r
163                  * ucCriticalNesting to keep a count of how                                     \\r
164                  * many times portENTER_CRITICAL() has been called.             \\r
165                  */                                                                                                                     \\r
166                 ucCriticalNesting++;                                                                            \\r
167         } while(0)\r
168 \r
169 #define portEXIT_CRITICAL()                                                                                     \\r
170         do                                                                                                                              \\r
171         {                                                                                                                               \\r
172                 if(ucCriticalNesting > portNO_CRITICAL_SECTION_NESTING)         \\r
173                 {                                                                                                                       \\r
174                         /*                                                                                                              \\r
175                          * Decrement the nesting count as we are leaving a              \\r
176                          * critical section.                                                                    \\r
177                          */                                                                                                             \\r
178                         ucCriticalNesting--;                                                                    \\r
179                 }                                                                                                                       \\r
180                                                                                                                                         \\r
181                 /*                                                                                                                      \\r
182                  * If the nesting level has reached zero then                           \\r
183                  * interrupts should be re-enabled.                                                     \\r
184                  */                                                                                                                     \\r
185                 if( ucCriticalNesting == portNO_CRITICAL_SECTION_NESTING )      \\r
186                 {                                                                                                                       \\r
187                         portENABLE_INTERRUPTS();                                                                \\r
188                 }                                                                                                                       \\r
189         } while(0)\r
190 \r
191 /*-----------------------------------------------------------*/\r
192 \r
193 /*\r
194  * The minimal stacksize is calculated on the first reference of\r
195  * portMINIMAL_STACK_SIZE. Some input to this calculation is\r
196  * compiletime determined, other input is port-defined (see port.c)\r
197  */\r
198 extern uint16_t usPortCALCULATE_MINIMAL_STACK_SIZE( void );\r
199 extern uint16_t usCalcMinStackSize;\r
200 \r
201 #define portMINIMAL_STACK_SIZE                                  \\r
202         ((usCalcMinStackSize == 0)                                      \\r
203                 ? usPortCALCULATE_MINIMAL_STACK_SIZE()  \\r
204                 : usCalcMinStackSize )\r
205 \r
206 /*\r
207  * WizC uses a downgrowing stack\r
208  */\r
209 #define portSTACK_GROWTH                        ( -1 )\r
210 \r
211 /*-----------------------------------------------------------*/\r
212 \r
213 /*\r
214  * Macro's that pushes all the registers that make up the context of a task onto\r
215  * the stack, then saves the new top of stack into the TCB. TOSU and TBLPTRU\r
216  * are only saved/restored on devices with more than 64kB (32k Words) ROM.\r
217  *\r
218  * The stackpointer is helt by WizC in FSR2 and points to the first free byte.\r
219  * WizC uses a "downgrowing" stack. There is no framepointer.\r
220  *\r
221  * We keep track of the interruptstatus using ucCriticalNesting. When this\r
222  * value equals zero, interrupts have to be enabled upon exit from the\r
223  * portRESTORE_CONTEXT macro.\r
224  *\r
225  * If this is called from an ISR then the interrupt enable bits must have been\r
226  * set for the ISR to ever get called.  Therefore we want to save\r
227  * ucCriticalNesting with value zero. This means the interrupts will again be\r
228  * re-enabled when the interrupted task is switched back in.\r
229  *\r
230  * If this is called from a manual context switch (i.e. from a call to yield),\r
231  * then we want to keep the current value of ucCritialNesting so it is restored\r
232  * with its current value. This allows a yield from within a critical section.\r
233  *\r
234  * The compiler uses some locations at the bottom of RAM for temporary\r
235  * storage. The compiler may also have been instructed to optimize\r
236  * function-parameters and local variables to global storage. The compiler\r
237  * uses an area called LocOpt for this wizC feature.\r
238  * The total overheadstorage has to be saved in it's entirety as part of\r
239  * a task context. These macro's store/restore from data address 0x0000 to\r
240  * (OVERHEADPAGE0-LOCOPTSIZE+MAXLOCOPTSIZE - 1).\r
241  * OVERHEADPAGE0, LOCOPTSIZE and MAXLOCOPTSIZE are compiler-generated\r
242  * assembler definitions.\r
243  */\r
244 \r
245 #define portSAVE_CONTEXT( ucInterruptForced )                                           \\r
246         do                                                                                                                              \\r
247         {                                                                                                                               \\r
248                 portDISABLE_INTERRUPTS();                                                                       \\r
249                                                                                                                                         \\r
250                 _Pragma("asm")                                                                                          \\r
251                         ;                                                                                                               \\r
252                         ; Push the relevant SFR's onto the task's stack                 \\r
253                         ;                                                                                                               \\r
254                         movff   STATUS,POSTDEC2                                                                 \\r
255                         movff   WREG,POSTDEC2                                                                   \\r
256                         movff   BSR,POSTDEC2                                                                    \\r
257                         movff   PRODH,POSTDEC2                                                                  \\r
258                         movff   PRODL,POSTDEC2                                                                  \\r
259                         movff   FSR0H,POSTDEC2                                                                  \\r
260                         movff   FSR0L,POSTDEC2                                                                  \\r
261                         movff   FSR1H,POSTDEC2                                                                  \\r
262                         movff   FSR1L,POSTDEC2                                                                  \\r
263                         movff   TABLAT,POSTDEC2                                                                 \\r
264                         if __ROMSIZE > 0x8000                                                                   \\r
265                                 movff   TBLPTRU,POSTDEC2                                                        \\r
266                         endif                                                                                                   \\r
267                         movff   TBLPTRH,POSTDEC2                                                                \\r
268                         movff   TBLPTRL,POSTDEC2                                                                \\r
269                         if __ROMSIZE > 0x8000                                                                   \\r
270                                 movff   PCLATU,POSTDEC2                                                         \\r
271                         endif                                                                                                   \\r
272                         movff   PCLATH,POSTDEC2                                                                 \\r
273                         ;                                                                                                               \\r
274                         ; Store the compiler-scratch-area as described above.   \\r
275                         ;                                                                                                               \\r
276                         movlw   OVERHEADPAGE0-LOCOPTSIZE+MAXLOCOPTSIZE                  \\r
277                         clrf    FSR0L,ACCESS                                                                    \\r
278                         clrf    FSR0H,ACCESS                                                                    \\r
279                 _rtos_S1:                                                                                                       \\r
280                         movff   POSTINC0,POSTDEC2                                                               \\r
281                         decfsz  WREG,W,ACCESS                                                                   \\r
282                         SMARTJUMP _rtos_S1                                                                              \\r
283                         ;                                                                                                               \\r
284                         ; Save the pic call/return-stack belonging to the               \\r
285                         ; current task by copying it to the task's software-    \\r
286                         ; stack. We save the hardware stack pointer (which              \\r
287                         ; is the number of addresses on the stack) in the               \\r
288                         ; W-register first because we need it later and it              \\r
289                         ; is modified in the save-loop by executing pop's.              \\r
290                         ; After the loop the W-register is stored on the                \\r
291                         ; stack, too.                                                                                   \\r
292                         ;                                                                                                               \\r
293                         movf    STKPTR,W,ACCESS                                                                 \\r
294                         bz              _rtos_s3                                                                                \\r
295                 _rtos_S2:                                                                                                       \\r
296                         if __ROMSIZE > 0x8000                                                                   \\r
297                                 movff   TOSU,POSTDEC2                                                           \\r
298                         endif                                                                                                   \\r
299                         movff   TOSH,POSTDEC2                                                                   \\r
300                         movff   TOSL,POSTDEC2                                                                   \\r
301                         pop                                                                                                             \\r
302                         tstfsz  STKPTR,ACCESS                                                                   \\r
303                         SMARTJUMP _rtos_S2                                                                              \\r
304                 _rtos_s3:                                                                                                       \\r
305                         movwf   POSTDEC2,ACCESS                                                                 \\r
306                         ;                                                                                                               \\r
307                         ; Next the value for ucCriticalNesting used by the              \\r
308                         ; task is stored on the stack. When                                             \\r
309                         ; (ucInterruptForced == portINTERRUPTS_FORCED), we save \\r
310                         ; it as 0 (portNO_CRITICAL_SECTION_NESTING).                    \\r
311                         ;                                                                                                               \\r
312                         if ucInterruptForced == portINTERRUPTS_FORCED                   \\r
313                                 clrf POSTDEC2,ACCESS                                                            \\r
314                         else                                                                                                    \\r
315                                 movff   ucCriticalNesting,POSTDEC2                                      \\r
316                         endif                                                                                                   \\r
317                         ;                                                                                                               \\r
318                         ; Save the new top of the software stack in the TCB.    \\r
319                         ;                                                                                                               \\r
320                         movff   pxCurrentTCB,FSR0L                                                              \\r
321                         movff   pxCurrentTCB+1,FSR0H                                                    \\r
322                         movff   FSR2L,POSTINC0                                                                  \\r
323                         movff   FSR2H,POSTINC0                                                                  \\r
324                 _Pragma("asmend")                                                                                       \\r
325         } while(0)\r
326 \r
327 /************************************************************/\r
328 \r
329 /*\r
330  * This is the reverse of portSAVE_CONTEXT.\r
331  */\r
332 #define portRESTORE_CONTEXT()                                                                           \\r
333         do                                                                                                                              \\r
334         {                                                                                                                               \\r
335                 _Pragma("asm")                                                                                          \\r
336                         ;                                                                                                               \\r
337                         ; Set FSR0 to point to pxCurrentTCB->pxTopOfStack.              \\r
338                         ;                                                                                                               \\r
339                         movff   pxCurrentTCB,FSR0L                                                              \\r
340                         movff   pxCurrentTCB+1,FSR0H                                                    \\r
341                         ;                                                                                                               \\r
342                         ; De-reference FSR0 to set the address it holds into    \\r
343                         ; FSR2 (i.e. *( pxCurrentTCB->pxTopOfStack ) ). FSR2    \\r
344                         ; is used by wizC as stackpointer.                                              \\r
345                         ;                                                                                                               \\r
346                         movff   POSTINC0,FSR2L                                                                  \\r
347                         movff   POSTINC0,FSR2H                                                                  \\r
348                         ;                                                                                                               \\r
349                         ; Next, the value for ucCriticalNesting used by the             \\r
350                         ; task is retrieved from the stack.                                             \\r
351                         ;                                                                                                               \\r
352                         movff   PREINC2,ucCriticalNesting                                               \\r
353                         ;                                                                                                               \\r
354                         ; Rebuild the pic call/return-stack. The number of              \\r
355                         ; return addresses is the next item on the task stack.  \\r
356                         ; Save this number in PRODL. Then fetch the addresses   \\r
357                         ; and store them on the hardwarestack.                                  \\r
358                         ; The datasheets say we can't use movff here...                 \\r
359                         ;                                                                                                               \\r
360                         movff   PREINC2,PRODL   // Use PRODL as tempregister    \\r
361                         clrf    STKPTR,ACCESS                                                                   \\r
362                 _rtos_R1:                                                                                                       \\r
363                         push                                                                                                    \\r
364                         movf    PREINC2,W,ACCESS                                                                \\r
365                         movwf   TOSL,ACCESS                                                                             \\r
366                         movf    PREINC2,W,ACCESS                                                                \\r
367                         movwf   TOSH,ACCESS                                                                             \\r
368                         if __ROMSIZE > 0x8000                                                                   \\r
369                                 movf    PREINC2,W,ACCESS                                                        \\r
370                                 movwf   TOSU,ACCESS                                                                     \\r
371                         else                                                                                                    \\r
372                                 clrf    TOSU,ACCESS                                                                     \\r
373                         endif                                                                                                   \\r
374                         decfsz  PRODL,F,ACCESS                                                                  \\r
375                         SMARTJUMP _rtos_R1                                                                              \\r
376                         ;                                                                                                               \\r
377                         ; Restore the compiler's working storage area to page 0 \\r
378                         ;                                                                                                               \\r
379                         movlw   OVERHEADPAGE0-LOCOPTSIZE+MAXLOCOPTSIZE                  \\r
380                         movwf   FSR0L,ACCESS                                                                    \\r
381                         clrf    FSR0H,ACCESS                                                                    \\r
382                 _rtos_R2:                                                                                                       \\r
383                         decf    FSR0L,F,ACCESS                                                                  \\r
384                         movff   PREINC2,INDF0                                                                   \\r
385                         tstfsz  FSR0L,ACCESS                                                                    \\r
386                         SMARTJUMP _rtos_R2                                                                              \\r
387                         ;                                                                                                               \\r
388                         ; Restore the sfr's forming the tasks context.                  \\r
389                         ; We cannot yet restore bsr, w and status because               \\r
390                         ; we need these registers for a final test.                             \\r
391                         ;                                                                                                               \\r
392                         movff   PREINC2,PCLATH                                                                  \\r
393                         if __ROMSIZE > 0x8000                                                                   \\r
394                                 movff   PREINC2,PCLATU                                                          \\r
395                         else                                                                                                    \\r
396                                 clrf    PCLATU,ACCESS                                                           \\r
397                         endif                                                                                                   \\r
398                         movff   PREINC2,TBLPTRL                                                                 \\r
399                         movff   PREINC2,TBLPTRH                                                                 \\r
400                         if __ROMSIZE > 0x8000                                                                   \\r
401                                 movff   PREINC2,TBLPTRU                                                         \\r
402                         else                                                                                                    \\r
403                                 clrf    TBLPTRU,ACCESS                                                          \\r
404                         endif                                                                                                   \\r
405                         movff   PREINC2,TABLAT                                                                  \\r
406                         movff   PREINC2,FSR1L                                                                   \\r
407                         movff   PREINC2,FSR1H                                                                   \\r
408                         movff   PREINC2,FSR0L                                                                   \\r
409                         movff   PREINC2,FSR0H                                                                   \\r
410                         movff   PREINC2,PRODL                                                                   \\r
411                         movff   PREINC2,PRODH                                                                   \\r
412                         ;                                                                                                               \\r
413                         ; The return from portRESTORE_CONTEXT() depends on              \\r
414                         ; the value of ucCriticalNesting. When it is zero,              \\r
415                         ; interrupts need to be enabled. This is done via a             \\r
416                         ; retfie instruction because we need the                                \\r
417                         ; interrupt-enabling and the return to the restored             \\r
418                         ; task to be uninterruptable.                                                   \\r
419                         ; Because bsr, status and W are affected by the test    \\r
420                         ; they are restored after the test.                                             \\r
421                         ;                                                                                                               \\r
422                         movlb   ucCriticalNesting>>8                                                    \\r
423                         tstfsz  ucCriticalNesting,BANKED                                                \\r
424                         SMARTJUMP _rtos_R4                                                                              \\r
425                 _rtos_R3:                                                                                                       \\r
426                         movff   PREINC2,BSR                                                                             \\r
427                         movff   PREINC2,WREG                                                                    \\r
428                         movff   PREINC2,STATUS                                                                  \\r
429                         retfie  0               ; Return enabling interrupts                    \\r
430                 _rtos_R4:                                                                                                       \\r
431                         movff   PREINC2,BSR                                                                             \\r
432                         movff   PREINC2,WREG                                                                    \\r
433                         movff   PREINC2,STATUS                                                                  \\r
434                         return  0               ; Return without affecting interrupts   \\r
435                 _Pragma("asmend")                                                                                       \\r
436         } while(0)\r
437 \r
438 /*-----------------------------------------------------------*/\r
439 \r
440 #define portTICK_PERIOD_MS      ( ( TickType_t ) 1000 / configTICK_RATE_HZ )\r
441 \r
442 /*-----------------------------------------------------------*/\r
443 \r
444 extern void vPortYield( void );\r
445 #define portYIELD()                             vPortYield()\r
446 \r
447 #define portNOP()       _Pragma("asm")                                                                  \\r
448                                                 nop                                                                                     \\r
449                                         _Pragma("asmend")\r
450 \r
451 /*-----------------------------------------------------------*/\r
452 \r
453 #define portTASK_FUNCTION( xFunction, pvParameters )            \\r
454         void pointed xFunction( void *pvParameters )            \\r
455         _Pragma(asmfunc xFunction)\r
456 \r
457 #define portTASK_FUNCTION_PROTO         portTASK_FUNCTION\r
458 /*-----------------------------------------------------------*/\r
459 \r
460 \r
461 #define volatile\r
462 #define register\r
463 \r
464 #endif /* PORTMACRO_H */\r
465 \r