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