2 FreeRTOS V7.5.2 - 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
65 /*-----------------------------------------------------------
\r
66 * Implementation of functions defined in portable.h for the ARM CM0 port.
\r
67 *----------------------------------------------------------*/
\r
70 #include "intrinsics.h"
\r
72 /* Scheduler includes. */
\r
73 #include "FreeRTOS.h"
\r
76 /* Constants required to manipulate the NVIC. */
\r
77 #define portNVIC_SYSTICK_CTRL ( ( volatile unsigned long *) 0xe000e010 )
\r
78 #define portNVIC_SYSTICK_LOAD ( ( volatile unsigned long *) 0xe000e014 )
\r
79 #define portNVIC_SYSPRI2 ( ( volatile unsigned long *) 0xe000ed20 )
\r
80 #define portNVIC_SYSTICK_CLK 0x00000004
\r
81 #define portNVIC_SYSTICK_INT 0x00000002
\r
82 #define portNVIC_SYSTICK_ENABLE 0x00000001
\r
83 #define portMIN_INTERRUPT_PRIORITY ( 255UL )
\r
84 #define portNVIC_PENDSV_PRI ( portMIN_INTERRUPT_PRIORITY << 16UL )
\r
85 #define portNVIC_SYSTICK_PRI ( portMIN_INTERRUPT_PRIORITY << 24UL )
\r
87 /* Constants required to set up the initial stack. */
\r
88 #define portINITIAL_XPSR ( 0x01000000 )
\r
90 /* For backward compatibility, ensure configKERNEL_INTERRUPT_PRIORITY is
\r
91 defined. The value 255 should also ensure backward compatibility.
\r
92 FreeRTOS.org versions prior to V4.3.0 did not include this definition. */
\r
93 #ifndef configKERNEL_INTERRUPT_PRIORITY
\r
94 #define configKERNEL_INTERRUPT_PRIORITY 0
\r
97 /* Each task maintains its own interrupt status in the critical nesting
\r
99 static unsigned portBASE_TYPE uxCriticalNesting = 0xaaaaaaaa;
\r
102 * Setup the timer to generate the tick interrupts.
\r
104 static void prvSetupTimerInterrupt( void );
\r
107 * Exception handlers.
\r
109 void xPortSysTickHandler( void );
\r
112 * Start first task is a separate function so it can be tested in isolation.
\r
114 extern void vPortStartFirstTask( void );
\r
117 * Used to catch tasks that attempt to return from their implementing function.
\r
119 static void prvTaskExitError( void );
\r
121 /*-----------------------------------------------------------*/
\r
124 * See header file for description.
\r
126 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
128 /* Simulate the stack frame as it would be created by a context switch
\r
130 pxTopOfStack--; /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */
\r
131 *pxTopOfStack = portINITIAL_XPSR; /* xPSR */
\r
133 *pxTopOfStack = ( portSTACK_TYPE ) pxCode; /* PC */
\r
135 *pxTopOfStack = ( portSTACK_TYPE ) prvTaskExitError; /* LR */
\r
136 pxTopOfStack -= 5; /* R12, R3, R2 and R1. */
\r
137 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */
\r
138 pxTopOfStack -= 8; /* R11..R4. */
\r
140 return pxTopOfStack;
\r
142 /*-----------------------------------------------------------*/
\r
144 static void prvTaskExitError( void )
\r
146 /* A function that implements a task must not exit or attempt to return to
\r
147 its caller as there is nothing to return to. If a task wants to exit it
\r
148 should instead call vTaskDelete( NULL ).
\r
150 Artificially force an assert() to be triggered if configASSERT() is
\r
151 defined, then stop here so application writers can catch the error. */
\r
152 configASSERT( uxCriticalNesting == ~0UL );
\r
153 portDISABLE_INTERRUPTS();
\r
156 /*-----------------------------------------------------------*/
\r
159 * See header file for description.
\r
161 portBASE_TYPE xPortStartScheduler( void )
\r
163 /* Make PendSV and SysTick the lowest priority interrupts. */
\r
164 *(portNVIC_SYSPRI2) |= portNVIC_PENDSV_PRI;
\r
165 *(portNVIC_SYSPRI2) |= portNVIC_SYSTICK_PRI;
\r
167 /* Start the timer that generates the tick ISR. Interrupts are disabled
\r
169 prvSetupTimerInterrupt();
\r
171 /* Initialise the critical nesting count ready for the first task. */
\r
172 uxCriticalNesting = 0;
\r
174 /* Start the first task. */
\r
175 vPortStartFirstTask();
\r
177 /* Should not get here! */
\r
180 /*-----------------------------------------------------------*/
\r
182 void vPortEndScheduler( void )
\r
184 /* It is unlikely that the CM0 port will require this function as there
\r
185 is nothing to return to. */
\r
187 /*-----------------------------------------------------------*/
\r
189 void vPortYield( void )
\r
191 /* Set a PendSV to request a context switch. */
\r
192 *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;
\r
194 /* Barriers are normally not required but do ensure the code is completely
\r
195 within the specified behaviour for the architecture. */
\r
199 /*-----------------------------------------------------------*/
\r
201 void vPortEnterCritical( void )
\r
203 portDISABLE_INTERRUPTS();
\r
204 uxCriticalNesting++;
\r
208 /*-----------------------------------------------------------*/
\r
210 void vPortExitCritical( void )
\r
212 uxCriticalNesting--;
\r
213 if( uxCriticalNesting == 0 )
\r
215 portENABLE_INTERRUPTS();
\r
218 /*-----------------------------------------------------------*/
\r
220 void xPortSysTickHandler( void )
\r
222 unsigned long ulPreviousMask;
\r
224 ulPreviousMask = portSET_INTERRUPT_MASK_FROM_ISR();
\r
226 /* Increment the RTOS tick. */
\r
227 if( xTaskIncrementTick() != pdFALSE )
\r
229 /* Pend a context switch. */
\r
230 *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;
\r
233 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulPreviousMask );
\r
235 /*-----------------------------------------------------------*/
\r
238 * Setup the systick timer to generate the tick interrupts at the required
\r
241 static void prvSetupTimerInterrupt( void )
\r
243 /* Configure SysTick to interrupt at the requested rate. */
\r
244 *(portNVIC_SYSTICK_LOAD) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
\r
245 *(portNVIC_SYSTICK_CTRL) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;
\r
247 /*-----------------------------------------------------------*/
\r