2 FreeRTOS V4.1.1 - Copyright (C) 2003-2006 Richard Barry.
\r
3 MCF5235 Port - Copyright (C) 2006 Christian Walter.
\r
5 This file is part of the FreeRTOS distribution.
\r
7 FreeRTOS is free software; you can redistribute it and/or modify
\r
8 it under the terms of the GNU General Public License** as published by
\r
9 the Free Software Foundation; either version 2 of the License, or
\r
10 (at your option) any later version.
\r
12 FreeRTOS is distributed in the hope that it will be useful,
\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 GNU General Public License for more details.
\r
17 You should have received a copy of the GNU General Public License
\r
18 along with FreeRTOS; if not, write to the Free Software
\r
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
21 A special exception to the GPL can be applied should you wish to distribute
\r
22 a combined work that includes FreeRTOS, without being obliged to provide
\r
23 the source code for any proprietary components. See the licensing section
\r
24 of http://www.FreeRTOS.org for full details of how and when the exception
\r
27 ***************************************************************************
\r
28 ***************************************************************************
\r
30 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
32 * This is a concise, step by step, 'hands on' guide that describes both *
\r
33 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
34 * explains numerous examples that are written using the FreeRTOS API. *
\r
35 * Full source code for all the examples is provided in an accompanying *
\r
38 ***************************************************************************
\r
39 ***************************************************************************
\r
41 Please ensure to read the configuration and relevant port sections of the
\r
42 online documentation.
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
56 #include "FreeRTOS.h"
\r
57 #include "FreeRTOSConfig.h"
\r
60 /* ------------------------ Types ----------------------------------------- */
\r
61 typedef volatile unsigned long vuint32;
\r
62 typedef volatile unsigned short vuint16;
\r
63 typedef volatile unsigned char vuint8;
\r
65 /* ------------------------ Defines --------------------------------------- */
\r
66 #define portVECTOR_TABLE __RAMVEC
\r
67 #define portVECTOR_SYSCALL ( 32 + portTRAP_YIELD )
\r
68 #define portVECTOR_TIMER ( 64 + 36 )
\r
70 #define MCF_PIT_PRESCALER 512UL
\r
71 #define MCF_PIT_TIMER_TICKS ( FSYS_2 / MCF_PIT_PRESCALER )
\r
72 #define MCF_PIT_MODULUS_REGISTER(freq) ( MCF_PIT_TIMER_TICKS / ( freq ) - 1UL)
\r
74 #define MCF_PIT_PMR0 ( *( vuint16 * )( void * )( &__IPSBAR[ 0x150002 ] ) )
\r
75 #define MCF_PIT_PCSR0 ( *( vuint16 * )( void * )( &__IPSBAR[ 0x150000 ] ) )
\r
76 #define MCF_PIT_PCSR_PRE(x) ( ( ( x ) & 0x000F ) << 8 )
\r
77 #define MCF_PIT_PCSR_EN ( 0x0001 )
\r
78 #define MCF_PIT_PCSR_RLD ( 0x0002 )
\r
79 #define MCF_PIT_PCSR_PIF ( 0x0004 )
\r
80 #define MCF_PIT_PCSR_PIE ( 0x0008 )
\r
81 #define MCF_PIT_PCSR_OVW ( 0x0010 )
\r
82 #define MCF_INTC0_ICR36 ( *( vuint8 * )( void * )( &__IPSBAR[ 0x000C64 ] ) )
\r
83 #define MCF_INTC0_IMRH ( *( vuint32 * )( void * )( &__IPSBAR[ 0x000C08 ] ) )
\r
84 #define MCF_INTC0_IMRH_INT_MASK36 ( 0x00000010 )
\r
85 #define MCF_INTC0_IMRH_MASKALL ( 0x00000001 )
\r
86 #define MCF_INTC0_ICRn_IP(x) ( ( ( x ) & 0x07 ) << 0 )
\r
87 #define MCF_INTC0_ICRn_IL(x) ( ( ( x ) & 0x07 ) << 3 )
\r
89 #define portNO_CRITICAL_NESTING ( ( unsigned long ) 0 )
\r
90 #define portINITIAL_CRITICAL_NESTING ( ( unsigned long ) 10 )
\r
92 /* ------------------------ Static variables ------------------------------ */
\r
93 volatile unsigned long ulCriticalNesting = portINITIAL_CRITICAL_NESTING;
\r
95 /* ------------------------ Static functions ------------------------------ */
\r
96 #if configUSE_PREEMPTION == 0
\r
97 static void prvPortPreemptiveTick ( void ) __attribute__ ((interrupt_handler));
\r
99 static void prvPortPreemptiveTick ( void );
\r
102 /* ------------------------ Start implementation -------------------------- */
\r
105 pxPortInitialiseStack( portSTACK_TYPE * pxTopOfStack, pdTASK_CODE pxCode,
\r
106 void *pvParameters )
\r
108 /* Place the parameter on the stack in the expected location. */
\r
109 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;
\r
112 /* Place dummy return address on stack. Tasks should never terminate so
\r
113 * we can set this to anything. */
\r
114 *pxTopOfStack = ( portSTACK_TYPE ) 0;
\r
117 /* Create a Motorola Coldfire exception stack frame. First comes the return
\r
119 *pxTopOfStack = ( portSTACK_TYPE ) pxCode;
\r
122 /* Format, fault-status, vector number for exception stack frame. Task
\r
123 * run in supervisor mode. */
\r
124 *pxTopOfStack = 0x40002000UL | ( portVECTOR_SYSCALL + 32 ) << 18;
\r
127 /* Set the initial critical section nesting counter to zero. This value
\r
128 * is used to restore the value of ulCriticalNesting. */
\r
132 *pxTopOfStack = ( portSTACK_TYPE ) 0xA6; /* A6 / FP */
\r
134 *pxTopOfStack = ( portSTACK_TYPE ) 0xA5; /* A5 */
\r
136 *pxTopOfStack = ( portSTACK_TYPE ) 0xA4; /* A4 */
\r
138 *pxTopOfStack = ( portSTACK_TYPE ) 0xA3; /* A3 */
\r
140 *pxTopOfStack = ( portSTACK_TYPE ) 0xA2; /* A2 */
\r
142 *pxTopOfStack = ( portSTACK_TYPE ) 0xA1; /* A1 */
\r
144 *pxTopOfStack = ( portSTACK_TYPE ) 0xA0; /* A0 */
\r
146 *pxTopOfStack = ( portSTACK_TYPE ) 0xD7; /* D7 */
\r
148 *pxTopOfStack = ( portSTACK_TYPE ) 0xD6; /* D6 */
\r
150 *pxTopOfStack = ( portSTACK_TYPE ) 0xD5; /* D5 */
\r
152 *pxTopOfStack = ( portSTACK_TYPE ) 0xD4; /* D4 */
\r
154 *pxTopOfStack = ( portSTACK_TYPE ) 0xD3; /* D3 */
\r
156 *pxTopOfStack = ( portSTACK_TYPE ) 0xD2; /* D2 */
\r
158 *pxTopOfStack = ( portSTACK_TYPE ) 0xD1; /* D1 */
\r
160 *pxTopOfStack = ( portSTACK_TYPE ) 0xD0; /* D0 */
\r
162 return pxTopOfStack;
\r
166 * Called by portYIELD() or taskYIELD() to manually force a context switch.
\r
169 prvPortYield( void )
\r
171 asm volatile ( "move.w #0x2700, %sr\n\t" );
\r
172 #if _GCC_USES_FP == 1
\r
173 asm volatile ( "unlk %fp\n\t" );
\r
175 /* Perform the context switch. First save the context of the current task. */
\r
176 portSAVE_CONTEXT( );
\r
178 /* Find the highest priority task that is ready to run. */
\r
179 vTaskSwitchContext( );
\r
181 /* Restore the context of the new task. */
\r
182 portRESTORE_CONTEXT( );
\r
185 #if configUSE_PREEMPTION == 0
\r
187 * The ISR used for the scheduler tick depends on whether the cooperative or
\r
188 * the preemptive scheduler is being used.
\r
191 prvPortPreemptiveTick ( void )
\r
193 /* The cooperative scheduler requires a normal IRQ service routine to
\r
194 * simply increment the system tick.
\r
197 vTaskIncrementTick( );
\r
198 MCF_PIT_PCSR0 |= MCF_PIT_PCSR_PIF;
\r
204 prvPortPreemptiveTick( void )
\r
206 asm volatile ( "move.w #0x2700, %sr\n\t" );
\r
207 #if _GCC_USES_FP == 1
\r
208 asm volatile ( "unlk %fp\n\t" );
\r
210 portSAVE_CONTEXT( );
\r
211 MCF_PIT_PCSR0 |= MCF_PIT_PCSR_PIF;
\r
212 vTaskIncrementTick( );
\r
213 vTaskSwitchContext( );
\r
214 portRESTORE_CONTEXT( );
\r
219 vPortEnterCritical()
\r
221 /* FIXME: We should store the old IPL here - How are we supposed to do
\r
224 ( void )portSET_IPL( portIPL_MAX );
\r
226 /* Now interrupts are disabled ulCriticalNesting can be accessed
\r
227 * directly. Increment ulCriticalNesting to keep a count of how many times
\r
228 * portENTER_CRITICAL() has been called. */
\r
229 ulCriticalNesting++;
\r
233 vPortExitCritical()
\r
235 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
\r
237 /* Decrement the nesting count as we are leaving a critical section. */
\r
238 ulCriticalNesting--;
\r
240 /* If the nesting level has reached zero then interrupts should be
\r
242 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
\r
244 ( void )portSET_IPL( 0 );
\r
250 xPortStartScheduler( void )
\r
252 extern void ( *portVECTOR_TABLE[ ] ) ( );
\r
254 /* Add entry in vector table for yield system call. */
\r
255 portVECTOR_TABLE[ portVECTOR_SYSCALL ] = prvPortYield;
\r
256 /* Add entry in vector table for periodic timer. */
\r
257 portVECTOR_TABLE[ portVECTOR_TIMER ] = prvPortPreemptiveTick;
\r
259 /* Configure the timer for the system clock. */
\r
260 if ( configTICK_RATE_HZ > 0)
\r
262 /* Configure prescaler */
\r
263 MCF_PIT_PCSR0 = MCF_PIT_PCSR_PRE( 0x9 ) | MCF_PIT_PCSR_RLD | MCF_PIT_PCSR_OVW;
\r
264 /* Initialize the periodic timer interrupt. */
\r
265 MCF_PIT_PMR0 = MCF_PIT_MODULUS_REGISTER( configTICK_RATE_HZ );
\r
266 /* Configure interrupt priority and level and unmask interrupt. */
\r
267 MCF_INTC0_ICR36 = MCF_INTC0_ICRn_IL( 0x1 ) | MCF_INTC0_ICRn_IP( 0x1 );
\r
268 MCF_INTC0_IMRH &= ~( MCF_INTC0_IMRH_INT_MASK36 | MCF_INTC0_IMRH_MASKALL );
\r
269 /* Enable interrupts */
\r
270 MCF_PIT_PCSR0 |= MCF_PIT_PCSR_PIE | MCF_PIT_PCSR_EN | MCF_PIT_PCSR_PIF;
\r
273 /* Restore the context of the first task that is going to run. */
\r
274 portRESTORE_CONTEXT( );
\r
276 /* Should not get here. */
\r
281 vPortEndScheduler( void )
\r