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
31 * Creates all the demo application tasks, then starts the scheduler. The WEB
\r
32 * documentation provides more details of the standard demo application tasks
\r
33 * (which just exist to test the kernel port and provide an example of how to use
\r
34 * each FreeRTOS API function).
\r
36 * In addition to the standard demo tasks, the following tasks and tests are
\r
37 * defined and/or created within this file:
\r
39 * "LCD" task - the LCD task is a 'gatekeeper' task. It is the only task that
\r
40 * is permitted to access the display directly. Other tasks wishing to write a
\r
41 * message to the LCD send the message on a queue to the LCD task instead of
\r
42 * accessing the LCD themselves. The LCD task just blocks on the queue waiting
\r
43 * for messages - waking and displaying the messages as they arrive. The use
\r
44 * of a gatekeeper in this manner permits both tasks and interrupts to write to
\r
45 * the LCD without worrying about mutual exclusion. This is demonstrated by the
\r
46 * check hook (see below) which sends messages to the display even though it
\r
47 * executes from an interrupt context.
\r
49 * "Check" hook - This only executes fully every five seconds from the tick
\r
50 * hook. Its main function is to check that all the standard demo tasks are
\r
51 * still operational. Should any unexpected behaviour be discovered within a
\r
52 * demo task then the tick hook will write an error to the LCD (via the LCD task).
\r
53 * If all the demo tasks are executing with their expected behaviour then the
\r
54 * check task writes PASS to the LCD (again via the LCD task), as described above.
\r
56 * LED tasks - These just demonstrate how multiple instances of a single task
\r
57 * definition can be created. Each LED task simply toggles an LED. The task
\r
58 * parameter is used to pass the number of the LED to be toggled into the task.
\r
60 * "uIP" task - This is the task that handles the uIP stack. All TCP/IP
\r
61 * processing is performed in this task.
\r
63 * "Fast Interrupt Test" - A high frequency periodic interrupt is generated
\r
64 * using a free running timer to demonstrate the use of the
\r
65 * configKERNEL_INTERRUPT_PRIORITY configuration constant. The interrupt
\r
66 * service routine measures the number of processor clocks that occur between
\r
67 * each interrupt - and in so doing measures the jitter in the interrupt timing.
\r
68 * The maximum measured jitter time is latched in the ulMaxJitter variable, and
\r
69 * displayed on the OLED display by the 'OLED' task as described below. The
\r
70 * fast interrupt is configured and handled in the timertest.c source file.
\r
74 /* Standard includes. */
\r
77 /* Scheduler includes. */
\r
78 #include "FreeRTOS.h"
\r
83 /* Library includes. */
\r
84 #include "stm32f10x_it.h"
\r
85 #include "stm32f10x_tim.h"
\r
86 #include "STM3210D_lcd.h"
\r
88 /* Demo app includes. */
\r
90 #include "integer.h"
\r
92 #include "partest.h"
\r
93 #include "semtest.h"
\r
95 #include "GenQTest.h"
\r
97 #include "recmutex.h"
\r
100 /* The time between cycles of the 'check' functionality (defined within the
\r
102 #define mainCHECK_DELAY ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )
\r
104 /* Task priorities. */
\r
105 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
106 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
107 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
108 #define mainUIP_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
109 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
110 #define mainLCD_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
111 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
112 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
114 /* The WEB server has a larger stack as it utilises stack hungry string
\r
115 handling library calls. */
\r
116 #define mainBASIC_WEB_STACK_SIZE ( configMINIMAL_STACK_SIZE * 4 )
\r
118 /* The length of the queue used to send messages to the LCD task. */
\r
119 #define mainQUEUE_SIZE ( 3 )
\r
121 /* The period of the system clock in nano seconds. This is used to calculate
\r
122 the jitter time in nano seconds. */
\r
123 #define mainNS_PER_CLOCK ( ( unsigned long ) ( ( 1.0 / ( double ) configCPU_CLOCK_HZ ) * 1000000000.0 ) )
\r
125 /*-----------------------------------------------------------*/
\r
128 * Configure the hardware for the demo.
\r
130 static void prvSetupHardware( void );
\r
133 * Very simple task that toggles an LED.
\r
135 static void prvLCDTask( void *pvparameters );
\r
138 * The task that handles the uIP stack. All TCP/IP processing is performed in
\r
141 extern void vuIP_Task( void *pvParameters );
\r
144 * The LCD gatekeeper task as described in the comments at the top of this file.
\r
146 static void prvLCDTask( void *pvParameters );
\r
149 * Configures the high frequency timers - those used to measure the timing
\r
150 * jitter while the real time kernel is executing.
\r
152 extern void vSetupHighFrequencyTimer( void );
\r
154 /*-----------------------------------------------------------*/
\r
156 /* The queue used to send messages to the LCD task. */
\r
157 QueueHandle_t xLCDQueue;
\r
159 /*-----------------------------------------------------------*/
\r
167 prvSetupHardware();
\r
169 /* Start the standard demo tasks. These are just here to exercise the
\r
170 kernel port and provide examples of how the FreeRTOS API can be used. */
\r
171 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
\r
172 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
173 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
\r
174 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
\r
175 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
176 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
\r
177 vStartQueuePeekTasks();
\r
178 vStartRecursiveMutexTasks();
\r
180 /* Create the uIP task. The WEB server runs in this task. */
\r
181 xTaskCreate( vuIP_Task, "uIP", mainBASIC_WEB_STACK_SIZE, ( void * ) NULL, mainUIP_TASK_PRIORITY, NULL );
\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( mainQUEUE_SIZE, sizeof( char * ) );
\r
187 /* Start the LCD gatekeeper task - as described in the comments at the top
\r
189 xTaskCreate( prvLCDTask, "LCD", configMINIMAL_STACK_SIZE * 2, NULL, mainLCD_TASK_PRIORITY, NULL );
\r
191 /* Configure the high frequency interrupt used to measure the interrupt
\r
192 jitter time. When debugging it can be helpful to comment this line out
\r
193 to prevent the debugger repeatedly going into the interrupt service
\r
195 vSetupHighFrequencyTimer();
\r
197 /* Start the scheduler. */
\r
198 vTaskStartScheduler();
\r
200 /* Will only get here if there was insufficient memory to create the idle
\r
201 task. The idle task is created within vTaskStartScheduler(). */
\r
204 /*-----------------------------------------------------------*/
\r
206 static void prvLCDTask( void *pvParameters )
\r
208 unsigned char *pucMessage;
\r
209 unsigned long ulLine = Line3;
\r
210 const unsigned long ulLineHeight = 24;
\r
211 static char cMsgBuf[ 30 ];
\r
212 extern unsigned short usMaxJitter;
\r
214 ( void ) pvParameters;
\r
216 /* The LCD gatekeeper task as described in the comments at the top of this
\r
219 /* Initialise the LCD and display a startup message that includes the
\r
220 configured IP address. */
\r
221 STM3210D_LCD_Init();
\r
223 LCD_SetTextColor(Green);
\r
224 LCD_DisplayStringLine( Line0, ( unsigned char * ) " www.FreeRTOS.org" );
\r
225 LCD_SetTextColor(Blue);
\r
226 sprintf( cMsgBuf, " %d.%d.%d.%d", configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 );
\r
227 LCD_DisplayStringLine( Line1, ( unsigned char * ) cMsgBuf );
\r
228 LCD_SetTextColor(Black);
\r
232 /* Wait for a message to arrive to be displayed. */
\r
233 xQueueReceive( xLCDQueue, &pucMessage, portMAX_DELAY );
\r
235 /* Clear the current line of text. */
\r
236 LCD_ClearLine( ulLine );
\r
238 /* Move on to the next line. */
\r
239 ulLine += ulLineHeight;
\r
240 if( ulLine > Line9 )
\r
245 /* Display the received text, and the max jitter value. */
\r
246 sprintf( cMsgBuf, "%s [%uns]", pucMessage, usMaxJitter * mainNS_PER_CLOCK );
\r
247 LCD_DisplayStringLine( ulLine, ( unsigned char * ) cMsgBuf );
\r
250 /*-----------------------------------------------------------*/
\r
252 static void prvSetupHardware( void )
\r
254 /* Start with the clocks in their expected state. */
\r
257 /* Enable HSE (high speed external clock). */
\r
258 RCC_HSEConfig( RCC_HSE_ON );
\r
260 /* Wait till HSE is ready. */
\r
261 while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )
\r
265 /* 2 wait states required on the flash. */
\r
266 *( ( unsigned long * ) 0x40022000 ) = 0x02;
\r
268 /* HCLK = SYSCLK */
\r
269 RCC_HCLKConfig( RCC_SYSCLK_Div1 );
\r
272 RCC_PCLK2Config( RCC_HCLK_Div1 );
\r
274 /* PCLK1 = HCLK/2 */
\r
275 RCC_PCLK1Config( RCC_HCLK_Div2 );
\r
277 /* PLLCLK = (25MHz / 2 ) * 5 = 62.5 MHz. */
\r
278 RCC_PLLConfig( RCC_PLLSource_HSE_Div2, RCC_PLLMul_5 );
\r
281 RCC_PLLCmd( ENABLE );
\r
283 /* Wait till PLL is ready. */
\r
284 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
\r
288 /* Select PLL as system clock source. */
\r
289 RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK );
\r
291 /* Wait till PLL is used as system clock source. */
\r
292 while( RCC_GetSYSCLKSource() != 0x08 )
\r
296 /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
\r
297 RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
\r
298 | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );
\r
300 /* Set the Vector Table base address at 0x08000000 */
\r
301 NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );
\r
303 NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
\r
305 /* Configure HCLK clock as SysTick clock source. */
\r
306 SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );
\r
308 /* Initialise the IO used for the LED outputs. */
\r
309 vParTestInitialise();
\r
311 /*-----------------------------------------------------------*/
\r
313 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
315 /* This function will get called if a task overflows its stack. If the
\r
316 parameters are corrupt then inspect pxCurrentTCB to find which was the
\r
320 ( void ) pcTaskName;
\r
324 /*-----------------------------------------------------------*/
\r
326 void vApplicationTickHook( void )
\r
328 char *pcMessage = "Status: PASS";
\r
329 static unsigned long ulTicksSinceLastDisplay = 0;
\r
330 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
332 /* Called from every tick interrupt as described in the comments at the top
\r
335 Have enough ticks passed to make it time to perform our health status
\r
337 ulTicksSinceLastDisplay++;
\r
338 if( ulTicksSinceLastDisplay >= mainCHECK_DELAY )
\r
340 /* Reset the counter so these checks run again in mainCHECK_DELAY
\r
342 ulTicksSinceLastDisplay = 0;
\r
344 /* Has an error been found in any task? */
\r
345 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
347 pcMessage = "ERROR: GEN Q";
\r
349 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
351 pcMessage = "ERROR: PEEK Q";
\r
353 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
\r
355 pcMessage = "ERROR: BLOCK Q";
\r
357 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
359 pcMessage = "ERROR: SEMAPHR";
\r
361 else if( xArePollingQueuesStillRunning() != pdTRUE )
\r
363 pcMessage = "ERROR: POLL Q";
\r
365 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
\r
367 pcMessage = "ERROR: INT MATH";
\r
369 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
371 pcMessage = "ERROR: REC MUTEX";
\r
374 /* Send the message to the OLED gatekeeper for display. The
\r
375 xHigherPriorityTaskWoken parameter is not actually used here
\r
376 as this function is running in the tick interrupt anyway - but
\r
377 it must still be supplied. */
\r
378 xHigherPriorityTaskWoken = pdFALSE;
\r
379 xQueueSendFromISR( xLCDQueue, &pcMessage, &xHigherPriorityTaskWoken );
\r
382 /*-----------------------------------------------------------*/
\r