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.
31 ***************************************************************************
37 #include "FreeRTOSConfig.h"
40 /* ------------------------ Types ----------------------------------------- */
41 typedef volatile unsigned long vuint32;
42 typedef volatile unsigned short vuint16;
43 typedef volatile unsigned char vuint8;
45 /* ------------------------ Defines --------------------------------------- */
46 #define portVECTOR_TABLE __RAMVEC
47 #define portVECTOR_SYSCALL ( 32 + portTRAP_YIELD )
48 #define portVECTOR_TIMER ( 64 + 36 )
50 #define MCF_PIT_PRESCALER 512UL
51 #define MCF_PIT_TIMER_TICKS ( FSYS_2 / MCF_PIT_PRESCALER )
52 #define MCF_PIT_MODULUS_REGISTER(freq) ( MCF_PIT_TIMER_TICKS / ( freq ) - 1UL)
54 #define MCF_PIT_PMR0 ( *( vuint16 * )( void * )( &__IPSBAR[ 0x150002 ] ) )
55 #define MCF_PIT_PCSR0 ( *( vuint16 * )( void * )( &__IPSBAR[ 0x150000 ] ) )
56 #define MCF_PIT_PCSR_PRE(x) ( ( ( x ) & 0x000F ) << 8 )
57 #define MCF_PIT_PCSR_EN ( 0x0001 )
58 #define MCF_PIT_PCSR_RLD ( 0x0002 )
59 #define MCF_PIT_PCSR_PIF ( 0x0004 )
60 #define MCF_PIT_PCSR_PIE ( 0x0008 )
61 #define MCF_PIT_PCSR_OVW ( 0x0010 )
62 #define MCF_INTC0_ICR36 ( *( vuint8 * )( void * )( &__IPSBAR[ 0x000C64 ] ) )
63 #define MCF_INTC0_IMRH ( *( vuint32 * )( void * )( &__IPSBAR[ 0x000C08 ] ) )
64 #define MCF_INTC0_IMRH_INT_MASK36 ( 0x00000010 )
65 #define MCF_INTC0_IMRH_MASKALL ( 0x00000001 )
66 #define MCF_INTC0_ICRn_IP(x) ( ( ( x ) & 0x07 ) << 0 )
67 #define MCF_INTC0_ICRn_IL(x) ( ( ( x ) & 0x07 ) << 3 )
69 #define portNO_CRITICAL_NESTING ( ( unsigned portLONG ) 0 )
70 #define portINITIAL_CRITICAL_NESTING ( ( unsigned portLONG ) 10 )
72 /* ------------------------ Static variables ------------------------------ */
73 volatile unsigned portLONG ulCriticalNesting = portINITIAL_CRITICAL_NESTING;
75 /* ------------------------ Static functions ------------------------------ */
76 #if configUSE_PREEMPTION == 0
77 static void prvPortPreemptiveTick ( void ) __attribute__ ((interrupt_handler));
79 static void prvPortPreemptiveTick ( void );
82 /* ------------------------ Start implementation -------------------------- */
85 pxPortInitialiseStack( portSTACK_TYPE * pxTopOfStack, pdTASK_CODE pxCode,
88 /* Place the parameter on the stack in the expected location. */
89 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;
92 /* Place dummy return address on stack. Tasks should never terminate so
93 * we can set this to anything. */
94 *pxTopOfStack = ( portSTACK_TYPE ) 0;
97 /* Create a Motorola Coldfire exception stack frame. First comes the return
99 *pxTopOfStack = ( portSTACK_TYPE ) pxCode;
102 /* Format, fault-status, vector number for exception stack frame. Task
103 * run in supervisor mode. */
104 *pxTopOfStack = 0x40002000UL | ( portVECTOR_SYSCALL + 32 ) << 18;
107 /* Set the initial critical section nesting counter to zero. This value
108 * is used to restore the value of ulCriticalNesting. */
112 *pxTopOfStack = ( portSTACK_TYPE ) 0xA6; /* A6 / FP */
114 *pxTopOfStack = ( portSTACK_TYPE ) 0xA5; /* A5 */
116 *pxTopOfStack = ( portSTACK_TYPE ) 0xA4; /* A4 */
118 *pxTopOfStack = ( portSTACK_TYPE ) 0xA3; /* A3 */
120 *pxTopOfStack = ( portSTACK_TYPE ) 0xA2; /* A2 */
122 *pxTopOfStack = ( portSTACK_TYPE ) 0xA1; /* A1 */
124 *pxTopOfStack = ( portSTACK_TYPE ) 0xA0; /* A0 */
126 *pxTopOfStack = ( portSTACK_TYPE ) 0xD7; /* D7 */
128 *pxTopOfStack = ( portSTACK_TYPE ) 0xD6; /* D6 */
130 *pxTopOfStack = ( portSTACK_TYPE ) 0xD5; /* D5 */
132 *pxTopOfStack = ( portSTACK_TYPE ) 0xD4; /* D4 */
134 *pxTopOfStack = ( portSTACK_TYPE ) 0xD3; /* D3 */
136 *pxTopOfStack = ( portSTACK_TYPE ) 0xD2; /* D2 */
138 *pxTopOfStack = ( portSTACK_TYPE ) 0xD1; /* D1 */
140 *pxTopOfStack = ( portSTACK_TYPE ) 0xD0; /* D0 */
146 * Called by portYIELD() or taskYIELD() to manually force a context switch.
151 asm volatile ( "move.w #0x2700, %sr\n\t" );
152 #if _GCC_USES_FP == 1
153 asm volatile ( "unlk %fp\n\t" );
155 /* Perform the context switch. First save the context of the current task. */
158 /* Find the highest priority task that is ready to run. */
159 vTaskSwitchContext( );
161 /* Restore the context of the new task. */
162 portRESTORE_CONTEXT( );
165 #if configUSE_PREEMPTION == 0
167 * The ISR used for the scheduler tick depends on whether the cooperative or
168 * the preemptive scheduler is being used.
171 prvPortPreemptiveTick ( void )
173 /* The cooperative scheduler requires a normal IRQ service routine to
174 * simply increment the system tick.
177 vTaskIncrementTick( );
178 MCF_PIT_PCSR0 |= MCF_PIT_PCSR_PIF;
184 prvPortPreemptiveTick( void )
186 asm volatile ( "move.w #0x2700, %sr\n\t" );
187 #if _GCC_USES_FP == 1
188 asm volatile ( "unlk %fp\n\t" );
191 MCF_PIT_PCSR0 |= MCF_PIT_PCSR_PIF;
192 vTaskIncrementTick( );
193 vTaskSwitchContext( );
194 portRESTORE_CONTEXT( );
201 /* FIXME: We should store the old IPL here - How are we supposed to do
204 ( void )portSET_IPL( portIPL_MAX );
206 /* Now interrupts are disabled ulCriticalNesting can be accessed
207 * directly. Increment ulCriticalNesting to keep a count of how many times
208 * portENTER_CRITICAL() has been called. */
215 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
217 /* Decrement the nesting count as we are leaving a critical section. */
220 /* If the nesting level has reached zero then interrupts should be
222 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
224 ( void )portSET_IPL( 0 );
230 xPortStartScheduler( void )
232 extern void ( *portVECTOR_TABLE[ ] ) ( );
234 /* Add entry in vector table for yield system call. */
235 portVECTOR_TABLE[ portVECTOR_SYSCALL ] = prvPortYield;
236 /* Add entry in vector table for periodic timer. */
237 portVECTOR_TABLE[ portVECTOR_TIMER ] = prvPortPreemptiveTick;
239 /* Configure the timer for the system clock. */
240 if ( configTICK_RATE_HZ > 0)
242 /* Configure prescaler */
243 MCF_PIT_PCSR0 = MCF_PIT_PCSR_PRE( 0x9 ) | MCF_PIT_PCSR_RLD | MCF_PIT_PCSR_OVW;
244 /* Initialize the periodic timer interrupt. */
245 MCF_PIT_PMR0 = MCF_PIT_MODULUS_REGISTER( configTICK_RATE_HZ );
246 /* Configure interrupt priority and level and unmask interrupt. */
247 MCF_INTC0_ICR36 = MCF_INTC0_ICRn_IL( 0x1 ) | MCF_INTC0_ICRn_IP( 0x1 );
248 MCF_INTC0_IMRH &= ~( MCF_INTC0_IMRH_INT_MASK36 | MCF_INTC0_IMRH_MASKALL );
249 /* Enable interrupts */
250 MCF_PIT_PCSR0 |= MCF_PIT_PCSR_PIE | MCF_PIT_PCSR_EN | MCF_PIT_PCSR_PIF;
253 /* Restore the context of the first task that is going to run. */
254 portRESTORE_CONTEXT( );
256 /* Should not get here. */
261 vPortEndScheduler( void )