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