2 * FreeRTOS Kernel V10.0.1
3 * Copyright (C) 2017 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, and two tasks. It then starts the
43 * The Queue Send Task:
44 * The queue send task is implemented by the prvQueueSendTask() function in
45 * this file. prvQueueSendTask() sits in a loop that causes it to repeatedly
46 * block for 200 milliseconds, before sending the value 100 to the queue that
47 * was created within main_blinky(). Once the value is sent, the task loops
48 * back around to block for another 200 milliseconds.
50 * The Queue Receive Task:
51 * The queue receive task is implemented by the prvQueueReceiveTask() function
52 * in this file. prvQueueReceiveTask() sits in a loop where it repeatedly
53 * blocks on attempts to read data from the queue that was created within
54 * main_blinky(). When data is received, the task checks the value of the
55 * data, and if the value equals the expected 100, toggles the LED. The 'block
56 * time' parameter passed to the queue receive function specifies that the
57 * task should be held in the Blocked state indefinitely to wait for data to
58 * be available on the queue. The queue receive task will only leave the
59 * Blocked state when the queue send task writes to the queue. As the queue
60 * send task writes to the queue every 200 milliseconds, the queue receive
61 * task leaves the Blocked state every 200 milliseconds, and therefore toggles
62 * the LED every 200 milliseconds.
65 /* Standard includes. */
68 /* Kernel includes. */
73 /* Priorities at which the tasks are created. */
74 #define mainQUEUE_RECEIVE_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
75 #define mainQUEUE_SEND_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
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 /*-----------------------------------------------------------*/
94 * The tasks as described in the comments at the top of this file.
96 static void prvQueueReceiveTask( void *pvParameters );
97 static void prvQueueSendTask( void *pvParameters );
100 * Called by main() to create the simply blinky style application if
101 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.
103 void main_blinky( void );
105 /*-----------------------------------------------------------*/
107 /* The queue used by both tasks. */
108 static QueueHandle_t xQueue = NULL;
110 /*-----------------------------------------------------------*/
112 void main_blinky( void )
114 /* Create the queue. */
115 xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );
119 /* Start the two tasks as described in the comments at the top of this
121 xTaskCreate( prvQueueReceiveTask, /* The function that implements the task. */
122 "Rx", /* The text name assigned to the task - for debug only as it is not used by the kernel. */
123 configMINIMAL_STACK_SIZE, /* The size of the stack to allocate to the task. */
124 ( void * ) mainQUEUE_RECEIVE_PARAMETER, /* The parameter passed to the task - just to check the functionality. */
125 mainQUEUE_RECEIVE_TASK_PRIORITY, /* The priority assigned to the task. */
126 NULL ); /* The task handle is not required, so NULL is passed. */
128 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, ( void * ) mainQUEUE_SEND_PARAMETER, mainQUEUE_SEND_TASK_PRIORITY, NULL );
130 /* Start the tasks and timer running. */
131 vTaskStartScheduler();
134 /* If all is well, the scheduler will now be running, and the following
135 line will never be reached. If the following line does execute, then
136 there was insufficient FreeRTOS heap memory available for the idle and/or
137 timer tasks to be created. See the memory management section on the
138 FreeRTOS web site for more details. */
141 __asm volatile( "NOP" );
144 /*-----------------------------------------------------------*/
146 static void prvQueueSendTask( void *pvParameters )
148 TickType_t xNextWakeTime;
149 const unsigned long ulValueToSend = 100UL;
151 /* Check the task parameter is as expected. */
152 configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_SEND_PARAMETER );
154 /* Initialise xNextWakeTime - this only needs to be done once. */
155 xNextWakeTime = xTaskGetTickCount();
159 /* Place this task in the blocked state until it is time to run again.
160 The block time is specified in ticks, the constant used converts ticks
161 to ms. While in the Blocked state this task will not consume any CPU
163 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );
165 /* Send to the queue - causing the queue receive task to unblock and
166 toggle the LED. 0 is used as the block time so the sending operation
167 will not block - it shouldn't need to block as the queue should always
168 be empty at this point in the code. */
169 xQueueSend( xQueue, &ulValueToSend, 0U );
172 /*-----------------------------------------------------------*/
174 static void prvQueueReceiveTask( void *pvParameters )
176 unsigned long ulReceivedValue;
178 /* Check the task parameter is as expected. */
179 configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_RECEIVE_PARAMETER );
183 /* Wait until something arrives in the queue - this task will block
184 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
186 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
188 /* To get here something must have been received from the queue, but
189 is it the expected value? If it is, toggle the LED. */
190 if( ulReceivedValue == 100UL )
193 ulReceivedValue = 0U;
197 /*-----------------------------------------------------------*/