2 FreeRTOS.org V4.3.0 - 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 for an IEC 61508 compliant version along
\r
32 with commercial development and support options.
\r
33 ***************************************************************************
\r
36 /*-----------------------------------------------------------
\r
37 * Implementation of functions defined in portable.h for the Cygnal port.
\r
38 *----------------------------------------------------------*/
\r
40 /* Standard includes. */
\r
43 /* Scheduler includes. */
\r
44 #include "FreeRTOS.h"
\r
47 /* Constants required to setup timer 2 to produce the RTOS tick. */
\r
48 #define portCLOCK_DIVISOR ( ( unsigned portLONG ) 12 )
\r
49 #define portMAX_TIMER_VALUE ( ( unsigned portLONG ) 0xffff )
\r
50 #define portENABLE_TIMER ( ( unsigned portCHAR ) 0x04 )
\r
51 #define portTIMER_2_INTERRUPT_ENABLE ( ( unsigned portCHAR ) 0x20 )
\r
53 /* The value used in the IE register when a task first starts. */
\r
54 #define portGLOBAL_INTERRUPT_BIT ( ( portSTACK_TYPE ) 0x80 )
\r
56 /* The value used in the PSW register when a task first starts. */
\r
57 #define portINITIAL_PSW ( ( portSTACK_TYPE ) 0x00 )
\r
59 /* Macro to clear the timer 2 interrupt flag. */
\r
60 #define portCLEAR_INTERRUPT_FLAG() TMR2CN &= ~0x80;
\r
62 /* Used during a context switch to store the size of the stack being copied
\r
64 data static unsigned portCHAR ucStackBytes;
\r
66 /* Used during a context switch to point to the next byte in XRAM from/to which
\r
67 a RAM byte is to be copied. */
\r
68 xdata static portSTACK_TYPE * data pxXRAMStack;
\r
70 /* Used during a context switch to point to the next byte in RAM from/to which
\r
71 an XRAM byte is to be copied. */
\r
72 data static portSTACK_TYPE * data pxRAMStack;
\r
74 /* We require the address of the pxCurrentTCB variable, but don't want to know
\r
75 any details of its type. */
\r
76 typedef void tskTCB;
\r
77 extern volatile tskTCB * volatile pxCurrentTCB;
\r
80 * Setup the hardware to generate an interrupt off timer 2 at the required
\r
83 static void prvSetupTimerInterrupt( void );
\r
85 /*-----------------------------------------------------------*/
\r
87 * Macro that copies the current stack from internal RAM to XRAM. This is
\r
88 * required as the 8051 only contains enough internal RAM for a single stack,
\r
89 * but we have a stack for every task.
\r
91 #define portCOPY_STACK_TO_XRAM() \
\r
93 /* pxCurrentTCB points to a TCB which itself points to the location into \
\r
94 which the first stack byte should be copied. Set pxXRAMStack to point \
\r
95 to the location into which the first stack byte is to be copied. */ \
\r
96 pxXRAMStack = ( xdata portSTACK_TYPE * ) *( ( xdata portSTACK_TYPE ** ) pxCurrentTCB ); \
\r
98 /* Set pxRAMStack to point to the first byte to be coped from the stack. */ \
\r
99 pxRAMStack = ( data portSTACK_TYPE * data ) configSTACK_START; \
\r
101 /* Calculate the size of the stack we are about to copy from the current \
\r
102 stack pointer value. */ \
\r
103 ucStackBytes = SP - ( configSTACK_START - 1 ); \
\r
105 /* Before starting to copy the stack, store the calculated stack size so \
\r
106 the stack can be restored when the task is resumed. */ \
\r
107 *pxXRAMStack = ucStackBytes; \
\r
109 /* Copy each stack byte in turn. pxXRAMStack is incremented first as we \
\r
110 have already stored the stack size into XRAM. */ \
\r
111 while( ucStackBytes ) \
\r
114 *pxXRAMStack = *pxRAMStack; \
\r
119 /*-----------------------------------------------------------*/
\r
122 * Macro that copies the stack of the task being resumed from XRAM into
\r
125 #define portCOPY_XRAM_TO_STACK() \
\r
127 /* Setup the pointers as per portCOPY_STACK_TO_XRAM(), but this time to \
\r
128 copy the data back out of XRAM and into the stack. */ \
\r
129 pxXRAMStack = ( xdata portSTACK_TYPE * ) *( ( xdata portSTACK_TYPE ** ) pxCurrentTCB ); \
\r
130 pxRAMStack = ( data portSTACK_TYPE * data ) ( configSTACK_START - 1 ); \
\r
132 /* The first value stored in XRAM was the size of the stack - i.e. the \
\r
133 number of bytes we need to copy back. */ \
\r
134 ucStackBytes = pxXRAMStack[ 0 ]; \
\r
136 /* Copy the required number of bytes back into the stack. */ \
\r
141 *pxRAMStack = *pxXRAMStack; \
\r
143 } while( ucStackBytes ); \
\r
145 /* Restore the stack pointer ready to use the restored stack. */ \
\r
146 SP = ( unsigned portCHAR ) pxRAMStack; \
\r
148 /*-----------------------------------------------------------*/
\r
151 * Macro to push the current execution context onto the stack, before the stack
\r
152 * is moved to XRAM.
\r
154 #define portSAVE_CONTEXT() \
\r
157 /* Push ACC first, as when restoring the context it must be restored \
\r
158 last (it is used to set the IE register). */ \
\r
160 /* Store the IE register then disable interrupts. */ \
\r
181 /*-----------------------------------------------------------*/
\r
184 * Macro that restores the execution context from the stack. The execution
\r
185 * context was saved into the stack before the stack was copied into XRAM.
\r
187 #define portRESTORE_CONTEXT() \
\r
203 /* The next byte of the stack is the IE register. Only the global \
\r
204 enable bit forms part of the task context. Pop off the IE then set \
\r
205 the global enable bit to match that of the stored IE register. */ \
\r
213 /* Finally pop off the ACC, which was the first register saved. */ \
\r
218 /*-----------------------------------------------------------*/
\r
221 * See header file for description.
\r
223 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
225 unsigned portLONG ulAddress;
\r
226 portSTACK_TYPE *pxStartOfStack;
\r
228 /* Leave space to write the size of the stack as the first byte. */
\r
229 pxStartOfStack = pxTopOfStack;
\r
232 /* Place a few bytes of known values on the bottom of the stack.
\r
233 This is just useful for debugging and can be uncommented if required.
\r
234 *pxTopOfStack = 0x11;
\r
236 *pxTopOfStack = 0x22;
\r
238 *pxTopOfStack = 0x33;
\r
242 /* Simulate how the stack would look after a call to the scheduler tick
\r
245 The return address that would have been pushed by the MCU. */
\r
246 ulAddress = ( unsigned portLONG ) pxCode;
\r
247 *pxTopOfStack = ( portSTACK_TYPE ) ulAddress;
\r
250 *pxTopOfStack = ( portSTACK_TYPE ) ( ulAddress );
\r
253 /* Next all the registers will have been pushed by portSAVE_CONTEXT(). */
\r
254 *pxTopOfStack = 0xaa; /* acc */
\r
257 /* We want tasks to start with interrupts enabled. */
\r
258 *pxTopOfStack = portGLOBAL_INTERRUPT_BIT;
\r
261 /* The function parameters will be passed in the DPTR and B register as
\r
262 a three byte generic pointer is used. */
\r
263 ulAddress = ( unsigned portLONG ) pvParameters;
\r
264 *pxTopOfStack = ( portSTACK_TYPE ) ulAddress; /* DPL */
\r
267 *pxTopOfStack = ( portSTACK_TYPE ) ulAddress; /* DPH */
\r
270 *pxTopOfStack = ( portSTACK_TYPE ) ulAddress; /* b */
\r
273 /* The remaining registers are straight forward. */
\r
274 *pxTopOfStack = 0x02; /* R2 */
\r
276 *pxTopOfStack = 0x03; /* R3 */
\r
278 *pxTopOfStack = 0x04; /* R4 */
\r
280 *pxTopOfStack = 0x05; /* R5 */
\r
282 *pxTopOfStack = 0x06; /* R6 */
\r
284 *pxTopOfStack = 0x07; /* R7 */
\r
286 *pxTopOfStack = 0x00; /* R0 */
\r
288 *pxTopOfStack = 0x01; /* R1 */
\r
290 *pxTopOfStack = 0x00; /* PSW */
\r
292 *pxTopOfStack = 0xbb; /* BP */
\r
294 /* Dont increment the stack size here as we don't want to include
\r
295 the stack size byte as part of the stack size count.
\r
297 Finally we place the stack size at the beginning. */
\r
298 *pxStartOfStack = ( portSTACK_TYPE ) ( pxTopOfStack - pxStartOfStack );
\r
300 /* Unlike most ports, we return the start of the stack as this is where the
\r
301 size of the stack is stored. */
\r
302 return pxStartOfStack;
\r
304 /*-----------------------------------------------------------*/
\r
307 * See header file for description.
\r
309 portBASE_TYPE xPortStartScheduler( void )
\r
311 /* Setup timer 2 to generate the RTOS tick. */
\r
312 prvSetupTimerInterrupt();
\r
314 /* Make sure we start with the expected SFR page. This line should not
\r
315 really be required. */
\r
318 /* Copy the stack for the first task to execute from XRAM into the stack,
\r
319 restore the task context from the new stack, then start running the task. */
\r
320 portCOPY_XRAM_TO_STACK();
\r
321 portRESTORE_CONTEXT();
\r
323 /* Should never get here! */
\r
326 /*-----------------------------------------------------------*/
\r
328 void vPortEndScheduler( void )
\r
330 /* Not implemented for this port. */
\r
332 /*-----------------------------------------------------------*/
\r
335 * Manual context switch. The first thing we do is save the registers so we
\r
336 * can use a naked attribute.
\r
338 void vPortYield( void ) _naked
\r
340 /* Save the execution context onto the stack, then copy the entire stack
\r
341 to XRAM. This is necessary as the internal RAM is only large enough to
\r
342 hold one stack, and we want one per task.
\r
344 PERFORMANCE COULD BE IMPROVED BY ONLY COPYING TO XRAM IF A TASK SWITCH
\r
346 portSAVE_CONTEXT();
\r
347 portCOPY_STACK_TO_XRAM();
\r
349 /* Call the standard scheduler context switch function. */
\r
350 vTaskSwitchContext();
\r
352 /* Copy the stack of the task about to execute from XRAM into RAM and
\r
353 restore it's context ready to run on exiting. */
\r
354 portCOPY_XRAM_TO_STACK();
\r
355 portRESTORE_CONTEXT();
\r
357 /*-----------------------------------------------------------*/
\r
359 #if configUSE_PREEMPTION == 1
\r
360 void vTimer2ISR( void ) interrupt 5 _naked
\r
362 /* Preemptive context switch function triggered by the timer 2 ISR.
\r
363 This does the same as vPortYield() (see above) with the addition
\r
364 of incrementing the RTOS tick count. */
\r
366 portSAVE_CONTEXT();
\r
367 portCOPY_STACK_TO_XRAM();
\r
369 vTaskIncrementTick();
\r
370 vTaskSwitchContext();
\r
372 portCLEAR_INTERRUPT_FLAG();
\r
373 portCOPY_XRAM_TO_STACK();
\r
374 portRESTORE_CONTEXT();
\r
377 void vTimer2ISR( void ) interrupt 5
\r
379 /* When using the cooperative scheduler the timer 2 ISR is only
\r
380 required to increment the RTOS tick count. */
\r
382 vTaskIncrementTick();
\r
383 portCLEAR_INTERRUPT_FLAG();
\r
386 /*-----------------------------------------------------------*/
\r
388 static void prvSetupTimerInterrupt( void )
\r
390 unsigned portCHAR ucOriginalSFRPage;
\r
392 /* Constants calculated to give the required timer capture values. */
\r
393 const unsigned portLONG ulTicksPerSecond = configCPU_CLOCK_HZ / portCLOCK_DIVISOR;
\r
394 const unsigned portLONG ulCaptureTime = ulTicksPerSecond / configTICK_RATE_HZ;
\r
395 const unsigned portLONG ulCaptureValue = portMAX_TIMER_VALUE - ulCaptureTime;
\r
396 const unsigned portCHAR ucLowCaptureByte = ( unsigned portCHAR ) ( ulCaptureValue & ( unsigned portLONG ) 0xff );
\r
397 const unsigned portCHAR ucHighCaptureByte = ( unsigned portCHAR ) ( ulCaptureValue >> ( unsigned portLONG ) 8 );
\r
399 /* NOTE: This uses a timer only present on 8052 architecture. */
\r
401 /* Remember the current SFR page so we can restore it at the end of the
\r
403 ucOriginalSFRPage = SFRPAGE;
\r
406 /* TMR2CF can be left in its default state. */
\r
407 TMR2CF = ( unsigned portCHAR ) 0;
\r
409 /* Setup the overflow reload value. */
\r
410 RCAP2L = ucLowCaptureByte;
\r
411 RCAP2H = ucHighCaptureByte;
\r
413 /* The initial load is performed manually. */
\r
414 TMR2L = ucLowCaptureByte;
\r
415 TMR2H = ucHighCaptureByte;
\r
417 /* Enable the timer 2 interrupts. */
\r
418 IE |= portTIMER_2_INTERRUPT_ENABLE;
\r
420 /* Interrupts are disabled when this is called so the timer can be started
\r
422 TMR2CN = portENABLE_TIMER;
\r
424 /* Restore the original SFR page. */
\r
425 SFRPAGE = ucOriginalSFRPage;
\r