]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/ARM7_LPC2000/port.c
6643e4c6775d7659fbed82548e5c2d8d3038c229
[freertos] / FreeRTOS / Source / portable / GCC / ARM7_LPC2000 / port.c
1 /*\r
2  * FreeRTOS Kernel V10.1.0\r
3  * Copyright (C) 2018 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 /*-----------------------------------------------------------\r
30  * Implementation of functions defined in portable.h for the ARM7 port.\r
31  *\r
32  * Components that can be compiled to either ARM or THUMB mode are\r
33  * contained in this file.  The ISR routines, which can only be compiled\r
34  * to ARM mode are contained in portISR.c.\r
35  *----------------------------------------------------------*/\r
36 \r
37 \r
38 /* Standard includes. */\r
39 #include <stdlib.h>\r
40 \r
41 /* Scheduler includes. */\r
42 #include "FreeRTOS.h"\r
43 #include "task.h"\r
44 \r
45 /* Constants required to setup the task context. */\r
46 #define portINITIAL_SPSR                                ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */\r
47 #define portTHUMB_MODE_BIT                              ( ( StackType_t ) 0x20 )\r
48 #define portINSTRUCTION_SIZE                    ( ( StackType_t ) 4 )\r
49 #define portNO_CRITICAL_SECTION_NESTING ( ( StackType_t ) 0 )\r
50 \r
51 /* Constants required to setup the tick ISR. */\r
52 #define portENABLE_TIMER                        ( ( uint8_t ) 0x01 )\r
53 #define portPRESCALE_VALUE                      0x00\r
54 #define portINTERRUPT_ON_MATCH          ( ( uint32_t ) 0x01 )\r
55 #define portRESET_COUNT_ON_MATCH        ( ( uint32_t ) 0x02 )\r
56 \r
57 /* Constants required to setup the VIC for the tick ISR. */\r
58 #define portTIMER_VIC_CHANNEL           ( ( uint32_t ) 0x0004 )\r
59 #define portTIMER_VIC_CHANNEL_BIT       ( ( uint32_t ) 0x0010 )\r
60 #define portTIMER_VIC_ENABLE            ( ( uint32_t ) 0x0020 )\r
61 \r
62 /*-----------------------------------------------------------*/\r
63 \r
64 /* Setup the timer to generate the tick interrupts. */\r
65 static void prvSetupTimerInterrupt( void );\r
66 \r
67 /* \r
68  * The scheduler can only be started from ARM mode, so \r
69  * vPortISRStartFirstSTask() is defined in portISR.c. \r
70  */\r
71 extern void vPortISRStartFirstTask( void );\r
72 \r
73 /*-----------------------------------------------------------*/\r
74 \r
75 /* \r
76  * Initialise the stack of a task to look exactly as if a call to \r
77  * portSAVE_CONTEXT had been called.\r
78  *\r
79  * See header file for description. \r
80  */\r
81 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
82 {\r
83 StackType_t *pxOriginalTOS;\r
84 \r
85         pxOriginalTOS = pxTopOfStack;\r
86 \r
87         /* To ensure asserts in tasks.c don't fail, although in this case the assert\r
88         is not really required. */\r
89         pxTopOfStack--;\r
90 \r
91         /* Setup the initial stack of the task.  The stack is set exactly as \r
92         expected by the portRESTORE_CONTEXT() macro. */\r
93 \r
94         /* First on the stack is the return address - which in this case is the\r
95         start of the task.  The offset is added to make the return address appear\r
96         as it would within an IRQ ISR. */\r
97         *pxTopOfStack = ( StackType_t ) pxCode + portINSTRUCTION_SIZE;          \r
98         pxTopOfStack--;\r
99 \r
100         *pxTopOfStack = ( StackType_t ) 0xaaaaaaaa;     /* R14 */\r
101         pxTopOfStack--; \r
102         *pxTopOfStack = ( StackType_t ) pxOriginalTOS; /* Stack used when task starts goes in R13. */\r
103         pxTopOfStack--;\r
104         *pxTopOfStack = ( StackType_t ) 0x12121212;     /* R12 */\r
105         pxTopOfStack--; \r
106         *pxTopOfStack = ( StackType_t ) 0x11111111;     /* R11 */\r
107         pxTopOfStack--; \r
108         *pxTopOfStack = ( StackType_t ) 0x10101010;     /* R10 */\r
109         pxTopOfStack--; \r
110         *pxTopOfStack = ( StackType_t ) 0x09090909;     /* R9 */\r
111         pxTopOfStack--; \r
112         *pxTopOfStack = ( StackType_t ) 0x08080808;     /* R8 */\r
113         pxTopOfStack--; \r
114         *pxTopOfStack = ( StackType_t ) 0x07070707;     /* R7 */\r
115         pxTopOfStack--; \r
116         *pxTopOfStack = ( StackType_t ) 0x06060606;     /* R6 */\r
117         pxTopOfStack--; \r
118         *pxTopOfStack = ( StackType_t ) 0x05050505;     /* R5 */\r
119         pxTopOfStack--; \r
120         *pxTopOfStack = ( StackType_t ) 0x04040404;     /* R4 */\r
121         pxTopOfStack--; \r
122         *pxTopOfStack = ( StackType_t ) 0x03030303;     /* R3 */\r
123         pxTopOfStack--; \r
124         *pxTopOfStack = ( StackType_t ) 0x02020202;     /* R2 */\r
125         pxTopOfStack--; \r
126         *pxTopOfStack = ( StackType_t ) 0x01010101;     /* R1 */\r
127         pxTopOfStack--; \r
128 \r
129         /* When the task starts is will expect to find the function parameter in\r
130         R0. */\r
131         *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */\r
132         pxTopOfStack--;\r
133 \r
134         /* The last thing onto the stack is the status register, which is set for\r
135         system mode, with interrupts enabled. */\r
136         *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;\r
137 \r
138         if( ( ( uint32_t ) pxCode & 0x01UL ) != 0x00 )\r
139         {\r
140                 /* We want the task to start in thumb mode. */\r
141                 *pxTopOfStack |= portTHUMB_MODE_BIT;\r
142         }\r
143 \r
144         pxTopOfStack--;\r
145 \r
146         /* Some optimisation levels use the stack differently to others.  This \r
147         means the interrupt flags cannot always be stored on the stack and will\r
148         instead be stored in a variable, which is then saved as part of the\r
149         tasks context. */\r
150         *pxTopOfStack = portNO_CRITICAL_SECTION_NESTING;\r
151 \r
152         return pxTopOfStack;\r
153 }\r
154 /*-----------------------------------------------------------*/\r
155 \r
156 BaseType_t xPortStartScheduler( void )\r
157 {\r
158         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
159         here already. */\r
160         prvSetupTimerInterrupt();\r
161 \r
162         /* Start the first task. */\r
163         vPortISRStartFirstTask();       \r
164 \r
165         /* Should not get here! */\r
166         return 0;\r
167 }\r
168 /*-----------------------------------------------------------*/\r
169 \r
170 void vPortEndScheduler( void )\r
171 {\r
172         /* It is unlikely that the ARM port will require this function as there\r
173         is nothing to return to.  */\r
174 }\r
175 /*-----------------------------------------------------------*/\r
176 \r
177 /*\r
178  * Setup the timer 0 to generate the tick interrupts at the required frequency.\r
179  */\r
180 static void prvSetupTimerInterrupt( void )\r
181 {\r
182 uint32_t ulCompareMatch;\r
183 extern void ( vTickISR )( void );\r
184 \r
185         /* A 1ms tick does not require the use of the timer prescale.  This is\r
186         defaulted to zero but can be used if necessary. */\r
187         T0_PR = portPRESCALE_VALUE;\r
188 \r
189         /* Calculate the match value required for our wanted tick rate. */\r
190         ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;\r
191 \r
192         /* Protect against divide by zero.  Using an if() statement still results\r
193         in a warning - hence the #if. */\r
194         #if portPRESCALE_VALUE != 0\r
195         {\r
196                 ulCompareMatch /= ( portPRESCALE_VALUE + 1 );\r
197         }\r
198         #endif\r
199         T0_MR0 = ulCompareMatch;\r
200 \r
201         /* Generate tick with timer 0 compare match. */\r
202         T0_MCR = portRESET_COUNT_ON_MATCH | portINTERRUPT_ON_MATCH;\r
203 \r
204         /* Setup the VIC for the timer. */\r
205         VICIntSelect &= ~( portTIMER_VIC_CHANNEL_BIT );\r
206         VICIntEnable |= portTIMER_VIC_CHANNEL_BIT;\r
207         \r
208         /* The ISR installed depends on whether the preemptive or cooperative\r
209         scheduler is being used. */\r
210 \r
211         VICVectAddr0 = ( int32_t ) vTickISR;\r
212         VICVectCntl0 = portTIMER_VIC_CHANNEL | portTIMER_VIC_ENABLE;\r
213 \r
214         /* Start the timer - interrupts are disabled when this function is called\r
215         so it is okay to do this here. */\r
216         T0_TCR = portENABLE_TIMER;\r
217 }\r
218 /*-----------------------------------------------------------*/\r
219 \r
220 \r
221 \r