2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 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. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
31 * documentation provides more details of the standard demo application tasks.
\r
32 * In addition to the standard demo tasks, the following tasks and tests are
\r
33 * defined and/or created within this file:
\r
35 * "Fast Interrupt Test" - A high frequency periodic interrupt is generated
\r
36 * using a free running timer to demonstrate the use of the
\r
37 * configKERNEL_INTERRUPT_PRIORITY configuration constant. The interrupt
\r
38 * service routine measures the number of processor clocks that occur between
\r
39 * each interrupt - and in so doing measures the jitter in the interrupt timing.
\r
40 * The maximum measured jitter time is latched in the ulMaxJitter variable, and
\r
41 * displayed on the LCD by the 'Check' task as described below. The
\r
42 * fast interrupt is configured and handled in the timertest.c source file.
\r
44 * "LCD" task - the LCD task is a 'gatekeeper' task. It is the only task that
\r
45 * is permitted to access the display directly. Other tasks wishing to write a
\r
46 * message to the LCD send the message on a queue to the LCD task instead of
\r
47 * accessing the LCD themselves. The LCD task just blocks on the queue waiting
\r
48 * for messages - waking and displaying the messages as they arrive.
\r
50 * "Check" task - This only executes every five seconds but has the highest
\r
51 * priority so is guaranteed to get processor time. Its main function is to
\r
52 * check that all the standard demo tasks are still operational. Should any
\r
53 * unexpected behaviour within a demo task be discovered the 'check' task will
\r
54 * write an error to the LCD (via the LCD task). If all the demo tasks are
\r
55 * executing with their expected behaviour then the check task writes PASS
\r
56 * along with the max jitter time to the LCD (again via the LCD task), as
\r
61 /* Standard includes. */
\r
64 /* Scheduler includes. */
\r
65 #include "FreeRTOS.h"
\r
69 /* Library includes. */
\r
70 #include "stm32f10x_it.h"
\r
72 /* Demo app includes. */
\r
74 #include "LCD_Message.h"
\r
77 #include "integer.h"
\r
78 #include "blocktim.h"
\r
79 #include "partest.h"
\r
80 #include "semtest.h"
\r
83 #include "comtest2.h"
\r
85 /* Task priorities. */
\r
86 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
87 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
88 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
89 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
90 #define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
91 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
92 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
93 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
95 /* Constants related to the LCD. */
\r
96 #define mainMAX_LINE ( 240 )
\r
97 #define mainROW_INCREMENT ( 24 )
\r
98 #define mainMAX_COLUMN ( 20 )
\r
99 #define mainCOLUMN_START ( 319 )
\r
100 #define mainCOLUMN_INCREMENT ( 16 )
\r
102 /* The maximum number of message that can be waiting for display at any one
\r
104 #define mainLCD_QUEUE_SIZE ( 3 )
\r
106 /* The check task uses the sprintf function so requires a little more stack. */
\r
107 #define mainCHECK_TASK_STACK_SIZE ( configMINIMAL_STACK_SIZE + 50 )
\r
109 /* Dimensions the buffer into which the jitter time is written. */
\r
110 #define mainMAX_MSG_LEN 25
\r
112 /* The time between cycles of the 'check' task. */
\r
113 #define mainCHECK_DELAY ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )
\r
115 /* The number of nano seconds between each processor clock. */
\r
116 #define mainNS_PER_CLOCK ( ( unsigned long ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )
\r
118 /* Baud rate used by the comtest tasks. */
\r
119 #define mainCOM_TEST_BAUD_RATE ( 115200 )
\r
121 /* The LED used by the comtest tasks. See the comtest.c file for more
\r
123 #define mainCOM_TEST_LED ( 3 )
\r
125 /*-----------------------------------------------------------*/
\r
128 * Configure the clocks, GPIO and other peripherals as required by the demo.
\r
130 static void prvSetupHardware( void );
\r
133 * Configure the LCD as required by the demo.
\r
135 static void prvConfigureLCD( void );
\r
138 * The LCD is written two by more than one task so is controlled by a
\r
139 * 'gatekeeper' task. This is the only task that is actually permitted to
\r
140 * access the LCD directly. Other tasks wanting to display a message send
\r
141 * the message to the gatekeeper.
\r
143 static void vLCDTask( void *pvParameters );
\r
146 * Retargets the C library printf function to the USART.
\r
148 int fputc( int ch, FILE *f );
\r
151 * Checks the status of all the demo tasks then prints a message to the
\r
152 * display. The message will be either PASS - and include in brackets the
\r
153 * maximum measured jitter time (as described at the to of the file), or a
\r
154 * message that describes which of the standard demo tasks an error has been
\r
157 * Messages are not written directly to the terminal, but passed to vLCDTask
\r
160 static void vCheckTask( void *pvParameters );
\r
163 * Configures the timers and interrupts for the fast interrupt test as
\r
164 * described at the top of this file.
\r
166 extern void vSetupTimerTest( void );
\r
168 /*-----------------------------------------------------------*/
\r
170 /* The queue used to send messages to the LCD task. */
\r
171 QueueHandle_t xLCDQueue;
\r
173 /*-----------------------------------------------------------*/
\r
181 prvSetupHardware();
\r
183 /* Create the queue used by the LCD task. Messages for display on the LCD
\r
184 are received via this queue. */
\r
185 xLCDQueue = xQueueCreate( mainLCD_QUEUE_SIZE, sizeof( xLCDMessage ) );
\r
187 /* Start the standard demo tasks. */
\r
188 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
189 vCreateBlockTimeTasks();
\r
190 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
191 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
192 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
193 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
194 vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );
\r
196 /* Start the tasks defined within this file/specific to this demo. */
\r
197 xTaskCreate( vCheckTask, "Check", mainCHECK_TASK_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
198 xTaskCreate( vLCDTask, "LCD", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
200 /* The suicide tasks must be created last as they need to know how many
\r
201 tasks were running prior to their creation in order to ascertain whether
\r
202 or not the correct/expected number of tasks are running at any given time. */
\r
203 vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
\r
205 /* Configure the timers used by the fast interrupt timer test. */
\r
208 /* Start the scheduler. */
\r
209 vTaskStartScheduler();
\r
211 /* Will only get here if there was not enough heap space to create the
\r
215 /*-----------------------------------------------------------*/
\r
217 void vLCDTask( void *pvParameters )
\r
219 xLCDMessage xMessage;
\r
221 /* Initialise the LCD and display a startup message. */
\r
223 LCD_DrawMonoPict( ( unsigned long * ) pcBitmap );
\r
227 /* Wait for a message to arrive that requires displaying. */
\r
228 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
\r
230 /* Display the message. Print each message to a different position. */
\r
231 printf( ( char const * ) xMessage.pcMessage );
\r
234 /*-----------------------------------------------------------*/
\r
236 static void vCheckTask( void *pvParameters )
\r
238 TickType_t xLastExecutionTime;
\r
239 xLCDMessage xMessage;
\r
240 static signed char cPassMessage[ mainMAX_MSG_LEN ];
\r
241 extern unsigned short usMaxJitter;
\r
243 xLastExecutionTime = xTaskGetTickCount();
\r
244 xMessage.pcMessage = cPassMessage;
\r
248 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
249 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
\r
251 /* Has an error been found in any task? */
\r
253 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
255 xMessage.pcMessage = "ERROR IN BLOCK Q\n";
\r
257 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
259 xMessage.pcMessage = "ERROR IN BLOCK TIME\n";
\r
261 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
263 xMessage.pcMessage = "ERROR IN SEMAPHORE\n";
\r
265 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
267 xMessage.pcMessage = "ERROR IN POLL Q\n";
\r
269 else if( xIsCreateTaskStillRunning() != pdTRUE )
\r
271 xMessage.pcMessage = "ERROR IN CREATE\n";
\r
273 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
275 xMessage.pcMessage = "ERROR IN MATH\n";
\r
277 else if( xAreComTestTasksStillRunning() != pdTRUE )
\r
279 xMessage.pcMessage = "ERROR IN COM TEST\n";
\r
283 sprintf( ( char * ) cPassMessage, "PASS [%uns]\n", ( ( unsigned long ) usMaxJitter ) * mainNS_PER_CLOCK );
\r
286 /* Send the message to the LCD gatekeeper for display. */
\r
287 xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );
\r
290 /*-----------------------------------------------------------*/
\r
292 static void prvSetupHardware( void )
\r
294 /* Start with the clocks in their expected state. */
\r
297 /* Enable HSE (high speed external clock). */
\r
298 RCC_HSEConfig( RCC_HSE_ON );
\r
300 /* Wait till HSE is ready. */
\r
301 while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )
\r
305 /* 2 wait states required on the flash. */
\r
306 *( ( unsigned long * ) 0x40022000 ) = 0x02;
\r
308 /* HCLK = SYSCLK */
\r
309 RCC_HCLKConfig( RCC_SYSCLK_Div1 );
\r
312 RCC_PCLK2Config( RCC_HCLK_Div1 );
\r
314 /* PCLK1 = HCLK/2 */
\r
315 RCC_PCLK1Config( RCC_HCLK_Div2 );
\r
317 /* PLLCLK = 8MHz * 9 = 72 MHz. */
\r
318 RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_9 );
\r
321 RCC_PLLCmd( ENABLE );
\r
323 /* Wait till PLL is ready. */
\r
324 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
\r
328 /* Select PLL as system clock source. */
\r
329 RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );
\r
331 /* Wait till PLL is used as system clock source. */
\r
332 while( RCC_GetSYSCLKSource() != 0x08 )
\r
336 /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
\r
337 RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
\r
338 | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );
\r
340 /* SPI2 Periph clock enable */
\r
341 RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );
\r
344 /* Set the Vector Table base address at 0x08000000 */
\r
345 NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );
\r
347 NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
\r
349 /* Configure HCLK clock as SysTick clock source. */
\r
350 SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );
\r
352 vParTestInitialise();
\r
354 /*-----------------------------------------------------------*/
\r
356 static void prvConfigureLCD( void )
\r
358 GPIO_InitTypeDef GPIO_InitStructure;
\r
360 /* Configure LCD Back Light (PA8) as output push-pull */
\r
361 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
\r
362 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
\r
363 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
\r
364 GPIO_Init( GPIOA, &GPIO_InitStructure );
\r
366 /* Set the Backlight Pin */
\r
367 GPIO_WriteBit(GPIOA, GPIO_Pin_8, Bit_SET);
\r
369 /* Initialize the LCD */
\r
372 /* Set the Back Color */
\r
373 LCD_SetBackColor( White );
\r
375 /* Set the Text Color */
\r
376 LCD_SetTextColor( 0x051F );
\r
380 /*-----------------------------------------------------------*/
\r
382 int fputc( int ch, FILE *f )
\r
384 static unsigned short usColumn = 0, usRefColumn = mainCOLUMN_START;
\r
385 static unsigned char ucLine = 0;
\r
387 if( ( usColumn == 0 ) && ( ucLine == 0 ) )
\r
394 /* Display one character on LCD */
\r
395 LCD_DisplayChar( ucLine, usRefColumn, (u8) ch );
\r
397 /* Decrement the column position by 16 */
\r
398 usRefColumn -= mainCOLUMN_INCREMENT;
\r
400 /* Increment the character counter */
\r
402 if( usColumn == mainMAX_COLUMN )
\r
404 ucLine += mainROW_INCREMENT;
\r
405 usRefColumn = mainCOLUMN_START;
\r
411 /* Move back to the first column of the next line. */
\r
412 ucLine += mainROW_INCREMENT;
\r
413 usRefColumn = mainCOLUMN_START;
\r
417 /* Wrap back to the top of the display. */
\r
418 if( ucLine >= mainMAX_LINE )
\r
425 /*-----------------------------------------------------------*/
\r
428 /* Keep the linker happy. */
\r
429 void assert_failed( unsigned char* pcFile, unsigned long ulLine )
\r