2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /******************************************************************************
\r
30 * This project provides two demo applications. A simple blinky style project,
\r
31 * and a more comprehensive test and demo application. The
\r
32 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined in this file) is used to
\r
33 * select between the two. The simply blinky demo is implemented and described
\r
34 * in main_blinky.c. The more comprehensive test and demo application is
\r
35 * implemented and described in main_full.c.
\r
37 * This file implements the code that is not demo specific, including the
\r
38 * hardware setup and FreeRTOS hook functions.
\r
40 * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON
\r
41 * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO
\r
42 * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!
\r
43 * http://www.FreeRTOS.org/RTOS_Intel_Quark_Galileo_GCC.html
\r
47 /* Standard includes. */
\r
50 /* Scheduler include files. */
\r
51 #include "FreeRTOS.h"
\r
55 /* Standard demo includes, only necessary for the tick hook. */
\r
56 #include "TimerDemo.h"
\r
57 #include "QueueOverwrite.h"
\r
58 #include "EventGroupsDemo.h"
\r
59 #include "QueueSet.h"
\r
60 #include "TaskNotify.h"
\r
61 #include "IntQueue.h"
\r
63 /* Added Galileo serial support. */
\r
64 #include "galileo_support.h"
\r
66 /* Set to 1 to sit in a loop on start up, allowing a debugger to connect to the
\r
67 application before main() executes. */
\r
68 #define mainWAIT_FOR_DEBUG_CONNECTION 0
\r
70 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,
\r
71 or 0 to run the more comprehensive test and demo application. */
\r
72 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY 1
\r
74 /*-----------------------------------------------------------*/
\r
77 * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.
\r
78 * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.
\r
80 #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )
\r
81 extern void main_blinky( void );
\r
83 extern void main_full( void );
\r
84 #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 */
\r
86 /* Prototypes for functions called from asm start up code. */
\r
88 void CRT_Init( void );
\r
91 * Prototypes for the standard FreeRTOS callback/hook functions implemented
\r
94 void vApplicationMallocFailedHook( void );
\r
95 void vApplicationIdleHook( void );
\r
96 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
\r
97 void vApplicationTickHook( void );
\r
100 * Perform any hardware/peripheral related initialisation necessary to run the
\r
103 static void prvSetupHardware( void );
\r
104 static void prvCalibrateLVTimer( void );
\r
107 * If mainWAIT_FOR_DEBUG_CONNECTION is set to 1 then the following function will
\r
108 * sit in a loop on start up, allowing a debugger to connect to the application
\r
109 * before main() executes. If mainWAIT_FOR_DEBUG_CONNECTION is not set to 1
\r
110 * then the following function does nothing.
\r
112 static void prvLoopToWaitForDebugConnection( void );
\r
115 * Helper functions used when an assert is triggered. The first periodically
\r
116 * displays an assert message, and the second clears the assert message when the
\r
117 * function called by the configASSERT() macro is exited.
\r
119 static void prvDisplayAssertion( const char * pcFile, unsigned long ulLine );
\r
120 static void prvClearAssertionLine( void );
\r
122 /*-----------------------------------------------------------*/
\r
124 /* See http://www.FreeRTOS.org/RTOS_Intel_Quark_Galileo_GCC.html for usage
\r
128 /* Optionally wait for a debugger to connect. */
\r
129 prvLoopToWaitForDebugConnection();
\r
131 /* Init the UART, GPIO, etc. */
\r
132 prvSetupHardware();
\r
134 /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top
\r
136 #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )
\r
138 g_printf_rcc( 3, 2, DEFAULT_SCREEN_COLOR, "Running main_blinky()." );
\r
143 g_printf_rcc( 3, 2, DEFAULT_SCREEN_COLOR, "Running main_full()." );
\r
150 /*-----------------------------------------------------------*/
\r
152 void vApplicationMallocFailedHook( void )
\r
154 /* Called if a call to pvPortMalloc() fails because there is insufficient
\r
155 free memory available in the FreeRTOS heap. pvPortMalloc() is called
\r
156 internally by FreeRTOS API functions that create tasks, queues, software
\r
157 timers, and semaphores. The size of the FreeRTOS heap is set by the
\r
158 configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h.
\r
160 Force an assert. */
\r
161 configASSERT( xTaskGetTickCount() == 0 );
\r
162 taskDISABLE_INTERRUPTS();
\r
165 /*-----------------------------------------------------------*/
\r
167 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
169 ( void ) pcTaskName;
\r
172 /* Run time stack overflow checking is performed if
\r
173 configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
\r
174 function is called if a stack overflow is detected.
\r
176 Increase the size of the stack allocated to the offending task.
\r
178 Force an assert. */
\r
179 configASSERT( pxTask == NULL );
\r
180 taskDISABLE_INTERRUPTS();
\r
183 /*-----------------------------------------------------------*/
\r
185 void vApplicationIdleHook( void )
\r
187 volatile unsigned long xFreeHeapSpace;
\r
189 /* This is just a trivial example of an idle hook. It is called on each
\r
190 cycle of the idle task. It must *NOT* attempt to block. In this case the
\r
191 idle task just queries the amount of FreeRTOS heap that remains. See the
\r
192 memory management section on the http://www.FreeRTOS.org web site for memory
\r
193 management options. If there is a lot of heap memory free then the
\r
194 configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up
\r
196 xFreeHeapSpace = xPortGetFreeHeapSize();
\r
198 /* Remove compiler warning about xFreeHeapSpace being set but never used. */
\r
199 ( void ) xFreeHeapSpace;
\r
201 /*-----------------------------------------------------------*/
\r
203 static void prvDisplayAssertion( const char * pcFile, unsigned long ulLine )
\r
205 extern void vMilliSecondDelay( uint32_t DelayTime );
\r
206 const uint32_t ul500ms = 500UL;
\r
208 /* Display assertion file and line. Don't use the gated g_printf just in
\r
209 the assert was triggered while the gating semaphore was taken. Always print
\r
211 UngatedMoveToScreenPosition( 23, 2 );
\r
212 printf( ANSI_COLOR_RED );
\r
213 printf( "ASSERT: File = %s, Line = %u\n\r", pcFile, ulLine );
\r
214 printf( ANSI_COLOR_RESET );
\r
215 printf( ANSI_SHOW_CURSOR );
\r
216 vMilliSecondDelay( ul500ms );
\r
218 /*-----------------------------------------------------------*/
\r
220 static void prvClearAssertionLine( void )
\r
222 UngatedMoveToScreenPosition( 23, 1 );
\r
223 printf( ANSI_COLOR_RESET );
\r
224 printf( ANSI_CLEAR_LINE );
\r
225 printf( ANSI_HIDE_CURSOR );
\r
227 /*-----------------------------------------------------------*/
\r
229 void vAssertCalled( const char * pcFile, unsigned long ulLine )
\r
231 volatile uint32_t ul = 0;
\r
236 taskENTER_CRITICAL();
\r
238 /* Set ul to a non-zero value or press a key to step out of this
\r
239 function in order to inspect the location of the assert(). */
\r
241 /* Clear any pending key presses. */
\r
242 while( ucGalileoGetchar() != 0 )
\r
244 /* Nothing to do here - the key press is just discarded. */
\r
249 prvDisplayAssertion(pcFile, ulLine);
\r
250 } while ( ( ul == pdFALSE ) && ( ucGalileoGetchar() == 0 ) );
\r
252 prvClearAssertionLine();
\r
254 taskEXIT_CRITICAL();
\r
256 /*-----------------------------------------------------------*/
\r
258 void vApplicationTickHook( void )
\r
260 #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0 )
\r
262 extern void vTimerPeriodicISRTests( void );
\r
264 /* The full demo includes a software timer demo/test that requires
\r
265 prodding periodically from the tick interrupt. */
\r
266 vTimerPeriodicISRTests();
\r
268 /* Call the periodic queue overwrite from ISR demo. */
\r
269 vQueueOverwritePeriodicISRDemo();
\r
271 /* Call the periodic event group from ISR demo. */
\r
272 vPeriodicEventGroupsProcessing();
\r
274 /* Call the periodic queue set from ISR demo. */
\r
275 vQueueSetAccessQueueSetFromISR();
\r
277 /* Use task notifications from an interrupt. */
\r
278 xNotifyTaskFromISR();
\r
282 /*-----------------------------------------------------------*/
\r
284 static void prvSetupHardware( void )
\r
286 /* Initialise the serial port and GPIO. */
\r
287 vInitializeGalileoSerialPort( DEBUG_SERIAL_PORT );
\r
288 vGalileoInitializeGpioController();
\r
289 vGalileoInitializeLegacyGPIO();
\r
291 /* Initialise HPET interrupt(s) */
\r
292 #if( ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 ) && ( hpetHPET_TIMER_IN_USE != 0 ) )
\r
294 portDISABLE_INTERRUPTS();
\r
295 vInitializeAllHPETInterrupts();
\r
299 /* Setup the LED. */
\r
300 vGalileoLegacyGPIOInitializationForLED();
\r
302 /* Demonstrates how to calibrate LAPIC Timer. The calibration value
\r
303 calculated here may get overwritten when the scheduler starts. */
\r
304 prvCalibrateLVTimer();
\r
306 /* Print RTOS loaded message. */
\r
309 /*-----------------------------------------------------------*/
\r
311 static void prvLoopToWaitForDebugConnection( void )
\r
313 /* Debug if define = 1. */
\r
314 #if( mainWAIT_FOR_DEBUG_CONNECTION == 1 )
\r
316 /* When using the debugger, set this value to pdFALSE, and the application
\r
317 will sit in a loop at the top of main() to allow the debugger to attached
\r
318 before the application starts running. Once attached, set
\r
319 ulExitResetSpinLoop to a non-zero value to leave the loop. */
\r
320 volatile uint32_t ulExitResetSpinLoop = pdFALSE;
\r
322 /* Must initialize UART before anything will print. */
\r
323 vInitializeGalileoSerialPort( DEBUG_SERIAL_PORT );
\r
325 /* RTOS loaded message. */
\r
328 /* Output instruction message. */
\r
329 MoveToScreenPosition( 3, 1 );
\r
330 g_printf( DEFAULT_SCREEN_COLOR );
\r
331 g_printf( " Waiting for JTAG connection.\n\n\r" );
\r
332 g_printf( ANSI_COLOR_RESET );
\r
333 g_printf( " Once connected, either set ulExitResetSpinLoop to a non-zero value,\n\r" );
\r
334 g_printf( " or you can [PRESS ANY KEY] to start the debug session.\n\n\r" );
\r
335 printf( ANSI_SHOW_CURSOR );
\r
337 /* Use the debugger to set the ulExitResetSpinLoop to a non-zero value
\r
338 or press a key to exit this loop, and step through the application. In
\r
339 Eclipse, simple hover over the variable to see its value in a pop-over
\r
340 box, then edit the value in the pop-over box. */
\r
345 } while( ( ulExitResetSpinLoop == pdFALSE ) && ( ucGalileoGetchar() == 0 ) );
\r
349 /*-----------------------------------------------------------*/
\r
351 void CRT_Init( void )
\r
353 extern uint32_t __bss_start[];
\r
354 extern uint32_t __bss_end[];
\r
355 extern uint32_t __data_vma[];
\r
356 extern uint32_t __data_lma[];
\r
357 extern uint32_t __data_start[];
\r
358 extern uint32_t __data_end[];
\r
362 /* Zero out bss. */
\r
363 xSize = ( ( size_t ) __bss_end ) - ( ( size_t ) __bss_start );
\r
364 memset( ( void * ) __bss_start, 0x00, xSize );
\r
366 /* Copy initialised variables. */
\r
367 xSize = ( ( size_t ) __data_end ) - ( ( size_t ) __data_start );
\r
368 memcpy( ( void * ) __data_vma, __data_lma, xSize );
\r
370 /* Ensure no interrupts are pending. */
\r
377 /*-----------------------------------------------------------*/
\r
379 static void prvCalibrateLVTimer( void )
\r
381 uint32_t uiInitialTimerCounts, uiCalibratedTimerCounts;
\r
383 /* Disable LAPIC Counter. */
\r
384 portAPIC_LVT_TIMER = portAPIC_DISABLE;
\r
386 /* Calibrate the LV Timer counts to ensure it matches the HPET timer over
\r
387 extended periods. */
\r
388 uiInitialTimerCounts = ( ( configCPU_CLOCK_HZ >> 4UL ) / configTICK_RATE_HZ );
\r
389 uiCalibratedTimerCounts = uiCalibrateTimer( 0, hpetLVTIMER );
\r
391 if( uiCalibratedTimerCounts != 0 )
\r
393 uiInitialTimerCounts = uiCalibratedTimerCounts;
\r
396 /* Set the interrupt frequency. */
\r
397 portAPIC_TMRDIV = portAPIC_DIV_16;
\r
398 portAPIC_TIMER_INITIAL_COUNT = uiInitialTimerCounts;
\r
400 /* Enable LAPIC Counter. */
\r
401 portAPIC_LVT_TIMER = portAPIC_TIMER_PERIODIC | portAPIC_TIMER_INT_VECTOR;
\r
403 /* Sometimes needed. */
\r
404 portAPIC_TMRDIV = portAPIC_DIV_16;
\r