2 FreeRTOS.org V4.2.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 for an IEC 61508 compliant version along
\r
32 with commercial development and support options.
\r
33 ***************************************************************************
\r
38 #include "FreeRTOS.h"
\r
41 /*-----------------------------------------------------------
\r
42 * Implementation of functions defined in portable.h for the AVR/IAR port.
\r
43 *----------------------------------------------------------*/
\r
45 /* Start tasks with interrupts enables. */
\r
46 #define portFLAGS_INT_ENABLED ( ( portSTACK_TYPE ) 0x80 )
\r
48 /* Hardware constants for timer 1. */
\r
49 #define portCLEAR_COUNTER_ON_MATCH ( ( unsigned portCHAR ) 0x08 )
\r
50 #define portPRESCALE_64 ( ( unsigned portCHAR ) 0x03 )
\r
51 #define portCLOCK_PRESCALER ( ( unsigned portLONG ) 64 )
\r
52 #define portCOMPARE_MATCH_A_INTERRUPT_ENABLE ( ( unsigned portCHAR ) 0x10 )
\r
54 /* The number of bytes used on the hardware stack by the task start address. */
\r
55 #define portBYTES_USED_BY_RETURN_ADDRESS ( 2 )
\r
56 /*-----------------------------------------------------------*/
\r
58 /* Stores the critical section nesting. This must not be initialised to 0.
\r
59 It will be initialised when a task starts. */
\r
60 #define portNO_CRITICAL_NESTING ( ( unsigned portBASE_TYPE ) 0 )
\r
61 unsigned portBASE_TYPE uxCriticalNesting = 0x50;
\r
65 * Perform hardware setup to enable ticks from timer 1, compare match A.
\r
67 static void prvSetupTimerInterrupt( void );
\r
70 * The IAR compiler does not have full support for inline assembler, so
\r
71 * these are defined in the portmacro assembler file.
\r
73 extern void vPortYieldFromTick( void );
\r
74 extern void vPortStart( void );
\r
76 /*-----------------------------------------------------------*/
\r
79 * See header file for description.
\r
81 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
83 unsigned portSHORT usAddress;
\r
84 portSTACK_TYPE *pxTopOfHardwareStack;
\r
86 /* Place a few bytes of known values on the bottom of the stack.
\r
87 This is just useful for debugging. */
\r
89 *pxTopOfStack = 0x11;
\r
91 *pxTopOfStack = 0x22;
\r
93 *pxTopOfStack = 0x33;
\r
96 /* Remember where the top of the hardware stack is - this is required
\r
98 pxTopOfHardwareStack = pxTopOfStack;
\r
101 /* Simulate how the stack would look after a call to vPortYield(). */
\r
103 /*lint -e950 -e611 -e923 Lint doesn't like this much - but nothing I can do about it. */
\r
107 /* The IAR compiler requires two stacks per task. First there is the
\r
108 hardware call stack which uses the AVR stack pointer. Second there is the
\r
109 software stack (local variables, parameter passing, etc.) which uses the
\r
112 This function places both stacks within the memory block passed in as the
\r
113 first parameter. The hardware stack is placed at the bottom of the memory
\r
114 block. A gap is then left for the hardware stack to grow. Next the software
\r
115 stack is placed. The amount of space between the software and hardware
\r
116 stacks is defined by configCALL_STACK_SIZE.
\r
120 The first part of the stack is the hardware stack. Place the start
\r
121 address of the task on the hardware stack. */
\r
122 usAddress = ( unsigned portSHORT ) pxCode;
\r
123 *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned portSHORT ) 0x00ff );
\r
127 *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned portSHORT ) 0x00ff );
\r
131 /* Leave enough space for the hardware stack before starting the software
\r
132 stack. The '- 2' is because we have already used two spaces for the
\r
133 address of the start of the task. */
\r
134 pxTopOfStack -= ( configCALL_STACK_SIZE - 2 );
\r
138 /* Next simulate the stack as if after a call to portSAVE_CONTEXT().
\r
139 portSAVE_CONTEXT places the flags on the stack immediately after r0
\r
140 to ensure the interrupts get disabled as soon as possible, and so ensuring
\r
141 the stack use is minimal should a context switch interrupt occur. */
\r
142 *pxTopOfStack = ( portSTACK_TYPE ) 0x00; /* R0 */
\r
144 *pxTopOfStack = portFLAGS_INT_ENABLED;
\r
147 /* Next place the address of the hardware stack. This is required so
\r
148 the AVR stack pointer can be restored to point to the hardware stack. */
\r
149 pxTopOfHardwareStack -= portBYTES_USED_BY_RETURN_ADDRESS;
\r
150 usAddress = ( unsigned portSHORT ) pxTopOfHardwareStack;
\r
153 *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned portSHORT ) 0x00ff );
\r
158 *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned portSHORT ) 0x00ff );
\r
164 /* Now the remaining registers. */
\r
165 *pxTopOfStack = ( portSTACK_TYPE ) 0x01; /* R1 */
\r
167 *pxTopOfStack = ( portSTACK_TYPE ) 0x02; /* R2 */
\r
169 *pxTopOfStack = ( portSTACK_TYPE ) 0x03; /* R3 */
\r
171 *pxTopOfStack = ( portSTACK_TYPE ) 0x04; /* R4 */
\r
173 *pxTopOfStack = ( portSTACK_TYPE ) 0x05; /* R5 */
\r
175 *pxTopOfStack = ( portSTACK_TYPE ) 0x06; /* R6 */
\r
177 *pxTopOfStack = ( portSTACK_TYPE ) 0x07; /* R7 */
\r
179 *pxTopOfStack = ( portSTACK_TYPE ) 0x08; /* R8 */
\r
181 *pxTopOfStack = ( portSTACK_TYPE ) 0x09; /* R9 */
\r
183 *pxTopOfStack = ( portSTACK_TYPE ) 0x10; /* R10 */
\r
185 *pxTopOfStack = ( portSTACK_TYPE ) 0x11; /* R11 */
\r
187 *pxTopOfStack = ( portSTACK_TYPE ) 0x12; /* R12 */
\r
189 *pxTopOfStack = ( portSTACK_TYPE ) 0x13; /* R13 */
\r
191 *pxTopOfStack = ( portSTACK_TYPE ) 0x14; /* R14 */
\r
193 *pxTopOfStack = ( portSTACK_TYPE ) 0x15; /* R15 */
\r
196 /* Place the parameter on the stack in the expected location. */
\r
197 usAddress = ( unsigned portSHORT ) pvParameters;
\r
198 *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned portSHORT ) 0x00ff );
\r
202 *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned portSHORT ) 0x00ff );
\r
205 *pxTopOfStack = ( portSTACK_TYPE ) 0x18; /* R18 */
\r
207 *pxTopOfStack = ( portSTACK_TYPE ) 0x19; /* R19 */
\r
209 *pxTopOfStack = ( portSTACK_TYPE ) 0x20; /* R20 */
\r
211 *pxTopOfStack = ( portSTACK_TYPE ) 0x21; /* R21 */
\r
213 *pxTopOfStack = ( portSTACK_TYPE ) 0x22; /* R22 */
\r
215 *pxTopOfStack = ( portSTACK_TYPE ) 0x23; /* R23 */
\r
217 *pxTopOfStack = ( portSTACK_TYPE ) 0x24; /* R24 */
\r
219 *pxTopOfStack = ( portSTACK_TYPE ) 0x25; /* R25 */
\r
221 *pxTopOfStack = ( portSTACK_TYPE ) 0x26; /* R26 X */
\r
223 *pxTopOfStack = ( portSTACK_TYPE ) 0x27; /* R27 */
\r
226 /* The Y register is not stored as it is used as the software stack and
\r
227 gets saved into the task control block. */
\r
229 *pxTopOfStack = ( portSTACK_TYPE ) 0x30; /* R30 Z */
\r
231 *pxTopOfStack = ( portSTACK_TYPE ) 0x031; /* R31 */
\r
234 *pxTopOfStack = portNO_CRITICAL_NESTING; /* Critical nesting is zero when the task starts. */
\r
236 /*lint +e950 +e611 +e923 */
\r
238 return pxTopOfStack;
\r
240 /*-----------------------------------------------------------*/
\r
242 portBASE_TYPE xPortStartScheduler( void )
\r
244 /* Setup the hardware to generate the tick. */
\r
245 prvSetupTimerInterrupt();
\r
247 /* Restore the context of the first task that is going to run.
\r
248 Normally we would just call portRESTORE_CONTEXT() here, but as the IAR
\r
249 compiler does not fully support inline assembler we have to make a call.*/
\r
253 /* Should not get here! */
\r
256 /*-----------------------------------------------------------*/
\r
258 void vPortEndScheduler( void )
\r
260 /* It is unlikely that the AVR port will get stopped. If required simply
\r
261 disable the tick interrupt here. */
\r
263 /*-----------------------------------------------------------*/
\r
266 * Setup timer 1 compare match A to generate a tick interrupt.
\r
268 static void prvSetupTimerInterrupt( void )
\r
270 unsigned portLONG ulCompareMatch;
\r
271 unsigned portCHAR ucHighByte, ucLowByte;
\r
273 /* Using 16bit timer 1 to generate the tick. Correct fuses must be
\r
274 selected for the configCPU_CLOCK_HZ clock. */
\r
276 ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
\r
278 /* We only have 16 bits so have to scale to get our required tick rate. */
\r
279 ulCompareMatch /= portCLOCK_PRESCALER;
\r
281 /* Adjust for correct value. */
\r
282 ulCompareMatch -= ( unsigned portLONG ) 1;
\r
284 /* Setup compare match value for compare match A. Interrupts are disabled
\r
285 before this is called so we need not worry here. */
\r
286 ucLowByte = ( unsigned portCHAR ) ( ulCompareMatch & ( unsigned portLONG ) 0xff );
\r
287 ulCompareMatch >>= 8;
\r
288 ucHighByte = ( unsigned portCHAR ) ( ulCompareMatch & ( unsigned portLONG ) 0xff );
\r
289 OCR1AH = ucHighByte;
\r
290 OCR1AL = ucLowByte;
\r
292 /* Setup clock source and compare match behaviour. */
\r
293 ucLowByte = portCLEAR_COUNTER_ON_MATCH | portPRESCALE_64;
\r
294 TCCR1B = ucLowByte;
\r
296 /* Enable the interrupt - this is okay as interrupt are currently globally
\r
298 TIMSK |= portCOMPARE_MATCH_A_INTERRUPT_ENABLE;
\r
300 /*-----------------------------------------------------------*/
\r
302 #if configUSE_PREEMPTION == 1
\r
305 * Tick ISR for preemptive scheduler. We can use a __task attribute as
\r
306 * the context is saved at the start of vPortYieldFromTick(). The tick
\r
307 * count is incremented after the context is saved.
\r
309 __task void SIG_OUTPUT_COMPARE1A( void )
\r
311 vPortYieldFromTick();
\r
318 * Tick ISR for the cooperative scheduler. All this does is increment the
\r
319 * tick count. We don't need to switch context, this can only be done by
\r
320 * manual calls to taskYIELD();
\r
322 * THE INTERRUPT VECTOR IS POPULATED IN portmacro.s90. DO NOT INSTALL
\r
323 * IT HERE USING THE USUAL PRAGMA.
\r
325 __interrupt void SIG_OUTPUT_COMPARE1A( void )
\r
327 vTaskIncrementTick();
\r
330 /*-----------------------------------------------------------*/
\r
332 void vPortEnterCritical( void )
\r
334 portDISABLE_INTERRUPTS();
\r
335 uxCriticalNesting++;
\r
337 /*-----------------------------------------------------------*/
\r
339 void vPortExitCritical( void )
\r
341 uxCriticalNesting--;
\r
342 if( uxCriticalNesting == portNO_CRITICAL_NESTING )
\r
344 portENABLE_INTERRUPTS();
\r