]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/RVDS/ARM7_LPC21xx/port.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Source / portable / RVDS / ARM7_LPC21xx / 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 /* Standard includes. */\r
31 #include <stdlib.h>\r
32 \r
33 /* Scheduler includes. */\r
34 #include "FreeRTOS.h"\r
35 #include "task.h"\r
36 \r
37 /* Constants required to setup the initial task context. */\r
38 #define portINITIAL_SPSR                                ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */\r
39 #define portTHUMB_MODE_BIT                              ( ( StackType_t ) 0x20 )\r
40 #define portINSTRUCTION_SIZE                    ( ( StackType_t ) 4 )\r
41 #define portNO_CRITICAL_SECTION_NESTING ( ( StackType_t ) 0 )\r
42 \r
43 /* Constants required to setup the tick ISR. */\r
44 #define portENABLE_TIMER                        ( ( uint8_t ) 0x01 )\r
45 #define portPRESCALE_VALUE                      0x00\r
46 #define portINTERRUPT_ON_MATCH          ( ( uint32_t ) 0x01 )\r
47 #define portRESET_COUNT_ON_MATCH        ( ( uint32_t ) 0x02 )\r
48 \r
49 /* Constants required to setup the VIC for the tick ISR. */\r
50 #define portTIMER_VIC_CHANNEL           ( ( uint32_t ) 0x0004 )\r
51 #define portTIMER_VIC_CHANNEL_BIT       ( ( uint32_t ) 0x0010 )\r
52 #define portTIMER_VIC_ENABLE            ( ( uint32_t ) 0x0020 )\r
53 \r
54 /* Constants required to handle interrupts. */\r
55 #define portTIMER_MATCH_ISR_BIT         ( ( uint8_t ) 0x01 )\r
56 #define portCLEAR_VIC_INTERRUPT         ( ( uint32_t ) 0 )\r
57 \r
58 /*-----------------------------------------------------------*/\r
59 \r
60 /* The code generated by the Keil compiler does not maintain separate\r
61 stack and frame pointers. The portENTER_CRITICAL macro cannot therefore\r
62 use the stack as per other ports.  Instead a variable is used to keep\r
63 track of the critical section nesting.  This variable has to be stored\r
64 as part of the task context and must be initialised to a non zero value. */\r
65 \r
66 #define portNO_CRITICAL_NESTING         ( ( uint32_t ) 0 )\r
67 volatile uint32_t ulCriticalNesting = 9999UL;\r
68 \r
69 /*-----------------------------------------------------------*/\r
70 \r
71 /* Setup the timer to generate the tick interrupts. */\r
72 static void prvSetupTimerInterrupt( void );\r
73 \r
74 /* \r
75  * The scheduler can only be started from ARM mode, so \r
76  * vPortStartFirstSTask() is defined in portISR.c. \r
77  */\r
78 extern __asm void vPortStartFirstTask( void );\r
79 \r
80 /*-----------------------------------------------------------*/\r
81 \r
82 /* \r
83  * See header file for description. \r
84  */\r
85 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
86 {\r
87 StackType_t *pxOriginalTOS;\r
88 \r
89         /* Setup the initial stack of the task.  The stack is set exactly as \r
90         expected by the portRESTORE_CONTEXT() macro.\r
91 \r
92         Remember where the top of the (simulated) stack is before we place \r
93         anything on it. */\r
94         pxOriginalTOS = pxTopOfStack;\r
95         \r
96         /* To ensure asserts in tasks.c don't fail, although in this case the assert\r
97         is not really required. */\r
98         pxTopOfStack--;\r
99 \r
100         /* First on the stack is the return address - which in this case is the\r
101         start of the task.  The offset is added to make the return address appear\r
102         as it would within an IRQ ISR. */\r
103         *pxTopOfStack = ( StackType_t ) pxCode + portINSTRUCTION_SIZE;          \r
104         pxTopOfStack--;\r
105 \r
106         *pxTopOfStack = ( StackType_t ) 0xaaaaaaaa;     /* R14 */\r
107         pxTopOfStack--; \r
108         *pxTopOfStack = ( StackType_t ) pxOriginalTOS; /* Stack used when task starts goes in R13. */\r
109         pxTopOfStack--;\r
110         *pxTopOfStack = ( StackType_t ) 0x12121212;     /* R12 */\r
111         pxTopOfStack--; \r
112         *pxTopOfStack = ( StackType_t ) 0x11111111;     /* R11 */\r
113         pxTopOfStack--; \r
114         *pxTopOfStack = ( StackType_t ) 0x10101010;     /* R10 */\r
115         pxTopOfStack--; \r
116         *pxTopOfStack = ( StackType_t ) 0x09090909;     /* R9 */\r
117         pxTopOfStack--; \r
118         *pxTopOfStack = ( StackType_t ) 0x08080808;     /* R8 */\r
119         pxTopOfStack--; \r
120         *pxTopOfStack = ( StackType_t ) 0x07070707;     /* R7 */\r
121         pxTopOfStack--; \r
122         *pxTopOfStack = ( StackType_t ) 0x06060606;     /* R6 */\r
123         pxTopOfStack--; \r
124         *pxTopOfStack = ( StackType_t ) 0x05050505;     /* R5 */\r
125         pxTopOfStack--; \r
126         *pxTopOfStack = ( StackType_t ) 0x04040404;     /* R4 */\r
127         pxTopOfStack--; \r
128         *pxTopOfStack = ( StackType_t ) 0x03030303;     /* R3 */\r
129         pxTopOfStack--; \r
130         *pxTopOfStack = ( StackType_t ) 0x02020202;     /* R2 */\r
131         pxTopOfStack--; \r
132         *pxTopOfStack = ( StackType_t ) 0x01010101;     /* R1 */\r
133         pxTopOfStack--; \r
134         *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */\r
135         pxTopOfStack--;\r
136 \r
137         /* The last thing onto the stack is the status register, which is set for\r
138         system mode, with interrupts enabled. */\r
139         *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;\r
140 \r
141         if( ( ( uint32_t ) pxCode & 0x01UL ) != 0x00UL )\r
142         {\r
143                 /* We want the task to start in thumb mode. */\r
144                 *pxTopOfStack |= portTHUMB_MODE_BIT;\r
145         }\r
146 \r
147         pxTopOfStack--;\r
148 \r
149         /* The code generated by the Keil compiler does not maintain separate\r
150         stack and frame pointers. The portENTER_CRITICAL macro cannot therefore\r
151         use the stack as per other ports.  Instead a variable is used to keep\r
152         track of the critical section nesting.  This variable has to be stored\r
153         as part of the task context and is initially set to zero. */\r
154         *pxTopOfStack = portNO_CRITICAL_SECTION_NESTING;\r
155 \r
156         return pxTopOfStack;\r
157 }\r
158 /*-----------------------------------------------------------*/\r
159 \r
160 BaseType_t xPortStartScheduler( void )\r
161 {\r
162         /* Start the timer that generates the tick ISR. */\r
163         prvSetupTimerInterrupt();\r
164 \r
165         /* Start the first task.  This is done from portISR.c as ARM mode must be\r
166         used. */\r
167         vPortStartFirstTask();\r
168 \r
169         /* Should not get here! */\r
170         return 0;\r
171 }\r
172 /*-----------------------------------------------------------*/\r
173 \r
174 void vPortEndScheduler( void )\r
175 {\r
176         /* It is unlikely that the ARM port will require this function as there\r
177         is nothing to return to.  If this is required - stop the tick ISR then\r
178         return back to main. */\r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 #if configUSE_PREEMPTION == 0\r
183 \r
184         /* \r
185          * The cooperative scheduler requires a normal IRQ service routine to \r
186          * simply increment the system tick. \r
187          */\r
188         void vNonPreemptiveTick( void ) __irq;\r
189         void vNonPreemptiveTick( void ) __irq\r
190         {\r
191                 /* Increment the tick count - this may make a delaying task ready\r
192                 to run - but a context switch is not performed. */              \r
193                 xTaskIncrementTick();\r
194 \r
195                 T0IR = portTIMER_MATCH_ISR_BIT;                         /* Clear the timer event */\r
196                 VICVectAddr = portCLEAR_VIC_INTERRUPT;          /* Acknowledge the Interrupt */\r
197         }\r
198 \r
199  #else\r
200 \r
201         /*\r
202          **************************************************************************\r
203          * The preemptive scheduler ISR is written in assembler and can be found   \r
204          * in the portASM.s file. This will only get used if portUSE_PREEMPTION\r
205          * is set to 1 in portmacro.h\r
206          ************************************************************************** \r
207          */\r
208 \r
209           void vPreemptiveTick( void );\r
210 \r
211 #endif\r
212 /*-----------------------------------------------------------*/\r
213 \r
214 static void prvSetupTimerInterrupt( void )\r
215 {\r
216 uint32_t ulCompareMatch;\r
217 \r
218         /* A 1ms tick does not require the use of the timer prescale.  This is\r
219         defaulted to zero but can be used if necessary. */\r
220         T0PR = portPRESCALE_VALUE;\r
221 \r
222         /* Calculate the match value required for our wanted tick rate. */\r
223         ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;\r
224 \r
225         /* Protect against divide by zero.  Using an if() statement still results\r
226         in a warning - hence the #if. */\r
227         #if portPRESCALE_VALUE != 0\r
228         {\r
229                 ulCompareMatch /= ( portPRESCALE_VALUE + 1 );\r
230         }\r
231         #endif\r
232 \r
233         T0MR0 = ulCompareMatch;\r
234 \r
235         /* Generate tick with timer 0 compare match. */\r
236         T0MCR = portRESET_COUNT_ON_MATCH | portINTERRUPT_ON_MATCH;\r
237 \r
238         /* Setup the VIC for the timer. */\r
239         VICIntSelect &= ~( portTIMER_VIC_CHANNEL_BIT );\r
240         VICIntEnable |= portTIMER_VIC_CHANNEL_BIT;\r
241         \r
242         /* The ISR installed depends on whether the preemptive or cooperative\r
243         scheduler is being used. */\r
244         #if configUSE_PREEMPTION == 1\r
245         {       \r
246                 VICVectAddr0 = ( uint32_t ) vPreemptiveTick;\r
247         }\r
248         #else\r
249         {\r
250                 VICVectAddr0 = ( uint32_t ) vNonPreemptiveTick;\r
251         }\r
252         #endif\r
253 \r
254         VICVectCntl0 = portTIMER_VIC_CHANNEL | portTIMER_VIC_ENABLE;\r
255 \r
256         /* Start the timer - interrupts are disabled when this function is called\r
257         so it is okay to do this here. */\r
258         T0TCR = portENABLE_TIMER;\r
259 }\r
260 /*-----------------------------------------------------------*/\r
261 \r
262 void vPortEnterCritical( void )\r
263 {\r
264         /* Disable interrupts as per portDISABLE_INTERRUPTS();                                                  */\r
265         __disable_irq();\r
266 \r
267         /* Now interrupts are disabled ulCriticalNesting can be accessed \r
268         directly.  Increment ulCriticalNesting to keep a count of how many times\r
269         portENTER_CRITICAL() has been called. */\r
270         ulCriticalNesting++;\r
271 }\r
272 /*-----------------------------------------------------------*/\r
273 \r
274 void vPortExitCritical( void )\r
275 {\r
276         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
277         {\r
278                 /* Decrement the nesting count as we are leaving a critical section. */\r
279                 ulCriticalNesting--;\r
280 \r
281                 /* If the nesting level has reached zero then interrupts should be\r
282                 re-enabled. */\r
283                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
284                 {\r
285                         /* Enable interrupts as per portEXIT_CRITICAL(). */\r
286                         __enable_irq();\r
287                 }\r
288         }\r
289 }\r
290 /*-----------------------------------------------------------*/\r
291 \r
292 \r