]> begriffs open source - cmsis-freertos/blob - Source/portable/IAR/AtmelSAM7S64/port.c
Sources updated to FreeRTOS 10.2.0
[cmsis-freertos] / Source / portable / IAR / AtmelSAM7S64 / port.c
1 /*
2  * FreeRTOS Kernel V10.2.0
3  * Copyright (C) 2019 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  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /*-----------------------------------------------------------
29  * Implementation of functions defined in portable.h for the Atmel ARM7 port.
30  *----------------------------------------------------------*/
31
32
33 /* Standard includes. */
34 #include <stdlib.h>
35
36 /* Scheduler includes. */
37 #include "FreeRTOS.h"
38 #include "task.h"
39
40 /* Constants required to setup the initial stack. */
41 #define portINITIAL_SPSR                                ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
42 #define portTHUMB_MODE_BIT                              ( ( StackType_t ) 0x20 )
43 #define portINSTRUCTION_SIZE                    ( ( StackType_t ) 4 )
44
45 /* Constants required to setup the PIT. */
46 #define portPIT_CLOCK_DIVISOR                   ( ( uint32_t ) 16 )
47 #define portPIT_COUNTER_VALUE                   ( ( ( configCPU_CLOCK_HZ / portPIT_CLOCK_DIVISOR ) / 1000UL ) * portTICK_PERIOD_MS )
48
49 /* Constants required to handle critical sections. */
50 #define portNO_CRITICAL_NESTING                 ( ( uint32_t ) 0 )
51
52
53 #define portINT_LEVEL_SENSITIVE  0
54 #define portPIT_ENABLE          ( ( uint16_t ) 0x1 << 24 )
55 #define portPIT_INT_ENABLE      ( ( uint16_t ) 0x1 << 25 )
56 /*-----------------------------------------------------------*/
57
58 /* Setup the PIT to generate the tick interrupts. */
59 static void prvSetupTimerInterrupt( void );
60
61 /* ulCriticalNesting will get set to zero when the first task starts.  It
62 cannot be initialised to 0 as this will cause interrupts to be enabled
63 during the kernel initialisation process. */
64 uint32_t ulCriticalNesting = ( uint32_t ) 9999;
65
66 /*-----------------------------------------------------------*/
67
68 /*
69  * Initialise the stack of a task to look exactly as if a call to
70  * portSAVE_CONTEXT had been called.
71  *
72  * See header file for description.
73  */
74 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
75 {
76 StackType_t *pxOriginalTOS;
77
78         pxOriginalTOS = pxTopOfStack;
79
80         /* To ensure asserts in tasks.c don't fail, although in this case the assert
81         is not really required. */
82         pxTopOfStack--;
83
84         /* Setup the initial stack of the task.  The stack is set exactly as
85         expected by the portRESTORE_CONTEXT() macro. */
86
87         /* First on the stack is the return address - which in this case is the
88         start of the task.  The offset is added to make the return address appear
89         as it would within an IRQ ISR. */
90         *pxTopOfStack = ( StackType_t ) pxCode + portINSTRUCTION_SIZE;          
91         pxTopOfStack--;
92
93         *pxTopOfStack = ( StackType_t ) 0xaaaaaaaa;     /* R14 */
94         pxTopOfStack--; 
95         *pxTopOfStack = ( StackType_t ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
96         pxTopOfStack--;
97         *pxTopOfStack = ( StackType_t ) 0x12121212;     /* R12 */
98         pxTopOfStack--; 
99         *pxTopOfStack = ( StackType_t ) 0x11111111;     /* R11 */
100         pxTopOfStack--; 
101         *pxTopOfStack = ( StackType_t ) 0x10101010;     /* R10 */
102         pxTopOfStack--; 
103         *pxTopOfStack = ( StackType_t ) 0x09090909;     /* R9 */
104         pxTopOfStack--; 
105         *pxTopOfStack = ( StackType_t ) 0x08080808;     /* R8 */
106         pxTopOfStack--; 
107         *pxTopOfStack = ( StackType_t ) 0x07070707;     /* R7 */
108         pxTopOfStack--; 
109         *pxTopOfStack = ( StackType_t ) 0x06060606;     /* R6 */
110         pxTopOfStack--; 
111         *pxTopOfStack = ( StackType_t ) 0x05050505;     /* R5 */
112         pxTopOfStack--; 
113         *pxTopOfStack = ( StackType_t ) 0x04040404;     /* R4 */
114         pxTopOfStack--; 
115         *pxTopOfStack = ( StackType_t ) 0x03030303;     /* R3 */
116         pxTopOfStack--; 
117         *pxTopOfStack = ( StackType_t ) 0x02020202;     /* R2 */
118         pxTopOfStack--; 
119         *pxTopOfStack = ( StackType_t ) 0x01010101;     /* R1 */
120         pxTopOfStack--; 
121
122         /* When the task starts is will expect to find the function parameter in
123         R0. */
124         *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
125         pxTopOfStack--;
126
127         /* The status register is set for system mode, with interrupts enabled. */
128         *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;
129         
130         if( ( ( uint32_t ) pxCode & 0x01UL ) != 0x00UL )
131         {
132                 /* We want the task to start in thumb mode. */
133                 *pxTopOfStack |= portTHUMB_MODE_BIT;
134         }       
135         
136         pxTopOfStack--;
137
138         /* Interrupt flags cannot always be stored on the stack and will
139         instead be stored in a variable, which is then saved as part of the
140         tasks context. */
141         *pxTopOfStack = portNO_CRITICAL_NESTING;
142
143         return pxTopOfStack;    
144 }
145 /*-----------------------------------------------------------*/
146
147 BaseType_t xPortStartScheduler( void )
148 {
149 extern void vPortStartFirstTask( void );
150
151         /* Start the timer that generates the tick ISR.  Interrupts are disabled
152         here already. */
153         prvSetupTimerInterrupt();
154
155         /* Start the first task. */
156         vPortStartFirstTask();  
157
158         /* Should not get here! */
159         return 0;
160 }
161 /*-----------------------------------------------------------*/
162
163 void vPortEndScheduler( void )
164 {
165         /* It is unlikely that the ARM port will require this function as there
166         is nothing to return to.  */
167 }
168 /*-----------------------------------------------------------*/
169
170 #if configUSE_PREEMPTION == 0
171
172         /* The cooperative scheduler requires a normal IRQ service routine to
173         simply increment the system tick. */
174         static __arm __irq void vPortNonPreemptiveTick( void );
175         static __arm __irq void vPortNonPreemptiveTick( void )
176         {
177                 uint32_t ulDummy;
178                 
179                 /* Increment the tick count - which may wake some tasks but as the
180                 preemptive scheduler is not being used any woken task is not given
181                 processor time no matter what its priority. */
182                 xTaskIncrementTick();
183                 
184                 /* Clear the PIT interrupt. */
185                 ulDummy = AT91C_BASE_PITC->PITC_PIVR;
186                 
187                 /* End the interrupt in the AIC. */
188                 AT91C_BASE_AIC->AIC_EOICR = ulDummy;
189         }
190
191 #else
192
193         /* Currently the IAR port requires the preemptive tick function to be
194         defined in an asm file. */
195
196 #endif
197
198 /*-----------------------------------------------------------*/
199
200 static void prvSetupTimerInterrupt( void )
201 {
202 AT91PS_PITC pxPIT = AT91C_BASE_PITC;
203
204         /* Setup the AIC for PIT interrupts.  The interrupt routine chosen depends
205         on whether the preemptive or cooperative scheduler is being used. */
206         #if configUSE_PREEMPTION == 0
207
208                 AT91F_AIC_ConfigureIt( AT91C_BASE_AIC, AT91C_ID_SYS, AT91C_AIC_PRIOR_HIGHEST, portINT_LEVEL_SENSITIVE, ( void (*)(void) ) vPortNonPreemptiveTick );
209
210         #else
211                 
212                 extern void ( vPortPreemptiveTick )( void );
213                 AT91F_AIC_ConfigureIt( AT91C_BASE_AIC, AT91C_ID_SYS, AT91C_AIC_PRIOR_HIGHEST, portINT_LEVEL_SENSITIVE, ( void (*)(void) ) vPortPreemptiveTick );
214
215         #endif
216
217         /* Configure the PIT period. */
218         pxPIT->PITC_PIMR = portPIT_ENABLE | portPIT_INT_ENABLE | portPIT_COUNTER_VALUE;
219
220         /* Enable the interrupt.  Global interrupts are disables at this point so
221         this is safe. */
222         AT91F_AIC_EnableIt( AT91C_BASE_AIC, AT91C_ID_SYS );
223 }
224 /*-----------------------------------------------------------*/
225
226 void vPortEnterCritical( void )
227 {
228         /* Disable interrupts first! */
229         __disable_interrupt();
230
231         /* Now interrupts are disabled ulCriticalNesting can be accessed
232         directly.  Increment ulCriticalNesting to keep a count of how many times
233         portENTER_CRITICAL() has been called. */
234         ulCriticalNesting++;
235 }
236 /*-----------------------------------------------------------*/
237
238 void vPortExitCritical( void )
239 {
240         if( ulCriticalNesting > portNO_CRITICAL_NESTING )
241         {
242                 /* Decrement the nesting count as we are leaving a critical section. */
243                 ulCriticalNesting--;
244
245                 /* If the nesting level has reached zero then interrupts should be
246                 re-enabled. */
247                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
248                 {
249                         __enable_interrupt();
250                 }
251         }
252 }
253 /*-----------------------------------------------------------*/
254
255
256
257
258
259