2 * FreeRTOS Kernel V10.1.1
3 * Copyright (C) 2018 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: This project provides two demo applications. A simple blinky style
30 * project, and a more comprehensive test and demo application. The
31 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
32 * between the two. See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
33 * in main.c. This file implements the simply blinky style version.
35 * NOTE 2: This file only contains the source code that is specific to the
36 * basic demo. Generic functions, such FreeRTOS hook functions, and functions
37 * required to configure the hardware, are defined in main.c.
38 ******************************************************************************
40 * main_blinky() creates one queue, two tasks, and one software timer. It then
41 * starts the scheduler.
43 * The Blinky Software Timer:
44 * This demonstrates an auto-reload software timer. The timer callback function
45 * does nothing but toggle an LED.
47 * The Queue Send Task:
48 * The queue send task is implemented by prvQueueSendTask() in main_blinky.c.
49 * prvQueueSendTask() repeatedly blocks for 200 milliseconds before sending the
50 * value 100 to the queue that was created in main_blinky().
52 * The Queue Receive Task:
53 * The queue receive task is implemented by prvQueueReceiveTask() in
54 * main_blinky.c. prvQueueReceiveTask() repeatedly blocks on attempts to read
55 * from the queue that was created in main_blinky(), toggling an LED each time
56 * data is received. The queue send task sends data to the queue every 200
57 * milliseconds, so the LED will toggle every 200 milliseconds.
60 /* Standard includes. */
63 /* Kernel includes. */
69 /* Standard demo includes. */
73 /* Priorities at which the tasks are created. */
74 #define mainQUEUE_SEND_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
75 #define mainQUEUE_RECEIVE_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
77 /* The rate at which data is sent to the queue. The 200ms value is converted
78 to ticks using the portTICK_PERIOD_MS constant. */
79 #define mainQUEUE_SEND_FREQUENCY_MS ( 200 / portTICK_PERIOD_MS )
81 /* The number of items the queue can hold. This is 1 as the receive task
82 will remove items as they are added, meaning the send task should always find
84 #define mainQUEUE_LENGTH ( 1 )
86 /* Values passed to the two tasks just to check the task parameter
88 #define mainQUEUE_SEND_PARAMETER ( 0x1111UL )
89 #define mainQUEUE_RECEIVE_PARAMETER ( 0x22UL )
91 /* The period of the blinky software timer. The period is specified in ms and
92 converted to ticks using the portTICK_PERIOD_MS constant. */
93 #define mainBLINKY_TIMER_PERIOD ( 50 / portTICK_PERIOD_MS )
95 /* The LED used by the communicating tasks and the blinky timer respectively. */
96 #define mainTASKS_LED ( 0 )
97 #define mainTIMER_LED ( 1 )
100 #define mainDONT_BLOCK ( 0 )
102 /*-----------------------------------------------------------*/
105 * The tasks as described in the comments at the top of this file.
107 static void prvQueueReceiveTask( void *pvParameters );
108 static void prvQueueSendTask( void *pvParameters );
111 * The callback function for the blinky software timer, as described at the top
114 static void prvBlinkyTimerCallback( TimerHandle_t xTimer );
117 * Called by main() to create the simply blinky style application if
118 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.
120 void main_blinky( void );
122 /*-----------------------------------------------------------*/
124 /* The queue used by both tasks. */
125 static QueueHandle_t xQueue = NULL;
127 /*-----------------------------------------------------------*/
129 void main_blinky( void )
131 TimerHandle_t xTimer;
133 /* Create the queue. */
134 xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );
135 configASSERT( xQueue );
139 /* Create the two tasks as described in the comments at the top of this
141 xTaskCreate( prvQueueReceiveTask, /* The function that implements the task. */
142 "Rx", /* The text name assigned to the task - for debug only as it is not used by the kernel. */
143 configMINIMAL_STACK_SIZE, /* The size of the stack to allocate to the task. */
144 ( void * ) mainQUEUE_RECEIVE_PARAMETER, /* The parameter passed to the task - just to check the functionality. */
145 mainQUEUE_RECEIVE_TASK_PRIORITY, /* The priority assigned to the task. */
146 NULL ); /* The task handle is not required, so NULL is passed. */
148 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, ( void * ) mainQUEUE_SEND_PARAMETER, mainQUEUE_SEND_TASK_PRIORITY, NULL );
151 /* Create the blinky software timer as described at the top of this file. */
152 xTimer = xTimerCreate( "Blinky", /* A text name, purely to help debugging. */
153 ( mainBLINKY_TIMER_PERIOD ),/* The timer period. */
154 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
155 ( void * ) 0, /* The ID is not used, so can be set to anything. */
156 prvBlinkyTimerCallback ); /* The callback function that inspects the status of all the other tasks. */
157 configASSERT( xTimer );
161 xTimerStart( xTimer, mainDONT_BLOCK );
164 /* Start the tasks and timer running. */
165 vTaskStartScheduler();
168 /* If all is well, the scheduler will now be running, and the following
169 line will never be reached. If the following line does execute, then
170 there was insufficient FreeRTOS heap memory available for the idle and/or
171 timer tasks to be created. See the memory management section on the
172 FreeRTOS web site for more details. http://www.freertos.org/a00111.html */
175 /*-----------------------------------------------------------*/
177 static void prvQueueSendTask( void *pvParameters )
179 TickType_t xNextWakeTime;
180 const unsigned long ulValueToSend = 100UL;
182 /* Remove compiler warnigns in the case that configASSERT() is not dfined. */
183 ( void ) pvParameters;
185 /* Check the task parameter is as expected. */
186 configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_SEND_PARAMETER );
188 /* Initialise xNextWakeTime - this only needs to be done once. */
189 xNextWakeTime = xTaskGetTickCount();
193 /* Place this task in the blocked state until it is time to run again.
194 The block time is specified in ticks, the constant used converts ticks
195 to ms. While in the Blocked state this task will not consume any CPU
197 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );
199 /* Send to the queue - causing the queue receive task to unblock and
200 toggle the LED. 0 is used as the block time so the sending operation
201 will not block - it shouldn't need to block as the queue should always
202 be empty at this point in the code. */
203 xQueueSend( xQueue, &ulValueToSend, 0U );
206 /*-----------------------------------------------------------*/
208 static void prvQueueReceiveTask( void *pvParameters )
210 unsigned long ulReceivedValue;
212 /* Remove compiler warnings in the case where configASSERT() is not defined. */
213 ( void ) pvParameters;
215 /* Check the task parameter is as expected. */
216 configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_RECEIVE_PARAMETER );
220 /* Wait until something arrives in the queue - this task will block
221 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
223 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
225 /* To get here something must have been received from the queue, but
226 is it the expected value? If it is, toggle the LED. */
227 if( ulReceivedValue == 100UL )
229 vParTestToggleLED( mainTASKS_LED );
230 ulReceivedValue = 0U;
234 /*-----------------------------------------------------------*/
236 static void prvBlinkyTimerCallback( TimerHandle_t xTimer )
238 /* Avoid compiler warnings. */
241 /* This function is called when the blinky software time expires. All the
242 function does is toggle the LED. LED mainTIMER_LED should therefore toggle
243 with the period set by mainBLINKY_TIMER_PERIOD. */
244 vParTestToggleLED( mainTIMER_LED );
246 /*-----------------------------------------------------------*/