2 FreeRTOS V8.1.1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\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
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
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\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
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
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
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\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
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
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
67 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
68 * documentation provides more details of the standard demo application tasks.
\r
69 * In addition to the standard demo tasks, the following tasks and tests are
\r
70 * defined and/or created within this file:
\r
72 * "Fast Interrupt Test" - A high frequency periodic interrupt is generated
\r
73 * using a free running timer to demonstrate the use of the
\r
74 * configKERNEL_INTERRUPT_PRIORITY configuration constant. The interrupt
\r
75 * service routine measures the number of processor clocks that occur between
\r
76 * each interrupt - and in so doing measures the jitter in the interrupt timing.
\r
77 * The maximum measured jitter time is latched in the ulMaxJitter variable, and
\r
78 * displayed on the LCD by the 'Check' task as described below. The
\r
79 * fast interrupt is configured and handled in the timertest.c source file.
\r
81 * "LCD" task - the LCD task is a 'gatekeeper' task. It is the only task that
\r
82 * is permitted to access the display directly. Other tasks wishing to write a
\r
83 * message to the LCD send the message on a queue to the LCD task instead of
\r
84 * accessing the LCD themselves. The LCD task just blocks on the queue waiting
\r
85 * for messages - waking and displaying the messages as they arrive.
\r
87 * "Check" task - This only executes every five seconds but has the highest
\r
88 * priority so is guaranteed to get processor time. Its main function is to
\r
89 * check that all the standard demo tasks are still operational. Should any
\r
90 * unexpected behaviour within a demo task be discovered the 'check' task will
\r
91 * write an error to the LCD (via the LCD task). If all the demo tasks are
\r
92 * executing with their expected behaviour then the check task writes PASS
\r
93 * along with the max jitter time to the LCD (again via the LCD task), as
\r
98 /* Standard includes. */
\r
101 /* Scheduler includes. */
\r
102 #include "FreeRTOS.h"
\r
106 /* Library includes. */
\r
107 #include "stm32f10x_it.h"
\r
109 /* Demo app includes. */
\r
111 #include "LCD_Message.h"
\r
112 #include "BlockQ.h"
\r
114 #include "integer.h"
\r
115 #include "blocktim.h"
\r
116 #include "partest.h"
\r
117 #include "semtest.h"
\r
120 #include "comtest2.h"
\r
122 /* Task priorities. */
\r
123 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
124 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
125 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
126 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
127 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
128 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
129 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
130 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
132 /* Constants related to the LCD. */
\r
133 #define mainMAX_LINE ( 240 )
\r
134 #define mainROW_INCREMENT ( 24 )
\r
135 #define mainMAX_COLUMN ( 20 )
\r
136 #define mainCOLUMN_START ( 319 )
\r
137 #define mainCOLUMN_INCREMENT ( 16 )
\r
139 /* The maximum number of message that can be waiting for display at any one
\r
141 #define mainLCD_QUEUE_SIZE ( 3 )
\r
143 /* The check task uses the sprintf function so requires a little more stack. */
\r
144 #define mainCHECK_TASK_STACK_SIZE ( configMINIMAL_STACK_SIZE + 50 )
\r
146 /* Dimensions the buffer into which the jitter time is written. */
\r
147 #define mainMAX_MSG_LEN 25
\r
149 /* The time between cycles of the 'check' task. */
\r
150 #define mainCHECK_DELAY ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )
\r
152 /* The number of nano seconds between each processor clock. */
\r
153 #define mainNS_PER_CLOCK ( ( unsigned long ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )
\r
155 /* Baud rate used by the comtest tasks. */
\r
156 #define mainCOM_TEST_BAUD_RATE ( 115200 )
\r
158 /* The LED used by the comtest tasks. See the comtest.c file for more
\r
160 #define mainCOM_TEST_LED ( 3 )
\r
162 /*-----------------------------------------------------------*/
\r
165 * Configure the clocks, GPIO and other peripherals as required by the demo.
\r
167 static void prvSetupHardware( void );
\r
170 * Configure the LCD as required by the demo.
\r
172 static void prvConfigureLCD( void );
\r
175 * The LCD is written two by more than one task so is controlled by a
\r
176 * 'gatekeeper' task. This is the only task that is actually permitted to
\r
177 * access the LCD directly. Other tasks wanting to display a message send
\r
178 * the message to the gatekeeper.
\r
180 static void vLCDTask( void *pvParameters );
\r
183 * Retargets the C library printf function to the USART.
\r
185 int fputc( int ch, FILE *f );
\r
188 * Checks the status of all the demo tasks then prints a message to the
\r
189 * display. The message will be either PASS - and include in brackets the
\r
190 * maximum measured jitter time (as described at the to of the file), or a
\r
191 * message that describes which of the standard demo tasks an error has been
\r
194 * Messages are not written directly to the terminal, but passed to vLCDTask
\r
197 static void vCheckTask( void *pvParameters );
\r
200 * Configures the timers and interrupts for the fast interrupt test as
\r
201 * described at the top of this file.
\r
203 extern void vSetupTimerTest( void );
\r
205 /*-----------------------------------------------------------*/
\r
207 /* The queue used to send messages to the LCD task. */
\r
208 QueueHandle_t xLCDQueue;
\r
210 /*-----------------------------------------------------------*/
\r
218 prvSetupHardware();
\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( mainLCD_QUEUE_SIZE, sizeof( xLCDMessage ) );
\r
224 /* Start the standard demo tasks. */
\r
225 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
226 vCreateBlockTimeTasks();
\r
227 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
228 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
229 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
230 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
231 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
233 /* Start the tasks defined within this file/specific to this demo. */
\r
234 xTaskCreate( vCheckTask, "Check", mainCHECK_TASK_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
235 xTaskCreate( vLCDTask, "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
237 /* The suicide tasks must be created last as they need to know how many
\r
238 tasks were running prior to their creation in order to ascertain whether
\r
239 or not the correct/expected number of tasks are running at any given time. */
\r
240 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
242 /* Configure the timers used by the fast interrupt timer test. */
\r
245 /* Start the scheduler. */
\r
246 vTaskStartScheduler();
\r
248 /* Will only get here if there was not enough heap space to create the
\r
252 /*-----------------------------------------------------------*/
\r
254 void vLCDTask( void *pvParameters )
\r
256 xLCDMessage xMessage;
\r
258 /* Initialise the LCD and display a startup message. */
\r
260 LCD_DrawMonoPict( ( unsigned long * ) pcBitmap );
\r
264 /* Wait for a message to arrive that requires displaying. */
\r
265 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
\r
267 /* Display the message. Print each message to a different position. */
\r
268 printf( ( char const * ) xMessage.pcMessage );
\r
271 /*-----------------------------------------------------------*/
\r
273 static void vCheckTask( void *pvParameters )
\r
275 TickType_t xLastExecutionTime;
\r
276 xLCDMessage xMessage;
\r
277 static signed char cPassMessage[ mainMAX_MSG_LEN ];
\r
278 extern unsigned short usMaxJitter;
\r
280 xLastExecutionTime = xTaskGetTickCount();
\r
281 xMessage.pcMessage = cPassMessage;
\r
285 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
286 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
\r
288 /* Has an error been found in any task? */
\r
290 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
292 xMessage.pcMessage = "ERROR IN BLOCK Q\n";
\r
294 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
296 xMessage.pcMessage = "ERROR IN BLOCK TIME\n";
\r
298 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
300 xMessage.pcMessage = "ERROR IN SEMAPHORE\n";
\r
302 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
304 xMessage.pcMessage = "ERROR IN POLL Q\n";
\r
306 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
308 xMessage.pcMessage = "ERROR IN CREATE\n";
\r
310 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
312 xMessage.pcMessage = "ERROR IN MATH\n";
\r
314 else if( xAreComTestTasksStillRunning() != pdTRUE )
\r
316 xMessage.pcMessage = "ERROR IN COM TEST\n";
\r
320 sprintf( ( char * ) cPassMessage, "PASS [%uns]\n", ( ( unsigned long ) usMaxJitter ) * mainNS_PER_CLOCK );
\r
323 /* Send the message to the LCD gatekeeper for display. */
\r
324 xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );
\r
327 /*-----------------------------------------------------------*/
\r
329 static void prvSetupHardware( void )
\r
331 /* Start with the clocks in their expected state. */
\r
334 /* Enable HSE (high speed external clock). */
\r
335 RCC_HSEConfig( RCC_HSE_ON );
\r
337 /* Wait till HSE is ready. */
\r
338 while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )
\r
342 /* 2 wait states required on the flash. */
\r
343 *( ( unsigned long * ) 0x40022000 ) = 0x02;
\r
345 /* HCLK = SYSCLK */
\r
346 RCC_HCLKConfig( RCC_SYSCLK_Div1 );
\r
349 RCC_PCLK2Config( RCC_HCLK_Div1 );
\r
351 /* PCLK1 = HCLK/2 */
\r
352 RCC_PCLK1Config( RCC_HCLK_Div2 );
\r
354 /* PLLCLK = 8MHz * 9 = 72 MHz. */
\r
355 RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_9 );
\r
358 RCC_PLLCmd( ENABLE );
\r
360 /* Wait till PLL is ready. */
\r
361 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
\r
365 /* Select PLL as system clock source. */
\r
366 RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );
\r
368 /* Wait till PLL is used as system clock source. */
\r
369 while( RCC_GetSYSCLKSource() != 0x08 )
\r
373 /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
\r
374 RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
\r
375 | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );
\r
377 /* SPI2 Periph clock enable */
\r
378 RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );
\r
381 /* Set the Vector Table base address at 0x08000000 */
\r
382 NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );
\r
384 NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
\r
386 /* Configure HCLK clock as SysTick clock source. */
\r
387 SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );
\r
389 vParTestInitialise();
\r
391 /*-----------------------------------------------------------*/
\r
393 static void prvConfigureLCD( void )
\r
395 GPIO_InitTypeDef GPIO_InitStructure;
\r
397 /* Configure LCD Back Light (PA8) as output push-pull */
\r
398 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
\r
399 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
\r
400 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
\r
401 GPIO_Init( GPIOA, &GPIO_InitStructure );
\r
403 /* Set the Backlight Pin */
\r
404 GPIO_WriteBit(GPIOA, GPIO_Pin_8, Bit_SET);
\r
406 /* Initialize the LCD */
\r
409 /* Set the Back Color */
\r
410 LCD_SetBackColor( White );
\r
412 /* Set the Text Color */
\r
413 LCD_SetTextColor( 0x051F );
\r
417 /*-----------------------------------------------------------*/
\r
419 int fputc( int ch, FILE *f )
\r
421 static unsigned short usColumn = 0, usRefColumn = mainCOLUMN_START;
\r
422 static unsigned char ucLine = 0;
\r
424 if( ( usColumn == 0 ) && ( ucLine == 0 ) )
\r
431 /* Display one character on LCD */
\r
432 LCD_DisplayChar( ucLine, usRefColumn, (u8) ch );
\r
434 /* Decrement the column position by 16 */
\r
435 usRefColumn -= mainCOLUMN_INCREMENT;
\r
437 /* Increment the character counter */
\r
439 if( usColumn == mainMAX_COLUMN )
\r
441 ucLine += mainROW_INCREMENT;
\r
442 usRefColumn = mainCOLUMN_START;
\r
448 /* Move back to the first column of the next line. */
\r
449 ucLine += mainROW_INCREMENT;
\r
450 usRefColumn = mainCOLUMN_START;
\r
454 /* Wrap back to the top of the display. */
\r
455 if( ucLine >= mainMAX_LINE )
\r
462 /*-----------------------------------------------------------*/
\r
465 /* Keep the linker happy. */
\r
466 void assert_failed( unsigned char* pcFile, unsigned long ulLine )
\r