2 * FreeRTOS Kernel V10.1.0
\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 two demo applications. A simple blinky style
\r
30 * project, and a more comprehensive demo application that makes use of
\r
31 * FreeRTOS_CLI, FreeRTOS+UDP and FreeRTOS+FAT SL. The
\r
32 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
\r
33 * between the two. See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
\r
34 * in main.c. This file implements the simply blinky style version.
\r
36 * NOTE 2: This file only contains the source code that is specific to the
\r
37 * basic demo. Generic functions, such FreeRTOS hook functions, and functions
\r
38 * required to configure the hardware, are defined in main.c.
\r
39 ******************************************************************************
\r
41 * main_blinky() creates one queue, two tasks and one software timer. It then
\r
42 * starts the scheduler.
\r
44 * The Queue Send Task:
\r
45 * The queue send task is implemented by the prvQueueSendTask() function in
\r
46 * this file. The task sits in a loop that sends a value to the queue every
\r
49 * The Queue Receive Task:
\r
50 * The queue receive task is implemented by the prvQueueReceiveTask() function
\r
51 * in this file. The task sits in a loop that blocks on the queue to wait for
\r
52 * data to arrive (it does not use any CPU time while it is in the Blocked
\r
53 * state), toggling an LED each time it receives the value sent by the queue
\r
54 * send task. As the queue send task writes to the queue every 200 milliseconds
\r
55 * the LED will toggle every 200 milliseconds.
\r
59 /* Kernel includes. */
\r
60 #include "FreeRTOS.h"
\r
65 /* Demo application includes. */
\r
66 #include "partest.h"
\r
68 /* Priorities at which the tasks are created. */
\r
69 #define mainQUEUE_RECEIVE_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
70 #define mainQUEUE_SEND_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
72 /* The rate at which data is sent to the queue. The 200ms value is converted
\r
73 to ticks using the portTICK_PERIOD_MS constant. */
\r
74 #define mainQUEUE_SEND_FREQUENCY_MS ( 200 / portTICK_PERIOD_MS )
\r
76 /* The number of items the queue can hold. This is 1 as the receive task
\r
77 will remove items as they are added, meaning the send task should always find
\r
79 #define mainQUEUE_LENGTH ( 1 )
\r
81 /* The period of the blinky software timer. The period is specified in ms and
\r
82 converted to ticks using the portTICK_PERIOD_MS constant. */
\r
83 #define mainBLINKY_TIMER_PERIOD ( 50 / portTICK_PERIOD_MS )
\r
85 /* A block time of zero simply means "don't block". */
\r
86 #define mainDONT_BLOCK ( 0 )
\r
88 /* The LEDs toggled by the timer callback and queue receive task respectively. */
\r
89 #define mainTIMER_LED 0
\r
90 #define mainTASK_LED 1
\r
92 /*-----------------------------------------------------------*/
\r
95 * The tasks as described in the comments at the top of this file.
\r
97 static void prvQueueReceiveTask( void *pvParameters );
\r
98 static void prvQueueSendTask( void *pvParameters );
\r
101 * The callback function for the blinky software timer, as described at the top
\r
104 static void prvBlinkyTimerCallback( TimerHandle_t xTimer );
\r
107 * Called by main() to create the simply blinky style application if
\r
108 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.
\r
110 void main_blinky( void );
\r
112 /*-----------------------------------------------------------*/
\r
114 void main_blinky( void )
\r
116 TimerHandle_t xTimer;
\r
117 QueueHandle_t xQueue;
\r
119 /* Create the queue. */
\r
120 xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );
\r
122 if( xQueue != NULL )
\r
124 /* Start the two tasks as described in the comments at the top of this
\r
126 xTaskCreate( prvQueueReceiveTask, /* The function that implements the task. */
\r
127 "Rx", /* The text name assigned to the task - for debug only as it is not used by the kernel. */
\r
128 configMINIMAL_STACK_SIZE, /* The size of the stack to allocate to the task. */
\r
129 ( void * ) xQueue, /* Pass the queue into the task using the task parameter. */
\r
130 mainQUEUE_RECEIVE_TASK_PRIORITY, /* The priority assigned to the task. */
\r
131 NULL ); /* The task handle is not required, so NULL is passed. */
\r
133 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, ( void * ) xQueue, mainQUEUE_SEND_TASK_PRIORITY, NULL );
\r
135 /* Create the blinky software timer as described at the top of this
\r
137 xTimer = xTimerCreate( "Blinky", /* A text name, purely to help debugging. */
\r
138 ( mainBLINKY_TIMER_PERIOD ),/* The timer period. */
\r
139 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
140 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
141 prvBlinkyTimerCallback ); /* The callback function that inspects the status of all the other tasks. */
\r
143 configASSERT( xTimer );
\r
145 if( xTimer != NULL )
\r
147 xTimerStart( xTimer, mainDONT_BLOCK );
\r
150 /* Start the tasks and timer running. */
\r
151 vTaskStartScheduler();
\r
154 /* If all is well, the scheduler will now be running, and the following
\r
155 line will never be reached. If the following line does execute, then
\r
156 there was insufficient FreeRTOS heap memory available for the idle and/or
\r
157 timer tasks to be created. See the memory management section on the
\r
158 FreeRTOS web site for more details. */
\r
161 /*-----------------------------------------------------------*/
\r
163 static void prvQueueSendTask( void *pvParameters )
\r
165 TickType_t xNextWakeTime;
\r
166 const unsigned long ulValueToSend = 100UL;
\r
167 QueueHandle_t xQueue;
\r
169 /* The handle of the queue is passed in using the task's parameter. */
\r
170 xQueue = ( QueueHandle_t ) pvParameters;
\r
172 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
173 xNextWakeTime = xTaskGetTickCount();
\r
177 /* Place this task in the blocked state until it is time to run again.
\r
178 The block time is specified in ticks, the constant used converts ticks
\r
179 to ms. While in the Blocked state this task will not consume any CPU
\r
181 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );
\r
183 /* Send to the queue - causing the queue receive task to unblock and
\r
184 toggle the LED. 0 is used as the block time so the sending operation
\r
185 will not block - it shouldn't need to block as the queue should always
\r
186 be empty at this point in the code. */
\r
187 xQueueSend( xQueue, &ulValueToSend, mainDONT_BLOCK );
\r
190 /*-----------------------------------------------------------*/
\r
192 static void prvQueueReceiveTask( void *pvParameters )
\r
194 unsigned long ulReceivedValue;
\r
195 QueueHandle_t xQueue;
\r
197 /* The queue is passed in as the task's parameter. */
\r
198 xQueue = ( QueueHandle_t ) pvParameters;
\r
202 /* Wait until something arrives in the queue - this task will block
\r
203 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
\r
204 FreeRTOSConfig.h. */
\r
205 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
\r
207 /* To get here something must have been received from the queue, but
\r
208 is it the expected value? If it is, toggle the LED. */
\r
209 if( ulReceivedValue == 100UL )
\r
211 vParTestToggleLED( mainTASK_LED );
\r
212 ulReceivedValue = 0U;
\r
216 /*-----------------------------------------------------------*/
\r
218 static void prvBlinkyTimerCallback( TimerHandle_t xTimer )
\r
220 /* Avoid compiler warnings. */
\r
223 /* This function is called when the blinky software time expires. All the
\r
224 function does is toggle the LED. LED mainTIMER_LED should therefore toggle
\r
225 with the period set by mainBLINKY_TIMER_PERIOD. */
\r
226 vParTestToggleLED( mainTIMER_LED );
\r
228 /*-----------------------------------------------------------*/
\r