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