2 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * The FreeRTOS eBook and reference manual are available to purchase for a *
\r
29 * small fee. Help yourself get started quickly while also helping the *
\r
30 * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *
\r
32 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\r
50 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
51 * documentation provides more details of the standard demo application tasks.
\r
52 * In addition to the standard demo tasks, the following tasks and tests are
\r
53 * defined and/or created within this file:
\r
55 * "Fast Interrupt Test" - A high frequency periodic interrupt is generated
\r
56 * using a free running timer to demonstrate the use of the
\r
57 * configKERNEL_INTERRUPT_PRIORITY configuration constant. The interrupt
\r
58 * service routine measures the number of processor clocks that occur between
\r
59 * each interrupt - and in so doing measures the jitter in the interrupt timing.
\r
60 * The maximum measured jitter time is latched in the ulMaxJitter variable, and
\r
61 * displayed on the LCD by the 'Check' task as described below. The
\r
62 * fast interrupt is configured and handled in the timertest.c source file.
\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.
\r
70 * "Check" task - This only executes every five seconds but has the highest
\r
71 * priority so is guaranteed to get processor time. Its main function is to
\r
72 * check that all the standard demo tasks are still operational. Should any
\r
73 * unexpected behaviour within a demo task be discovered the 'check' task will
\r
74 * write an error to the LCD (via the LCD task). If all the demo tasks are
\r
75 * executing with their expected behaviour then the check task writes PASS
\r
76 * along with the max jitter time to the LCD (again via the LCD task), as
\r
81 /* Standard includes. */
\r
84 /* Scheduler includes. */
\r
85 #include "FreeRTOS.h"
\r
89 /* Library includes. */
\r
90 #include "stm32f10x_it.h"
\r
92 /* Demo app includes. */
\r
94 #include "LCD_Message.h"
\r
97 #include "integer.h"
\r
98 #include "blocktim.h"
\r
99 #include "partest.h"
\r
100 #include "semtest.h"
\r
103 #include "comtest2.h"
\r
105 /* Task priorities. */
\r
106 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
107 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
108 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
109 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
110 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
111 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
112 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
113 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
115 /* Constants related to the LCD. */
\r
116 #define mainMAX_LINE ( 240 )
\r
117 #define mainROW_INCREMENT ( 24 )
\r
118 #define mainMAX_COLUMN ( 20 )
\r
119 #define mainCOLUMN_START ( 319 )
\r
120 #define mainCOLUMN_INCREMENT ( 16 )
\r
122 /* The maximum number of message that can be waiting for display at any one
\r
124 #define mainLCD_QUEUE_SIZE ( 3 )
\r
126 /* The check task uses the sprintf function so requires a little more stack. */
\r
127 #define mainCHECK_TASK_STACK_SIZE ( configMINIMAL_STACK_SIZE + 50 )
\r
129 /* Dimensions the buffer into which the jitter time is written. */
\r
130 #define mainMAX_MSG_LEN 25
\r
132 /* The time between cycles of the 'check' task. */
\r
133 #define mainCHECK_DELAY ( ( portTickType ) 5000 / portTICK_RATE_MS )
\r
135 /* The number of nano seconds between each processor clock. */
\r
136 #define mainNS_PER_CLOCK ( ( unsigned portLONG ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )
\r
138 /* Baud rate used by the comtest tasks. */
\r
139 #define mainCOM_TEST_BAUD_RATE ( 115200 )
\r
141 /* The LED used by the comtest tasks. See the comtest.c file for more
\r
143 #define mainCOM_TEST_LED ( 3 )
\r
145 /*-----------------------------------------------------------*/
\r
148 * Configure the clocks, GPIO and other peripherals as required by the demo.
\r
150 static void prvSetupHardware( void );
\r
153 * Configure the LCD as required by the demo.
\r
155 static void prvConfigureLCD( void );
\r
158 * The LCD is written two by more than one task so is controlled by a
\r
159 * 'gatekeeper' task. This is the only task that is actually permitted to
\r
160 * access the LCD directly. Other tasks wanting to display a message send
\r
161 * the message to the gatekeeper.
\r
163 static void vLCDTask( void *pvParameters );
\r
166 * Retargets the C library printf function to the USART.
\r
168 int fputc( int ch, FILE *f );
\r
171 * Checks the status of all the demo tasks then prints a message to the
\r
172 * display. The message will be either PASS - and include in brackets the
\r
173 * maximum measured jitter time (as described at the to of the file), or a
\r
174 * message that describes which of the standard demo tasks an error has been
\r
177 * Messages are not written directly to the terminal, but passed to vLCDTask
\r
180 static void vCheckTask( void *pvParameters );
\r
183 * Configures the timers and interrupts for the fast interrupt test as
\r
184 * described at the top of this file.
\r
186 extern void vSetupTimerTest( void );
\r
188 /*-----------------------------------------------------------*/
\r
190 /* The queue used to send messages to the LCD task. */
\r
191 xQueueHandle xLCDQueue;
\r
193 /*-----------------------------------------------------------*/
\r
201 prvSetupHardware();
\r
203 /* Create the queue used by the LCD task. Messages for display on the LCD
\r
204 are received via this queue. */
\r
205 xLCDQueue = xQueueCreate( mainLCD_QUEUE_SIZE, sizeof( xLCDMessage ) );
\r
207 /* Start the standard demo tasks. */
\r
208 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
209 vCreateBlockTimeTasks();
\r
210 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
211 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
212 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
213 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
214 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
216 /* Start the tasks defined within this file/specific to this demo. */
\r
217 xTaskCreate( vCheckTask, ( signed portCHAR * ) "Check", mainCHECK_TASK_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
218 xTaskCreate( vLCDTask, ( signed portCHAR * ) "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
220 /* The suicide tasks must be created last as they need to know how many
\r
221 tasks were running prior to their creation in order to ascertain whether
\r
222 or not the correct/expected number of tasks are running at any given time. */
\r
223 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
225 /* Configure the timers used by the fast interrupt timer test. */
\r
228 /* Start the scheduler. */
\r
229 vTaskStartScheduler();
\r
231 /* Will only get here if there was not enough heap space to create the
\r
235 /*-----------------------------------------------------------*/
\r
237 void vLCDTask( void *pvParameters )
\r
239 xLCDMessage xMessage;
\r
241 /* Initialise the LCD and display a startup message. */
\r
243 LCD_DrawMonoPict( ( unsigned portLONG * ) pcBitmap );
\r
247 /* Wait for a message to arrive that requires displaying. */
\r
248 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
\r
250 /* Display the message. Print each message to a different position. */
\r
251 printf( ( portCHAR const * ) xMessage.pcMessage );
\r
254 /*-----------------------------------------------------------*/
\r
256 static void vCheckTask( void *pvParameters )
\r
258 portTickType xLastExecutionTime;
\r
259 xLCDMessage xMessage;
\r
260 static signed portCHAR cPassMessage[ mainMAX_MSG_LEN ];
\r
261 extern unsigned portSHORT usMaxJitter;
\r
263 xLastExecutionTime = xTaskGetTickCount();
\r
264 xMessage.pcMessage = cPassMessage;
\r
268 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
269 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
\r
271 /* Has an error been found in any task? */
\r
273 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
275 xMessage.pcMessage = "ERROR IN BLOCK Q\n";
\r
277 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
279 xMessage.pcMessage = "ERROR IN BLOCK TIME\n";
\r
281 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
283 xMessage.pcMessage = "ERROR IN SEMAPHORE\n";
\r
285 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
287 xMessage.pcMessage = "ERROR IN POLL Q\n";
\r
289 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
291 xMessage.pcMessage = "ERROR IN CREATE\n";
\r
293 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
295 xMessage.pcMessage = "ERROR IN MATH\n";
\r
297 else if( xAreComTestTasksStillRunning() != pdTRUE )
\r
299 xMessage.pcMessage = "ERROR IN COM TEST\n";
\r
303 sprintf( ( portCHAR * ) cPassMessage, "PASS [%uns]\n", ( ( unsigned portLONG ) usMaxJitter ) * mainNS_PER_CLOCK );
\r
306 /* Send the message to the LCD gatekeeper for display. */
\r
307 xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );
\r
310 /*-----------------------------------------------------------*/
\r
312 static void prvSetupHardware( void )
\r
314 /* Start with the clocks in their expected state. */
\r
317 /* Enable HSE (high speed external clock). */
\r
318 RCC_HSEConfig( RCC_HSE_ON );
\r
320 /* Wait till HSE is ready. */
\r
321 while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )
\r
325 /* 2 wait states required on the flash. */
\r
326 *( ( unsigned portLONG * ) 0x40022000 ) = 0x02;
\r
328 /* HCLK = SYSCLK */
\r
329 RCC_HCLKConfig( RCC_SYSCLK_Div1 );
\r
332 RCC_PCLK2Config( RCC_HCLK_Div1 );
\r
334 /* PCLK1 = HCLK/2 */
\r
335 RCC_PCLK1Config( RCC_HCLK_Div2 );
\r
337 /* PLLCLK = 8MHz * 9 = 72 MHz. */
\r
338 RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_9 );
\r
341 RCC_PLLCmd( ENABLE );
\r
343 /* Wait till PLL is ready. */
\r
344 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
\r
348 /* Select PLL as system clock source. */
\r
349 RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );
\r
351 /* Wait till PLL is used as system clock source. */
\r
352 while( RCC_GetSYSCLKSource() != 0x08 )
\r
356 /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
\r
357 RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
\r
358 | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );
\r
360 /* SPI2 Periph clock enable */
\r
361 RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );
\r
364 /* Set the Vector Table base address at 0x08000000 */
\r
365 NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );
\r
367 NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
\r
369 /* Configure HCLK clock as SysTick clock source. */
\r
370 SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );
\r
372 vParTestInitialise();
\r
374 /*-----------------------------------------------------------*/
\r
376 static void prvConfigureLCD( void )
\r
378 GPIO_InitTypeDef GPIO_InitStructure;
\r
380 /* Configure LCD Back Light (PA8) as output push-pull */
\r
381 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
\r
382 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
\r
383 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
\r
384 GPIO_Init( GPIOA, &GPIO_InitStructure );
\r
386 /* Set the Backlight Pin */
\r
387 GPIO_WriteBit(GPIOA, GPIO_Pin_8, Bit_SET);
\r
389 /* Initialize the LCD */
\r
392 /* Set the Back Color */
\r
393 LCD_SetBackColor( White );
\r
395 /* Set the Text Color */
\r
396 LCD_SetTextColor( 0x051F );
\r
400 /*-----------------------------------------------------------*/
\r
402 int fputc( int ch, FILE *f )
\r
404 static unsigned portSHORT usColumn = 0, usRefColumn = mainCOLUMN_START;
\r
405 static unsigned portCHAR ucLine = 0;
\r
407 if( ( usColumn == 0 ) && ( ucLine == 0 ) )
\r
414 /* Display one character on LCD */
\r
415 LCD_DisplayChar( ucLine, usRefColumn, (u8) ch );
\r
417 /* Decrement the column position by 16 */
\r
418 usRefColumn -= mainCOLUMN_INCREMENT;
\r
420 /* Increment the character counter */
\r
422 if( usColumn == mainMAX_COLUMN )
\r
424 ucLine += mainROW_INCREMENT;
\r
425 usRefColumn = mainCOLUMN_START;
\r
431 /* Move back to the first column of the next line. */
\r
432 ucLine += mainROW_INCREMENT;
\r
433 usRefColumn = mainCOLUMN_START;
\r
437 /* Wrap back to the top of the display. */
\r
438 if( ucLine >= mainMAX_LINE )
\r
445 /*-----------------------------------------------------------*/
\r
448 /* Keep the linker happy. */
\r
449 void assert_failed( unsigned portCHAR* pcFile, unsigned portLONG ulLine )
\r