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