2 FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
70 + AVR port - Replaced the inb() and outb() functions with direct memory
\r
71 access. This allows the port to be built with the 20050414 build of
\r
76 #include <avr/interrupt.h>
\r
78 #include "FreeRTOS.h"
\r
81 /*-----------------------------------------------------------
\r
82 * Implementation of functions defined in portable.h for the AVR port.
\r
83 *----------------------------------------------------------*/
\r
85 /* Start tasks with interrupts enables. */
\r
86 #define portFLAGS_INT_ENABLED ( ( StackType_t ) 0x80 )
\r
88 /* Hardware constants for timer 1. */
\r
89 #define portCLEAR_COUNTER_ON_MATCH ( ( uint8_t ) 0x08 )
\r
90 #define portPRESCALE_64 ( ( uint8_t ) 0x03 )
\r
91 #define portCLOCK_PRESCALER ( ( uint32_t ) 64 )
\r
92 #define portCOMPARE_MATCH_A_INTERRUPT_ENABLE ( ( uint8_t ) 0x10 )
\r
94 /*-----------------------------------------------------------*/
\r
96 /* We require the address of the pxCurrentTCB variable, but don't want to know
\r
97 any details of its type. */
\r
99 extern volatile TCB_t * volatile pxCurrentTCB;
\r
101 /*-----------------------------------------------------------*/
\r
104 * Macro to save all the general purpose registers, the save the stack pointer
\r
107 * The first thing we do is save the flags then disable interrupts. This is to
\r
108 * guard our stack against having a context switch interrupt after we have already
\r
109 * pushed the registers onto the stack - causing the 32 registers to be on the
\r
112 * r1 is set to zero as the compiler expects it to be thus, however some
\r
113 * of the math routines make use of R1.
\r
115 * The interrupts will have been disabled during the call to portSAVE_CONTEXT()
\r
116 * so we need not worry about reading/writing to the stack pointer.
\r
119 #define portSAVE_CONTEXT() \
\r
120 asm volatile ( "push r0 \n\t" \
\r
121 "in r0, __SREG__ \n\t" \
\r
156 "lds r26, pxCurrentTCB \n\t" \
\r
157 "lds r27, pxCurrentTCB + 1 \n\t" \
\r
158 "in r0, 0x3d \n\t" \
\r
160 "in r0, 0x3e \n\t" \
\r
165 * Opposite to portSAVE_CONTEXT(). Interrupts will have been disabled during
\r
166 * the context save so we can write to the stack pointer.
\r
169 #define portRESTORE_CONTEXT() \
\r
170 asm volatile ( "lds r26, pxCurrentTCB \n\t" \
\r
171 "lds r27, pxCurrentTCB + 1 \n\t" \
\r
172 "ld r28, x+ \n\t" \
\r
173 "out __SP_L__, r28 \n\t" \
\r
174 "ld r29, x+ \n\t" \
\r
175 "out __SP_H__, r29 \n\t" \
\r
208 "out __SREG__, r0 \n\t" \
\r
212 /*-----------------------------------------------------------*/
\r
215 * Perform hardware setup to enable ticks from timer 1, compare match A.
\r
217 static void prvSetupTimerInterrupt( void );
\r
218 /*-----------------------------------------------------------*/
\r
221 * See header file for description.
\r
223 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
225 uint16_t usAddress;
\r
227 /* Place a few bytes of known values on the bottom of the stack.
\r
228 This is just useful for debugging. */
\r
230 *pxTopOfStack = 0x11;
\r
232 *pxTopOfStack = 0x22;
\r
234 *pxTopOfStack = 0x33;
\r
237 /* Simulate how the stack would look after a call to vPortYield() generated by
\r
240 /*lint -e950 -e611 -e923 Lint doesn't like this much - but nothing I can do about it. */
\r
242 /* The start of the task code will be popped off the stack last, so place
\r
244 usAddress = ( uint16_t ) pxCode;
\r
245 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
\r
249 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
\r
252 /* Next simulate the stack as if after a call to portSAVE_CONTEXT().
\r
253 portSAVE_CONTEXT places the flags on the stack immediately after r0
\r
254 to ensure the interrupts get disabled as soon as possible, and so ensuring
\r
255 the stack use is minimal should a context switch interrupt occur. */
\r
256 *pxTopOfStack = ( StackType_t ) 0x00; /* R0 */
\r
258 *pxTopOfStack = portFLAGS_INT_ENABLED;
\r
262 /* Now the remaining registers. The compiler expects R1 to be 0. */
\r
263 *pxTopOfStack = ( StackType_t ) 0x00; /* R1 */
\r
265 *pxTopOfStack = ( StackType_t ) 0x02; /* R2 */
\r
267 *pxTopOfStack = ( StackType_t ) 0x03; /* R3 */
\r
269 *pxTopOfStack = ( StackType_t ) 0x04; /* R4 */
\r
271 *pxTopOfStack = ( StackType_t ) 0x05; /* R5 */
\r
273 *pxTopOfStack = ( StackType_t ) 0x06; /* R6 */
\r
275 *pxTopOfStack = ( StackType_t ) 0x07; /* R7 */
\r
277 *pxTopOfStack = ( StackType_t ) 0x08; /* R8 */
\r
279 *pxTopOfStack = ( StackType_t ) 0x09; /* R9 */
\r
281 *pxTopOfStack = ( StackType_t ) 0x10; /* R10 */
\r
283 *pxTopOfStack = ( StackType_t ) 0x11; /* R11 */
\r
285 *pxTopOfStack = ( StackType_t ) 0x12; /* R12 */
\r
287 *pxTopOfStack = ( StackType_t ) 0x13; /* R13 */
\r
289 *pxTopOfStack = ( StackType_t ) 0x14; /* R14 */
\r
291 *pxTopOfStack = ( StackType_t ) 0x15; /* R15 */
\r
293 *pxTopOfStack = ( StackType_t ) 0x16; /* R16 */
\r
295 *pxTopOfStack = ( StackType_t ) 0x17; /* R17 */
\r
297 *pxTopOfStack = ( StackType_t ) 0x18; /* R18 */
\r
299 *pxTopOfStack = ( StackType_t ) 0x19; /* R19 */
\r
301 *pxTopOfStack = ( StackType_t ) 0x20; /* R20 */
\r
303 *pxTopOfStack = ( StackType_t ) 0x21; /* R21 */
\r
305 *pxTopOfStack = ( StackType_t ) 0x22; /* R22 */
\r
307 *pxTopOfStack = ( StackType_t ) 0x23; /* R23 */
\r
310 /* Place the parameter on the stack in the expected location. */
\r
311 usAddress = ( uint16_t ) pvParameters;
\r
312 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
\r
316 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
\r
319 *pxTopOfStack = ( StackType_t ) 0x26; /* R26 X */
\r
321 *pxTopOfStack = ( StackType_t ) 0x27; /* R27 */
\r
323 *pxTopOfStack = ( StackType_t ) 0x28; /* R28 Y */
\r
325 *pxTopOfStack = ( StackType_t ) 0x29; /* R29 */
\r
327 *pxTopOfStack = ( StackType_t ) 0x30; /* R30 Z */
\r
329 *pxTopOfStack = ( StackType_t ) 0x031; /* R31 */
\r
332 /*lint +e950 +e611 +e923 */
\r
334 return pxTopOfStack;
\r
336 /*-----------------------------------------------------------*/
\r
338 BaseType_t xPortStartScheduler( void )
\r
340 /* Setup the hardware to generate the tick. */
\r
341 prvSetupTimerInterrupt();
\r
343 /* Restore the context of the first task that is going to run. */
\r
344 portRESTORE_CONTEXT();
\r
346 /* Simulate a function call end as generated by the compiler. We will now
\r
347 jump to the start of the task the context of which we have just restored. */
\r
348 asm volatile ( "ret" );
\r
350 /* Should not get here. */
\r
353 /*-----------------------------------------------------------*/
\r
355 void vPortEndScheduler( void )
\r
357 /* It is unlikely that the AVR port will get stopped. If required simply
\r
358 disable the tick interrupt here. */
\r
360 /*-----------------------------------------------------------*/
\r
363 * Manual context switch. The first thing we do is save the registers so we
\r
364 * can use a naked attribute.
\r
366 void vPortYield( void ) __attribute__ ( ( naked ) );
\r
367 void vPortYield( void )
\r
369 portSAVE_CONTEXT();
\r
370 vTaskSwitchContext();
\r
371 portRESTORE_CONTEXT();
\r
373 asm volatile ( "ret" );
\r
375 /*-----------------------------------------------------------*/
\r
378 * Context switch function used by the tick. This must be identical to
\r
379 * vPortYield() from the call to vTaskSwitchContext() onwards. The only
\r
380 * difference from vPortYield() is the tick count is incremented as the
\r
381 * call comes from the tick ISR.
\r
383 void vPortYieldFromTick( void ) __attribute__ ( ( naked ) );
\r
384 void vPortYieldFromTick( void )
\r
386 portSAVE_CONTEXT();
\r
387 if( xTaskIncrementTick() != pdFALSE )
\r
389 vTaskSwitchContext();
\r
391 portRESTORE_CONTEXT();
\r
393 asm volatile ( "ret" );
\r
395 /*-----------------------------------------------------------*/
\r
398 * Setup timer 1 compare match A to generate a tick interrupt.
\r
400 static void prvSetupTimerInterrupt( void )
\r
402 uint32_t ulCompareMatch;
\r
403 uint8_t ucHighByte, ucLowByte;
\r
405 /* Using 16bit timer 1 to generate the tick. Correct fuses must be
\r
406 selected for the configCPU_CLOCK_HZ clock. */
\r
408 ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
\r
410 /* We only have 16 bits so have to scale to get our required tick rate. */
\r
411 ulCompareMatch /= portCLOCK_PRESCALER;
\r
413 /* Adjust for correct value. */
\r
414 ulCompareMatch -= ( uint32_t ) 1;
\r
416 /* Setup compare match value for compare match A. Interrupts are disabled
\r
417 before this is called so we need not worry here. */
\r
418 ucLowByte = ( uint8_t ) ( ulCompareMatch & ( uint32_t ) 0xff );
\r
419 ulCompareMatch >>= 8;
\r
420 ucHighByte = ( uint8_t ) ( ulCompareMatch & ( uint32_t ) 0xff );
\r
421 OCR1AH = ucHighByte;
\r
422 OCR1AL = ucLowByte;
\r
424 /* Setup clock source and compare match behaviour. */
\r
425 ucLowByte = portCLEAR_COUNTER_ON_MATCH | portPRESCALE_64;
\r
426 TCCR1B = ucLowByte;
\r
428 /* Enable the interrupt - this is okay as interrupt are currently globally
\r
431 ucLowByte |= portCOMPARE_MATCH_A_INTERRUPT_ENABLE;
\r
434 /*-----------------------------------------------------------*/
\r
436 #if configUSE_PREEMPTION == 1
\r
439 * Tick ISR for preemptive scheduler. We can use a naked attribute as
\r
440 * the context is saved at the start of vPortYieldFromTick(). The tick
\r
441 * count is incremented after the context is saved.
\r
443 void SIG_OUTPUT_COMPARE1A( void ) __attribute__ ( ( signal, naked ) );
\r
444 void SIG_OUTPUT_COMPARE1A( void )
\r
446 vPortYieldFromTick();
\r
447 asm volatile ( "reti" );
\r
452 * Tick ISR for the cooperative scheduler. All this does is increment the
\r
453 * tick count. We don't need to switch context, this can only be done by
\r
454 * manual calls to taskYIELD();
\r
456 void SIG_OUTPUT_COMPARE1A( void ) __attribute__ ( ( signal ) );
\r
457 void SIG_OUTPUT_COMPARE1A( void )
\r
459 xTaskIncrementTick();
\r