2 FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
69 #include "FreeRTOS.h"
\r
72 /*-----------------------------------------------------------
\r
73 * Implementation of functions defined in portable.h for the AVR/IAR port.
\r
74 *----------------------------------------------------------*/
\r
76 /* Start tasks with interrupts enables. */
\r
77 #define portFLAGS_INT_ENABLED ( ( portSTACK_TYPE ) 0x80 )
\r
79 /* Hardware constants for timer 1. */
\r
80 #define portCLEAR_COUNTER_ON_MATCH ( ( unsigned char ) 0x08 )
\r
81 #define portPRESCALE_64 ( ( unsigned char ) 0x03 )
\r
82 #define portCLOCK_PRESCALER ( ( unsigned long ) 64 )
\r
83 #define portCOMPARE_MATCH_A_INTERRUPT_ENABLE ( ( unsigned char ) 0x10 )
\r
85 /* The number of bytes used on the hardware stack by the task start address. */
\r
86 #define portBYTES_USED_BY_RETURN_ADDRESS ( 2 )
\r
87 /*-----------------------------------------------------------*/
\r
89 /* Stores the critical section nesting. This must not be initialised to 0.
\r
90 It will be initialised when a task starts. */
\r
91 #define portNO_CRITICAL_NESTING ( ( unsigned portBASE_TYPE ) 0 )
\r
92 unsigned portBASE_TYPE uxCriticalNesting = 0x50;
\r
96 * Perform hardware setup to enable ticks from timer 1, compare match A.
\r
98 static void prvSetupTimerInterrupt( void );
\r
101 * The IAR compiler does not have full support for inline assembler, so
\r
102 * these are defined in the portmacro assembler file.
\r
104 extern void vPortYieldFromTick( void );
\r
105 extern void vPortStart( void );
\r
107 /*-----------------------------------------------------------*/
\r
110 * See header file for description.
\r
112 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
114 unsigned short usAddress;
\r
115 portSTACK_TYPE *pxTopOfHardwareStack;
\r
117 /* Place a few bytes of known values on the bottom of the stack.
\r
118 This is just useful for debugging. */
\r
120 *pxTopOfStack = 0x11;
\r
122 *pxTopOfStack = 0x22;
\r
124 *pxTopOfStack = 0x33;
\r
127 /* Remember where the top of the hardware stack is - this is required
\r
129 pxTopOfHardwareStack = pxTopOfStack;
\r
132 /* Simulate how the stack would look after a call to vPortYield(). */
\r
134 /*lint -e950 -e611 -e923 Lint doesn't like this much - but nothing I can do about it. */
\r
138 /* The IAR compiler requires two stacks per task. First there is the
\r
139 hardware call stack which uses the AVR stack pointer. Second there is the
\r
140 software stack (local variables, parameter passing, etc.) which uses the
\r
143 This function places both stacks within the memory block passed in as the
\r
144 first parameter. The hardware stack is placed at the bottom of the memory
\r
145 block. A gap is then left for the hardware stack to grow. Next the software
\r
146 stack is placed. The amount of space between the software and hardware
\r
147 stacks is defined by configCALL_STACK_SIZE.
\r
151 The first part of the stack is the hardware stack. Place the start
\r
152 address of the task on the hardware stack. */
\r
153 usAddress = ( unsigned short ) pxCode;
\r
154 *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned short ) 0x00ff );
\r
158 *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned short ) 0x00ff );
\r
162 /* Leave enough space for the hardware stack before starting the software
\r
163 stack. The '- 2' is because we have already used two spaces for the
\r
164 address of the start of the task. */
\r
165 pxTopOfStack -= ( configCALL_STACK_SIZE - 2 );
\r
169 /* Next simulate the stack as if after a call to portSAVE_CONTEXT().
\r
170 portSAVE_CONTEXT places the flags on the stack immediately after r0
\r
171 to ensure the interrupts get disabled as soon as possible, and so ensuring
\r
172 the stack use is minimal should a context switch interrupt occur. */
\r
173 *pxTopOfStack = ( portSTACK_TYPE ) 0x00; /* R0 */
\r
175 *pxTopOfStack = portFLAGS_INT_ENABLED;
\r
178 /* Next place the address of the hardware stack. This is required so
\r
179 the AVR stack pointer can be restored to point to the hardware stack. */
\r
180 pxTopOfHardwareStack -= portBYTES_USED_BY_RETURN_ADDRESS;
\r
181 usAddress = ( unsigned short ) pxTopOfHardwareStack;
\r
184 *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned short ) 0x00ff );
\r
189 *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned short ) 0x00ff );
\r
195 /* Now the remaining registers. */
\r
196 *pxTopOfStack = ( portSTACK_TYPE ) 0x01; /* R1 */
\r
198 *pxTopOfStack = ( portSTACK_TYPE ) 0x02; /* R2 */
\r
200 *pxTopOfStack = ( portSTACK_TYPE ) 0x03; /* R3 */
\r
202 *pxTopOfStack = ( portSTACK_TYPE ) 0x04; /* R4 */
\r
204 *pxTopOfStack = ( portSTACK_TYPE ) 0x05; /* R5 */
\r
206 *pxTopOfStack = ( portSTACK_TYPE ) 0x06; /* R6 */
\r
208 *pxTopOfStack = ( portSTACK_TYPE ) 0x07; /* R7 */
\r
210 *pxTopOfStack = ( portSTACK_TYPE ) 0x08; /* R8 */
\r
212 *pxTopOfStack = ( portSTACK_TYPE ) 0x09; /* R9 */
\r
214 *pxTopOfStack = ( portSTACK_TYPE ) 0x10; /* R10 */
\r
216 *pxTopOfStack = ( portSTACK_TYPE ) 0x11; /* R11 */
\r
218 *pxTopOfStack = ( portSTACK_TYPE ) 0x12; /* R12 */
\r
220 *pxTopOfStack = ( portSTACK_TYPE ) 0x13; /* R13 */
\r
222 *pxTopOfStack = ( portSTACK_TYPE ) 0x14; /* R14 */
\r
224 *pxTopOfStack = ( portSTACK_TYPE ) 0x15; /* R15 */
\r
227 /* Place the parameter on the stack in the expected location. */
\r
228 usAddress = ( unsigned short ) pvParameters;
\r
229 *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned short ) 0x00ff );
\r
233 *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned short ) 0x00ff );
\r
236 *pxTopOfStack = ( portSTACK_TYPE ) 0x18; /* R18 */
\r
238 *pxTopOfStack = ( portSTACK_TYPE ) 0x19; /* R19 */
\r
240 *pxTopOfStack = ( portSTACK_TYPE ) 0x20; /* R20 */
\r
242 *pxTopOfStack = ( portSTACK_TYPE ) 0x21; /* R21 */
\r
244 *pxTopOfStack = ( portSTACK_TYPE ) 0x22; /* R22 */
\r
246 *pxTopOfStack = ( portSTACK_TYPE ) 0x23; /* R23 */
\r
248 *pxTopOfStack = ( portSTACK_TYPE ) 0x24; /* R24 */
\r
250 *pxTopOfStack = ( portSTACK_TYPE ) 0x25; /* R25 */
\r
252 *pxTopOfStack = ( portSTACK_TYPE ) 0x26; /* R26 X */
\r
254 *pxTopOfStack = ( portSTACK_TYPE ) 0x27; /* R27 */
\r
257 /* The Y register is not stored as it is used as the software stack and
\r
258 gets saved into the task control block. */
\r
260 *pxTopOfStack = ( portSTACK_TYPE ) 0x30; /* R30 Z */
\r
262 *pxTopOfStack = ( portSTACK_TYPE ) 0x031; /* R31 */
\r
265 *pxTopOfStack = portNO_CRITICAL_NESTING; /* Critical nesting is zero when the task starts. */
\r
267 /*lint +e950 +e611 +e923 */
\r
269 return pxTopOfStack;
\r
271 /*-----------------------------------------------------------*/
\r
273 portBASE_TYPE xPortStartScheduler( void )
\r
275 /* Setup the hardware to generate the tick. */
\r
276 prvSetupTimerInterrupt();
\r
278 /* Restore the context of the first task that is going to run.
\r
279 Normally we would just call portRESTORE_CONTEXT() here, but as the IAR
\r
280 compiler does not fully support inline assembler we have to make a call.*/
\r
284 /* Should not get here! */
\r
287 /*-----------------------------------------------------------*/
\r
289 void vPortEndScheduler( void )
\r
291 /* It is unlikely that the AVR port will get stopped. If required simply
\r
292 disable the tick interrupt here. */
\r
294 /*-----------------------------------------------------------*/
\r
297 * Setup timer 1 compare match A to generate a tick interrupt.
\r
299 static void prvSetupTimerInterrupt( void )
\r
301 unsigned long ulCompareMatch;
\r
302 unsigned char ucHighByte, ucLowByte;
\r
304 /* Using 16bit timer 1 to generate the tick. Correct fuses must be
\r
305 selected for the configCPU_CLOCK_HZ clock. */
\r
307 ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
\r
309 /* We only have 16 bits so have to scale to get our required tick rate. */
\r
310 ulCompareMatch /= portCLOCK_PRESCALER;
\r
312 /* Adjust for correct value. */
\r
313 ulCompareMatch -= ( unsigned long ) 1;
\r
315 /* Setup compare match value for compare match A. Interrupts are disabled
\r
316 before this is called so we need not worry here. */
\r
317 ucLowByte = ( unsigned char ) ( ulCompareMatch & ( unsigned long ) 0xff );
\r
318 ulCompareMatch >>= 8;
\r
319 ucHighByte = ( unsigned char ) ( ulCompareMatch & ( unsigned long ) 0xff );
\r
320 OCR1AH = ucHighByte;
\r
321 OCR1AL = ucLowByte;
\r
323 /* Setup clock source and compare match behaviour. */
\r
324 ucLowByte = portCLEAR_COUNTER_ON_MATCH | portPRESCALE_64;
\r
325 TCCR1B = ucLowByte;
\r
327 /* Enable the interrupt - this is okay as interrupt are currently globally
\r
329 TIMSK |= portCOMPARE_MATCH_A_INTERRUPT_ENABLE;
\r
331 /*-----------------------------------------------------------*/
\r
333 #if configUSE_PREEMPTION == 1
\r
336 * Tick ISR for preemptive scheduler. We can use a __task attribute as
\r
337 * the context is saved at the start of vPortYieldFromTick(). The tick
\r
338 * count is incremented after the context is saved.
\r
340 __task void SIG_OUTPUT_COMPARE1A( void )
\r
342 vPortYieldFromTick();
\r
349 * Tick ISR for the cooperative scheduler. All this does is increment the
\r
350 * tick count. We don't need to switch context, this can only be done by
\r
351 * manual calls to taskYIELD();
\r
353 * THE INTERRUPT VECTOR IS POPULATED IN portmacro.s90. DO NOT INSTALL
\r
354 * IT HERE USING THE USUAL PRAGMA.
\r
356 __interrupt void SIG_OUTPUT_COMPARE1A( void )
\r
358 vTaskIncrementTick();
\r
361 /*-----------------------------------------------------------*/
\r
363 void vPortEnterCritical( void )
\r
365 portDISABLE_INTERRUPTS();
\r
366 uxCriticalNesting++;
\r
368 /*-----------------------------------------------------------*/
\r
370 void vPortExitCritical( void )
\r
372 uxCriticalNesting--;
\r
373 if( uxCriticalNesting == portNO_CRITICAL_NESTING )
\r
375 portENABLE_INTERRUPTS();
\r