]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/IA32_flat_GCC_Galileo_Gen_2/main.c
07102337ca11364bb9c490b4be174bc9613d60bd
[freertos] / FreeRTOS / Demo / IA32_flat_GCC_Galileo_Gen_2 / main.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
14  *\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
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \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
35  *\r
36  * This file implements the code that is not demo specific, including the\r
37  * hardware setup and FreeRTOS hook functions.\r
38  *\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
43  *\r
44  */\r
45 \r
46 /* Standard includes. */\r
47 #include <stdlib.h>\r
48 \r
49 /* Scheduler include files. */\r
50 #include "FreeRTOS.h"\r
51 #include "task.h"\r
52 #include "semphr.h"\r
53 \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
61 \r
62 /* Added Galileo serial support. */\r
63 #include "galileo_support.h"\r
64 \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
68 \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
72 \r
73 /*-----------------------------------------------------------*/\r
74 \r
75 /*\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
78  */\r
79 #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
80         extern void main_blinky( void );\r
81 #else\r
82         extern void main_full( void );\r
83 #endif /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 */\r
84 \r
85 /* Prototypes for functions called from asm start up code. */\r
86 int main( void );\r
87 void CRT_Init( void );\r
88 \r
89 /*\r
90  * Prototypes for the standard FreeRTOS callback/hook functions implemented\r
91  * within this file.\r
92  */\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
97 \r
98 /*\r
99  * Perform any hardware/peripheral related initialisation necessary to run the\r
100  * demo.\r
101  */\r
102 static void prvSetupHardware( void );\r
103 static void prvCalibrateLVTimer( void );\r
104 \r
105 /*\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
110  */\r
111 static void prvLoopToWaitForDebugConnection( void );\r
112 \r
113 /*\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
117  */\r
118 static void prvDisplayAssertion( const char * pcFile, unsigned long ulLine );\r
119 static void prvClearAssertionLine( void );\r
120 \r
121 /*-----------------------------------------------------------*/\r
122 \r
123 /* See http://www.FreeRTOS.org/RTOS_Intel_Quark_Galileo_GCC.html for usage\r
124 instructions. */\r
125 int main( void )\r
126 {\r
127         /* Optionally wait for a debugger to connect. */\r
128         prvLoopToWaitForDebugConnection();\r
129 \r
130         /* Init the UART, GPIO, etc. */\r
131         prvSetupHardware();\r
132 \r
133         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
134         of this file. */\r
135         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
136         {\r
137                 g_printf_rcc( 3, 2, DEFAULT_SCREEN_COLOR, "Running main_blinky()." );\r
138                 main_blinky();\r
139         }\r
140         #else\r
141         {\r
142                 g_printf_rcc( 3, 2, DEFAULT_SCREEN_COLOR, "Running main_full()." );\r
143                 main_full();\r
144         }\r
145         #endif\r
146 \r
147         return 0;\r
148 }\r
149 /*-----------------------------------------------------------*/\r
150 \r
151 void vApplicationMallocFailedHook( void )\r
152 {\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
158 \r
159         Force an assert. */\r
160         configASSERT( xTaskGetTickCount() == 0 );\r
161         taskDISABLE_INTERRUPTS();\r
162         for( ;; );\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
167 {\r
168         ( void ) pcTaskName;\r
169         ( void ) pxTask;\r
170 \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
174 \r
175         Increase the size of the stack allocated to the offending task.\r
176 \r
177         Force an assert. */\r
178         configASSERT( pxTask == NULL );\r
179         taskDISABLE_INTERRUPTS();\r
180         for( ;; );\r
181 }\r
182 /*-----------------------------------------------------------*/\r
183 \r
184 void vApplicationIdleHook( void )\r
185 {\r
186         volatile unsigned long xFreeHeapSpace;\r
187 \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
194         RAM. */\r
195         xFreeHeapSpace = xPortGetFreeHeapSize();\r
196 \r
197         /* Remove compiler warning about xFreeHeapSpace being set but never used. */\r
198         ( void ) xFreeHeapSpace;\r
199 }\r
200 /*-----------------------------------------------------------*/\r
201 \r
202 static void prvDisplayAssertion( const char * pcFile, unsigned long ulLine )\r
203 {\r
204 extern void vMilliSecondDelay( uint32_t DelayTime );\r
205 const uint32_t ul500ms = 500UL;\r
206 \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
209         on line 23. */\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
216 }\r
217 /*-----------------------------------------------------------*/\r
218 \r
219 static void prvClearAssertionLine( void )\r
220 {\r
221         UngatedMoveToScreenPosition( 23, 1 );\r
222         printf( ANSI_COLOR_RESET );\r
223         printf( ANSI_CLEAR_LINE );\r
224         printf( ANSI_HIDE_CURSOR );\r
225 }\r
226 /*-----------------------------------------------------------*/\r
227 \r
228 void vAssertCalled( const char * pcFile, unsigned long ulLine )\r
229 {\r
230 volatile uint32_t ul = 0;\r
231 \r
232         ( void ) pcFile;\r
233         ( void ) ulLine;\r
234 \r
235         taskENTER_CRITICAL();\r
236         {\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
239 \r
240                 /* Clear any pending key presses. */\r
241                 while( ucGalileoGetchar() != 0 )\r
242                 {\r
243                         /* Nothing to do here - the key press is just discarded. */\r
244                 }\r
245 \r
246                 do\r
247                 {\r
248                    prvDisplayAssertion(pcFile, ulLine);\r
249                 } while ( ( ul == pdFALSE ) && ( ucGalileoGetchar() == 0 ) );\r
250 \r
251                 prvClearAssertionLine();\r
252         }\r
253         taskEXIT_CRITICAL();\r
254 }\r
255 /*-----------------------------------------------------------*/\r
256 \r
257 void vApplicationTickHook( void )\r
258 {\r
259         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0 )\r
260         {\r
261                 extern void vTimerPeriodicISRTests( void );\r
262 \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
266 \r
267                 /* Call the periodic queue overwrite from ISR demo. */\r
268                 vQueueOverwritePeriodicISRDemo();\r
269 \r
270                 /* Call the periodic event group from ISR demo. */\r
271                 vPeriodicEventGroupsProcessing();\r
272 \r
273                 /* Call the periodic queue set from ISR demo. */\r
274                 vQueueSetAccessQueueSetFromISR();\r
275 \r
276                 /* Use task notifications from an interrupt. */\r
277                 xNotifyTaskFromISR();\r
278         }\r
279         #endif\r
280 }\r
281 /*-----------------------------------------------------------*/\r
282 \r
283 static void prvSetupHardware( void )\r
284 {\r
285         /* Initialise the serial port and GPIO. */\r
286         vInitializeGalileoSerialPort( DEBUG_SERIAL_PORT );\r
287         vGalileoInitializeGpioController();\r
288         vGalileoInitializeLegacyGPIO();\r
289 \r
290         /* Initialise HPET interrupt(s) */\r
291         #if( ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY != 1 ) && ( hpetHPET_TIMER_IN_USE != 0 ) )\r
292         {\r
293                 portDISABLE_INTERRUPTS();\r
294                 vInitializeAllHPETInterrupts();\r
295         }\r
296         #endif\r
297 \r
298         /* Setup the LED. */\r
299         vGalileoLegacyGPIOInitializationForLED();\r
300 \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
304 \r
305         /* Print RTOS loaded message. */\r
306         vPrintBanner();\r
307 }\r
308 /*-----------------------------------------------------------*/\r
309 \r
310 static void prvLoopToWaitForDebugConnection( void )\r
311 {\r
312         /* Debug if define = 1. */\r
313         #if( mainWAIT_FOR_DEBUG_CONNECTION == 1 )\r
314         {\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
320 \r
321                 /* Must initialize UART before anything will print. */\r
322                 vInitializeGalileoSerialPort( DEBUG_SERIAL_PORT );\r
323 \r
324                 /* RTOS loaded message. */\r
325                 vPrintBanner();\r
326 \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
335 \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
340                 do\r
341                 {\r
342                         portNOP();\r
343 \r
344                 } while( ( ulExitResetSpinLoop == pdFALSE ) && ( ucGalileoGetchar() == 0 ) );\r
345         }\r
346         #endif\r
347 }\r
348 /*-----------------------------------------------------------*/\r
349 \r
350 void CRT_Init( void )\r
351 {\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
358 uint32_t x = 255;\r
359 size_t xSize;\r
360 \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
364 \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
368 \r
369         /* Ensure no interrupts are pending. */\r
370         do\r
371         {\r
372                 portAPIC_EOI = 0;\r
373                 x--;\r
374         } while( x > 0 );\r
375 }\r
376 /*-----------------------------------------------------------*/\r
377 \r
378 static void prvCalibrateLVTimer( void )\r
379 {\r
380 uint32_t uiInitialTimerCounts, uiCalibratedTimerCounts;\r
381 \r
382         /* Disable LAPIC Counter. */\r
383         portAPIC_LVT_TIMER = portAPIC_DISABLE;\r
384 \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
389 \r
390         if( uiCalibratedTimerCounts != 0 )\r
391         {\r
392                 uiInitialTimerCounts = uiCalibratedTimerCounts;\r
393         }\r
394 \r
395         /* Set the interrupt frequency. */\r
396         portAPIC_TMRDIV = portAPIC_DIV_16;\r
397         portAPIC_TIMER_INITIAL_COUNT = uiInitialTimerCounts;\r
398 \r
399         /* Enable LAPIC Counter. */\r
400         portAPIC_LVT_TIMER = portAPIC_TIMER_PERIODIC | portAPIC_TIMER_INT_VECTOR;\r
401 \r
402         /* Sometimes needed. */\r
403         portAPIC_TMRDIV = portAPIC_DIV_16;\r
404 }\r