]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/RVDS/ARM7_LPC21xx/port.c
Update version numbers in preparation for new release.
[freertos] / FreeRTOS / Source / portable / RVDS / ARM7_LPC21xx / port.c
1 /*\r
2     FreeRTOS V8.2.2 - Copyright (C) 2015 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 \r
71 /* Standard includes. */\r
72 #include <stdlib.h>\r
73 \r
74 /* Scheduler includes. */\r
75 #include "FreeRTOS.h"\r
76 #include "task.h"\r
77 \r
78 /* Constants required to setup the initial task context. */\r
79 #define portINITIAL_SPSR                                ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */\r
80 #define portTHUMB_MODE_BIT                              ( ( StackType_t ) 0x20 )\r
81 #define portINSTRUCTION_SIZE                    ( ( StackType_t ) 4 )\r
82 #define portNO_CRITICAL_SECTION_NESTING ( ( StackType_t ) 0 )\r
83 \r
84 /* Constants required to setup the tick ISR. */\r
85 #define portENABLE_TIMER                        ( ( uint8_t ) 0x01 )\r
86 #define portPRESCALE_VALUE                      0x00\r
87 #define portINTERRUPT_ON_MATCH          ( ( uint32_t ) 0x01 )\r
88 #define portRESET_COUNT_ON_MATCH        ( ( uint32_t ) 0x02 )\r
89 \r
90 /* Constants required to setup the VIC for the tick ISR. */\r
91 #define portTIMER_VIC_CHANNEL           ( ( uint32_t ) 0x0004 )\r
92 #define portTIMER_VIC_CHANNEL_BIT       ( ( uint32_t ) 0x0010 )\r
93 #define portTIMER_VIC_ENABLE            ( ( uint32_t ) 0x0020 )\r
94 \r
95 /* Constants required to handle interrupts. */\r
96 #define portTIMER_MATCH_ISR_BIT         ( ( uint8_t ) 0x01 )\r
97 #define portCLEAR_VIC_INTERRUPT         ( ( uint32_t ) 0 )\r
98 \r
99 /*-----------------------------------------------------------*/\r
100 \r
101 /* The code generated by the Keil compiler does not maintain separate\r
102 stack and frame pointers. The portENTER_CRITICAL macro cannot therefore\r
103 use the stack as per other ports.  Instead a variable is used to keep\r
104 track of the critical section nesting.  This variable has to be stored\r
105 as part of the task context and must be initialised to a non zero value. */\r
106 \r
107 #define portNO_CRITICAL_NESTING         ( ( uint32_t ) 0 )\r
108 volatile uint32_t ulCriticalNesting = 9999UL;\r
109 \r
110 /*-----------------------------------------------------------*/\r
111 \r
112 /* Setup the timer to generate the tick interrupts. */\r
113 static void prvSetupTimerInterrupt( void );\r
114 \r
115 /* \r
116  * The scheduler can only be started from ARM mode, so \r
117  * vPortStartFirstSTask() is defined in portISR.c. \r
118  */\r
119 extern __asm void vPortStartFirstTask( void );\r
120 \r
121 /*-----------------------------------------------------------*/\r
122 \r
123 /* \r
124  * See header file for description. \r
125  */\r
126 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
127 {\r
128 StackType_t *pxOriginalTOS;\r
129 \r
130         /* Setup the initial stack of the task.  The stack is set exactly as \r
131         expected by the portRESTORE_CONTEXT() macro.\r
132 \r
133         Remember where the top of the (simulated) stack is before we place \r
134         anything on it. */\r
135         pxOriginalTOS = pxTopOfStack;\r
136         \r
137         /* To ensure asserts in tasks.c don't fail, although in this case the assert\r
138         is not really required. */\r
139         pxTopOfStack--;\r
140 \r
141         /* First on the stack is the return address - which in this case is the\r
142         start of the task.  The offset is added to make the return address appear\r
143         as it would within an IRQ ISR. */\r
144         *pxTopOfStack = ( StackType_t ) pxCode + portINSTRUCTION_SIZE;          \r
145         pxTopOfStack--;\r
146 \r
147         *pxTopOfStack = ( StackType_t ) 0xaaaaaaaa;     /* R14 */\r
148         pxTopOfStack--; \r
149         *pxTopOfStack = ( StackType_t ) pxOriginalTOS; /* Stack used when task starts goes in R13. */\r
150         pxTopOfStack--;\r
151         *pxTopOfStack = ( StackType_t ) 0x12121212;     /* R12 */\r
152         pxTopOfStack--; \r
153         *pxTopOfStack = ( StackType_t ) 0x11111111;     /* R11 */\r
154         pxTopOfStack--; \r
155         *pxTopOfStack = ( StackType_t ) 0x10101010;     /* R10 */\r
156         pxTopOfStack--; \r
157         *pxTopOfStack = ( StackType_t ) 0x09090909;     /* R9 */\r
158         pxTopOfStack--; \r
159         *pxTopOfStack = ( StackType_t ) 0x08080808;     /* R8 */\r
160         pxTopOfStack--; \r
161         *pxTopOfStack = ( StackType_t ) 0x07070707;     /* R7 */\r
162         pxTopOfStack--; \r
163         *pxTopOfStack = ( StackType_t ) 0x06060606;     /* R6 */\r
164         pxTopOfStack--; \r
165         *pxTopOfStack = ( StackType_t ) 0x05050505;     /* R5 */\r
166         pxTopOfStack--; \r
167         *pxTopOfStack = ( StackType_t ) 0x04040404;     /* R4 */\r
168         pxTopOfStack--; \r
169         *pxTopOfStack = ( StackType_t ) 0x03030303;     /* R3 */\r
170         pxTopOfStack--; \r
171         *pxTopOfStack = ( StackType_t ) 0x02020202;     /* R2 */\r
172         pxTopOfStack--; \r
173         *pxTopOfStack = ( StackType_t ) 0x01010101;     /* R1 */\r
174         pxTopOfStack--; \r
175         *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */\r
176         pxTopOfStack--;\r
177 \r
178         /* The last thing onto the stack is the status register, which is set for\r
179         system mode, with interrupts enabled. */\r
180         *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;\r
181 \r
182         if( ( ( uint32_t ) pxCode & 0x01UL ) != 0x00UL )\r
183         {\r
184                 /* We want the task to start in thumb mode. */\r
185                 *pxTopOfStack |= portTHUMB_MODE_BIT;\r
186         }\r
187 \r
188         pxTopOfStack--;\r
189 \r
190         /* The code generated by the Keil compiler does not maintain separate\r
191         stack and frame pointers. The portENTER_CRITICAL macro cannot therefore\r
192         use the stack as per other ports.  Instead a variable is used to keep\r
193         track of the critical section nesting.  This variable has to be stored\r
194         as part of the task context and is initially set to zero. */\r
195         *pxTopOfStack = portNO_CRITICAL_SECTION_NESTING;\r
196 \r
197         return pxTopOfStack;\r
198 }\r
199 /*-----------------------------------------------------------*/\r
200 \r
201 BaseType_t xPortStartScheduler( void )\r
202 {\r
203         /* Start the timer that generates the tick ISR. */\r
204         prvSetupTimerInterrupt();\r
205 \r
206         /* Start the first task.  This is done from portISR.c as ARM mode must be\r
207         used. */\r
208         vPortStartFirstTask();\r
209 \r
210         /* Should not get here! */\r
211         return 0;\r
212 }\r
213 /*-----------------------------------------------------------*/\r
214 \r
215 void vPortEndScheduler( void )\r
216 {\r
217         /* It is unlikely that the ARM port will require this function as there\r
218         is nothing to return to.  If this is required - stop the tick ISR then\r
219         return back to main. */\r
220 }\r
221 /*-----------------------------------------------------------*/\r
222 \r
223 #if configUSE_PREEMPTION == 0\r
224 \r
225         /* \r
226          * The cooperative scheduler requires a normal IRQ service routine to \r
227          * simply increment the system tick. \r
228          */\r
229         void vNonPreemptiveTick( void ) __irq;\r
230         void vNonPreemptiveTick( void ) __irq\r
231         {\r
232                 /* Increment the tick count - this may make a delaying task ready\r
233                 to run - but a context switch is not performed. */              \r
234                 xTaskIncrementTick();\r
235 \r
236                 T0IR = portTIMER_MATCH_ISR_BIT;                         /* Clear the timer event */\r
237                 VICVectAddr = portCLEAR_VIC_INTERRUPT;          /* Acknowledge the Interrupt */\r
238         }\r
239 \r
240  #else\r
241 \r
242         /*\r
243          **************************************************************************\r
244          * The preemptive scheduler ISR is written in assembler and can be found   \r
245          * in the portASM.s file. This will only get used if portUSE_PREEMPTION\r
246          * is set to 1 in portmacro.h\r
247          ************************************************************************** \r
248          */\r
249 \r
250           void vPreemptiveTick( void );\r
251 \r
252 #endif\r
253 /*-----------------------------------------------------------*/\r
254 \r
255 static void prvSetupTimerInterrupt( void )\r
256 {\r
257 uint32_t ulCompareMatch;\r
258 \r
259         /* A 1ms tick does not require the use of the timer prescale.  This is\r
260         defaulted to zero but can be used if necessary. */\r
261         T0PR = portPRESCALE_VALUE;\r
262 \r
263         /* Calculate the match value required for our wanted tick rate. */\r
264         ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;\r
265 \r
266         /* Protect against divide by zero.  Using an if() statement still results\r
267         in a warning - hence the #if. */\r
268         #if portPRESCALE_VALUE != 0\r
269         {\r
270                 ulCompareMatch /= ( portPRESCALE_VALUE + 1 );\r
271         }\r
272         #endif\r
273 \r
274         T0MR0 = ulCompareMatch;\r
275 \r
276         /* Generate tick with timer 0 compare match. */\r
277         T0MCR = portRESET_COUNT_ON_MATCH | portINTERRUPT_ON_MATCH;\r
278 \r
279         /* Setup the VIC for the timer. */\r
280         VICIntSelect &= ~( portTIMER_VIC_CHANNEL_BIT );\r
281         VICIntEnable |= portTIMER_VIC_CHANNEL_BIT;\r
282         \r
283         /* The ISR installed depends on whether the preemptive or cooperative\r
284         scheduler is being used. */\r
285         #if configUSE_PREEMPTION == 1\r
286         {       \r
287                 VICVectAddr0 = ( uint32_t ) vPreemptiveTick;\r
288         }\r
289         #else\r
290         {\r
291                 VICVectAddr0 = ( uint32_t ) vNonPreemptiveTick;\r
292         }\r
293         #endif\r
294 \r
295         VICVectCntl0 = portTIMER_VIC_CHANNEL | portTIMER_VIC_ENABLE;\r
296 \r
297         /* Start the timer - interrupts are disabled when this function is called\r
298         so it is okay to do this here. */\r
299         T0TCR = portENABLE_TIMER;\r
300 }\r
301 /*-----------------------------------------------------------*/\r
302 \r
303 void vPortEnterCritical( void )\r
304 {\r
305         /* Disable interrupts as per portDISABLE_INTERRUPTS();                                                  */\r
306         __disable_irq();\r
307 \r
308         /* Now interrupts are disabled ulCriticalNesting can be accessed \r
309         directly.  Increment ulCriticalNesting to keep a count of how many times\r
310         portENTER_CRITICAL() has been called. */\r
311         ulCriticalNesting++;\r
312 }\r
313 /*-----------------------------------------------------------*/\r
314 \r
315 void vPortExitCritical( void )\r
316 {\r
317         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
318         {\r
319                 /* Decrement the nesting count as we are leaving a critical section. */\r
320                 ulCriticalNesting--;\r
321 \r
322                 /* If the nesting level has reached zero then interrupts should be\r
323                 re-enabled. */\r
324                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
325                 {\r
326                         /* Enable interrupts as per portEXIT_CRITICAL(). */\r
327                         __enable_irq();\r
328                 }\r
329         }\r
330 }\r
331 /*-----------------------------------------------------------*/\r
332 \r
333 \r