]> begriffs open source - freertos/blob - portable/Tasking/ARM_CM4F/port.c
Add SMP in the License Header (#402)
[freertos] / portable / Tasking / ARM_CM4F / port.c
1 /*
2  * FreeRTOS SMP Kernel V202110.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * https://www.FreeRTOS.org
23  * https://github.com/FreeRTOS
24  *
25  */
26
27 /*-----------------------------------------------------------
28 * Implementation of functions defined in portable.h for the ARM CM4F port.
29 *----------------------------------------------------------*/
30
31 /* Scheduler includes. */
32 #include "FreeRTOS.h"
33 #include "task.h"
34
35 /* Constants required to manipulate the NVIC. */
36 #define portNVIC_SYSTICK_CTRL       ( ( volatile uint32_t * ) 0xe000e010 )
37 #define portNVIC_SYSTICK_LOAD       ( ( volatile uint32_t * ) 0xe000e014 )
38 #define portNVIC_SHPR3_REG          ( ( volatile uint32_t * ) 0xe000ed20 )
39 #define portNVIC_SYSTICK_CLK        0x00000004
40 #define portNVIC_SYSTICK_INT        0x00000002
41 #define portNVIC_SYSTICK_ENABLE     0x00000001
42 #define portNVIC_PENDSV_PRI         ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 16 )
43 #define portNVIC_SYSTICK_PRI        ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 24 )
44
45 /* Masks off all bits but the VECTACTIVE bits in the ICSR register. */
46 #define portVECTACTIVE_MASK         ( 0xFFUL )
47
48 /* Constants required to manipulate the VFP. */
49 #define portFPCCR                   ( ( volatile uint32_t * ) 0xe000ef34 ) /* Floating point context control register. */
50 #define portASPEN_AND_LSPEN_BITS    ( 0x3UL << 30UL )
51
52 /* Constants required to set up the initial stack. */
53 #define portINITIAL_XPSR            ( 0x01000000 )
54 #define portINITIAL_EXC_RETURN      ( 0xfffffffd )
55
56 /* Let the user override the pre-loading of the initial LR with the address of
57  * prvTaskExitError() in case it messes up unwinding of the stack in the
58  * debugger. */
59 #ifdef configTASK_RETURN_ADDRESS
60     #define portTASK_RETURN_ADDRESS    configTASK_RETURN_ADDRESS
61 #else
62     #define portTASK_RETURN_ADDRESS    prvTaskExitError
63 #endif
64
65 /* For strict compliance with the Cortex-M spec the task start address should
66  * have bit-0 clear, as it is loaded into the PC on exit from an ISR. */
67 #define portSTART_ADDRESS_MASK    ( ( StackType_t ) 0xfffffffeUL )
68
69 /* The priority used by the kernel is assigned to a variable to make access
70  * from inline assembler easier. */
71 const uint32_t ulKernelPriority = configKERNEL_INTERRUPT_PRIORITY;
72
73 /* Each task maintains its own interrupt status in the critical nesting
74  * variable. */
75 static uint32_t ulCriticalNesting = 0xaaaaaaaaUL;
76
77 /*
78  * Setup the timer to generate the tick interrupts.
79  */
80 static void prvSetupTimerInterrupt( void );
81
82 /*
83  * Exception handlers.
84  */
85 void SysTick_Handler( void );
86
87 /*
88  * Functions defined in port_asm.asm.
89  */
90 extern void vPortEnableVFP( void );
91 extern void vPortStartFirstTask( void );
92
93 /*
94  * Used to catch tasks that attempt to return from their implementing function.
95  */
96 static void prvTaskExitError( void );
97
98 /* This exists purely to allow the const to be used from within the
99  * port_asm.asm assembly file. */
100 const uint32_t ulMaxSyscallInterruptPriorityConst = configMAX_SYSCALL_INTERRUPT_PRIORITY;
101
102 /*-----------------------------------------------------------*/
103
104 /*
105  * See header file for description.
106  */
107 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
108                                      TaskFunction_t pxCode,
109                                      void * pvParameters )
110 {
111     /* Simulate the stack frame as it would be created by a context switch
112      * interrupt. */
113
114     /* Offset added to account for the way the MCU uses the stack on entry/exit
115      * of interrupts, and to ensure alignment. */
116     pxTopOfStack--;
117
118     *pxTopOfStack = portINITIAL_XPSR;                                    /* xPSR */
119     pxTopOfStack--;
120     *pxTopOfStack = ( ( StackType_t ) pxCode ) & portSTART_ADDRESS_MASK; /* PC */
121     pxTopOfStack--;
122     *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS;             /* LR */
123
124     /* Save code space by skipping register initialisation. */
125     pxTopOfStack -= 5;                            /* R12, R3, R2 and R1. */
126     *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
127
128     /* A save method is being used that requires each task to maintain its
129      * own exec return value. */
130     pxTopOfStack--;
131     *pxTopOfStack = portINITIAL_EXC_RETURN;
132
133     pxTopOfStack -= 8; /* R11, R10, R9, R8, R7, R6, R5 and R4. */
134
135     return pxTopOfStack;
136 }
137 /*-----------------------------------------------------------*/
138
139 static void prvTaskExitError( void )
140 {
141     /* A function that implements a task must not exit or attempt to return to
142      * its caller as there is nothing to return to.  If a task wants to exit it
143      * should instead call vTaskDelete( NULL ).
144      *
145      * Artificially force an assert() to be triggered if configASSERT() is
146      * defined, then stop here so application writers can catch the error. */
147     configASSERT( ulCriticalNesting == ~0UL );
148     portDISABLE_INTERRUPTS();
149
150     for( ; ; )
151     {
152     }
153 }
154 /*-----------------------------------------------------------*/
155
156 /*
157  * See header file for description.
158  */
159 BaseType_t xPortStartScheduler( void )
160 {
161     /* configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0.
162      * See https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
163     configASSERT( ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) );
164
165     /* Make PendSV and SysTick the lowest priority interrupts. */
166     *( portNVIC_SHPR3_REG ) |= portNVIC_PENDSV_PRI;
167     *( portNVIC_SHPR3_REG ) |= portNVIC_SYSTICK_PRI;
168
169     /* Start the timer that generates the tick ISR.  Interrupts are disabled
170      * here already. */
171     prvSetupTimerInterrupt();
172
173     /* Initialise the critical nesting count ready for the first task. */
174     ulCriticalNesting = 0;
175
176     /* Ensure the VFP is enabled - it should be anyway. */
177     vPortEnableVFP();
178
179     /* Lazy save always. */
180     *( portFPCCR ) |= portASPEN_AND_LSPEN_BITS;
181
182     /* Start the first task. */
183     vPortStartFirstTask();
184
185     /* Should not get here! */
186     return 0;
187 }
188 /*-----------------------------------------------------------*/
189
190 void vPortEndScheduler( void )
191 {
192     /* Not implemented in ports where there is nothing to return to.
193      * Artificially force an assert. */
194     configASSERT( ulCriticalNesting == 1000UL );
195 }
196 /*-----------------------------------------------------------*/
197
198 void vPortYield( void )
199 {
200     /* Set a PendSV to request a context switch. */
201     *( portNVIC_INT_CTRL ) = portNVIC_PENDSVSET;
202
203     /* Barriers are normally not required but do ensure the code is completely
204      * within the specified behaviour for the architecture. */
205     __DSB();
206     __ISB();
207 }
208 /*-----------------------------------------------------------*/
209
210 void vPortEnterCritical( void )
211 {
212     portDISABLE_INTERRUPTS();
213     ulCriticalNesting++;
214     __DSB();
215     __ISB();
216
217     /* This is not the interrupt safe version of the enter critical function so
218      * assert() if it is being called from an interrupt context.  Only API
219      * functions that end in "FromISR" can be used in an interrupt.  Only assert if
220      * the critical nesting count is 1 to protect against recursive calls if the
221      * assert function also uses a critical section. */
222     if( ulCriticalNesting == 1 )
223     {
224         configASSERT( ( ( *( portNVIC_INT_CTRL ) ) & portVECTACTIVE_MASK ) == 0 );
225     }
226 }
227 /*-----------------------------------------------------------*/
228
229 void vPortExitCritical( void )
230 {
231     configASSERT( ulCriticalNesting );
232     ulCriticalNesting--;
233
234     if( ulCriticalNesting == 0 )
235     {
236         portENABLE_INTERRUPTS();
237     }
238 }
239 /*-----------------------------------------------------------*/
240
241 void SysTick_Handler( void )
242 {
243     uint32_t ulDummy;
244
245     ulDummy = portSET_INTERRUPT_MASK_FROM_ISR();
246     {
247         if( xTaskIncrementTick() != pdFALSE )
248         {
249             /* Pend a context switch. */
250             *( portNVIC_INT_CTRL ) = portNVIC_PENDSVSET;
251         }
252     }
253     portCLEAR_INTERRUPT_MASK_FROM_ISR( ulDummy );
254 }
255 /*-----------------------------------------------------------*/
256
257 /*
258  * Setup the systick timer to generate the tick interrupts at the required
259  * frequency.
260  */
261 void prvSetupTimerInterrupt( void )
262 {
263     /* Configure SysTick to interrupt at the requested rate. */
264     *( portNVIC_SYSTICK_LOAD ) = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
265     *( portNVIC_SYSTICK_CTRL ) = portNVIC_SYSTICK_CLK | portNVIC_SYSTICK_INT | portNVIC_SYSTICK_ENABLE;
266 }
267 /*-----------------------------------------------------------*/