2 FreeRTOS V6.0.5 - Copyright (C) 2010 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\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
13 * then take a look at the FreeRTOS eBook *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\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
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\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
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
56 * documentation provides more details of the standard demo application tasks.
\r
57 * In addition to the standard demo tasks, the following tasks and tests are
\r
58 * defined and/or created within this file:
\r
60 * "Fast Interrupt Test" - A high frequency periodic interrupt is generated
\r
61 * using a free running timer to demonstrate the use of the
\r
62 * configKERNEL_INTERRUPT_PRIORITY configuration constant. The interrupt
\r
63 * service routine measures the number of processor clocks that occur between
\r
64 * each interrupt - and in so doing measures the jitter in the interrupt timing.
\r
65 * The maximum measured jitter time is latched in the ulMaxJitter variable, and
\r
66 * displayed on the LCD by the 'Check' task as described below. The
\r
67 * fast interrupt is configured and handled in the timertest.c source file.
\r
69 * "LCD" task - the LCD task is a 'gatekeeper' task. It is the only task that
\r
70 * is permitted to access the display directly. Other tasks wishing to write a
\r
71 * message to the LCD send the message on a queue to the LCD task instead of
\r
72 * accessing the LCD themselves. The LCD task just blocks on the queue waiting
\r
73 * for messages - waking and displaying the messages as they arrive.
\r
75 * "Check" task - This only executes every five seconds but has the highest
\r
76 * priority so is guaranteed to get processor time. Its main function is to
\r
77 * check that all the standard demo tasks are still operational. Should any
\r
78 * unexpected behaviour within a demo task be discovered the 'check' task will
\r
79 * write an error to the LCD (via the LCD task). If all the demo tasks are
\r
80 * executing with their expected behaviour then the check task writes PASS
\r
81 * along with the max jitter time to the LCD (again via the LCD task), as
\r
86 /* Standard includes. */
\r
89 /* Scheduler includes. */
\r
90 #include "FreeRTOS.h"
\r
94 /* Library includes. */
\r
95 #include "stm32f10x_it.h"
\r
97 /* Demo app includes. */
\r
99 #include "LCD_Message.h"
\r
100 #include "BlockQ.h"
\r
102 #include "integer.h"
\r
103 #include "blocktim.h"
\r
104 #include "partest.h"
\r
105 #include "semtest.h"
\r
108 #include "comtest2.h"
\r
110 /* Task priorities. */
\r
111 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
112 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
113 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
114 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
115 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
116 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
117 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
118 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
120 /* Constants related to the LCD. */
\r
121 #define mainMAX_LINE ( 240 )
\r
122 #define mainROW_INCREMENT ( 24 )
\r
123 #define mainMAX_COLUMN ( 20 )
\r
124 #define mainCOLUMN_START ( 319 )
\r
125 #define mainCOLUMN_INCREMENT ( 16 )
\r
127 /* The maximum number of message that can be waiting for display at any one
\r
129 #define mainLCD_QUEUE_SIZE ( 3 )
\r
131 /* The check task uses the sprintf function so requires a little more stack. */
\r
132 #define mainCHECK_TASK_STACK_SIZE ( configMINIMAL_STACK_SIZE + 50 )
\r
134 /* Dimensions the buffer into which the jitter time is written. */
\r
135 #define mainMAX_MSG_LEN 25
\r
137 /* The time between cycles of the 'check' task. */
\r
138 #define mainCHECK_DELAY ( ( portTickType ) 5000 / portTICK_RATE_MS )
\r
140 /* The number of nano seconds between each processor clock. */
\r
141 #define mainNS_PER_CLOCK ( ( unsigned portLONG ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )
\r
143 /* Baud rate used by the comtest tasks. */
\r
144 #define mainCOM_TEST_BAUD_RATE ( 115200 )
\r
146 /* The LED used by the comtest tasks. See the comtest.c file for more
\r
148 #define mainCOM_TEST_LED ( 3 )
\r
150 /*-----------------------------------------------------------*/
\r
153 * Configure the clocks, GPIO and other peripherals as required by the demo.
\r
155 static void prvSetupHardware( void );
\r
158 * Configure the LCD as required by the demo.
\r
160 static void prvConfigureLCD( void );
\r
163 * The LCD is written two by more than one task so is controlled by a
\r
164 * 'gatekeeper' task. This is the only task that is actually permitted to
\r
165 * access the LCD directly. Other tasks wanting to display a message send
\r
166 * the message to the gatekeeper.
\r
168 static void vLCDTask( void *pvParameters );
\r
171 * Retargets the C library printf function to the USART.
\r
173 int fputc( int ch, FILE *f );
\r
176 * Checks the status of all the demo tasks then prints a message to the
\r
177 * display. The message will be either PASS - and include in brackets the
\r
178 * maximum measured jitter time (as described at the to of the file), or a
\r
179 * message that describes which of the standard demo tasks an error has been
\r
182 * Messages are not written directly to the terminal, but passed to vLCDTask
\r
185 static void vCheckTask( void *pvParameters );
\r
188 * Configures the timers and interrupts for the fast interrupt test as
\r
189 * described at the top of this file.
\r
191 extern void vSetupTimerTest( void );
\r
193 /*-----------------------------------------------------------*/
\r
195 /* The queue used to send messages to the LCD task. */
\r
196 xQueueHandle xLCDQueue;
\r
198 /*-----------------------------------------------------------*/
\r
206 prvSetupHardware();
\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( mainLCD_QUEUE_SIZE, sizeof( xLCDMessage ) );
\r
212 /* Start the standard demo tasks. */
\r
213 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
214 vCreateBlockTimeTasks();
\r
215 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
216 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
217 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
218 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
219 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
221 /* Start the tasks defined within this file/specific to this demo. */
\r
222 xTaskCreate( vCheckTask, ( signed portCHAR * ) "Check", mainCHECK_TASK_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
223 xTaskCreate( vLCDTask, ( signed portCHAR * ) "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
225 /* The suicide tasks must be created last as they need to know how many
\r
226 tasks were running prior to their creation in order to ascertain whether
\r
227 or not the correct/expected number of tasks are running at any given time. */
\r
228 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
230 /* Configure the timers used by the fast interrupt timer test. */
\r
233 /* Start the scheduler. */
\r
234 vTaskStartScheduler();
\r
236 /* Will only get here if there was not enough heap space to create the
\r
240 /*-----------------------------------------------------------*/
\r
242 void vLCDTask( void *pvParameters )
\r
244 xLCDMessage xMessage;
\r
246 /* Initialise the LCD and display a startup message. */
\r
248 LCD_DrawMonoPict( ( unsigned portLONG * ) pcBitmap );
\r
252 /* Wait for a message to arrive that requires displaying. */
\r
253 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
\r
255 /* Display the message. Print each message to a different position. */
\r
256 printf( ( portCHAR const * ) xMessage.pcMessage );
\r
259 /*-----------------------------------------------------------*/
\r
261 static void vCheckTask( void *pvParameters )
\r
263 portTickType xLastExecutionTime;
\r
264 xLCDMessage xMessage;
\r
265 static signed portCHAR cPassMessage[ mainMAX_MSG_LEN ];
\r
266 extern unsigned portSHORT usMaxJitter;
\r
268 xLastExecutionTime = xTaskGetTickCount();
\r
269 xMessage.pcMessage = cPassMessage;
\r
273 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
274 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
\r
276 /* Has an error been found in any task? */
\r
278 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
280 xMessage.pcMessage = "ERROR IN BLOCK Q\n";
\r
282 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
284 xMessage.pcMessage = "ERROR IN BLOCK TIME\n";
\r
286 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
288 xMessage.pcMessage = "ERROR IN SEMAPHORE\n";
\r
290 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
292 xMessage.pcMessage = "ERROR IN POLL Q\n";
\r
294 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
296 xMessage.pcMessage = "ERROR IN CREATE\n";
\r
298 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
300 xMessage.pcMessage = "ERROR IN MATH\n";
\r
302 else if( xAreComTestTasksStillRunning() != pdTRUE )
\r
304 xMessage.pcMessage = "ERROR IN COM TEST\n";
\r
308 sprintf( ( portCHAR * ) cPassMessage, "PASS [%uns]\n", ( ( unsigned portLONG ) usMaxJitter ) * mainNS_PER_CLOCK );
\r
311 /* Send the message to the LCD gatekeeper for display. */
\r
312 xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );
\r
315 /*-----------------------------------------------------------*/
\r
317 static void prvSetupHardware( void )
\r
319 /* Start with the clocks in their expected state. */
\r
322 /* Enable HSE (high speed external clock). */
\r
323 RCC_HSEConfig( RCC_HSE_ON );
\r
325 /* Wait till HSE is ready. */
\r
326 while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )
\r
330 /* 2 wait states required on the flash. */
\r
331 *( ( unsigned portLONG * ) 0x40022000 ) = 0x02;
\r
333 /* HCLK = SYSCLK */
\r
334 RCC_HCLKConfig( RCC_SYSCLK_Div1 );
\r
337 RCC_PCLK2Config( RCC_HCLK_Div1 );
\r
339 /* PCLK1 = HCLK/2 */
\r
340 RCC_PCLK1Config( RCC_HCLK_Div2 );
\r
342 /* PLLCLK = 8MHz * 9 = 72 MHz. */
\r
343 RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_9 );
\r
346 RCC_PLLCmd( ENABLE );
\r
348 /* Wait till PLL is ready. */
\r
349 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
\r
353 /* Select PLL as system clock source. */
\r
354 RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );
\r
356 /* Wait till PLL is used as system clock source. */
\r
357 while( RCC_GetSYSCLKSource() != 0x08 )
\r
361 /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
\r
362 RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
\r
363 | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );
\r
365 /* SPI2 Periph clock enable */
\r
366 RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );
\r
369 /* Set the Vector Table base address at 0x08000000 */
\r
370 NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );
\r
372 NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
\r
374 /* Configure HCLK clock as SysTick clock source. */
\r
375 SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );
\r
377 vParTestInitialise();
\r
379 /*-----------------------------------------------------------*/
\r
381 static void prvConfigureLCD( void )
\r
383 GPIO_InitTypeDef GPIO_InitStructure;
\r
385 /* Configure LCD Back Light (PA8) as output push-pull */
\r
386 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
\r
387 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
\r
388 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
\r
389 GPIO_Init( GPIOA, &GPIO_InitStructure );
\r
391 /* Set the Backlight Pin */
\r
392 GPIO_WriteBit(GPIOA, GPIO_Pin_8, Bit_SET);
\r
394 /* Initialize the LCD */
\r
397 /* Set the Back Color */
\r
398 LCD_SetBackColor( White );
\r
400 /* Set the Text Color */
\r
401 LCD_SetTextColor( 0x051F );
\r
405 /*-----------------------------------------------------------*/
\r
407 int fputc( int ch, FILE *f )
\r
409 static unsigned portSHORT usColumn = 0, usRefColumn = mainCOLUMN_START;
\r
410 static unsigned portCHAR ucLine = 0;
\r
412 if( ( usColumn == 0 ) && ( ucLine == 0 ) )
\r
419 /* Display one character on LCD */
\r
420 LCD_DisplayChar( ucLine, usRefColumn, (u8) ch );
\r
422 /* Decrement the column position by 16 */
\r
423 usRefColumn -= mainCOLUMN_INCREMENT;
\r
425 /* Increment the character counter */
\r
427 if( usColumn == mainMAX_COLUMN )
\r
429 ucLine += mainROW_INCREMENT;
\r
430 usRefColumn = mainCOLUMN_START;
\r
436 /* Move back to the first column of the next line. */
\r
437 ucLine += mainROW_INCREMENT;
\r
438 usRefColumn = mainCOLUMN_START;
\r
442 /* Wrap back to the top of the display. */
\r
443 if( ucLine >= mainMAX_LINE )
\r
450 /*-----------------------------------------------------------*/
\r
453 /* Keep the linker happy. */
\r
454 void assert_failed( unsigned portCHAR* pcFile, unsigned portLONG ulLine )
\r