2 FreeRTOS.org V4.7.1 - Copyright (C) 2003-2008 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
28 Please ensure to read the configuration and relevant port sections of the
\r
29 online documentation.
\r
31 +++ http://www.FreeRTOS.org +++
\r
32 Documentation, latest information, license and contact details.
\r
34 +++ http://www.SafeRTOS.com +++
\r
35 A version that is certified for use in safety critical systems.
\r
37 +++ http://www.OpenRTOS.com +++
\r
38 Commercial support, development, porting, licensing and training services.
\r
40 ***************************************************************************
\r
44 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
45 * documentation provides more details of the standard demo application tasks.
\r
46 * In addition to the standard demo tasks, the following tasks and tests are
\r
47 * defined and/or created within this file:
\r
49 * "Fast Interrupt Test" - A high frequency periodic interrupt is generated
\r
50 * using a free running timer to demonstrate the use of the
\r
51 * configKERNEL_INTERRUPT_PRIORITY configuration constant. The interrupt
\r
52 * service routine measures the number of processor clocks that occur between
\r
53 * each interrupt - and in so doing measures the jitter in the interrupt timing.
\r
54 * The maximum measured jitter time is latched in the ulMaxJitter variable, and
\r
55 * displayed on the LCD by the 'Check' task as described below. The
\r
56 * fast interrupt is configured and handled in the timertest.c source file.
\r
58 * "LCD" task - the LCD task is a 'gatekeeper' task. It is the only task that
\r
59 * is permitted to access the display directly. Other tasks wishing to write a
\r
60 * message to the LCD send the message on a queue to the LCD task instead of
\r
61 * accessing the LCD themselves. The LCD task just blocks on the queue waiting
\r
62 * for messages - waking and displaying the messages as they arrive.
\r
64 * "Check" task - This only executes every five seconds but has the highest
\r
65 * priority so is guaranteed to get processor time. Its main function is to
\r
66 * check that all the standard demo tasks are still operational. Should any
\r
67 * unexpected behaviour within a demo task be discovered the 'check' task will
\r
68 * write an error to the LCD (via the LCD task). If all the demo tasks are
\r
69 * executing with their expected behaviour then the check task writes PASS
\r
70 * along with the max jitter time to the LCD (again via the LCD task), as
\r
75 /* Standard includes. */
\r
78 /* Scheduler includes. */
\r
79 #include "FreeRTOS.h"
\r
83 /* Library includes. */
\r
84 #include "stm32f10x_it.h"
\r
86 /* Demo app includes. */
\r
88 #include "LCD_Message.h"
\r
91 #include "integer.h"
\r
92 #include "blocktim.h"
\r
93 #include "partest.h"
\r
94 #include "semtest.h"
\r
97 #include "comtest2.h"
\r
99 /* Task priorities. */
\r
100 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
101 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
102 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
103 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
104 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
105 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
106 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
107 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
109 /* Constants related to the LCD. */
\r
110 #define mainMAX_LINE ( 240 )
\r
111 #define mainROW_INCREMENT ( 24 )
\r
112 #define mainMAX_COLUMN ( 20 )
\r
113 #define mainCOLUMN_START ( 319 )
\r
114 #define mainCOLUMN_INCREMENT ( 16 )
\r
116 /* The maximum number of message that can be waiting for display at any one
\r
118 #define mainLCD_QUEUE_SIZE ( 3 )
\r
120 /* The check task uses the sprintf function so requires a little more stack. */
\r
121 #define mainCHECK_TASK_STACK_SIZE ( configMINIMAL_STACK_SIZE + 50 )
\r
123 /* Dimensions the buffer into which the jitter time is written. */
\r
124 #define mainMAX_MSG_LEN 25
\r
126 /* The time between cycles of the 'check' task. */
\r
127 #define mainCHECK_DELAY ( ( portTickType ) 5000 / portTICK_RATE_MS )
\r
129 /* The number of nano seconds between each processor clock. */
\r
130 #define mainNS_PER_CLOCK ( ( unsigned portLONG ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )
\r
132 /* Baud rate used by the comtest tasks. */
\r
133 #define mainCOM_TEST_BAUD_RATE ( 115200 )
\r
135 /* The LED used by the comtest tasks. See the comtest.c file for more
\r
137 #define mainCOM_TEST_LED ( 3 )
\r
139 /*-----------------------------------------------------------*/
\r
142 * Configure the clocks, GPIO and other peripherals as required by the demo.
\r
144 static void prvSetupHardware( void );
\r
147 * Configure the LCD as required by the demo.
\r
149 static void prvConfigureLCD( void );
\r
152 * The LCD is written two by more than one task so is controlled by a
\r
153 * 'gatekeeper' task. This is the only task that is actually permitted to
\r
154 * access the LCD directly. Other tasks wanting to display a message send
\r
155 * the message to the gatekeeper.
\r
157 static void vLCDTask( void *pvParameters );
\r
160 * Retargets the C library printf function to the USART.
\r
162 int fputc( int ch, FILE *f );
\r
165 * Checks the status of all the demo tasks then prints a message to the
\r
166 * display. The message will be either PASS - and include in brackets the
\r
167 * maximum measured jitter time (as described at the to of the file), or a
\r
168 * message that describes which of the standard demo tasks an error has been
\r
171 * Messages are not written directly to the terminal, but passed to vLCDTask
\r
174 static void vCheckTask( void *pvParameters );
\r
177 * Configures the timers and interrupts for the fast interrupt test as
\r
178 * described at the top of this file.
\r
180 extern void vSetupTimerTest( void );
\r
182 /*-----------------------------------------------------------*/
\r
184 /* The queue used to send messages to the LCD task. */
\r
185 xQueueHandle xLCDQueue;
\r
187 /*-----------------------------------------------------------*/
\r
195 prvSetupHardware();
\r
197 /* Create the queue used by the LCD task. Messages for display on the LCD
\r
198 are received via this queue. */
\r
199 xLCDQueue = xQueueCreate( mainLCD_QUEUE_SIZE, sizeof( xLCDMessage ) );
\r
201 /* Start the standard demo tasks. */
\r
202 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
203 vCreateBlockTimeTasks();
\r
204 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
205 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
206 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
207 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
208 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
210 /* Start the tasks defined within this file/specific to this demo. */
\r
211 xTaskCreate( vCheckTask, ( signed portCHAR * ) "Check", mainCHECK_TASK_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
212 xTaskCreate( vLCDTask, ( signed portCHAR * ) "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
214 /* The suicide tasks must be created last as they need to know how many
\r
215 tasks were running prior to their creation in order to ascertain whether
\r
216 or not the correct/expected number of tasks are running at any given time. */
\r
217 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
219 /* Configure the timers used by the fast interrupt timer test. */
\r
222 /* Start the scheduler. */
\r
223 vTaskStartScheduler();
\r
225 /* Will only get here if there was not enough heap space to create the
\r
229 /*-----------------------------------------------------------*/
\r
231 void vLCDTask( void *pvParameters )
\r
233 xLCDMessage xMessage;
\r
235 /* Initialise the LCD and display a startup message. */
\r
237 LCD_DrawMonoPict( ( unsigned portLONG * ) pcBitmap );
\r
241 /* Wait for a message to arrive that requires displaying. */
\r
242 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
\r
244 /* Display the message. Print each message to a different position. */
\r
245 printf( ( portCHAR const * ) xMessage.pcMessage );
\r
248 /*-----------------------------------------------------------*/
\r
250 static void vCheckTask( void *pvParameters )
\r
252 portTickType xLastExecutionTime;
\r
253 xLCDMessage xMessage;
\r
254 static signed portCHAR cPassMessage[ mainMAX_MSG_LEN ];
\r
255 extern unsigned portSHORT usMaxJitter;
\r
257 xLastExecutionTime = xTaskGetTickCount();
\r
258 xMessage.pcMessage = cPassMessage;
\r
262 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
263 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
\r
265 /* Has an error been found in any task? */
\r
267 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
269 xMessage.pcMessage = "ERROR IN BLOCK Q\n";
\r
271 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
273 xMessage.pcMessage = "ERROR IN BLOCK TIME\n";
\r
275 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
277 xMessage.pcMessage = "ERROR IN SEMAPHORE\n";
\r
279 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
281 xMessage.pcMessage = "ERROR IN POLL Q\n";
\r
283 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
285 xMessage.pcMessage = "ERROR IN CREATE\n";
\r
287 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
289 xMessage.pcMessage = "ERROR IN MATH\n";
\r
291 else if( xAreComTestTasksStillRunning() != pdTRUE )
\r
293 xMessage.pcMessage = "ERROR IN COM TEST\n";
\r
297 sprintf( ( portCHAR * ) cPassMessage, "PASS [%uns]\n", ( ( unsigned portLONG ) usMaxJitter ) * mainNS_PER_CLOCK );
\r
300 /* Send the message to the LCD gatekeeper for display. */
\r
301 xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );
\r
304 /*-----------------------------------------------------------*/
\r
306 static void prvSetupHardware( void )
\r
308 /* Start with the clocks in their expected state. */
\r
311 /* Enable HSE (high speed external clock). */
\r
312 RCC_HSEConfig( RCC_HSE_ON );
\r
314 /* Wait till HSE is ready. */
\r
315 while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )
\r
319 /* 2 wait states required on the flash. */
\r
320 *( ( unsigned portLONG * ) 0x40022000 ) = 0x02;
\r
322 /* HCLK = SYSCLK */
\r
323 RCC_HCLKConfig( RCC_SYSCLK_Div1 );
\r
326 RCC_PCLK2Config( RCC_HCLK_Div1 );
\r
328 /* PCLK1 = HCLK/2 */
\r
329 RCC_PCLK1Config( RCC_HCLK_Div2 );
\r
331 /* PLLCLK = 8MHz * 9 = 72 MHz. */
\r
332 RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_9 );
\r
335 RCC_PLLCmd( ENABLE );
\r
337 /* Wait till PLL is ready. */
\r
338 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
\r
342 /* Select PLL as system clock source. */
\r
343 RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );
\r
345 /* Wait till PLL is used as system clock source. */
\r
346 while( RCC_GetSYSCLKSource() != 0x08 )
\r
350 /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
\r
351 RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
\r
352 | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );
\r
354 /* SPI2 Periph clock enable */
\r
355 RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );
\r
358 /* Set the Vector Table base address at 0x08000000 */
\r
359 NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );
\r
361 NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
\r
363 /* Configure HCLK clock as SysTick clock source. */
\r
364 SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );
\r
366 vParTestInitialise();
\r
368 /*-----------------------------------------------------------*/
\r
370 static void prvConfigureLCD( void )
\r
372 GPIO_InitTypeDef GPIO_InitStructure;
\r
374 /* Configure LCD Back Light (PA8) as output push-pull */
\r
375 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
\r
376 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
\r
377 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
\r
378 GPIO_Init( GPIOA, &GPIO_InitStructure );
\r
380 /* Set the Backlight Pin */
\r
381 GPIO_WriteBit(GPIOA, GPIO_Pin_8, Bit_SET);
\r
383 /* Initialize the LCD */
\r
386 /* Set the Back Color */
\r
387 LCD_SetBackColor( White );
\r
389 /* Set the Text Color */
\r
390 LCD_SetTextColor( 0x051F );
\r
394 /*-----------------------------------------------------------*/
\r
396 int fputc( int ch, FILE *f )
\r
398 static unsigned portSHORT usColumn = 0, usRefColumn = mainCOLUMN_START;
\r
399 static unsigned portCHAR ucLine = 0;
\r
401 if( ( usColumn == 0 ) && ( ucLine == 0 ) )
\r
408 /* Display one character on LCD */
\r
409 LCD_DisplayChar( ucLine, usRefColumn, (u8) ch );
\r
411 /* Decrement the column position by 16 */
\r
412 usRefColumn -= mainCOLUMN_INCREMENT;
\r
414 /* Increment the character counter */
\r
416 if( usColumn == mainMAX_COLUMN )
\r
418 ucLine += mainROW_INCREMENT;
\r
419 usRefColumn = mainCOLUMN_START;
\r
425 /* Move back to the first column of the next line. */
\r
426 ucLine += mainROW_INCREMENT;
\r
427 usRefColumn = mainCOLUMN_START;
\r
431 /* Wrap back to the top of the display. */
\r
432 if( ucLine >= mainMAX_LINE )
\r
439 /*-----------------------------------------------------------*/
\r
442 /* Keep the linker happy. */
\r
443 void assert_failed( unsigned portCHAR* pcFile, unsigned portLONG ulLine )
\r