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
68 #include "FreeRTOS.h"
\r
71 /*-----------------------------------------------------------
\r
72 * Implementation of functions defined in portable.h for the AVR/IAR port.
\r
73 *----------------------------------------------------------*/
\r
75 /* Start tasks with interrupts enables. */
\r
76 #define portFLAGS_INT_ENABLED ( ( StackType_t ) 0x80 )
\r
78 /* Hardware constants for timer 1. */
\r
79 #define portCLEAR_COUNTER_ON_MATCH ( ( uint8_t ) 0x08 )
\r
80 #define portPRESCALE_64 ( ( uint8_t ) 0x03 )
\r
81 #define portCLOCK_PRESCALER ( ( uint32_t ) 64 )
\r
82 #define portCOMPARE_MATCH_A_INTERRUPT_ENABLE ( ( uint8_t ) 0x10 )
\r
84 /* The number of bytes used on the hardware stack by the task start address. */
\r
85 #define portBYTES_USED_BY_RETURN_ADDRESS ( 2 )
\r
86 /*-----------------------------------------------------------*/
\r
88 /* Stores the critical section nesting. This must not be initialised to 0.
\r
89 It will be initialised when a task starts. */
\r
90 #define portNO_CRITICAL_NESTING ( ( UBaseType_t ) 0 )
\r
91 UBaseType_t uxCriticalNesting = 0x50;
\r
95 * Perform hardware setup to enable ticks from timer 1, compare match A.
\r
97 static void prvSetupTimerInterrupt( void );
\r
100 * The IAR compiler does not have full support for inline assembler, so
\r
101 * these are defined in the portmacro assembler file.
\r
103 extern void vPortYieldFromTick( void );
\r
104 extern void vPortStart( void );
\r
106 /*-----------------------------------------------------------*/
\r
109 * See header file for description.
\r
111 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
113 uint16_t usAddress;
\r
114 StackType_t *pxTopOfHardwareStack;
\r
116 /* Place a few bytes of known values on the bottom of the stack.
\r
117 This is just useful for debugging. */
\r
119 *pxTopOfStack = 0x11;
\r
121 *pxTopOfStack = 0x22;
\r
123 *pxTopOfStack = 0x33;
\r
126 /* Remember where the top of the hardware stack is - this is required
\r
128 pxTopOfHardwareStack = pxTopOfStack;
\r
131 /* Simulate how the stack would look after a call to vPortYield(). */
\r
133 /*lint -e950 -e611 -e923 Lint doesn't like this much - but nothing I can do about it. */
\r
137 /* The IAR compiler requires two stacks per task. First there is the
\r
138 hardware call stack which uses the AVR stack pointer. Second there is the
\r
139 software stack (local variables, parameter passing, etc.) which uses the
\r
142 This function places both stacks within the memory block passed in as the
\r
143 first parameter. The hardware stack is placed at the bottom of the memory
\r
144 block. A gap is then left for the hardware stack to grow. Next the software
\r
145 stack is placed. The amount of space between the software and hardware
\r
146 stacks is defined by configCALL_STACK_SIZE.
\r
150 The first part of the stack is the hardware stack. Place the start
\r
151 address of the task on the hardware stack. */
\r
152 usAddress = ( uint16_t ) pxCode;
\r
153 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
\r
157 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
\r
161 /* Leave enough space for the hardware stack before starting the software
\r
162 stack. The '- 2' is because we have already used two spaces for the
\r
163 address of the start of the task. */
\r
164 pxTopOfStack -= ( configCALL_STACK_SIZE - 2 );
\r
168 /* Next simulate the stack as if after a call to portSAVE_CONTEXT().
\r
169 portSAVE_CONTEXT places the flags on the stack immediately after r0
\r
170 to ensure the interrupts get disabled as soon as possible, and so ensuring
\r
171 the stack use is minimal should a context switch interrupt occur. */
\r
172 *pxTopOfStack = ( StackType_t ) 0x00; /* R0 */
\r
174 *pxTopOfStack = portFLAGS_INT_ENABLED;
\r
177 /* Next place the address of the hardware stack. This is required so
\r
178 the AVR stack pointer can be restored to point to the hardware stack. */
\r
179 pxTopOfHardwareStack -= portBYTES_USED_BY_RETURN_ADDRESS;
\r
180 usAddress = ( uint16_t ) pxTopOfHardwareStack;
\r
183 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
\r
188 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
\r
194 /* Now the remaining registers. */
\r
195 *pxTopOfStack = ( StackType_t ) 0x01; /* R1 */
\r
197 *pxTopOfStack = ( StackType_t ) 0x02; /* R2 */
\r
199 *pxTopOfStack = ( StackType_t ) 0x03; /* R3 */
\r
201 *pxTopOfStack = ( StackType_t ) 0x04; /* R4 */
\r
203 *pxTopOfStack = ( StackType_t ) 0x05; /* R5 */
\r
205 *pxTopOfStack = ( StackType_t ) 0x06; /* R6 */
\r
207 *pxTopOfStack = ( StackType_t ) 0x07; /* R7 */
\r
209 *pxTopOfStack = ( StackType_t ) 0x08; /* R8 */
\r
211 *pxTopOfStack = ( StackType_t ) 0x09; /* R9 */
\r
213 *pxTopOfStack = ( StackType_t ) 0x10; /* R10 */
\r
215 *pxTopOfStack = ( StackType_t ) 0x11; /* R11 */
\r
217 *pxTopOfStack = ( StackType_t ) 0x12; /* R12 */
\r
219 *pxTopOfStack = ( StackType_t ) 0x13; /* R13 */
\r
221 *pxTopOfStack = ( StackType_t ) 0x14; /* R14 */
\r
223 *pxTopOfStack = ( StackType_t ) 0x15; /* R15 */
\r
226 /* Place the parameter on the stack in the expected location. */
\r
227 usAddress = ( uint16_t ) pvParameters;
\r
228 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
\r
232 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
\r
235 *pxTopOfStack = ( StackType_t ) 0x18; /* R18 */
\r
237 *pxTopOfStack = ( StackType_t ) 0x19; /* R19 */
\r
239 *pxTopOfStack = ( StackType_t ) 0x20; /* R20 */
\r
241 *pxTopOfStack = ( StackType_t ) 0x21; /* R21 */
\r
243 *pxTopOfStack = ( StackType_t ) 0x22; /* R22 */
\r
245 *pxTopOfStack = ( StackType_t ) 0x23; /* R23 */
\r
247 *pxTopOfStack = ( StackType_t ) 0x24; /* R24 */
\r
249 *pxTopOfStack = ( StackType_t ) 0x25; /* R25 */
\r
251 *pxTopOfStack = ( StackType_t ) 0x26; /* R26 X */
\r
253 *pxTopOfStack = ( StackType_t ) 0x27; /* R27 */
\r
256 /* The Y register is not stored as it is used as the software stack and
\r
257 gets saved into the task control block. */
\r
259 *pxTopOfStack = ( StackType_t ) 0x30; /* R30 Z */
\r
261 *pxTopOfStack = ( StackType_t ) 0x031; /* R31 */
\r
264 *pxTopOfStack = portNO_CRITICAL_NESTING; /* Critical nesting is zero when the task starts. */
\r
266 /*lint +e950 +e611 +e923 */
\r
268 return pxTopOfStack;
\r
270 /*-----------------------------------------------------------*/
\r
272 BaseType_t xPortStartScheduler( void )
\r
274 /* Setup the hardware to generate the tick. */
\r
275 prvSetupTimerInterrupt();
\r
277 /* Restore the context of the first task that is going to run.
\r
278 Normally we would just call portRESTORE_CONTEXT() here, but as the IAR
\r
279 compiler does not fully support inline assembler we have to make a call.*/
\r
282 /* Should not get here! */
\r
285 /*-----------------------------------------------------------*/
\r
287 void vPortEndScheduler( void )
\r
289 /* It is unlikely that the AVR port will get stopped. If required simply
\r
290 disable the tick interrupt here. */
\r
292 /*-----------------------------------------------------------*/
\r
295 * Setup timer 1 compare match A to generate a tick interrupt.
\r
297 static void prvSetupTimerInterrupt( void )
\r
299 uint32_t ulCompareMatch;
\r
300 uint8_t ucHighByte, ucLowByte;
\r
302 /* Using 16bit timer 1 to generate the tick. Correct fuses must be
\r
303 selected for the configCPU_CLOCK_HZ clock. */
\r
305 ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
\r
307 /* We only have 16 bits so have to scale to get our required tick rate. */
\r
308 ulCompareMatch /= portCLOCK_PRESCALER;
\r
310 /* Adjust for correct value. */
\r
311 ulCompareMatch -= ( uint32_t ) 1;
\r
313 /* Setup compare match value for compare match A. Interrupts are disabled
\r
314 before this is called so we need not worry here. */
\r
315 ucLowByte = ( uint8_t ) ( ulCompareMatch & ( uint32_t ) 0xff );
\r
316 ulCompareMatch >>= 8;
\r
317 ucHighByte = ( uint8_t ) ( ulCompareMatch & ( uint32_t ) 0xff );
\r
318 OCR1AH = ucHighByte;
\r
319 OCR1AL = ucLowByte;
\r
321 /* Setup clock source and compare match behaviour. */
\r
322 ucLowByte = portCLEAR_COUNTER_ON_MATCH | portPRESCALE_64;
\r
323 TCCR1B = ucLowByte;
\r
325 /* Enable the interrupt - this is okay as interrupt are currently globally
\r
327 TIMSK |= portCOMPARE_MATCH_A_INTERRUPT_ENABLE;
\r
329 /*-----------------------------------------------------------*/
\r
331 #if configUSE_PREEMPTION == 1
\r
334 * Tick ISR for preemptive scheduler. We can use a __task attribute as
\r
335 * the context is saved at the start of vPortYieldFromTick(). The tick
\r
336 * count is incremented after the context is saved.
\r
338 __task void SIG_OUTPUT_COMPARE1A( void )
\r
340 vPortYieldFromTick();
\r
347 * Tick ISR for the cooperative scheduler. All this does is increment the
\r
348 * tick count. We don't need to switch context, this can only be done by
\r
349 * manual calls to taskYIELD();
\r
351 * THE INTERRUPT VECTOR IS POPULATED IN portmacro.s90. DO NOT INSTALL
\r
352 * IT HERE USING THE USUAL PRAGMA.
\r
354 __interrupt void SIG_OUTPUT_COMPARE1A( void )
\r
356 xTaskIncrementTick();
\r
359 /*-----------------------------------------------------------*/
\r
361 void vPortEnterCritical( void )
\r
363 portDISABLE_INTERRUPTS();
\r
364 uxCriticalNesting++;
\r
366 /*-----------------------------------------------------------*/
\r
368 void vPortExitCritical( void )
\r
370 uxCriticalNesting--;
\r
371 if( uxCriticalNesting == portNO_CRITICAL_NESTING )
\r
373 portENABLE_INTERRUPTS();
\r