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 See http://www.FreeRTOS.org for documentation, latest information, license
\r
29 and contact details. Please ensure to read the configuration and relevant
\r
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.
\r
34 ***************************************************************************
\r
39 #include "FreeRTOS.h"
\r
40 #include "FreeRTOSConfig.h"
\r
43 /* ------------------------ Types ----------------------------------------- */
\r
44 typedef volatile unsigned long vuint32;
\r
45 typedef volatile unsigned short vuint16;
\r
46 typedef volatile unsigned char vuint8;
\r
48 /* ------------------------ Defines --------------------------------------- */
\r
49 #define portVECTOR_TABLE __RAMVEC
\r
50 #define portVECTOR_SYSCALL ( 32 + portTRAP_YIELD )
\r
51 #define portVECTOR_TIMER ( 64 + 36 )
\r
53 #define MCF_PIT_PRESCALER 512UL
\r
54 #define MCF_PIT_TIMER_TICKS ( FSYS_2 / MCF_PIT_PRESCALER )
\r
55 #define MCF_PIT_MODULUS_REGISTER(freq) ( MCF_PIT_TIMER_TICKS / ( freq ) - 1UL)
\r
57 #define MCF_PIT_PMR0 ( *( vuint16 * )( void * )( &__IPSBAR[ 0x150002 ] ) )
\r
58 #define MCF_PIT_PCSR0 ( *( vuint16 * )( void * )( &__IPSBAR[ 0x150000 ] ) )
\r
59 #define MCF_PIT_PCSR_PRE(x) ( ( ( x ) & 0x000F ) << 8 )
\r
60 #define MCF_PIT_PCSR_EN ( 0x0001 )
\r
61 #define MCF_PIT_PCSR_RLD ( 0x0002 )
\r
62 #define MCF_PIT_PCSR_PIF ( 0x0004 )
\r
63 #define MCF_PIT_PCSR_PIE ( 0x0008 )
\r
64 #define MCF_PIT_PCSR_OVW ( 0x0010 )
\r
65 #define MCF_INTC0_ICR36 ( *( vuint8 * )( void * )( &__IPSBAR[ 0x000C64 ] ) )
\r
66 #define MCF_INTC0_IMRH ( *( vuint32 * )( void * )( &__IPSBAR[ 0x000C08 ] ) )
\r
67 #define MCF_INTC0_IMRH_INT_MASK36 ( 0x00000010 )
\r
68 #define MCF_INTC0_IMRH_MASKALL ( 0x00000001 )
\r
69 #define MCF_INTC0_ICRn_IP(x) ( ( ( x ) & 0x07 ) << 0 )
\r
70 #define MCF_INTC0_ICRn_IL(x) ( ( ( x ) & 0x07 ) << 3 )
\r
72 #define portNO_CRITICAL_NESTING ( ( unsigned portLONG ) 0 )
\r
73 #define portINITIAL_CRITICAL_NESTING ( ( unsigned portLONG ) 10 )
\r
75 /* ------------------------ Static variables ------------------------------ */
\r
76 volatile unsigned portLONG ulCriticalNesting = portINITIAL_CRITICAL_NESTING;
\r
78 /* ------------------------ Static functions ------------------------------ */
\r
79 #if configUSE_PREEMPTION == 0
\r
80 static void prvPortPreemptiveTick ( void ) __attribute__ ((interrupt_handler));
\r
82 static void prvPortPreemptiveTick ( void );
\r
85 /* ------------------------ Start implementation -------------------------- */
\r
88 pxPortInitialiseStack( portSTACK_TYPE * pxTopOfStack, pdTASK_CODE pxCode,
\r
89 void *pvParameters )
\r
91 /* Place the parameter on the stack in the expected location. */
\r
92 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;
\r
95 /* Place dummy return address on stack. Tasks should never terminate so
\r
96 * we can set this to anything. */
\r
97 *pxTopOfStack = ( portSTACK_TYPE ) 0;
\r
100 /* Create a Motorola Coldfire exception stack frame. First comes the return
\r
102 *pxTopOfStack = ( portSTACK_TYPE ) pxCode;
\r
105 /* Format, fault-status, vector number for exception stack frame. Task
\r
106 * run in supervisor mode. */
\r
107 *pxTopOfStack = 0x40002000UL | ( portVECTOR_SYSCALL + 32 ) << 18;
\r
110 /* Set the initial critical section nesting counter to zero. This value
\r
111 * is used to restore the value of ulCriticalNesting. */
\r
115 *pxTopOfStack = ( portSTACK_TYPE ) 0xA6; /* A6 / FP */
\r
117 *pxTopOfStack = ( portSTACK_TYPE ) 0xA5; /* A5 */
\r
119 *pxTopOfStack = ( portSTACK_TYPE ) 0xA4; /* A4 */
\r
121 *pxTopOfStack = ( portSTACK_TYPE ) 0xA3; /* A3 */
\r
123 *pxTopOfStack = ( portSTACK_TYPE ) 0xA2; /* A2 */
\r
125 *pxTopOfStack = ( portSTACK_TYPE ) 0xA1; /* A1 */
\r
127 *pxTopOfStack = ( portSTACK_TYPE ) 0xA0; /* A0 */
\r
129 *pxTopOfStack = ( portSTACK_TYPE ) 0xD7; /* D7 */
\r
131 *pxTopOfStack = ( portSTACK_TYPE ) 0xD6; /* D6 */
\r
133 *pxTopOfStack = ( portSTACK_TYPE ) 0xD5; /* D5 */
\r
135 *pxTopOfStack = ( portSTACK_TYPE ) 0xD4; /* D4 */
\r
137 *pxTopOfStack = ( portSTACK_TYPE ) 0xD3; /* D3 */
\r
139 *pxTopOfStack = ( portSTACK_TYPE ) 0xD2; /* D2 */
\r
141 *pxTopOfStack = ( portSTACK_TYPE ) 0xD1; /* D1 */
\r
143 *pxTopOfStack = ( portSTACK_TYPE ) 0xD0; /* D0 */
\r
145 return pxTopOfStack;
\r
149 * Called by portYIELD() or taskYIELD() to manually force a context switch.
\r
152 prvPortYield( void )
\r
154 asm volatile ( "move.w #0x2700, %sr\n\t" );
\r
155 #if _GCC_USES_FP == 1
\r
156 asm volatile ( "unlk %fp\n\t" );
\r
158 /* Perform the context switch. First save the context of the current task. */
\r
159 portSAVE_CONTEXT( );
\r
161 /* Find the highest priority task that is ready to run. */
\r
162 vTaskSwitchContext( );
\r
164 /* Restore the context of the new task. */
\r
165 portRESTORE_CONTEXT( );
\r
168 #if configUSE_PREEMPTION == 0
\r
170 * The ISR used for the scheduler tick depends on whether the cooperative or
\r
171 * the preemptive scheduler is being used.
\r
174 prvPortPreemptiveTick ( void )
\r
176 /* The cooperative scheduler requires a normal IRQ service routine to
\r
177 * simply increment the system tick.
\r
180 vTaskIncrementTick( );
\r
181 MCF_PIT_PCSR0 |= MCF_PIT_PCSR_PIF;
\r
187 prvPortPreemptiveTick( void )
\r
189 asm volatile ( "move.w #0x2700, %sr\n\t" );
\r
190 #if _GCC_USES_FP == 1
\r
191 asm volatile ( "unlk %fp\n\t" );
\r
193 portSAVE_CONTEXT( );
\r
194 MCF_PIT_PCSR0 |= MCF_PIT_PCSR_PIF;
\r
195 vTaskIncrementTick( );
\r
196 vTaskSwitchContext( );
\r
197 portRESTORE_CONTEXT( );
\r
202 vPortEnterCritical()
\r
204 /* FIXME: We should store the old IPL here - How are we supposed to do
\r
207 ( void )portSET_IPL( portIPL_MAX );
\r
209 /* Now interrupts are disabled ulCriticalNesting can be accessed
\r
210 * directly. Increment ulCriticalNesting to keep a count of how many times
\r
211 * portENTER_CRITICAL() has been called. */
\r
212 ulCriticalNesting++;
\r
216 vPortExitCritical()
\r
218 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
\r
220 /* Decrement the nesting count as we are leaving a critical section. */
\r
221 ulCriticalNesting--;
\r
223 /* If the nesting level has reached zero then interrupts should be
\r
225 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
\r
227 ( void )portSET_IPL( 0 );
\r
233 xPortStartScheduler( void )
\r
235 extern void ( *portVECTOR_TABLE[ ] ) ( );
\r
237 /* Add entry in vector table for yield system call. */
\r
238 portVECTOR_TABLE[ portVECTOR_SYSCALL ] = prvPortYield;
\r
239 /* Add entry in vector table for periodic timer. */
\r
240 portVECTOR_TABLE[ portVECTOR_TIMER ] = prvPortPreemptiveTick;
\r
242 /* Configure the timer for the system clock. */
\r
243 if ( configTICK_RATE_HZ > 0)
\r
245 /* Configure prescaler */
\r
246 MCF_PIT_PCSR0 = MCF_PIT_PCSR_PRE( 0x9 ) | MCF_PIT_PCSR_RLD | MCF_PIT_PCSR_OVW;
\r
247 /* Initialize the periodic timer interrupt. */
\r
248 MCF_PIT_PMR0 = MCF_PIT_MODULUS_REGISTER( configTICK_RATE_HZ );
\r
249 /* Configure interrupt priority and level and unmask interrupt. */
\r
250 MCF_INTC0_ICR36 = MCF_INTC0_ICRn_IL( 0x1 ) | MCF_INTC0_ICRn_IP( 0x1 );
\r
251 MCF_INTC0_IMRH &= ~( MCF_INTC0_IMRH_INT_MASK36 | MCF_INTC0_IMRH_MASKALL );
\r
252 /* Enable interrupts */
\r
253 MCF_PIT_PCSR0 |= MCF_PIT_PCSR_PIE | MCF_PIT_PCSR_EN | MCF_PIT_PCSR_PIF;
\r
256 /* Restore the context of the first task that is going to run. */
\r
257 portRESTORE_CONTEXT( );
\r
259 /* Should not get here. */
\r
264 vPortEndScheduler( void )
\r