]> git.sur5r.net Git - freertos/blob - Source/portable/GCC/MCF5235/port.c
b9c843160a30366c259d07e68a1ae5cb031ad0ff
[freertos] / Source / portable / GCC / MCF5235 / port.c
1 /*
2     FreeRTOS V4.1.1 - Copyright (C) 2003-2006 Richard Barry.
3     MCF5235 Port - Copyright (C) 2006 Christian Walter.
4
5     This file is part of the FreeRTOS distribution.
6
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.
11
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.
16
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
20
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
25     can be applied.
26
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
31 \r
32         Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along\r
33         with commercial development and support options.
34     ***************************************************************************
35 */
36
37 #include <stdlib.h>
38
39 #include "FreeRTOS.h"
40 #include "FreeRTOSConfig.h"
41 #include "task.h"
42
43 /* ------------------------ Types ----------------------------------------- */
44 typedef volatile unsigned long vuint32;
45 typedef volatile unsigned short vuint16;
46 typedef volatile unsigned char vuint8;
47
48 /* ------------------------ Defines --------------------------------------- */
49 #define portVECTOR_TABLE                __RAMVEC
50 #define portVECTOR_SYSCALL              ( 32 + portTRAP_YIELD )
51 #define portVECTOR_TIMER                ( 64 + 36 )
52
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)
56
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 )
71
72 #define portNO_CRITICAL_NESTING         ( ( unsigned portLONG ) 0 )
73 #define portINITIAL_CRITICAL_NESTING    ( ( unsigned portLONG ) 10 )
74
75 /* ------------------------ Static variables ------------------------------ */
76 volatile unsigned portLONG              ulCriticalNesting = portINITIAL_CRITICAL_NESTING;
77
78 /* ------------------------ Static functions ------------------------------ */
79 #if configUSE_PREEMPTION == 0
80 static void prvPortPreemptiveTick ( void ) __attribute__ ((interrupt_handler));
81 #else
82 static void prvPortPreemptiveTick ( void );
83 #endif
84
85 /* ------------------------ Start implementation -------------------------- */
86
87 portSTACK_TYPE *
88 pxPortInitialiseStack( portSTACK_TYPE * pxTopOfStack, pdTASK_CODE pxCode,
89                        void *pvParameters )
90 {
91     /* Place the parameter on the stack in the expected location. */
92     *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;
93     pxTopOfStack--;
94
95     /* Place dummy return address on stack. Tasks should never terminate so
96      * we can set this to anything. */
97     *pxTopOfStack = ( portSTACK_TYPE ) 0;
98     pxTopOfStack--;
99
100     /* Create a Motorola Coldfire exception stack frame. First comes the return
101      * address. */
102     *pxTopOfStack = ( portSTACK_TYPE ) pxCode;
103     pxTopOfStack--;
104
105     /* Format, fault-status, vector number for exception stack frame. Task
106      * run in supervisor mode. */
107     *pxTopOfStack = 0x40002000UL | ( portVECTOR_SYSCALL + 32 ) << 18;
108     pxTopOfStack--;
109
110     /* Set the initial critical section nesting counter to zero. This value
111      * is used to restore the value of ulCriticalNesting. */
112     *pxTopOfStack = 0;
113     *pxTopOfStack--;
114
115     *pxTopOfStack = ( portSTACK_TYPE ) 0xA6;    /* A6 / FP */
116     pxTopOfStack--;
117     *pxTopOfStack = ( portSTACK_TYPE ) 0xA5;    /* A5 */
118     pxTopOfStack--;
119     *pxTopOfStack = ( portSTACK_TYPE ) 0xA4;    /* A4 */
120     pxTopOfStack--;
121     *pxTopOfStack = ( portSTACK_TYPE ) 0xA3;    /* A3 */
122     pxTopOfStack--;
123     *pxTopOfStack = ( portSTACK_TYPE ) 0xA2;    /* A2 */
124     pxTopOfStack--;
125     *pxTopOfStack = ( portSTACK_TYPE ) 0xA1;    /* A1 */
126     pxTopOfStack--;
127     *pxTopOfStack = ( portSTACK_TYPE ) 0xA0;    /* A0 */
128     pxTopOfStack--;
129     *pxTopOfStack = ( portSTACK_TYPE ) 0xD7;    /* D7 */
130     pxTopOfStack--;
131     *pxTopOfStack = ( portSTACK_TYPE ) 0xD6;    /* D6 */
132     pxTopOfStack--;
133     *pxTopOfStack = ( portSTACK_TYPE ) 0xD5;    /* D5 */
134     pxTopOfStack--;
135     *pxTopOfStack = ( portSTACK_TYPE ) 0xD4;    /* D4 */
136     pxTopOfStack--;
137     *pxTopOfStack = ( portSTACK_TYPE ) 0xD3;    /* D3 */
138     pxTopOfStack--;
139     *pxTopOfStack = ( portSTACK_TYPE ) 0xD2;    /* D2 */
140     pxTopOfStack--;
141     *pxTopOfStack = ( portSTACK_TYPE ) 0xD1;    /* D1 */
142     pxTopOfStack--;
143     *pxTopOfStack = ( portSTACK_TYPE ) 0xD0;    /* D0 */
144
145     return pxTopOfStack;
146 }
147
148 /*
149  * Called by portYIELD() or taskYIELD() to manually force a context switch.
150  */
151 static void
152 prvPortYield( void )
153 {
154     asm volatile ( "move.w  #0x2700, %sr\n\t" );
155 #if _GCC_USES_FP == 1
156     asm volatile ( "unlk %fp\n\t" );
157 #endif
158      /* Perform the context switch.  First save the context of the current task. */
159     portSAVE_CONTEXT(  );
160
161     /* Find the highest priority task that is ready to run. */
162     vTaskSwitchContext(  );
163
164     /* Restore the context of the new task. */
165     portRESTORE_CONTEXT(  );
166 }
167
168 #if configUSE_PREEMPTION == 0
169 /*
170  * The ISR used for the scheduler tick depends on whether the cooperative or
171  * the preemptive scheduler is being used.
172  */
173 static void
174 prvPortPreemptiveTick ( void )
175 {
176     /* The cooperative scheduler requires a normal IRQ service routine to
177      * simply increment the system tick.
178      */
179
180     vTaskIncrementTick(  );
181     MCF_PIT_PCSR0 |= MCF_PIT_PCSR_PIF;
182 }
183
184 #else
185
186 static void
187 prvPortPreemptiveTick( void )
188 {
189     asm volatile ( "move.w  #0x2700, %sr\n\t" );
190 #if _GCC_USES_FP == 1
191     asm volatile ( "unlk %fp\n\t" );
192 #endif
193     portSAVE_CONTEXT(  );
194     MCF_PIT_PCSR0 |= MCF_PIT_PCSR_PIF;
195     vTaskIncrementTick(  );
196     vTaskSwitchContext(  );
197     portRESTORE_CONTEXT(  );
198 }
199 #endif
200
201 void
202 vPortEnterCritical()
203 {
204     /* FIXME: We should store the old IPL here - How are we supposed to do
205      * this.
206      */
207     ( void )portSET_IPL( portIPL_MAX );
208
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. */
212     ulCriticalNesting++;
213 }
214
215 void
216 vPortExitCritical()
217 {
218     if( ulCriticalNesting > portNO_CRITICAL_NESTING )
219     {
220         /* Decrement the nesting count as we are leaving a critical section. */
221         ulCriticalNesting--;
222
223         /* If the nesting level has reached zero then interrupts should be
224         re-enabled. */
225         if( ulCriticalNesting == portNO_CRITICAL_NESTING )
226         {
227             ( void )portSET_IPL( 0 );
228         }
229     }
230 }
231
232 portBASE_TYPE
233 xPortStartScheduler( void )
234 {
235     extern void     ( *portVECTOR_TABLE[  ] ) (  );
236
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;
241
242     /* Configure the timer for the system clock. */
243     if ( configTICK_RATE_HZ > 0)
244     {
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;
254     }
255
256     /* Restore the context of the first task that is going to run. */
257     portRESTORE_CONTEXT(  );
258
259     /* Should not get here. */
260     return pdTRUE;
261 }
262
263 void
264 vPortEndScheduler( void )
265 {
266 }