]> begriffs open source - freertos/blob - Source/portable/IAR/STR91x/port.c
Changes between V4.5.0 and V4.6.0 released October 28 2007
[freertos] / Source / portable / IAR / STR91x / port.c
1 /*\r
2         FreeRTOS.org V4.6.0 - 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 ARM9\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 #include <assert.h>\r
47 \r
48 /* Scheduler includes. */\r
49 #include "FreeRTOS.h"\r
50 #include "task.h"\r
51 \r
52 #ifndef configUSE_WATCHDOG_TICK\r
53         #error configUSE_WATCHDOG_TICK must be set to either 1 or 0 in FreeRTOSConfig.h to use either the Watchdog or timer 2 to generate the tick interrupt respectively.\r
54 #endif\r
55 \r
56 /* Constants required to setup the initial stack. */\r
57 #ifndef _RUN_TASK_IN_ARM_MODE_\r
58         #define portINITIAL_SPSR                        ( ( portSTACK_TYPE ) 0x3f ) /* System mode, THUMB mode, interrupts enabled. */\r
59 #else\r
60         #define portINITIAL_SPSR                        ( ( portSTACK_TYPE ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */\r
61 #endif\r
62 \r
63 #define portINSTRUCTION_SIZE                    ( ( portSTACK_TYPE ) 4 )\r
64 \r
65 /* Constants required to handle critical sections. */\r
66 #define portNO_CRITICAL_NESTING                 ( ( unsigned portLONG ) 0 )\r
67 \r
68 #ifndef abs\r
69         #define abs(x) ((x)>0 ? (x) : -(x))\r
70 #endif\r
71 \r
72 /**\r
73  * Toggle a led using the following algorithm:\r
74  * if ( GPIO_ReadBit(GPIO9, GPIO_Pin_2) )\r
75  * {\r
76  *   GPIO_WriteBit( GPIO9, GPIO_Pin_2, Bit_RESET );\r
77  * }\r
78  * else\r
79  * {\r
80  *   GPIO_WriteBit( GPIO9, GPIO_Pin_2, Bit_RESET );\r
81  * }\r
82  *\r
83  */\r
84 #define TOGGLE_LED(port,pin)                                                                    \\r
85         if ( ((((port)->DR[(pin)<<2])) & (pin)) != Bit_RESET )          \\r
86         {                                                                                                                       \\r
87         (port)->DR[(pin) <<2] = 0x00;                                                   \\r
88         }                                                                                                                       \\r
89         else                                                                                                            \\r
90         {                                                                                                                       \\r
91         (port)->DR[(pin) <<2] = (pin);                                                  \\r
92         }\r
93 \r
94 \r
95 /*-----------------------------------------------------------*/\r
96 \r
97 /* Setup the watchdog to generate the tick interrupts. */\r
98 static void prvSetupTimerInterrupt( void );\r
99 \r
100 /* ulCriticalNesting will get set to zero when the first task starts.  It\r
101 cannot be initialised to 0 as this will cause interrupts to be enabled\r
102 during the kernel initialisation process. */\r
103 unsigned portLONG ulCriticalNesting = ( unsigned portLONG ) 9999;\r
104 \r
105 /* Tick interrupt routines for cooperative and preemptive operation\r
106 respectively.  The preemptive version is not defined as __irq as it is called\r
107 from an asm wrapper function. */\r
108 void WDG_IRQHandler( void );\r
109 \r
110 /* VIC interrupt default handler. */\r
111 static void prvDefaultHandler( void );\r
112 \r
113 #if configUSE_WATCHDOG_TICK == 0\r
114         /* Used to update the OCR timer register */\r
115         static u16 s_nPulseLength;\r
116 #endif\r
117 \r
118 /*-----------------------------------------------------------*/\r
119 \r
120 /*\r
121  * Initialise the stack of a task to look exactly as if a call to\r
122  * portSAVE_CONTEXT had been called.\r
123  *\r
124  * See header file for description.\r
125  */\r
126 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
127 {\r
128         portSTACK_TYPE *pxOriginalTOS;\r
129 \r
130         pxOriginalTOS = pxTopOfStack;\r
131 \r
132         /* Setup the initial stack of the task.  The stack is set exactly as\r
133         expected by the portRESTORE_CONTEXT() macro. */\r
134 \r
135         /* First on the stack is the return address - which in this case is the\r
136         start of the task.  The offset is added to make the return address appear\r
137         as it would within an IRQ ISR. */\r
138         *pxTopOfStack = ( portSTACK_TYPE ) pxCode + portINSTRUCTION_SIZE;               \r
139         pxTopOfStack--;\r
140 \r
141         *pxTopOfStack = ( portSTACK_TYPE ) 0xaaaaaaaa;  /* R14 */\r
142         pxTopOfStack--; \r
143         *pxTopOfStack = ( portSTACK_TYPE ) pxOriginalTOS; /* Stack used when task starts goes in R13. */\r
144         pxTopOfStack--;\r
145         *pxTopOfStack = ( portSTACK_TYPE ) 0x12121212;  /* R12 */\r
146         pxTopOfStack--; \r
147         *pxTopOfStack = ( portSTACK_TYPE ) 0x11111111;  /* R11 */\r
148         pxTopOfStack--; \r
149         *pxTopOfStack = ( portSTACK_TYPE ) 0x10101010;  /* R10 */\r
150         pxTopOfStack--; \r
151         *pxTopOfStack = ( portSTACK_TYPE ) 0x09090909;  /* R9 */\r
152         pxTopOfStack--; \r
153         *pxTopOfStack = ( portSTACK_TYPE ) 0x08080808;  /* R8 */\r
154         pxTopOfStack--; \r
155         *pxTopOfStack = ( portSTACK_TYPE ) 0x07070707;  /* R7 */\r
156         pxTopOfStack--; \r
157         *pxTopOfStack = ( portSTACK_TYPE ) 0x06060606;  /* R6 */\r
158         pxTopOfStack--; \r
159         *pxTopOfStack = ( portSTACK_TYPE ) 0x05050505;  /* R5 */\r
160         pxTopOfStack--; \r
161         *pxTopOfStack = ( portSTACK_TYPE ) 0x04040404;  /* R4 */\r
162         pxTopOfStack--; \r
163         *pxTopOfStack = ( portSTACK_TYPE ) 0x03030303;  /* R3 */\r
164         pxTopOfStack--; \r
165         *pxTopOfStack = ( portSTACK_TYPE ) 0x02020202;  /* R2 */\r
166         pxTopOfStack--; \r
167         *pxTopOfStack = ( portSTACK_TYPE ) 0x01010101;  /* R1 */\r
168         pxTopOfStack--; \r
169 \r
170         /* When the task starts is will expect to find the function parameter in\r
171         R0. */\r
172         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */\r
173         pxTopOfStack--;\r
174 \r
175         /* The status register is set for system mode, with interrupts enabled. */\r
176         *pxTopOfStack = ( portSTACK_TYPE ) portINITIAL_SPSR;\r
177         pxTopOfStack--;\r
178 \r
179         /* Interrupt flags cannot always be stored on the stack and will\r
180         instead be stored in a variable, which is then saved as part of the\r
181         tasks context. */\r
182         *pxTopOfStack = portNO_CRITICAL_NESTING;\r
183 \r
184         return pxTopOfStack;    \r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 portBASE_TYPE xPortStartScheduler( void )\r
189 {\r
190 extern void vPortStartFirstTask( void );\r
191 \r
192         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
193         here already. */\r
194         prvSetupTimerInterrupt();\r
195 \r
196         /* Start the first task. */\r
197         vPortStartFirstTask();  \r
198 \r
199         /* Should not get here! */\r
200         return 0;\r
201 }\r
202 /*-----------------------------------------------------------*/\r
203 \r
204 void vPortEndScheduler( void )\r
205 {\r
206         /* It is unlikely that the ARM port will require this function as there\r
207         is nothing to return to.  */\r
208 }\r
209 /*-----------------------------------------------------------*/\r
210 \r
211 /* This function is called from an asm wrapper, so does not require the __irq\r
212 keyword. */\r
213 #if configUSE_WATCHDOG_TICK == 1\r
214 \r
215         static void prvFindFactors(u32 n, u16 *a, u32 *b)\r
216         {\r
217                 /* This function is copied from the ST STR7 library and is\r
218                 copyright STMicroelectronics.  Reproduced with permission. */\r
219         \r
220                 u32 b0;\r
221                 u16 a0;\r
222                 long err, err_min=n;\r
223         \r
224                 *a = a0 = ((n-1)/65536ul) + 1;\r
225                 *b = b0 = n / *a;\r
226         \r
227                 for (; *a <= 256; (*a)++)\r
228                 {\r
229                         *b = n / *a;\r
230                         err = (long)*a * (long)*b - (long)n;\r
231                         if (abs(err) > (*a / 2))\r
232                         {\r
233                                 (*b)++;\r
234                                 err = (long)*a * (long)*b - (long)n;\r
235                         }\r
236                         if (abs(err) < abs(err_min))\r
237                         {\r
238                                 err_min = err;\r
239                                 a0 = *a;\r
240                                 b0 = *b;\r
241                                 if (err == 0) break;\r
242                         }\r
243                 }\r
244         \r
245                 *a = a0;\r
246                 *b = b0;\r
247         }\r
248         /*-----------------------------------------------------------*/\r
249 \r
250         static void prvSetupTimerInterrupt( void )\r
251         {\r
252         WDG_InitTypeDef xWdg;\r
253         unsigned portSHORT a;\r
254         unsigned portLONG n = configCPU_PERIPH_HZ / configTICK_RATE_HZ, b;\r
255         \r
256                 /* Configure the watchdog as a free running timer that generates a\r
257                 periodic interrupt. */\r
258         \r
259                 SCU_APBPeriphClockConfig( __WDG, ENABLE );\r
260                 WDG_DeInit();\r
261                 WDG_StructInit(&xWdg);\r
262                 prvFindFactors( n, &a, &b );\r
263                 xWdg.WDG_Prescaler = a - 1;\r
264                 xWdg.WDG_Preload = b - 1;\r
265                 WDG_Init( &xWdg );\r
266                 WDG_ITConfig(ENABLE);\r
267                 \r
268                 /* Configure the VIC for the WDG interrupt. */\r
269                 VIC_Config( WDG_ITLine, VIC_IRQ, 10 );\r
270                 VIC_ITCmd( WDG_ITLine, ENABLE );\r
271                 \r
272                 /* Install the default handlers for both VIC's. */\r
273                 VIC0->DVAR = ( unsigned portLONG ) prvDefaultHandler;\r
274                 VIC1->DVAR = ( unsigned portLONG ) prvDefaultHandler;\r
275                 \r
276                 WDG_Cmd(ENABLE);\r
277         }\r
278         /*-----------------------------------------------------------*/\r
279 \r
280         void WDG_IRQHandler( void )\r
281         {\r
282                 {\r
283                         /* Increment the tick counter. */\r
284                         vTaskIncrementTick();\r
285                 \r
286                         #if configUSE_PREEMPTION == 1\r
287                         {\r
288                                 /* The new tick value might unblock a task.  Ensure the highest task that\r
289                                 is ready to execute is the task that will execute when the tick ISR\r
290                                 exits. */\r
291                                 vTaskSwitchContext();\r
292                         }\r
293                         #endif /* configUSE_PREEMPTION. */\r
294                 \r
295                         /* Clear the interrupt in the watchdog. */\r
296                         WDG->SR &= ~0x0001;\r
297                 }\r
298         }\r
299 \r
300 #else\r
301 \r
302         static void prvFindFactors(u32 n, u8 *a, u16 *b)\r
303         {\r
304                 /* This function is copied from the ST STR7 library and is\r
305                 copyright STMicroelectronics.  Reproduced with permission. */\r
306         \r
307                 u16 b0;\r
308                 u8 a0;\r
309                 long err, err_min=n;\r
310         \r
311         \r
312                 *a = a0 = ((n-1)/256) + 1;\r
313                 *b = b0 = n / *a;\r
314         \r
315                 for (; *a <= 256; (*a)++)\r
316                 {\r
317                         *b = n / *a;\r
318                         err = (long)*a * (long)*b - (long)n;\r
319                         if (abs(err) > (*a / 2))\r
320                         {\r
321                                 (*b)++;\r
322                                 err = (long)*a * (long)*b - (long)n;\r
323                         }\r
324                         if (abs(err) < abs(err_min))\r
325                         {\r
326                                 err_min = err;\r
327                                 a0 = *a;\r
328                                 b0 = *b;\r
329                                 if (err == 0) break;\r
330                         }\r
331                 }\r
332         \r
333                 *a = a0;\r
334                 *b = b0;\r
335         }\r
336         /*-----------------------------------------------------------*/\r
337 \r
338         static void prvSetupTimerInterrupt( void )\r
339         {\r
340                 unsigned portCHAR a;\r
341                 unsigned portSHORT b;\r
342                 unsigned portLONG n = configCPU_PERIPH_HZ / configTICK_RATE_HZ;\r
343                 \r
344                 TIM_InitTypeDef timer;\r
345                 \r
346                 SCU_APBPeriphClockConfig( __TIM23, ENABLE );\r
347                 TIM_DeInit(TIM2);\r
348                 TIM_StructInit(&timer);\r
349                 prvFindFactors( n, &a, &b );\r
350                 \r
351                 timer.TIM_Mode           = TIM_OCM_CHANNEL_1;\r
352                 timer.TIM_OC1_Modes      = TIM_TIMING;\r
353                 timer.TIM_Clock_Source   = TIM_CLK_APB;\r
354                 timer.TIM_Clock_Edge     = TIM_CLK_EDGE_RISING;\r
355                 timer.TIM_Prescaler      = a-1;\r
356                 timer.TIM_Pulse_Level_1  = TIM_HIGH;\r
357                 timer.TIM_Pulse_Length_1 = s_nPulseLength  = b-1;\r
358                 \r
359                 TIM_Init (TIM2, &timer);\r
360                 TIM_ITConfig(TIM2, TIM_IT_OC1, ENABLE);\r
361                 /* Configure the VIC for the WDG interrupt. */\r
362                 VIC_Config( TIM2_ITLine, VIC_IRQ, 10 );\r
363                 VIC_ITCmd( TIM2_ITLine, ENABLE );\r
364                 \r
365                 /* Install the default handlers for both VIC's. */\r
366                 VIC0->DVAR = ( unsigned portLONG ) prvDefaultHandler;\r
367                 VIC1->DVAR = ( unsigned portLONG ) prvDefaultHandler;\r
368                 \r
369                 TIM_CounterCmd(TIM2, TIM_CLEAR);\r
370                 TIM_CounterCmd(TIM2, TIM_START);\r
371         }\r
372         /*-----------------------------------------------------------*/\r
373 \r
374         void TIM2_IRQHandler( void )\r
375         {\r
376                 /* Reset the timer counter to avioid overflow. */\r
377                 TIM2->OC1R += s_nPulseLength;\r
378                 \r
379                 /* Increment the tick counter. */\r
380                 vTaskIncrementTick();\r
381                 \r
382                 #if configUSE_PREEMPTION == 1\r
383                 {\r
384                         /* The new tick value might unblock a task.  Ensure the highest task that\r
385                         is ready to execute is the task that will execute when the tick ISR\r
386                         exits. */\r
387                         vTaskSwitchContext();\r
388                 }\r
389                 #endif\r
390                 \r
391                 /* Clear the interrupt in the watchdog. */\r
392                 TIM2->SR &= ~TIM_FLAG_OC1;\r
393         }\r
394 \r
395 #endif /* USE_WATCHDOG_TICK */\r
396 \r
397 /*-----------------------------------------------------------*/\r
398 \r
399 __arm __interwork void vPortEnterCritical( void )\r
400 {\r
401         /* Disable interrupts first! */\r
402         portDISABLE_INTERRUPTS();\r
403 \r
404         /* Now interrupts are disabled ulCriticalNesting can be accessed\r
405         directly.  Increment ulCriticalNesting to keep a count of how many times\r
406         portENTER_CRITICAL() has been called. */\r
407         ulCriticalNesting++;\r
408 }\r
409 /*-----------------------------------------------------------*/\r
410 \r
411 __arm __interwork void vPortExitCritical( void )\r
412 {\r
413         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
414         {\r
415                 /* Decrement the nesting count as we are leaving a critical section. */\r
416                 ulCriticalNesting--;\r
417 \r
418                 /* If the nesting level has reached zero then interrupts should be\r
419                 re-enabled. */\r
420                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
421                 {\r
422                         portENABLE_INTERRUPTS();\r
423                 }\r
424         }\r
425 }\r
426 /*-----------------------------------------------------------*/\r
427 \r
428 static void prvDefaultHandler( void )\r
429 {\r
430 }\r
431 \r
432 \r
433 \r
434 \r
435 \r