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