2 FreeRTOS.org V4.3.0 - Copyright (C) 2003-2007 Richard Barry.
\r
4 This file is part of the FreeRTOS distribution.
\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
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
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
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
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
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
36 /*-----------------------------------------------------------
\r
37 * Implementation of functions defined in portable.h for the ST STR91x ARM7
\r
39 *----------------------------------------------------------*/
\r
41 /* Library includes. */
\r
42 #include "91x_lib.h"
\r
44 /* Standard includes. */
\r
47 /* Scheduler includes. */
\r
48 #include "FreeRTOS.h"
\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
55 #define portINITIAL_SPSR ( ( portSTACK_TYPE ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
\r
57 #define portINSTRUCTION_SIZE ( ( portSTACK_TYPE ) 4 )
\r
59 /* Constants required to handle critical sections. */
\r
60 #define portNO_CRITICAL_NESTING ( ( unsigned portLONG ) 0 )
\r
63 /*-----------------------------------------------------------*/
\r
65 /* Setup the watchdog to generate the tick interrupts. */
\r
66 static void prvSetupTimerInterrupt( void );
\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
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
79 /* VIC interrupt default handler. */
\r
80 static void prvDefaultHandler( void );
\r
82 /*-----------------------------------------------------------*/
\r
85 * Initialise the stack of a task to look exactly as if a call to
\r
86 * portSAVE_CONTEXT had been called.
\r
88 * See header file for description.
\r
90 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
92 portSTACK_TYPE *pxOriginalTOS;
\r
94 pxOriginalTOS = pxTopOfStack;
\r
96 /* Setup the initial stack of the task. The stack is set exactly as
\r
97 expected by the portRESTORE_CONTEXT() macro. */
\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
105 *pxTopOfStack = ( portSTACK_TYPE ) 0xaaaaaaaa; /* R14 */
\r
107 *pxTopOfStack = ( portSTACK_TYPE ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
\r
109 *pxTopOfStack = ( portSTACK_TYPE ) 0x12121212; /* R12 */
\r
111 *pxTopOfStack = ( portSTACK_TYPE ) 0x11111111; /* R11 */
\r
113 *pxTopOfStack = ( portSTACK_TYPE ) 0x10101010; /* R10 */
\r
115 *pxTopOfStack = ( portSTACK_TYPE ) 0x09090909; /* R9 */
\r
117 *pxTopOfStack = ( portSTACK_TYPE ) 0x08080808; /* R8 */
\r
119 *pxTopOfStack = ( portSTACK_TYPE ) 0x07070707; /* R7 */
\r
121 *pxTopOfStack = ( portSTACK_TYPE ) 0x06060606; /* R6 */
\r
123 *pxTopOfStack = ( portSTACK_TYPE ) 0x05050505; /* R5 */
\r
125 *pxTopOfStack = ( portSTACK_TYPE ) 0x04040404; /* R4 */
\r
127 *pxTopOfStack = ( portSTACK_TYPE ) 0x03030303; /* R3 */
\r
129 *pxTopOfStack = ( portSTACK_TYPE ) 0x02020202; /* R2 */
\r
131 *pxTopOfStack = ( portSTACK_TYPE ) 0x01010101; /* R1 */
\r
134 /* When the task starts is will expect to find the function parameter in
\r
136 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters; /* R0 */
\r
139 /* The status register is set for system mode, with interrupts enabled. */
\r
140 *pxTopOfStack = ( portSTACK_TYPE ) portINITIAL_SPSR;
\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
146 *pxTopOfStack = portNO_CRITICAL_NESTING;
\r
148 return pxTopOfStack;
\r
150 /*-----------------------------------------------------------*/
\r
152 portBASE_TYPE xPortStartScheduler( void )
\r
154 extern void vPortStartFirstTask( void );
\r
156 /* Start the timer that generates the tick ISR. Interrupts are disabled
\r
158 prvSetupTimerInterrupt();
\r
160 /* Start the first task. */
\r
161 vPortStartFirstTask();
\r
163 /* Should not get here! */
\r
166 /*-----------------------------------------------------------*/
\r
168 void vPortEndScheduler( void )
\r
170 /* It is unlikely that the ARM port will require this function as there
\r
171 is nothing to return to. */
\r
173 /*-----------------------------------------------------------*/
\r
175 /* This function is called from an asm wrapper, so does not require the __irq
\r
177 void WDG_IRQHandler( void )
\r
179 /* Increment the tick counter. */
\r
180 vTaskIncrementTick();
\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
186 vTaskSwitchContext();
\r
189 /* Clear the interrupt in the watchdog. */
\r
190 WDG->SR &= ~0x0001;
\r
192 /*-----------------------------------------------------------*/
\r
195 #define abs(x) ((x)>0 ? (x) : -(x))
\r
198 static void prvFindFactors(u32 n, u16 *a, u32 *b)
\r
200 /* This function is copied from the ST STR7 library and is
\r
201 copyright STMicroelectronics. Reproduced with permission. */
\r
205 long err, err_min=n;
\r
207 *a = a0 = ((n-1)/65536ul) + 1;
\r
210 for (; *a <= 256; (*a)++)
\r
213 err = (long)*a * (long)*b - (long)n;
\r
214 if (abs(err) > (*a / 2))
\r
217 err = (long)*a * (long)*b - (long)n;
\r
219 if (abs(err) < abs(err_min))
\r
224 if (err == 0) break;
\r
231 /*-----------------------------------------------------------*/
\r
233 static void prvSetupTimerInterrupt( void )
\r
235 WDG_InitTypeDef xWdg;
\r
236 unsigned portSHORT a;
\r
237 unsigned portLONG n = configCPU_PERIPH_HZ / configTICK_RATE_HZ, b;
\r
239 /* Configure the watchdog as a free running timer that generates a
\r
240 periodic interrupt. */
\r
242 SCU_APBPeriphClockConfig( __WDG, ENABLE );
\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
249 WDG_ITConfig(ENABLE);
\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
255 /* Install the default handlers for both VIC's. */
\r
256 VIC0->DVAR = ( unsigned portLONG ) prvDefaultHandler;
\r
257 VIC1->DVAR = ( unsigned portLONG ) prvDefaultHandler;
\r
262 /*-----------------------------------------------------------*/
\r
264 __arm __interwork void vPortEnterCritical( void )
\r
266 /* Disable interrupts first! */
\r
267 portDISABLE_INTERRUPTS();
\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
274 /*-----------------------------------------------------------*/
\r
276 __arm __interwork void vPortExitCritical( void )
\r
278 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
\r
280 /* Decrement the nesting count as we are leaving a critical section. */
\r
281 ulCriticalNesting--;
\r
283 /* If the nesting level has reached zero then interrupts should be
\r
285 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
\r
287 portENABLE_INTERRUPTS();
\r
291 /*-----------------------------------------------------------*/
\r
293 static void prvDefaultHandler( void )
\r