]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MicroBlaze_Kintex7_EthernetLite/RTOSDemo/src/main.c
2b877bbe3e6e22125e85812c7880f7ba71c38ef4
[freertos] / FreeRTOS / Demo / MicroBlaze_Kintex7_EthernetLite / RTOSDemo / src / main.c
1 /*\r
2     FreeRTOS V8.2.0 - Copyright (C) 2015 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  * This project provides three demo applications.  A simple blinky style\r
72  * project, a more comprehensive test and demo application, and an lwIP example.\r
73  * The mainSELECTED_APPLICATION setting (defined in this file) is used to\r
74  * select between the three.  The simply blinky demo is implemented and\r
75  * described in main_blinky.c.  The more comprehensive test and demo application\r
76  * is implemented and described in main_full.c.  The lwIP example is implemented\r
77  * and described in main_lwIP.c.\r
78  *\r
79  * This file implements the code that is not demo specific, including the\r
80  * hardware setup and FreeRTOS hook functions.\r
81  *\r
82  * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON\r
83  * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO\r
84  * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!\r
85  *\r
86  */\r
87 \r
88 /* Standard includes. */\r
89 #include <stdio.h>\r
90 #include <limits.h>\r
91 \r
92 /* Scheduler include files. */\r
93 #include "FreeRTOS.h"\r
94 #include "task.h"\r
95 \r
96 /* Demo app includes. */\r
97 #include "partest.h"\r
98 \r
99 /* Xilinx includes. */\r
100 #include "xtmrctr.h"\r
101 #include "xil_cache.h"\r
102 \r
103 /* mainSELECTED_APPLICATION is used to select between three demo applications,\r
104  * as described at the top of this file.\r
105  *\r
106  * When mainSELECTED_APPLICATION is set to 0 the simple blinky example will\r
107  * be run.\r
108  *\r
109  * When mainSELECTED_APPLICATION is set to 1 the comprehensive test and demo\r
110  * application will be run.\r
111  *\r
112  * When mainSELECTED_APPLICATION is set to 2 the lwIP example will be run.\r
113  */\r
114 #define mainSELECTED_APPLICATION        0\r
115 \r
116 /*-----------------------------------------------------------*/\r
117 \r
118 /*\r
119  * Configure the hardware as necessary to run this demo.\r
120  */\r
121 static void prvSetupHardware( void );\r
122 \r
123 /*\r
124 * See the comments at the top of this file and above the\r
125 * mainSELECTED_APPLICATION definition.\r
126 */\r
127 #if ( mainSELECTED_APPLICATION == 0 )\r
128         extern void main_blinky( void );\r
129 #elif ( mainSELECTED_APPLICATION == 1 )\r
130         extern void main_full( void );\r
131 #elif ( mainSELECTED_APPLICATION == 2 )\r
132         extern void main_lwIP( void );\r
133 #else\r
134         #error Invalid mainSELECTED_APPLICATION setting.  See the comments at the top of this file and above the mainSELECTED_APPLICATION definition.\r
135 #endif\r
136 \r
137 /* Prototypes for the standard FreeRTOS callback/hook functions implemented\r
138 within this file. */\r
139 void vApplicationMallocFailedHook( void );\r
140 void vApplicationIdleHook( void );\r
141 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
142 void vApplicationTickHook( void );\r
143 \r
144 /* The dual timer is used to generate the RTOS tick interrupt and as a time base\r
145 for the run time stats. */\r
146 static XTmrCtr xTickTimerInstance;\r
147 \r
148 /*-----------------------------------------------------------*/\r
149 volatile uint32_t ulx = 999;\r
150 \r
151 int main( void )\r
152 {\r
153         /* Check start up code executed correctly. */\r
154         configASSERT( ulx == 999 );\r
155         ulx = 0;\r
156 \r
157         /* Configure the hardware ready to run the demo. */\r
158         prvSetupHardware();\r
159 \r
160         /* The mainSELECTED_APPLICATION setting is described at the top\r
161         of this file. */\r
162         #if( mainSELECTED_APPLICATION == 0 )\r
163         {\r
164                 main_blinky();\r
165         }\r
166         #elif( mainSELECTED_APPLICATION == 1 )\r
167         {\r
168                 main_full();\r
169         }\r
170         #else\r
171         {\r
172                 main_lwIP();\r
173         }\r
174         #endif\r
175 \r
176         /* Don't expect to reach here. */\r
177         return 0;\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 static void prvSetupHardware( void )\r
182 {\r
183         microblaze_disable_interrupts();\r
184 \r
185         #if defined( XPAR_MICROBLAZE_USE_ICACHE ) && ( XPAR_MICROBLAZE_USE_ICACHE != 0 )\r
186         {\r
187                 Xil_ICacheInvalidate();\r
188                 Xil_ICacheEnable();\r
189         }\r
190         #endif\r
191 \r
192         #if defined( XPAR_MICROBLAZE_USE_DCACHE ) && ( XPAR_MICROBLAZE_USE_DCACHE != 0 )\r
193         {\r
194                 Xil_DCacheInvalidate();\r
195                 Xil_DCacheEnable();\r
196         }\r
197         #endif\r
198 \r
199         /* Initialise the LEDs.  ParTest is a historic name which used to stand for\r
200         PARallel port TEST. */\r
201         vParTestInitialise();\r
202 }\r
203 /*-----------------------------------------------------------*/\r
204 \r
205 void vApplicationMallocFailedHook( void )\r
206 {\r
207 volatile uint32_t ulDummy = 0;\r
208 \r
209         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
210         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
211         internally by FreeRTOS API functions that create tasks, queues, software\r
212         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
213         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h.  Force an\r
214         assertion failure. */\r
215         configASSERT( ulDummy != 0 );\r
216 }\r
217 /*-----------------------------------------------------------*/\r
218 \r
219 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
220 {\r
221         ( void ) pcTaskName;\r
222         ( void ) pxTask;\r
223 \r
224         /* Run time stack overflow checking is performed if\r
225         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
226         function is called if a stack overflow is detected.  Force an assertion\r
227         failure. */\r
228         configASSERT( ( char * ) pxTask == pcTaskName );\r
229 }\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 void vApplicationIdleHook( void )\r
233 {\r
234         #if( mainSELECTED_APPLICATION == 1 )\r
235         {\r
236                 extern void vFullDemoIdleHook( void );\r
237 \r
238                 /* When the full demo is build the idle hook is used to create some\r
239                 timers to flash LEDs. */\r
240                 vFullDemoIdleHook();\r
241         }\r
242         #endif\r
243 }\r
244 /*-----------------------------------------------------------*/\r
245 \r
246 void vAssertCalled( const char * pcFile, unsigned long ulLine )\r
247 {\r
248 volatile unsigned long ul = 0;\r
249 \r
250         ( void ) pcFile;\r
251         ( void ) ulLine;\r
252 \r
253         taskENTER_CRITICAL();\r
254         {\r
255                 /* Set ul to a non-zero value using the debugger to step out of this\r
256                 function. */\r
257                 while( ul == 0 )\r
258                 {\r
259                         portNOP();\r
260                 }\r
261         }\r
262         taskEXIT_CRITICAL();\r
263 }\r
264 /*-----------------------------------------------------------*/\r
265 \r
266 void vApplicationTickHook( void )\r
267 {\r
268         #if( mainSELECTED_APPLICATION == 1 )\r
269         {\r
270                 extern void vFullDemoTickHook( void );\r
271 \r
272                 /* When the full demo is build the tick hook is used to demonstrate\r
273                 functions being called from an interrupt and perform some tests. */\r
274                 vFullDemoTickHook();\r
275         }\r
276         #endif\r
277 }\r
278 /*-----------------------------------------------------------*/\r
279 \r
280 /* This is an application defined callback function used to install the tick\r
281 interrupt handler.  It is provided as an application callback because the kernel\r
282 will run on lots of different MicroBlaze and FPGA configurations - not all of\r
283 which will have the same timer peripherals defined or available.  This example\r
284 uses the Dual Timer 0.  If that is available on your hardware platform then this\r
285 example callback implementation may not require modification.   The name of the\r
286 interrupt handler that must be installed is vPortTickISR(), which the function\r
287 below declares as an extern. */\r
288 void vApplicationSetupTimerInterrupt( void )\r
289 {\r
290 portBASE_TYPE xStatus;\r
291 const unsigned char ucTickTimerCounterNumber = ( unsigned char ) 0U;\r
292 const unsigned char ucRunTimeStatsCounterNumber = ( unsigned char ) 1U;\r
293 const unsigned long ulCounterValue = ( ( XPAR_TMRCTR_0_CLOCK_FREQ_HZ / configTICK_RATE_HZ ) - 1UL );\r
294 extern void vPortTickISR( void *pvUnused );\r
295 \r
296         /* Initialise the timer/counter. */\r
297         xStatus = XTmrCtr_Initialize( &xTickTimerInstance, XPAR_TMRCTR_0_DEVICE_ID );\r
298 \r
299         if( xStatus == XST_SUCCESS )\r
300         {\r
301                 /* Install the tick interrupt handler as the timer ISR.\r
302                 *NOTE* The xPortInstallInterruptHandler() API function must be used for\r
303                 this purpose. */\r
304                 xStatus = xPortInstallInterruptHandler( XPAR_INTC_0_TMRCTR_0_VEC_ID, vPortTickISR, NULL );\r
305         }\r
306 \r
307         if( xStatus == pdPASS )\r
308         {\r
309                 /* Enable the timer interrupt in the interrupt controller.\r
310                 *NOTE* The vPortEnableInterrupt() API function must be used for this\r
311                 purpose. */\r
312                 vPortEnableInterrupt( XPAR_INTC_0_TMRCTR_0_VEC_ID );\r
313 \r
314                 /* Configure the timer interrupt handler. */\r
315                 XTmrCtr_SetHandler( &xTickTimerInstance, ( void * ) vPortTickISR, NULL );\r
316 \r
317                 /* Set the correct period for the timer. */\r
318                 XTmrCtr_SetResetValue( &xTickTimerInstance, ucTickTimerCounterNumber, ulCounterValue );\r
319 \r
320                 /* Enable the interrupts.  Auto-reload mode is used to generate a\r
321                 periodic tick.  Note that interrupts are disabled when this function is\r
322                 called, so interrupts will not start to be processed until the first\r
323                 task has started to run. */\r
324                 XTmrCtr_SetOptions( &xTickTimerInstance, ucTickTimerCounterNumber, ( XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION | XTC_DOWN_COUNT_OPTION ) );\r
325 \r
326                 /* Start the timer. */\r
327                 XTmrCtr_Start( &xTickTimerInstance, ucTickTimerCounterNumber );\r
328 \r
329 \r
330 \r
331 \r
332                 /* The second timer is used as the time base for the run time stats.\r
333                 Auto-reload mode is used to ensure the timer does not stop. */\r
334                 XTmrCtr_SetOptions( &xTickTimerInstance, ucRunTimeStatsCounterNumber, XTC_AUTO_RELOAD_OPTION );\r
335 \r
336                 /* Start the timer. */\r
337                 XTmrCtr_Start( &xTickTimerInstance, ucRunTimeStatsCounterNumber );\r
338         }\r
339 \r
340         /* Sanity check that the function executed as expected. */\r
341         configASSERT( ( xStatus == pdPASS ) );\r
342 }\r
343 /*-----------------------------------------------------------*/\r
344 \r
345 /* This is an application defined callback function used to clear whichever\r
346 interrupt was installed by the the vApplicationSetupTimerInterrupt() callback\r
347 function.  It is provided as an application callback because the kernel will run\r
348 on lots of different MicroBlaze and FPGA configurations - not all of which will\r
349 have the same timer peripherals defined or available.  This example uses the\r
350 dual timer 0.  If that is available on your hardware platform then this example\r
351 callback implementation will not require modification provided the example\r
352 definition of vApplicationSetupTimerInterrupt() is also not modified. */\r
353 void vApplicationClearTimerInterrupt( void )\r
354 {\r
355 unsigned long ulCSR;\r
356 \r
357         /* Clear the timer interrupt */\r
358         ulCSR = XTmrCtr_GetControlStatusReg( XPAR_TMRCTR_0_BASEADDR, 0 );\r
359         XTmrCtr_SetControlStatusReg( XPAR_TMRCTR_0_BASEADDR, 0, ulCSR );\r
360 }\r
361 /*-----------------------------------------------------------*/\r
362 \r
363 void *malloc( size_t x )\r
364 {\r
365         /* Just to check it never gets called as there is no heap defined (other\r
366         than the FreeRTOS heap). */\r
367         for( ;; );\r
368 }\r
369 /*-----------------------------------------------------------*/\r
370 \r
371 uint32_t ulMainGetRunTimeCounterValue( void )\r
372 {\r
373 static uint32_t ulOverflows = 0, ulLastTime = 0;\r
374 uint32_t ulTimeNow, ulReturn;\r
375 const uint32_t ulPrescale = 10, ulTCR2Offset = 24UL;\r
376 \r
377         ulTimeNow = * ( ( uint32_t * ) ( XPAR_TMRCTR_0_BASEADDR + ulTCR2Offset ) );\r
378 \r
379         if( ulTimeNow < ulLastTime )\r
380         {\r
381                 /* 32 as its a 32-bit number. */\r
382                 ulOverflows += ( 1UL << ( 32 - ulPrescale ) );\r
383         }\r
384         ulLastTime = ulTimeNow;\r
385 \r
386         ulReturn = ( ulTimeNow >> ulPrescale ) + ulOverflows;\r
387 \r
388         return ulReturn;\r
389 }\r
390 /*-----------------------------------------------------------*/\r
391 \r
392 int outbyte( int c )\r
393 {\r
394         return c;\r
395 }\r
396 \r
397 void xil_printf( const char *pc, ... )\r
398 {\r
399         ( void ) pc;\r
400 }\r
401 \r
402 \r