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