2 FreeRTOS V8.2.2 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\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
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
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
25 ***************************************************************************
\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
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
37 ***************************************************************************
\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
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
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
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
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
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
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
78 #if !defined(_SERIES) || _SERIES != 18
\r
79 #error "WizC supports FreeRTOS on the Microchip PIC18-series only"
\r
82 #if !defined(QUICKCALL) || QUICKCALL != 1
\r
83 #error "QuickCall must be enabled (see ProjectOptions/Optimisations)"
\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
97 typedef portSTACK_TYPE StackType_t;
\r
98 typedef signed char BaseType_t;
\r
99 typedef unsigned char UBaseType_t;
\r
102 #if( configUSE_16_BIT_TICKS == 1 )
\r
103 typedef uint16_t TickType_t;
\r
104 #define portMAX_DELAY ( TickType_t ) ( 0xFFFF )
\r
106 typedef uint32_t TickType_t;
\r
107 #define portMAX_DELAY ( TickType_t ) ( 0xFFFFFFFF )
\r
110 #define portBYTE_ALIGNMENT 1
\r
112 /*-----------------------------------------------------------*/
\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
118 #define portINTERRUPTS_FORCED (0x01)
\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
124 #define portINTERRUPTS_UNCHANGED (0x00)
\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
129 #define portINTERRUPTS_INITIAL_STATE (portINTERRUPTS_FORCED)
\r
132 * Macros to modify the global interrupt enable bit in INTCON.
\r
134 #define portDISABLE_INTERRUPTS() \
\r
138 } while(bGIE) // MicroChip recommends this check!
\r
140 #define portENABLE_INTERRUPTS() \
\r
146 /*-----------------------------------------------------------*/
\r
149 * Critical section macros.
\r
151 extern uint8_t ucCriticalNesting;
\r
153 #define portNO_CRITICAL_SECTION_NESTING ( ( uint8_t ) 0 )
\r
155 #define portENTER_CRITICAL() \
\r
158 portDISABLE_INTERRUPTS(); \
\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
166 ucCriticalNesting++; \
\r
169 #define portEXIT_CRITICAL() \
\r
172 if(ucCriticalNesting > portNO_CRITICAL_SECTION_NESTING) \
\r
175 * Decrement the nesting count as we are leaving a \
\r
176 * critical section. \
\r
178 ucCriticalNesting--; \
\r
182 * If the nesting level has reached zero then \
\r
183 * interrupts should be re-enabled. \
\r
185 if( ucCriticalNesting == portNO_CRITICAL_SECTION_NESTING ) \
\r
187 portENABLE_INTERRUPTS(); \
\r
191 /*-----------------------------------------------------------*/
\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
198 extern uint16_t usPortCALCULATE_MINIMAL_STACK_SIZE( void );
\r
199 extern uint16_t usCalcMinStackSize;
\r
201 #define portMINIMAL_STACK_SIZE \
\r
202 ((usCalcMinStackSize == 0) \
\r
203 ? usPortCALCULATE_MINIMAL_STACK_SIZE() \
\r
204 : usCalcMinStackSize )
\r
207 * WizC uses a downgrowing stack
\r
209 #define portSTACK_GROWTH ( -1 )
\r
211 /*-----------------------------------------------------------*/
\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
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
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
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
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
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
245 #define portSAVE_CONTEXT( ucInterruptForced ) \
\r
248 portDISABLE_INTERRUPTS(); \
\r
252 ; Push the relevant SFR's onto the task's stack \
\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
267 movff TBLPTRH,POSTDEC2 \
\r
268 movff TBLPTRL,POSTDEC2 \
\r
269 if __ROMSIZE > 0x8000 \
\r
270 movff PCLATU,POSTDEC2 \
\r
272 movff PCLATH,POSTDEC2 \
\r
274 ; Store the compiler-scratch-area as described above. \
\r
276 movlw OVERHEADPAGE0-LOCOPTSIZE+MAXLOCOPTSIZE \
\r
277 clrf FSR0L,ACCESS \
\r
278 clrf FSR0H,ACCESS \
\r
280 movff POSTINC0,POSTDEC2 \
\r
281 decfsz WREG,W,ACCESS \
\r
282 SMARTJUMP _rtos_S1 \
\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
293 movf STKPTR,W,ACCESS \
\r
296 if __ROMSIZE > 0x8000 \
\r
297 movff TOSU,POSTDEC2 \
\r
299 movff TOSH,POSTDEC2 \
\r
300 movff TOSL,POSTDEC2 \
\r
302 tstfsz STKPTR,ACCESS \
\r
303 SMARTJUMP _rtos_S2 \
\r
305 movwf POSTDEC2,ACCESS \
\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
312 if ucInterruptForced == portINTERRUPTS_FORCED \
\r
313 clrf POSTDEC2,ACCESS \
\r
315 movff ucCriticalNesting,POSTDEC2 \
\r
318 ; Save the new top of the software stack in the TCB. \
\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
327 /************************************************************/
\r
330 * This is the reverse of portSAVE_CONTEXT.
\r
332 #define portRESTORE_CONTEXT() \
\r
337 ; Set FSR0 to point to pxCurrentTCB->pxTopOfStack. \
\r
339 movff pxCurrentTCB,FSR0L \
\r
340 movff pxCurrentTCB+1,FSR0H \
\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
346 movff POSTINC0,FSR2L \
\r
347 movff POSTINC0,FSR2H \
\r
349 ; Next, the value for ucCriticalNesting used by the \
\r
350 ; task is retrieved from the stack. \
\r
352 movff PREINC2,ucCriticalNesting \
\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
360 movff PREINC2,PRODL // Use PRODL as tempregister \
\r
361 clrf STKPTR,ACCESS \
\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
374 decfsz PRODL,F,ACCESS \
\r
375 SMARTJUMP _rtos_R1 \
\r
377 ; Restore the compiler's working storage area to page 0 \
\r
379 movlw OVERHEADPAGE0-LOCOPTSIZE+MAXLOCOPTSIZE \
\r
380 movwf FSR0L,ACCESS \
\r
381 clrf FSR0H,ACCESS \
\r
383 decf FSR0L,F,ACCESS \
\r
384 movff PREINC2,INDF0 \
\r
385 tstfsz FSR0L,ACCESS \
\r
386 SMARTJUMP _rtos_R2 \
\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
392 movff PREINC2,PCLATH \
\r
393 if __ROMSIZE > 0x8000 \
\r
394 movff PREINC2,PCLATU \
\r
396 clrf PCLATU,ACCESS \
\r
398 movff PREINC2,TBLPTRL \
\r
399 movff PREINC2,TBLPTRH \
\r
400 if __ROMSIZE > 0x8000 \
\r
401 movff PREINC2,TBLPTRU \
\r
403 clrf TBLPTRU,ACCESS \
\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
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
422 movlb ucCriticalNesting>>8 \
\r
423 tstfsz ucCriticalNesting,BANKED \
\r
424 SMARTJUMP _rtos_R4 \
\r
426 movff PREINC2,BSR \
\r
427 movff PREINC2,WREG \
\r
428 movff PREINC2,STATUS \
\r
429 retfie 0 ; Return enabling interrupts \
\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
438 /*-----------------------------------------------------------*/
\r
440 #define portTICK_PERIOD_MS ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
\r
442 /*-----------------------------------------------------------*/
\r
444 extern void vPortYield( void );
\r
445 #define portYIELD() vPortYield()
\r
447 #define portNOP() _Pragma("asm") \
\r
451 /*-----------------------------------------------------------*/
\r
453 #define portTASK_FUNCTION( xFunction, pvParameters ) \
\r
454 void pointed xFunction( void *pvParameters ) \
\r
455 _Pragma(asmfunc xFunction)
\r
457 #define portTASK_FUNCTION_PROTO portTASK_FUNCTION
\r
458 /*-----------------------------------------------------------*/
\r
464 #endif /* PORTMACRO_H */
\r