]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/WIN32-MSVC/main_blinky.c
320ad130d541e68856066304e85a7dbd3ca3c5a1
[freertos] / FreeRTOS / Demo / WIN32-MSVC / main_blinky.c
1 /*\r
2     FreeRTOS V8.2.3 - Copyright (C) 2015 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /******************************************************************************\r
71  * NOTE: Windows will not be running the FreeRTOS demo threads continuously, so\r
72  * do not expect to get real time behaviour from the FreeRTOS Windows port, or\r
73  * this demo application.  Also, the timing information in the FreeRTOS+Trace\r
74  * logs have no meaningful units.  See the documentation page for the Windows\r
75  * port for further information:\r
76  * http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html\r
77  *\r
78  * NOTE 2:  This project provides two demo applications.  A simple blinky style\r
79  * project, and a more comprehensive test and demo application.  The\r
80  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
81  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
82  * in main.c.  This file implements the simply blinky version.  Console output\r
83  * is used in place of the normal LED toggling.\r
84  *\r
85  * NOTE 3:  This file only contains the source code that is specific to the\r
86  * basic demo.  Generic functions, such FreeRTOS hook functions, are defined\r
87  * in main.c.\r
88  ******************************************************************************\r
89  *\r
90  * main_blinky() creates one queue, one software timer, and two tasks.  It then\r
91  * starts the scheduler.\r
92  *\r
93  * The Queue Send Task:\r
94  * The queue send task is implemented by the prvQueueSendTask() function in\r
95  * this file.  It uses vTaskDelayUntil() to create a periodic task that sends\r
96  * the value 100 to the queue every 200 milliseconds (please read the notes\r
97  * above regarding the accuracy of timing under Windows).\r
98  *\r
99  * The Queue Send Software Timer:\r
100  * The timer is a one-shot timer that is reset by a key press.  The timer's\r
101  * period is set to two seconds - if the timer expires then its callback\r
102  * function writes the value 200 to the queue.  The callback function is\r
103  * implemented by prvQueueSendTimerCallback() within this file.\r
104  *\r
105  * The Queue Receive Task:\r
106  * The queue receive task is implemented by the prvQueueReceiveTask() function\r
107  * in this file.  prvQueueReceiveTask() waits for data to arrive on the queue.\r
108  * When data is received, the task checks the value of the data, then outputs a\r
109  * message to indicate if the data came from the queue send task or the queue\r
110  * send software timer.\r
111  *\r
112  * Expected Behaviour:\r
113  * - The queue send task writes to the queue every 200ms, so every 200ms the\r
114  *   queue receive task will output a message indicating that data was received\r
115  *   on the queue from the queue send task.\r
116  * - The queue send software timer has a period of two seconds, and is reset\r
117  *   each time a key is pressed.  So if two seconds expire without a key being\r
118  *   pressed then the queue receive task will output a message indicating that\r
119  *   data was received on the queue from the queue send software timer.\r
120  *\r
121  * NOTE:  Console input and output relies on Windows system calls, which can\r
122  * interfere with the execution of the FreeRTOS Windows port.  This demo only\r
123  * uses Windows system call occasionally.  Heavier use of Windows system calls\r
124  * can crash the port.\r
125  */\r
126 \r
127 /* Standard includes. */\r
128 #include <stdio.h>\r
129 #include <conio.h>\r
130 \r
131 /* Kernel includes. */\r
132 #include "FreeRTOS.h"\r
133 #include "task.h"\r
134 #include "timers.h"\r
135 #include "semphr.h"\r
136 \r
137 /* Priorities at which the tasks are created. */\r
138 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
139 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
140 \r
141 /* The rate at which data is sent to the queue.  The times are converted from\r
142 milliseconds to ticks using the pdMS_TO_TICKS() macro. */\r
143 #define mainTASK_SEND_FREQUENCY_MS                      pdMS_TO_TICKS( 200UL )\r
144 #define mainTIMER_SEND_FREQUENCY_MS                     pdMS_TO_TICKS( 2000UL )\r
145 \r
146 /* The number of items the queue can hold at once. */\r
147 #define mainQUEUE_LENGTH                                        ( 2 )\r
148 \r
149 /* The values sent to the queue receive task from the queue send task and the\r
150 queue send software timer respectively. */\r
151 #define mainVALUE_SENT_FROM_TASK                        ( 100UL )\r
152 #define mainVALUE_SENT_FROM_TIMER                       ( 200UL )\r
153 \r
154 /*-----------------------------------------------------------*/\r
155 \r
156 /*\r
157  * The tasks as described in the comments at the top of this file.\r
158  */\r
159 static void prvQueueReceiveTask( void *pvParameters );\r
160 static void prvQueueSendTask( void *pvParameters );\r
161 \r
162 /*\r
163  * The callback function executed when the software timer expires.\r
164  */\r
165 static void prvQueueSendTimerCallback( TimerHandle_t xTimerHandle );\r
166 \r
167 /*-----------------------------------------------------------*/\r
168 \r
169 /* The queue used by both tasks. */\r
170 static QueueHandle_t xQueue = NULL;\r
171 \r
172 /* A software timer that is started from the tick hook. */\r
173 static TimerHandle_t xTimer = NULL;\r
174 \r
175 /*-----------------------------------------------------------*/\r
176 \r
177 /*** SEE THE COMMENTS AT THE TOP OF THIS FILE ***/\r
178 void main_blinky( void )\r
179 {\r
180 const TickType_t xTimerPeriod = mainTIMER_SEND_FREQUENCY_MS;\r
181 \r
182         /* Create the queue. */\r
183         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( uint32_t ) );\r
184 \r
185         if( xQueue != NULL )\r
186         {\r
187                 /* Start the two tasks as described in the comments at the top of this\r
188                 file. */\r
189                 xTaskCreate( prvQueueReceiveTask,                       /* The function that implements the task. */\r
190                                         "Rx",                                                   /* The text name assigned to the task - for debug only as it is not used by the kernel. */\r
191                                         configMINIMAL_STACK_SIZE,               /* The size of the stack to allocate to the task. */\r
192                                         NULL,                                                   /* The parameter passed to the task - not used in this simple case. */\r
193                                         mainQUEUE_RECEIVE_TASK_PRIORITY,/* The priority assigned to the task. */\r
194                                         NULL );                                                 /* The task handle is not required, so NULL is passed. */\r
195 \r
196                 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
197 \r
198                 /* Create the software timer, but don't start it yet. */\r
199                 xTimer = xTimerCreate( "Timer",                         /* The text name assigned to the software timer - for debug only as it is not used by the kernel. */\r
200                                                                 xTimerPeriod,           /* The period of the software timer in ticks. */\r
201                                                                 pdFALSE,                        /* xAutoReload is set to pdFALSE, so this is a one shot timer. */\r
202                                                                 NULL,                           /* The timer's ID is not used. */\r
203                                                                 prvQueueSendTimerCallback );/* The function executed when the timer expires. */\r
204 \r
205                 /* Start the tasks and timer running. */\r
206                 vTaskStartScheduler();\r
207         }\r
208 \r
209         /* If all is well, the scheduler will now be running, and the following\r
210         line will never be reached.  If the following line does execute, then\r
211         there was insufficient FreeRTOS heap memory available for the idle and/or\r
212         timer tasks     to be created.  See the memory management section on the\r
213         FreeRTOS web site for more details. */\r
214         for( ;; );\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \r
218 static void prvQueueSendTask( void *pvParameters )\r
219 {\r
220 TickType_t xNextWakeTime;\r
221 const TickType_t xBlockTime = mainTASK_SEND_FREQUENCY_MS;\r
222 const uint32_t ulValueToSend = mainVALUE_SENT_FROM_TASK;\r
223 \r
224         /* Prevent the compiler warning about the unused parameter. */\r
225         ( void ) pvParameters;\r
226 \r
227         /* Initialise xNextWakeTime - this only needs to be done once. */\r
228         xNextWakeTime = xTaskGetTickCount();\r
229 \r
230         for( ;; )\r
231         {\r
232                 /* Place this task in the blocked state until it is time to run again.\r
233                 The block time is specified in ticks, pdMS_TO_TICKS() was used to\r
234                 convert a time specified in milliseconds into a time specified in ticks.\r
235                 While in the Blocked state this task will not consume any CPU time. */\r
236                 vTaskDelayUntil( &xNextWakeTime, xBlockTime );\r
237 \r
238                 /* Send to the queue - causing the queue receive task to unblock and\r
239                 write to the console.  0 is used as the block time so the send operation\r
240                 will not block - it shouldn't need to block as the queue should always\r
241                 have at least one space at this point in the code. */\r
242                 xQueueSend( xQueue, &ulValueToSend, 0U );\r
243         }\r
244 }\r
245 /*-----------------------------------------------------------*/\r
246 \r
247 static void prvQueueSendTimerCallback( TimerHandle_t xTimerHandle )\r
248 {\r
249 const uint32_t ulValueToSend = mainVALUE_SENT_FROM_TIMER;\r
250 \r
251         /* This is the software timer callback function.  The software timer has a\r
252         period of two seconds and is reset each time a key is pressed.  This\r
253         callback function will execute if the timer expires, which will only happen\r
254         if a key is not pressed for two seconds. */\r
255 \r
256         /* Avoid compiler warnings resulting from the unused parameter. */\r
257         ( void ) xTimerHandle;\r
258 \r
259         /* Send to the queue - causing the queue receive task to unblock and\r
260         write out a message.  This function is called from the timer/daemon task, so\r
261         must not block.  Hence the block time is set to 0. */\r
262         xQueueSend( xQueue, &ulValueToSend, 0U );\r
263 }\r
264 /*-----------------------------------------------------------*/\r
265 \r
266 static void prvQueueReceiveTask( void *pvParameters )\r
267 {\r
268 uint32_t ulReceivedValue;\r
269 \r
270         /* Prevent the compiler warning about the unused parameter. */\r
271         ( void ) pvParameters;\r
272 \r
273         for( ;; )\r
274         {\r
275                 /* Wait until something arrives in the queue - this task will block\r
276                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
277                 FreeRTOSConfig.h.  It will not use any CPU time while it is in the\r
278                 Blocked state. */\r
279                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
280 \r
281                 /*  To get here something must have been received from the queue, but\r
282                 is it an expected value?  Normally calling printf() from a task is not\r
283                 a good idea.  Here there is lots of stack space and only one task is\r
284                 using console IO so it is ok.  However, note the comments at the top of\r
285                 this file about the risks of making Windows system calls (such as \r
286                 console output) from a FreeRTOS task. */\r
287                 if( ulReceivedValue == mainVALUE_SENT_FROM_TASK )\r
288                 {\r
289                         printf( "Message received from task\r\n" );\r
290                 }\r
291                 else if( ulReceivedValue == mainVALUE_SENT_FROM_TIMER )\r
292                 {\r
293                         printf( "Message received from software timer\r\n" );\r
294                 }\r
295                 else\r
296                 {\r
297                         printf( "Unexpected message\r\n" );\r
298                 }\r
299 \r
300                 /* Reset the timer if a key has been pressed.  The timer will write\r
301                 mainVALUE_SENT_FROM_TIMER to the queue when it expires. */\r
302                 if( _kbhit() != 0 )\r
303                 {\r
304                         /* Remove the key from the input buffer. */\r
305                         ( void ) _getch();\r
306 \r
307                         /* Reset the software timer. */\r
308                         xTimerReset( xTimer, portMAX_DELAY );\r
309                 }\r
310         }\r
311 }\r
312 /*-----------------------------------------------------------*/\r
313 \r
314 \r