2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
69 + AVR port - Replaced the inb() and outb() functions with direct memory
\r
70 access. This allows the port to be built with the 20050414 build of
\r
75 #include <avr/interrupt.h>
\r
77 #include "FreeRTOS.h"
\r
80 /*-----------------------------------------------------------
\r
81 * Implementation of functions defined in portable.h for the AVR port.
\r
82 *----------------------------------------------------------*/
\r
84 /* Start tasks with interrupts enables. */
\r
85 #define portFLAGS_INT_ENABLED ( ( portSTACK_TYPE ) 0x80 )
\r
87 /* Hardware constants for timer 1. */
\r
88 #define portCLEAR_COUNTER_ON_MATCH ( ( unsigned char ) 0x08 )
\r
89 #define portPRESCALE_64 ( ( unsigned char ) 0x03 )
\r
90 #define portCLOCK_PRESCALER ( ( unsigned long ) 64 )
\r
91 #define portCOMPARE_MATCH_A_INTERRUPT_ENABLE ( ( unsigned char ) 0x10 )
\r
93 /*-----------------------------------------------------------*/
\r
95 /* We require the address of the pxCurrentTCB variable, but don't want to know
\r
96 any details of its type. */
\r
97 typedef void tskTCB;
\r
98 extern volatile tskTCB * volatile pxCurrentTCB;
\r
100 /*-----------------------------------------------------------*/
\r
103 * Macro to save all the general purpose registers, the save the stack pointer
\r
106 * The first thing we do is save the flags then disable interrupts. This is to
\r
107 * guard our stack against having a context switch interrupt after we have already
\r
108 * pushed the registers onto the stack - causing the 32 registers to be on the
\r
111 * r1 is set to zero as the compiler expects it to be thus, however some
\r
112 * of the math routines make use of R1.
\r
114 * The interrupts will have been disabled during the call to portSAVE_CONTEXT()
\r
115 * so we need not worry about reading/writing to the stack pointer.
\r
118 #define portSAVE_CONTEXT() \
\r
119 asm volatile ( "push r0 \n\t" \
\r
120 "in r0, __SREG__ \n\t" \
\r
155 "lds r26, pxCurrentTCB \n\t" \
\r
156 "lds r27, pxCurrentTCB + 1 \n\t" \
\r
157 "in r0, 0x3d \n\t" \
\r
159 "in r0, 0x3e \n\t" \
\r
164 * Opposite to portSAVE_CONTEXT(). Interrupts will have been disabled during
\r
165 * the context save so we can write to the stack pointer.
\r
168 #define portRESTORE_CONTEXT() \
\r
169 asm volatile ( "lds r26, pxCurrentTCB \n\t" \
\r
170 "lds r27, pxCurrentTCB + 1 \n\t" \
\r
171 "ld r28, x+ \n\t" \
\r
172 "out __SP_L__, r28 \n\t" \
\r
173 "ld r29, x+ \n\t" \
\r
174 "out __SP_H__, r29 \n\t" \
\r
207 "out __SREG__, r0 \n\t" \
\r
211 /*-----------------------------------------------------------*/
\r
214 * Perform hardware setup to enable ticks from timer 1, compare match A.
\r
216 static void prvSetupTimerInterrupt( void );
\r
217 /*-----------------------------------------------------------*/
\r
220 * See header file for description.
\r
222 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
224 unsigned short usAddress;
\r
226 /* Place a few bytes of known values on the bottom of the stack.
\r
227 This is just useful for debugging. */
\r
229 *pxTopOfStack = 0x11;
\r
231 *pxTopOfStack = 0x22;
\r
233 *pxTopOfStack = 0x33;
\r
236 /* Simulate how the stack would look after a call to vPortYield() generated by
\r
239 /*lint -e950 -e611 -e923 Lint doesn't like this much - but nothing I can do about it. */
\r
241 /* The start of the task code will be popped off the stack last, so place
\r
243 usAddress = ( unsigned short ) pxCode;
\r
244 *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned short ) 0x00ff );
\r
248 *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned short ) 0x00ff );
\r
251 /* Next simulate the stack as if after a call to portSAVE_CONTEXT().
\r
252 portSAVE_CONTEXT places the flags on the stack immediately after r0
\r
253 to ensure the interrupts get disabled as soon as possible, and so ensuring
\r
254 the stack use is minimal should a context switch interrupt occur. */
\r
255 *pxTopOfStack = ( portSTACK_TYPE ) 0x00; /* R0 */
\r
257 *pxTopOfStack = portFLAGS_INT_ENABLED;
\r
261 /* Now the remaining registers. The compiler expects R1 to be 0. */
\r
262 *pxTopOfStack = ( portSTACK_TYPE ) 0x00; /* R1 */
\r
264 *pxTopOfStack = ( portSTACK_TYPE ) 0x02; /* R2 */
\r
266 *pxTopOfStack = ( portSTACK_TYPE ) 0x03; /* R3 */
\r
268 *pxTopOfStack = ( portSTACK_TYPE ) 0x04; /* R4 */
\r
270 *pxTopOfStack = ( portSTACK_TYPE ) 0x05; /* R5 */
\r
272 *pxTopOfStack = ( portSTACK_TYPE ) 0x06; /* R6 */
\r
274 *pxTopOfStack = ( portSTACK_TYPE ) 0x07; /* R7 */
\r
276 *pxTopOfStack = ( portSTACK_TYPE ) 0x08; /* R8 */
\r
278 *pxTopOfStack = ( portSTACK_TYPE ) 0x09; /* R9 */
\r
280 *pxTopOfStack = ( portSTACK_TYPE ) 0x10; /* R10 */
\r
282 *pxTopOfStack = ( portSTACK_TYPE ) 0x11; /* R11 */
\r
284 *pxTopOfStack = ( portSTACK_TYPE ) 0x12; /* R12 */
\r
286 *pxTopOfStack = ( portSTACK_TYPE ) 0x13; /* R13 */
\r
288 *pxTopOfStack = ( portSTACK_TYPE ) 0x14; /* R14 */
\r
290 *pxTopOfStack = ( portSTACK_TYPE ) 0x15; /* R15 */
\r
292 *pxTopOfStack = ( portSTACK_TYPE ) 0x16; /* R16 */
\r
294 *pxTopOfStack = ( portSTACK_TYPE ) 0x17; /* R17 */
\r
296 *pxTopOfStack = ( portSTACK_TYPE ) 0x18; /* R18 */
\r
298 *pxTopOfStack = ( portSTACK_TYPE ) 0x19; /* R19 */
\r
300 *pxTopOfStack = ( portSTACK_TYPE ) 0x20; /* R20 */
\r
302 *pxTopOfStack = ( portSTACK_TYPE ) 0x21; /* R21 */
\r
304 *pxTopOfStack = ( portSTACK_TYPE ) 0x22; /* R22 */
\r
306 *pxTopOfStack = ( portSTACK_TYPE ) 0x23; /* R23 */
\r
309 /* Place the parameter on the stack in the expected location. */
\r
310 usAddress = ( unsigned short ) pvParameters;
\r
311 *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned short ) 0x00ff );
\r
315 *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned short ) 0x00ff );
\r
318 *pxTopOfStack = ( portSTACK_TYPE ) 0x26; /* R26 X */
\r
320 *pxTopOfStack = ( portSTACK_TYPE ) 0x27; /* R27 */
\r
322 *pxTopOfStack = ( portSTACK_TYPE ) 0x28; /* R28 Y */
\r
324 *pxTopOfStack = ( portSTACK_TYPE ) 0x29; /* R29 */
\r
326 *pxTopOfStack = ( portSTACK_TYPE ) 0x30; /* R30 Z */
\r
328 *pxTopOfStack = ( portSTACK_TYPE ) 0x031; /* R31 */
\r
331 /*lint +e950 +e611 +e923 */
\r
333 return pxTopOfStack;
\r
335 /*-----------------------------------------------------------*/
\r
337 portBASE_TYPE xPortStartScheduler( void )
\r
339 /* Setup the hardware to generate the tick. */
\r
340 prvSetupTimerInterrupt();
\r
342 /* Restore the context of the first task that is going to run. */
\r
343 portRESTORE_CONTEXT();
\r
345 /* Simulate a function call end as generated by the compiler. We will now
\r
346 jump to the start of the task the context of which we have just restored. */
\r
347 asm volatile ( "ret" );
\r
349 /* Should not get here. */
\r
352 /*-----------------------------------------------------------*/
\r
354 void vPortEndScheduler( void )
\r
356 /* It is unlikely that the AVR port will get stopped. If required simply
\r
357 disable the tick interrupt here. */
\r
359 /*-----------------------------------------------------------*/
\r
362 * Manual context switch. The first thing we do is save the registers so we
\r
363 * can use a naked attribute.
\r
365 void vPortYield( void ) __attribute__ ( ( naked ) );
\r
366 void vPortYield( void )
\r
368 portSAVE_CONTEXT();
\r
369 vTaskSwitchContext();
\r
370 portRESTORE_CONTEXT();
\r
372 asm volatile ( "ret" );
\r
374 /*-----------------------------------------------------------*/
\r
377 * Context switch function used by the tick. This must be identical to
\r
378 * vPortYield() from the call to vTaskSwitchContext() onwards. The only
\r
379 * difference from vPortYield() is the tick count is incremented as the
\r
380 * call comes from the tick ISR.
\r
382 void vPortYieldFromTick( void ) __attribute__ ( ( naked ) );
\r
383 void vPortYieldFromTick( void )
\r
385 portSAVE_CONTEXT();
\r
386 if( xTaskIncrementTick() != pdFALSE )
\r
388 vTaskSwitchContext();
\r
390 portRESTORE_CONTEXT();
\r
392 asm volatile ( "ret" );
\r
394 /*-----------------------------------------------------------*/
\r
397 * Setup timer 1 compare match A to generate a tick interrupt.
\r
399 static void prvSetupTimerInterrupt( void )
\r
401 unsigned long ulCompareMatch;
\r
402 unsigned char ucHighByte, ucLowByte;
\r
404 /* Using 16bit timer 1 to generate the tick. Correct fuses must be
\r
405 selected for the configCPU_CLOCK_HZ clock. */
\r
407 ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
\r
409 /* We only have 16 bits so have to scale to get our required tick rate. */
\r
410 ulCompareMatch /= portCLOCK_PRESCALER;
\r
412 /* Adjust for correct value. */
\r
413 ulCompareMatch -= ( unsigned long ) 1;
\r
415 /* Setup compare match value for compare match A. Interrupts are disabled
\r
416 before this is called so we need not worry here. */
\r
417 ucLowByte = ( unsigned char ) ( ulCompareMatch & ( unsigned long ) 0xff );
\r
418 ulCompareMatch >>= 8;
\r
419 ucHighByte = ( unsigned char ) ( ulCompareMatch & ( unsigned long ) 0xff );
\r
420 OCR1AH = ucHighByte;
\r
421 OCR1AL = ucLowByte;
\r
423 /* Setup clock source and compare match behaviour. */
\r
424 ucLowByte = portCLEAR_COUNTER_ON_MATCH | portPRESCALE_64;
\r
425 TCCR1B = ucLowByte;
\r
427 /* Enable the interrupt - this is okay as interrupt are currently globally
\r
430 ucLowByte |= portCOMPARE_MATCH_A_INTERRUPT_ENABLE;
\r
433 /*-----------------------------------------------------------*/
\r
435 #if configUSE_PREEMPTION == 1
\r
438 * Tick ISR for preemptive scheduler. We can use a naked attribute as
\r
439 * the context is saved at the start of vPortYieldFromTick(). The tick
\r
440 * count is incremented after the context is saved.
\r
442 void SIG_OUTPUT_COMPARE1A( void ) __attribute__ ( ( signal, naked ) );
\r
443 void SIG_OUTPUT_COMPARE1A( void )
\r
445 vPortYieldFromTick();
\r
446 asm volatile ( "reti" );
\r
451 * Tick ISR for the cooperative scheduler. All this does is increment the
\r
452 * tick count. We don't need to switch context, this can only be done by
\r
453 * manual calls to taskYIELD();
\r
455 void SIG_OUTPUT_COMPARE1A( void ) __attribute__ ( ( signal ) );
\r
456 void SIG_OUTPUT_COMPARE1A( void )
\r
458 xTaskIncrementTick();
\r