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 1: This project provides two demo applications. A simple blinky style
72 * project, and a more comprehensive test and demo application. The
73 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
74 * between the two. See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
75 * in main.c. This file implements the simply blinky style version.
77 * NOTE 2: This file only contains the source code that is specific to the
78 * basic demo. Generic functions, such FreeRTOS hook functions, and functions
79 * required to configure the hardware, are defined in main.c.
80 ******************************************************************************
82 * main_blinky() creates one queue, two tasks, and one software timer. It then
83 * starts the scheduler.
85 * The Blinky Software Timer:
86 * This demonstrates an auto-reload software timer. The timer callback function
87 * does nothing but toggle an LED.
89 * The Queue Send Task:
90 * The queue send task is implemented by the prvQueueSendTask() function in
91 * this file. prvQueueSendTask() sits in a loop that causes it to repeatedly
92 * block for 200 milliseconds, before sending the value 100 to the queue that
93 * was created within main_blinky(). Once the value is sent, the task loops
94 * back around to block for another 200 milliseconds.
96 * The Queue Receive Task:
97 * The queue receive task is implemented by the prvQueueReceiveTask() function
98 * in this file. prvQueueReceiveTask() sits in a loop where it repeatedly
99 * blocks on attempts to read data from the queue that was created within
100 * main_blinky(). When data is received, the task checks the value of the
101 * data, and if the value equals the expected 100, toggles the LED. The 'block
102 * time' parameter passed to the queue receive function specifies that the
103 * task should be held in the Blocked state indefinitely to wait for data to
104 * be available on the queue. The queue receive task will only leave the
105 * Blocked state when the queue send task writes to the queue. As the queue
106 * send task writes to the queue every 200 milliseconds, the queue receive
107 * task leaves the Blocked state every 200 milliseconds, and therefore toggles
108 * the LED every 200 milliseconds.
111 /* Standard includes. */
114 /* Kernel includes. */
115 #include "FreeRTOS.h"
120 /* Standard demo includes. */
123 /* Priorities at which the tasks are created. */
124 #define mainQUEUE_RECEIVE_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
125 #define mainQUEUE_SEND_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
127 /* The rate at which data is sent to the queue. The 200ms value is converted
128 to ticks using the portTICK_PERIOD_MS constant. */
129 #define mainQUEUE_SEND_FREQUENCY_MS ( 200 / portTICK_PERIOD_MS )
131 /* The number of items the queue can hold. This is 1 as the receive task
132 will remove items as they are added, meaning the send task should always find
134 #define mainQUEUE_LENGTH ( 1 )
136 /* Values passed to the two tasks just to check the task parameter
138 #define mainQUEUE_SEND_PARAMETER ( 0x1111UL )
139 #define mainQUEUE_RECEIVE_PARAMETER ( 0x22UL )
141 /* The period of the blinky software timer. The period is specified in ms and
142 converted to ticks using the portTICK_PERIOD_MS constant. */
143 #define mainBLINKY_TIMER_PERIOD ( 50 / portTICK_PERIOD_MS )
145 /* The LED used by the communicating tasks and the blinky timer respectively. */
146 #define mainTASKS_LED ( 0 )
147 #define mainTIMER_LED ( 1 )
150 #define mainDONT_BLOCK ( 0 )
152 /*-----------------------------------------------------------*/
155 * The tasks as described in the comments at the top of this file.
157 static void prvQueueReceiveTask( void *pvParameters );
158 static void prvQueueSendTask( void *pvParameters );
161 * The callback function for the blinky software timer, as described at the top
164 static void prvBlinkyTimerCallback( TimerHandle_t xTimer );
167 * Called by main() to create the simply blinky style application if
168 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.
170 void main_blinky( void );
172 /*-----------------------------------------------------------*/
174 /* The queue used by both tasks. */
175 static QueueHandle_t xQueue = NULL;
177 /*-----------------------------------------------------------*/
179 void main_blinky( void )
181 TimerHandle_t xTimer;
183 /* Create the queue. */
184 xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );
188 /* Create the two tasks as described in the comments at the top of this
190 xTaskCreate( prvQueueReceiveTask, /* The function that implements the task. */
191 "Rx", /* The text name assigned to the task - for debug only as it is not used by the kernel. */
192 configMINIMAL_STACK_SIZE, /* The size of the stack to allocate to the task. */
193 ( void * ) mainQUEUE_RECEIVE_PARAMETER, /* The parameter passed to the task - just to check the functionality. */
194 mainQUEUE_RECEIVE_TASK_PRIORITY, /* The priority assigned to the task. */
195 NULL ); /* The task handle is not required, so NULL is passed. */
197 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, ( void * ) mainQUEUE_SEND_PARAMETER, mainQUEUE_SEND_TASK_PRIORITY, NULL );
199 /* Create the blinky software timer as described at the top of this
201 xTimer = xTimerCreate( "Blinky", /* A text name, purely to help debugging. */
202 ( mainBLINKY_TIMER_PERIOD ),/* The timer period. */
203 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
204 ( void * ) 0, /* The ID is not used, so can be set to anything. */
205 prvBlinkyTimerCallback /* The callback function that inspects the status of all the other tasks. */
210 xTimerStart( xTimer, mainDONT_BLOCK );
213 /* Start the tasks and timer running. */
214 vTaskStartScheduler();
217 /* If all is well, the scheduler will now be running, and the following
218 line will never be reached. If the following line does execute, then
219 there was insufficient FreeRTOS heap memory available for the idle and/or
220 timer tasks to be created. See the memory management section on the
221 FreeRTOS web site for more details. */
224 /*-----------------------------------------------------------*/
226 static void prvQueueSendTask( void *pvParameters )
228 TickType_t xNextWakeTime;
229 const unsigned long ulValueToSend = 100UL;
231 /* Check the task parameter is as expected. */
232 configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_SEND_PARAMETER );
234 /* Initialise xNextWakeTime - this only needs to be done once. */
235 xNextWakeTime = xTaskGetTickCount();
239 /* Place this task in the blocked state until it is time to run again.
240 The block time is specified in ticks, the constant used converts ticks
241 to ms. While in the Blocked state this task will not consume any CPU
243 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );
245 /* Send to the queue - causing the queue receive task to unblock and
246 toggle the LED. 0 is used as the block time so the sending operation
247 will not block - it shouldn't need to block as the queue should always
248 be empty at this point in the code. */
249 xQueueSend( xQueue, &ulValueToSend, 0U );
252 /*-----------------------------------------------------------*/
254 static void prvQueueReceiveTask( void *pvParameters )
256 unsigned long ulReceivedValue;
258 /* Check the task parameter is as expected. */
259 configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_RECEIVE_PARAMETER );
263 /* Wait until something arrives in the queue - this task will block
264 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
266 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
268 /* To get here something must have been received from the queue, but
269 is it the expected value? If it is, toggle the LED. */
270 if( ulReceivedValue == 100UL )
272 vParTestToggleLED( mainTASKS_LED );
273 ulReceivedValue = 0U;
277 /*-----------------------------------------------------------*/
279 static void prvBlinkyTimerCallback( TimerHandle_t xTimer )
281 /* This function is called when the blinky software time expires. All the
282 function does is toggle the LED. LED mainTIMER_LED should therefore toggle
283 with the period set by mainBLINKY_TIMER_PERIOD. */
284 vParTestToggleLED( mainTIMER_LED );