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