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