]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/AtmelSAM9XE/port.c
Update version number ready for next release.
[freertos] / FreeRTOS / Source / portable / IAR / AtmelSAM9XE / port.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 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 /* Hardware includes. */\r
41 #include <board.h>\r
42 #include <pio/pio.h>\r
43 #include <pio/pio_it.h>\r
44 #include <pit/pit.h>\r
45 #include <aic/aic.h>\r
46 #include <tc/tc.h>\r
47 #include <utility/led.h>\r
48 #include <utility/trace.h>\r
49 \r
50 /*-----------------------------------------------------------*/\r
51 \r
52 /* Constants required to setup the initial stack. */\r
53 #define portINITIAL_SPSR                                ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */\r
54 #define portTHUMB_MODE_BIT                              ( ( StackType_t ) 0x20 )\r
55 #define portINSTRUCTION_SIZE                    ( ( StackType_t ) 4 )\r
56 \r
57 /* Constants required to setup the PIT. */\r
58 #define port1MHz_IN_Hz                                  ( 1000000ul )\r
59 #define port1SECOND_IN_uS                               ( 1000000.0 )\r
60 \r
61 /* Constants required to handle critical sections. */\r
62 #define portNO_CRITICAL_NESTING                 ( ( uint32_t ) 0 )\r
63 \r
64 \r
65 #define portINT_LEVEL_SENSITIVE  0\r
66 #define portPIT_ENABLE          ( ( uint16_t ) 0x1 << 24 )\r
67 #define portPIT_INT_ENABLE      ( ( uint16_t ) 0x1 << 25 )\r
68 /*-----------------------------------------------------------*/\r
69 \r
70 /* Setup the PIT to generate the tick interrupts. */\r
71 static void prvSetupTimerInterrupt( void );\r
72 \r
73 /* The PIT interrupt handler - the RTOS tick. */\r
74 static void vPortTickISR( void );\r
75 \r
76 /* ulCriticalNesting will get set to zero when the first task starts.  It\r
77 cannot be initialised to 0 as this will cause interrupts to be enabled\r
78 during the kernel initialisation process. */\r
79 uint32_t ulCriticalNesting = ( uint32_t ) 9999;\r
80 \r
81 /*-----------------------------------------------------------*/\r
82 \r
83 /*\r
84  * Initialise the stack of a task to look exactly as if a call to\r
85  * portSAVE_CONTEXT had been called.\r
86  *\r
87  * See header file for description.\r
88  */\r
89 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
90 {\r
91 StackType_t *pxOriginalTOS;\r
92 \r
93         pxOriginalTOS = pxTopOfStack;\r
94 \r
95         /* To ensure asserts in tasks.c don't fail, although in this case the assert\r
96         is not really required. */\r
97         pxTopOfStack--;\r
98 \r
99         /* Setup the initial stack of the task.  The stack is set exactly as\r
100         expected by the portRESTORE_CONTEXT() macro. */\r
101 \r
102         /* First on the stack is the return address - which in this case is the\r
103         start of the task.  The offset is added to make the return address appear\r
104         as it would within an IRQ ISR. */\r
105         *pxTopOfStack = ( StackType_t ) pxCode + portINSTRUCTION_SIZE;          \r
106         pxTopOfStack--;\r
107 \r
108         *pxTopOfStack = ( StackType_t ) 0xaaaaaaaa;     /* R14 */\r
109         pxTopOfStack--; \r
110         *pxTopOfStack = ( StackType_t ) pxOriginalTOS; /* Stack used when task starts goes in R13. */\r
111         pxTopOfStack--;\r
112         *pxTopOfStack = ( StackType_t ) 0x12121212;     /* R12 */\r
113         pxTopOfStack--; \r
114         *pxTopOfStack = ( StackType_t ) 0x11111111;     /* R11 */\r
115         pxTopOfStack--; \r
116         *pxTopOfStack = ( StackType_t ) 0x10101010;     /* R10 */\r
117         pxTopOfStack--; \r
118         *pxTopOfStack = ( StackType_t ) 0x09090909;     /* R9 */\r
119         pxTopOfStack--; \r
120         *pxTopOfStack = ( StackType_t ) 0x08080808;     /* R8 */\r
121         pxTopOfStack--; \r
122         *pxTopOfStack = ( StackType_t ) 0x07070707;     /* R7 */\r
123         pxTopOfStack--; \r
124         *pxTopOfStack = ( StackType_t ) 0x06060606;     /* R6 */\r
125         pxTopOfStack--; \r
126         *pxTopOfStack = ( StackType_t ) 0x05050505;     /* R5 */\r
127         pxTopOfStack--; \r
128         *pxTopOfStack = ( StackType_t ) 0x04040404;     /* R4 */\r
129         pxTopOfStack--; \r
130         *pxTopOfStack = ( StackType_t ) 0x03030303;     /* R3 */\r
131         pxTopOfStack--; \r
132         *pxTopOfStack = ( StackType_t ) 0x02020202;     /* R2 */\r
133         pxTopOfStack--; \r
134         *pxTopOfStack = ( StackType_t ) 0x01010101;     /* R1 */\r
135         pxTopOfStack--; \r
136 \r
137         /* When the task starts is will expect to find the function parameter in\r
138         R0. */\r
139         *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */\r
140         pxTopOfStack--;\r
141 \r
142         /* The status register is set for system mode, with interrupts enabled. */\r
143         *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;\r
144         \r
145         #ifdef THUMB_INTERWORK\r
146         {\r
147                 /* We want the task to start in thumb mode. */\r
148                 *pxTopOfStack |= portTHUMB_MODE_BIT;\r
149         }\r
150         #endif\r
151         \r
152         pxTopOfStack--;\r
153 \r
154         /* Interrupt flags cannot always be stored on the stack and will\r
155         instead be stored in a variable, which is then saved as part of the\r
156         tasks context. */\r
157         *pxTopOfStack = portNO_CRITICAL_NESTING;\r
158 \r
159         return pxTopOfStack;    \r
160 }\r
161 /*-----------------------------------------------------------*/\r
162 \r
163 BaseType_t xPortStartScheduler( void )\r
164 {\r
165 extern void vPortStartFirstTask( void );\r
166 \r
167         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
168         here already. */\r
169         prvSetupTimerInterrupt();\r
170 \r
171         /* Start the first task. */\r
172         vPortStartFirstTask();  \r
173 \r
174         /* Should not get here! */\r
175         return 0;\r
176 }\r
177 /*-----------------------------------------------------------*/\r
178 \r
179 void vPortEndScheduler( void )\r
180 {\r
181         /* It is unlikely that the ARM port will require this function as there\r
182         is nothing to return to.  */\r
183 }\r
184 /*-----------------------------------------------------------*/\r
185 \r
186 static __arm void vPortTickISR( void )\r
187 {\r
188 volatile uint32_t ulDummy;\r
189         \r
190         /* Increment the tick count - which may wake some tasks but as the\r
191         preemptive scheduler is not being used any woken task is not given\r
192         processor time no matter what its priority. */\r
193         if( xTaskIncrementTick() != pdFALSE )\r
194         {\r
195                 vTaskSwitchContext();\r
196         }\r
197                 \r
198         /* Clear the PIT interrupt. */\r
199         ulDummy = AT91C_BASE_PITC->PITC_PIVR;\r
200         \r
201         /* To remove compiler warning. */\r
202         ( void ) ulDummy;\r
203         \r
204         /* The AIC is cleared in the asm wrapper, outside of this function. */\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 static void prvSetupTimerInterrupt( void )\r
209 {\r
210 const uint32_t ulPeriodIn_uS = ( 1.0 / ( double ) configTICK_RATE_HZ ) * port1SECOND_IN_uS;\r
211 \r
212         /* Setup the PIT for the required frequency. */\r
213         PIT_Init( ulPeriodIn_uS, BOARD_MCK / port1MHz_IN_Hz );\r
214         \r
215         /* Setup the PIT interrupt. */\r
216         AIC_DisableIT( AT91C_ID_SYS );\r
217         AIC_ConfigureIT( AT91C_ID_SYS, AT91C_AIC_PRIOR_LOWEST, vPortTickISR );\r
218         AIC_EnableIT( AT91C_ID_SYS );\r
219         PIT_EnableIT();\r
220 }\r
221 /*-----------------------------------------------------------*/\r
222 \r
223 void vPortEnterCritical( void )\r
224 {\r
225         /* Disable interrupts first! */\r
226         __disable_irq();\r
227 \r
228         /* Now interrupts are disabled ulCriticalNesting can be accessed\r
229         directly.  Increment ulCriticalNesting to keep a count of how many times\r
230         portENTER_CRITICAL() has been called. */\r
231         ulCriticalNesting++;\r
232 }\r
233 /*-----------------------------------------------------------*/\r
234 \r
235 void vPortExitCritical( void )\r
236 {\r
237         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
238         {\r
239                 /* Decrement the nesting count as we are leaving a critical section. */\r
240                 ulCriticalNesting--;\r
241 \r
242                 /* If the nesting level has reached zero then interrupts should be\r
243                 re-enabled. */\r
244                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
245                 {\r
246                         __enable_irq();\r
247                 }\r
248         }\r
249 }\r
250 /*-----------------------------------------------------------*/\r
251 \r
252 \r
253 \r
254 \r
255 \r
256 \r