]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/PPC405_Xilinx/port.c
Prepare for V7.2.0 release.
[freertos] / FreeRTOS / Source / portable / GCC / PPC405_Xilinx / port.c
1 /*\r
2     FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\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 modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or 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     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /*-----------------------------------------------------------\r
68  * Implementation of functions defined in portable.h for the PPC405 port.\r
69  *----------------------------------------------------------*/\r
70 \r
71 \r
72 /* Scheduler includes. */\r
73 #include "FreeRTOS.h"\r
74 #include "task.h"\r
75 \r
76 /* Library includes. */\r
77 #include "xtime_l.h"\r
78 #include "xintc.h"\r
79 #include "xintc_i.h"\r
80 \r
81 /*-----------------------------------------------------------*/\r
82 \r
83 /* Definitions to set the initial MSR of each task. */\r
84 #define portCRITICAL_INTERRUPT_ENABLE   ( 1UL << 17UL )\r
85 #define portEXTERNAL_INTERRUPT_ENABLE   ( 1UL << 15UL )\r
86 #define portMACHINE_CHECK_ENABLE                ( 1UL << 12UL )\r
87 \r
88 #if configUSE_FPU == 1\r
89         #define portAPU_PRESENT                         ( 1UL << 25UL )\r
90         #define portFCM_FPU_PRESENT                     ( 1UL << 13UL )\r
91 #else\r
92         #define portAPU_PRESENT                         ( 0UL )\r
93         #define portFCM_FPU_PRESENT                     ( 0UL )\r
94 #endif\r
95 \r
96 #define portINITIAL_MSR         ( portCRITICAL_INTERRUPT_ENABLE | portEXTERNAL_INTERRUPT_ENABLE | portMACHINE_CHECK_ENABLE | portAPU_PRESENT | portFCM_FPU_PRESENT )\r
97 \r
98 \r
99 extern const unsigned _SDA_BASE_;\r
100 extern const unsigned _SDA2_BASE_;\r
101 \r
102 /*-----------------------------------------------------------*/\r
103 \r
104 /*\r
105  * Setup the system timer to generate the tick interrupt.\r
106  */\r
107 static void prvSetupTimerInterrupt( void );\r
108 \r
109 /*\r
110  * The handler for the tick interrupt - defined in portasm.s.\r
111  */\r
112 extern void vPortTickISR( void );\r
113 \r
114 /*\r
115  * The handler for the yield function - defined in portasm.s.\r
116  */\r
117 extern void vPortYield( void );\r
118 \r
119 /*\r
120  * Function to start the scheduler running by starting the highest\r
121  * priority task that has thus far been created.\r
122  */\r
123 extern void vPortStartFirstTask( void );\r
124 \r
125 /*-----------------------------------------------------------*/\r
126 \r
127 /* Structure used to hold the state of the interrupt controller. */\r
128 static XIntc xInterruptController;\r
129 \r
130 /*-----------------------------------------------------------*/\r
131 \r
132 /* \r
133  * Initialise the stack of a task to look exactly as if the task had been\r
134  * interrupted.\r
135  * \r
136  * See the header file portable.h.\r
137  */\r
138 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
139 {\r
140         /* Place a known value at the bottom of the stack for debugging. */\r
141         *pxTopOfStack = 0xDEADBEEF;\r
142         pxTopOfStack--;\r
143 \r
144         /* EABI stack frame. */\r
145         pxTopOfStack -= 20;     /* Previous backchain and LR, R31 to R4 inclusive. */\r
146 \r
147         /* Parameters in R13. */\r
148         *pxTopOfStack = ( portSTACK_TYPE ) &_SDA_BASE_; /* address of the first small data area */\r
149         pxTopOfStack -= 10;\r
150 \r
151         /* Parameters in R3. */\r
152         *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;\r
153         pxTopOfStack--;\r
154 \r
155         /* Parameters in R2. */\r
156         *pxTopOfStack = ( portSTACK_TYPE ) &_SDA2_BASE_;        /* address of the second small data area */\r
157         pxTopOfStack--;\r
158 \r
159         /* R1 is the stack pointer so is omitted. */\r
160 \r
161         *pxTopOfStack = 0x10000001UL;;  /* R0. */\r
162         pxTopOfStack--;\r
163         *pxTopOfStack = 0x00000000UL;   /* USPRG0. */\r
164         pxTopOfStack--;\r
165         *pxTopOfStack = 0x00000000UL;   /* CR. */\r
166         pxTopOfStack--;\r
167         *pxTopOfStack = 0x00000000UL;   /* XER. */\r
168         pxTopOfStack--;\r
169         *pxTopOfStack = 0x00000000UL;   /* CTR. */\r
170         pxTopOfStack--;\r
171         *pxTopOfStack = ( portSTACK_TYPE ) vPortEndScheduler;   /* LR. */\r
172         pxTopOfStack--;\r
173         *pxTopOfStack = ( portSTACK_TYPE ) pxCode; /* SRR0. */\r
174         pxTopOfStack--;\r
175         *pxTopOfStack = portINITIAL_MSR;/* SRR1. */\r
176         pxTopOfStack--;\r
177         *pxTopOfStack = ( portSTACK_TYPE ) vPortEndScheduler;/* Next LR. */\r
178         pxTopOfStack--;\r
179         *pxTopOfStack = 0x00000000UL;/* Backchain. */\r
180 \r
181         return pxTopOfStack;\r
182 }\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 portBASE_TYPE xPortStartScheduler( void )\r
186 {\r
187         prvSetupTimerInterrupt();\r
188         XExc_RegisterHandler( XEXC_ID_SYSTEM_CALL, ( XExceptionHandler ) vPortYield, ( void * ) 0 );\r
189         vPortStartFirstTask();\r
190 \r
191         /* Should not get here as the tasks are now running! */\r
192         return pdFALSE;\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 void vPortEndScheduler( void )\r
197 {\r
198         /* Not implemented. */\r
199         for( ;; );\r
200 }\r
201 /*-----------------------------------------------------------*/\r
202 \r
203 /*\r
204  * Hardware initialisation to generate the RTOS tick.   \r
205  */\r
206 static void prvSetupTimerInterrupt( void )\r
207 {\r
208 const unsigned long ulInterval = ( ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL );\r
209 \r
210         XTime_PITClearInterrupt();\r
211         XTime_FITClearInterrupt();\r
212         XTime_WDTClearInterrupt();\r
213         XTime_WDTDisableInterrupt();\r
214         XTime_FITDisableInterrupt();\r
215 \r
216         XExc_RegisterHandler( XEXC_ID_PIT_INT, ( XExceptionHandler ) vPortTickISR, ( void * ) 0 );\r
217 \r
218         XTime_PITEnableAutoReload();\r
219         XTime_PITSetInterval( ulInterval );\r
220         XTime_PITEnableInterrupt();\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 void vPortISRHandler( void *pvNullDoNotUse )\r
225 {\r
226 unsigned long ulInterruptStatus, ulInterruptMask = 1UL;\r
227 portBASE_TYPE xInterruptNumber;\r
228 XIntc_Config *pxInterruptController;\r
229 XIntc_VectorTableEntry *pxTable;\r
230 \r
231         /* Just to remove compiler warning. */\r
232         ( void ) pvNullDoNotUse;        \r
233 \r
234         /* Get the configuration by using the device ID - in this case it is\r
235         assumed that only one interrupt controller is being used. */\r
236         pxInterruptController = &XIntc_ConfigTable[ XPAR_XPS_INTC_0_DEVICE_ID ];\r
237   \r
238         /* Which interrupts are pending? */\r
239         ulInterruptStatus = XIntc_mGetIntrStatus( pxInterruptController->BaseAddress );\r
240   \r
241         for( xInterruptNumber = 0; xInterruptNumber < XPAR_INTC_MAX_NUM_INTR_INPUTS; xInterruptNumber++ )\r
242         {\r
243                 if( ulInterruptStatus & 0x01UL )\r
244                 {\r
245                         /* Clear the pending interrupt. */\r
246                         XIntc_mAckIntr( pxInterruptController->BaseAddress, ulInterruptMask );\r
247 \r
248                         /* Call the registered handler. */\r
249                         pxTable = &( pxInterruptController->HandlerTable[ xInterruptNumber ] );\r
250                         pxTable->Handler( pxTable->CallBackRef );\r
251                 }\r
252         \r
253                 /* Check the next interrupt. */\r
254                 ulInterruptMask <<= 0x01UL;\r
255                 ulInterruptStatus >>= 0x01UL;\r
256 \r
257                 /* Have we serviced all interrupts? */\r
258                 if( ulInterruptStatus == 0UL )\r
259                 {\r
260                         break;\r
261                 }\r
262         }\r
263 }\r
264 /*-----------------------------------------------------------*/\r
265 \r
266 void vPortSetupInterruptController( void )\r
267 {\r
268 extern void vPortISRWrapper( void );\r
269 \r
270         /* Perform all library calls necessary to initialise the exception table\r
271         and interrupt controller.  This assumes only one interrupt controller is in\r
272         use. */\r
273         XExc_mDisableExceptions( XEXC_NON_CRITICAL );\r
274         XExc_Init();\r
275 \r
276         /* The library functions save the context - we then jump to a wrapper to\r
277         save the stack into the TCB.  The wrapper then calls the handler defined\r
278         above. */\r
279         XExc_RegisterHandler( XEXC_ID_NON_CRITICAL_INT, ( XExceptionHandler ) vPortISRWrapper, NULL );\r
280         XIntc_Initialize( &xInterruptController, XPAR_XPS_INTC_0_DEVICE_ID );\r
281         XIntc_Start( &xInterruptController, XIN_REAL_MODE );\r
282 }\r
283 /*-----------------------------------------------------------*/\r
284 \r
285 portBASE_TYPE xPortInstallInterruptHandler( unsigned char ucInterruptID, XInterruptHandler pxHandler, void *pvCallBackRef )\r
286 {\r
287 portBASE_TYPE xReturn = pdFAIL;\r
288 \r
289         /* This function is defined here so the scope of xInterruptController can\r
290         remain within this file. */\r
291 \r
292         if( XST_SUCCESS == XIntc_Connect( &xInterruptController, ucInterruptID, pxHandler, pvCallBackRef ) )\r
293         {\r
294                 XIntc_Enable( &xInterruptController, ucInterruptID );\r
295                 xReturn = pdPASS;\r
296         }\r
297 \r
298         return xReturn;         \r
299 }\r