]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32F107_GCC_Rowley/main.c
Update license information text files for the CLI, TCP and UDP products to be correct...
[freertos] / FreeRTOS / Demo / CORTEX_STM32F107_GCC_Rowley / 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 /*\r
31  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
32  * documentation provides more details of the standard demo application tasks\r
33  * (which just exist to test the kernel port and provide an example of how to use\r
34  * each FreeRTOS API function).\r
35  *\r
36  * In addition to the standard demo tasks, the following tasks and tests are\r
37  * defined and/or created within this file:\r
38  *\r
39  * "LCD" task - the LCD task is a 'gatekeeper' task.  It is the only task that\r
40  * is permitted to access the display directly.  Other tasks wishing to write a\r
41  * message to the LCD send the message on a queue to the LCD task instead of\r
42  * accessing the LCD themselves.  The LCD task just blocks on the queue waiting\r
43  * for messages - waking and displaying the messages as they arrive.  The use\r
44  * of a gatekeeper in this manner permits both tasks and interrupts to write to\r
45  * the LCD without worrying about mutual exclusion.  This is demonstrated by the\r
46  * check hook (see below) which sends messages to the display even though it\r
47  * executes from an interrupt context.\r
48  *\r
49  * "Check" hook -  This only executes fully every five seconds from the tick\r
50  * hook.  Its main function is to check that all the standard demo tasks are\r
51  * still operational.  Should any unexpected behaviour be discovered within a\r
52  * demo task then the tick hook will write an error to the LCD (via the LCD task).\r
53  * If all the demo tasks are executing with their expected behaviour then the\r
54  * check task writes PASS to the LCD (again via the LCD task), as described above.\r
55  *\r
56  * LED tasks - These just demonstrate how multiple instances of a single task\r
57  * definition can be created.  Each LED task simply toggles an LED.  The task\r
58  * parameter is used to pass the number of the LED to be toggled into the task.\r
59  *\r
60  * "uIP" task -  This is the task that handles the uIP stack.  All TCP/IP\r
61  * processing is performed in this task.\r
62  *\r
63  * "Fast Interrupt Test" - A high frequency periodic interrupt is generated\r
64  * using a free running timer to demonstrate the use of the\r
65  * configKERNEL_INTERRUPT_PRIORITY configuration constant.  The interrupt\r
66  * service routine measures the number of processor clocks that occur between\r
67  * each interrupt - and in so doing measures the jitter in the interrupt timing.\r
68  * The maximum measured jitter time is latched in the ulMaxJitter variable, and\r
69  * displayed on the OLED display by the 'OLED' task as described below.  The\r
70  * fast interrupt is configured and handled in the timertest.c source file.\r
71  *\r
72  */\r
73 \r
74 /* Standard includes. */\r
75 #include <stdio.h>\r
76 \r
77 /* Scheduler includes. */\r
78 #include "FreeRTOS.h"\r
79 #include "task.h"\r
80 #include "queue.h"\r
81 #include "semphr.h"\r
82 \r
83 /* Library includes. */\r
84 #include "stm32f10x_it.h"\r
85 #include "stm32f10x_tim.h"\r
86 #include "STM3210D_lcd.h"\r
87 \r
88 /* Demo app includes. */\r
89 #include "BlockQ.h"\r
90 #include "integer.h"\r
91 #include "flash.h"\r
92 #include "partest.h"\r
93 #include "semtest.h"\r
94 #include "PollQ.h"\r
95 #include "GenQTest.h"\r
96 #include "QPeek.h"\r
97 #include "recmutex.h"\r
98 \r
99 \r
100 /* The time between cycles of the 'check' functionality (defined within the\r
101 tick hook. */\r
102 #define mainCHECK_DELAY                                         ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )\r
103 \r
104 /* Task priorities. */\r
105 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
106 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
107 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
108 #define mainUIP_TASK_PRIORITY                           ( tskIDLE_PRIORITY + 3 )\r
109 #define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
110 #define mainLCD_TASK_PRIORITY                           ( tskIDLE_PRIORITY + 3 )\r
111 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
112 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
113 \r
114 /* The WEB server has a larger stack as it utilises stack hungry string\r
115 handling library calls. */\r
116 #define mainBASIC_WEB_STACK_SIZE            ( configMINIMAL_STACK_SIZE * 4 )\r
117 \r
118 /* The length of the queue used to send messages to the LCD task. */\r
119 #define mainQUEUE_SIZE                                          ( 3 )\r
120 \r
121 /* The period of the system clock in nano seconds.  This is used to calculate\r
122 the jitter time in nano seconds. */\r
123 #define mainNS_PER_CLOCK                                        ( ( unsigned long ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )\r
124 \r
125 /*-----------------------------------------------------------*/\r
126 \r
127 /*\r
128  * Configure the hardware for the demo.\r
129  */\r
130 static void prvSetupHardware( void );\r
131 \r
132 /*\r
133  * Very simple task that toggles an LED.\r
134  */\r
135 static void prvLCDTask( void *pvparameters );\r
136 \r
137 /*\r
138  * The task that handles the uIP stack.  All TCP/IP processing is performed in\r
139  * this task.\r
140  */\r
141 extern void vuIP_Task( void *pvParameters );\r
142 \r
143 /*\r
144  * The LCD gatekeeper task as described in the comments at the top of this file.\r
145  * */\r
146 static void prvLCDTask( void *pvParameters );\r
147 \r
148 /*\r
149  * Configures the high frequency timers - those used to measure the timing\r
150  * jitter while the real time kernel is executing.\r
151  */\r
152 extern void vSetupHighFrequencyTimer( void );\r
153 \r
154 /*-----------------------------------------------------------*/\r
155 \r
156 /* The queue used to send messages to the LCD task. */\r
157 QueueHandle_t xLCDQueue;\r
158 \r
159 /*-----------------------------------------------------------*/\r
160 \r
161 int main( void )\r
162 {\r
163 #ifdef DEBUG\r
164   debug();\r
165 #endif\r
166 \r
167         prvSetupHardware();\r
168 \r
169         /* Start the standard demo tasks.  These are just here to exercise the\r
170         kernel port and provide examples of how the FreeRTOS API can be used. */\r
171         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
172     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
173     vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
174     vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
175     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
176         vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
177     vStartQueuePeekTasks();\r
178     vStartRecursiveMutexTasks();\r
179 \r
180         /* Create the uIP task.  The WEB server runs in this task. */\r
181     xTaskCreate( vuIP_Task, "uIP", mainBASIC_WEB_STACK_SIZE, ( void * ) NULL, mainUIP_TASK_PRIORITY, NULL );\r
182 \r
183         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
184         are received via this queue. */\r
185         xLCDQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( char * ) );\r
186 \r
187         /* Start the LCD gatekeeper task - as described in the comments at the top\r
188         of this file. */\r
189         xTaskCreate( prvLCDTask, "LCD", configMINIMAL_STACK_SIZE * 2, NULL, mainLCD_TASK_PRIORITY, NULL );\r
190 \r
191         /* Configure the high frequency interrupt used to measure the interrupt\r
192         jitter time.  When debugging it can be helpful to comment this line out\r
193         to prevent the debugger repeatedly going into the interrupt service\r
194         routine. */\r
195         vSetupHighFrequencyTimer();\r
196 \r
197     /* Start the scheduler. */\r
198         vTaskStartScheduler();\r
199 \r
200     /* Will only get here if there was insufficient memory to create the idle\r
201     task.  The idle task is created within vTaskStartScheduler(). */\r
202         for( ;; );\r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 static void prvLCDTask( void *pvParameters )\r
207 {\r
208 unsigned char *pucMessage;\r
209 unsigned long ulLine = Line3;\r
210 const unsigned long ulLineHeight = 24;\r
211 static char cMsgBuf[ 30 ];\r
212 extern unsigned short usMaxJitter;\r
213 \r
214         ( void ) pvParameters;\r
215 \r
216         /* The LCD gatekeeper task as described in the comments at the top of this\r
217         file. */\r
218 \r
219         /* Initialise the LCD and display a startup message that includes the\r
220         configured IP address. */\r
221         STM3210D_LCD_Init();\r
222         LCD_Clear(White);\r
223         LCD_SetTextColor(Green);\r
224         LCD_DisplayStringLine( Line0, ( unsigned char * ) "  www.FreeRTOS.org" );\r
225     LCD_SetTextColor(Blue);\r
226     sprintf( cMsgBuf, "  %d.%d.%d.%d", configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
227         LCD_DisplayStringLine( Line1, ( unsigned char * ) cMsgBuf );\r
228         LCD_SetTextColor(Black);\r
229 \r
230         for( ;; )\r
231         {\r
232                 /* Wait for a message to arrive to be displayed. */\r
233                 xQueueReceive( xLCDQueue, &pucMessage, portMAX_DELAY );\r
234 \r
235                 /* Clear the current line of text. */\r
236                 LCD_ClearLine( ulLine );\r
237 \r
238                 /* Move on to the next line. */\r
239                 ulLine += ulLineHeight;\r
240                 if( ulLine > Line9 )\r
241                 {\r
242                         ulLine = Line3;\r
243                 }\r
244 \r
245                 /* Display the received text, and the max jitter value. */\r
246                 sprintf( cMsgBuf, "%s [%uns]", pucMessage, usMaxJitter * mainNS_PER_CLOCK );\r
247                 LCD_DisplayStringLine( ulLine, ( unsigned char * ) cMsgBuf );\r
248         }\r
249 }\r
250 /*-----------------------------------------------------------*/\r
251 \r
252 static void prvSetupHardware( void )\r
253 {\r
254         /* Start with the clocks in their expected state. */\r
255         RCC_DeInit();\r
256 \r
257         /* Enable HSE (high speed external clock). */\r
258         RCC_HSEConfig( RCC_HSE_ON );\r
259 \r
260         /* Wait till HSE is ready. */\r
261         while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )\r
262         {\r
263         }\r
264 \r
265         /* 2 wait states required on the flash. */\r
266         *( ( unsigned long * ) 0x40022000 ) = 0x02;\r
267 \r
268         /* HCLK = SYSCLK */\r
269         RCC_HCLKConfig( RCC_SYSCLK_Div1 );\r
270 \r
271         /* PCLK2 = HCLK */\r
272         RCC_PCLK2Config( RCC_HCLK_Div1 );\r
273 \r
274         /* PCLK1 = HCLK/2 */\r
275         RCC_PCLK1Config( RCC_HCLK_Div2 );\r
276 \r
277         /* PLLCLK = (25MHz / 2 ) * 5 = 62.5 MHz. */\r
278         RCC_PLLConfig( RCC_PLLSource_HSE_Div2, RCC_PLLMul_5 );\r
279 \r
280         /* Enable PLL. */\r
281         RCC_PLLCmd( ENABLE );\r
282 \r
283         /* Wait till PLL is ready. */\r
284         while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)\r
285         {\r
286         }\r
287 \r
288         /* Select PLL as system clock source. */\r
289         RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );\r
290 \r
291         /* Wait till PLL is used as system clock source. */\r
292         while( RCC_GetSYSCLKSource() != 0x08 )\r
293         {\r
294         }\r
295 \r
296         /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */\r
297         RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC\r
298                                                         | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );\r
299 \r
300         /* Set the Vector Table base address at 0x08000000 */\r
301         NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );\r
302 \r
303         NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );\r
304 \r
305         /* Configure HCLK clock as SysTick clock source. */\r
306         SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );\r
307 \r
308         /* Initialise the IO used for the LED outputs. */\r
309         vParTestInitialise();\r
310 }\r
311 /*-----------------------------------------------------------*/\r
312 \r
313 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
314 {\r
315         /* This function will get called if a task overflows its stack.   If the\r
316         parameters are corrupt then inspect pxCurrentTCB to find which was the\r
317         offending task. */\r
318 \r
319         ( void ) pxTask;\r
320         ( void ) pcTaskName;\r
321 \r
322         for( ;; );\r
323 }\r
324 /*-----------------------------------------------------------*/\r
325 \r
326 void vApplicationTickHook( void )\r
327 {\r
328 char *pcMessage = "Status: PASS";\r
329 static unsigned long ulTicksSinceLastDisplay = 0;\r
330 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
331 \r
332         /* Called from every tick interrupt as described in the comments at the top\r
333         of this file.\r
334 \r
335         Have enough ticks passed to make it     time to perform our health status\r
336         check again? */\r
337         ulTicksSinceLastDisplay++;\r
338         if( ulTicksSinceLastDisplay >= mainCHECK_DELAY )\r
339         {\r
340                 /* Reset the counter so these checks run again in mainCHECK_DELAY\r
341                 ticks time. */\r
342                 ulTicksSinceLastDisplay = 0;\r
343 \r
344                 /* Has an error been found in any task? */\r
345                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
346                 {\r
347                         pcMessage = "ERROR: GEN Q";\r
348                 }\r
349                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
350                 {\r
351                         pcMessage = "ERROR: PEEK Q";\r
352                 }\r
353                 else if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
354                 {\r
355                         pcMessage = "ERROR: BLOCK Q";\r
356                 }\r
357             else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
358             {\r
359                 pcMessage = "ERROR: SEMAPHR";\r
360             }\r
361             else if( xArePollingQueuesStillRunning() != pdTRUE )\r
362             {\r
363                 pcMessage = "ERROR: POLL Q";\r
364             }\r
365             else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
366             {\r
367                 pcMessage = "ERROR: INT MATH";\r
368             }\r
369             else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
370             {\r
371                 pcMessage = "ERROR: REC MUTEX";\r
372             }\r
373 \r
374                 /* Send the message to the OLED gatekeeper for display.  The\r
375                 xHigherPriorityTaskWoken parameter is not actually used here\r
376                 as this function is running in the tick interrupt anyway - but\r
377                 it must still be supplied. */\r
378                 xHigherPriorityTaskWoken = pdFALSE;\r
379                 xQueueSendFromISR( xLCDQueue, &pcMessage, &xHigherPriorityTaskWoken );\r
380         }\r
381 }\r
382 /*-----------------------------------------------------------*/\r