2 * FreeRTOS Kernel V10.1.1
\r
3 * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /******************************************************************************
\r
29 * NOTE 1: This project provides three demo applications. A simple blinky
\r
30 * style project, a more comprehensive test and demo application, and an
\r
31 * lwIP example. The mainSELECTED_APPLICATION setting in main.c is used to
\r
32 * select between the three. See the notes on using mainSELECTED_APPLICATION
\r
33 * in main.c. This file implements the simply blinky style version.
\r
35 * NOTE 2: This file only contains the source code that is specific to the
\r
36 * basic demo. Generic functions, such FreeRTOS hook functions, and functions
\r
37 * required to configure the hardware are defined in main.c.
\r
38 ******************************************************************************
\r
40 * main_blinky() creates one queue, and two tasks. It then starts the
\r
43 * The Queue Send Task:
\r
44 * The queue send task is implemented by the prvQueueSendTask() function in
\r
45 * this file. prvQueueSendTask() sits in a loop that causes it to repeatedly
\r
46 * block for 200 milliseconds, before sending the value 100 to the queue that
\r
47 * was created within main_blinky(). Once the value is sent, the task loops
\r
48 * back around to block for another 200 milliseconds...and so on.
\r
50 * The Queue Receive Task:
\r
51 * The queue receive task is implemented by the prvQueueReceiveTask() function
\r
52 * in this file. prvQueueReceiveTask() sits in a loop where it repeatedly
\r
53 * blocks on attempts to read data from the queue that was created within
\r
54 * main_blinky(). When data is received, the task checks the value of the
\r
55 * data, and if the value equals the expected 100, toggles an LED. The 'block
\r
56 * time' parameter passed to the queue receive function specifies that the
\r
57 * task should be held in the Blocked state indefinitely to wait for data to
\r
58 * be available on the queue. The queue receive task will only leave the
\r
59 * Blocked state when the queue send task writes to the queue. As the queue
\r
60 * send task writes to the queue every 200 milliseconds, the queue receive
\r
61 * task leaves the Blocked state every 200 milliseconds, and therefore toggles
\r
62 * the LED every 200 milliseconds.
\r
65 /* Kernel includes. */
\r
66 #include "FreeRTOS.h"
\r
70 /* Standard demo includes. */
\r
71 #include "partest.h"
\r
73 /* Priorities at which the tasks are created. */
\r
74 #define mainQUEUE_RECEIVE_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
75 #define mainQUEUE_SEND_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
77 /* The rate at which data is sent to the queue. The 200ms value is converted
\r
78 to ticks using the portTICK_PERIOD_MS constant. */
\r
79 #define mainQUEUE_SEND_FREQUENCY_MS ( 200 / portTICK_PERIOD_MS )
\r
81 /* The number of items the queue can hold. This is 1 as the receive task
\r
82 will remove items as they are added, meaning the send task should always find
\r
84 #define mainQUEUE_LENGTH ( 1 )
\r
86 /* The LED toggled by the Rx task. */
\r
87 #define mainTASK_LED ( 0 )
\r
89 /*-----------------------------------------------------------*/
\r
92 * The tasks as described in the comments at the top of this file.
\r
94 static void prvQueueReceiveTask( void *pvParameters );
\r
95 static void prvQueueSendTask( void *pvParameters );
\r
97 /*-----------------------------------------------------------*/
\r
99 /* The queue used by both tasks. */
\r
100 static QueueHandle_t xQueue = NULL;
\r
102 /*-----------------------------------------------------------*/
\r
104 void main_blinky( void )
\r
106 /* Create the queue. */
\r
107 xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( uint32_t ) );
\r
109 if( xQueue != NULL )
\r
111 /* Start the two tasks as described in the comments at the top of this
\r
113 xTaskCreate( prvQueueReceiveTask, /* The function that implements the task. */
\r
114 "Rx", /* The text name assigned to the task - for debug only as it is not used by the kernel. */
\r
115 configMINIMAL_STACK_SIZE, /* The size of the stack to allocate to the task. */
\r
116 NULL, /* The parameter passed to the task - not used in this case. */
\r
117 mainQUEUE_RECEIVE_TASK_PRIORITY, /* The priority assigned to the task. */
\r
118 NULL ); /* The task handle is not required, so NULL is passed. */
\r
120 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );
\r
122 /* Start the tasks and timer running. */
\r
123 vTaskStartScheduler();
\r
126 /* If all is well, the scheduler will now be running, and the following
\r
127 line will never be reached. If the following line does execute, then
\r
128 there was either insufficient FreeRTOS heap memory available for the idle
\r
129 and/or timer tasks to be created, or vTaskStartScheduler() was called from
\r
130 User mode. See the memory management section on the FreeRTOS web site for
\r
131 more details on the FreeRTOS heap http://www.freertos.org/a00111.html. The
\r
132 mode from which main() is called is set in the C start up code and must be
\r
133 a privileged mode (not user mode). */
\r
136 /*-----------------------------------------------------------*/
\r
138 static void prvQueueSendTask( void *pvParameters )
\r
140 TickType_t xNextWakeTime;
\r
141 const unsigned long ulValueToSend = 100UL;
\r
143 /* Remove compiler warning about unused parameter. */
\r
144 ( void ) pvParameters;
\r
146 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
147 xNextWakeTime = xTaskGetTickCount();
\r
151 /* Place this task in the blocked state until it is time to run again. */
\r
152 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );
\r
154 /* Send to the queue - causing the queue receive task to unblock and
\r
155 toggle the LED. 0 is used as the block time so the sending operation
\r
156 will not block - it shouldn't need to block as the queue should always
\r
157 be empty at this point in the code. */
\r
158 xQueueSend( xQueue, &ulValueToSend, 0U );
\r
161 /*-----------------------------------------------------------*/
\r
163 static void prvQueueReceiveTask( void *pvParameters )
\r
165 unsigned long ulReceivedValue;
\r
166 const unsigned long ulExpectedValue = 100UL;
\r
168 /* Remove compiler warning about unused parameter. */
\r
169 ( void ) pvParameters;
\r
173 /* Wait until something arrives in the queue - this task will block
\r
174 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
\r
175 FreeRTOSConfig.h. */
\r
176 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
\r
178 /* To get here something must have been received from the queue, but
\r
179 is it the expected value? If it is, toggle the LED. */
\r
180 if( ulReceivedValue == ulExpectedValue )
\r
182 vParTestToggleLED( mainTASK_LED );
\r
183 ulReceivedValue = 0U;
\r
187 /*-----------------------------------------------------------*/
\r