2 FreeRTOS V9.0.1 - Copyright (C) 2017 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
70 /*-----------------------------------------------------------
\r
71 * Implementation of functions defined in portable.h for the ARM CM0 port.
\r
72 *----------------------------------------------------------*/
\r
74 /* Scheduler includes. */
\r
75 #include "FreeRTOS.h"
\r
78 /* Constants required to manipulate the NVIC. */
\r
79 #define portNVIC_SYSTICK_CTRL ( ( volatile uint32_t * ) 0xe000e010 )
\r
80 #define portNVIC_SYSTICK_LOAD ( ( volatile uint32_t * ) 0xe000e014 )
\r
81 #define portNVIC_SYSTICK_CURRENT_VALUE ( ( volatile uint32_t * ) 0xe000e018 )
\r
82 #define portNVIC_INT_CTRL ( ( volatile uint32_t *) 0xe000ed04 )
\r
83 #define portNVIC_SYSPRI2 ( ( volatile uint32_t *) 0xe000ed20 )
\r
84 #define portNVIC_SYSTICK_CLK 0x00000004
\r
85 #define portNVIC_SYSTICK_INT 0x00000002
\r
86 #define portNVIC_SYSTICK_ENABLE 0x00000001
\r
87 #define portNVIC_PENDSVSET 0x10000000
\r
88 #define portMIN_INTERRUPT_PRIORITY ( 255UL )
\r
89 #define portNVIC_PENDSV_PRI ( portMIN_INTERRUPT_PRIORITY << 16UL )
\r
90 #define portNVIC_SYSTICK_PRI ( portMIN_INTERRUPT_PRIORITY << 24UL )
\r
92 /* Constants required to set up the initial stack. */
\r
93 #define portINITIAL_XPSR ( 0x01000000 )
\r
95 /* Let the user override the pre-loading of the initial LR with the address of
\r
96 prvTaskExitError() in case it messes up unwinding of the stack in the
\r
98 #ifdef configTASK_RETURN_ADDRESS
\r
99 #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS
\r
101 #define portTASK_RETURN_ADDRESS prvTaskExitError
\r
105 * Setup the timer to generate the tick interrupts.
\r
107 static void prvSetupTimerInterrupt( void );
\r
110 * Exception handlers.
\r
112 void xPortPendSVHandler( void ) __attribute__ (( naked ));
\r
113 void xPortSysTickHandler( void );
\r
114 void vPortSVCHandler( void );
\r
117 * Start first task is a separate function so it can be tested in isolation.
\r
119 static void vPortStartFirstTask( void ) __attribute__ (( naked ));
\r
122 * Used to catch tasks that attempt to return from their implementing function.
\r
124 static void prvTaskExitError( void );
\r
126 /*-----------------------------------------------------------*/
\r
128 /* Each task maintains its own interrupt status in the critical nesting
\r
130 static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;
\r
132 /*-----------------------------------------------------------*/
\r
135 * See header file for description.
\r
137 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
139 /* Simulate the stack frame as it would be created by a context switch
\r
141 pxTopOfStack--; /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */
\r
142 *pxTopOfStack = portINITIAL_XPSR; /* xPSR */
\r
144 *pxTopOfStack = ( StackType_t ) pxCode; /* PC */
\r
146 *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS; /* LR */
\r
147 pxTopOfStack -= 5; /* R12, R3, R2 and R1. */
\r
148 *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
\r
149 pxTopOfStack -= 8; /* R11..R4. */
\r
151 return pxTopOfStack;
\r
153 /*-----------------------------------------------------------*/
\r
155 static void prvTaskExitError( void )
\r
157 volatile uint32_t ulDummy = 0UL;
\r
159 /* A function that implements a task must not exit or attempt to return to
\r
160 its caller as there is nothing to return to. If a task wants to exit it
\r
161 should instead call vTaskDelete( NULL ).
\r
163 Artificially force an assert() to be triggered if configASSERT() is
\r
164 defined, then stop here so application writers can catch the error. */
\r
165 configASSERT( uxCriticalNesting == ~0UL );
\r
166 portDISABLE_INTERRUPTS();
\r
167 while( ulDummy == 0 )
\r
169 /* This file calls prvTaskExitError() after the scheduler has been
\r
170 started to remove a compiler warning about the function being defined
\r
171 but never called. ulDummy is used purely to quieten other warnings
\r
172 about code appearing after this function is called - making ulDummy
\r
173 volatile makes the compiler think the function could return and
\r
174 therefore not output an 'unreachable code' warning for code that appears
\r
178 /*-----------------------------------------------------------*/
\r
180 void vPortSVCHandler( void )
\r
182 /* This function is no longer used, but retained for backward
\r
185 /*-----------------------------------------------------------*/
\r
187 void vPortStartFirstTask( void )
\r
189 /* The MSP stack is not reset as, unlike on M3/4 parts, there is no vector
\r
190 table offset register that can be used to locate the initial stack value.
\r
191 Not all M0 parts have the application vector table at address 0. */
\r
193 " .syntax unified \n"
\r
194 " ldr r2, pxCurrentTCBConst2 \n" /* Obtain location of pxCurrentTCB. */
\r
196 " ldr r0, [r3] \n" /* The first item in pxCurrentTCB is the task top of stack. */
\r
197 " adds r0, #32 \n" /* Discard everything up to r0. */
\r
198 " msr psp, r0 \n" /* This is now the new top of stack to use in the task. */
\r
199 " movs r0, #2 \n" /* Switch to the psp stack. */
\r
200 " msr CONTROL, r0 \n"
\r
202 " pop {r0-r5} \n" /* Pop the registers that are saved automatically. */
\r
203 " mov lr, r5 \n" /* lr is now in r5. */
\r
204 " pop {r3} \n" /* Return address is now in r3. */
\r
205 " pop {r2} \n" /* Pop and discard XPSR. */
\r
206 " cpsie i \n" /* The first task has its context and interrupts can be enabled. */
\r
207 " bx r3 \n" /* Finally, jump to the user defined task code. */
\r
210 "pxCurrentTCBConst2: .word pxCurrentTCB "
\r
213 /*-----------------------------------------------------------*/
\r
216 * See header file for description.
\r
218 BaseType_t xPortStartScheduler( void )
\r
220 /* Make PendSV, CallSV and SysTick the same priroity as the kernel. */
\r
221 *(portNVIC_SYSPRI2) |= portNVIC_PENDSV_PRI;
\r
222 *(portNVIC_SYSPRI2) |= portNVIC_SYSTICK_PRI;
\r
224 /* Start the timer that generates the tick ISR. Interrupts are disabled
\r
226 prvSetupTimerInterrupt();
\r
228 /* Initialise the critical nesting count ready for the first task. */
\r
229 uxCriticalNesting = 0;
\r
231 /* Start the first task. */
\r
232 vPortStartFirstTask();
\r
234 /* Should never get here as the tasks will now be executing! Call the task
\r
235 exit error function to prevent compiler warnings about a static function
\r
236 not being called in the case that the application writer overrides this
\r
237 functionality by defining configTASK_RETURN_ADDRESS. Call
\r
238 vTaskSwitchContext() so link time optimisation does not remove the
\r
240 vTaskSwitchContext();
\r
241 prvTaskExitError();
\r
243 /* Should not get here! */
\r
246 /*-----------------------------------------------------------*/
\r
248 void vPortEndScheduler( void )
\r
250 /* Not implemented in ports where there is nothing to return to.
\r
251 Artificially force an assert. */
\r
252 configASSERT( uxCriticalNesting == 1000UL );
\r
254 /*-----------------------------------------------------------*/
\r
256 void vPortYield( void )
\r
258 /* Set a PendSV to request a context switch. */
\r
259 *( portNVIC_INT_CTRL ) = portNVIC_PENDSVSET;
\r
261 /* Barriers are normally not required but do ensure the code is completely
\r
262 within the specified behaviour for the architecture. */
\r
263 __asm volatile( "dsb" ::: "memory" );
\r
264 __asm volatile( "isb" );
\r
266 /*-----------------------------------------------------------*/
\r
268 void vPortEnterCritical( void )
\r
270 portDISABLE_INTERRUPTS();
\r
271 uxCriticalNesting++;
\r
272 __asm volatile( "dsb" ::: "memory" );
\r
273 __asm volatile( "isb" );
\r
275 /*-----------------------------------------------------------*/
\r
277 void vPortExitCritical( void )
\r
279 configASSERT( uxCriticalNesting );
\r
280 uxCriticalNesting--;
\r
281 if( uxCriticalNesting == 0 )
\r
283 portENABLE_INTERRUPTS();
\r
286 /*-----------------------------------------------------------*/
\r
288 uint32_t ulSetInterruptMaskFromISR( void )
\r
291 " mrs r0, PRIMASK \n"
\r
297 #if !defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
\r
298 /* To avoid compiler warnings. The return statement will nevere be reached,
\r
299 but some compilers warn if it is not included, while others won't compile if
\r
304 /*-----------------------------------------------------------*/
\r
306 void vClearInterruptMaskFromISR( __attribute__( ( unused ) ) uint32_t ulMask )
\r
309 " msr PRIMASK, r0 \n"
\r
314 #if !defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
\r
315 /* Just to avoid compiler warning. ulMask is used from the asm code but
\r
316 the compiler can't see that. Some compilers generate warnings without the
\r
317 following line, while others generate warnings if the line is included. */
\r
321 /*-----------------------------------------------------------*/
\r
323 void xPortPendSVHandler( void )
\r
325 /* This is a naked function. */
\r
329 " .syntax unified \n"
\r
332 " ldr r3, pxCurrentTCBConst \n" /* Get the location of the current TCB. */
\r
335 " subs r0, r0, #32 \n" /* Make space for the remaining low registers. */
\r
336 " str r0, [r2] \n" /* Save the new top of stack. */
\r
337 " stmia r0!, {r4-r7} \n" /* Store the low registers that are not saved automatically. */
\r
338 " mov r4, r8 \n" /* Store the high registers. */
\r
342 " stmia r0!, {r4-r7} \n"
\r
344 " push {r3, r14} \n"
\r
346 " bl vTaskSwitchContext \n"
\r
348 " pop {r2, r3} \n" /* lr goes in r3. r2 now holds tcb pointer. */
\r
351 " ldr r0, [r1] \n" /* The first item in pxCurrentTCB is the task top of stack. */
\r
352 " adds r0, r0, #16 \n" /* Move to the high registers. */
\r
353 " ldmia r0!, {r4-r7} \n" /* Pop the high registers. */
\r
359 " msr psp, r0 \n" /* Remember the new top of stack for the task. */
\r
361 " subs r0, r0, #32 \n" /* Go back for the low registers that are not automatically restored. */
\r
362 " ldmia r0!, {r4-r7} \n" /* Pop low registers. */
\r
367 "pxCurrentTCBConst: .word pxCurrentTCB "
\r
370 /*-----------------------------------------------------------*/
\r
372 void xPortSysTickHandler( void )
\r
374 uint32_t ulPreviousMask;
\r
376 ulPreviousMask = portSET_INTERRUPT_MASK_FROM_ISR();
\r
378 /* Increment the RTOS tick. */
\r
379 if( xTaskIncrementTick() != pdFALSE )
\r
381 /* Pend a context switch. */
\r
382 *(portNVIC_INT_CTRL) = portNVIC_PENDSVSET;
\r
385 portCLEAR_INTERRUPT_MASK_FROM_ISR( ulPreviousMask );
\r
387 /*-----------------------------------------------------------*/
\r
390 * Setup the systick timer to generate the tick interrupts at the required
\r
393 void prvSetupTimerInterrupt( void )
\r
395 /* Stop and reset the SysTick. */
\r
396 *(portNVIC_SYSTICK_CTRL) = 0UL;
\r
397 *(portNVIC_SYSTICK_CURRENT_VALUE) = 0UL;
\r
399 /* Configure SysTick to interrupt at the requested rate. */
\r
400 *(portNVIC_SYSTICK_LOAD) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
\r
401 *(portNVIC_SYSTICK_CTRL) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;
\r
403 /*-----------------------------------------------------------*/
\r