2 FreeRTOS V7.4.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
77 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
78 * documentation provides more details of the standard demo application tasks
\r
79 * (which just exist to test the kernel port and provide an example of how to use
\r
80 * each FreeRTOS API function).
\r
82 * In addition to the standard demo tasks, the following tasks and tests are
\r
83 * defined and/or created within this file:
\r
85 * "LCD" task - the LCD task is a 'gatekeeper' task. It is the only task that
\r
86 * is permitted to access the display directly. Other tasks wishing to write a
\r
87 * message to the LCD send the message on a queue to the LCD task instead of
\r
88 * accessing the LCD themselves. The LCD task just blocks on the queue waiting
\r
89 * for messages - waking and displaying the messages as they arrive. The use
\r
90 * of a gatekeeper in this manner permits both tasks and interrupts to write to
\r
91 * the LCD without worrying about mutual exclusion. This is demonstrated by the
\r
92 * check hook (see below) which sends messages to the display even though it
\r
93 * executes from an interrupt context.
\r
95 * "Check" hook - This only executes fully every five seconds from the tick
\r
96 * hook. Its main function is to check that all the standard demo tasks are
\r
97 * still operational. Should any unexpected behaviour be discovered within a
\r
98 * demo task then the tick hook will write an error to the LCD (via the LCD task).
\r
99 * If all the demo tasks are executing with their expected behaviour then the
\r
100 * check task writes PASS to the LCD (again via the LCD task), as described above.
\r
102 * LED tasks - These just demonstrate how multiple instances of a single task
\r
103 * definition can be created. Each LED task simply toggles an LED. The task
\r
104 * parameter is used to pass the number of the LED to be toggled into the task.
\r
106 * "uIP" task - This is the task that handles the uIP stack. All TCP/IP
\r
107 * processing is performed in this task.
\r
109 * "Fast Interrupt Test" - A high frequency periodic interrupt is generated
\r
110 * using a free running timer to demonstrate the use of the
\r
111 * configKERNEL_INTERRUPT_PRIORITY configuration constant. The interrupt
\r
112 * service routine measures the number of processor clocks that occur between
\r
113 * each interrupt - and in so doing measures the jitter in the interrupt timing.
\r
114 * The maximum measured jitter time is latched in the ulMaxJitter variable, and
\r
115 * displayed on the OLED display by the 'OLED' task as described below. The
\r
116 * fast interrupt is configured and handled in the timertest.c source file.
\r
120 /* Standard includes. */
\r
123 /* Scheduler includes. */
\r
124 #include "FreeRTOS.h"
\r
127 #include "semphr.h"
\r
129 /* Library includes. */
\r
130 #include "stm32f10x_it.h"
\r
131 #include "stm32f10x_tim.h"
\r
132 #include "STM3210D_lcd.h"
\r
134 /* Demo app includes. */
\r
135 #include "BlockQ.h"
\r
136 #include "integer.h"
\r
138 #include "partest.h"
\r
139 #include "semtest.h"
\r
141 #include "GenQTest.h"
\r
143 #include "recmutex.h"
\r
146 /* The time between cycles of the 'check' functionality (defined within the
\r
148 #define mainCHECK_DELAY ( ( portTickType ) 5000 / portTICK_RATE_MS )
\r
150 /* Task priorities. */
\r
151 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
152 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
153 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
154 #define mainUIP_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
155 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
156 #define mainLCD_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
157 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
158 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
160 /* The WEB server has a larger stack as it utilises stack hungry string
\r
161 handling library calls. */
\r
162 #define mainBASIC_WEB_STACK_SIZE ( configMINIMAL_STACK_SIZE * 4 )
\r
164 /* The length of the queue used to send messages to the LCD task. */
\r
165 #define mainQUEUE_SIZE ( 3 )
\r
167 /* The period of the system clock in nano seconds. This is used to calculate
\r
168 the jitter time in nano seconds. */
\r
169 #define mainNS_PER_CLOCK ( ( unsigned long ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )
\r
171 /*-----------------------------------------------------------*/
\r
174 * Configure the hardware for the demo.
\r
176 static void prvSetupHardware( void );
\r
179 * Very simple task that toggles an LED.
\r
181 static void prvLCDTask( void *pvparameters );
\r
184 * The task that handles the uIP stack. All TCP/IP processing is performed in
\r
187 extern void vuIP_Task( void *pvParameters );
\r
190 * The LCD gatekeeper task as described in the comments at the top of this file.
\r
192 static void prvLCDTask( void *pvParameters );
\r
195 * Configures the high frequency timers - those used to measure the timing
\r
196 * jitter while the real time kernel is executing.
\r
198 extern void vSetupHighFrequencyTimer( void );
\r
200 /*-----------------------------------------------------------*/
\r
202 /* The queue used to send messages to the LCD task. */
\r
203 xQueueHandle xLCDQueue;
\r
205 /*-----------------------------------------------------------*/
\r
213 prvSetupHardware();
\r
215 /* Start the standard demo tasks. These are just here to exercise the
\r
216 kernel port and provide examples of how the FreeRTOS API can be used. */
\r
217 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
218 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
219 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
220 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
221 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
222 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
223 vStartQueuePeekTasks();
\r
224 vStartRecursiveMutexTasks();
\r
226 /* Create the uIP task. The WEB server runs in this task. */
\r
227 xTaskCreate( vuIP_Task, ( signed char * ) "uIP", mainBASIC_WEB_STACK_SIZE, ( void * ) NULL, mainUIP_TASK_PRIORITY, NULL );
\r
229 /* Create the queue used by the LCD task. Messages for display on the LCD
\r
230 are received via this queue. */
\r
231 xLCDQueue = xQueueCreate( mainQUEUE_SIZE, sizeof( char * ) );
\r
233 /* Start the LCD gatekeeper task - as described in the comments at the top
\r
235 xTaskCreate( prvLCDTask, ( signed char * ) "LCD", configMINIMAL_STACK_SIZE * 2, NULL, mainLCD_TASK_PRIORITY, NULL );
\r
237 /* Configure the high frequency interrupt used to measure the interrupt
\r
238 jitter time. When debugging it can be helpful to comment this line out
\r
239 to prevent the debugger repeatedly going into the interrupt service
\r
241 vSetupHighFrequencyTimer();
\r
243 /* Start the scheduler. */
\r
244 vTaskStartScheduler();
\r
246 /* Will only get here if there was insufficient memory to create the idle
\r
247 task. The idle task is created within vTaskStartScheduler(). */
\r
250 /*-----------------------------------------------------------*/
\r
252 static void prvLCDTask( void *pvParameters )
\r
254 unsigned char *pucMessage;
\r
255 unsigned long ulLine = Line3;
\r
256 const unsigned long ulLineHeight = 24;
\r
257 static char cMsgBuf[ 30 ];
\r
258 extern unsigned short usMaxJitter;
\r
260 ( void ) pvParameters;
\r
262 /* The LCD gatekeeper task as described in the comments at the top of this
\r
265 /* Initialise the LCD and display a startup message that includes the
\r
266 configured IP address. */
\r
267 STM3210D_LCD_Init();
\r
269 LCD_SetTextColor(Green);
\r
270 LCD_DisplayStringLine( Line0, ( unsigned char * ) " www.FreeRTOS.org" );
\r
271 LCD_SetTextColor(Blue);
\r
272 sprintf( cMsgBuf, " %d.%d.%d.%d", configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
273 LCD_DisplayStringLine( Line1, ( unsigned char * ) cMsgBuf );
\r
274 LCD_SetTextColor(Black);
\r
278 /* Wait for a message to arrive to be displayed. */
\r
279 xQueueReceive( xLCDQueue, &pucMessage, portMAX_DELAY );
\r
281 /* Clear the current line of text. */
\r
282 LCD_ClearLine( ulLine );
\r
284 /* Move on to the next line. */
\r
285 ulLine += ulLineHeight;
\r
286 if( ulLine > Line9 )
\r
291 /* Display the received text, and the max jitter value. */
\r
292 sprintf( cMsgBuf, "%s [%uns]", pucMessage, usMaxJitter * mainNS_PER_CLOCK );
\r
293 LCD_DisplayStringLine( ulLine, ( unsigned char * ) cMsgBuf );
\r
296 /*-----------------------------------------------------------*/
\r
298 static void prvSetupHardware( void )
\r
300 /* Start with the clocks in their expected state. */
\r
303 /* Enable HSE (high speed external clock). */
\r
304 RCC_HSEConfig( RCC_HSE_ON );
\r
306 /* Wait till HSE is ready. */
\r
307 while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )
\r
311 /* 2 wait states required on the flash. */
\r
312 *( ( unsigned long * ) 0x40022000 ) = 0x02;
\r
314 /* HCLK = SYSCLK */
\r
315 RCC_HCLKConfig( RCC_SYSCLK_Div1 );
\r
318 RCC_PCLK2Config( RCC_HCLK_Div1 );
\r
320 /* PCLK1 = HCLK/2 */
\r
321 RCC_PCLK1Config( RCC_HCLK_Div2 );
\r
323 /* PLLCLK = (25MHz / 2 ) * 5 = 62.5 MHz. */
\r
324 RCC_PLLConfig( RCC_PLLSource_HSE_Div2, RCC_PLLMul_5 );
\r
327 RCC_PLLCmd( ENABLE );
\r
329 /* Wait till PLL is ready. */
\r
330 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
\r
334 /* Select PLL as system clock source. */
\r
335 RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );
\r
337 /* Wait till PLL is used as system clock source. */
\r
338 while( RCC_GetSYSCLKSource() != 0x08 )
\r
342 /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
\r
343 RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
\r
344 | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );
\r
346 /* Set the Vector Table base address at 0x08000000 */
\r
347 NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );
\r
349 NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
\r
351 /* Configure HCLK clock as SysTick clock source. */
\r
352 SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );
\r
354 /* Initialise the IO used for the LED outputs. */
\r
355 vParTestInitialise();
\r
357 /*-----------------------------------------------------------*/
\r
359 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )
\r
361 /* This function will get called if a task overflows its stack. If the
\r
362 parameters are corrupt then inspect pxCurrentTCB to find which was the
\r
366 ( void ) pcTaskName;
\r
370 /*-----------------------------------------------------------*/
\r
372 void vApplicationTickHook( void )
\r
374 char *pcMessage = "Status: PASS";
\r
375 static unsigned long ulTicksSinceLastDisplay = 0;
\r
376 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
378 /* Called from every tick interrupt as described in the comments at the top
\r
381 Have enough ticks passed to make it time to perform our health status
\r
383 ulTicksSinceLastDisplay++;
\r
384 if( ulTicksSinceLastDisplay >= mainCHECK_DELAY )
\r
386 /* Reset the counter so these checks run again in mainCHECK_DELAY
\r
388 ulTicksSinceLastDisplay = 0;
\r
390 /* Has an error been found in any task? */
\r
391 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
393 pcMessage = "ERROR: GEN Q";
\r
395 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
397 pcMessage = "ERROR: PEEK Q";
\r
399 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
401 pcMessage = "ERROR: BLOCK Q";
\r
403 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
405 pcMessage = "ERROR: SEMAPHR";
\r
407 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
409 pcMessage = "ERROR: POLL Q";
\r
411 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
413 pcMessage = "ERROR: INT MATH";
\r
415 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
417 pcMessage = "ERROR: REC MUTEX";
\r
420 /* Send the message to the OLED gatekeeper for display. The
\r
421 xHigherPriorityTaskWoken parameter is not actually used here
\r
422 as this function is running in the tick interrupt anyway - but
\r
423 it must still be supplied. */
\r
424 xHigherPriorityTaskWoken = pdFALSE;
\r
425 xQueueSendFromISR( xLCDQueue, &pcMessage, &xHigherPriorityTaskWoken );
\r
428 /*-----------------------------------------------------------*/
\r