2 FreeRTOS V8.2.1 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\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
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
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
25 ***************************************************************************
\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
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
37 ***************************************************************************
\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
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
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
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
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
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
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
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
78 * This file implements the code that is not demo specific, including the
\r
79 * hardware setup and FreeRTOS hook functions.
\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
88 /* Standard includes. */
\r
91 /* Scheduler include files. */
\r
92 #include "FreeRTOS.h"
\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
104 /* Added Galileo serial support. */
\r
105 #include "galileo_support.h"
\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
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
115 /*-----------------------------------------------------------*/
\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
121 #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )
\r
122 extern void main_blinky( void );
\r
124 extern void main_full( void );
\r
125 #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 */
\r
127 /* Prototypes for functions called from asm start up code. */
\r
129 void CRT_Init( void );
\r
132 * Prototypes for the standard FreeRTOS callback/hook functions implemented
\r
133 * within this file.
\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
141 * Perform any hardware/peripheral related initialisation necessary to run the
\r
144 static void prvSetupHardware( void );
\r
145 static void prvCalibrateLVTimer( void );
\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
153 static void prvLoopToWaitForDebugConnection( void );
\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
160 static void prvDisplayAssertion( const char * pcFile, unsigned long ulLine );
\r
161 static void prvClearAssertionLine( void );
\r
163 /*-----------------------------------------------------------*/
\r
165 /* See http://www.FreeRTOS.org/RTOS_Intel_Quark_Galileo_GCC.html for usage
\r
169 /* Optionally wait for a debugger to connect. */
\r
170 prvLoopToWaitForDebugConnection();
\r
172 /* Init the UART, GPIO, etc. */
\r
173 prvSetupHardware();
\r
175 /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top
\r
177 #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )
\r
179 g_printf_rcc( 3, 2, DEFAULT_SCREEN_COLOR, "Running main_blinky()." );
\r
184 g_printf_rcc( 3, 2, DEFAULT_SCREEN_COLOR, "Running main_full()." );
\r
191 /*-----------------------------------------------------------*/
\r
193 void vApplicationMallocFailedHook( void )
\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
201 Force an assert. */
\r
202 configASSERT( xTaskGetTickCount() == 0 );
\r
203 taskDISABLE_INTERRUPTS();
\r
206 /*-----------------------------------------------------------*/
\r
208 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
210 ( void ) pcTaskName;
\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
217 Increase the size of the stack allocated to the offending task.
\r
219 Force an assert. */
\r
220 configASSERT( pxTask == NULL );
\r
221 taskDISABLE_INTERRUPTS();
\r
224 /*-----------------------------------------------------------*/
\r
226 void vApplicationIdleHook( void )
\r
228 volatile unsigned long xFreeHeapSpace;
\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
237 xFreeHeapSpace = xPortGetFreeHeapSize();
\r
239 /* Remove compiler warning about xFreeHeapSpace being set but never used. */
\r
240 ( void ) xFreeHeapSpace;
\r
242 /*-----------------------------------------------------------*/
\r
244 static void prvDisplayAssertion( const char * pcFile, unsigned long ulLine )
\r
246 extern void vMilliSecondDelay( uint32_t DelayTime );
\r
247 const uint32_t ul500ms = 500UL;
\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
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
259 /*-----------------------------------------------------------*/
\r
261 static void prvClearAssertionLine( void )
\r
263 UngatedMoveToScreenPosition( 23, 1 );
\r
264 printf( ANSI_COLOR_RESET );
\r
265 printf( ANSI_CLEAR_LINE );
\r
266 printf( ANSI_HIDE_CURSOR );
\r
268 /*-----------------------------------------------------------*/
\r
270 void vAssertCalled( const char * pcFile, unsigned long ulLine )
\r
272 volatile uint32_t ul = 0;
\r
277 taskENTER_CRITICAL();
\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
282 /* Clear any pending key presses. */
\r
283 while( ucGalileoGetchar() != 0 )
\r
285 /* Nothing to do here - the key press is just discarded. */
\r
290 prvDisplayAssertion(pcFile, ulLine);
\r
291 } while ( ( ul == pdFALSE ) && ( ucGalileoGetchar() == 0 ) );
\r
293 prvClearAssertionLine();
\r
295 taskEXIT_CRITICAL();
\r
297 /*-----------------------------------------------------------*/
\r
299 void vApplicationTickHook( void )
\r
301 #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0 )
\r
303 extern void vTimerPeriodicISRTests( void );
\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
309 /* Call the periodic queue overwrite from ISR demo. */
\r
310 vQueueOverwritePeriodicISRDemo();
\r
312 /* Call the periodic event group from ISR demo. */
\r
313 vPeriodicEventGroupsProcessing();
\r
315 /* Call the periodic queue set from ISR demo. */
\r
316 vQueueSetAccessQueueSetFromISR();
\r
318 /* Use task notifications from an interrupt. */
\r
319 xNotifyTaskFromISR();
\r
323 /*-----------------------------------------------------------*/
\r
325 static void prvSetupHardware( void )
\r
327 /* Initialise the serial port and GPIO. */
\r
328 vInitializeGalileoSerialPort( DEBUG_SERIAL_PORT );
\r
329 vGalileoInitializeGpioController();
\r
330 vGalileoInitializeLegacyGPIO();
\r
332 /* Initialise HPET interrupt(s) */
\r
333 #if( ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 ) && ( hpetHPET_TIMER_IN_USE != 0 ) )
\r
335 portDISABLE_INTERRUPTS();
\r
336 vInitializeAllHPETInterrupts();
\r
340 /* Setup the LED. */
\r
341 vGalileoLegacyGPIOInitializationForLED();
\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
347 /* Print RTOS loaded message. */
\r
350 /*-----------------------------------------------------------*/
\r
352 static void prvLoopToWaitForDebugConnection( void )
\r
354 /* Debug if define = 1. */
\r
355 #if( mainWAIT_FOR_DEBUG_CONNECTION == 1 )
\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
363 /* Must initialize UART before anything will print. */
\r
364 vInitializeGalileoSerialPort( DEBUG_SERIAL_PORT );
\r
366 /* RTOS loaded message. */
\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
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
386 } while( ( ulExitResetSpinLoop == pdFALSE ) && ( ucGalileoGetchar() == 0 ) );
\r
390 /*-----------------------------------------------------------*/
\r
392 void CRT_Init( void )
\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
403 /* Zero out bss. */
\r
404 xSize = ( ( size_t ) __bss_end ) - ( ( size_t ) __bss_start );
\r
405 memset( ( void * ) __bss_start, 0x00, xSize );
\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
411 /* Ensure no interrupts are pending. */
\r
418 /*-----------------------------------------------------------*/
\r
420 static void prvCalibrateLVTimer( void )
\r
422 uint32_t uiInitialTimerCounts, uiCalibratedTimerCounts;
\r
424 /* Disable LAPIC Counter. */
\r
425 portAPIC_LVT_TIMER = portAPIC_DISABLE;
\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
432 if( uiCalibratedTimerCounts != 0 )
\r
434 uiInitialTimerCounts = uiCalibratedTimerCounts;
\r
437 /* Set the interrupt frequency. */
\r
438 portAPIC_TMRDIV = portAPIC_DIV_16;
\r
439 portAPIC_TIMER_INITIAL_COUNT = uiInitialTimerCounts;
\r
441 /* Enable LAPIC Counter. */
\r
442 portAPIC_LVT_TIMER = portAPIC_TIMER_PERIODIC | portAPIC_TIMER_INT_VECTOR;
\r
444 /* Sometimes needed. */
\r
445 portAPIC_TMRDIV = portAPIC_DIV_16;
\r