]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/STR71x/port.c
Update version number in readiness for V10.2.0 release.
[freertos] / FreeRTOS / Source / portable / IAR / STR71x / port.c
1 /*\r
2  * FreeRTOS Kernel V10.2.0\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 ST STR71x ARM7\r
30  * port.\r
31  *----------------------------------------------------------*/\r
32 \r
33 /* Library includes. */\r
34 #include "wdg.h"\r
35 #include "eic.h"\r
36 \r
37 /* Standard includes. */\r
38 #include <stdlib.h>\r
39 \r
40 /* Scheduler includes. */\r
41 #include "FreeRTOS.h"\r
42 #include "task.h"\r
43 \r
44 /* Constants required to setup the initial stack. */\r
45 #define portINITIAL_SPSR                                ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */\r
46 #define portTHUMB_MODE_BIT                              ( ( StackType_t ) 0x20 )\r
47 #define portINSTRUCTION_SIZE                    ( ( StackType_t ) 4 )\r
48 \r
49 /* Constants required to handle critical sections. */\r
50 #define portNO_CRITICAL_NESTING                 ( ( uint32_t ) 0 )\r
51 \r
52 #define portMICROS_PER_SECOND 1000000\r
53 \r
54 /*-----------------------------------------------------------*/\r
55 \r
56 /* Setup the watchdog to generate the tick interrupts. */\r
57 static void prvSetupTimerInterrupt( void );\r
58 \r
59 /* ulCriticalNesting will get set to zero when the first task starts.  It\r
60 cannot be initialised to 0 as this will cause interrupts to be enabled\r
61 during the kernel initialisation process. */\r
62 uint32_t ulCriticalNesting = ( uint32_t ) 9999;\r
63 \r
64 /* Tick interrupt routines for cooperative and preemptive operation\r
65 respectively.  The preemptive version is not defined as __irq as it is called\r
66 from an asm wrapper function. */\r
67 __arm __irq void vPortNonPreemptiveTick( void );\r
68 void vPortPreemptiveTick( void );\r
69 \r
70 /*-----------------------------------------------------------*/\r
71 \r
72 /*\r
73  * Initialise the stack of a task to look exactly as if a call to\r
74  * portSAVE_CONTEXT had been called.\r
75  *\r
76  * See header file for description.\r
77  */\r
78 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
79 {\r
80 StackType_t *pxOriginalTOS;\r
81 \r
82         pxOriginalTOS = pxTopOfStack;\r
83 \r
84         /* To ensure asserts in tasks.c don't fail, although in this case the assert\r
85         is not really required. */\r
86         pxTopOfStack--;\r
87 \r
88         /* Setup the initial stack of the task.  The stack is set exactly as\r
89         expected by the portRESTORE_CONTEXT() macro. */\r
90 \r
91         /* First on the stack is the return address - which in this case is the\r
92         start of the task.  The offset is added to make the return address appear\r
93         as it would within an IRQ ISR. */\r
94         *pxTopOfStack = ( StackType_t ) pxCode + portINSTRUCTION_SIZE;          \r
95         pxTopOfStack--;\r
96 \r
97         *pxTopOfStack = ( StackType_t ) 0xaaaaaaaa;     /* R14 */\r
98         pxTopOfStack--; \r
99         *pxTopOfStack = ( StackType_t ) pxOriginalTOS; /* Stack used when task starts goes in R13. */\r
100         pxTopOfStack--;\r
101         *pxTopOfStack = ( StackType_t ) 0x12121212;     /* R12 */\r
102         pxTopOfStack--; \r
103         *pxTopOfStack = ( StackType_t ) 0x11111111;     /* R11 */\r
104         pxTopOfStack--; \r
105         *pxTopOfStack = ( StackType_t ) 0x10101010;     /* R10 */\r
106         pxTopOfStack--; \r
107         *pxTopOfStack = ( StackType_t ) 0x09090909;     /* R9 */\r
108         pxTopOfStack--; \r
109         *pxTopOfStack = ( StackType_t ) 0x08080808;     /* R8 */\r
110         pxTopOfStack--; \r
111         *pxTopOfStack = ( StackType_t ) 0x07070707;     /* R7 */\r
112         pxTopOfStack--; \r
113         *pxTopOfStack = ( StackType_t ) 0x06060606;     /* R6 */\r
114         pxTopOfStack--; \r
115         *pxTopOfStack = ( StackType_t ) 0x05050505;     /* R5 */\r
116         pxTopOfStack--; \r
117         *pxTopOfStack = ( StackType_t ) 0x04040404;     /* R4 */\r
118         pxTopOfStack--; \r
119         *pxTopOfStack = ( StackType_t ) 0x03030303;     /* R3 */\r
120         pxTopOfStack--; \r
121         *pxTopOfStack = ( StackType_t ) 0x02020202;     /* R2 */\r
122         pxTopOfStack--; \r
123         *pxTopOfStack = ( StackType_t ) 0x01010101;     /* R1 */\r
124         pxTopOfStack--; \r
125 \r
126         /* When the task starts is will expect to find the function parameter in\r
127         R0. */\r
128         *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */\r
129         pxTopOfStack--;\r
130 \r
131         /* The status register is set for system mode, with interrupts enabled. */\r
132         *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;\r
133         \r
134         if( ( ( uint32_t ) pxCode & 0x01UL ) != 0x00UL )\r
135         {\r
136                 /* We want the task to start in thumb mode. */\r
137                 *pxTopOfStack |= portTHUMB_MODE_BIT;\r
138         }               \r
139         \r
140         pxTopOfStack--;\r
141 \r
142         /* Interrupt flags cannot always be stored on the stack and will\r
143         instead be stored in a variable, which is then saved as part of the\r
144         tasks context. */\r
145         *pxTopOfStack = portNO_CRITICAL_NESTING;\r
146 \r
147         return pxTopOfStack;    \r
148 }\r
149 /*-----------------------------------------------------------*/\r
150 \r
151 BaseType_t xPortStartScheduler( void )\r
152 {\r
153 extern void vPortStartFirstTask( void );\r
154 \r
155         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
156         here already. */\r
157         prvSetupTimerInterrupt();\r
158 \r
159         /* Start the first task. */\r
160         vPortStartFirstTask();  \r
161 \r
162         /* Should not get here! */\r
163         return 0;\r
164 }\r
165 /*-----------------------------------------------------------*/\r
166 \r
167 void vPortEndScheduler( void )\r
168 {\r
169         /* It is unlikely that the ARM port will require this function as there\r
170         is nothing to return to.  */\r
171 }\r
172 /*-----------------------------------------------------------*/\r
173 \r
174 /* The cooperative scheduler requires a normal IRQ service routine to\r
175 simply increment the system tick. */\r
176 __arm __irq void vPortNonPreemptiveTick( void )\r
177 {\r
178         /* Increment the tick count - which may wake some tasks but as the\r
179         preemptive scheduler is not being used any woken task is not given\r
180         processor time no matter what its priority. */\r
181         xTaskIncrementTick();\r
182 \r
183         /* Clear the interrupt in the watchdog and EIC. */\r
184         WDG->SR = 0x0000;\r
185         portCLEAR_EIC();                \r
186 }\r
187 /*-----------------------------------------------------------*/\r
188 \r
189 /* This function is called from an asm wrapper, so does not require the __irq\r
190 keyword. */\r
191 void vPortPreemptiveTick( void )\r
192 {\r
193         /* Increment the tick counter. */\r
194         if( xTaskIncrementTick() != pdFALSE )\r
195         {\r
196                 /* Select a new task to execute. */\r
197                 vTaskSwitchContext();\r
198         }\r
199 \r
200         /* Clear the interrupt in the watchdog and EIC. */\r
201         WDG->SR = 0x0000;\r
202         portCLEAR_EIC();                        \r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 static void prvSetupTimerInterrupt( void )\r
207 {\r
208         /* Set the watchdog up to generate a periodic tick. */\r
209         WDG_ECITConfig( DISABLE );\r
210         WDG_CntOnOffConfig( DISABLE );\r
211         WDG_PeriodValueConfig( portMICROS_PER_SECOND / configTICK_RATE_HZ );\r
212 \r
213         /* Setup the tick interrupt in the EIC. */\r
214         EIC_IRQChannelPriorityConfig( WDG_IRQChannel, 1 );\r
215         EIC_IRQChannelConfig( WDG_IRQChannel, ENABLE );\r
216         EIC_IRQConfig( ENABLE );\r
217         WDG_ECITConfig( ENABLE );\r
218 \r
219         /* Start the timer - interrupts are actually disabled at this point so\r
220         it is safe to do this here. */\r
221         WDG_CntOnOffConfig( ENABLE );\r
222 }\r
223 /*-----------------------------------------------------------*/\r
224 \r
225 __arm __interwork void vPortEnterCritical( void )\r
226 {\r
227         /* Disable interrupts first! */\r
228         __disable_interrupt();\r
229 \r
230         /* Now interrupts are disabled ulCriticalNesting can be accessed\r
231         directly.  Increment ulCriticalNesting to keep a count of how many times\r
232         portENTER_CRITICAL() has been called. */\r
233         ulCriticalNesting++;\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 __arm __interwork void vPortExitCritical( void )\r
238 {\r
239         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
240         {\r
241                 /* Decrement the nesting count as we are leaving a critical section. */\r
242                 ulCriticalNesting--;\r
243 \r
244                 /* If the nesting level has reached zero then interrupts should be\r
245                 re-enabled. */\r
246                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
247                 {\r
248                         __enable_interrupt();\r
249                 }\r
250         }\r
251 }\r
252 /*-----------------------------------------------------------*/\r
253 \r
254 \r
255 \r
256 \r
257 \r
258 \r