2 * FreeRTOS Kernel V10.2.1
\r
3 * Copyright (C) 2019 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.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /******************************************************************************
\r
29 * This project provides two demo applications. A simple blinky style project,
\r
30 * and a more comprehensive test and demo application. The
\r
31 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined in this file) is used to
\r
32 * select between the two. The simply blinky demo is implemented and described
\r
33 * in main_blinky.c. The more comprehensive test and demo application is
\r
34 * implemented and described in main_full.c.
\r
36 * This file implements the code that is not demo specific, including the
\r
37 * hardware setup and FreeRTOS hook functions.
\r
39 * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON
\r
40 * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO
\r
41 * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!
\r
42 * http://www.FreeRTOS.org/RTOS_Intel_Quark_Galileo_GCC.html
\r
46 /* Standard includes. */
\r
49 /* Scheduler include files. */
\r
50 #include "FreeRTOS.h"
\r
54 /* Standard demo includes, only necessary for the tick hook. */
\r
55 #include "TimerDemo.h"
\r
56 #include "QueueOverwrite.h"
\r
57 #include "EventGroupsDemo.h"
\r
58 #include "QueueSet.h"
\r
59 #include "TaskNotify.h"
\r
60 #include "IntQueue.h"
\r
62 /* Added Galileo serial support. */
\r
63 #include "galileo_support.h"
\r
65 /* Set to 1 to sit in a loop on start up, allowing a debugger to connect to the
\r
66 application before main() executes. */
\r
67 #define mainWAIT_FOR_DEBUG_CONNECTION 0
\r
69 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,
\r
70 or 0 to run the more comprehensive test and demo application. */
\r
71 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY 1
\r
73 /*-----------------------------------------------------------*/
\r
76 * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.
\r
77 * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.
\r
79 #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )
\r
80 extern void main_blinky( void );
\r
82 extern void main_full( void );
\r
83 #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 */
\r
85 /* Prototypes for functions called from asm start up code. */
\r
87 void CRT_Init( void );
\r
90 * Prototypes for the standard FreeRTOS callback/hook functions implemented
\r
93 void vApplicationMallocFailedHook( void );
\r
94 void vApplicationIdleHook( void );
\r
95 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
\r
96 void vApplicationTickHook( void );
\r
99 * Perform any hardware/peripheral related initialisation necessary to run the
\r
102 static void prvSetupHardware( void );
\r
103 static void prvCalibrateLVTimer( void );
\r
106 * If mainWAIT_FOR_DEBUG_CONNECTION is set to 1 then the following function will
\r
107 * sit in a loop on start up, allowing a debugger to connect to the application
\r
108 * before main() executes. If mainWAIT_FOR_DEBUG_CONNECTION is not set to 1
\r
109 * then the following function does nothing.
\r
111 static void prvLoopToWaitForDebugConnection( void );
\r
114 * Helper functions used when an assert is triggered. The first periodically
\r
115 * displays an assert message, and the second clears the assert message when the
\r
116 * function called by the configASSERT() macro is exited.
\r
118 static void prvDisplayAssertion( const char * pcFile, unsigned long ulLine );
\r
119 static void prvClearAssertionLine( void );
\r
121 /*-----------------------------------------------------------*/
\r
123 /* See http://www.FreeRTOS.org/RTOS_Intel_Quark_Galileo_GCC.html for usage
\r
127 /* Optionally wait for a debugger to connect. */
\r
128 prvLoopToWaitForDebugConnection();
\r
130 /* Init the UART, GPIO, etc. */
\r
131 prvSetupHardware();
\r
133 /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top
\r
135 #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )
\r
137 g_printf_rcc( 3, 2, DEFAULT_SCREEN_COLOR, "Running main_blinky()." );
\r
142 g_printf_rcc( 3, 2, DEFAULT_SCREEN_COLOR, "Running main_full()." );
\r
149 /*-----------------------------------------------------------*/
\r
151 void vApplicationMallocFailedHook( void )
\r
153 /* Called if a call to pvPortMalloc() fails because there is insufficient
\r
154 free memory available in the FreeRTOS heap. pvPortMalloc() is called
\r
155 internally by FreeRTOS API functions that create tasks, queues, software
\r
156 timers, and semaphores. The size of the FreeRTOS heap is set by the
\r
157 configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h.
\r
159 Force an assert. */
\r
160 configASSERT( xTaskGetTickCount() == 0 );
\r
161 taskDISABLE_INTERRUPTS();
\r
164 /*-----------------------------------------------------------*/
\r
166 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
168 ( void ) pcTaskName;
\r
171 /* Run time stack overflow checking is performed if
\r
172 configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
\r
173 function is called if a stack overflow is detected.
\r
175 Increase the size of the stack allocated to the offending task.
\r
177 Force an assert. */
\r
178 configASSERT( pxTask == NULL );
\r
179 taskDISABLE_INTERRUPTS();
\r
182 /*-----------------------------------------------------------*/
\r
184 void vApplicationIdleHook( void )
\r
186 volatile unsigned long xFreeHeapSpace;
\r
188 /* This is just a trivial example of an idle hook. It is called on each
\r
189 cycle of the idle task. It must *NOT* attempt to block. In this case the
\r
190 idle task just queries the amount of FreeRTOS heap that remains. See the
\r
191 memory management section on the http://www.FreeRTOS.org web site for memory
\r
192 management options. If there is a lot of heap memory free then the
\r
193 configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up
\r
195 xFreeHeapSpace = xPortGetFreeHeapSize();
\r
197 /* Remove compiler warning about xFreeHeapSpace being set but never used. */
\r
198 ( void ) xFreeHeapSpace;
\r
200 /*-----------------------------------------------------------*/
\r
202 static void prvDisplayAssertion( const char * pcFile, unsigned long ulLine )
\r
204 extern void vMilliSecondDelay( uint32_t DelayTime );
\r
205 const uint32_t ul500ms = 500UL;
\r
207 /* Display assertion file and line. Don't use the gated g_printf just in
\r
208 the assert was triggered while the gating semaphore was taken. Always print
\r
210 UngatedMoveToScreenPosition( 23, 2 );
\r
211 printf( ANSI_COLOR_RED );
\r
212 printf( "ASSERT: File = %s, Line = %u\n\r", pcFile, ulLine );
\r
213 printf( ANSI_COLOR_RESET );
\r
214 printf( ANSI_SHOW_CURSOR );
\r
215 vMilliSecondDelay( ul500ms );
\r
217 /*-----------------------------------------------------------*/
\r
219 static void prvClearAssertionLine( void )
\r
221 UngatedMoveToScreenPosition( 23, 1 );
\r
222 printf( ANSI_COLOR_RESET );
\r
223 printf( ANSI_CLEAR_LINE );
\r
224 printf( ANSI_HIDE_CURSOR );
\r
226 /*-----------------------------------------------------------*/
\r
228 void vAssertCalled( const char * pcFile, unsigned long ulLine )
\r
230 volatile uint32_t ul = 0;
\r
235 taskENTER_CRITICAL();
\r
237 /* Set ul to a non-zero value or press a key to step out of this
\r
238 function in order to inspect the location of the assert(). */
\r
240 /* Clear any pending key presses. */
\r
241 while( ucGalileoGetchar() != 0 )
\r
243 /* Nothing to do here - the key press is just discarded. */
\r
248 prvDisplayAssertion(pcFile, ulLine);
\r
249 } while ( ( ul == pdFALSE ) && ( ucGalileoGetchar() == 0 ) );
\r
251 prvClearAssertionLine();
\r
253 taskEXIT_CRITICAL();
\r
255 /*-----------------------------------------------------------*/
\r
257 void vApplicationTickHook( void )
\r
259 #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0 )
\r
261 extern void vTimerPeriodicISRTests( void );
\r
263 /* The full demo includes a software timer demo/test that requires
\r
264 prodding periodically from the tick interrupt. */
\r
265 vTimerPeriodicISRTests();
\r
267 /* Call the periodic queue overwrite from ISR demo. */
\r
268 vQueueOverwritePeriodicISRDemo();
\r
270 /* Call the periodic event group from ISR demo. */
\r
271 vPeriodicEventGroupsProcessing();
\r
273 /* Call the periodic queue set from ISR demo. */
\r
274 vQueueSetAccessQueueSetFromISR();
\r
276 /* Use task notifications from an interrupt. */
\r
277 xNotifyTaskFromISR();
\r
281 /*-----------------------------------------------------------*/
\r
283 static void prvSetupHardware( void )
\r
285 /* Initialise the serial port and GPIO. */
\r
286 vInitializeGalileoSerialPort( DEBUG_SERIAL_PORT );
\r
287 vGalileoInitializeGpioController();
\r
288 vGalileoInitializeLegacyGPIO();
\r
290 /* Initialise HPET interrupt(s) */
\r
291 #if( ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 ) && ( hpetHPET_TIMER_IN_USE != 0 ) )
\r
293 portDISABLE_INTERRUPTS();
\r
294 vInitializeAllHPETInterrupts();
\r
298 /* Setup the LED. */
\r
299 vGalileoLegacyGPIOInitializationForLED();
\r
301 /* Demonstrates how to calibrate LAPIC Timer. The calibration value
\r
302 calculated here may get overwritten when the scheduler starts. */
\r
303 prvCalibrateLVTimer();
\r
305 /* Print RTOS loaded message. */
\r
308 /*-----------------------------------------------------------*/
\r
310 static void prvLoopToWaitForDebugConnection( void )
\r
312 /* Debug if define = 1. */
\r
313 #if( mainWAIT_FOR_DEBUG_CONNECTION == 1 )
\r
315 /* When using the debugger, set this value to pdFALSE, and the application
\r
316 will sit in a loop at the top of main() to allow the debugger to attached
\r
317 before the application starts running. Once attached, set
\r
318 ulExitResetSpinLoop to a non-zero value to leave the loop. */
\r
319 volatile uint32_t ulExitResetSpinLoop = pdFALSE;
\r
321 /* Must initialize UART before anything will print. */
\r
322 vInitializeGalileoSerialPort( DEBUG_SERIAL_PORT );
\r
324 /* RTOS loaded message. */
\r
327 /* Output instruction message. */
\r
328 MoveToScreenPosition( 3, 1 );
\r
329 g_printf( DEFAULT_SCREEN_COLOR );
\r
330 g_printf( " Waiting for JTAG connection.\n\n\r" );
\r
331 g_printf( ANSI_COLOR_RESET );
\r
332 g_printf( " Once connected, either set ulExitResetSpinLoop to a non-zero value,\n\r" );
\r
333 g_printf( " or you can [PRESS ANY KEY] to start the debug session.\n\n\r" );
\r
334 printf( ANSI_SHOW_CURSOR );
\r
336 /* Use the debugger to set the ulExitResetSpinLoop to a non-zero value
\r
337 or press a key to exit this loop, and step through the application. In
\r
338 Eclipse, simple hover over the variable to see its value in a pop-over
\r
339 box, then edit the value in the pop-over box. */
\r
344 } while( ( ulExitResetSpinLoop == pdFALSE ) && ( ucGalileoGetchar() == 0 ) );
\r
348 /*-----------------------------------------------------------*/
\r
350 void CRT_Init( void )
\r
352 extern uint32_t __bss_start[];
\r
353 extern uint32_t __bss_end[];
\r
354 extern uint32_t __data_vma[];
\r
355 extern uint32_t __data_lma[];
\r
356 extern uint32_t __data_start[];
\r
357 extern uint32_t __data_end[];
\r
361 /* Zero out bss. */
\r
362 xSize = ( ( size_t ) __bss_end ) - ( ( size_t ) __bss_start );
\r
363 memset( ( void * ) __bss_start, 0x00, xSize );
\r
365 /* Copy initialised variables. */
\r
366 xSize = ( ( size_t ) __data_end ) - ( ( size_t ) __data_start );
\r
367 memcpy( ( void * ) __data_vma, __data_lma, xSize );
\r
369 /* Ensure no interrupts are pending. */
\r
376 /*-----------------------------------------------------------*/
\r
378 static void prvCalibrateLVTimer( void )
\r
380 uint32_t uiInitialTimerCounts, uiCalibratedTimerCounts;
\r
382 /* Disable LAPIC Counter. */
\r
383 portAPIC_LVT_TIMER = portAPIC_DISABLE;
\r
385 /* Calibrate the LV Timer counts to ensure it matches the HPET timer over
\r
386 extended periods. */
\r
387 uiInitialTimerCounts = ( ( configCPU_CLOCK_HZ >> 4UL ) / configTICK_RATE_HZ );
\r
388 uiCalibratedTimerCounts = uiCalibrateTimer( 0, hpetLVTIMER );
\r
390 if( uiCalibratedTimerCounts != 0 )
\r
392 uiInitialTimerCounts = uiCalibratedTimerCounts;
\r
395 /* Set the interrupt frequency. */
\r
396 portAPIC_TMRDIV = portAPIC_DIV_16;
\r
397 portAPIC_TIMER_INITIAL_COUNT = uiInitialTimerCounts;
\r
399 /* Enable LAPIC Counter. */
\r
400 portAPIC_LVT_TIMER = portAPIC_TIMER_PERIODIC | portAPIC_TIMER_INT_VECTOR;
\r
402 /* Sometimes needed. */
\r
403 portAPIC_TMRDIV = portAPIC_DIV_16;
\r