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