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