]> begriffs open source - cmsis-freertos/blob - Demo/WIN32-MingW/main_blinky.c
Update cmsis_os2.c
[cmsis-freertos] / Demo / WIN32-MingW / 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 1: The Win32 port is a simulation (or is that emulation?) only!  Do not
72  * expect to get real time behaviour from the Win32 port or this demo
73  * application.  It is provided as a convenient development and demonstration
74  * test bed only.  This was tested using Windows XP on a dual core laptop.
75  *
76  * Windows will not be running the FreeRTOS simulator threads continuously, so
77  * the timing information in the FreeRTOS+Trace logs have no meaningful units.
78  * See the documentation page for the Windows simulator for an explanation of
79  * the slow timing:
80  * http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html
81  * - READ THE WEB DOCUMENTATION FOR THIS PORT FOR MORE INFORMATION ON USING IT -
82  *
83  * NOTE 2:  This project provides two demo applications.  A simple blinky style
84  * project, and a more comprehensive test and demo application.  The
85  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
86  * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
87  * in main.c.  This file implements the simply blinky style version.
88  *
89  * NOTE 3:  This file only contains the source code that is specific to the
90  * basic demo.  Generic functions, such FreeRTOS hook functions, are defined
91  * in main.c.
92  ******************************************************************************
93  *
94  * main_blinky() creates one queue, and two tasks.  It then starts the
95  * scheduler.
96  *
97  * The Queue Send Task:
98  * The queue send task is implemented by the prvQueueSendTask() function in
99  * this file.  prvQueueSendTask() sits in a loop that causes it to repeatedly
100  * block for 200 (simulated as far as the scheduler is concerned, but in
101  * reality much longer - see notes above) milliseconds, before sending the
102  * value 100 to the queue that was created within main_blinky().  Once the
103  * value is sent, the task loops back around to block for another 200
104  * (simulated) milliseconds.
105  *
106  * The Queue Receive Task:
107  * The queue receive task is implemented by the prvQueueReceiveTask() function
108  * in this file.  prvQueueReceiveTask() sits in a loop where it repeatedly
109  * blocks on attempts to read data from the queue that was created within
110  * main_blinky().  When data is received, the task checks the value of the
111  * data, and if the value equals the expected 100, outputs a message.  The
112  * 'block time' parameter passed to the queue receive function specifies that
113  * the task should be held in the Blocked state indefinitely to wait for data
114  * to be available on the queue.  The queue receive task will only leave the
115  * Blocked state when the queue send task writes to the queue.  As the queue
116  * send task writes to the queue every 200 (simulated - see notes above)
117  * milliseconds, the queue receive task leaves the Blocked state every 200
118  * milliseconds, and therefore outputs a message every 200 milliseconds.
119  */
120
121 /* Standard includes. */
122 #include <stdio.h>
123
124 /* Kernel includes. */
125 #include "FreeRTOS.h"
126 #include "task.h"
127 #include "semphr.h"
128
129 /* Priorities at which the tasks are created. */
130 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )
131 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )
132
133 /* The rate at which data is sent to the queue.  The 200ms value is converted
134 to ticks using the portTICK_PERIOD_MS constant. */
135 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 200 / portTICK_PERIOD_MS )
136
137 /* The number of items the queue can hold.  This is 1 as the receive task
138 will remove items as they are added, meaning the send task should always find
139 the queue empty. */
140 #define mainQUEUE_LENGTH                                        ( 1 )
141
142 /* Values passed to the two tasks just to check the task parameter
143 functionality. */
144 #define mainQUEUE_SEND_PARAMETER                        ( 0x1111UL )
145 #define mainQUEUE_RECEIVE_PARAMETER                     ( 0x22UL )
146
147 /*-----------------------------------------------------------*/
148
149 /*
150  * The tasks as described in the comments at the top of this file.
151  */
152 static void prvQueueReceiveTask( void *pvParameters );
153 static void prvQueueSendTask( void *pvParameters );
154
155 /*-----------------------------------------------------------*/
156
157 /* The queue used by both tasks. */
158 static QueueHandle_t xQueue = NULL;
159
160 /*-----------------------------------------------------------*/
161
162 void main_blinky( void )
163 {
164         /* Create the queue. */
165         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );
166
167         if( xQueue != NULL )
168         {
169                 /* Start the two tasks as described in the comments at the top of this
170                 file. */
171                 xTaskCreate( prvQueueReceiveTask,                                       /* The function that implements the task. */
172                                         "Rx",                                                                   /* The text name assigned to the task - for debug only as it is not used by the kernel. */
173                                         configMINIMAL_STACK_SIZE,                               /* The size of the stack to allocate to the task. */
174                                         ( void * ) mainQUEUE_RECEIVE_PARAMETER, /* The parameter passed to the task - just to check the functionality. */
175                                         mainQUEUE_RECEIVE_TASK_PRIORITY,                /* The priority assigned to the task. */
176                                         NULL );                                                                 /* The task handle is not required, so NULL is passed. */
177
178                 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, ( void * ) mainQUEUE_SEND_PARAMETER, mainQUEUE_SEND_TASK_PRIORITY, NULL );
179
180                 /* Start the tasks and timer running. */
181                 vTaskStartScheduler();
182         }
183
184         /* If all is well, the scheduler will now be running, and the following
185         line will never be reached.  If the following line does execute, then
186         there was insufficient FreeRTOS heap memory available for the idle and/or
187         timer tasks     to be created.  See the memory management section on the
188         FreeRTOS web site for more details. */
189         for( ;; );
190 }
191 /*-----------------------------------------------------------*/
192
193 static void prvQueueSendTask( void *pvParameters )
194 {
195 TickType_t xNextWakeTime;
196 const unsigned long ulValueToSend = 100UL;
197 const TickType_t xBlockTime = pdMS_TO_TICKS( mainQUEUE_SEND_FREQUENCY_MS );
198
199         /* Remove compiler warning in the case that configASSERT() is not
200         defined. */
201         ( void ) pvParameters;
202
203         /* Check the task parameter is as expected. */
204         configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_SEND_PARAMETER );
205
206         /* Initialise xNextWakeTime - this only needs to be done once. */
207         xNextWakeTime = xTaskGetTickCount();
208
209         for( ;; )
210         {
211                 /* Place this task in the blocked state until it is time to run again.
212                 The block time is specified in ticks, the constant used converts ticks
213                 to ms.  While in the Blocked state this task will not consume any CPU
214                 time. */
215                 vTaskDelayUntil( &xNextWakeTime, xBlockTime );
216
217                 /* Send to the queue - causing the queue receive task to unblock and
218                 toggle the LED.  0 is used as the block time so the sending operation
219                 will not block - it shouldn't need to block as the queue should always
220                 be empty at this point in the code. */
221                 xQueueSend( xQueue, &ulValueToSend, 0U );
222         }
223 }
224 /*-----------------------------------------------------------*/
225
226 static void prvQueueReceiveTask( void *pvParameters )
227 {
228 unsigned long ulReceivedValue;
229
230         /* Remove compiler warning in the case that configASSERT() is not
231         defined. */
232         ( void ) pvParameters;
233
234         /* Check the task parameter is as expected. */
235         configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_RECEIVE_PARAMETER );
236
237         for( ;; )
238         {
239                 /* Wait until something arrives in the queue - this task will block
240                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
241                 FreeRTOSConfig.h. */
242                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
243
244                 /*  To get here something must have been received from the queue, but
245                 is it the expected value?  If it is, toggle the LED. */
246                 if( ulReceivedValue == 100UL )
247                 {
248                         /* Normally calling printf() from a task is not a good idea.  Here
249                         there is lots of stack space and only one task is using console  IO
250                         so it is ok. */
251                         printf( "Message received\r\n" );
252                         fflush( stdout );
253                         ulReceivedValue = 0U;
254                 }
255         }
256 }
257 /*-----------------------------------------------------------*/
258