2 FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
7 This file is part of the FreeRTOS distribution.
9 FreeRTOS is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License (version 2) as published by the
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
13 ***************************************************************************
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
15 >>! distribute a combined work that includes FreeRTOS without being !<<
16 >>! obliged to provide the source code for proprietary components !<<
17 >>! outside of the FreeRTOS kernel. !<<
18 ***************************************************************************
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
23 link: http://www.freertos.org/a00114.html
25 ***************************************************************************
27 * FreeRTOS provides completely free yet professionally developed, *
28 * robust, strictly quality controlled, supported, and cross *
29 * platform software that is more than just the market leader, it *
30 * is the industry's de facto standard. *
32 * Help yourself get started quickly while simultaneously helping *
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
34 * tutorial book, reference manual, or both: *
35 * http://www.FreeRTOS.org/Documentation *
37 ***************************************************************************
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
40 the FAQ page "My application does not run, what could be wrong?". Have you
41 defined configASSERT()?
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
44 embedded software for free we request you assist our global community by
45 participating in the support forum.
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
48 be as productive as possible as early as possible. Now you can receive
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50 Ltd, and the world's leading authority on the world's leading RTOS.
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
61 licenses offer ticketed support, indemnification and commercial middleware.
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64 engineered and independently SIL3 certified version for use in safety and
65 mission critical applications that require provable dependability.
72 * Creates all the demo application tasks, then starts the scheduler. The WEB
73 * documentation provides more details of the standard demo application tasks
74 * (which just exist to test the kernel port and provide an example of how to use
75 * each FreeRTOS API function).
77 * In addition to the standard demo tasks, the following tasks and tests are
78 * defined and/or created within this file:
80 * "Check" task - This only executes every five seconds but has the highest
81 * priority so is guaranteed to get processor time. Its main function is to
82 * check that all the standard demo tasks are still operational. The check task
83 * will toggle LED 3 (PB11) every five seconds so long as no errors have been
84 * detected. The toggle rate will increase to half a second if an error has
85 * been found in any task.
87 * "Echo" task - This is a very basic task that simply echoes any characters
88 * received on COM0 (USART1). This can be tested by transmitting a text file
89 * from a dumb terminal to the STM32 USART then observing or capturing the text
90 * that is echoed back. Missing characters will be all the more obvious if the
91 * file contains a simple repeating string of fixed width.
93 * Currently this demo does not include interrupt nesting examples. High
94 * frequency timer and simpler nesting examples can be found in most Cortex-M3
97 * The functions used to initialise, set and clear LED outputs are normally
98 * defined in partest.c. This demo includes two partest files, one that is
99 * configured for use with the Keil MCBSTM32 evaluation board (called
100 * ParTest_MCBSTM32.c) and one that is configured for use with the official
101 * ST Eval board (called ParTest_ST_Eval.c). One one of these files should be
102 * included in the build at any one time, as appropriate for the hardware
103 * actually being used.
106 /* Standard includes. */
109 /* Scheduler includes. */
110 #include "FreeRTOS.h"
114 /* Library includes. */
115 #include "stm32f10x_it.h"
117 /* Demo app includes. */
123 #include "GenQTest.h"
125 #include "recmutex.h"
127 /* Driver includes. */
128 #include "STM32_USART.h"
131 /* The time between cycles of the 'check' task - which depends on whether the
132 check task has detected an error or not. */
133 #define mainCHECK_DELAY_NO_ERROR ( ( TickType_t ) 5000 / portTICK_PERIOD_MS )
134 #define mainCHECK_DELAY_ERROR ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
136 /* The LED controlled by the 'check' task. */
137 #define mainCHECK_LED ( 3 )
139 /* Task priorities. */
140 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
141 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
142 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
143 #define mainFLASH_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
144 #define mainECHO_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
145 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
146 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
148 /* COM port and baud rate used by the echo task. */
149 #define mainCOM0 ( 0 )
150 #define mainBAUD_RATE ( 115200 )
152 /*-----------------------------------------------------------*/
155 * Configure the hardware for the demo.
157 static void prvSetupHardware( void );
159 /* The 'check' task as described at the top of this file. */
160 static void prvCheckTask( void *pvParameters );
162 /* A simple task that echoes all the characters that are received on COM0
164 static void prvUSARTEchoTask( void *pvParameters );
166 /*-----------------------------------------------------------*/
174 /* Set up the clocks and memory interface. */
177 /* Start the standard demo tasks. These are just here to exercise the
178 kernel port and provide examples of how the FreeRTOS API can be used. */
179 vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
180 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
181 vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
182 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
183 vStartLEDFlashTasks( mainFLASH_TASK_PRIORITY );
184 vStartQueuePeekTasks();
185 vStartRecursiveMutexTasks();
187 /* Create the 'echo' task, which is also defined within this file. */
188 xTaskCreate( prvUSARTEchoTask, "Echo", configMINIMAL_STACK_SIZE, NULL, mainECHO_TASK_PRIORITY, NULL );
190 /* Create the 'check' task, which is also defined within this file. */
191 xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );
193 /* Start the scheduler. */
194 vTaskStartScheduler();
196 /* Will only get here if there was insufficient memory to create the idle
197 task. The idle task is created within vTaskStartScheduler(). */
200 /*-----------------------------------------------------------*/
202 /* Described at the top of this file. */
203 static void prvCheckTask( void *pvParameters )
205 TickType_t xLastExecutionTime;
206 unsigned long ulTicksToWait = mainCHECK_DELAY_NO_ERROR;
208 /* Just to remove the compiler warning about the unused parameter. */
209 ( void ) pvParameters;
211 /* Initialise the variable used to control our iteration rate prior to
213 xLastExecutionTime = xTaskGetTickCount();
217 /* Wait until it is time to run the tests again. */
218 vTaskDelayUntil( &xLastExecutionTime, ulTicksToWait );
220 /* Has an error been found in any task? */
221 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
223 /* Reduce the time between cycles of this task - which has the
224 effect of increasing the rate at which the 'check' LED toggles to
225 indicate the existence of an error to an observer. */
226 ulTicksToWait = mainCHECK_DELAY_ERROR;
228 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
230 ulTicksToWait = mainCHECK_DELAY_ERROR;
232 else if( xAreBlockingQueuesStillRunning() != pdTRUE )
234 ulTicksToWait = mainCHECK_DELAY_ERROR;
236 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
238 ulTicksToWait = mainCHECK_DELAY_ERROR;
240 else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
242 ulTicksToWait = mainCHECK_DELAY_ERROR;
244 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
246 ulTicksToWait = mainCHECK_DELAY_ERROR;
249 vParTestToggleLED( mainCHECK_LED );
252 /*-----------------------------------------------------------*/
254 /* Described at the top of this file. */
255 static void prvUSARTEchoTask( void *pvParameters )
259 /* String declared static to ensure it does not end up on the stack, no matter
260 what the optimisation level. */
261 static const char *pcLongishString =
262 "ABBA was a Swedish pop music group formed in Stockholm in 1972, consisting of Anni-Frid Frida Lyngstad, "
263 "Björn Ulvaeus, Benny Andersson and Agnetha Fältskog. Throughout the band's existence, Fältskog and Ulvaeus "
264 "were a married couple, as were Lyngstad and Andersson - although both couples later divorced. They became one "
265 "of the most commercially successful acts in the history of popular music, and they topped the charts worldwide "
266 "from 1972 to 1983. ABBA gained international popularity employing catchy song hooks, simple lyrics, sound "
267 "effects (reverb, phasing) and a Wall of Sound achieved by overdubbing the female singers' voices in multiple "
268 "harmonies. As their popularity grew, they were sought after to tour Europe, Australia, and North America, drawing "
269 "crowds of ardent fans, notably in Australia. Touring became a contentious issue, being particularly cumbersome for "
270 "Fältskog, but they continued to release studio albums to widespread commercial success. At the height of their "
271 "popularity, however, both relationships began suffering strain that led ultimately to the collapse of first the "
272 "Ulvaeus-Fältskog marriage (in 1979) and then of the Andersson-Lyngstad marriage in 1981. In the late 1970s and early "
273 "1980s these relationship changes began manifesting in the group's music, as they produced more thoughtful, "
274 "introspective lyrics with different compositions.";
276 /* Just to avoid compiler warnings. */
277 ( void ) pvParameters;
279 /* Initialise COM0, which is USART1 according to the STM32 libraries. */
280 lCOMPortInit( mainCOM0, mainBAUD_RATE );
282 /* Try sending out a string all in one go, as a very basic test of the
283 lSerialPutString() function. */
284 lSerialPutString( mainCOM0, pcLongishString, strlen( pcLongishString ) );
288 /* Block to wait for a character to be received on COM0. */
289 xSerialGetChar( mainCOM0, &cChar, portMAX_DELAY );
291 /* Write the received character back to COM0. */
292 xSerialPutChar( mainCOM0, cChar, 0 );
295 /*-----------------------------------------------------------*/
297 static void prvSetupHardware( void )
299 /* RCC system reset(for debug purpose). */
303 RCC_HSEConfig( RCC_HSE_ON );
305 /* Wait till HSE is ready. */
306 while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);
309 RCC_HCLKConfig( RCC_SYSCLK_Div1 );
312 RCC_PCLK2Config( RCC_HCLK_Div1 );
314 /* PCLK1 = HCLK/2. */
315 RCC_PCLK1Config( RCC_HCLK_Div2 );
317 /* ADCCLK = PCLK2/4. */
318 RCC_ADCCLKConfig( RCC_PCLK2_Div4 );
320 /* Flash 2 wait state. */
321 *( volatile unsigned long * )0x40022000 = 0x01;
323 /* PLLCLK = 8MHz * 9 = 72 MHz */
324 RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_9 );
327 RCC_PLLCmd( ENABLE );
329 /* Wait till PLL is ready. */
330 while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
332 /* Select PLL as system clock source. */
333 RCC_SYSCLKConfig (RCC_SYSCLKSource_PLLCLK);
335 /* Wait till PLL is used as system clock source. */
336 while (RCC_GetSYSCLKSource() != 0x08);
338 /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
339 RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
340 | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE );
342 /* Set the Vector Table base address at 0x08000000. */
343 NVIC_SetVectorTable( NVIC_VectTab_FLASH, 0x0 );
345 NVIC_PriorityGroupConfig( NVIC_PriorityGroup_4 );
347 /* Configure HCLK clock as SysTick clock source. */
348 SysTick_CLKSourceConfig( SysTick_CLKSource_HCLK );
350 /* Initialise the IO used for the LED outputs. */
351 vParTestInitialise();
353 /* SPI2 Periph clock enable */
354 RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );
356 /*-----------------------------------------------------------*/
358 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
360 /* This function will get called if a task overflows its stack. If the
361 parameters are corrupt then inspect pxCurrentTCB to find which was the
369 /*-----------------------------------------------------------*/
371 void assert_failed( unsigned char *pucFile, unsigned long ulLine )