2 * FreeRTOS Kernel V10.2.1
\r
3 * Copyright (C) 2019 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
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 * (which just exist to test the kernel port and provide an example of how to use
\r
33 * each FreeRTOS API function).
\r
35 * In addition to the standard demo tasks, the following tasks and tests are
\r
36 * defined and/or created within this file:
\r
38 * "LCD" task - the LCD task is a 'gatekeeper' task. It is the only task that
\r
39 * is permitted to access the display directly. Other tasks wishing to write a
\r
40 * message to the LCD send the message on a queue to the LCD task instead of
\r
41 * accessing the LCD themselves. The LCD task just blocks on the queue waiting
\r
42 * for messages - waking and displaying the messages as they arrive. The use
\r
43 * of a gatekeeper in this manner permits both tasks and interrupts to write to
\r
44 * the LCD without worrying about mutual exclusion. This is demonstrated by the
\r
45 * check hook (see below) which sends messages to the display even though it
\r
46 * executes from an interrupt context.
\r
48 * "Check" hook - This only executes fully every five seconds from the tick
\r
49 * hook. Its main function is to check that all the standard demo tasks are
\r
50 * still operational. Should any unexpected behaviour be discovered within a
\r
51 * demo task then the tick hook will write an error to the LCD (via the LCD task).
\r
52 * If all the demo tasks are executing with their expected behaviour then the
\r
53 * check task writes PASS to the LCD (again via the LCD task), as described above.
\r
55 * LED tasks - These just demonstrate how multiple instances of a single task
\r
56 * definition can be created. Each LED task simply toggles an LED. The task
\r
57 * parameter is used to pass the number of the LED to be toggled into the task.
\r
59 * "uIP" task - This is the task that handles the uIP stack. All TCP/IP
\r
60 * processing is performed in this task.
\r
62 * "Fast Interrupt Test" - A high frequency periodic interrupt is generated
\r
63 * using a free running timer to demonstrate the use of the
\r
64 * configKERNEL_INTERRUPT_PRIORITY configuration constant. The interrupt
\r
65 * service routine measures the number of processor clocks that occur between
\r
66 * each interrupt - and in so doing measures the jitter in the interrupt timing.
\r
67 * The maximum measured jitter time is latched in the ulMaxJitter variable, and
\r
68 * displayed on the OLED display by the 'OLED' task as described below. The
\r
69 * fast interrupt is configured and handled in the timertest.c source file.
\r
73 /* Standard includes. */
\r
76 /* Scheduler includes. */
\r
77 #include "FreeRTOS.h"
\r
82 /* Library includes. */
\r
83 #include "stm32f10x_it.h"
\r
84 #include "stm32f10x_tim.h"
\r
85 #include "STM3210D_lcd.h"
\r
87 /* Demo app includes. */
\r
89 #include "integer.h"
\r
91 #include "partest.h"
\r
92 #include "semtest.h"
\r
94 #include "GenQTest.h"
\r
96 #include "recmutex.h"
\r
99 /* The time between cycles of the 'check' functionality (defined within the
\r
101 #define mainCHECK_DELAY ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )
\r
103 /* Task priorities. */
\r
104 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
105 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
106 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
107 #define mainUIP_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
108 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
109 #define mainLCD_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
110 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
111 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
113 /* The WEB server has a larger stack as it utilises stack hungry string
\r
114 handling library calls. */
\r
115 #define mainBASIC_WEB_STACK_SIZE ( configMINIMAL_STACK_SIZE * 4 )
\r
117 /* The length of the queue used to send messages to the LCD task. */
\r
118 #define mainQUEUE_SIZE ( 3 )
\r
120 /* The period of the system clock in nano seconds. This is used to calculate
\r
121 the jitter time in nano seconds. */
\r
122 #define mainNS_PER_CLOCK ( ( unsigned long ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )
\r
124 /*-----------------------------------------------------------*/
\r
127 * Configure the hardware for the demo.
\r
129 static void prvSetupHardware( void );
\r
132 * Very simple task that toggles an LED.
\r
134 static void prvLCDTask( void *pvparameters );
\r
137 * The task that handles the uIP stack. All TCP/IP processing is performed in
\r
140 extern void vuIP_Task( void *pvParameters );
\r
143 * The LCD gatekeeper task as described in the comments at the top of this file.
\r
145 static void prvLCDTask( void *pvParameters );
\r
148 * Configures the high frequency timers - those used to measure the timing
\r
149 * jitter while the real time kernel is executing.
\r
151 extern void vSetupHighFrequencyTimer( void );
\r
153 /*-----------------------------------------------------------*/
\r
155 /* The queue used to send messages to the LCD task. */
\r
156 QueueHandle_t xLCDQueue;
\r
158 /*-----------------------------------------------------------*/
\r
166 prvSetupHardware();
\r
168 /* Start the standard demo tasks. These are just here to exercise the
\r
169 kernel port and provide examples of how the FreeRTOS API can be used. */
\r
170 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
171 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
172 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
173 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
174 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
175 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
176 vStartQueuePeekTasks();
\r
177 vStartRecursiveMutexTasks();
\r
179 /* Create the uIP task. The WEB server runs in this task. */
\r
180 xTaskCreate( vuIP_Task, "uIP", mainBASIC_WEB_STACK_SIZE, ( void * ) NULL, mainUIP_TASK_PRIORITY, NULL );
\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( mainQUEUE_SIZE, sizeof( char * ) );
\r
186 /* Start the LCD gatekeeper task - as described in the comments at the top
\r
188 xTaskCreate( prvLCDTask, "LCD", configMINIMAL_STACK_SIZE * 2, NULL, mainLCD_TASK_PRIORITY, NULL );
\r
190 /* Configure the high frequency interrupt used to measure the interrupt
\r
191 jitter time. When debugging it can be helpful to comment this line out
\r
192 to prevent the debugger repeatedly going into the interrupt service
\r
194 vSetupHighFrequencyTimer();
\r
196 /* Start the scheduler. */
\r
197 vTaskStartScheduler();
\r
199 /* Will only get here if there was insufficient memory to create the idle
\r
200 task. The idle task is created within vTaskStartScheduler(). */
\r
203 /*-----------------------------------------------------------*/
\r
205 static void prvLCDTask( void *pvParameters )
\r
207 unsigned char *pucMessage;
\r
208 unsigned long ulLine = Line3;
\r
209 const unsigned long ulLineHeight = 24;
\r
210 static char cMsgBuf[ 30 ];
\r
211 extern unsigned short usMaxJitter;
\r
213 ( void ) pvParameters;
\r
215 /* The LCD gatekeeper task as described in the comments at the top of this
\r
218 /* Initialise the LCD and display a startup message that includes the
\r
219 configured IP address. */
\r
220 STM3210D_LCD_Init();
\r
222 LCD_SetTextColor(Green);
\r
223 LCD_DisplayStringLine( Line0, ( unsigned char * ) " www.FreeRTOS.org" );
\r
224 LCD_SetTextColor(Blue);
\r
225 sprintf( cMsgBuf, " %d.%d.%d.%d", configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
226 LCD_DisplayStringLine( Line1, ( unsigned char * ) cMsgBuf );
\r
227 LCD_SetTextColor(Black);
\r
231 /* Wait for a message to arrive to be displayed. */
\r
232 xQueueReceive( xLCDQueue, &pucMessage, portMAX_DELAY );
\r
234 /* Clear the current line of text. */
\r
235 LCD_ClearLine( ulLine );
\r
237 /* Move on to the next line. */
\r
238 ulLine += ulLineHeight;
\r
239 if( ulLine > Line9 )
\r
244 /* Display the received text, and the max jitter value. */
\r
245 sprintf( cMsgBuf, "%s [%uns]", pucMessage, usMaxJitter * mainNS_PER_CLOCK );
\r
246 LCD_DisplayStringLine( ulLine, ( unsigned char * ) cMsgBuf );
\r
249 /*-----------------------------------------------------------*/
\r
251 static void prvSetupHardware( void )
\r
253 /* Start with the clocks in their expected state. */
\r
256 /* Enable HSE (high speed external clock). */
\r
257 RCC_HSEConfig( RCC_HSE_ON );
\r
259 /* Wait till HSE is ready. */
\r
260 while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )
\r
264 /* 2 wait states required on the flash. */
\r
265 *( ( unsigned long * ) 0x40022000 ) = 0x02;
\r
267 /* HCLK = SYSCLK */
\r
268 RCC_HCLKConfig( RCC_SYSCLK_Div1 );
\r
271 RCC_PCLK2Config( RCC_HCLK_Div1 );
\r
273 /* PCLK1 = HCLK/2 */
\r
274 RCC_PCLK1Config( RCC_HCLK_Div2 );
\r
276 /* PLLCLK = (25MHz / 2 ) * 5 = 62.5 MHz. */
\r
277 RCC_PLLConfig( RCC_PLLSource_HSE_Div2, RCC_PLLMul_5 );
\r
280 RCC_PLLCmd( ENABLE );
\r
282 /* Wait till PLL is ready. */
\r
283 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
\r
287 /* Select PLL as system clock source. */
\r
288 RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );
\r
290 /* Wait till PLL is used as system clock source. */
\r
291 while( RCC_GetSYSCLKSource() != 0x08 )
\r
295 /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
\r
296 RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
\r
297 | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );
\r
299 /* Set the Vector Table base address at 0x08000000 */
\r
300 NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );
\r
302 NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
\r
304 /* Configure HCLK clock as SysTick clock source. */
\r
305 SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );
\r
307 /* Initialise the IO used for the LED outputs. */
\r
308 vParTestInitialise();
\r
310 /*-----------------------------------------------------------*/
\r
312 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
314 /* This function will get called if a task overflows its stack. If the
\r
315 parameters are corrupt then inspect pxCurrentTCB to find which was the
\r
319 ( void ) pcTaskName;
\r
323 /*-----------------------------------------------------------*/
\r
325 void vApplicationTickHook( void )
\r
327 char *pcMessage = "Status: PASS";
\r
328 static unsigned long ulTicksSinceLastDisplay = 0;
\r
329 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
331 /* Called from every tick interrupt as described in the comments at the top
\r
334 Have enough ticks passed to make it time to perform our health status
\r
336 ulTicksSinceLastDisplay++;
\r
337 if( ulTicksSinceLastDisplay >= mainCHECK_DELAY )
\r
339 /* Reset the counter so these checks run again in mainCHECK_DELAY
\r
341 ulTicksSinceLastDisplay = 0;
\r
343 /* Has an error been found in any task? */
\r
344 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
346 pcMessage = "ERROR: GEN Q";
\r
348 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
350 pcMessage = "ERROR: PEEK Q";
\r
352 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
354 pcMessage = "ERROR: BLOCK Q";
\r
356 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
358 pcMessage = "ERROR: SEMAPHR";
\r
360 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
362 pcMessage = "ERROR: POLL Q";
\r
364 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
366 pcMessage = "ERROR: INT MATH";
\r
368 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
370 pcMessage = "ERROR: REC MUTEX";
\r
373 /* Send the message to the OLED gatekeeper for display. The
\r
374 xHigherPriorityTaskWoken parameter is not actually used here
\r
375 as this function is running in the tick interrupt anyway - but
\r
376 it must still be supplied. */
\r
377 xHigherPriorityTaskWoken = pdFALSE;
\r
378 xQueueSendFromISR( xLCDQueue, &pcMessage, &xHigherPriorityTaskWoken );
\r
381 /*-----------------------------------------------------------*/
\r