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