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