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