]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_CLI_with_Trace_Windows_Simulator/main.c
Update version numbers in preparation for V8.2.0 release candidate 1.
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_CLI_with_Trace_Windows_Simulator / main.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 /*\r
97  ******************************************************************************\r
98  * -NOTE- The Win32 port is a simulation (or is that emulation?) only!  Do not\r
99  * expect to get real time behaviour from the Win32 port or this demo\r
100  * application.  It is provided as a convenient development and demonstration\r
101  * test bed only.  This was tested using Windows XP on a dual core laptop.\r
102  *\r
103  * Windows will not be running the FreeRTOS simulator threads continuously, so\r
104  * the timing information in the FreeRTOS+Trace logs have no meaningful units.\r
105  * See the documentation page for the Windows simulator for an explanation of\r
106  * the slow timing:\r
107  * http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html\r
108  * - READ THE WEB DOCUMENTATION FOR THIS PORT FOR MORE INFORMATION ON USING IT -\r
109  *\r
110  * Documentation for this demo can be found on:\r
111  * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_Trace/Free_RTOS_Plus_Trace_CLI_Example.shtml\r
112  ******************************************************************************\r
113  *\r
114  * This is a simple FreeRTOS Windows simulator project that makes it easy to\r
115  * evaluate FreeRTOS+CLI and FreeRTOS+Trace on a standard desktop PC, without\r
116  * any external hardware or interfaces being required.\r
117  *\r
118  * To keep everything as simple as possible, the command line interface is\r
119  * accessed through a UDP socket on the default Windows loopback IP address of\r
120  * 127.0.0.1.  Full instructions are provided on the documentation page\r
121  * referenced above.\r
122  *\r
123  * Commands are provided to both start and stop a FreeRTOS+Trace recording.\r
124  * Stopping a recording will result in the recorded data being saved to the\r
125  * hard disk, ready for viewing in the FreeRTOS+Trace graphical user interface.\r
126  * Again, full instructions are provided on the documentation page referenced\r
127  * above.\r
128  *\r
129  * A queue send task and a queue receive task are defined in this file.  The\r
130  * queue receive task spends most of its time blocked on the queue waiting for\r
131  * messages to arrive.  The queue send task periodically sends a message to the\r
132  * queue, causing the queue receive task to exit the Blocked state.  The\r
133  * priority of the queue receive task is above that of the queue send task, so\r
134  * it pre-empts the queue send task as soon as it leaves the Blocked state.  It\r
135  * then consumes the message from the queue and prints "message received" to\r
136  * the screen before returning to block on the queue once again.  This\r
137  * sequencing is clearly visible in the recorded FreeRTOS+Trace data.\r
138  *\r
139  */\r
140 \r
141 /* Standard includes. */\r
142 #include <stdio.h>\r
143 #include <stdint.h>\r
144 \r
145 /* FreeRTOS includes. */\r
146 #include <FreeRTOS.h>\r
147 #include "task.h"\r
148 #include "queue.h"\r
149 \r
150 /* FreeRTOS+Trace includes. */\r
151 #include "trcUser.h"\r
152 \r
153 /* Priorities at which the tasks are created. */\r
154 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
155 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
156 #define mainUDP_CLI_TASK_PRIORITY                       ( tskIDLE_PRIORITY )\r
157 \r
158 /* The rate at which data is sent to the queue.  The (simulated) 250ms value is\r
159 converted to ticks using the portTICK_RATE_MS constant. */\r
160 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 250 / portTICK_RATE_MS )\r
161 \r
162 /* The number of items the queue can hold.  This is 1 as the receive task\r
163 will remove items as they are added, meaning the send task should always find\r
164 the queue empty. */\r
165 #define mainQUEUE_LENGTH                                        ( 1 )\r
166 \r
167 /*-----------------------------------------------------------*/\r
168 \r
169 /*\r
170  * The queue send and receive tasks as described in the comments at the top of\r
171  * this file.\r
172  */\r
173 static void prvQueueReceiveTask( void *pvParameters );\r
174 static void prvQueueSendTask( void *pvParameters );\r
175 \r
176 /*\r
177  * The task that implements the UDP command interpreter using FreeRTOS+CLI.\r
178  */\r
179 extern void vUDPCommandInterpreterTask( void *pvParameters );\r
180 \r
181 /*\r
182  * Register commands that can be used with FreeRTOS+CLI through the UDP socket.\r
183  * The commands are defined in CLI-commands.c.\r
184  */\r
185 extern void vRegisterCLICommands( void );\r
186 \r
187 /* The queue used by both tasks. */\r
188 static xQueueHandle xQueue = NULL;\r
189 \r
190 /* The user trace event posted to the trace recording on each tick interrupt.\r
191 Note tick events will not appear in the trace recording with regular period\r
192 because this project runs in a Windows simulator, and does not therefore\r
193 exhibit deterministic behaviour. */\r
194 traceLabel xTickTraceUserEvent;\r
195 \r
196 /*-----------------------------------------------------------*/\r
197 \r
198 int main( void )\r
199 {\r
200 const uint32_t ulLongTime_ms = 250UL;\r
201 \r
202         /* Initialise the trace recorder and create the label used to post user\r
203         events to the trace recording on each tick interrupt. */\r
204         vTraceInitTraceData();\r
205         xTickTraceUserEvent = xTraceOpenLabel( "tick" );\r
206 \r
207         /* Create the queue used to pass messages from the queue send task to the\r
208         queue receive task. */\r
209         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
210 \r
211         /* Give the queue a name for the FreeRTOS+Trace log. */\r
212         vTraceSetQueueName( xQueue, "DemoQ" );\r
213 \r
214         /* Start the two tasks as described in the comments at the top of this\r
215         file. */\r
216         xTaskCreate( prvQueueReceiveTask,                               /* The function that implements the task. */\r
217                                 "Rx",                                                           /* The text name assigned to the task - for debug only as it is not used by the kernel. */\r
218                                 configMINIMAL_STACK_SIZE,                       /* The size of the stack to allocate to the task.  Not actually used as a stack in the Win32 simulator port. */\r
219                                 NULL,                                                           /* The parameter passed to the task - not used in this example. */\r
220                                 mainQUEUE_RECEIVE_TASK_PRIORITY,        /* The priority assigned to the task. */\r
221                                 NULL );                                                         /* The task handle is not required, so NULL is passed. */\r
222 \r
223         xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
224 \r
225         /* Create the task that handles the CLI on a UDP port.  The port number\r
226         is set using the configUDP_CLI_PORT_NUMBER setting in FreeRTOSConfig.h. */\r
227         xTaskCreate( vUDPCommandInterpreterTask, "CLI", configMINIMAL_STACK_SIZE, NULL, mainUDP_CLI_TASK_PRIORITY, NULL );\r
228 \r
229         /* Register commands with the FreeRTOS+CLI command interpreter. */\r
230         vRegisterCLICommands();\r
231 \r
232         /* Start the tasks and timer running. */\r
233         vTaskStartScheduler();\r
234 \r
235         /* If all is well, the scheduler will now be running, and the following\r
236         line will never be reached.  If the following line does execute, then\r
237         there was insufficient FreeRTOS heap memory available for the idle and/or\r
238         timer tasks     to be created.  See the memory management section on the\r
239         FreeRTOS web site for more details (this is standard text that is not not\r
240         really applicable to the Win32 simulator port). */\r
241         for( ;; )\r
242         {\r
243                 Sleep( ulLongTime_ms );\r
244         }\r
245 }\r
246 /*-----------------------------------------------------------*/\r
247 \r
248 static void prvQueueSendTask( void *pvParameters )\r
249 {\r
250 TickType_t xNextWakeTime;\r
251 const unsigned long ulValueToSend = 100UL;\r
252 \r
253         /* Remove warning about unused parameters. */\r
254         ( void ) pvParameters;\r
255 \r
256         /* Initialise xNextWakeTime - this only needs to be done once. */\r
257         xNextWakeTime = xTaskGetTickCount();\r
258 \r
259         for( ;; )\r
260         {\r
261                 /* Place this task in the blocked state until it is time to run again.\r
262                 While in the Blocked state this task will not consume any CPU time. */\r
263                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
264 \r
265                 /* Send to the queue - causing the queue receive task to unblock and\r
266                 write a message to the display.  0 is used as the block time so the\r
267                 sending operation will not block - it shouldn't need to block as the\r
268                 queue should always     be empty at this point in the code, and it is an\r
269                 error if it is not. */\r
270                 xQueueSend( xQueue, &ulValueToSend, 0U );\r
271         }\r
272 }\r
273 /*-----------------------------------------------------------*/\r
274 \r
275 static void prvQueueReceiveTask( void *pvParameters )\r
276 {\r
277 unsigned long ulReceivedValue;\r
278 \r
279         /* Remove warning about unused parameters. */\r
280         ( void ) pvParameters;\r
281 \r
282         for( ;; )\r
283         {\r
284                 /* Wait until something arrives in the queue - this task will block\r
285                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
286                 FreeRTOSConfig.h. */\r
287                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
288 \r
289                 /*  To get here something must have been received from the queue, but\r
290                 is it the expected value?  If it is, write the message to the\r
291                 display before looping back to block on the queue again. */\r
292                 if( ulReceivedValue == 100UL )\r
293                 {\r
294                         printf( "Message received!\r\n" );\r
295                         ulReceivedValue = 0U;\r
296                 }\r
297         }\r
298 }\r
299 /*-----------------------------------------------------------*/\r
300 \r
301 void vApplicationIdleHook( void )\r
302 {\r
303 const unsigned long ulMSToSleep = 5;\r
304 \r
305         /* This function is called on each cycle of the idle task if\r
306         configUSE_IDLE_HOOK is set to 1 in FreeRTOSConfig.h.  Sleep to reduce CPU\r
307         load. */\r
308         Sleep( ulMSToSleep );\r
309 }\r
310 /*-----------------------------------------------------------*/\r
311 \r
312 void vAssertCalled( void )\r
313 {\r
314 const unsigned long ulLongSleep = 1000UL;\r
315 \r
316         taskDISABLE_INTERRUPTS();\r
317         for( ;; )\r
318         {\r
319                 Sleep( ulLongSleep );\r
320         }\r
321 }\r
322 /*-----------------------------------------------------------*/\r
323 \r
324 void vApplicationTickHook( void )\r
325 {\r
326         /* Write a user event to the trace log.\r
327         Note tick events will not appear in the trace recording with regular period\r
328         because this project runs in a Windows simulator, and does not therefore\r
329         exhibit deterministic behaviour. */\r
330         vTraceUserEvent( xTickTraceUserEvent );\r
331 }\r
332 \r