2 FreeRTOS V4.1.1 - Copyright (C) 2003-2006 Richard Barry.
3 MCF5235 Port - Copyright (C) 2006 Christian Walter.
5 This file is part of the FreeRTOS distribution.
7 FreeRTOS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 FreeRTOS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with FreeRTOS; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 A special exception to the GPL can be applied should you wish to distribute
22 a combined work that includes FreeRTOS, without being obliged to provide
23 the source code for any proprietary components. See the licensing section
24 of http://www.FreeRTOS.org for full details of how and when the exception
27 ***************************************************************************
28 See http://www.FreeRTOS.org for documentation, latest information, license
29 and contact details. Please ensure to read the configuration and relevant
30 port sections of the online documentation.
\r
32 Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along
\r
33 with commercial development and support options.
34 ***************************************************************************
40 #include "FreeRTOSConfig.h"
43 /* ------------------------ Types ----------------------------------------- */
44 typedef volatile unsigned long vuint32;
45 typedef volatile unsigned short vuint16;
46 typedef volatile unsigned char vuint8;
48 /* ------------------------ Defines --------------------------------------- */
49 #define portVECTOR_TABLE __RAMVEC
50 #define portVECTOR_SYSCALL ( 32 + portTRAP_YIELD )
51 #define portVECTOR_TIMER ( 64 + 36 )
53 #define MCF_PIT_PRESCALER 512UL
54 #define MCF_PIT_TIMER_TICKS ( FSYS_2 / MCF_PIT_PRESCALER )
55 #define MCF_PIT_MODULUS_REGISTER(freq) ( MCF_PIT_TIMER_TICKS / ( freq ) - 1UL)
57 #define MCF_PIT_PMR0 ( *( vuint16 * )( void * )( &__IPSBAR[ 0x150002 ] ) )
58 #define MCF_PIT_PCSR0 ( *( vuint16 * )( void * )( &__IPSBAR[ 0x150000 ] ) )
59 #define MCF_PIT_PCSR_PRE(x) ( ( ( x ) & 0x000F ) << 8 )
60 #define MCF_PIT_PCSR_EN ( 0x0001 )
61 #define MCF_PIT_PCSR_RLD ( 0x0002 )
62 #define MCF_PIT_PCSR_PIF ( 0x0004 )
63 #define MCF_PIT_PCSR_PIE ( 0x0008 )
64 #define MCF_PIT_PCSR_OVW ( 0x0010 )
65 #define MCF_INTC0_ICR36 ( *( vuint8 * )( void * )( &__IPSBAR[ 0x000C64 ] ) )
66 #define MCF_INTC0_IMRH ( *( vuint32 * )( void * )( &__IPSBAR[ 0x000C08 ] ) )
67 #define MCF_INTC0_IMRH_INT_MASK36 ( 0x00000010 )
68 #define MCF_INTC0_IMRH_MASKALL ( 0x00000001 )
69 #define MCF_INTC0_ICRn_IP(x) ( ( ( x ) & 0x07 ) << 0 )
70 #define MCF_INTC0_ICRn_IL(x) ( ( ( x ) & 0x07 ) << 3 )
72 #define portNO_CRITICAL_NESTING ( ( unsigned portLONG ) 0 )
73 #define portINITIAL_CRITICAL_NESTING ( ( unsigned portLONG ) 10 )
75 /* ------------------------ Static variables ------------------------------ */
76 volatile unsigned portLONG ulCriticalNesting = portINITIAL_CRITICAL_NESTING;
78 /* ------------------------ Static functions ------------------------------ */
79 #if configUSE_PREEMPTION == 0
80 static void prvPortPreemptiveTick ( void ) __attribute__ ((interrupt_handler));
82 static void prvPortPreemptiveTick ( void );
85 /* ------------------------ Start implementation -------------------------- */
88 pxPortInitialiseStack( portSTACK_TYPE * pxTopOfStack, pdTASK_CODE pxCode,
91 /* Place the parameter on the stack in the expected location. */
92 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;
95 /* Place dummy return address on stack. Tasks should never terminate so
96 * we can set this to anything. */
97 *pxTopOfStack = ( portSTACK_TYPE ) 0;
100 /* Create a Motorola Coldfire exception stack frame. First comes the return
102 *pxTopOfStack = ( portSTACK_TYPE ) pxCode;
105 /* Format, fault-status, vector number for exception stack frame. Task
106 * run in supervisor mode. */
107 *pxTopOfStack = 0x40002000UL | ( portVECTOR_SYSCALL + 32 ) << 18;
110 /* Set the initial critical section nesting counter to zero. This value
111 * is used to restore the value of ulCriticalNesting. */
115 *pxTopOfStack = ( portSTACK_TYPE ) 0xA6; /* A6 / FP */
117 *pxTopOfStack = ( portSTACK_TYPE ) 0xA5; /* A5 */
119 *pxTopOfStack = ( portSTACK_TYPE ) 0xA4; /* A4 */
121 *pxTopOfStack = ( portSTACK_TYPE ) 0xA3; /* A3 */
123 *pxTopOfStack = ( portSTACK_TYPE ) 0xA2; /* A2 */
125 *pxTopOfStack = ( portSTACK_TYPE ) 0xA1; /* A1 */
127 *pxTopOfStack = ( portSTACK_TYPE ) 0xA0; /* A0 */
129 *pxTopOfStack = ( portSTACK_TYPE ) 0xD7; /* D7 */
131 *pxTopOfStack = ( portSTACK_TYPE ) 0xD6; /* D6 */
133 *pxTopOfStack = ( portSTACK_TYPE ) 0xD5; /* D5 */
135 *pxTopOfStack = ( portSTACK_TYPE ) 0xD4; /* D4 */
137 *pxTopOfStack = ( portSTACK_TYPE ) 0xD3; /* D3 */
139 *pxTopOfStack = ( portSTACK_TYPE ) 0xD2; /* D2 */
141 *pxTopOfStack = ( portSTACK_TYPE ) 0xD1; /* D1 */
143 *pxTopOfStack = ( portSTACK_TYPE ) 0xD0; /* D0 */
149 * Called by portYIELD() or taskYIELD() to manually force a context switch.
154 asm volatile ( "move.w #0x2700, %sr\n\t" );
155 #if _GCC_USES_FP == 1
156 asm volatile ( "unlk %fp\n\t" );
158 /* Perform the context switch. First save the context of the current task. */
161 /* Find the highest priority task that is ready to run. */
162 vTaskSwitchContext( );
164 /* Restore the context of the new task. */
165 portRESTORE_CONTEXT( );
168 #if configUSE_PREEMPTION == 0
170 * The ISR used for the scheduler tick depends on whether the cooperative or
171 * the preemptive scheduler is being used.
174 prvPortPreemptiveTick ( void )
176 /* The cooperative scheduler requires a normal IRQ service routine to
177 * simply increment the system tick.
180 vTaskIncrementTick( );
181 MCF_PIT_PCSR0 |= MCF_PIT_PCSR_PIF;
187 prvPortPreemptiveTick( void )
189 asm volatile ( "move.w #0x2700, %sr\n\t" );
190 #if _GCC_USES_FP == 1
191 asm volatile ( "unlk %fp\n\t" );
194 MCF_PIT_PCSR0 |= MCF_PIT_PCSR_PIF;
195 vTaskIncrementTick( );
196 vTaskSwitchContext( );
197 portRESTORE_CONTEXT( );
204 /* FIXME: We should store the old IPL here - How are we supposed to do
207 ( void )portSET_IPL( portIPL_MAX );
209 /* Now interrupts are disabled ulCriticalNesting can be accessed
210 * directly. Increment ulCriticalNesting to keep a count of how many times
211 * portENTER_CRITICAL() has been called. */
218 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
220 /* Decrement the nesting count as we are leaving a critical section. */
223 /* If the nesting level has reached zero then interrupts should be
225 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
227 ( void )portSET_IPL( 0 );
233 xPortStartScheduler( void )
235 extern void ( *portVECTOR_TABLE[ ] ) ( );
237 /* Add entry in vector table for yield system call. */
238 portVECTOR_TABLE[ portVECTOR_SYSCALL ] = prvPortYield;
239 /* Add entry in vector table for periodic timer. */
240 portVECTOR_TABLE[ portVECTOR_TIMER ] = prvPortPreemptiveTick;
242 /* Configure the timer for the system clock. */
243 if ( configTICK_RATE_HZ > 0)
245 /* Configure prescaler */
246 MCF_PIT_PCSR0 = MCF_PIT_PCSR_PRE( 0x9 ) | MCF_PIT_PCSR_RLD | MCF_PIT_PCSR_OVW;
247 /* Initialize the periodic timer interrupt. */
248 MCF_PIT_PMR0 = MCF_PIT_MODULUS_REGISTER( configTICK_RATE_HZ );
249 /* Configure interrupt priority and level and unmask interrupt. */
250 MCF_INTC0_ICR36 = MCF_INTC0_ICRn_IL( 0x1 ) | MCF_INTC0_ICRn_IP( 0x1 );
251 MCF_INTC0_IMRH &= ~( MCF_INTC0_IMRH_INT_MASK36 | MCF_INTC0_IMRH_MASKALL );
252 /* Enable interrupts */
253 MCF_PIT_PCSR0 |= MCF_PIT_PCSR_PIE | MCF_PIT_PCSR_EN | MCF_PIT_PCSR_PIF;
256 /* Restore the context of the first task that is going to run. */
257 portRESTORE_CONTEXT( );
259 /* Should not get here. */
264 vPortEndScheduler( void )