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