]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/IA32_flat_GCC_Galileo_Gen_2/main.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Demo / IA32_flat_GCC_Galileo_Gen_2 / main.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  * This project provides two demo applications.  A simple blinky style project,\r
72  * and a more comprehensive test and demo application.  The\r
73  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined in this file) is used to\r
74  * select between the two.  The simply blinky demo is implemented and described\r
75  * in main_blinky.c.  The more comprehensive test and demo application is\r
76  * implemented and described in main_full.c.\r
77  *\r
78  * This file implements the code that is not demo specific, including the\r
79  * hardware setup and FreeRTOS hook functions.\r
80  *\r
81  * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON\r
82  * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO\r
83  * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!\r
84  * http://www.FreeRTOS.org/RTOS_Intel_Quark_Galileo_GCC.html\r
85  *\r
86  */\r
87 \r
88 /* Standard includes. */\r
89 #include <stdlib.h>\r
90 \r
91 /* Scheduler include files. */\r
92 #include "FreeRTOS.h"\r
93 #include "task.h"\r
94 #include "semphr.h"\r
95 \r
96 /* Standard demo includes, only necessary for the tick hook. */\r
97 #include "TimerDemo.h"\r
98 #include "QueueOverwrite.h"\r
99 #include "EventGroupsDemo.h"\r
100 #include "QueueSet.h"\r
101 #include "TaskNotify.h"\r
102 #include "IntQueue.h"\r
103 \r
104 /* Added Galileo serial support. */\r
105 #include "galileo_support.h"\r
106 \r
107 /* Set to 1 to sit in a loop on start up, allowing a debugger to connect to the\r
108 application before main() executes. */\r
109 #define mainWAIT_FOR_DEBUG_CONNECTION           0\r
110 \r
111 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,\r
112 or 0 to run the more comprehensive test and demo application. */\r
113 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      1\r
114 \r
115 /*-----------------------------------------------------------*/\r
116 \r
117 /*\r
118  * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
119  * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
120  */\r
121 #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
122         extern void main_blinky( void );\r
123 #else\r
124         extern void main_full( void );\r
125 #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 */\r
126 \r
127 /* Prototypes for functions called from asm start up code. */\r
128 int main( void );\r
129 void CRT_Init( void );\r
130 \r
131 /*\r
132  * Prototypes for the standard FreeRTOS callback/hook functions implemented\r
133  * within this file.\r
134  */\r
135 void vApplicationMallocFailedHook( void );\r
136 void vApplicationIdleHook( void );\r
137 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
138 void vApplicationTickHook( void );\r
139 \r
140 /*\r
141  * Perform any hardware/peripheral related initialisation necessary to run the\r
142  * demo.\r
143  */\r
144 static void prvSetupHardware( void );\r
145 static void prvCalibrateLVTimer( void );\r
146 \r
147 /*\r
148  * If mainWAIT_FOR_DEBUG_CONNECTION is set to 1 then the following function will\r
149  * sit in a loop on start up, allowing a debugger to connect to the application\r
150  * before main() executes.  If mainWAIT_FOR_DEBUG_CONNECTION is not set to 1\r
151  * then the following function does nothing.\r
152  */\r
153 static void prvLoopToWaitForDebugConnection( void );\r
154 \r
155 /*\r
156  * Helper functions used when an assert is triggered.  The first periodically\r
157  * displays an assert message, and the second clears the assert message when the\r
158  * function called by the configASSERT() macro is exited.\r
159  */\r
160 static void prvDisplayAssertion( const char * pcFile, unsigned long ulLine );\r
161 static void prvClearAssertionLine( void );\r
162 \r
163 /*-----------------------------------------------------------*/\r
164 \r
165 /* See http://www.FreeRTOS.org/RTOS_Intel_Quark_Galileo_GCC.html for usage\r
166 instructions. */\r
167 int main( void )\r
168 {\r
169         /* Optionally wait for a debugger to connect. */\r
170         prvLoopToWaitForDebugConnection();\r
171 \r
172         /* Init the UART, GPIO, etc. */\r
173         prvSetupHardware();\r
174 \r
175         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
176         of this file. */\r
177         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
178         {\r
179                 g_printf_rcc( 3, 2, DEFAULT_SCREEN_COLOR, "Running main_blinky()." );\r
180                 main_blinky();\r
181         }\r
182         #else\r
183         {\r
184                 g_printf_rcc( 3, 2, DEFAULT_SCREEN_COLOR, "Running main_full()." );\r
185                 main_full();\r
186         }\r
187         #endif\r
188 \r
189         return 0;\r
190 }\r
191 /*-----------------------------------------------------------*/\r
192 \r
193 void vApplicationMallocFailedHook( void )\r
194 {\r
195         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
196         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
197         internally by FreeRTOS API functions that create tasks, queues, software\r
198         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
199         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h.\r
200 \r
201         Force an assert. */\r
202         configASSERT( xTaskGetTickCount() == 0 );\r
203         taskDISABLE_INTERRUPTS();\r
204         for( ;; );\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
209 {\r
210         ( void ) pcTaskName;\r
211         ( void ) pxTask;\r
212 \r
213         /* Run time stack overflow checking is performed if\r
214         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
215         function is called if a stack overflow is detected.\r
216 \r
217         Increase the size of the stack allocated to the offending task.\r
218 \r
219         Force an assert. */\r
220         configASSERT( pxTask == NULL );\r
221         taskDISABLE_INTERRUPTS();\r
222         for( ;; );\r
223 }\r
224 /*-----------------------------------------------------------*/\r
225 \r
226 void vApplicationIdleHook( void )\r
227 {\r
228         volatile unsigned long xFreeHeapSpace;\r
229 \r
230         /* This is just a trivial example of an idle hook.  It is called on each\r
231         cycle of the idle task.  It must *NOT* attempt to block.  In this case the\r
232         idle task just queries the amount of FreeRTOS heap that remains.  See the\r
233         memory management section on the http://www.FreeRTOS.org web site for memory\r
234         management options.  If there is a lot of heap memory free then the\r
235         configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up\r
236         RAM. */\r
237         xFreeHeapSpace = xPortGetFreeHeapSize();\r
238 \r
239         /* Remove compiler warning about xFreeHeapSpace being set but never used. */\r
240         ( void ) xFreeHeapSpace;\r
241 }\r
242 /*-----------------------------------------------------------*/\r
243 \r
244 static void prvDisplayAssertion( const char * pcFile, unsigned long ulLine )\r
245 {\r
246 extern void vMilliSecondDelay( uint32_t DelayTime );\r
247 const uint32_t ul500ms = 500UL;\r
248 \r
249         /* Display assertion file and line. Don't use the gated g_printf just in\r
250         the assert was triggered while the gating semaphore was taken.  Always print\r
251         on line 23. */\r
252         UngatedMoveToScreenPosition( 23, 2 );\r
253         printf( ANSI_COLOR_RED );\r
254         printf( "ASSERT: File = %s, Line = %u\n\r", pcFile, ulLine );\r
255         printf( ANSI_COLOR_RESET );\r
256         printf( ANSI_SHOW_CURSOR );\r
257         vMilliSecondDelay( ul500ms );\r
258 }\r
259 /*-----------------------------------------------------------*/\r
260 \r
261 static void prvClearAssertionLine( void )\r
262 {\r
263         UngatedMoveToScreenPosition( 23, 1 );\r
264         printf( ANSI_COLOR_RESET );\r
265         printf( ANSI_CLEAR_LINE );\r
266         printf( ANSI_HIDE_CURSOR );\r
267 }\r
268 /*-----------------------------------------------------------*/\r
269 \r
270 void vAssertCalled( const char * pcFile, unsigned long ulLine )\r
271 {\r
272 volatile uint32_t ul = 0;\r
273 \r
274         ( void ) pcFile;\r
275         ( void ) ulLine;\r
276 \r
277         taskENTER_CRITICAL();\r
278         {\r
279                 /* Set ul to a non-zero value or press a key to step out of this\r
280                 function in order to inspect the location of the assert(). */\r
281 \r
282                 /* Clear any pending key presses. */\r
283                 while( ucGalileoGetchar() != 0 )\r
284                 {\r
285                         /* Nothing to do here - the key press is just discarded. */\r
286                 }\r
287 \r
288                 do\r
289                 {\r
290                    prvDisplayAssertion(pcFile, ulLine);\r
291                 } while ( ( ul == pdFALSE ) && ( ucGalileoGetchar() == 0 ) );\r
292 \r
293                 prvClearAssertionLine();\r
294         }\r
295         taskEXIT_CRITICAL();\r
296 }\r
297 /*-----------------------------------------------------------*/\r
298 \r
299 void vApplicationTickHook( void )\r
300 {\r
301         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0 )\r
302         {\r
303                 extern void vTimerPeriodicISRTests( void );\r
304 \r
305                 /* The full demo includes a software timer demo/test that requires\r
306                 prodding periodically from the tick interrupt. */\r
307                 vTimerPeriodicISRTests();\r
308 \r
309                 /* Call the periodic queue overwrite from ISR demo. */\r
310                 vQueueOverwritePeriodicISRDemo();\r
311 \r
312                 /* Call the periodic event group from ISR demo. */\r
313                 vPeriodicEventGroupsProcessing();\r
314 \r
315                 /* Call the periodic queue set from ISR demo. */\r
316                 vQueueSetAccessQueueSetFromISR();\r
317 \r
318                 /* Use task notifications from an interrupt. */\r
319                 xNotifyTaskFromISR();\r
320         }\r
321         #endif\r
322 }\r
323 /*-----------------------------------------------------------*/\r
324 \r
325 static void prvSetupHardware( void )\r
326 {\r
327         /* Initialise the serial port and GPIO. */\r
328         vInitializeGalileoSerialPort( DEBUG_SERIAL_PORT );\r
329         vGalileoInitializeGpioController();\r
330         vGalileoInitializeLegacyGPIO();\r
331 \r
332         /* Initialise HPET interrupt(s) */\r
333         #if( ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 ) && ( hpetHPET_TIMER_IN_USE != 0 ) )\r
334         {\r
335                 portDISABLE_INTERRUPTS();\r
336                 vInitializeAllHPETInterrupts();\r
337         }\r
338         #endif\r
339 \r
340         /* Setup the LED. */\r
341         vGalileoLegacyGPIOInitializationForLED();\r
342 \r
343         /* Demonstrates how to calibrate LAPIC Timer.  The calibration value\r
344         calculated here may get overwritten when the scheduler starts. */\r
345         prvCalibrateLVTimer();\r
346 \r
347         /* Print RTOS loaded message. */\r
348         vPrintBanner();\r
349 }\r
350 /*-----------------------------------------------------------*/\r
351 \r
352 static void prvLoopToWaitForDebugConnection( void )\r
353 {\r
354         /* Debug if define = 1. */\r
355         #if( mainWAIT_FOR_DEBUG_CONNECTION == 1 )\r
356         {\r
357         /* When using the debugger, set this value to pdFALSE, and the application\r
358         will sit in a loop at the top of main() to allow the debugger to attached\r
359         before the application starts running.  Once attached, set\r
360         ulExitResetSpinLoop to a non-zero value to leave the loop. */\r
361         volatile uint32_t ulExitResetSpinLoop = pdFALSE;\r
362 \r
363                 /* Must initialize UART before anything will print. */\r
364                 vInitializeGalileoSerialPort( DEBUG_SERIAL_PORT );\r
365 \r
366                 /* RTOS loaded message. */\r
367                 vPrintBanner();\r
368 \r
369                 /* Output instruction message. */\r
370                 MoveToScreenPosition( 3, 1 );\r
371                 g_printf( DEFAULT_SCREEN_COLOR );\r
372                 g_printf( " Waiting for JTAG connection.\n\n\r" );\r
373                 g_printf( ANSI_COLOR_RESET );\r
374                 g_printf( " Once connected, either set ulExitResetSpinLoop to a non-zero value,\n\r" );\r
375                 g_printf( " or you can [PRESS ANY KEY] to start the debug session.\n\n\r" );\r
376                 printf( ANSI_SHOW_CURSOR );\r
377 \r
378                 /* Use the debugger to set the ulExitResetSpinLoop to a non-zero value\r
379                 or press a key to exit this loop, and step through the application.  In\r
380                 Eclipse, simple hover over the variable to see its value in a pop-over\r
381                 box, then edit the value in the pop-over box. */\r
382                 do\r
383                 {\r
384                         portNOP();\r
385 \r
386                 } while( ( ulExitResetSpinLoop == pdFALSE ) && ( ucGalileoGetchar() == 0 ) );\r
387         }\r
388         #endif\r
389 }\r
390 /*-----------------------------------------------------------*/\r
391 \r
392 void CRT_Init( void )\r
393 {\r
394 extern uint32_t __bss_start[];\r
395 extern uint32_t __bss_end[];\r
396 extern uint32_t __data_vma[];\r
397 extern uint32_t __data_lma[];\r
398 extern uint32_t __data_start[];\r
399 extern uint32_t __data_end[];\r
400 uint32_t x = 255;\r
401 size_t xSize;\r
402 \r
403         /* Zero out bss. */\r
404         xSize = ( ( size_t ) __bss_end ) - ( ( size_t ) __bss_start );\r
405         memset( ( void * ) __bss_start, 0x00, xSize );\r
406 \r
407         /* Copy initialised variables. */\r
408         xSize = ( ( size_t ) __data_end ) - ( ( size_t ) __data_start );\r
409         memcpy( ( void * ) __data_vma, __data_lma, xSize );\r
410 \r
411         /* Ensure no interrupts are pending. */\r
412         do\r
413         {\r
414                 portAPIC_EOI = 0;\r
415                 x--;\r
416         } while( x > 0 );\r
417 }\r
418 /*-----------------------------------------------------------*/\r
419 \r
420 static void prvCalibrateLVTimer( void )\r
421 {\r
422 uint32_t uiInitialTimerCounts, uiCalibratedTimerCounts;\r
423 \r
424         /* Disable LAPIC Counter. */\r
425         portAPIC_LVT_TIMER = portAPIC_DISABLE;\r
426 \r
427         /* Calibrate the LV Timer counts to ensure it matches the HPET timer over\r
428         extended periods. */\r
429         uiInitialTimerCounts = ( ( configCPU_CLOCK_HZ >> 4UL ) / configTICK_RATE_HZ );\r
430         uiCalibratedTimerCounts = uiCalibrateTimer( 0, hpetLVTIMER );\r
431 \r
432         if( uiCalibratedTimerCounts != 0 )\r
433         {\r
434                 uiInitialTimerCounts = uiCalibratedTimerCounts;\r
435         }\r
436 \r
437         /* Set the interrupt frequency. */\r
438         portAPIC_TMRDIV = portAPIC_DIV_16;\r
439         portAPIC_TIMER_INITIAL_COUNT = uiInitialTimerCounts;\r
440 \r
441         /* Enable LAPIC Counter. */\r
442         portAPIC_LVT_TIMER = portAPIC_TIMER_PERIODIC | portAPIC_TIMER_INT_VECTOR;\r
443 \r
444         /* Sometimes needed. */\r
445         portAPIC_TMRDIV = portAPIC_DIV_16;\r
446 }\r