2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
100 + AVR port - Replaced the inb() and outb() functions with direct memory
\r
101 access. This allows the port to be built with the 20050414 build of
\r
105 #include <stdlib.h>
\r
106 #include <avr/interrupt.h>
\r
108 #include "FreeRTOS.h"
\r
111 /*-----------------------------------------------------------
\r
112 * Implementation of functions defined in portable.h for the AVR port.
\r
113 *----------------------------------------------------------*/
\r
115 /* Start tasks with interrupts enables. */
\r
116 #define portFLAGS_INT_ENABLED ( ( StackType_t ) 0x80 )
\r
118 /* Hardware constants for timer 1. */
\r
119 #define portCLEAR_COUNTER_ON_MATCH ( ( uint8_t ) 0x08 )
\r
120 #define portPRESCALE_64 ( ( uint8_t ) 0x03 )
\r
121 #define portCLOCK_PRESCALER ( ( uint32_t ) 64 )
\r
122 #define portCOMPARE_MATCH_A_INTERRUPT_ENABLE ( ( uint8_t ) 0x10 )
\r
124 /*-----------------------------------------------------------*/
\r
126 /* We require the address of the pxCurrentTCB variable, but don't want to know
\r
127 any details of its type. */
\r
128 typedef void TCB_t;
\r
129 extern volatile TCB_t * volatile pxCurrentTCB;
\r
131 /*-----------------------------------------------------------*/
\r
134 * Macro to save all the general purpose registers, the save the stack pointer
\r
137 * The first thing we do is save the flags then disable interrupts. This is to
\r
138 * guard our stack against having a context switch interrupt after we have already
\r
139 * pushed the registers onto the stack - causing the 32 registers to be on the
\r
142 * r1 is set to zero as the compiler expects it to be thus, however some
\r
143 * of the math routines make use of R1.
\r
145 * The interrupts will have been disabled during the call to portSAVE_CONTEXT()
\r
146 * so we need not worry about reading/writing to the stack pointer.
\r
149 #define portSAVE_CONTEXT() \
\r
150 asm volatile ( "push r0 \n\t" \
\r
151 "in r0, __SREG__ \n\t" \
\r
186 "lds r26, pxCurrentTCB \n\t" \
\r
187 "lds r27, pxCurrentTCB + 1 \n\t" \
\r
188 "in r0, 0x3d \n\t" \
\r
190 "in r0, 0x3e \n\t" \
\r
195 * Opposite to portSAVE_CONTEXT(). Interrupts will have been disabled during
\r
196 * the context save so we can write to the stack pointer.
\r
199 #define portRESTORE_CONTEXT() \
\r
200 asm volatile ( "lds r26, pxCurrentTCB \n\t" \
\r
201 "lds r27, pxCurrentTCB + 1 \n\t" \
\r
202 "ld r28, x+ \n\t" \
\r
203 "out __SP_L__, r28 \n\t" \
\r
204 "ld r29, x+ \n\t" \
\r
205 "out __SP_H__, r29 \n\t" \
\r
238 "out __SREG__, r0 \n\t" \
\r
242 /*-----------------------------------------------------------*/
\r
245 * Perform hardware setup to enable ticks from timer 1, compare match A.
\r
247 static void prvSetupTimerInterrupt( void );
\r
248 /*-----------------------------------------------------------*/
\r
251 * See header file for description.
\r
253 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
255 uint16_t usAddress;
\r
257 /* Place a few bytes of known values on the bottom of the stack.
\r
258 This is just useful for debugging. */
\r
260 *pxTopOfStack = 0x11;
\r
262 *pxTopOfStack = 0x22;
\r
264 *pxTopOfStack = 0x33;
\r
267 /* Simulate how the stack would look after a call to vPortYield() generated by
\r
270 /*lint -e950 -e611 -e923 Lint doesn't like this much - but nothing I can do about it. */
\r
272 /* The start of the task code will be popped off the stack last, so place
\r
274 usAddress = ( uint16_t ) pxCode;
\r
275 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
\r
279 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
\r
282 /* Next simulate the stack as if after a call to portSAVE_CONTEXT().
\r
283 portSAVE_CONTEXT places the flags on the stack immediately after r0
\r
284 to ensure the interrupts get disabled as soon as possible, and so ensuring
\r
285 the stack use is minimal should a context switch interrupt occur. */
\r
286 *pxTopOfStack = ( StackType_t ) 0x00; /* R0 */
\r
288 *pxTopOfStack = portFLAGS_INT_ENABLED;
\r
292 /* Now the remaining registers. The compiler expects R1 to be 0. */
\r
293 *pxTopOfStack = ( StackType_t ) 0x00; /* R1 */
\r
295 *pxTopOfStack = ( StackType_t ) 0x02; /* R2 */
\r
297 *pxTopOfStack = ( StackType_t ) 0x03; /* R3 */
\r
299 *pxTopOfStack = ( StackType_t ) 0x04; /* R4 */
\r
301 *pxTopOfStack = ( StackType_t ) 0x05; /* R5 */
\r
303 *pxTopOfStack = ( StackType_t ) 0x06; /* R6 */
\r
305 *pxTopOfStack = ( StackType_t ) 0x07; /* R7 */
\r
307 *pxTopOfStack = ( StackType_t ) 0x08; /* R8 */
\r
309 *pxTopOfStack = ( StackType_t ) 0x09; /* R9 */
\r
311 *pxTopOfStack = ( StackType_t ) 0x10; /* R10 */
\r
313 *pxTopOfStack = ( StackType_t ) 0x11; /* R11 */
\r
315 *pxTopOfStack = ( StackType_t ) 0x12; /* R12 */
\r
317 *pxTopOfStack = ( StackType_t ) 0x13; /* R13 */
\r
319 *pxTopOfStack = ( StackType_t ) 0x14; /* R14 */
\r
321 *pxTopOfStack = ( StackType_t ) 0x15; /* R15 */
\r
323 *pxTopOfStack = ( StackType_t ) 0x16; /* R16 */
\r
325 *pxTopOfStack = ( StackType_t ) 0x17; /* R17 */
\r
327 *pxTopOfStack = ( StackType_t ) 0x18; /* R18 */
\r
329 *pxTopOfStack = ( StackType_t ) 0x19; /* R19 */
\r
331 *pxTopOfStack = ( StackType_t ) 0x20; /* R20 */
\r
333 *pxTopOfStack = ( StackType_t ) 0x21; /* R21 */
\r
335 *pxTopOfStack = ( StackType_t ) 0x22; /* R22 */
\r
337 *pxTopOfStack = ( StackType_t ) 0x23; /* R23 */
\r
340 /* Place the parameter on the stack in the expected location. */
\r
341 usAddress = ( uint16_t ) pvParameters;
\r
342 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
\r
346 *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
\r
349 *pxTopOfStack = ( StackType_t ) 0x26; /* R26 X */
\r
351 *pxTopOfStack = ( StackType_t ) 0x27; /* R27 */
\r
353 *pxTopOfStack = ( StackType_t ) 0x28; /* R28 Y */
\r
355 *pxTopOfStack = ( StackType_t ) 0x29; /* R29 */
\r
357 *pxTopOfStack = ( StackType_t ) 0x30; /* R30 Z */
\r
359 *pxTopOfStack = ( StackType_t ) 0x031; /* R31 */
\r
362 /*lint +e950 +e611 +e923 */
\r
364 return pxTopOfStack;
\r
366 /*-----------------------------------------------------------*/
\r
368 BaseType_t xPortStartScheduler( void )
\r
370 /* Setup the hardware to generate the tick. */
\r
371 prvSetupTimerInterrupt();
\r
373 /* Restore the context of the first task that is going to run. */
\r
374 portRESTORE_CONTEXT();
\r
376 /* Simulate a function call end as generated by the compiler. We will now
\r
377 jump to the start of the task the context of which we have just restored. */
\r
378 asm volatile ( "ret" );
\r
380 /* Should not get here. */
\r
383 /*-----------------------------------------------------------*/
\r
385 void vPortEndScheduler( void )
\r
387 /* It is unlikely that the AVR port will get stopped. If required simply
\r
388 disable the tick interrupt here. */
\r
390 /*-----------------------------------------------------------*/
\r
393 * Manual context switch. The first thing we do is save the registers so we
\r
394 * can use a naked attribute.
\r
396 void vPortYield( void ) __attribute__ ( ( naked ) );
\r
397 void vPortYield( void )
\r
399 portSAVE_CONTEXT();
\r
400 vTaskSwitchContext();
\r
401 portRESTORE_CONTEXT();
\r
403 asm volatile ( "ret" );
\r
405 /*-----------------------------------------------------------*/
\r
408 * Context switch function used by the tick. This must be identical to
\r
409 * vPortYield() from the call to vTaskSwitchContext() onwards. The only
\r
410 * difference from vPortYield() is the tick count is incremented as the
\r
411 * call comes from the tick ISR.
\r
413 void vPortYieldFromTick( void ) __attribute__ ( ( naked ) );
\r
414 void vPortYieldFromTick( void )
\r
416 portSAVE_CONTEXT();
\r
417 if( xTaskIncrementTick() != pdFALSE )
\r
419 vTaskSwitchContext();
\r
421 portRESTORE_CONTEXT();
\r
423 asm volatile ( "ret" );
\r
425 /*-----------------------------------------------------------*/
\r
428 * Setup timer 1 compare match A to generate a tick interrupt.
\r
430 static void prvSetupTimerInterrupt( void )
\r
432 uint32_t ulCompareMatch;
\r
433 uint8_t ucHighByte, ucLowByte;
\r
435 /* Using 16bit timer 1 to generate the tick. Correct fuses must be
\r
436 selected for the configCPU_CLOCK_HZ clock. */
\r
438 ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;
\r
440 /* We only have 16 bits so have to scale to get our required tick rate. */
\r
441 ulCompareMatch /= portCLOCK_PRESCALER;
\r
443 /* Adjust for correct value. */
\r
444 ulCompareMatch -= ( uint32_t ) 1;
\r
446 /* Setup compare match value for compare match A. Interrupts are disabled
\r
447 before this is called so we need not worry here. */
\r
448 ucLowByte = ( uint8_t ) ( ulCompareMatch & ( uint32_t ) 0xff );
\r
449 ulCompareMatch >>= 8;
\r
450 ucHighByte = ( uint8_t ) ( ulCompareMatch & ( uint32_t ) 0xff );
\r
451 OCR1AH = ucHighByte;
\r
452 OCR1AL = ucLowByte;
\r
454 /* Setup clock source and compare match behaviour. */
\r
455 ucLowByte = portCLEAR_COUNTER_ON_MATCH | portPRESCALE_64;
\r
456 TCCR1B = ucLowByte;
\r
458 /* Enable the interrupt - this is okay as interrupt are currently globally
\r
461 ucLowByte |= portCOMPARE_MATCH_A_INTERRUPT_ENABLE;
\r
464 /*-----------------------------------------------------------*/
\r
466 #if configUSE_PREEMPTION == 1
\r
469 * Tick ISR for preemptive scheduler. We can use a naked attribute as
\r
470 * the context is saved at the start of vPortYieldFromTick(). The tick
\r
471 * count is incremented after the context is saved.
\r
473 void SIG_OUTPUT_COMPARE1A( void ) __attribute__ ( ( signal, naked ) );
\r
474 void SIG_OUTPUT_COMPARE1A( void )
\r
476 vPortYieldFromTick();
\r
477 asm volatile ( "reti" );
\r
482 * Tick ISR for the cooperative scheduler. All this does is increment the
\r
483 * tick count. We don't need to switch context, this can only be done by
\r
484 * manual calls to taskYIELD();
\r
486 void SIG_OUTPUT_COMPARE1A( void ) __attribute__ ( ( signal ) );
\r
487 void SIG_OUTPUT_COMPARE1A( void )
\r
489 xTaskIncrementTick();
\r