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