]> git.sur5r.net Git - freertos/blob - Source/portable/IAR/STR91x/port.c
ed5d2802d2abafa17704274e28cab1c8ea5743fe
[freertos] / Source / portable / IAR / STR91x / port.c
1 /*\r
2         FreeRTOS.org V4.2.1 - Copyright (C) 2003-2007 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS distribution.\r
5 \r
6         FreeRTOS is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section\r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         See http://www.FreeRTOS.org for documentation, latest information, license\r
28         and contact details.  Please ensure to read the configuration and relevant\r
29         port sections of the online documentation.\r
30 \r
31         Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along\r
32         with commercial development and support options.\r
33         ***************************************************************************\r
34 */\r
35 \r
36 /*-----------------------------------------------------------\r
37  * Implementation of functions defined in portable.h for the ST STR91x ARM7\r
38  * port.\r
39  *----------------------------------------------------------*/\r
40 \r
41 /* Library includes. */\r
42 #include "91x_lib.h"\r
43 \r
44 /* Standard includes. */\r
45 #include <stdlib.h>\r
46 \r
47 /* Scheduler includes. */\r
48 #include "FreeRTOS.h"\r
49 #include "task.h"\r
50 \r
51 /* Constants required to setup the initial stack. */\r
52 #ifndef _RUN_TASK_IN_ARM_MODE_\r
53         #define portINITIAL_SPSR                        ( ( portSTACK_TYPE ) 0x3f ) /* System mode, THUMB mode, interrupts enabled. */\r
54 #else\r
55         #define portINITIAL_SPSR                        ( ( portSTACK_TYPE ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */\r
56 #endif\r
57 #define portINSTRUCTION_SIZE                    ( ( portSTACK_TYPE ) 4 )\r
58 \r
59 /* Constants required to handle critical sections. */\r
60 #define portNO_CRITICAL_NESTING                 ( ( unsigned portLONG ) 0 )\r
61 \r
62 \r
63 /*-----------------------------------------------------------*/\r
64 \r
65 /* Setup the watchdog to generate the tick interrupts. */\r
66 static void prvSetupTimerInterrupt( void );\r
67 \r
68 /* ulCriticalNesting will get set to zero when the first task starts.  It\r
69 cannot be initialised to 0 as this will cause interrupts to be enabled\r
70 during the kernel initialisation process. */\r
71 unsigned portLONG ulCriticalNesting = ( unsigned portLONG ) 9999;\r
72 \r
73 /* Tick interrupt routines for cooperative and preemptive operation\r
74 respectively.  The preemptive version is not defined as __irq as it is called\r
75 from an asm wrapper function. */\r
76 __arm __irq void vPortNonPreemptiveTick( void );\r
77 void WDG_IRQHandler( void );\r
78 \r
79 /* VIC interrupt default handler. */\r
80 static void prvDefaultHandler( void );\r
81 \r
82 /*-----------------------------------------------------------*/\r
83 \r
84 /*\r
85  * Initialise the stack of a task to look exactly as if a call to\r
86  * portSAVE_CONTEXT had been called.\r
87  *\r
88  * See header file for description.\r
89  */\r
90 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
91 {\r
92         portSTACK_TYPE *pxOriginalTOS;\r
93 \r
94         pxOriginalTOS = pxTopOfStack;\r
95 \r
96         /* Setup the initial stack of the task.  The stack is set exactly as\r
97         expected by the portRESTORE_CONTEXT() macro. */\r
98 \r
99         /* First on the stack is the return address - which in this case is the\r
100         start of the task.  The offset is added to make the return address appear\r
101         as it would within an IRQ ISR. */\r
102         *pxTopOfStack = ( portSTACK_TYPE ) pxCode + portINSTRUCTION_SIZE;               \r
103         pxTopOfStack--;\r
104 \r
105         *pxTopOfStack = ( portSTACK_TYPE ) 0xaaaaaaaa;  /* R14 */\r
106         pxTopOfStack--; \r
107         *pxTopOfStack = ( portSTACK_TYPE ) pxOriginalTOS; /* Stack used when task starts goes in R13. */\r
108         pxTopOfStack--;\r
109         *pxTopOfStack = ( portSTACK_TYPE ) 0x12121212;  /* R12 */\r
110         pxTopOfStack--; \r
111         *pxTopOfStack = ( portSTACK_TYPE ) 0x11111111;  /* R11 */\r
112         pxTopOfStack--; \r
113         *pxTopOfStack = ( portSTACK_TYPE ) 0x10101010;  /* R10 */\r
114         pxTopOfStack--; \r
115         *pxTopOfStack = ( portSTACK_TYPE ) 0x09090909;  /* R9 */\r
116         pxTopOfStack--; \r
117         *pxTopOfStack = ( portSTACK_TYPE ) 0x08080808;  /* R8 */\r
118         pxTopOfStack--; \r
119         *pxTopOfStack = ( portSTACK_TYPE ) 0x07070707;  /* R7 */\r
120         pxTopOfStack--; \r
121         *pxTopOfStack = ( portSTACK_TYPE ) 0x06060606;  /* R6 */\r
122         pxTopOfStack--; \r
123         *pxTopOfStack = ( portSTACK_TYPE ) 0x05050505;  /* R5 */\r
124         pxTopOfStack--; \r
125         *pxTopOfStack = ( portSTACK_TYPE ) 0x04040404;  /* R4 */\r
126         pxTopOfStack--; \r
127         *pxTopOfStack = ( portSTACK_TYPE ) 0x03030303;  /* R3 */\r
128         pxTopOfStack--; \r
129         *pxTopOfStack = ( portSTACK_TYPE ) 0x02020202;  /* R2 */\r
130         pxTopOfStack--; \r
131         *pxTopOfStack = ( portSTACK_TYPE ) 0x01010101;  /* R1 */\r
132         pxTopOfStack--; \r
133 \r
134         /* When the task starts is will expect to find the function parameter in\r
135         R0. */\r
136         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */\r
137         pxTopOfStack--;\r
138 \r
139         /* The status register is set for system mode, with interrupts enabled. */\r
140         *pxTopOfStack = ( portSTACK_TYPE ) portINITIAL_SPSR;\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 portBASE_TYPE 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 /* This function is called from an asm wrapper, so does not require the __irq\r
176 keyword. */\r
177 void WDG_IRQHandler( void )\r
178 {\r
179         /* Increment the tick counter. */\r
180         vTaskIncrementTick();\r
181 \r
182         #if configUSE_PREEMPTION == 1\r
183                 /* The new tick value might unblock a task.  Ensure the highest task that\r
184                 is ready to execute is the task that will execute when the tick ISR\r
185                 exits. */\r
186                 vTaskSwitchContext();\r
187         #endif\r
188                 \r
189         /* Clear the interrupt in the watchdog. */\r
190         WDG->SR &= ~0x0001;\r
191 }\r
192 /*-----------------------------------------------------------*/\r
193 \r
194 #ifndef abs\r
195         #define abs(x) ((x)>0 ? (x) : -(x))\r
196 #endif\r
197 \r
198 static void prvFindFactors(u32 n, u16 *a, u32 *b)\r
199 {\r
200         /* This function is copied from the ST STR7 library and is\r
201         copyright STMicroelectronics.  Reproduced with permission. */\r
202 \r
203         u32 b0;\r
204         u16 a0;\r
205         long err, err_min=n;\r
206 \r
207         *a = a0 = ((n-1)/65536ul) + 1;\r
208         *b = b0 = n / *a;\r
209 \r
210         for (; *a <= 256; (*a)++)\r
211         {\r
212                 *b = n / *a;\r
213                 err = (long)*a * (long)*b - (long)n;\r
214                 if (abs(err) > (*a / 2))\r
215                 {\r
216                         (*b)++;\r
217                         err = (long)*a * (long)*b - (long)n;\r
218                 }\r
219                 if (abs(err) < abs(err_min))\r
220                 {\r
221                         err_min = err;\r
222                         a0 = *a;\r
223                         b0 = *b;\r
224                         if (err == 0) break;\r
225                 }\r
226         }\r
227 \r
228         *a = a0;\r
229         *b = b0;\r
230 }\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 static void prvSetupTimerInterrupt( void )\r
234 {\r
235 WDG_InitTypeDef xWdg;\r
236 unsigned portSHORT a;\r
237 unsigned portLONG n = configCPU_PERIPH_HZ / configTICK_RATE_HZ, b;\r
238 \r
239         /* Configure the watchdog as a free running timer that generates a\r
240         periodic interrupt. */\r
241 \r
242         SCU_APBPeriphClockConfig( __WDG, ENABLE );\r
243         WDG_DeInit();\r
244         WDG_StructInit(&xWdg);\r
245         prvFindFactors( n, &a, &b );\r
246         xWdg.WDG_Prescaler = a - 1;\r
247         xWdg.WDG_Preload = b - 1;\r
248         WDG_Init( &xWdg );\r
249         WDG_ITConfig(ENABLE);\r
250         \r
251         /* Configure the VIC for the WDG interrupt. */\r
252         VIC_Config( WDG_ITLine, VIC_IRQ, 10 );\r
253         VIC_ITCmd( WDG_ITLine, ENABLE );\r
254         \r
255         /* Install the default handlers for both VIC's. */\r
256         VIC0->DVAR = ( unsigned portLONG ) prvDefaultHandler;\r
257         VIC1->DVAR = ( unsigned portLONG ) prvDefaultHandler;\r
258         \r
259         WDG_Cmd(ENABLE);\r
260 }\r
261 \r
262 /*-----------------------------------------------------------*/\r
263 \r
264 __arm __interwork void vPortEnterCritical( void )\r
265 {\r
266         /* Disable interrupts first! */\r
267         portDISABLE_INTERRUPTS();\r
268 \r
269         /* Now interrupts are disabled ulCriticalNesting can be accessed\r
270         directly.  Increment ulCriticalNesting to keep a count of how many times\r
271         portENTER_CRITICAL() has been called. */\r
272         ulCriticalNesting++;\r
273 }\r
274 /*-----------------------------------------------------------*/\r
275 \r
276 __arm __interwork void vPortExitCritical( void )\r
277 {\r
278         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
279         {\r
280                 /* Decrement the nesting count as we are leaving a critical section. */\r
281                 ulCriticalNesting--;\r
282 \r
283                 /* If the nesting level has reached zero then interrupts should be\r
284                 re-enabled. */\r
285                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
286                 {\r
287                         portENABLE_INTERRUPTS();\r
288                 }\r
289         }\r
290 }\r
291 /*-----------------------------------------------------------*/\r
292 \r
293 static void prvDefaultHandler( void )\r
294 {\r
295 }\r
296 \r
297 \r
298 \r
299 \r
300 \r