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