]> git.sur5r.net Git - freertos/blob - Source/portable/GCC/PPC405_Xilinx/port.c
Update Cortex M3 ports to ensure 8 byte alignment.
[freertos] / Source / portable / GCC / PPC405_Xilinx / port.c
1 /*\r
2     FreeRTOS V6.0.1 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public \r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it \r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained \r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*-----------------------------------------------------------\r
55  * Implementation of functions defined in portable.h for the PPC405 port.\r
56  *----------------------------------------------------------*/\r
57 \r
58 \r
59 /* Scheduler includes. */\r
60 #include "FreeRTOS.h"\r
61 #include "task.h"\r
62 \r
63 /* Library includes. */\r
64 #include "xtime_l.h"\r
65 #include "xintc.h"\r
66 #include "xintc_i.h"\r
67 \r
68 /*-----------------------------------------------------------*/\r
69 \r
70 /* Definitions to set the initial MSR of each task. */\r
71 #define portCRITICAL_INTERRUPT_ENABLE   ( 1UL << 17UL )\r
72 #define portEXTERNAL_INTERRUPT_ENABLE   ( 1UL << 15UL )\r
73 #define portMACHINE_CHECK_ENABLE                ( 1UL << 12UL )\r
74 \r
75 #if configUSE_FPU == 1\r
76         #define portAPU_PRESENT                         ( 1UL << 25UL )\r
77         #define portFCM_FPU_PRESENT                     ( 1UL << 13UL )\r
78 #else\r
79         #define portAPU_PRESENT                         ( 0UL )\r
80         #define portFCM_FPU_PRESENT                     ( 0UL )\r
81 #endif\r
82 \r
83 #define portINITIAL_MSR         ( portCRITICAL_INTERRUPT_ENABLE | portEXTERNAL_INTERRUPT_ENABLE | portMACHINE_CHECK_ENABLE | portAPU_PRESENT | portFCM_FPU_PRESENT )\r
84 \r
85 \r
86 extern const unsigned _SDA_BASE_;\r
87 extern const unsigned _SDA2_BASE_;\r
88 \r
89 /*-----------------------------------------------------------*/\r
90 \r
91 /*\r
92  * Setup the system timer to generate the tick interrupt.\r
93  */\r
94 static void prvSetupTimerInterrupt( void );\r
95 \r
96 /*\r
97  * The handler for the tick interrupt - defined in portasm.s.\r
98  */\r
99 extern void vPortTickISR( void );\r
100 \r
101 /*\r
102  * The handler for the yield function - defined in portasm.s.\r
103  */\r
104 extern void vPortYield( void );\r
105 \r
106 /*\r
107  * Function to start the scheduler running by starting the highest\r
108  * priority task that has thus far been created.\r
109  */\r
110 extern void vPortStartFirstTask( void );\r
111 \r
112 /*-----------------------------------------------------------*/\r
113 \r
114 /* Structure used to hold the state of the interrupt controller. */\r
115 static XIntc xInterruptController;\r
116 \r
117 /*-----------------------------------------------------------*/\r
118 \r
119 /* \r
120  * Initialise the stack of a task to look exactly as if the task had been\r
121  * interrupted.\r
122  * \r
123  * See the header file portable.h.\r
124  */\r
125 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
126 {\r
127         /* Place a known value at the bottom of the stack for debugging. */\r
128         *pxTopOfStack = 0xDEADBEEF;\r
129         pxTopOfStack--;\r
130 \r
131         /* EABI stack frame. */\r
132         pxTopOfStack -= 20;     /* Previous backchain and LR, R31 to R4 inclusive. */\r
133 \r
134         /* Parameters in R13. */\r
135         *pxTopOfStack = ( portSTACK_TYPE ) &_SDA_BASE_; /* address of the first small data area */\r
136         pxTopOfStack -= 10;\r
137 \r
138         /* Parameters in R3. */\r
139         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;\r
140         pxTopOfStack--;\r
141 \r
142         /* Parameters in R2. */\r
143         *pxTopOfStack = ( portSTACK_TYPE ) &_SDA2_BASE_;        /* address of the second small data area */\r
144         pxTopOfStack--;\r
145 \r
146         /* R1 is the stack pointer so is omitted. */\r
147 \r
148         *pxTopOfStack = 0x10000001UL;;  /* R0. */\r
149         pxTopOfStack--;\r
150         *pxTopOfStack = 0x00000000UL;   /* USPRG0. */\r
151         pxTopOfStack--;\r
152         *pxTopOfStack = 0x00000000UL;   /* CR. */\r
153         pxTopOfStack--;\r
154         *pxTopOfStack = 0x00000000UL;   /* XER. */\r
155         pxTopOfStack--;\r
156         *pxTopOfStack = 0x00000000UL;   /* CTR. */\r
157         pxTopOfStack--;\r
158         *pxTopOfStack = ( portSTACK_TYPE ) vPortEndScheduler;   /* LR. */\r
159         pxTopOfStack--;\r
160         *pxTopOfStack = ( portSTACK_TYPE ) pxCode; /* SRR0. */\r
161         pxTopOfStack--;\r
162         *pxTopOfStack = portINITIAL_MSR;/* SRR1. */\r
163         pxTopOfStack--;\r
164         *pxTopOfStack = ( portSTACK_TYPE ) vPortEndScheduler;/* Next LR. */\r
165         pxTopOfStack--;\r
166         *pxTopOfStack = 0x00000000UL;/* Backchain. */\r
167 \r
168         return pxTopOfStack;\r
169 }\r
170 /*-----------------------------------------------------------*/\r
171 \r
172 portBASE_TYPE xPortStartScheduler( void )\r
173 {\r
174         prvSetupTimerInterrupt();\r
175         XExc_RegisterHandler( XEXC_ID_SYSTEM_CALL, ( XExceptionHandler ) vPortYield, ( void * ) 0 );\r
176         vPortStartFirstTask();\r
177 \r
178         /* Should not get here as the tasks are now running! */\r
179         return pdFALSE;\r
180 }\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 void vPortEndScheduler( void )\r
184 {\r
185         /* Not implemented. */\r
186         for( ;; );\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 /*\r
191  * Hardware initialisation to generate the RTOS tick.   \r
192  */\r
193 static void prvSetupTimerInterrupt( void )\r
194 {\r
195 const unsigned long ulInterval = ( ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL );\r
196 \r
197         XTime_PITClearInterrupt();\r
198         XTime_FITClearInterrupt();\r
199         XTime_WDTClearInterrupt();\r
200         XTime_WDTDisableInterrupt();\r
201         XTime_FITDisableInterrupt();\r
202 \r
203         XExc_RegisterHandler( XEXC_ID_PIT_INT, ( XExceptionHandler ) vPortTickISR, ( void * ) 0 );\r
204 \r
205         XTime_PITEnableAutoReload();\r
206         XTime_PITSetInterval( ulInterval );\r
207         XTime_PITEnableInterrupt();\r
208 }\r
209 /*-----------------------------------------------------------*/\r
210 \r
211 void vPortISRHandler( void *pvNullDoNotUse )\r
212 {\r
213 unsigned long ulInterruptStatus, ulInterruptMask = 1UL;\r
214 portBASE_TYPE xInterruptNumber;\r
215 XIntc_Config *pxInterruptController;\r
216 XIntc_VectorTableEntry *pxTable;\r
217 \r
218         /* Just to remove compiler warning. */\r
219         ( void ) pvNullDoNotUse;        \r
220 \r
221         /* Get the configuration by using the device ID - in this case it is\r
222         assumed that only one interrupt controller is being used. */\r
223         pxInterruptController = &XIntc_ConfigTable[ XPAR_XPS_INTC_0_DEVICE_ID ];\r
224   \r
225         /* Which interrupts are pending? */\r
226         ulInterruptStatus = XIntc_mGetIntrStatus( pxInterruptController->BaseAddress );\r
227   \r
228         for( xInterruptNumber = 0; xInterruptNumber < XPAR_INTC_MAX_NUM_INTR_INPUTS; xInterruptNumber++ )\r
229         {\r
230                 if( ulInterruptStatus & 0x01UL )\r
231                 {\r
232                         /* Clear the pending interrupt. */\r
233                         XIntc_mAckIntr( pxInterruptController->BaseAddress, ulInterruptMask );\r
234 \r
235                         /* Call the registered handler. */\r
236                         pxTable = &( pxInterruptController->HandlerTable[ xInterruptNumber ] );\r
237                         pxTable->Handler( pxTable->CallBackRef );\r
238                 }\r
239         \r
240                 /* Check the next interrupt. */\r
241                 ulInterruptMask <<= 0x01UL;\r
242                 ulInterruptStatus >>= 0x01UL;\r
243 \r
244                 /* Have we serviced all interrupts? */\r
245                 if( ulInterruptStatus == 0UL )\r
246                 {\r
247                         break;\r
248                 }\r
249         }\r
250 }\r
251 /*-----------------------------------------------------------*/\r
252 \r
253 void vPortSetupInterruptController( void )\r
254 {\r
255 extern void vPortISRWrapper( void );\r
256 \r
257         /* Perform all library calls necessary to initialise the exception table\r
258         and interrupt controller.  This assumes only one interrupt controller is in\r
259         use. */\r
260         XExc_mDisableExceptions( XEXC_NON_CRITICAL );\r
261         XExc_Init();\r
262 \r
263         /* The library functions save the context - we then jump to a wrapper to\r
264         save the stack into the TCB.  The wrapper then calls the handler defined\r
265         above. */\r
266         XExc_RegisterHandler( XEXC_ID_NON_CRITICAL_INT, ( XExceptionHandler ) vPortISRWrapper, NULL );\r
267         XIntc_Initialize( &xInterruptController, XPAR_XPS_INTC_0_DEVICE_ID );\r
268         XIntc_Start( &xInterruptController, XIN_REAL_MODE );\r
269 }\r
270 /*-----------------------------------------------------------*/\r
271 \r
272 portBASE_TYPE xPortInstallInterruptHandler( unsigned char ucInterruptID, XInterruptHandler pxHandler, void *pvCallBackRef )\r
273 {\r
274 portBASE_TYPE xReturn = pdFAIL;\r
275 \r
276         /* This function is defined here so the scope of xInterruptController can\r
277         remain within this file. */\r
278 \r
279         if( XST_SUCCESS == XIntc_Connect( &xInterruptController, ucInterruptID, pxHandler, pvCallBackRef ) )\r
280         {\r
281                 XIntc_Enable( &xInterruptController, ucInterruptID );\r
282                 xReturn = pdPASS;\r
283         }\r
284 \r
285         return xReturn;         \r
286 }\r