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