2 * FreeRTOS Kernel V10.3.0
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * http://www.FreeRTOS.org
23 * http://aws.amazon.com/freertos
28 /******************************************************************************
29 * NOTE 1: Windows will not be running the FreeRTOS demo threads continuously, so
30 * do not expect to get real time behaviour from the FreeRTOS Windows port, or
31 * this demo application. Also, the timing information in the FreeRTOS+Trace
32 * logs have no meaningful units. See the documentation page for the Windows
33 * port for further information:
34 * http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html
36 * NOTE 2: This project provides two demo applications. A simple blinky style
37 * project, and a more comprehensive test and demo application. The
38 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
39 * between the two. See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
40 * in main.c. This file implements the simply blinky version. Console output
41 * is used in place of the normal LED toggling.
43 * NOTE 3: This file only contains the source code that is specific to the
44 * basic demo. Generic functions, such FreeRTOS hook functions, are defined
46 ******************************************************************************
48 * main_blinky() creates one queue, one software timer, and two tasks. It then
49 * starts the scheduler.
51 * The Queue Send Task:
52 * The queue send task is implemented by the prvQueueSendTask() function in
53 * this file. It uses vTaskDelayUntil() to create a periodic task that sends
54 * the value 100 to the queue every 200 milliseconds (please read the notes
55 * above regarding the accuracy of timing under Windows).
57 * The Queue Send Software Timer:
58 * The timer is an auto-reload timer with a period of two seconds. The timer's
59 * callback function writes the value 200 to the queue. The callback function
60 * is implemented by prvQueueSendTimerCallback() within this file.
62 * The Queue Receive Task:
63 * The queue receive task is implemented by the prvQueueReceiveTask() function
64 * in this file. prvQueueReceiveTask() waits for data to arrive on the queue.
65 * When data is received, the task checks the value of the data, then outputs a
66 * message to indicate if the data came from the queue send task or the queue
67 * send software timer.
70 * - The queue send task writes to the queue every 200ms, so every 200ms the
71 * queue receive task will output a message indicating that data was received
72 * on the queue from the queue send task.
73 * - The queue send software timer has a period of two seconds, and is reset
74 * each time a key is pressed. So if two seconds expire without a key being
75 * pressed then the queue receive task will output a message indicating that
76 * data was received on the queue from the queue send software timer.
78 * NOTE: Console input and output relies on Windows system calls, which can
79 * interfere with the execution of the FreeRTOS Windows port. This demo only
80 * uses Windows system call occasionally. Heavier use of Windows system calls
87 /* Kernel includes. */
96 /* Priorities at which the tasks are created. */
97 #define mainQUEUE_RECEIVE_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
98 #define mainQUEUE_SEND_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
100 /* The rate at which data is sent to the queue. The times are converted from
101 milliseconds to ticks using the pdMS_TO_TICKS() macro. */
102 #define mainTASK_SEND_FREQUENCY_MS pdMS_TO_TICKS( 200UL )
103 #define mainTIMER_SEND_FREQUENCY_MS pdMS_TO_TICKS( 2000UL )
105 /* The number of items the queue can hold at once. */
106 #define mainQUEUE_LENGTH ( 2 )
108 /* The values sent to the queue receive task from the queue send task and the
109 queue send software timer respectively. */
110 #define mainVALUE_SENT_FROM_TASK ( 100UL )
111 #define mainVALUE_SENT_FROM_TIMER ( 200UL )
113 /*-----------------------------------------------------------*/
116 * The tasks as described in the comments at the top of this file.
118 static void prvQueueReceiveTask( void *pvParameters );
119 static void prvQueueSendTask( void *pvParameters );
122 * The callback function executed when the software timer expires.
124 static void prvQueueSendTimerCallback( TimerHandle_t xTimerHandle );
126 /*-----------------------------------------------------------*/
128 /* The queue used by both tasks. */
129 static QueueHandle_t xQueue = NULL;
131 /* A software timer that is started from the tick hook. */
132 static TimerHandle_t xTimer = NULL;
134 /*-----------------------------------------------------------*/
136 /*** SEE THE COMMENTS AT THE TOP OF THIS FILE ***/
137 void main_blinky( void )
139 const TickType_t xTimerPeriod = mainTIMER_SEND_FREQUENCY_MS;
141 /* Create the queue. */
142 xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( uint32_t ) );
146 /* Start the two tasks as described in the comments at the top of this
148 xTaskCreate( prvQueueReceiveTask, /* The function that implements the task. */
149 "Rx", /* The text name assigned to the task - for debug only as it is not used by the kernel. */
150 configMINIMAL_STACK_SIZE, /* The size of the stack to allocate to the task. */
151 NULL, /* The parameter passed to the task - not used in this simple case. */
152 mainQUEUE_RECEIVE_TASK_PRIORITY,/* The priority assigned to the task. */
153 NULL ); /* The task handle is not required, so NULL is passed. */
155 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );
157 /* Create the software timer, but don't start it yet. */
158 xTimer = xTimerCreate( "Timer", /* The text name assigned to the software timer - for debug only as it is not used by the kernel. */
159 xTimerPeriod, /* The period of the software timer in ticks. */
160 pdTRUE, /* xAutoReload is set to pdTRUE. */
161 NULL, /* The timer's ID is not used. */
162 prvQueueSendTimerCallback );/* The function executed when the timer expires. */
166 xTimerStart( xTimer, 0 );
169 /* Start the tasks and timer running. */
170 vTaskStartScheduler();
173 /* If all is well, the scheduler will now be running, and the following
174 line will never be reached. If the following line does execute, then
175 there was insufficient FreeRTOS heap memory available for the idle and/or
176 timer tasks to be created. See the memory management section on the
177 FreeRTOS web site for more details. */
180 /*-----------------------------------------------------------*/
182 static void prvQueueSendTask( void *pvParameters )
184 TickType_t xNextWakeTime;
185 const TickType_t xBlockTime = mainTASK_SEND_FREQUENCY_MS;
186 const uint32_t ulValueToSend = mainVALUE_SENT_FROM_TASK;
188 /* Prevent the compiler warning about the unused parameter. */
189 ( void ) pvParameters;
191 /* Initialise xNextWakeTime - this only needs to be done once. */
192 xNextWakeTime = xTaskGetTickCount();
196 /* Place this task in the blocked state until it is time to run again.
197 The block time is specified in ticks, pdMS_TO_TICKS() was used to
198 convert a time specified in milliseconds into a time specified in ticks.
199 While in the Blocked state this task will not consume any CPU time. */
200 vTaskDelayUntil( &xNextWakeTime, xBlockTime );
202 /* Send to the queue - causing the queue receive task to unblock and
203 write to the console. 0 is used as the block time so the send operation
204 will not block - it shouldn't need to block as the queue should always
205 have at least one space at this point in the code. */
206 xQueueSend( xQueue, &ulValueToSend, 0U );
209 /*-----------------------------------------------------------*/
211 static void prvQueueSendTimerCallback( TimerHandle_t xTimerHandle )
213 const uint32_t ulValueToSend = mainVALUE_SENT_FROM_TIMER;
215 /* This is the software timer callback function. The software timer has a
216 period of two seconds and is reset each time a key is pressed. This
217 callback function will execute if the timer expires, which will only happen
218 if a key is not pressed for two seconds. */
220 /* Avoid compiler warnings resulting from the unused parameter. */
221 ( void ) xTimerHandle;
223 /* Send to the queue - causing the queue receive task to unblock and
224 write out a message. This function is called from the timer/daemon task, so
225 must not block. Hence the block time is set to 0. */
226 xQueueSend( xQueue, &ulValueToSend, 0U );
228 /*-----------------------------------------------------------*/
230 static void prvQueueReceiveTask( void *pvParameters )
232 uint32_t ulReceivedValue;
234 /* Prevent the compiler warning about the unused parameter. */
235 ( void ) pvParameters;
239 /* Wait until something arrives in the queue - this task will block
240 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
241 FreeRTOSConfig.h. It will not use any CPU time while it is in the
243 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
245 /* To get here something must have been received from the queue, but
246 is it an expected value? Normally calling printf() from a task is not
247 a good idea. Here there is lots of stack space and only one task is
248 using console IO so it is ok. However, note the comments at the top of
249 this file about the risks of making Windows system calls (such as
250 console output) from a FreeRTOS task. */
251 if( ulReceivedValue == mainVALUE_SENT_FROM_TASK )
253 console_print( "Message received from task\n" );
255 else if( ulReceivedValue == mainVALUE_SENT_FROM_TIMER )
257 console_print( "Message received from software timer\n" );
261 console_print( "Unexpected message\n" );
265 /*-----------------------------------------------------------*/