2 * FreeRTOS Kernel V10.1.0
\r
3 * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
30 * documentation provides more details of the standard demo application tasks.
\r
31 * In addition to the standard demo tasks, the following tasks and tests are
\r
32 * defined and/or created within this file:
\r
34 * "Fast Interrupt Test" - A high frequency periodic interrupt is generated
\r
35 * using a free running timer to demonstrate the use of the
\r
36 * configKERNEL_INTERRUPT_PRIORITY configuration constant. The interrupt
\r
37 * service routine measures the number of processor clocks that occur between
\r
38 * each interrupt - and in so doing measures the jitter in the interrupt timing.
\r
39 * The maximum measured jitter time is latched in the ulMaxJitter variable, and
\r
40 * displayed on the LCD by the 'Check' task as described below. The
\r
41 * fast interrupt is configured and handled in the timertest.c source file.
\r
43 * "LCD" task - the LCD task is a 'gatekeeper' task. It is the only task that
\r
44 * is permitted to access the display directly. Other tasks wishing to write a
\r
45 * message to the LCD send the message on a queue to the LCD task instead of
\r
46 * accessing the LCD themselves. The LCD task just blocks on the queue waiting
\r
47 * for messages - waking and displaying the messages as they arrive.
\r
49 * "Check" task - This only executes every five seconds but has the highest
\r
50 * priority so is guaranteed to get processor time. Its main function is to
\r
51 * check that all the standard demo tasks are still operational. Should any
\r
52 * unexpected behaviour within a demo task be discovered the 'check' task will
\r
53 * write an error to the LCD (via the LCD task). If all the demo tasks are
\r
54 * executing with their expected behaviour then the check task writes PASS
\r
55 * along with the max jitter time to the LCD (again via the LCD task), as
\r
60 /* Standard includes. */
\r
63 /* Scheduler includes. */
\r
64 #include "FreeRTOS.h"
\r
68 /* Library includes. */
\r
69 #include "stm32f10x_it.h"
\r
71 /* Demo app includes. */
\r
73 #include "LCD_Message.h"
\r
76 #include "integer.h"
\r
77 #include "blocktim.h"
\r
78 #include "partest.h"
\r
79 #include "semtest.h"
\r
82 #include "comtest2.h"
\r
84 /* Task priorities. */
\r
85 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
86 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
87 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
88 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
89 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
90 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
91 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
92 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
94 /* Constants related to the LCD. */
\r
95 #define mainMAX_LINE ( 240 )
\r
96 #define mainROW_INCREMENT ( 24 )
\r
97 #define mainMAX_COLUMN ( 20 )
\r
98 #define mainCOLUMN_START ( 319 )
\r
99 #define mainCOLUMN_INCREMENT ( 16 )
\r
101 /* The maximum number of message that can be waiting for display at any one
\r
103 #define mainLCD_QUEUE_SIZE ( 3 )
\r
105 /* The check task uses the sprintf function so requires a little more stack. */
\r
106 #define mainCHECK_TASK_STACK_SIZE ( configMINIMAL_STACK_SIZE + 50 )
\r
108 /* Dimensions the buffer into which the jitter time is written. */
\r
109 #define mainMAX_MSG_LEN 25
\r
111 /* The time between cycles of the 'check' task. */
\r
112 #define mainCHECK_DELAY ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )
\r
114 /* The number of nano seconds between each processor clock. */
\r
115 #define mainNS_PER_CLOCK ( ( unsigned long ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )
\r
117 /* Baud rate used by the comtest tasks. */
\r
118 #define mainCOM_TEST_BAUD_RATE ( 115200 )
\r
120 /* The LED used by the comtest tasks. See the comtest.c file for more
\r
122 #define mainCOM_TEST_LED ( 3 )
\r
124 /*-----------------------------------------------------------*/
\r
127 * Configure the clocks, GPIO and other peripherals as required by the demo.
\r
129 static void prvSetupHardware( void );
\r
132 * Configure the LCD as required by the demo.
\r
134 static void prvConfigureLCD( void );
\r
137 * The LCD is written two by more than one task so is controlled by a
\r
138 * 'gatekeeper' task. This is the only task that is actually permitted to
\r
139 * access the LCD directly. Other tasks wanting to display a message send
\r
140 * the message to the gatekeeper.
\r
142 static void vLCDTask( void *pvParameters );
\r
145 * Retargets the C library printf function to the USART.
\r
147 int fputc( int ch, FILE *f );
\r
150 * Checks the status of all the demo tasks then prints a message to the
\r
151 * display. The message will be either PASS - and include in brackets the
\r
152 * maximum measured jitter time (as described at the to of the file), or a
\r
153 * message that describes which of the standard demo tasks an error has been
\r
156 * Messages are not written directly to the terminal, but passed to vLCDTask
\r
159 static void vCheckTask( void *pvParameters );
\r
162 * Configures the timers and interrupts for the fast interrupt test as
\r
163 * described at the top of this file.
\r
165 extern void vSetupTimerTest( void );
\r
167 /*-----------------------------------------------------------*/
\r
169 /* The queue used to send messages to the LCD task. */
\r
170 QueueHandle_t xLCDQueue;
\r
172 /*-----------------------------------------------------------*/
\r
180 prvSetupHardware();
\r
182 /* Create the queue used by the LCD task. Messages for display on the LCD
\r
183 are received via this queue. */
\r
184 xLCDQueue = xQueueCreate( mainLCD_QUEUE_SIZE, sizeof( xLCDMessage ) );
\r
186 /* Start the standard demo tasks. */
\r
187 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
188 vCreateBlockTimeTasks();
\r
189 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
190 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
191 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
192 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
193 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
195 /* Start the tasks defined within this file/specific to this demo. */
\r
196 xTaskCreate( vCheckTask, "Check", mainCHECK_TASK_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
197 xTaskCreate( vLCDTask, "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
199 /* The suicide tasks must be created last as they need to know how many
\r
200 tasks were running prior to their creation in order to ascertain whether
\r
201 or not the correct/expected number of tasks are running at any given time. */
\r
202 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
204 /* Configure the timers used by the fast interrupt timer test. */
\r
207 /* Start the scheduler. */
\r
208 vTaskStartScheduler();
\r
210 /* Will only get here if there was not enough heap space to create the
\r
214 /*-----------------------------------------------------------*/
\r
216 void vLCDTask( void *pvParameters )
\r
218 xLCDMessage xMessage;
\r
220 /* Initialise the LCD and display a startup message. */
\r
222 LCD_DrawMonoPict( ( unsigned long * ) pcBitmap );
\r
226 /* Wait for a message to arrive that requires displaying. */
\r
227 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
\r
229 /* Display the message. Print each message to a different position. */
\r
230 printf( ( char const * ) xMessage.pcMessage );
\r
233 /*-----------------------------------------------------------*/
\r
235 static void vCheckTask( void *pvParameters )
\r
237 TickType_t xLastExecutionTime;
\r
238 xLCDMessage xMessage;
\r
239 static signed char cPassMessage[ mainMAX_MSG_LEN ];
\r
240 extern unsigned short usMaxJitter;
\r
242 xLastExecutionTime = xTaskGetTickCount();
\r
243 xMessage.pcMessage = cPassMessage;
\r
247 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
248 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
\r
250 /* Has an error been found in any task? */
\r
252 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
254 xMessage.pcMessage = "ERROR IN BLOCK Q\n";
\r
256 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
258 xMessage.pcMessage = "ERROR IN BLOCK TIME\n";
\r
260 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
262 xMessage.pcMessage = "ERROR IN SEMAPHORE\n";
\r
264 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
266 xMessage.pcMessage = "ERROR IN POLL Q\n";
\r
268 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
270 xMessage.pcMessage = "ERROR IN CREATE\n";
\r
272 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
274 xMessage.pcMessage = "ERROR IN MATH\n";
\r
276 else if( xAreComTestTasksStillRunning() != pdTRUE )
\r
278 xMessage.pcMessage = "ERROR IN COM TEST\n";
\r
282 sprintf( ( char * ) cPassMessage, "PASS [%uns]\n", ( ( unsigned long ) usMaxJitter ) * mainNS_PER_CLOCK );
\r
285 /* Send the message to the LCD gatekeeper for display. */
\r
286 xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );
\r
289 /*-----------------------------------------------------------*/
\r
291 static void prvSetupHardware( void )
\r
293 /* Start with the clocks in their expected state. */
\r
296 /* Enable HSE (high speed external clock). */
\r
297 RCC_HSEConfig( RCC_HSE_ON );
\r
299 /* Wait till HSE is ready. */
\r
300 while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )
\r
304 /* 2 wait states required on the flash. */
\r
305 *( ( unsigned long * ) 0x40022000 ) = 0x02;
\r
307 /* HCLK = SYSCLK */
\r
308 RCC_HCLKConfig( RCC_SYSCLK_Div1 );
\r
311 RCC_PCLK2Config( RCC_HCLK_Div1 );
\r
313 /* PCLK1 = HCLK/2 */
\r
314 RCC_PCLK1Config( RCC_HCLK_Div2 );
\r
316 /* PLLCLK = 8MHz * 9 = 72 MHz. */
\r
317 RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_9 );
\r
320 RCC_PLLCmd( ENABLE );
\r
322 /* Wait till PLL is ready. */
\r
323 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
\r
327 /* Select PLL as system clock source. */
\r
328 RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );
\r
330 /* Wait till PLL is used as system clock source. */
\r
331 while( RCC_GetSYSCLKSource() != 0x08 )
\r
335 /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
\r
336 RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
\r
337 | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );
\r
339 /* SPI2 Periph clock enable */
\r
340 RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );
\r
343 /* Set the Vector Table base address at 0x08000000 */
\r
344 NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );
\r
346 NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
\r
348 /* Configure HCLK clock as SysTick clock source. */
\r
349 SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );
\r
351 vParTestInitialise();
\r
353 /*-----------------------------------------------------------*/
\r
355 static void prvConfigureLCD( void )
\r
357 GPIO_InitTypeDef GPIO_InitStructure;
\r
359 /* Configure LCD Back Light (PA8) as output push-pull */
\r
360 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
\r
361 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
\r
362 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
\r
363 GPIO_Init( GPIOA, &GPIO_InitStructure );
\r
365 /* Set the Backlight Pin */
\r
366 GPIO_WriteBit(GPIOA, GPIO_Pin_8, Bit_SET);
\r
368 /* Initialize the LCD */
\r
371 /* Set the Back Color */
\r
372 LCD_SetBackColor( White );
\r
374 /* Set the Text Color */
\r
375 LCD_SetTextColor( 0x051F );
\r
379 /*-----------------------------------------------------------*/
\r
381 int fputc( int ch, FILE *f )
\r
383 static unsigned short usColumn = 0, usRefColumn = mainCOLUMN_START;
\r
384 static unsigned char ucLine = 0;
\r
386 if( ( usColumn == 0 ) && ( ucLine == 0 ) )
\r
393 /* Display one character on LCD */
\r
394 LCD_DisplayChar( ucLine, usRefColumn, (u8) ch );
\r
396 /* Decrement the column position by 16 */
\r
397 usRefColumn -= mainCOLUMN_INCREMENT;
\r
399 /* Increment the character counter */
\r
401 if( usColumn == mainMAX_COLUMN )
\r
403 ucLine += mainROW_INCREMENT;
\r
404 usRefColumn = mainCOLUMN_START;
\r
410 /* Move back to the first column of the next line. */
\r
411 ucLine += mainROW_INCREMENT;
\r
412 usRefColumn = mainCOLUMN_START;
\r
416 /* Wrap back to the top of the display. */
\r
417 if( ucLine >= mainMAX_LINE )
\r
424 /*-----------------------------------------------------------*/
\r
427 /* Keep the linker happy. */
\r
428 void assert_failed( unsigned char* pcFile, unsigned long ulLine )
\r