2 FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS books - available as PDF or paperback *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
62 #if !defined(_SERIES) || _SERIES != 18
\r
63 #error "WizC supports FreeRTOS on the Microchip PIC18-series only"
\r
66 #if !defined(QUICKCALL) || QUICKCALL != 1
\r
67 #error "QuickCall must be enabled (see ProjectOptions/Optimisations)"
\r
73 #define portCHAR char
\r
74 #define portFLOAT float
\r
75 #define portDOUBLE portFLOAT
\r
76 #define portLONG long
\r
77 #define portSHORT short
\r
78 #define portSTACK_TYPE unsigned char
\r
79 #define portBASE_TYPE char
\r
81 #if( configUSE_16_BIT_TICKS == 1 )
\r
82 typedef unsigned portSHORT portTickType;
\r
83 #define portMAX_DELAY ( portTickType ) ( 0xFFFF )
\r
85 typedef unsigned portLONG portTickType;
\r
86 #define portMAX_DELAY ( portTickType ) ( 0xFFFFFFFF )
\r
89 #define portBYTE_ALIGNMENT 1
\r
91 /*-----------------------------------------------------------*/
\r
94 * Constant used for context switch macro when we require the interrupt
\r
95 * enable state to be forced when the interrupted task is switched back in.
\r
97 #define portINTERRUPTS_FORCED (0x01)
\r
100 * Constant used for context switch macro when we require the interrupt
\r
101 * enable state to be unchanged when the interrupted task is switched back in.
\r
103 #define portINTERRUPTS_UNCHANGED (0x00)
\r
105 /* Initial interrupt enable state for newly created tasks. This value is
\r
106 * used when a task switches in for the first time.
\r
108 #define portINTERRUPTS_INITIAL_STATE (portINTERRUPTS_FORCED)
\r
111 * Macros to modify the global interrupt enable bit in INTCON.
\r
113 #define portDISABLE_INTERRUPTS() \
\r
117 } while(bGIE) // MicroChip recommends this check!
\r
119 #define portENABLE_INTERRUPTS() \
\r
125 /*-----------------------------------------------------------*/
\r
128 * Critical section macros.
\r
130 extern unsigned portCHAR ucCriticalNesting;
\r
132 #define portNO_CRITICAL_SECTION_NESTING ( ( unsigned portCHAR ) 0 )
\r
134 #define portENTER_CRITICAL() \
\r
137 portDISABLE_INTERRUPTS(); \
\r
140 * Now interrupts are disabled ucCriticalNesting \
\r
141 * can be accessed directly. Increment \
\r
142 * ucCriticalNesting to keep a count of how \
\r
143 * many times portENTER_CRITICAL() has been called. \
\r
145 ucCriticalNesting++; \
\r
148 #define portEXIT_CRITICAL() \
\r
151 if(ucCriticalNesting > portNO_CRITICAL_SECTION_NESTING) \
\r
154 * Decrement the nesting count as we are leaving a \
\r
155 * critical section. \
\r
157 ucCriticalNesting--; \
\r
161 * If the nesting level has reached zero then \
\r
162 * interrupts should be re-enabled. \
\r
164 if( ucCriticalNesting == portNO_CRITICAL_SECTION_NESTING ) \
\r
166 portENABLE_INTERRUPTS(); \
\r
170 /*-----------------------------------------------------------*/
\r
173 * The minimal stacksize is calculated on the first reference of
\r
174 * portMINIMAL_STACK_SIZE. Some input to this calculation is
\r
175 * compiletime determined, other input is port-defined (see port.c)
\r
177 extern unsigned portSHORT usPortCALCULATE_MINIMAL_STACK_SIZE( void );
\r
178 extern unsigned portSHORT usCalcMinStackSize;
\r
180 #define portMINIMAL_STACK_SIZE \
\r
181 ((usCalcMinStackSize == 0) \
\r
182 ? usPortCALCULATE_MINIMAL_STACK_SIZE() \
\r
183 : usCalcMinStackSize )
\r
186 * WizC uses a downgrowing stack
\r
188 #define portSTACK_GROWTH ( -1 )
\r
190 /*-----------------------------------------------------------*/
\r
193 * Macro's that pushes all the registers that make up the context of a task onto
\r
194 * the stack, then saves the new top of stack into the TCB. TOSU and TBLPTRU
\r
195 * are only saved/restored on devices with more than 64kB (32k Words) ROM.
\r
197 * The stackpointer is helt by WizC in FSR2 and points to the first free byte.
\r
198 * WizC uses a "downgrowing" stack. There is no framepointer.
\r
200 * We keep track of the interruptstatus using ucCriticalNesting. When this
\r
201 * value equals zero, interrupts have to be enabled upon exit from the
\r
202 * portRESTORE_CONTEXT macro.
\r
204 * If this is called from an ISR then the interrupt enable bits must have been
\r
205 * set for the ISR to ever get called. Therefore we want to save
\r
206 * ucCriticalNesting with value zero. This means the interrupts will again be
\r
207 * re-enabled when the interrupted task is switched back in.
\r
209 * If this is called from a manual context switch (i.e. from a call to yield),
\r
210 * then we want to keep the current value of ucCritialNesting so it is restored
\r
211 * with its current value. This allows a yield from within a critical section.
\r
213 * The compiler uses some locations at the bottom of RAM for temporary
\r
214 * storage. The compiler may also have been instructed to optimize
\r
215 * function-parameters and local variables to global storage. The compiler
\r
216 * uses an area called LocOpt for this wizC feature.
\r
217 * The total overheadstorage has to be saved in it's entirety as part of
\r
218 * a task context. These macro's store/restore from data address 0x0000 to
\r
219 * (OVERHEADPAGE0-LOCOPTSIZE+MAXLOCOPTSIZE - 1).
\r
220 * OVERHEADPAGE0, LOCOPTSIZE and MAXLOCOPTSIZE are compiler-generated
\r
221 * assembler definitions.
\r
224 #define portSAVE_CONTEXT( ucInterruptForced ) \
\r
227 portDISABLE_INTERRUPTS(); \
\r
231 ; Push the relevant SFR's onto the task's stack \
\r
233 movff STATUS,POSTDEC2 \
\r
234 movff WREG,POSTDEC2 \
\r
235 movff BSR,POSTDEC2 \
\r
236 movff PRODH,POSTDEC2 \
\r
237 movff PRODL,POSTDEC2 \
\r
238 movff FSR0H,POSTDEC2 \
\r
239 movff FSR0L,POSTDEC2 \
\r
240 movff FSR1H,POSTDEC2 \
\r
241 movff FSR1L,POSTDEC2 \
\r
242 movff TABLAT,POSTDEC2 \
\r
243 if __ROMSIZE > 0x8000 \
\r
244 movff TBLPTRU,POSTDEC2 \
\r
246 movff TBLPTRH,POSTDEC2 \
\r
247 movff TBLPTRL,POSTDEC2 \
\r
248 if __ROMSIZE > 0x8000 \
\r
249 movff PCLATU,POSTDEC2 \
\r
251 movff PCLATH,POSTDEC2 \
\r
253 ; Store the compiler-scratch-area as described above. \
\r
255 movlw OVERHEADPAGE0-LOCOPTSIZE+MAXLOCOPTSIZE \
\r
256 clrf FSR0L,ACCESS \
\r
257 clrf FSR0H,ACCESS \
\r
259 movff POSTINC0,POSTDEC2 \
\r
260 decfsz WREG,W,ACCESS \
\r
261 SMARTJUMP _rtos_S1 \
\r
263 ; Save the pic call/return-stack belonging to the \
\r
264 ; current task by copying it to the task's software- \
\r
265 ; stack. We save the hardware stack pointer (which \
\r
266 ; is the number of addresses on the stack) in the \
\r
267 ; W-register first because we need it later and it \
\r
268 ; is modified in the save-loop by executing pop's. \
\r
269 ; After the loop the W-register is stored on the \
\r
272 movf STKPTR,W,ACCESS \
\r
275 if __ROMSIZE > 0x8000 \
\r
276 movff TOSU,POSTDEC2 \
\r
278 movff TOSH,POSTDEC2 \
\r
279 movff TOSL,POSTDEC2 \
\r
281 tstfsz STKPTR,ACCESS \
\r
282 SMARTJUMP _rtos_S2 \
\r
284 movwf POSTDEC2,ACCESS \
\r
286 ; Next the value for ucCriticalNesting used by the \
\r
287 ; task is stored on the stack. When \
\r
288 ; (ucInterruptForced == portINTERRUPTS_FORCED), we save \
\r
289 ; it as 0 (portNO_CRITICAL_SECTION_NESTING). \
\r
291 if ucInterruptForced == portINTERRUPTS_FORCED \
\r
292 clrf POSTDEC2,ACCESS \
\r
294 movff ucCriticalNesting,POSTDEC2 \
\r
297 ; Save the new top of the software stack in the TCB. \
\r
299 movff pxCurrentTCB,FSR0L \
\r
300 movff pxCurrentTCB+1,FSR0H \
\r
301 movff FSR2L,POSTINC0 \
\r
302 movff FSR2H,POSTINC0 \
\r
303 _Pragma("asmend") \
\r
306 /************************************************************/
\r
309 * This is the reverse of portSAVE_CONTEXT.
\r
311 #define portRESTORE_CONTEXT() \
\r
316 ; Set FSR0 to point to pxCurrentTCB->pxTopOfStack. \
\r
318 movff pxCurrentTCB,FSR0L \
\r
319 movff pxCurrentTCB+1,FSR0H \
\r
321 ; De-reference FSR0 to set the address it holds into \
\r
322 ; FSR2 (i.e. *( pxCurrentTCB->pxTopOfStack ) ). FSR2 \
\r
323 ; is used by wizC as stackpointer. \
\r
325 movff POSTINC0,FSR2L \
\r
326 movff POSTINC0,FSR2H \
\r
328 ; Next, the value for ucCriticalNesting used by the \
\r
329 ; task is retrieved from the stack. \
\r
331 movff PREINC2,ucCriticalNesting \
\r
333 ; Rebuild the pic call/return-stack. The number of \
\r
334 ; return addresses is the next item on the task stack. \
\r
335 ; Save this number in PRODL. Then fetch the addresses \
\r
336 ; and store them on the hardwarestack. \
\r
337 ; The datasheets say we can't use movff here... \
\r
339 movff PREINC2,PRODL // Use PRODL as tempregister \
\r
340 clrf STKPTR,ACCESS \
\r
343 movf PREINC2,W,ACCESS \
\r
344 movwf TOSL,ACCESS \
\r
345 movf PREINC2,W,ACCESS \
\r
346 movwf TOSH,ACCESS \
\r
347 if __ROMSIZE > 0x8000 \
\r
348 movf PREINC2,W,ACCESS \
\r
349 movwf TOSU,ACCESS \
\r
353 decfsz PRODL,F,ACCESS \
\r
354 SMARTJUMP _rtos_R1 \
\r
356 ; Restore the compiler's working storage area to page 0 \
\r
358 movlw OVERHEADPAGE0-LOCOPTSIZE+MAXLOCOPTSIZE \
\r
359 movwf FSR0L,ACCESS \
\r
360 clrf FSR0H,ACCESS \
\r
362 decf FSR0L,F,ACCESS \
\r
363 movff PREINC2,INDF0 \
\r
364 tstfsz FSR0L,ACCESS \
\r
365 SMARTJUMP _rtos_R2 \
\r
367 ; Restore the sfr's forming the tasks context. \
\r
368 ; We cannot yet restore bsr, w and status because \
\r
369 ; we need these registers for a final test. \
\r
371 movff PREINC2,PCLATH \
\r
372 if __ROMSIZE > 0x8000 \
\r
373 movff PREINC2,PCLATU \
\r
375 clrf PCLATU,ACCESS \
\r
377 movff PREINC2,TBLPTRL \
\r
378 movff PREINC2,TBLPTRH \
\r
379 if __ROMSIZE > 0x8000 \
\r
380 movff PREINC2,TBLPTRU \
\r
382 clrf TBLPTRU,ACCESS \
\r
384 movff PREINC2,TABLAT \
\r
385 movff PREINC2,FSR1L \
\r
386 movff PREINC2,FSR1H \
\r
387 movff PREINC2,FSR0L \
\r
388 movff PREINC2,FSR0H \
\r
389 movff PREINC2,PRODL \
\r
390 movff PREINC2,PRODH \
\r
392 ; The return from portRESTORE_CONTEXT() depends on \
\r
393 ; the value of ucCriticalNesting. When it is zero, \
\r
394 ; interrupts need to be enabled. This is done via a \
\r
395 ; retfie instruction because we need the \
\r
396 ; interrupt-enabling and the return to the restored \
\r
397 ; task to be uninterruptable. \
\r
398 ; Because bsr, status and W are affected by the test \
\r
399 ; they are restored after the test. \
\r
401 movlb ucCriticalNesting>>8 \
\r
402 tstfsz ucCriticalNesting,BANKED \
\r
403 SMARTJUMP _rtos_R4 \
\r
405 movff PREINC2,BSR \
\r
406 movff PREINC2,WREG \
\r
407 movff PREINC2,STATUS \
\r
408 retfie 0 ; Return enabling interrupts \
\r
410 movff PREINC2,BSR \
\r
411 movff PREINC2,WREG \
\r
412 movff PREINC2,STATUS \
\r
413 return 0 ; Return without affecting interrupts \
\r
414 _Pragma("asmend") \
\r
417 /*-----------------------------------------------------------*/
\r
419 #define portTICK_RATE_MS ( ( portTickType ) 1000 / configTICK_RATE_HZ )
\r
421 /*-----------------------------------------------------------*/
\r
423 extern void vPortYield( void );
\r
424 #define portYIELD() vPortYield()
\r
426 #define portNOP() _Pragma("asm") \
\r
430 /*-----------------------------------------------------------*/
\r
432 #define portTASK_FUNCTION( xFunction, pvParameters ) \
\r
433 void pointed xFunction( void *pvParameters ) \
\r
434 _Pragma(asmfunc xFunction)
\r
436 #define portTASK_FUNCTION_PROTO portTASK_FUNCTION
\r
437 /*-----------------------------------------------------------*/
\r
443 #endif /* PORTMACRO_H */
\r