2 FreeRTOS.org V4.6.1 - Copyright (C) 2003-2007 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
31 Also see http://www.SafeRTOS.com a version that has been certified for use
\r
32 in safety critical systems, plus commercial licensing, development and
\r
34 ***************************************************************************
\r
37 /*-----------------------------------------------------------
\r
38 * Implementation of functions defined in portable.h for the Cygnal port.
\r
39 *----------------------------------------------------------*/
\r
41 /* Standard includes. */
\r
44 /* Scheduler includes. */
\r
45 #include "FreeRTOS.h"
\r
48 /* Constants required to setup timer 2 to produce the RTOS tick. */
\r
49 #define portCLOCK_DIVISOR ( ( unsigned portLONG ) 12 )
\r
50 #define portMAX_TIMER_VALUE ( ( unsigned portLONG ) 0xffff )
\r
51 #define portENABLE_TIMER ( ( unsigned portCHAR ) 0x04 )
\r
52 #define portTIMER_2_INTERRUPT_ENABLE ( ( unsigned portCHAR ) 0x20 )
\r
54 /* The value used in the IE register when a task first starts. */
\r
55 #define portGLOBAL_INTERRUPT_BIT ( ( portSTACK_TYPE ) 0x80 )
\r
57 /* The value used in the PSW register when a task first starts. */
\r
58 #define portINITIAL_PSW ( ( portSTACK_TYPE ) 0x00 )
\r
60 /* Macro to clear the timer 2 interrupt flag. */
\r
61 #define portCLEAR_INTERRUPT_FLAG() TMR2CN &= ~0x80;
\r
63 /* Used during a context switch to store the size of the stack being copied
\r
65 data static unsigned portCHAR ucStackBytes;
\r
67 /* Used during a context switch to point to the next byte in XRAM from/to which
\r
68 a RAM byte is to be copied. */
\r
69 xdata static portSTACK_TYPE * data pxXRAMStack;
\r
71 /* Used during a context switch to point to the next byte in RAM from/to which
\r
72 an XRAM byte is to be copied. */
\r
73 data static portSTACK_TYPE * data pxRAMStack;
\r
75 /* We require the address of the pxCurrentTCB variable, but don't want to know
\r
76 any details of its type. */
\r
77 typedef void tskTCB;
\r
78 extern volatile tskTCB * volatile pxCurrentTCB;
\r
81 * Setup the hardware to generate an interrupt off timer 2 at the required
\r
84 static void prvSetupTimerInterrupt( void );
\r
86 /*-----------------------------------------------------------*/
\r
88 * Macro that copies the current stack from internal RAM to XRAM. This is
\r
89 * required as the 8051 only contains enough internal RAM for a single stack,
\r
90 * but we have a stack for every task.
\r
92 #define portCOPY_STACK_TO_XRAM() \
\r
94 /* pxCurrentTCB points to a TCB which itself points to the location into \
\r
95 which the first stack byte should be copied. Set pxXRAMStack to point \
\r
96 to the location into which the first stack byte is to be copied. */ \
\r
97 pxXRAMStack = ( xdata portSTACK_TYPE * ) *( ( xdata portSTACK_TYPE ** ) pxCurrentTCB ); \
\r
99 /* Set pxRAMStack to point to the first byte to be coped from the stack. */ \
\r
100 pxRAMStack = ( data portSTACK_TYPE * data ) configSTACK_START; \
\r
102 /* Calculate the size of the stack we are about to copy from the current \
\r
103 stack pointer value. */ \
\r
104 ucStackBytes = SP - ( configSTACK_START - 1 ); \
\r
106 /* Before starting to copy the stack, store the calculated stack size so \
\r
107 the stack can be restored when the task is resumed. */ \
\r
108 *pxXRAMStack = ucStackBytes; \
\r
110 /* Copy each stack byte in turn. pxXRAMStack is incremented first as we \
\r
111 have already stored the stack size into XRAM. */ \
\r
112 while( ucStackBytes ) \
\r
115 *pxXRAMStack = *pxRAMStack; \
\r
120 /*-----------------------------------------------------------*/
\r
123 * Macro that copies the stack of the task being resumed from XRAM into
\r
126 #define portCOPY_XRAM_TO_STACK() \
\r
128 /* Setup the pointers as per portCOPY_STACK_TO_XRAM(), but this time to \
\r
129 copy the data back out of XRAM and into the stack. */ \
\r
130 pxXRAMStack = ( xdata portSTACK_TYPE * ) *( ( xdata portSTACK_TYPE ** ) pxCurrentTCB ); \
\r
131 pxRAMStack = ( data portSTACK_TYPE * data ) ( configSTACK_START - 1 ); \
\r
133 /* The first value stored in XRAM was the size of the stack - i.e. the \
\r
134 number of bytes we need to copy back. */ \
\r
135 ucStackBytes = pxXRAMStack[ 0 ]; \
\r
137 /* Copy the required number of bytes back into the stack. */ \
\r
142 *pxRAMStack = *pxXRAMStack; \
\r
144 } while( ucStackBytes ); \
\r
146 /* Restore the stack pointer ready to use the restored stack. */ \
\r
147 SP = ( unsigned portCHAR ) pxRAMStack; \
\r
149 /*-----------------------------------------------------------*/
\r
152 * Macro to push the current execution context onto the stack, before the stack
\r
153 * is moved to XRAM.
\r
155 #define portSAVE_CONTEXT() \
\r
158 /* Push ACC first, as when restoring the context it must be restored \
\r
159 last (it is used to set the IE register). */ \
\r
161 /* Store the IE register then disable interrupts. */ \
\r
182 /*-----------------------------------------------------------*/
\r
185 * Macro that restores the execution context from the stack. The execution
\r
186 * context was saved into the stack before the stack was copied into XRAM.
\r
188 #define portRESTORE_CONTEXT() \
\r
204 /* The next byte of the stack is the IE register. Only the global \
\r
205 enable bit forms part of the task context. Pop off the IE then set \
\r
206 the global enable bit to match that of the stored IE register. */ \
\r
214 /* Finally pop off the ACC, which was the first register saved. */ \
\r
219 /*-----------------------------------------------------------*/
\r
222 * See header file for description.
\r
224 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
226 unsigned portLONG ulAddress;
\r
227 portSTACK_TYPE *pxStartOfStack;
\r
229 /* Leave space to write the size of the stack as the first byte. */
\r
230 pxStartOfStack = pxTopOfStack;
\r
233 /* Place a few bytes of known values on the bottom of the stack.
\r
234 This is just useful for debugging and can be uncommented if required.
\r
235 *pxTopOfStack = 0x11;
\r
237 *pxTopOfStack = 0x22;
\r
239 *pxTopOfStack = 0x33;
\r
243 /* Simulate how the stack would look after a call to the scheduler tick
\r
246 The return address that would have been pushed by the MCU. */
\r
247 ulAddress = ( unsigned portLONG ) pxCode;
\r
248 *pxTopOfStack = ( portSTACK_TYPE ) ulAddress;
\r
251 *pxTopOfStack = ( portSTACK_TYPE ) ( ulAddress );
\r
254 /* Next all the registers will have been pushed by portSAVE_CONTEXT(). */
\r
255 *pxTopOfStack = 0xaa; /* acc */
\r
258 /* We want tasks to start with interrupts enabled. */
\r
259 *pxTopOfStack = portGLOBAL_INTERRUPT_BIT;
\r
262 /* The function parameters will be passed in the DPTR and B register as
\r
263 a three byte generic pointer is used. */
\r
264 ulAddress = ( unsigned portLONG ) pvParameters;
\r
265 *pxTopOfStack = ( portSTACK_TYPE ) ulAddress; /* DPL */
\r
268 *pxTopOfStack = ( portSTACK_TYPE ) ulAddress; /* DPH */
\r
271 *pxTopOfStack = ( portSTACK_TYPE ) ulAddress; /* b */
\r
274 /* The remaining registers are straight forward. */
\r
275 *pxTopOfStack = 0x02; /* R2 */
\r
277 *pxTopOfStack = 0x03; /* R3 */
\r
279 *pxTopOfStack = 0x04; /* R4 */
\r
281 *pxTopOfStack = 0x05; /* R5 */
\r
283 *pxTopOfStack = 0x06; /* R6 */
\r
285 *pxTopOfStack = 0x07; /* R7 */
\r
287 *pxTopOfStack = 0x00; /* R0 */
\r
289 *pxTopOfStack = 0x01; /* R1 */
\r
291 *pxTopOfStack = 0x00; /* PSW */
\r
293 *pxTopOfStack = 0xbb; /* BP */
\r
295 /* Dont increment the stack size here as we don't want to include
\r
296 the stack size byte as part of the stack size count.
\r
298 Finally we place the stack size at the beginning. */
\r
299 *pxStartOfStack = ( portSTACK_TYPE ) ( pxTopOfStack - pxStartOfStack );
\r
301 /* Unlike most ports, we return the start of the stack as this is where the
\r
302 size of the stack is stored. */
\r
303 return pxStartOfStack;
\r
305 /*-----------------------------------------------------------*/
\r
308 * See header file for description.
\r
310 portBASE_TYPE xPortStartScheduler( void )
\r
312 /* Setup timer 2 to generate the RTOS tick. */
\r
313 prvSetupTimerInterrupt();
\r
315 /* Make sure we start with the expected SFR page. This line should not
\r
316 really be required. */
\r
319 /* Copy the stack for the first task to execute from XRAM into the stack,
\r
320 restore the task context from the new stack, then start running the task. */
\r
321 portCOPY_XRAM_TO_STACK();
\r
322 portRESTORE_CONTEXT();
\r
324 /* Should never get here! */
\r
327 /*-----------------------------------------------------------*/
\r
329 void vPortEndScheduler( void )
\r
331 /* Not implemented for this port. */
\r
333 /*-----------------------------------------------------------*/
\r
336 * Manual context switch. The first thing we do is save the registers so we
\r
337 * can use a naked attribute.
\r
339 void vPortYield( void ) _naked
\r
341 /* Save the execution context onto the stack, then copy the entire stack
\r
342 to XRAM. This is necessary as the internal RAM is only large enough to
\r
343 hold one stack, and we want one per task.
\r
345 PERFORMANCE COULD BE IMPROVED BY ONLY COPYING TO XRAM IF A TASK SWITCH
\r
347 portSAVE_CONTEXT();
\r
348 portCOPY_STACK_TO_XRAM();
\r
350 /* Call the standard scheduler context switch function. */
\r
351 vTaskSwitchContext();
\r
353 /* Copy the stack of the task about to execute from XRAM into RAM and
\r
354 restore it's context ready to run on exiting. */
\r
355 portCOPY_XRAM_TO_STACK();
\r
356 portRESTORE_CONTEXT();
\r
358 /*-----------------------------------------------------------*/
\r
360 #if configUSE_PREEMPTION == 1
\r
361 void vTimer2ISR( void ) interrupt 5 _naked
\r
363 /* Preemptive context switch function triggered by the timer 2 ISR.
\r
364 This does the same as vPortYield() (see above) with the addition
\r
365 of incrementing the RTOS tick count. */
\r
367 portSAVE_CONTEXT();
\r
368 portCOPY_STACK_TO_XRAM();
\r
370 vTaskIncrementTick();
\r
371 vTaskSwitchContext();
\r
373 portCLEAR_INTERRUPT_FLAG();
\r
374 portCOPY_XRAM_TO_STACK();
\r
375 portRESTORE_CONTEXT();
\r
378 void vTimer2ISR( void ) interrupt 5
\r
380 /* When using the cooperative scheduler the timer 2 ISR is only
\r
381 required to increment the RTOS tick count. */
\r
383 vTaskIncrementTick();
\r
384 portCLEAR_INTERRUPT_FLAG();
\r
387 /*-----------------------------------------------------------*/
\r
389 static void prvSetupTimerInterrupt( void )
\r
391 unsigned portCHAR ucOriginalSFRPage;
\r
393 /* Constants calculated to give the required timer capture values. */
\r
394 const unsigned portLONG ulTicksPerSecond = configCPU_CLOCK_HZ / portCLOCK_DIVISOR;
\r
395 const unsigned portLONG ulCaptureTime = ulTicksPerSecond / configTICK_RATE_HZ;
\r
396 const unsigned portLONG ulCaptureValue = portMAX_TIMER_VALUE - ulCaptureTime;
\r
397 const unsigned portCHAR ucLowCaptureByte = ( unsigned portCHAR ) ( ulCaptureValue & ( unsigned portLONG ) 0xff );
\r
398 const unsigned portCHAR ucHighCaptureByte = ( unsigned portCHAR ) ( ulCaptureValue >> ( unsigned portLONG ) 8 );
\r
400 /* NOTE: This uses a timer only present on 8052 architecture. */
\r
402 /* Remember the current SFR page so we can restore it at the end of the
\r
404 ucOriginalSFRPage = SFRPAGE;
\r
407 /* TMR2CF can be left in its default state. */
\r
408 TMR2CF = ( unsigned portCHAR ) 0;
\r
410 /* Setup the overflow reload value. */
\r
411 RCAP2L = ucLowCaptureByte;
\r
412 RCAP2H = ucHighCaptureByte;
\r
414 /* The initial load is performed manually. */
\r
415 TMR2L = ucLowCaptureByte;
\r
416 TMR2H = ucHighCaptureByte;
\r
418 /* Enable the timer 2 interrupts. */
\r
419 IE |= portTIMER_2_INTERRUPT_ENABLE;
\r
421 /* Interrupts are disabled when this is called so the timer can be started
\r
423 TMR2CN = portENABLE_TIMER;
\r
425 /* Restore the original SFR page. */
\r
426 SFRPAGE = ucOriginalSFRPage;
\r