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