]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/WizC/PIC18/portmacro.h
Update version number in readiness for V10.2.0 release.
[freertos] / FreeRTOS / Source / portable / WizC / PIC18 / portmacro.h
1 /*\r
2  * FreeRTOS Kernel V10.2.0\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29 Changes from V3.0.0\r
30 \r
31 Changes from V3.0.1\r
32 */\r
33 #ifndef PORTMACRO_H\r
34 #define PORTMACRO_H\r
35 \r
36 #if !defined(_SERIES) || _SERIES != 18\r
37         #error "WizC supports FreeRTOS on the Microchip PIC18-series only"\r
38 #endif\r
39 \r
40 #if !defined(QUICKCALL) || QUICKCALL != 1\r
41         #error "QuickCall must be enabled (see ProjectOptions/Optimisations)"\r
42 #endif\r
43 \r
44 #include <stddef.h>\r
45 #include <pic.h>\r
46 \r
47 #define portCHAR                char\r
48 #define portFLOAT               float\r
49 #define portDOUBLE              portFLOAT\r
50 #define portLONG                long\r
51 #define portSHORT               short\r
52 #define portSTACK_TYPE  uint8_t\r
53 #define portBASE_TYPE   char\r
54 \r
55 typedef portSTACK_TYPE StackType_t;\r
56 typedef signed char BaseType_t;\r
57 typedef unsigned char UBaseType_t;\r
58 \r
59 \r
60 #if( configUSE_16_BIT_TICKS == 1 )\r
61         typedef uint16_t TickType_t;\r
62         #define portMAX_DELAY ( TickType_t )    ( 0xFFFF )\r
63 #else\r
64         typedef uint32_t TickType_t;\r
65         #define portMAX_DELAY ( TickType_t )    ( 0xFFFFFFFF )\r
66 #endif\r
67 \r
68 #define portBYTE_ALIGNMENT                      1\r
69 \r
70 /*-----------------------------------------------------------*/\r
71 \r
72 /*\r
73  * Constant used for context switch macro when we require the interrupt\r
74  * enable state to be forced when the interrupted task is switched back in.\r
75  */\r
76 #define portINTERRUPTS_FORCED                           (0x01)\r
77 \r
78 /*\r
79  * Constant used for context switch macro when we require the interrupt\r
80  * enable state to be unchanged when the interrupted task is switched back in.\r
81  */\r
82 #define portINTERRUPTS_UNCHANGED                        (0x00)\r
83 \r
84 /* Initial interrupt enable state for newly created tasks.  This value is\r
85  * used when a task switches in for the first time.\r
86  */\r
87 #define portINTERRUPTS_INITIAL_STATE            (portINTERRUPTS_FORCED)\r
88 \r
89 /*\r
90  * Macros to modify the global interrupt enable bit in INTCON.\r
91  */\r
92 #define portDISABLE_INTERRUPTS()        \\r
93         do                                                              \\r
94         {                                                               \\r
95                 bGIE=0;                                         \\r
96         } while(bGIE)   // MicroChip recommends this check!\r
97 \r
98 #define portENABLE_INTERRUPTS()         \\r
99         do                                                              \\r
100         {                                                               \\r
101                 bGIE=1;                                         \\r
102         } while(0)\r
103 \r
104 /*-----------------------------------------------------------*/\r
105 \r
106 /*\r
107  * Critical section macros.\r
108  */\r
109 extern uint8_t ucCriticalNesting;\r
110 \r
111 #define portNO_CRITICAL_SECTION_NESTING         ( ( uint8_t ) 0 )\r
112 \r
113 #define portENTER_CRITICAL()                                                                            \\r
114         do                                                                                                                              \\r
115         {                                                                                                                               \\r
116                 portDISABLE_INTERRUPTS();                                                                       \\r
117                                                                                                                                         \\r
118                 /*                                                                                                                      \\r
119                  * Now interrupts are disabled ucCriticalNesting                        \\r
120                  * can be accessed directly. Increment                                          \\r
121                  * ucCriticalNesting to keep a count of how                                     \\r
122                  * many times portENTER_CRITICAL() has been called.             \\r
123                  */                                                                                                                     \\r
124                 ucCriticalNesting++;                                                                            \\r
125         } while(0)\r
126 \r
127 #define portEXIT_CRITICAL()                                                                                     \\r
128         do                                                                                                                              \\r
129         {                                                                                                                               \\r
130                 if(ucCriticalNesting > portNO_CRITICAL_SECTION_NESTING)         \\r
131                 {                                                                                                                       \\r
132                         /*                                                                                                              \\r
133                          * Decrement the nesting count as we are leaving a              \\r
134                          * critical section.                                                                    \\r
135                          */                                                                                                             \\r
136                         ucCriticalNesting--;                                                                    \\r
137                 }                                                                                                                       \\r
138                                                                                                                                         \\r
139                 /*                                                                                                                      \\r
140                  * If the nesting level has reached zero then                           \\r
141                  * interrupts should be re-enabled.                                                     \\r
142                  */                                                                                                                     \\r
143                 if( ucCriticalNesting == portNO_CRITICAL_SECTION_NESTING )      \\r
144                 {                                                                                                                       \\r
145                         portENABLE_INTERRUPTS();                                                                \\r
146                 }                                                                                                                       \\r
147         } while(0)\r
148 \r
149 /*-----------------------------------------------------------*/\r
150 \r
151 /*\r
152  * The minimal stacksize is calculated on the first reference of\r
153  * portMINIMAL_STACK_SIZE. Some input to this calculation is\r
154  * compiletime determined, other input is port-defined (see port.c)\r
155  */\r
156 extern uint16_t usPortCALCULATE_MINIMAL_STACK_SIZE( void );\r
157 extern uint16_t usCalcMinStackSize;\r
158 \r
159 #define portMINIMAL_STACK_SIZE                                  \\r
160         ((usCalcMinStackSize == 0)                                      \\r
161                 ? usPortCALCULATE_MINIMAL_STACK_SIZE()  \\r
162                 : usCalcMinStackSize )\r
163 \r
164 /*\r
165  * WizC uses a downgrowing stack\r
166  */\r
167 #define portSTACK_GROWTH                        ( -1 )\r
168 \r
169 /*-----------------------------------------------------------*/\r
170 \r
171 /*\r
172  * Macro's that pushes all the registers that make up the context of a task onto\r
173  * the stack, then saves the new top of stack into the TCB. TOSU and TBLPTRU\r
174  * are only saved/restored on devices with more than 64kB (32k Words) ROM.\r
175  *\r
176  * The stackpointer is helt by WizC in FSR2 and points to the first free byte.\r
177  * WizC uses a "downgrowing" stack. There is no framepointer.\r
178  *\r
179  * We keep track of the interruptstatus using ucCriticalNesting. When this\r
180  * value equals zero, interrupts have to be enabled upon exit from the\r
181  * portRESTORE_CONTEXT macro.\r
182  *\r
183  * If this is called from an ISR then the interrupt enable bits must have been\r
184  * set for the ISR to ever get called.  Therefore we want to save\r
185  * ucCriticalNesting with value zero. This means the interrupts will again be\r
186  * re-enabled when the interrupted task is switched back in.\r
187  *\r
188  * If this is called from a manual context switch (i.e. from a call to yield),\r
189  * then we want to keep the current value of ucCritialNesting so it is restored\r
190  * with its current value. This allows a yield from within a critical section.\r
191  *\r
192  * The compiler uses some locations at the bottom of RAM for temporary\r
193  * storage. The compiler may also have been instructed to optimize\r
194  * function-parameters and local variables to global storage. The compiler\r
195  * uses an area called LocOpt for this wizC feature.\r
196  * The total overheadstorage has to be saved in it's entirety as part of\r
197  * a task context. These macro's store/restore from data address 0x0000 to\r
198  * (OVERHEADPAGE0-LOCOPTSIZE+MAXLOCOPTSIZE - 1).\r
199  * OVERHEADPAGE0, LOCOPTSIZE and MAXLOCOPTSIZE are compiler-generated\r
200  * assembler definitions.\r
201  */\r
202 \r
203 #define portSAVE_CONTEXT( ucInterruptForced )                                           \\r
204         do                                                                                                                              \\r
205         {                                                                                                                               \\r
206                 portDISABLE_INTERRUPTS();                                                                       \\r
207                                                                                                                                         \\r
208                 _Pragma("asm")                                                                                          \\r
209                         ;                                                                                                               \\r
210                         ; Push the relevant SFR's onto the task's stack                 \\r
211                         ;                                                                                                               \\r
212                         movff   STATUS,POSTDEC2                                                                 \\r
213                         movff   WREG,POSTDEC2                                                                   \\r
214                         movff   BSR,POSTDEC2                                                                    \\r
215                         movff   PRODH,POSTDEC2                                                                  \\r
216                         movff   PRODL,POSTDEC2                                                                  \\r
217                         movff   FSR0H,POSTDEC2                                                                  \\r
218                         movff   FSR0L,POSTDEC2                                                                  \\r
219                         movff   FSR1H,POSTDEC2                                                                  \\r
220                         movff   FSR1L,POSTDEC2                                                                  \\r
221                         movff   TABLAT,POSTDEC2                                                                 \\r
222                         if __ROMSIZE > 0x8000                                                                   \\r
223                                 movff   TBLPTRU,POSTDEC2                                                        \\r
224                         endif                                                                                                   \\r
225                         movff   TBLPTRH,POSTDEC2                                                                \\r
226                         movff   TBLPTRL,POSTDEC2                                                                \\r
227                         if __ROMSIZE > 0x8000                                                                   \\r
228                                 movff   PCLATU,POSTDEC2                                                         \\r
229                         endif                                                                                                   \\r
230                         movff   PCLATH,POSTDEC2                                                                 \\r
231                         ;                                                                                                               \\r
232                         ; Store the compiler-scratch-area as described above.   \\r
233                         ;                                                                                                               \\r
234                         movlw   OVERHEADPAGE0-LOCOPTSIZE+MAXLOCOPTSIZE                  \\r
235                         clrf    FSR0L,ACCESS                                                                    \\r
236                         clrf    FSR0H,ACCESS                                                                    \\r
237                 _rtos_S1:                                                                                                       \\r
238                         movff   POSTINC0,POSTDEC2                                                               \\r
239                         decfsz  WREG,W,ACCESS                                                                   \\r
240                         SMARTJUMP _rtos_S1                                                                              \\r
241                         ;                                                                                                               \\r
242                         ; Save the pic call/return-stack belonging to the               \\r
243                         ; current task by copying it to the task's software-    \\r
244                         ; stack. We save the hardware stack pointer (which              \\r
245                         ; is the number of addresses on the stack) in the               \\r
246                         ; W-register first because we need it later and it              \\r
247                         ; is modified in the save-loop by executing pop's.              \\r
248                         ; After the loop the W-register is stored on the                \\r
249                         ; stack, too.                                                                                   \\r
250                         ;                                                                                                               \\r
251                         movf    STKPTR,W,ACCESS                                                                 \\r
252                         bz              _rtos_s3                                                                                \\r
253                 _rtos_S2:                                                                                                       \\r
254                         if __ROMSIZE > 0x8000                                                                   \\r
255                                 movff   TOSU,POSTDEC2                                                           \\r
256                         endif                                                                                                   \\r
257                         movff   TOSH,POSTDEC2                                                                   \\r
258                         movff   TOSL,POSTDEC2                                                                   \\r
259                         pop                                                                                                             \\r
260                         tstfsz  STKPTR,ACCESS                                                                   \\r
261                         SMARTJUMP _rtos_S2                                                                              \\r
262                 _rtos_s3:                                                                                                       \\r
263                         movwf   POSTDEC2,ACCESS                                                                 \\r
264                         ;                                                                                                               \\r
265                         ; Next the value for ucCriticalNesting used by the              \\r
266                         ; task is stored on the stack. When                                             \\r
267                         ; (ucInterruptForced == portINTERRUPTS_FORCED), we save \\r
268                         ; it as 0 (portNO_CRITICAL_SECTION_NESTING).                    \\r
269                         ;                                                                                                               \\r
270                         if ucInterruptForced == portINTERRUPTS_FORCED                   \\r
271                                 clrf POSTDEC2,ACCESS                                                            \\r
272                         else                                                                                                    \\r
273                                 movff   ucCriticalNesting,POSTDEC2                                      \\r
274                         endif                                                                                                   \\r
275                         ;                                                                                                               \\r
276                         ; Save the new top of the software stack in the TCB.    \\r
277                         ;                                                                                                               \\r
278                         movff   pxCurrentTCB,FSR0L                                                              \\r
279                         movff   pxCurrentTCB+1,FSR0H                                                    \\r
280                         movff   FSR2L,POSTINC0                                                                  \\r
281                         movff   FSR2H,POSTINC0                                                                  \\r
282                 _Pragma("asmend")                                                                                       \\r
283         } while(0)\r
284 \r
285 /************************************************************/\r
286 \r
287 /*\r
288  * This is the reverse of portSAVE_CONTEXT.\r
289  */\r
290 #define portRESTORE_CONTEXT()                                                                           \\r
291         do                                                                                                                              \\r
292         {                                                                                                                               \\r
293                 _Pragma("asm")                                                                                          \\r
294                         ;                                                                                                               \\r
295                         ; Set FSR0 to point to pxCurrentTCB->pxTopOfStack.              \\r
296                         ;                                                                                                               \\r
297                         movff   pxCurrentTCB,FSR0L                                                              \\r
298                         movff   pxCurrentTCB+1,FSR0H                                                    \\r
299                         ;                                                                                                               \\r
300                         ; De-reference FSR0 to set the address it holds into    \\r
301                         ; FSR2 (i.e. *( pxCurrentTCB->pxTopOfStack ) ). FSR2    \\r
302                         ; is used by wizC as stackpointer.                                              \\r
303                         ;                                                                                                               \\r
304                         movff   POSTINC0,FSR2L                                                                  \\r
305                         movff   POSTINC0,FSR2H                                                                  \\r
306                         ;                                                                                                               \\r
307                         ; Next, the value for ucCriticalNesting used by the             \\r
308                         ; task is retrieved from the stack.                                             \\r
309                         ;                                                                                                               \\r
310                         movff   PREINC2,ucCriticalNesting                                               \\r
311                         ;                                                                                                               \\r
312                         ; Rebuild the pic call/return-stack. The number of              \\r
313                         ; return addresses is the next item on the task stack.  \\r
314                         ; Save this number in PRODL. Then fetch the addresses   \\r
315                         ; and store them on the hardwarestack.                                  \\r
316                         ; The datasheets say we can't use movff here...                 \\r
317                         ;                                                                                                               \\r
318                         movff   PREINC2,PRODL   // Use PRODL as tempregister    \\r
319                         clrf    STKPTR,ACCESS                                                                   \\r
320                 _rtos_R1:                                                                                                       \\r
321                         push                                                                                                    \\r
322                         movf    PREINC2,W,ACCESS                                                                \\r
323                         movwf   TOSL,ACCESS                                                                             \\r
324                         movf    PREINC2,W,ACCESS                                                                \\r
325                         movwf   TOSH,ACCESS                                                                             \\r
326                         if __ROMSIZE > 0x8000                                                                   \\r
327                                 movf    PREINC2,W,ACCESS                                                        \\r
328                                 movwf   TOSU,ACCESS                                                                     \\r
329                         else                                                                                                    \\r
330                                 clrf    TOSU,ACCESS                                                                     \\r
331                         endif                                                                                                   \\r
332                         decfsz  PRODL,F,ACCESS                                                                  \\r
333                         SMARTJUMP _rtos_R1                                                                              \\r
334                         ;                                                                                                               \\r
335                         ; Restore the compiler's working storage area to page 0 \\r
336                         ;                                                                                                               \\r
337                         movlw   OVERHEADPAGE0-LOCOPTSIZE+MAXLOCOPTSIZE                  \\r
338                         movwf   FSR0L,ACCESS                                                                    \\r
339                         clrf    FSR0H,ACCESS                                                                    \\r
340                 _rtos_R2:                                                                                                       \\r
341                         decf    FSR0L,F,ACCESS                                                                  \\r
342                         movff   PREINC2,INDF0                                                                   \\r
343                         tstfsz  FSR0L,ACCESS                                                                    \\r
344                         SMARTJUMP _rtos_R2                                                                              \\r
345                         ;                                                                                                               \\r
346                         ; Restore the sfr's forming the tasks context.                  \\r
347                         ; We cannot yet restore bsr, w and status because               \\r
348                         ; we need these registers for a final test.                             \\r
349                         ;                                                                                                               \\r
350                         movff   PREINC2,PCLATH                                                                  \\r
351                         if __ROMSIZE > 0x8000                                                                   \\r
352                                 movff   PREINC2,PCLATU                                                          \\r
353                         else                                                                                                    \\r
354                                 clrf    PCLATU,ACCESS                                                           \\r
355                         endif                                                                                                   \\r
356                         movff   PREINC2,TBLPTRL                                                                 \\r
357                         movff   PREINC2,TBLPTRH                                                                 \\r
358                         if __ROMSIZE > 0x8000                                                                   \\r
359                                 movff   PREINC2,TBLPTRU                                                         \\r
360                         else                                                                                                    \\r
361                                 clrf    TBLPTRU,ACCESS                                                          \\r
362                         endif                                                                                                   \\r
363                         movff   PREINC2,TABLAT                                                                  \\r
364                         movff   PREINC2,FSR1L                                                                   \\r
365                         movff   PREINC2,FSR1H                                                                   \\r
366                         movff   PREINC2,FSR0L                                                                   \\r
367                         movff   PREINC2,FSR0H                                                                   \\r
368                         movff   PREINC2,PRODL                                                                   \\r
369                         movff   PREINC2,PRODH                                                                   \\r
370                         ;                                                                                                               \\r
371                         ; The return from portRESTORE_CONTEXT() depends on              \\r
372                         ; the value of ucCriticalNesting. When it is zero,              \\r
373                         ; interrupts need to be enabled. This is done via a             \\r
374                         ; retfie instruction because we need the                                \\r
375                         ; interrupt-enabling and the return to the restored             \\r
376                         ; task to be uninterruptable.                                                   \\r
377                         ; Because bsr, status and W are affected by the test    \\r
378                         ; they are restored after the test.                                             \\r
379                         ;                                                                                                               \\r
380                         movlb   ucCriticalNesting>>8                                                    \\r
381                         tstfsz  ucCriticalNesting,BANKED                                                \\r
382                         SMARTJUMP _rtos_R4                                                                              \\r
383                 _rtos_R3:                                                                                                       \\r
384                         movff   PREINC2,BSR                                                                             \\r
385                         movff   PREINC2,WREG                                                                    \\r
386                         movff   PREINC2,STATUS                                                                  \\r
387                         retfie  0               ; Return enabling interrupts                    \\r
388                 _rtos_R4:                                                                                                       \\r
389                         movff   PREINC2,BSR                                                                             \\r
390                         movff   PREINC2,WREG                                                                    \\r
391                         movff   PREINC2,STATUS                                                                  \\r
392                         return  0               ; Return without affecting interrupts   \\r
393                 _Pragma("asmend")                                                                                       \\r
394         } while(0)\r
395 \r
396 /*-----------------------------------------------------------*/\r
397 \r
398 #define portTICK_PERIOD_MS      ( ( TickType_t ) 1000 / configTICK_RATE_HZ )\r
399 \r
400 /*-----------------------------------------------------------*/\r
401 \r
402 extern void vPortYield( void );\r
403 #define portYIELD()                             vPortYield()\r
404 \r
405 #define portNOP()       _Pragma("asm")                                                                  \\r
406                                                 nop                                                                                     \\r
407                                         _Pragma("asmend")\r
408 \r
409 /*-----------------------------------------------------------*/\r
410 \r
411 #define portTASK_FUNCTION( xFunction, pvParameters )            \\r
412         void pointed xFunction( void *pvParameters )            \\r
413         _Pragma(asmfunc xFunction)\r
414 \r
415 #define portTASK_FUNCTION_PROTO         portTASK_FUNCTION\r
416 /*-----------------------------------------------------------*/\r
417 \r
418 \r
419 #define volatile\r
420 #define register\r
421 \r
422 #endif /* PORTMACRO_H */\r
423 \r