2 FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not itcan be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
75 /*-----------------------------------------------------------
\r
76 * Implementation of functions defined in portable.h for the Cygnal port.
\r
77 *----------------------------------------------------------*/
\r
79 /* Standard includes. */
\r
82 /* Scheduler includes. */
\r
83 #include "FreeRTOS.h"
\r
86 /* Constants required to setup timer 2 to produce the RTOS tick. */
\r
87 #define portCLOCK_DIVISOR ( ( unsigned long ) 12 )
\r
88 #define portMAX_TIMER_VALUE ( ( unsigned long ) 0xffff )
\r
89 #define portENABLE_TIMER ( ( unsigned char ) 0x04 )
\r
90 #define portTIMER_2_INTERRUPT_ENABLE ( ( unsigned char ) 0x20 )
\r
92 /* The value used in the IE register when a task first starts. */
\r
93 #define portGLOBAL_INTERRUPT_BIT ( ( portSTACK_TYPE ) 0x80 )
\r
95 /* The value used in the PSW register when a task first starts. */
\r
96 #define portINITIAL_PSW ( ( portSTACK_TYPE ) 0x00 )
\r
98 /* Macro to clear the timer 2 interrupt flag. */
\r
99 #define portCLEAR_INTERRUPT_FLAG() TMR2CN &= ~0x80;
\r
101 /* Used during a context switch to store the size of the stack being copied
\r
102 to or from XRAM. */
\r
103 data static unsigned char ucStackBytes;
\r
105 /* Used during a context switch to point to the next byte in XRAM from/to which
\r
106 a RAM byte is to be copied. */
\r
107 xdata static portSTACK_TYPE * data pxXRAMStack;
\r
109 /* Used during a context switch to point to the next byte in RAM from/to which
\r
110 an XRAM byte is to be copied. */
\r
111 data static portSTACK_TYPE * data pxRAMStack;
\r
113 /* We require the address of the pxCurrentTCB variable, but don't want to know
\r
114 any details of its type. */
\r
115 typedef void tskTCB;
\r
116 extern volatile tskTCB * volatile pxCurrentTCB;
\r
119 * Setup the hardware to generate an interrupt off timer 2 at the required
\r
122 static void prvSetupTimerInterrupt( void );
\r
124 /*-----------------------------------------------------------*/
\r
126 * Macro that copies the current stack from internal RAM to XRAM. This is
\r
127 * required as the 8051 only contains enough internal RAM for a single stack,
\r
128 * but we have a stack for every task.
\r
130 #define portCOPY_STACK_TO_XRAM() \
\r
132 /* pxCurrentTCB points to a TCB which itself points to the location into \
\r
133 which the first stack byte should be copied. Set pxXRAMStack to point \
\r
134 to the location into which the first stack byte is to be copied. */ \
\r
135 pxXRAMStack = ( xdata portSTACK_TYPE * ) *( ( xdata portSTACK_TYPE ** ) pxCurrentTCB ); \
\r
137 /* Set pxRAMStack to point to the first byte to be coped from the stack. */ \
\r
138 pxRAMStack = ( data portSTACK_TYPE * data ) configSTACK_START; \
\r
140 /* Calculate the size of the stack we are about to copy from the current \
\r
141 stack pointer value. */ \
\r
142 ucStackBytes = SP - ( configSTACK_START - 1 ); \
\r
144 /* Before starting to copy the stack, store the calculated stack size so \
\r
145 the stack can be restored when the task is resumed. */ \
\r
146 *pxXRAMStack = ucStackBytes; \
\r
148 /* Copy each stack byte in turn. pxXRAMStack is incremented first as we \
\r
149 have already stored the stack size into XRAM. */ \
\r
150 while( ucStackBytes ) \
\r
153 *pxXRAMStack = *pxRAMStack; \
\r
158 /*-----------------------------------------------------------*/
\r
161 * Macro that copies the stack of the task being resumed from XRAM into
\r
164 #define portCOPY_XRAM_TO_STACK() \
\r
166 /* Setup the pointers as per portCOPY_STACK_TO_XRAM(), but this time to \
\r
167 copy the data back out of XRAM and into the stack. */ \
\r
168 pxXRAMStack = ( xdata portSTACK_TYPE * ) *( ( xdata portSTACK_TYPE ** ) pxCurrentTCB ); \
\r
169 pxRAMStack = ( data portSTACK_TYPE * data ) ( configSTACK_START - 1 ); \
\r
171 /* The first value stored in XRAM was the size of the stack - i.e. the \
\r
172 number of bytes we need to copy back. */ \
\r
173 ucStackBytes = pxXRAMStack[ 0 ]; \
\r
175 /* Copy the required number of bytes back into the stack. */ \
\r
180 *pxRAMStack = *pxXRAMStack; \
\r
182 } while( ucStackBytes ); \
\r
184 /* Restore the stack pointer ready to use the restored stack. */ \
\r
185 SP = ( unsigned char ) pxRAMStack; \
\r
187 /*-----------------------------------------------------------*/
\r
190 * Macro to push the current execution context onto the stack, before the stack
\r
191 * is moved to XRAM.
\r
193 #define portSAVE_CONTEXT() \
\r
196 /* Push ACC first, as when restoring the context it must be restored \
\r
197 last (it is used to set the IE register). */ \
\r
199 /* Store the IE register then disable interrupts. */ \
\r
220 /*-----------------------------------------------------------*/
\r
223 * Macro that restores the execution context from the stack. The execution
\r
224 * context was saved into the stack before the stack was copied into XRAM.
\r
226 #define portRESTORE_CONTEXT() \
\r
242 /* The next byte of the stack is the IE register. Only the global \
\r
243 enable bit forms part of the task context. Pop off the IE then set \
\r
244 the global enable bit to match that of the stored IE register. */ \
\r
252 /* Finally pop off the ACC, which was the first register saved. */ \
\r
257 /*-----------------------------------------------------------*/
\r
260 * See header file for description.
\r
262 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
264 unsigned long ulAddress;
\r
265 portSTACK_TYPE *pxStartOfStack;
\r
267 /* Leave space to write the size of the stack as the first byte. */
\r
268 pxStartOfStack = pxTopOfStack;
\r
271 /* Place a few bytes of known values on the bottom of the stack.
\r
272 This is just useful for debugging and can be uncommented if required.
\r
273 *pxTopOfStack = 0x11;
\r
275 *pxTopOfStack = 0x22;
\r
277 *pxTopOfStack = 0x33;
\r
281 /* Simulate how the stack would look after a call to the scheduler tick
\r
284 The return address that would have been pushed by the MCU. */
\r
285 ulAddress = ( unsigned long ) pxCode;
\r
286 *pxTopOfStack = ( portSTACK_TYPE ) ulAddress;
\r
289 *pxTopOfStack = ( portSTACK_TYPE ) ( ulAddress );
\r
292 /* Next all the registers will have been pushed by portSAVE_CONTEXT(). */
\r
293 *pxTopOfStack = 0xaa; /* acc */
\r
296 /* We want tasks to start with interrupts enabled. */
\r
297 *pxTopOfStack = portGLOBAL_INTERRUPT_BIT;
\r
300 /* The function parameters will be passed in the DPTR and B register as
\r
301 a three byte generic pointer is used. */
\r
302 ulAddress = ( unsigned long ) pvParameters;
\r
303 *pxTopOfStack = ( portSTACK_TYPE ) ulAddress; /* DPL */
\r
306 *pxTopOfStack = ( portSTACK_TYPE ) ulAddress; /* DPH */
\r
309 *pxTopOfStack = ( portSTACK_TYPE ) ulAddress; /* b */
\r
312 /* The remaining registers are straight forward. */
\r
313 *pxTopOfStack = 0x02; /* R2 */
\r
315 *pxTopOfStack = 0x03; /* R3 */
\r
317 *pxTopOfStack = 0x04; /* R4 */
\r
319 *pxTopOfStack = 0x05; /* R5 */
\r
321 *pxTopOfStack = 0x06; /* R6 */
\r
323 *pxTopOfStack = 0x07; /* R7 */
\r
325 *pxTopOfStack = 0x00; /* R0 */
\r
327 *pxTopOfStack = 0x01; /* R1 */
\r
329 *pxTopOfStack = 0x00; /* PSW */
\r
331 *pxTopOfStack = 0xbb; /* BP */
\r
333 /* Dont increment the stack size here as we don't want to include
\r
334 the stack size byte as part of the stack size count.
\r
336 Finally we place the stack size at the beginning. */
\r
337 *pxStartOfStack = ( portSTACK_TYPE ) ( pxTopOfStack - pxStartOfStack );
\r
339 /* Unlike most ports, we return the start of the stack as this is where the
\r
340 size of the stack is stored. */
\r
341 return pxStartOfStack;
\r
343 /*-----------------------------------------------------------*/
\r
346 * See header file for description.
\r
348 portBASE_TYPE xPortStartScheduler( void )
\r
350 /* Setup timer 2 to generate the RTOS tick. */
\r
351 prvSetupTimerInterrupt();
\r
353 /* Make sure we start with the expected SFR page. This line should not
\r
354 really be required. */
\r
357 /* Copy the stack for the first task to execute from XRAM into the stack,
\r
358 restore the task context from the new stack, then start running the task. */
\r
359 portCOPY_XRAM_TO_STACK();
\r
360 portRESTORE_CONTEXT();
\r
362 /* Should never get here! */
\r
365 /*-----------------------------------------------------------*/
\r
367 void vPortEndScheduler( void )
\r
369 /* Not implemented for this port. */
\r
371 /*-----------------------------------------------------------*/
\r
374 * Manual context switch. The first thing we do is save the registers so we
\r
375 * can use a naked attribute.
\r
377 void vPortYield( void ) _naked
\r
379 /* Save the execution context onto the stack, then copy the entire stack
\r
380 to XRAM. This is necessary as the internal RAM is only large enough to
\r
381 hold one stack, and we want one per task.
\r
383 PERFORMANCE COULD BE IMPROVED BY ONLY COPYING TO XRAM IF A TASK SWITCH
\r
385 portSAVE_CONTEXT();
\r
386 portCOPY_STACK_TO_XRAM();
\r
388 /* Call the standard scheduler context switch function. */
\r
389 vTaskSwitchContext();
\r
391 /* Copy the stack of the task about to execute from XRAM into RAM and
\r
392 restore it's context ready to run on exiting. */
\r
393 portCOPY_XRAM_TO_STACK();
\r
394 portRESTORE_CONTEXT();
\r
396 /*-----------------------------------------------------------*/
\r
398 #if configUSE_PREEMPTION == 1
\r
399 void vTimer2ISR( void ) interrupt 5 _naked
\r
401 /* Preemptive context switch function triggered by the timer 2 ISR.
\r
402 This does the same as vPortYield() (see above) with the addition
\r
403 of incrementing the RTOS tick count. */
\r
405 portSAVE_CONTEXT();
\r
406 portCOPY_STACK_TO_XRAM();
\r
408 vTaskIncrementTick();
\r
409 vTaskSwitchContext();
\r
411 portCLEAR_INTERRUPT_FLAG();
\r
412 portCOPY_XRAM_TO_STACK();
\r
413 portRESTORE_CONTEXT();
\r
416 void vTimer2ISR( void ) interrupt 5
\r
418 /* When using the cooperative scheduler the timer 2 ISR is only
\r
419 required to increment the RTOS tick count. */
\r
421 vTaskIncrementTick();
\r
422 portCLEAR_INTERRUPT_FLAG();
\r
425 /*-----------------------------------------------------------*/
\r
427 static void prvSetupTimerInterrupt( void )
\r
429 unsigned char ucOriginalSFRPage;
\r
431 /* Constants calculated to give the required timer capture values. */
\r
432 const unsigned long ulTicksPerSecond = configCPU_CLOCK_HZ / portCLOCK_DIVISOR;
\r
433 const unsigned long ulCaptureTime = ulTicksPerSecond / configTICK_RATE_HZ;
\r
434 const unsigned long ulCaptureValue = portMAX_TIMER_VALUE - ulCaptureTime;
\r
435 const unsigned char ucLowCaptureByte = ( unsigned char ) ( ulCaptureValue & ( unsigned long ) 0xff );
\r
436 const unsigned char ucHighCaptureByte = ( unsigned char ) ( ulCaptureValue >> ( unsigned long ) 8 );
\r
438 /* NOTE: This uses a timer only present on 8052 architecture. */
\r
440 /* Remember the current SFR page so we can restore it at the end of the
\r
442 ucOriginalSFRPage = SFRPAGE;
\r
445 /* TMR2CF can be left in its default state. */
\r
446 TMR2CF = ( unsigned char ) 0;
\r
448 /* Setup the overflow reload value. */
\r
449 RCAP2L = ucLowCaptureByte;
\r
450 RCAP2H = ucHighCaptureByte;
\r
452 /* The initial load is performed manually. */
\r
453 TMR2L = ucLowCaptureByte;
\r
454 TMR2H = ucHighCaptureByte;
\r
456 /* Enable the timer 2 interrupts. */
\r
457 IE |= portTIMER_2_INTERRUPT_ENABLE;
\r
459 /* Interrupts are disabled when this is called so the timer can be started
\r
461 TMR2CN = portENABLE_TIMER;
\r
463 /* Restore the original SFR page. */
\r
464 SFRPAGE = ucOriginalSFRPage;
\r