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