2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 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. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /******************************************************************************
\r
30 * NOTE 1: This project provides two demo applications. A simple blinky style
\r
31 * project, and a more comprehensive test and demo application. 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 Blinky Software Timer:
\r
45 * This demonstrates an auto-reload software timer. The timer callback function
\r
46 * does nothing but toggle an LED.
\r
48 * The Queue Send Task:
\r
49 * The queue send task is implemented by prvQueueSendTask() in main_blinky.c.
\r
50 * prvQueueSendTask() repeatedly blocks for 200 milliseconds before sending the
\r
51 * value 100 to the queue that was created in main_blinky().
\r
53 * The Queue Receive Task:
\r
54 * The queue receive task is implemented by prvQueueReceiveTask() in
\r
55 * main_blinky.c. prvQueueReceiveTask() repeatedly blocks on attempts to read
\r
56 * from the queue that was created in main_blinky(), toggling an LED each time
\r
57 * data is received. The queue send task sends data to the queue every 200
\r
58 * milliseconds, so the LED will toggle every 200 milliseconds.
\r
61 /* Standard includes. */
\r
64 /* Kernel includes. */
\r
65 #include "FreeRTOS.h"
\r
70 /* Priorities at which the tasks are created. */
\r
71 #define mainQUEUE_SEND_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
72 #define mainQUEUE_RECEIVE_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
74 /* The rate at which data is sent to the queue. The 200ms value is converted
\r
75 to ticks using the portTICK_PERIOD_MS constant. */
\r
76 #define mainQUEUE_SEND_FREQUENCY_MS ( 200 / portTICK_PERIOD_MS )
\r
78 /* The number of items the queue can hold. This is 1 as the receive task
\r
79 will remove items as they are added, meaning the send task should always find
\r
81 #define mainQUEUE_LENGTH ( 1 )
\r
83 /* Values passed to the two tasks just to check the task parameter
\r
85 #define mainQUEUE_SEND_PARAMETER ( 0x1111UL )
\r
86 #define mainQUEUE_RECEIVE_PARAMETER ( 0x22UL )
\r
88 /* The period of the blinky software timer. The period is specified in ms and
\r
89 converted to ticks using the portTICK_PERIOD_MS constant. */
\r
90 #define mainBLINKY_TIMER_PERIOD ( 50 / portTICK_PERIOD_MS )
\r
92 /* The LED used by the communicating tasks and the blinky timer respectively. */
\r
93 #define mainTASKS_LED ( 0 )
\r
94 #define mainTIMER_LED ( 1 )
\r
97 #define mainDONT_BLOCK ( 0 )
\r
99 /*-----------------------------------------------------------*/
\r
102 * The tasks as described in the comments at the top of this file.
\r
104 static void prvQueueReceiveTask( void *pvParameters );
\r
105 static void prvQueueSendTask( void *pvParameters );
\r
108 * The callback function for the blinky software timer, as described at the top
\r
111 static void prvBlinkyTimerCallback( TimerHandle_t xTimer );
\r
114 * Called by main() to create the simply blinky style application if
\r
115 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.
\r
117 void main_blinky( void );
\r
120 * LED toggle function that uses a critical section to ensure thread safety.
\r
122 extern void vToggleLED( uint8_t ucLED );
\r
124 /*-----------------------------------------------------------*/
\r
126 /* The queue used by both tasks. */
\r
127 static QueueHandle_t xQueue = NULL;
\r
129 /*-----------------------------------------------------------*/
\r
131 void main_blinky( void )
\r
133 TimerHandle_t xTimer;
\r
135 /* Create the queue. */
\r
136 xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );
\r
137 configASSERT( xQueue );
\r
139 if( xQueue != NULL )
\r
141 /* Create the two tasks as described in the comments at the top of this
\r
143 xTaskCreate( prvQueueReceiveTask, /* The function that implements the task. */
\r
144 "Rx", /* The text name assigned to the task - for debug only as it is not used by the kernel. */
\r
145 configMINIMAL_STACK_SIZE, /* The size of the stack to allocate to the task. */
\r
146 ( void * ) mainQUEUE_RECEIVE_PARAMETER, /* The parameter passed to the task - just to check the functionality. */
\r
147 mainQUEUE_RECEIVE_TASK_PRIORITY, /* The priority assigned to the task. */
\r
148 NULL ); /* The task handle is not required, so NULL is passed. */
\r
150 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, ( void * ) mainQUEUE_SEND_PARAMETER, mainQUEUE_SEND_TASK_PRIORITY, NULL );
\r
153 /* Create the blinky software timer as described at the top of this file. */
\r
154 xTimer = xTimerCreate( "Blinky", /* A text name, purely to help debugging. */
\r
155 ( mainBLINKY_TIMER_PERIOD ),/* The timer period. */
\r
156 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
157 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
158 prvBlinkyTimerCallback ); /* The callback function that inspects the status of all the other tasks. */
\r
159 configASSERT( xTimer );
\r
161 if( xTimer != NULL )
\r
163 xTimerStart( xTimer, mainDONT_BLOCK );
\r
166 /* Start the tasks and timer running. */
\r
167 vTaskStartScheduler();
\r
170 /* If all is well, the scheduler will now be running, and the following
\r
171 line will never be reached. If the following line does execute, then
\r
172 there was insufficient FreeRTOS heap memory available for the idle and/or
\r
173 timer tasks to be created. See the memory management section on the
\r
174 FreeRTOS web site for more details. http://www.freertos.org/a00111.html */
\r
177 /*-----------------------------------------------------------*/
\r
179 static void prvQueueSendTask( void *pvParameters )
\r
181 TickType_t xNextWakeTime;
\r
182 const unsigned long ulValueToSend = 100UL;
\r
184 /* Remove compiler warnings in the case that configASSERT() is not defined. */
\r
185 ( void ) pvParameters;
\r
187 /* Check the task parameter is as expected. */
\r
188 configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_SEND_PARAMETER );
\r
190 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
191 xNextWakeTime = xTaskGetTickCount();
\r
195 /* Place this task in the blocked state until it is time to run again.
\r
196 The block time is specified in ticks, the constant used converts ticks
\r
197 to ms. While in the Blocked state this task will not consume any CPU
\r
199 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );
\r
201 /* Send to the queue - causing the queue receive task to unblock and
\r
202 toggle the LED. 0 is used as the block time so the sending operation
\r
203 will not block - it shouldn't need to block as the queue should always
\r
204 be empty at this point in the code. */
\r
205 xQueueSend( xQueue, &ulValueToSend, 0U );
\r
208 /*-----------------------------------------------------------*/
\r
210 static void prvQueueReceiveTask( void *pvParameters )
\r
212 unsigned long ulReceivedValue;
\r
214 /* Remove compiler warnings in the case where configASSERT() is not defined. */
\r
215 ( void ) pvParameters;
\r
217 /* Check the task parameter is as expected. */
\r
218 configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_RECEIVE_PARAMETER );
\r
222 /* Wait until something arrives in the queue - this task will block
\r
223 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
\r
224 FreeRTOSConfig.h. */
\r
225 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
\r
227 /* To get here something must have been received from the queue, but
\r
228 is it the expected value? If it is, toggle the LED. */
\r
229 if( ulReceivedValue == 100UL )
\r
231 vToggleLED( mainTASKS_LED );
\r
232 ulReceivedValue = 0U;
\r
236 /*-----------------------------------------------------------*/
\r
238 static void prvBlinkyTimerCallback( TimerHandle_t xTimer )
\r
240 /* Avoid compiler warnings. */
\r
243 /* This function is called when the blinky software time expires. All the
\r
244 function does is toggle the LED. LED mainTIMER_LED should therefore toggle
\r
245 with the period set by mainBLINKY_TIMER_PERIOD. */
\r
246 vToggleLED( mainTIMER_LED );
\r
248 /*-----------------------------------------------------------*/
\r