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