2 FreeRTOS V8.2.0 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
74 + AVR port - Replaced the inb() and outb() functions with direct memory
\r
75 access. This allows the port to be built with the 20050414 build of
\r
80 #include <avr/interrupt.h>
\r
82 #include "FreeRTOS.h"
\r
85 /*-----------------------------------------------------------
\r
86 * Implementation of functions defined in portable.h for the AVR port.
\r
87 *----------------------------------------------------------*/
\r
89 /* Start tasks with interrupts enables. */
\r
90 #define portFLAGS_INT_ENABLED ( ( StackType_t ) 0x80 )
\r
92 /* Hardware constants for timer 1. */
\r
93 #define portCLEAR_COUNTER_ON_MATCH ( ( uint8_t ) 0x08 )
\r
94 #define portPRESCALE_64 ( ( uint8_t ) 0x03 )
\r
95 #define portCLOCK_PRESCALER ( ( uint32_t ) 64 )
\r
96 #define portCOMPARE_MATCH_A_INTERRUPT_ENABLE ( ( uint8_t ) 0x10 )
\r
98 /*-----------------------------------------------------------*/
\r
100 /* We require the address of the pxCurrentTCB variable, but don't want to know
\r
101 any details of its type. */
\r
102 typedef void TCB_t;
\r
103 extern volatile TCB_t * volatile pxCurrentTCB;
\r
105 /*-----------------------------------------------------------*/
\r
108 * Macro to save all the general purpose registers, the save the stack pointer
\r
111 * The first thing we do is save the flags then disable interrupts. This is to
\r
112 * guard our stack against having a context switch interrupt after we have already
\r
113 * pushed the registers onto the stack - causing the 32 registers to be on the
\r
116 * r1 is set to zero as the compiler expects it to be thus, however some
\r
117 * of the math routines make use of R1.
\r
119 * The interrupts will have been disabled during the call to portSAVE_CONTEXT()
\r
120 * so we need not worry about reading/writing to the stack pointer.
\r
123 #define portSAVE_CONTEXT() \
\r
124 asm volatile ( "push r0 \n\t" \
\r
125 "in r0, __SREG__ \n\t" \
\r
160 "lds r26, pxCurrentTCB \n\t" \
\r
161 "lds r27, pxCurrentTCB + 1 \n\t" \
\r
162 "in r0, 0x3d \n\t" \
\r
164 "in r0, 0x3e \n\t" \
\r
169 * Opposite to portSAVE_CONTEXT(). Interrupts will have been disabled during
\r
170 * the context save so we can write to the stack pointer.
\r
173 #define portRESTORE_CONTEXT() \
\r
174 asm volatile ( "lds r26, pxCurrentTCB \n\t" \
\r
175 "lds r27, pxCurrentTCB + 1 \n\t" \
\r
176 "ld r28, x+ \n\t" \
\r
177 "out __SP_L__, r28 \n\t" \
\r
178 "ld r29, x+ \n\t" \
\r
179 "out __SP_H__, r29 \n\t" \
\r
212 "out __SREG__, r0 \n\t" \
\r
216 /*-----------------------------------------------------------*/
\r
219 * Perform hardware setup to enable ticks from timer 1, compare match A.
\r
221 static void prvSetupTimerInterrupt( void );
\r
222 /*-----------------------------------------------------------*/
\r
225 * See header file for description.
\r
227 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
229 uint16_t usAddress;
\r
231 /* Place a few bytes of known values on the bottom of the stack.
\r
232 This is just useful for debugging. */
\r
234 *pxTopOfStack = 0x11;
\r
236 *pxTopOfStack = 0x22;
\r
238 *pxTopOfStack = 0x33;
\r
241 /* Simulate how the stack would look after a call to vPortYield() generated by
\r
244 /*lint -e950 -e611 -e923 Lint doesn't like this much - but nothing I can do about it. */
\r
246 /* The start of the task code will be popped off the stack last, so place
\r
248 usAddress = ( uint16_t ) pxCode;
\r
249 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
\r
253 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
\r
256 /* Next simulate the stack as if after a call to portSAVE_CONTEXT().
\r
257 portSAVE_CONTEXT places the flags on the stack immediately after r0
\r
258 to ensure the interrupts get disabled as soon as possible, and so ensuring
\r
259 the stack use is minimal should a context switch interrupt occur. */
\r
260 *pxTopOfStack = ( StackType_t ) 0x00; /* R0 */
\r
262 *pxTopOfStack = portFLAGS_INT_ENABLED;
\r
266 /* Now the remaining registers. The compiler expects R1 to be 0. */
\r
267 *pxTopOfStack = ( StackType_t ) 0x00; /* R1 */
\r
269 *pxTopOfStack = ( StackType_t ) 0x02; /* R2 */
\r
271 *pxTopOfStack = ( StackType_t ) 0x03; /* R3 */
\r
273 *pxTopOfStack = ( StackType_t ) 0x04; /* R4 */
\r
275 *pxTopOfStack = ( StackType_t ) 0x05; /* R5 */
\r
277 *pxTopOfStack = ( StackType_t ) 0x06; /* R6 */
\r
279 *pxTopOfStack = ( StackType_t ) 0x07; /* R7 */
\r
281 *pxTopOfStack = ( StackType_t ) 0x08; /* R8 */
\r
283 *pxTopOfStack = ( StackType_t ) 0x09; /* R9 */
\r
285 *pxTopOfStack = ( StackType_t ) 0x10; /* R10 */
\r
287 *pxTopOfStack = ( StackType_t ) 0x11; /* R11 */
\r
289 *pxTopOfStack = ( StackType_t ) 0x12; /* R12 */
\r
291 *pxTopOfStack = ( StackType_t ) 0x13; /* R13 */
\r
293 *pxTopOfStack = ( StackType_t ) 0x14; /* R14 */
\r
295 *pxTopOfStack = ( StackType_t ) 0x15; /* R15 */
\r
297 *pxTopOfStack = ( StackType_t ) 0x16; /* R16 */
\r
299 *pxTopOfStack = ( StackType_t ) 0x17; /* R17 */
\r
301 *pxTopOfStack = ( StackType_t ) 0x18; /* R18 */
\r
303 *pxTopOfStack = ( StackType_t ) 0x19; /* R19 */
\r
305 *pxTopOfStack = ( StackType_t ) 0x20; /* R20 */
\r
307 *pxTopOfStack = ( StackType_t ) 0x21; /* R21 */
\r
309 *pxTopOfStack = ( StackType_t ) 0x22; /* R22 */
\r
311 *pxTopOfStack = ( StackType_t ) 0x23; /* R23 */
\r
314 /* Place the parameter on the stack in the expected location. */
\r
315 usAddress = ( uint16_t ) pvParameters;
\r
316 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
\r
320 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
\r
323 *pxTopOfStack = ( StackType_t ) 0x26; /* R26 X */
\r
325 *pxTopOfStack = ( StackType_t ) 0x27; /* R27 */
\r
327 *pxTopOfStack = ( StackType_t ) 0x28; /* R28 Y */
\r
329 *pxTopOfStack = ( StackType_t ) 0x29; /* R29 */
\r
331 *pxTopOfStack = ( StackType_t ) 0x30; /* R30 Z */
\r
333 *pxTopOfStack = ( StackType_t ) 0x031; /* R31 */
\r
336 /*lint +e950 +e611 +e923 */
\r
338 return pxTopOfStack;
\r
340 /*-----------------------------------------------------------*/
\r
342 BaseType_t xPortStartScheduler( void )
\r
344 /* Setup the hardware to generate the tick. */
\r
345 prvSetupTimerInterrupt();
\r
347 /* Restore the context of the first task that is going to run. */
\r
348 portRESTORE_CONTEXT();
\r
350 /* Simulate a function call end as generated by the compiler. We will now
\r
351 jump to the start of the task the context of which we have just restored. */
\r
352 asm volatile ( "ret" );
\r
354 /* Should not get here. */
\r
357 /*-----------------------------------------------------------*/
\r
359 void vPortEndScheduler( void )
\r
361 /* It is unlikely that the AVR port will get stopped. If required simply
\r
362 disable the tick interrupt here. */
\r
364 /*-----------------------------------------------------------*/
\r
367 * Manual context switch. The first thing we do is save the registers so we
\r
368 * can use a naked attribute.
\r
370 void vPortYield( void ) __attribute__ ( ( naked ) );
\r
371 void vPortYield( void )
\r
373 portSAVE_CONTEXT();
\r
374 vTaskSwitchContext();
\r
375 portRESTORE_CONTEXT();
\r
377 asm volatile ( "ret" );
\r
379 /*-----------------------------------------------------------*/
\r
382 * Context switch function used by the tick. This must be identical to
\r
383 * vPortYield() from the call to vTaskSwitchContext() onwards. The only
\r
384 * difference from vPortYield() is the tick count is incremented as the
\r
385 * call comes from the tick ISR.
\r
387 void vPortYieldFromTick( void ) __attribute__ ( ( naked ) );
\r
388 void vPortYieldFromTick( void )
\r
390 portSAVE_CONTEXT();
\r
391 if( xTaskIncrementTick() != pdFALSE )
\r
393 vTaskSwitchContext();
\r
395 portRESTORE_CONTEXT();
\r
397 asm volatile ( "ret" );
\r
399 /*-----------------------------------------------------------*/
\r
402 * Setup timer 1 compare match A to generate a tick interrupt.
\r
404 static void prvSetupTimerInterrupt( void )
\r
406 uint32_t ulCompareMatch;
\r
407 uint8_t ucHighByte, ucLowByte;
\r
409 /* Using 16bit timer 1 to generate the tick. Correct fuses must be
\r
410 selected for the configCPU_CLOCK_HZ clock. */
\r
412 ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
\r
414 /* We only have 16 bits so have to scale to get our required tick rate. */
\r
415 ulCompareMatch /= portCLOCK_PRESCALER;
\r
417 /* Adjust for correct value. */
\r
418 ulCompareMatch -= ( uint32_t ) 1;
\r
420 /* Setup compare match value for compare match A. Interrupts are disabled
\r
421 before this is called so we need not worry here. */
\r
422 ucLowByte = ( uint8_t ) ( ulCompareMatch & ( uint32_t ) 0xff );
\r
423 ulCompareMatch >>= 8;
\r
424 ucHighByte = ( uint8_t ) ( ulCompareMatch & ( uint32_t ) 0xff );
\r
425 OCR1AH = ucHighByte;
\r
426 OCR1AL = ucLowByte;
\r
428 /* Setup clock source and compare match behaviour. */
\r
429 ucLowByte = portCLEAR_COUNTER_ON_MATCH | portPRESCALE_64;
\r
430 TCCR1B = ucLowByte;
\r
432 /* Enable the interrupt - this is okay as interrupt are currently globally
\r
435 ucLowByte |= portCOMPARE_MATCH_A_INTERRUPT_ENABLE;
\r
438 /*-----------------------------------------------------------*/
\r
440 #if configUSE_PREEMPTION == 1
\r
443 * Tick ISR for preemptive scheduler. We can use a naked attribute as
\r
444 * the context is saved at the start of vPortYieldFromTick(). The tick
\r
445 * count is incremented after the context is saved.
\r
447 void SIG_OUTPUT_COMPARE1A( void ) __attribute__ ( ( signal, naked ) );
\r
448 void SIG_OUTPUT_COMPARE1A( void )
\r
450 vPortYieldFromTick();
\r
451 asm volatile ( "reti" );
\r
456 * Tick ISR for the cooperative scheduler. All this does is increment the
\r
457 * tick count. We don't need to switch context, this can only be done by
\r
458 * manual calls to taskYIELD();
\r
460 void SIG_OUTPUT_COMPARE1A( void ) __attribute__ ( ( signal ) );
\r
461 void SIG_OUTPUT_COMPARE1A( void )
\r
463 xTaskIncrementTick();
\r