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