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.
70 /******************************************************************************
71 * NOTE: Windows will not be running the FreeRTOS demo threads continuously, so
72 * do not expect to get real time behaviour from the FreeRTOS Windows port, or
73 * this demo application. Also, the timing information in the FreeRTOS+Trace
74 * logs have no meaningful units. See the documentation page for the Windows
75 * port for further information:
76 * http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html
78 * NOTE 2: This project provides two demo applications. A simple blinky style
79 * project, and a more comprehensive test and demo application. The
80 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
81 * between the two. See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
82 * in main.c. This file implements the simply blinky version. Console output
83 * is used in place of the normal LED toggling.
85 * NOTE 3: This file only contains the source code that is specific to the
86 * basic demo. Generic functions, such FreeRTOS hook functions, are defined
88 ******************************************************************************
90 * main_blinky() creates one queue, one software timer, and two tasks. It then
91 * starts the scheduler.
93 * The Queue Send Task:
94 * The queue send task is implemented by the prvQueueSendTask() function in
95 * this file. It uses vTaskDelayUntil() to create a periodic task that sends
96 * the value 100 to the queue every 200 milliseconds (please read the notes
97 * above regarding the accuracy of timing under Windows).
99 * The Queue Send Software Timer:
100 * The timer is a one-shot timer that is reset by a key press. The timer's
101 * period is set to two seconds - if the timer expires then its callback
102 * function writes the value 200 to the queue. The callback function is
103 * implemented by prvQueueSendTimerCallback() within this file.
105 * The Queue Receive Task:
106 * The queue receive task is implemented by the prvQueueReceiveTask() function
107 * in this file. prvQueueReceiveTask() waits for data to arrive on the queue.
108 * When data is received, the task checks the value of the data, then outputs a
109 * message to indicate if the data came from the queue send task or the queue
110 * send software timer.
112 * Expected Behaviour:
113 * - The queue send task writes to the queue every 200ms, so every 200ms the
114 * queue receive task will output a message indicating that data was received
115 * on the queue from the queue send task.
116 * - The queue send software timer has a period of two seconds, and is reset
117 * each time a key is pressed. So if two seconds expire without a key being
118 * pressed then the queue receive task will output a message indicating that
119 * data was received on the queue from the queue send software timer.
121 * NOTE: Console input and output relies on Windows system calls, which can
122 * interfere with the execution of the FreeRTOS Windows port. This demo only
123 * uses Windows system call occasionally. Heavier use of Windows system calls
124 * can crash the port.
127 /* Standard includes. */
131 /* Kernel includes. */
132 #include "FreeRTOS.h"
137 /* Priorities at which the tasks are created. */
138 #define mainQUEUE_RECEIVE_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
139 #define mainQUEUE_SEND_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
141 /* The rate at which data is sent to the queue. The times are converted from
142 milliseconds to ticks using the pdMS_TO_TICKS() macro. */
143 #define mainTASK_SEND_FREQUENCY_MS pdMS_TO_TICKS( 200UL )
144 #define mainTIMER_SEND_FREQUENCY_MS pdMS_TO_TICKS( 2000UL )
146 /* The number of items the queue can hold at once. */
147 #define mainQUEUE_LENGTH ( 2 )
149 /* The values sent to the queue receive task from the queue send task and the
150 queue send software timer respectively. */
151 #define mainVALUE_SENT_FROM_TASK ( 100UL )
152 #define mainVALUE_SENT_FROM_TIMER ( 200UL )
154 /*-----------------------------------------------------------*/
157 * The tasks as described in the comments at the top of this file.
159 static void prvQueueReceiveTask( void *pvParameters );
160 static void prvQueueSendTask( void *pvParameters );
163 * The callback function executed when the software timer expires.
165 static void prvQueueSendTimerCallback( TimerHandle_t xTimerHandle );
167 /*-----------------------------------------------------------*/
169 /* The queue used by both tasks. */
170 static QueueHandle_t xQueue = NULL;
172 /* A software timer that is started from the tick hook. */
173 static TimerHandle_t xTimer = NULL;
175 /*-----------------------------------------------------------*/
177 /*** SEE THE COMMENTS AT THE TOP OF THIS FILE ***/
178 void main_blinky( void )
180 const TickType_t xTimerPeriod = mainTIMER_SEND_FREQUENCY_MS;
182 /* Create the queue. */
183 xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( uint32_t ) );
187 /* Start the two tasks as described in the comments at the top of this
189 xTaskCreate( prvQueueReceiveTask, /* The function that implements the task. */
190 "Rx", /* The text name assigned to the task - for debug only as it is not used by the kernel. */
191 configMINIMAL_STACK_SIZE, /* The size of the stack to allocate to the task. */
192 NULL, /* The parameter passed to the task - not used in this simple case. */
193 mainQUEUE_RECEIVE_TASK_PRIORITY,/* The priority assigned to the task. */
194 NULL ); /* The task handle is not required, so NULL is passed. */
196 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );
198 /* Create the software timer, but don't start it yet. */
199 xTimer = xTimerCreate( "Timer", /* The text name assigned to the software timer - for debug only as it is not used by the kernel. */
200 xTimerPeriod, /* The period of the software timer in ticks. */
201 pdFALSE, /* xAutoReload is set to pdFALSE, so this is a one shot timer. */
202 NULL, /* The timer's ID is not used. */
203 prvQueueSendTimerCallback );/* The function executed when the timer expires. */
205 /* Start the tasks and timer running. */
206 vTaskStartScheduler();
209 /* If all is well, the scheduler will now be running, and the following
210 line will never be reached. If the following line does execute, then
211 there was insufficient FreeRTOS heap memory available for the idle and/or
212 timer tasks to be created. See the memory management section on the
213 FreeRTOS web site for more details. */
216 /*-----------------------------------------------------------*/
218 static void prvQueueSendTask( void *pvParameters )
220 TickType_t xNextWakeTime;
221 const TickType_t xBlockTime = mainTASK_SEND_FREQUENCY_MS;
222 const uint32_t ulValueToSend = mainVALUE_SENT_FROM_TASK;
224 /* Prevent the compiler warning about the unused parameter. */
225 ( void ) pvParameters;
227 /* Initialise xNextWakeTime - this only needs to be done once. */
228 xNextWakeTime = xTaskGetTickCount();
232 /* Place this task in the blocked state until it is time to run again.
233 The block time is specified in ticks, pdMS_TO_TICKS() was used to
234 convert a time specified in milliseconds into a time specified in ticks.
235 While in the Blocked state this task will not consume any CPU time. */
236 vTaskDelayUntil( &xNextWakeTime, xBlockTime );
238 /* Send to the queue - causing the queue receive task to unblock and
239 write to the console. 0 is used as the block time so the send operation
240 will not block - it shouldn't need to block as the queue should always
241 have at least one space at this point in the code. */
242 xQueueSend( xQueue, &ulValueToSend, 0U );
245 /*-----------------------------------------------------------*/
247 static void prvQueueSendTimerCallback( TimerHandle_t xTimerHandle )
249 const uint32_t ulValueToSend = mainVALUE_SENT_FROM_TIMER;
251 /* This is the software timer callback function. The software timer has a
252 period of two seconds and is reset each time a key is pressed. This
253 callback function will execute if the timer expires, which will only happen
254 if a key is not pressed for two seconds. */
256 /* Avoid compiler warnings resulting from the unused parameter. */
257 ( void ) xTimerHandle;
259 /* Send to the queue - causing the queue receive task to unblock and
260 write out a message. This function is called from the timer/daemon task, so
261 must not block. Hence the block time is set to 0. */
262 xQueueSend( xQueue, &ulValueToSend, 0U );
264 /*-----------------------------------------------------------*/
266 static void prvQueueReceiveTask( void *pvParameters )
268 uint32_t ulReceivedValue;
270 /* Prevent the compiler warning about the unused parameter. */
271 ( void ) pvParameters;
275 /* Wait until something arrives in the queue - this task will block
276 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
277 FreeRTOSConfig.h. It will not use any CPU time while it is in the
279 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
281 /* To get here something must have been received from the queue, but
282 is it an expected value? Normally calling printf() from a task is not
283 a good idea. Here there is lots of stack space and only one task is
284 using console IO so it is ok. However, note the comments at the top of
285 this file about the risks of making Windows system calls (such as
286 console output) from a FreeRTOS task. */
287 if( ulReceivedValue == mainVALUE_SENT_FROM_TASK )
289 printf( "Message received from task\r\n" );
291 else if( ulReceivedValue == mainVALUE_SENT_FROM_TIMER )
293 printf( "Message received from software timer\r\n" );
297 printf( "Unexpected message\r\n" );
300 /* Reset the timer if a key has been pressed. The timer will write
301 mainVALUE_SENT_FROM_TIMER to the queue when it expires. */
304 /* Remove the key from the input buffer. */
307 /* Reset the software timer. */
308 xTimerReset( xTimer, portMAX_DELAY );
312 /*-----------------------------------------------------------*/