2 * FreeRTOS Kernel V10.3.0
\r
3 * Copyright (C) 2020 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. Messages
\r
48 * can either be a text string to display, or an instruction to update MEMS
\r
49 * input. The MEMS input is used to display a ball that can be moved around
\r
50 * LCD by tilting the STM32 Primer. 45% is taken as the neutral position.
\r
52 * "Check" task - This only executes every five seconds but has the highest
\r
53 * priority so is guaranteed to get processor time. Its main function is to
\r
54 * check that all the standard demo tasks are still operational. Should any
\r
55 * unexpected behaviour within a demo task be discovered the 'check' task will
\r
56 * write an error to the LCD (via the LCD task). If all the demo tasks are
\r
57 * executing with their expected behaviour then the check task writes PASS
\r
58 * along with the max jitter time to the LCD (again via the LCD task), as
\r
61 * Tick Hook - A tick hook is provided just for demonstration purposes. In
\r
62 * this case it is used to periodically send an instruction to updated the
\r
63 * MEMS input to the LCD task.
\r
67 /* CircleOS includes. Some of the CircleOS peripheral functionality is
\r
68 utilised, although CircleOS itself is not used. */
\r
71 /* Standard includes. */
\r
74 /* Scheduler includes. */
\r
75 #include "FreeRTOS.h"
\r
79 /* Demo app includes. */
\r
81 #include "blocktim.h"
\r
82 #include "GenQTest.h"
\r
83 #include "partest.h"
\r
86 /* The bitmap used to display the FreeRTOS.org logo is stored in 16bit format
\r
87 and therefore takes up a large proportion of the Flash space. Setting this
\r
88 parameter to 0 excludes the bitmap from the build, freeing up Flash space for
\r
90 #define mainINCLUDE_BITMAP 0
\r
92 #if mainINCLUDE_BITMAP == 1
\r
96 /* Task priorities. */
\r
97 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
98 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
99 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
100 #define mainGEN_Q_PRIORITY ( tskIDLE_PRIORITY + 0 )
\r
101 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
103 /* Splash screen related constants. */
\r
104 #define mainBITMAP_Y ( 38 )
\r
105 #define mainBITMAP_X ( 18 )
\r
106 #define mainURL_Y ( 8 )
\r
107 #define mainURL_X ( 78 )
\r
108 #define mainSPLASH_SCREEN_DELAY ( 2000 / portTICK_PERIOD_MS )
\r
110 /* Text drawing related constants. */
\r
111 #define mainLCD_CHAR_HEIGHT ( 13 )
\r
112 #define mainLCD_MAX_Y ( 110 )
\r
114 /* The maximum number of message that can be waiting for display at any one
\r
116 #define mainLCD_QUEUE_SIZE ( 3 )
\r
118 /* The check task uses the sprintf function so requires a little more stack. */
\r
119 #define mainCHECK_TASK_STACK_SIZE ( configMINIMAL_STACK_SIZE + 50 )
\r
121 /* The LCD task calls some of the CircleOS functions (for MEMS and LCD access),
\r
122 these can require a larger stack. */
\r
123 #define configLCD_TASK_STACK_SIZE ( configMINIMAL_STACK_SIZE + 50 )
\r
125 /* Dimensions the buffer into which the jitter time is written. */
\r
126 #define mainMAX_MSG_LEN 25
\r
128 /* The time between cycles of the 'check' task. */
\r
129 #define mainCHECK_DELAY ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )
\r
131 /* The period at which the MEMS input should be updated. */
\r
132 #define mainMEMS_DELAY ( ( TickType_t ) 100 / portTICK_PERIOD_MS )
\r
134 /* The rate at which the flash task toggles the LED. */
\r
135 #define mainFLASH_DELAY ( ( TickType_t ) 1000 / portTICK_PERIOD_MS )
\r
137 /* The number of nano seconds between each processor clock. */
\r
138 #define mainNS_PER_CLOCK ( ( unsigned long ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )
\r
140 /* The two types of message that can be sent to the LCD task. */
\r
141 #define mainUPDATE_BALL_MESSAGE ( 0 )
\r
142 #define mainWRITE_STRING_MESSAGE ( 1 )
\r
144 /* Type of the message sent to the LCD task. */
\r
147 portBASE_TYPE xMessageType;
\r
148 signed char *pcMessage;
\r
151 /*-----------------------------------------------------------*/
\r
154 * Configure the clocks, GPIO and other peripherals as required by the demo.
\r
156 static void prvSetupHardware( void );
\r
159 * The LCD is written two by more than one task so is controlled by a
\r
160 * 'gatekeeper' task. This is the only task that is actually permitted to
\r
161 * access the LCD directly. Other tasks wanting to display a message send
\r
162 * the message to the gatekeeper.
\r
164 static void prvLCDTask( void *pvParameters );
\r
167 * Checks the status of all the demo tasks then prints a message to the
\r
168 * display. The message will be either PASS - and include in brackets the
\r
169 * maximum measured jitter time (as described at the to of the file), or a
\r
170 * message that describes which of the standard demo tasks an error has been
\r
173 * Messages are not written directly to the terminal, but passed to prvLCDTask
\r
176 * The check task also receives instructions to update the MEMS input, which
\r
177 * in turn can also lead to the LCD being updated.
\r
179 static void prvCheckTask( void *pvParameters );
\r
182 * Configures the timers and interrupts for the fast interrupt test as
\r
183 * described at the top of this file.
\r
185 extern void vSetupTimerTest( void );
\r
188 * A cut down version of sprintf() used to percent the HUGE GCC library
\r
189 * equivalent from being included in the binary image.
\r
191 extern int sprintf(char *out, const char *format, ...);
\r
194 * Simple toggle the LED periodically for timing verification.
\r
196 static void prvFlashTask( void *pvParameters );
\r
198 /*-----------------------------------------------------------*/
\r
200 /* The queue used to send messages to the LCD task. */
\r
201 QueueHandle_t xLCDQueue;
\r
203 /*-----------------------------------------------------------*/
\r
211 prvSetupHardware();
\r
213 /* Create the queue used by the LCD task. Messages for display on the LCD
\r
214 are received via this queue. */
\r
215 xLCDQueue = xQueueCreate( mainLCD_QUEUE_SIZE, sizeof( xLCDMessage ) );
\r
217 /* Start the standard demo tasks. */
\r
218 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
219 vCreateBlockTimeTasks();
\r
220 vStartGenericQueueTasks( mainGEN_Q_PRIORITY );
\r
221 vStartQueuePeekTasks();
\r
222 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
224 /* Start the tasks defined within this file/specific to this demo. */
\r
225 xTaskCreate( prvCheckTask, "Check", mainCHECK_TASK_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
\r
226 xTaskCreate( prvLCDTask, "LCD", configLCD_TASK_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
227 xTaskCreate( prvFlashTask, "Flash", configMINIMAL_STACK_SIZE, NULL, mainFLASH_TASK_PRIORITY, NULL );
\r
229 /* Configure the timers used by the fast interrupt timer test. */
\r
232 /* Start the scheduler. */
\r
233 vTaskStartScheduler();
\r
235 /* Will only get here if there was not enough heap space to create the
\r
239 /*-----------------------------------------------------------*/
\r
241 void prvLCDTask( void *pvParameters )
\r
243 xLCDMessage xMessage;
\r
244 char cY = mainLCD_CHAR_HEIGHT;
\r
245 const char * const pcString = "www.FreeRTOS.org";
\r
246 const char * const pcBlankLine = " ";
\r
250 #if mainINCLUDE_BITMAP == 1
\r
251 DRAW_SetImage( pucImage, mainBITMAP_Y, mainBITMAP_X, bmpBITMAP_HEIGHT, bmpBITMAP_WIDTH );
\r
254 LCD_SetScreenOrientation( V9 );
\r
255 DRAW_DisplayString( mainURL_Y, mainURL_X, pcString, strlen( pcString ) );
\r
256 vTaskDelay( mainSPLASH_SCREEN_DELAY );
\r
257 LCD_FillRect( 0, 0, CHIP_SCREEN_WIDTH, CHIP_SCREEN_HEIGHT, RGB_WHITE );
\r
261 /* Wait for a message to arrive that requires displaying. */
\r
262 while( xQueueReceive( xLCDQueue, &xMessage, portMAX_DELAY ) != pdPASS );
\r
264 /* Check the message type. */
\r
265 if( xMessage.xMessageType == mainUPDATE_BALL_MESSAGE )
\r
267 /* Read the MEMS and update the ball display on the LCD if required. */
\r
273 /* A text string was sent. First blank off the old text string, then
\r
274 draw the new text on the next line down. */
\r
275 DRAW_DisplayString( 0, cY, pcBlankLine, strlen( pcBlankLine ) );
\r
277 cY -= mainLCD_CHAR_HEIGHT;
\r
278 if( cY <= ( mainLCD_CHAR_HEIGHT - 1 ) )
\r
280 /* Wrap the line onto which we are going to write the text. */
\r
281 cY = mainLCD_MAX_Y;
\r
284 /* Display the message. */
\r
285 DRAW_DisplayString( 0, cY, xMessage.pcMessage, strlen( xMessage.pcMessage ) );
\r
289 /*-----------------------------------------------------------*/
\r
291 static void prvCheckTask( void *pvParameters )
\r
293 TickType_t xLastExecutionTime;
\r
294 xLCDMessage xMessage;
\r
295 static signed char cPassMessage[ mainMAX_MSG_LEN ];
\r
296 extern unsigned short usMaxJitter;
\r
298 /* Initialise the xLastExecutionTime variable on task entry. */
\r
299 xLastExecutionTime = xTaskGetTickCount();
\r
301 /* Setup the message we are going to send to the LCD task. */
\r
302 xMessage.xMessageType = mainWRITE_STRING_MESSAGE;
\r
303 xMessage.pcMessage = cPassMessage;
\r
307 /* Perform this check every mainCHECK_DELAY milliseconds. */
\r
308 vTaskDelayUntil( &xLastExecutionTime, mainCHECK_DELAY );
\r
310 /* Has an error been found in any task? If so then point the text
\r
311 we are going to send to the LCD task to an error message instead of
\r
312 the PASS message. */
\r
313 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
315 xMessage.pcMessage = "ERROR IN GEN Q";
\r
317 if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
319 xMessage.pcMessage = "ERROR IN BLOCK Q";
\r
321 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
323 xMessage.pcMessage = "ERROR IN BLOCK TIME";
\r
325 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
327 xMessage.pcMessage = "ERROR IN POLL Q";
\r
329 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
331 xMessage.pcMessage = "ERROR IN PEEK Q";
\r
335 /* No errors were found in any task, so send a pass message
\r
336 with the max measured jitter time also included (as per the
\r
337 fast interrupt test described at the top of this file and on
\r
338 the online documentation page for this demo application). */
\r
339 sprintf( ( char * ) cPassMessage, "PASS [%uns]", ( ( unsigned long ) usMaxJitter ) * mainNS_PER_CLOCK );
\r
342 /* Send the message to the LCD gatekeeper for display. */
\r
343 xQueueSend( xLCDQueue, &xMessage, portMAX_DELAY );
\r
346 /*-----------------------------------------------------------*/
\r
348 void vApplicationTickHook( void )
\r
350 static unsigned long ulCallCount;
\r
351 static const xLCDMessage xMemsMessage = { mainUPDATE_BALL_MESSAGE, NULL };
\r
352 static portBASE_TYPE xHigherPriorityTaskWoken;
\r
354 /* Periodically send a message to the LCD task telling it to update
\r
355 the MEMS input, and then if necessary the LCD. */
\r
357 if( ulCallCount >= mainMEMS_DELAY )
\r
360 xHigherPriorityTaskWoken = pdFALSE;
\r
361 xQueueSendFromISR( xLCDQueue, &xMemsMessage, &xHigherPriorityTaskWoken );
\r
364 /*-----------------------------------------------------------*/
\r
366 static void prvSetupHardware( void )
\r
368 /* Start with the clocks in their expected state. */
\r
371 /* Enable HSE (high speed external clock). */
\r
372 RCC_HSEConfig( RCC_HSE_ON );
\r
374 /* Wait till HSE is ready. */
\r
375 while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )
\r
379 /* 2 wait states required on the flash. */
\r
380 *( ( unsigned long * ) 0x40022000 ) = 0x02;
\r
382 /* HCLK = SYSCLK */
\r
383 RCC_HCLKConfig( RCC_SYSCLK_Div1 );
\r
386 RCC_PCLK2Config( RCC_HCLK_Div1 );
\r
388 /* PCLK1 = HCLK/2 */
\r
389 RCC_PCLK1Config( RCC_HCLK_Div2 );
\r
391 /* PLLCLK = 12MHz * 6 = 72 MHz. */
\r
392 RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_6 );
\r
395 RCC_PLLCmd( ENABLE );
\r
397 /* Wait till PLL is ready. */
\r
398 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
\r
402 /* Select PLL as system clock source. */
\r
403 RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );
\r
405 /* Wait till PLL is used as system clock source. */
\r
406 while( RCC_GetSYSCLKSource() != 0x08 )
\r
410 /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
\r
411 RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
\r
412 | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );
\r
414 /* SPI2 Periph clock enable */
\r
415 RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );
\r
418 /* Set the Vector Table base address at 0x08000000 */
\r
419 NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );
\r
421 NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
\r
423 /* Configure HCLK clock as SysTick clock source. */
\r
424 SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );
\r
426 /* Misc initialisation, including some of the CircleOS features. Note
\r
427 that CircleOS itself is not used. */
\r
428 vParTestInitialise();
\r
431 POINTER_SetMode( POINTER_RESTORE_LESS );
\r
433 /*-----------------------------------------------------------*/
\r
435 static void prvFlashTask( void *pvParameters )
\r
437 TickType_t xLastExecutionTime;
\r
439 /* Initialise the xLastExecutionTime variable on task entry. */
\r
440 xLastExecutionTime = xTaskGetTickCount();
\r
444 /* Simple toggle the LED periodically. This just provides some timing
\r
446 vTaskDelayUntil( &xLastExecutionTime, mainFLASH_DELAY );
\r
447 vParTestToggleLED( 0 );
\r
450 /*-----------------------------------------------------------*/
\r
452 void starting_delay( unsigned long ul )
\r
454 vTaskDelay( ( TickType_t ) ul );
\r