]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/STR91x/port.c
5b460a527c496a8206cbbdba0b4233fe02c81077
[freertos] / FreeRTOS / Source / portable / IAR / STR91x / port.c
1 /*\r
2     FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd. \r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /*-----------------------------------------------------------\r
67  * Implementation of functions defined in portable.h for the ST STR91x ARM9\r
68  * port.\r
69  *----------------------------------------------------------*/\r
70 \r
71 /* Library includes. */\r
72 #include "91x_lib.h"\r
73 \r
74 /* Standard includes. */\r
75 #include <stdlib.h>\r
76 #include <assert.h>\r
77 \r
78 /* Scheduler includes. */\r
79 #include "FreeRTOS.h"\r
80 #include "task.h"\r
81 \r
82 #ifndef configUSE_WATCHDOG_TICK\r
83         #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
84 #endif\r
85 \r
86 /* Constants required to setup the initial stack. */\r
87 #ifndef _RUN_TASK_IN_ARM_MODE_\r
88         #define portINITIAL_SPSR                        ( ( StackType_t ) 0x3f ) /* System mode, THUMB mode, interrupts enabled. */\r
89 #else\r
90         #define portINITIAL_SPSR                        ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */\r
91 #endif\r
92 \r
93 #define portINSTRUCTION_SIZE                    ( ( StackType_t ) 4 )\r
94 \r
95 /* Constants required to handle critical sections. */\r
96 #define portNO_CRITICAL_NESTING                 ( ( uint32_t ) 0 )\r
97 \r
98 #ifndef abs\r
99         #define abs(x) ((x)>0 ? (x) : -(x))\r
100 #endif\r
101 \r
102 /**\r
103  * Toggle a led using the following algorithm:\r
104  * if ( GPIO_ReadBit(GPIO9, GPIO_Pin_2) )\r
105  * {\r
106  *   GPIO_WriteBit( GPIO9, GPIO_Pin_2, Bit_RESET );\r
107  * }\r
108  * else\r
109  * {\r
110  *   GPIO_WriteBit( GPIO9, GPIO_Pin_2, Bit_RESET );\r
111  * }\r
112  *\r
113  */\r
114 #define TOGGLE_LED(port,pin)                                                                    \\r
115         if ( ((((port)->DR[(pin)<<2])) & (pin)) != Bit_RESET )          \\r
116         {                                                                                                                       \\r
117         (port)->DR[(pin) <<2] = 0x00;                                                   \\r
118         }                                                                                                                       \\r
119         else                                                                                                            \\r
120         {                                                                                                                       \\r
121         (port)->DR[(pin) <<2] = (pin);                                                  \\r
122         }\r
123 \r
124 \r
125 /*-----------------------------------------------------------*/\r
126 \r
127 /* Setup the watchdog to generate the tick interrupts. */\r
128 static void prvSetupTimerInterrupt( void );\r
129 \r
130 /* ulCriticalNesting will get set to zero when the first task starts.  It\r
131 cannot be initialised to 0 as this will cause interrupts to be enabled\r
132 during the kernel initialisation process. */\r
133 uint32_t ulCriticalNesting = ( uint32_t ) 9999;\r
134 \r
135 /* Tick interrupt routines for cooperative and preemptive operation\r
136 respectively.  The preemptive version is not defined as __irq as it is called\r
137 from an asm wrapper function. */\r
138 void WDG_IRQHandler( void );\r
139 \r
140 /* VIC interrupt default handler. */\r
141 static void prvDefaultHandler( void );\r
142 \r
143 #if configUSE_WATCHDOG_TICK == 0\r
144         /* Used to update the OCR timer register */\r
145         static u16 s_nPulseLength;\r
146 #endif\r
147 \r
148 /*-----------------------------------------------------------*/\r
149 \r
150 /*\r
151  * Initialise the stack of a task to look exactly as if a call to\r
152  * portSAVE_CONTEXT had been called.\r
153  *\r
154  * See header file for description.\r
155  */\r
156 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
157 {\r
158         StackType_t *pxOriginalTOS;\r
159 \r
160         pxOriginalTOS = pxTopOfStack;\r
161 \r
162         /* To ensure asserts in tasks.c don't fail, although in this case the assert\r
163         is not really required. */\r
164         pxTopOfStack--;\r
165 \r
166         /* Setup the initial stack of the task.  The stack is set exactly as\r
167         expected by the portRESTORE_CONTEXT() macro. */\r
168 \r
169         /* First on the stack is the return address - which in this case is the\r
170         start of the task.  The offset is added to make the return address appear\r
171         as it would within an IRQ ISR. */\r
172         *pxTopOfStack = ( StackType_t ) pxCode + portINSTRUCTION_SIZE;          \r
173         pxTopOfStack--;\r
174 \r
175         *pxTopOfStack = ( StackType_t ) 0xaaaaaaaa;     /* R14 */\r
176         pxTopOfStack--; \r
177         *pxTopOfStack = ( StackType_t ) pxOriginalTOS; /* Stack used when task starts goes in R13. */\r
178         pxTopOfStack--;\r
179         *pxTopOfStack = ( StackType_t ) 0x12121212;     /* R12 */\r
180         pxTopOfStack--; \r
181         *pxTopOfStack = ( StackType_t ) 0x11111111;     /* R11 */\r
182         pxTopOfStack--; \r
183         *pxTopOfStack = ( StackType_t ) 0x10101010;     /* R10 */\r
184         pxTopOfStack--; \r
185         *pxTopOfStack = ( StackType_t ) 0x09090909;     /* R9 */\r
186         pxTopOfStack--; \r
187         *pxTopOfStack = ( StackType_t ) 0x08080808;     /* R8 */\r
188         pxTopOfStack--; \r
189         *pxTopOfStack = ( StackType_t ) 0x07070707;     /* R7 */\r
190         pxTopOfStack--; \r
191         *pxTopOfStack = ( StackType_t ) 0x06060606;     /* R6 */\r
192         pxTopOfStack--; \r
193         *pxTopOfStack = ( StackType_t ) 0x05050505;     /* R5 */\r
194         pxTopOfStack--; \r
195         *pxTopOfStack = ( StackType_t ) 0x04040404;     /* R4 */\r
196         pxTopOfStack--; \r
197         *pxTopOfStack = ( StackType_t ) 0x03030303;     /* R3 */\r
198         pxTopOfStack--; \r
199         *pxTopOfStack = ( StackType_t ) 0x02020202;     /* R2 */\r
200         pxTopOfStack--; \r
201         *pxTopOfStack = ( StackType_t ) 0x01010101;     /* R1 */\r
202         pxTopOfStack--; \r
203 \r
204         /* When the task starts is will expect to find the function parameter in\r
205         R0. */\r
206         *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */\r
207         pxTopOfStack--;\r
208 \r
209         /* The status register is set for system mode, with interrupts enabled. */\r
210         *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;\r
211         pxTopOfStack--;\r
212 \r
213         /* Interrupt flags cannot always be stored on the stack and will\r
214         instead be stored in a variable, which is then saved as part of the\r
215         tasks context. */\r
216         *pxTopOfStack = portNO_CRITICAL_NESTING;\r
217 \r
218         return pxTopOfStack;    \r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 BaseType_t xPortStartScheduler( void )\r
223 {\r
224 extern void vPortStartFirstTask( void );\r
225 \r
226         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
227         here already. */\r
228         prvSetupTimerInterrupt();\r
229 \r
230         /* Start the first task. */\r
231         vPortStartFirstTask();  \r
232 \r
233         /* Should not get here! */\r
234         return 0;\r
235 }\r
236 /*-----------------------------------------------------------*/\r
237 \r
238 void vPortEndScheduler( void )\r
239 {\r
240         /* It is unlikely that the ARM port will require this function as there\r
241         is nothing to return to.  */\r
242 }\r
243 /*-----------------------------------------------------------*/\r
244 \r
245 /* This function is called from an asm wrapper, so does not require the __irq\r
246 keyword. */\r
247 #if configUSE_WATCHDOG_TICK == 1\r
248 \r
249         static void prvFindFactors(u32 n, u16 *a, u32 *b)\r
250         {\r
251                 /* This function is copied from the ST STR7 library and is\r
252                 copyright STMicroelectronics.  Reproduced with permission. */\r
253         \r
254                 u32 b0;\r
255                 u16 a0;\r
256                 int32_t err, err_min=n;\r
257         \r
258                 *a = a0 = ((n-1)/65536ul) + 1;\r
259                 *b = b0 = n / *a;\r
260         \r
261                 for (; *a <= 256; (*a)++)\r
262                 {\r
263                         *b = n / *a;\r
264                         err = (int32_t)*a * (int32_t)*b - (int32_t)n;\r
265                         if (abs(err) > (*a / 2))\r
266                         {\r
267                                 (*b)++;\r
268                                 err = (int32_t)*a * (int32_t)*b - (int32_t)n;\r
269                         }\r
270                         if (abs(err) < abs(err_min))\r
271                         {\r
272                                 err_min = err;\r
273                                 a0 = *a;\r
274                                 b0 = *b;\r
275                                 if (err == 0) break;\r
276                         }\r
277                 }\r
278         \r
279                 *a = a0;\r
280                 *b = b0;\r
281         }\r
282         /*-----------------------------------------------------------*/\r
283 \r
284         static void prvSetupTimerInterrupt( void )\r
285         {\r
286         WDG_InitTypeDef xWdg;\r
287         uint16_t a;\r
288         uint32_t n = configCPU_PERIPH_HZ / configTICK_RATE_HZ, b;\r
289         \r
290                 /* Configure the watchdog as a free running timer that generates a\r
291                 periodic interrupt. */\r
292         \r
293                 SCU_APBPeriphClockConfig( __WDG, ENABLE );\r
294                 WDG_DeInit();\r
295                 WDG_StructInit(&xWdg);\r
296                 prvFindFactors( n, &a, &b );\r
297                 xWdg.WDG_Prescaler = a - 1;\r
298                 xWdg.WDG_Preload = b - 1;\r
299                 WDG_Init( &xWdg );\r
300                 WDG_ITConfig(ENABLE);\r
301                 \r
302                 /* Configure the VIC for the WDG interrupt. */\r
303                 VIC_Config( WDG_ITLine, VIC_IRQ, 10 );\r
304                 VIC_ITCmd( WDG_ITLine, ENABLE );\r
305                 \r
306                 /* Install the default handlers for both VIC's. */\r
307                 VIC0->DVAR = ( uint32_t ) prvDefaultHandler;\r
308                 VIC1->DVAR = ( uint32_t ) prvDefaultHandler;\r
309                 \r
310                 WDG_Cmd(ENABLE);\r
311         }\r
312         /*-----------------------------------------------------------*/\r
313 \r
314         void WDG_IRQHandler( void )\r
315         {\r
316                 {\r
317                         /* Increment the tick counter. */\r
318                         if( xTaskIncrementTick() != pdFALSE )\r
319                         {               \r
320                                 /* Select a new task to execute. */\r
321                                 vTaskSwitchContext();\r
322                         }\r
323                 \r
324                         /* Clear the interrupt in the watchdog. */\r
325                         WDG->SR &= ~0x0001;\r
326                 }\r
327         }\r
328 \r
329 #else\r
330 \r
331         static void prvFindFactors(u32 n, u8 *a, u16 *b)\r
332         {\r
333                 /* This function is copied from the ST STR7 library and is\r
334                 copyright STMicroelectronics.  Reproduced with permission. */\r
335         \r
336                 u16 b0;\r
337                 u8 a0;\r
338                 int32_t err, err_min=n;\r
339         \r
340         \r
341                 *a = a0 = ((n-1)/256) + 1;\r
342                 *b = b0 = n / *a;\r
343         \r
344                 for (; *a <= 256; (*a)++)\r
345                 {\r
346                         *b = n / *a;\r
347                         err = (int32_t)*a * (int32_t)*b - (int32_t)n;\r
348                         if (abs(err) > (*a / 2))\r
349                         {\r
350                                 (*b)++;\r
351                                 err = (int32_t)*a * (int32_t)*b - (int32_t)n;\r
352                         }\r
353                         if (abs(err) < abs(err_min))\r
354                         {\r
355                                 err_min = err;\r
356                                 a0 = *a;\r
357                                 b0 = *b;\r
358                                 if (err == 0) break;\r
359                         }\r
360                 }\r
361         \r
362                 *a = a0;\r
363                 *b = b0;\r
364         }\r
365         /*-----------------------------------------------------------*/\r
366 \r
367         static void prvSetupTimerInterrupt( void )\r
368         {\r
369                 uint8_t a;\r
370                 uint16_t b;\r
371                 uint32_t n = configCPU_PERIPH_HZ / configTICK_RATE_HZ;\r
372                 \r
373                 TIM_InitTypeDef timer;\r
374                 \r
375                 SCU_APBPeriphClockConfig( __TIM23, ENABLE );\r
376                 TIM_DeInit(TIM2);\r
377                 TIM_StructInit(&timer);\r
378                 prvFindFactors( n, &a, &b );\r
379                 \r
380                 timer.TIM_Mode           = TIM_OCM_CHANNEL_1;\r
381                 timer.TIM_OC1_Modes      = TIM_TIMING;\r
382                 timer.TIM_Clock_Source   = TIM_CLK_APB;\r
383                 timer.TIM_Clock_Edge     = TIM_CLK_EDGE_RISING;\r
384                 timer.TIM_Prescaler      = a-1;\r
385                 timer.TIM_Pulse_Level_1  = TIM_HIGH;\r
386                 timer.TIM_Pulse_Length_1 = s_nPulseLength  = b-1;\r
387                 \r
388                 TIM_Init (TIM2, &timer);\r
389                 TIM_ITConfig(TIM2, TIM_IT_OC1, ENABLE);\r
390                 /* Configure the VIC for the WDG interrupt. */\r
391                 VIC_Config( TIM2_ITLine, VIC_IRQ, 10 );\r
392                 VIC_ITCmd( TIM2_ITLine, ENABLE );\r
393                 \r
394                 /* Install the default handlers for both VIC's. */\r
395                 VIC0->DVAR = ( uint32_t ) prvDefaultHandler;\r
396                 VIC1->DVAR = ( uint32_t ) prvDefaultHandler;\r
397                 \r
398                 TIM_CounterCmd(TIM2, TIM_CLEAR);\r
399                 TIM_CounterCmd(TIM2, TIM_START);\r
400         }\r
401         /*-----------------------------------------------------------*/\r
402 \r
403         void TIM2_IRQHandler( void )\r
404         {\r
405                 /* Reset the timer counter to avioid overflow. */\r
406                 TIM2->OC1R += s_nPulseLength;\r
407                 \r
408                 /* Increment the tick counter. */\r
409                 if( xTaskIncrementTick() != pdFALSE )\r
410                 {\r
411                         /* Select a new task to run. */\r
412                         vTaskSwitchContext();\r
413                 }\r
414                 \r
415                 /* Clear the interrupt in the watchdog. */\r
416                 TIM2->SR &= ~TIM_FLAG_OC1;\r
417         }\r
418 \r
419 #endif /* USE_WATCHDOG_TICK */\r
420 \r
421 /*-----------------------------------------------------------*/\r
422 \r
423 __arm __interwork void vPortEnterCritical( void )\r
424 {\r
425         /* Disable interrupts first! */\r
426         portDISABLE_INTERRUPTS();\r
427 \r
428         /* Now interrupts are disabled ulCriticalNesting can be accessed\r
429         directly.  Increment ulCriticalNesting to keep a count of how many times\r
430         portENTER_CRITICAL() has been called. */\r
431         ulCriticalNesting++;\r
432 }\r
433 /*-----------------------------------------------------------*/\r
434 \r
435 __arm __interwork void vPortExitCritical( void )\r
436 {\r
437         if( ulCriticalNesting > portNO_CRITICAL_NESTING )\r
438         {\r
439                 /* Decrement the nesting count as we are leaving a critical section. */\r
440                 ulCriticalNesting--;\r
441 \r
442                 /* If the nesting level has reached zero then interrupts should be\r
443                 re-enabled. */\r
444                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
445                 {\r
446                         portENABLE_INTERRUPTS();\r
447                 }\r
448         }\r
449 }\r
450 /*-----------------------------------------------------------*/\r
451 \r
452 static void prvDefaultHandler( void )\r
453 {\r
454 }\r
455 \r
456 \r
457 \r
458 \r
459 \r