]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_STM32F107_GCC_Rowley/main.c
4a36a582372df67511d9e1ed0eaed8ae5bd96581
[freertos] / FreeRTOS / Demo / CORTEX_STM32F107_GCC_Rowley / main.c
1 /*\r
2     FreeRTOS V9.0.0rc1 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 \r
71 /*\r
72  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
73  * documentation provides more details of the standard demo application tasks\r
74  * (which just exist to test the kernel port and provide an example of how to use\r
75  * each FreeRTOS API function).\r
76  *\r
77  * In addition to the standard demo tasks, the following tasks and tests are\r
78  * defined and/or created within this file:\r
79  *\r
80  * "LCD" task - the LCD task is a 'gatekeeper' task.  It is the only task that\r
81  * is permitted to access the display directly.  Other tasks wishing to write a\r
82  * message to the LCD send the message on a queue to the LCD task instead of\r
83  * accessing the LCD themselves.  The LCD task just blocks on the queue waiting\r
84  * for messages - waking and displaying the messages as they arrive.  The use\r
85  * of a gatekeeper in this manner permits both tasks and interrupts to write to\r
86  * the LCD without worrying about mutual exclusion.  This is demonstrated by the\r
87  * check hook (see below) which sends messages to the display even though it\r
88  * executes from an interrupt context.\r
89  *\r
90  * "Check" hook -  This only executes fully every five seconds from the tick\r
91  * hook.  Its main function is to check that all the standard demo tasks are\r
92  * still operational.  Should any unexpected behaviour be discovered within a\r
93  * demo task then the tick hook will write an error to the LCD (via the LCD task).\r
94  * If all the demo tasks are executing with their expected behaviour then the\r
95  * check task writes PASS to the LCD (again via the LCD task), as described above.\r
96  *\r
97  * LED tasks - These just demonstrate how multiple instances of a single task\r
98  * definition can be created.  Each LED task simply toggles an LED.  The task\r
99  * parameter is used to pass the number of the LED to be toggled into the task.\r
100  *\r
101  * "uIP" task -  This is the task that handles the uIP stack.  All TCP/IP\r
102  * processing is performed in this task.\r
103  *\r
104  * "Fast Interrupt Test" - A high frequency periodic interrupt is generated\r
105  * using a free running timer to demonstrate the use of the\r
106  * configKERNEL_INTERRUPT_PRIORITY configuration constant.  The interrupt\r
107  * service routine measures the number of processor clocks that occur between\r
108  * each interrupt - and in so doing measures the jitter in the interrupt timing.\r
109  * The maximum measured jitter time is latched in the ulMaxJitter variable, and\r
110  * displayed on the OLED display by the 'OLED' task as described below.  The\r
111  * fast interrupt is configured and handled in the timertest.c source file.\r
112  *\r
113  */\r
114 \r
115 /* Standard includes. */\r
116 #include <stdio.h>\r
117 \r
118 /* Scheduler includes. */\r
119 #include "FreeRTOS.h"\r
120 #include "task.h"\r
121 #include "queue.h"\r
122 #include "semphr.h"\r
123 \r
124 /* Library includes. */\r
125 #include "stm32f10x_it.h"\r
126 #include "stm32f10x_tim.h"\r
127 #include "STM3210D_lcd.h"\r
128 \r
129 /* Demo app includes. */\r
130 #include "BlockQ.h"\r
131 #include "integer.h"\r
132 #include "flash.h"\r
133 #include "partest.h"\r
134 #include "semtest.h"\r
135 #include "PollQ.h"\r
136 #include "GenQTest.h"\r
137 #include "QPeek.h"\r
138 #include "recmutex.h"\r
139 \r
140 \r
141 /* The time between cycles of the 'check' functionality (defined within the\r
142 tick hook. */\r
143 #define mainCHECK_DELAY                                         ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )\r
144 \r
145 /* Task priorities. */\r
146 #define mainQUEUE_POLL_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
147 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
148 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
149 #define mainUIP_TASK_PRIORITY                           ( tskIDLE_PRIORITY + 3 )\r
150 #define mainFLASH_TASK_PRIORITY                         ( tskIDLE_PRIORITY + 2 )\r
151 #define mainLCD_TASK_PRIORITY                           ( tskIDLE_PRIORITY + 3 )\r
152 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
153 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
154 \r
155 /* The WEB server has a larger stack as it utilises stack hungry string\r
156 handling library calls. */\r
157 #define mainBASIC_WEB_STACK_SIZE            ( configMINIMAL_STACK_SIZE * 4 )\r
158 \r
159 /* The length of the queue used to send messages to the LCD task. */\r
160 #define mainQUEUE_SIZE                                          ( 3 )\r
161 \r
162 /* The period of the system clock in nano seconds.  This is used to calculate\r
163 the jitter time in nano seconds. */\r
164 #define mainNS_PER_CLOCK                                        ( ( unsigned long ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )\r
165 \r
166 /*-----------------------------------------------------------*/\r
167 \r
168 /*\r
169  * Configure the hardware for the demo.\r
170  */\r
171 static void prvSetupHardware( void );\r
172 \r
173 /*\r
174  * Very simple task that toggles an LED.\r
175  */\r
176 static void prvLCDTask( void *pvparameters );\r
177 \r
178 /*\r
179  * The task that handles the uIP stack.  All TCP/IP processing is performed in\r
180  * this task.\r
181  */\r
182 extern void vuIP_Task( void *pvParameters );\r
183 \r
184 /*\r
185  * The LCD gatekeeper task as described in the comments at the top of this file.\r
186  * */\r
187 static void prvLCDTask( void *pvParameters );\r
188 \r
189 /*\r
190  * Configures the high frequency timers - those used to measure the timing\r
191  * jitter while the real time kernel is executing.\r
192  */\r
193 extern void vSetupHighFrequencyTimer( void );\r
194 \r
195 /*-----------------------------------------------------------*/\r
196 \r
197 /* The queue used to send messages to the LCD task. */\r
198 QueueHandle_t xLCDQueue;\r
199 \r
200 /*-----------------------------------------------------------*/\r
201 \r
202 int main( void )\r
203 {\r
204 #ifdef DEBUG\r
205   debug();\r
206 #endif\r
207 \r
208         prvSetupHardware();\r
209 \r
210         /* Start the standard demo tasks.  These are just here to exercise the\r
211         kernel port and provide examples of how the FreeRTOS API can be used. */\r
212         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
213     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
214     vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
215     vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );\r
216     vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
217         vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );\r
218     vStartQueuePeekTasks();\r
219     vStartRecursiveMutexTasks();\r
220 \r
221         /* Create the uIP task.  The WEB server runs in this task. */\r
222     xTaskCreate( vuIP_Task, "uIP", mainBASIC_WEB_STACK_SIZE, ( void * ) NULL, mainUIP_TASK_PRIORITY, NULL );\r
223 \r
224         /* Create the queue used by the LCD task.  Messages for display on the LCD\r
225         are received via this queue. */\r
226         xLCDQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( char * ) );\r
227 \r
228         /* Start the LCD gatekeeper task - as described in the comments at the top\r
229         of this file. */\r
230         xTaskCreate( prvLCDTask, "LCD", configMINIMAL_STACK_SIZE * 2, NULL, mainLCD_TASK_PRIORITY, NULL );\r
231 \r
232         /* Configure the high frequency interrupt used to measure the interrupt\r
233         jitter time.  When debugging it can be helpful to comment this line out\r
234         to prevent the debugger repeatedly going into the interrupt service\r
235         routine. */\r
236         vSetupHighFrequencyTimer();\r
237 \r
238     /* Start the scheduler. */\r
239         vTaskStartScheduler();\r
240 \r
241     /* Will only get here if there was insufficient memory to create the idle\r
242     task.  The idle task is created within vTaskStartScheduler(). */\r
243         for( ;; );\r
244 }\r
245 /*-----------------------------------------------------------*/\r
246 \r
247 static void prvLCDTask( void *pvParameters )\r
248 {\r
249 unsigned char *pucMessage;\r
250 unsigned long ulLine = Line3;\r
251 const unsigned long ulLineHeight = 24;\r
252 static char cMsgBuf[ 30 ];\r
253 extern unsigned short usMaxJitter;\r
254 \r
255         ( void ) pvParameters;\r
256 \r
257         /* The LCD gatekeeper task as described in the comments at the top of this\r
258         file. */\r
259 \r
260         /* Initialise the LCD and display a startup message that includes the\r
261         configured IP address. */\r
262         STM3210D_LCD_Init();\r
263         LCD_Clear(White);\r
264         LCD_SetTextColor(Green);\r
265         LCD_DisplayStringLine( Line0, ( unsigned char * ) "  www.FreeRTOS.org" );\r
266     LCD_SetTextColor(Blue);\r
267     sprintf( cMsgBuf, "  %d.%d.%d.%d", configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );\r
268         LCD_DisplayStringLine( Line1, ( unsigned char * ) cMsgBuf );\r
269         LCD_SetTextColor(Black);\r
270 \r
271         for( ;; )\r
272         {\r
273                 /* Wait for a message to arrive to be displayed. */\r
274                 xQueueReceive( xLCDQueue, &pucMessage, portMAX_DELAY );\r
275 \r
276                 /* Clear the current line of text. */\r
277                 LCD_ClearLine( ulLine );\r
278 \r
279                 /* Move on to the next line. */\r
280                 ulLine += ulLineHeight;\r
281                 if( ulLine > Line9 )\r
282                 {\r
283                         ulLine = Line3;\r
284                 }\r
285 \r
286                 /* Display the received text, and the max jitter value. */\r
287                 sprintf( cMsgBuf, "%s [%uns]", pucMessage, usMaxJitter * mainNS_PER_CLOCK );\r
288                 LCD_DisplayStringLine( ulLine, ( unsigned char * ) cMsgBuf );\r
289         }\r
290 }\r
291 /*-----------------------------------------------------------*/\r
292 \r
293 static void prvSetupHardware( void )\r
294 {\r
295         /* Start with the clocks in their expected state. */\r
296         RCC_DeInit();\r
297 \r
298         /* Enable HSE (high speed external clock). */\r
299         RCC_HSEConfig( RCC_HSE_ON );\r
300 \r
301         /* Wait till HSE is ready. */\r
302         while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )\r
303         {\r
304         }\r
305 \r
306         /* 2 wait states required on the flash. */\r
307         *( ( unsigned long * ) 0x40022000 ) = 0x02;\r
308 \r
309         /* HCLK = SYSCLK */\r
310         RCC_HCLKConfig( RCC_SYSCLK_Div1 );\r
311 \r
312         /* PCLK2 = HCLK */\r
313         RCC_PCLK2Config( RCC_HCLK_Div1 );\r
314 \r
315         /* PCLK1 = HCLK/2 */\r
316         RCC_PCLK1Config( RCC_HCLK_Div2 );\r
317 \r
318         /* PLLCLK = (25MHz / 2 ) * 5 = 62.5 MHz. */\r
319         RCC_PLLConfig( RCC_PLLSource_HSE_Div2, RCC_PLLMul_5 );\r
320 \r
321         /* Enable PLL. */\r
322         RCC_PLLCmd( ENABLE );\r
323 \r
324         /* Wait till PLL is ready. */\r
325         while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)\r
326         {\r
327         }\r
328 \r
329         /* Select PLL as system clock source. */\r
330         RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );\r
331 \r
332         /* Wait till PLL is used as system clock source. */\r
333         while( RCC_GetSYSCLKSource() != 0x08 )\r
334         {\r
335         }\r
336 \r
337         /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */\r
338         RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC\r
339                                                         | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );\r
340 \r
341         /* Set the Vector Table base address at 0x08000000 */\r
342         NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );\r
343 \r
344         NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );\r
345 \r
346         /* Configure HCLK clock as SysTick clock source. */\r
347         SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );\r
348 \r
349         /* Initialise the IO used for the LED outputs. */\r
350         vParTestInitialise();\r
351 }\r
352 /*-----------------------------------------------------------*/\r
353 \r
354 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
355 {\r
356         /* This function will get called if a task overflows its stack.   If the\r
357         parameters are corrupt then inspect pxCurrentTCB to find which was the\r
358         offending task. */\r
359 \r
360         ( void ) pxTask;\r
361         ( void ) pcTaskName;\r
362 \r
363         for( ;; );\r
364 }\r
365 /*-----------------------------------------------------------*/\r
366 \r
367 void vApplicationTickHook( void )\r
368 {\r
369 char *pcMessage = "Status: PASS";\r
370 static unsigned long ulTicksSinceLastDisplay = 0;\r
371 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
372 \r
373         /* Called from every tick interrupt as described in the comments at the top\r
374         of this file.\r
375 \r
376         Have enough ticks passed to make it     time to perform our health status\r
377         check again? */\r
378         ulTicksSinceLastDisplay++;\r
379         if( ulTicksSinceLastDisplay >= mainCHECK_DELAY )\r
380         {\r
381                 /* Reset the counter so these checks run again in mainCHECK_DELAY\r
382                 ticks time. */\r
383                 ulTicksSinceLastDisplay = 0;\r
384 \r
385                 /* Has an error been found in any task? */\r
386                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
387                 {\r
388                         pcMessage = "ERROR: GEN Q";\r
389                 }\r
390                 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
391                 {\r
392                         pcMessage = "ERROR: PEEK Q";\r
393                 }\r
394                 else if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
395                 {\r
396                         pcMessage = "ERROR: BLOCK Q";\r
397                 }\r
398             else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
399             {\r
400                 pcMessage = "ERROR: SEMAPHR";\r
401             }\r
402             else if( xArePollingQueuesStillRunning() != pdTRUE )\r
403             {\r
404                 pcMessage = "ERROR: POLL Q";\r
405             }\r
406             else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
407             {\r
408                 pcMessage = "ERROR: INT MATH";\r
409             }\r
410             else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
411             {\r
412                 pcMessage = "ERROR: REC MUTEX";\r
413             }\r
414 \r
415                 /* Send the message to the OLED gatekeeper for display.  The\r
416                 xHigherPriorityTaskWoken parameter is not actually used here\r
417                 as this function is running in the tick interrupt anyway - but\r
418                 it must still be supplied. */\r
419                 xHigherPriorityTaskWoken = pdFALSE;\r
420                 xQueueSendFromISR( xLCDQueue, &pcMessage, &xHigherPriorityTaskWoken );\r
421         }\r
422 }\r
423 /*-----------------------------------------------------------*/\r