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