2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /*-----------------------------------------------------------
\r
30 * Implementation of functions defined in portable.h for the Cygnal port.
\r
31 *----------------------------------------------------------*/
\r
33 /* Standard includes. */
\r
36 /* Scheduler includes. */
\r
37 #include "FreeRTOS.h"
\r
40 /* Constants required to setup timer 2 to produce the RTOS tick. */
\r
41 #define portCLOCK_DIVISOR ( ( uint32_t ) 12 )
\r
42 #define portMAX_TIMER_VALUE ( ( uint32_t ) 0xffff )
\r
43 #define portENABLE_TIMER ( ( uint8_t ) 0x04 )
\r
44 #define portTIMER_2_INTERRUPT_ENABLE ( ( uint8_t ) 0x20 )
\r
46 /* The value used in the IE register when a task first starts. */
\r
47 #define portGLOBAL_INTERRUPT_BIT ( ( StackType_t ) 0x80 )
\r
49 /* The value used in the PSW register when a task first starts. */
\r
50 #define portINITIAL_PSW ( ( StackType_t ) 0x00 )
\r
52 /* Macro to clear the timer 2 interrupt flag. */
\r
53 #define portCLEAR_INTERRUPT_FLAG() TMR2CN &= ~0x80;
\r
55 /* Used during a context switch to store the size of the stack being copied
\r
57 data static uint8_t ucStackBytes;
\r
59 /* Used during a context switch to point to the next byte in XRAM from/to which
\r
60 a RAM byte is to be copied. */
\r
61 xdata static StackType_t * data pxXRAMStack;
\r
63 /* Used during a context switch to point to the next byte in RAM from/to which
\r
64 an XRAM byte is to be copied. */
\r
65 data static StackType_t * data pxRAMStack;
\r
67 /* We require the address of the pxCurrentTCB variable, but don't want to know
\r
68 any details of its type. */
\r
70 extern volatile TCB_t * volatile pxCurrentTCB;
\r
73 * Setup the hardware to generate an interrupt off timer 2 at the required
\r
76 static void prvSetupTimerInterrupt( void );
\r
78 /*-----------------------------------------------------------*/
\r
80 * Macro that copies the current stack from internal RAM to XRAM. This is
\r
81 * required as the 8051 only contains enough internal RAM for a single stack,
\r
82 * but we have a stack for every task.
\r
84 #define portCOPY_STACK_TO_XRAM() \
\r
86 /* pxCurrentTCB points to a TCB which itself points to the location into \
\r
87 which the first stack byte should be copied. Set pxXRAMStack to point \
\r
88 to the location into which the first stack byte is to be copied. */ \
\r
89 pxXRAMStack = ( xdata StackType_t * ) *( ( xdata StackType_t ** ) pxCurrentTCB ); \
\r
91 /* Set pxRAMStack to point to the first byte to be coped from the stack. */ \
\r
92 pxRAMStack = ( data StackType_t * data ) configSTACK_START; \
\r
94 /* Calculate the size of the stack we are about to copy from the current \
\r
95 stack pointer value. */ \
\r
96 ucStackBytes = SP - ( configSTACK_START - 1 ); \
\r
98 /* Before starting to copy the stack, store the calculated stack size so \
\r
99 the stack can be restored when the task is resumed. */ \
\r
100 *pxXRAMStack = ucStackBytes; \
\r
102 /* Copy each stack byte in turn. pxXRAMStack is incremented first as we \
\r
103 have already stored the stack size into XRAM. */ \
\r
104 while( ucStackBytes ) \
\r
107 *pxXRAMStack = *pxRAMStack; \
\r
112 /*-----------------------------------------------------------*/
\r
115 * Macro that copies the stack of the task being resumed from XRAM into
\r
118 #define portCOPY_XRAM_TO_STACK() \
\r
120 /* Setup the pointers as per portCOPY_STACK_TO_XRAM(), but this time to \
\r
121 copy the data back out of XRAM and into the stack. */ \
\r
122 pxXRAMStack = ( xdata StackType_t * ) *( ( xdata StackType_t ** ) pxCurrentTCB ); \
\r
123 pxRAMStack = ( data StackType_t * data ) ( configSTACK_START - 1 ); \
\r
125 /* The first value stored in XRAM was the size of the stack - i.e. the \
\r
126 number of bytes we need to copy back. */ \
\r
127 ucStackBytes = pxXRAMStack[ 0 ]; \
\r
129 /* Copy the required number of bytes back into the stack. */ \
\r
134 *pxRAMStack = *pxXRAMStack; \
\r
136 } while( ucStackBytes ); \
\r
138 /* Restore the stack pointer ready to use the restored stack. */ \
\r
139 SP = ( uint8_t ) pxRAMStack; \
\r
141 /*-----------------------------------------------------------*/
\r
144 * Macro to push the current execution context onto the stack, before the stack
\r
145 * is moved to XRAM.
\r
147 #define portSAVE_CONTEXT() \
\r
150 /* Push ACC first, as when restoring the context it must be restored \
\r
151 last (it is used to set the IE register). */ \
\r
153 /* Store the IE register then disable interrupts. */ \
\r
174 /*-----------------------------------------------------------*/
\r
177 * Macro that restores the execution context from the stack. The execution
\r
178 * context was saved into the stack before the stack was copied into XRAM.
\r
180 #define portRESTORE_CONTEXT() \
\r
196 /* The next byte of the stack is the IE register. Only the global \
\r
197 enable bit forms part of the task context. Pop off the IE then set \
\r
198 the global enable bit to match that of the stored IE register. */ \
\r
206 /* Finally pop off the ACC, which was the first register saved. */ \
\r
211 /*-----------------------------------------------------------*/
\r
214 * See header file for description.
\r
216 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
218 uint32_t ulAddress;
\r
219 StackType_t *pxStartOfStack;
\r
221 /* Leave space to write the size of the stack as the first byte. */
\r
222 pxStartOfStack = pxTopOfStack;
\r
225 /* Place a few bytes of known values on the bottom of the stack.
\r
226 This is just useful for debugging and can be uncommented if required.
\r
227 *pxTopOfStack = 0x11;
\r
229 *pxTopOfStack = 0x22;
\r
231 *pxTopOfStack = 0x33;
\r
235 /* Simulate how the stack would look after a call to the scheduler tick
\r
238 The return address that would have been pushed by the MCU. */
\r
239 ulAddress = ( uint32_t ) pxCode;
\r
240 *pxTopOfStack = ( StackType_t ) ulAddress;
\r
243 *pxTopOfStack = ( StackType_t ) ( ulAddress );
\r
246 /* Next all the registers will have been pushed by portSAVE_CONTEXT(). */
\r
247 *pxTopOfStack = 0xaa; /* acc */
\r
250 /* We want tasks to start with interrupts enabled. */
\r
251 *pxTopOfStack = portGLOBAL_INTERRUPT_BIT;
\r
254 /* The function parameters will be passed in the DPTR and B register as
\r
255 a three byte generic pointer is used. */
\r
256 ulAddress = ( uint32_t ) pvParameters;
\r
257 *pxTopOfStack = ( StackType_t ) ulAddress; /* DPL */
\r
260 *pxTopOfStack = ( StackType_t ) ulAddress; /* DPH */
\r
263 *pxTopOfStack = ( StackType_t ) ulAddress; /* b */
\r
266 /* The remaining registers are straight forward. */
\r
267 *pxTopOfStack = 0x02; /* R2 */
\r
269 *pxTopOfStack = 0x03; /* R3 */
\r
271 *pxTopOfStack = 0x04; /* R4 */
\r
273 *pxTopOfStack = 0x05; /* R5 */
\r
275 *pxTopOfStack = 0x06; /* R6 */
\r
277 *pxTopOfStack = 0x07; /* R7 */
\r
279 *pxTopOfStack = 0x00; /* R0 */
\r
281 *pxTopOfStack = 0x01; /* R1 */
\r
283 *pxTopOfStack = 0x00; /* PSW */
\r
285 *pxTopOfStack = 0xbb; /* BP */
\r
287 /* Dont increment the stack size here as we don't want to include
\r
288 the stack size byte as part of the stack size count.
\r
290 Finally we place the stack size at the beginning. */
\r
291 *pxStartOfStack = ( StackType_t ) ( pxTopOfStack - pxStartOfStack );
\r
293 /* Unlike most ports, we return the start of the stack as this is where the
\r
294 size of the stack is stored. */
\r
295 return pxStartOfStack;
\r
297 /*-----------------------------------------------------------*/
\r
300 * See header file for description.
\r
302 BaseType_t xPortStartScheduler( void )
\r
304 /* Setup timer 2 to generate the RTOS tick. */
\r
305 prvSetupTimerInterrupt();
\r
307 /* Make sure we start with the expected SFR page. This line should not
\r
308 really be required. */
\r
311 /* Copy the stack for the first task to execute from XRAM into the stack,
\r
312 restore the task context from the new stack, then start running the task. */
\r
313 portCOPY_XRAM_TO_STACK();
\r
314 portRESTORE_CONTEXT();
\r
316 /* Should never get here! */
\r
319 /*-----------------------------------------------------------*/
\r
321 void vPortEndScheduler( void )
\r
323 /* Not implemented for this port. */
\r
325 /*-----------------------------------------------------------*/
\r
328 * Manual context switch. The first thing we do is save the registers so we
\r
329 * can use a naked attribute.
\r
331 void vPortYield( void ) _naked
\r
333 /* Save the execution context onto the stack, then copy the entire stack
\r
334 to XRAM. This is necessary as the internal RAM is only large enough to
\r
335 hold one stack, and we want one per task.
\r
337 PERFORMANCE COULD BE IMPROVED BY ONLY COPYING TO XRAM IF A TASK SWITCH
\r
339 portSAVE_CONTEXT();
\r
340 portCOPY_STACK_TO_XRAM();
\r
342 /* Call the standard scheduler context switch function. */
\r
343 vTaskSwitchContext();
\r
345 /* Copy the stack of the task about to execute from XRAM into RAM and
\r
346 restore it's context ready to run on exiting. */
\r
347 portCOPY_XRAM_TO_STACK();
\r
348 portRESTORE_CONTEXT();
\r
350 /*-----------------------------------------------------------*/
\r
352 #if configUSE_PREEMPTION == 1
\r
353 void vTimer2ISR( void ) interrupt 5 _naked
\r
355 /* Preemptive context switch function triggered by the timer 2 ISR.
\r
356 This does the same as vPortYield() (see above) with the addition
\r
357 of incrementing the RTOS tick count. */
\r
359 portSAVE_CONTEXT();
\r
360 portCOPY_STACK_TO_XRAM();
\r
362 if( xTaskIncrementTick() != pdFALSE )
\r
364 vTaskSwitchContext();
\r
367 portCLEAR_INTERRUPT_FLAG();
\r
368 portCOPY_XRAM_TO_STACK();
\r
369 portRESTORE_CONTEXT();
\r
372 void vTimer2ISR( void ) interrupt 5
\r
374 /* When using the cooperative scheduler the timer 2 ISR is only
\r
375 required to increment the RTOS tick count. */
\r
377 xTaskIncrementTick();
\r
378 portCLEAR_INTERRUPT_FLAG();
\r
381 /*-----------------------------------------------------------*/
\r
383 static void prvSetupTimerInterrupt( void )
\r
385 uint8_t ucOriginalSFRPage;
\r
387 /* Constants calculated to give the required timer capture values. */
\r
388 const uint32_t ulTicksPerSecond = configCPU_CLOCK_HZ / portCLOCK_DIVISOR;
\r
389 const uint32_t ulCaptureTime = ulTicksPerSecond / configTICK_RATE_HZ;
\r
390 const uint32_t ulCaptureValue = portMAX_TIMER_VALUE - ulCaptureTime;
\r
391 const uint8_t ucLowCaptureByte = ( uint8_t ) ( ulCaptureValue & ( uint32_t ) 0xff );
\r
392 const uint8_t ucHighCaptureByte = ( uint8_t ) ( ulCaptureValue >> ( uint32_t ) 8 );
\r
394 /* NOTE: This uses a timer only present on 8052 architecture. */
\r
396 /* Remember the current SFR page so we can restore it at the end of the
\r
398 ucOriginalSFRPage = SFRPAGE;
\r
401 /* TMR2CF can be left in its default state. */
\r
402 TMR2CF = ( uint8_t ) 0;
\r
404 /* Setup the overflow reload value. */
\r
405 RCAP2L = ucLowCaptureByte;
\r
406 RCAP2H = ucHighCaptureByte;
\r
408 /* The initial load is performed manually. */
\r
409 TMR2L = ucLowCaptureByte;
\r
410 TMR2H = ucHighCaptureByte;
\r
412 /* Enable the timer 2 interrupts. */
\r
413 IE |= portTIMER_2_INTERRUPT_ENABLE;
\r
415 /* Interrupts are disabled when this is called so the timer can be started
\r
417 TMR2CN = portENABLE_TIMER;
\r
419 /* Restore the original SFR page. */
\r
420 SFRPAGE = ucOriginalSFRPage;
\r