]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_CLI_with_Trace_Windows_Simulator/main.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_CLI_with_Trace_Windows_Simulator / main.c
1 /*\r
2     FreeRTOS V8.1.2 - 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     ***************************************************************************\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     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS 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  * Windows will not be running the FreeRTOS simulator threads continuously, so\r
74  * the timing information in the FreeRTOS+Trace logs have no meaningful units.\r
75  * See the documentation page for the Windows simulator for an explanation of\r
76  * the slow timing:\r
77  * http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html\r
78  * - READ THE WEB DOCUMENTATION FOR THIS PORT FOR MORE INFORMATION ON USING IT -\r
79  *\r
80  * Documentation for this demo can be found on:\r
81  * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_Trace/Free_RTOS_Plus_Trace_CLI_Example.shtml\r
82  ******************************************************************************\r
83  *\r
84  * This is a simple FreeRTOS Windows simulator project that makes it easy to\r
85  * evaluate FreeRTOS+CLI and FreeRTOS+Trace on a standard desktop PC, without\r
86  * any external hardware or interfaces being required.\r
87  *\r
88  * To keep everything as simple as possible, the command line interface is\r
89  * accessed through a UDP socket on the default Windows loopback IP address of\r
90  * 127.0.0.1.  Full instructions are provided on the documentation page\r
91  * referenced above.\r
92  *\r
93  * Commands are provided to both start and stop a FreeRTOS+Trace recording.\r
94  * Stopping a recording will result in the recorded data being saved to the\r
95  * hard disk, ready for viewing in the FreeRTOS+Trace graphical user interface.\r
96  * Again, full instructions are provided on the documentation page referenced\r
97  * above.\r
98  *\r
99  * A queue send task and a queue receive task are defined in this file.  The\r
100  * queue receive task spends most of its time blocked on the queue waiting for\r
101  * messages to arrive.  The queue send task periodically sends a message to the\r
102  * queue, causing the queue receive task to exit the Blocked state.  The\r
103  * priority of the queue receive task is above that of the queue send task, so\r
104  * it pre-empts the queue send task as soon as it leaves the Blocked state.  It\r
105  * then consumes the message from the queue and prints "message received" to\r
106  * the screen before returning to block on the queue once again.  This\r
107  * sequencing is clearly visible in the recorded FreeRTOS+Trace data.\r
108  *\r
109  */\r
110 \r
111 /* Standard includes. */\r
112 #include <stdio.h>\r
113 #include <stdint.h>\r
114 \r
115 /* FreeRTOS includes. */\r
116 #include <FreeRTOS.h>\r
117 #include "task.h"\r
118 #include "queue.h"\r
119 \r
120 /* FreeRTOS+Trace includes. */\r
121 #include "trcUser.h"\r
122 \r
123 /* Priorities at which the tasks are created. */\r
124 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
125 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
126 #define mainUDP_CLI_TASK_PRIORITY                       ( tskIDLE_PRIORITY )\r
127 \r
128 /* The rate at which data is sent to the queue.  The (simulated) 250ms value is\r
129 converted to ticks using the portTICK_RATE_MS constant. */\r
130 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 250 / portTICK_RATE_MS )\r
131 \r
132 /* The number of items the queue can hold.  This is 1 as the receive task\r
133 will remove items as they are added, meaning the send task should always find\r
134 the queue empty. */\r
135 #define mainQUEUE_LENGTH                                        ( 1 )\r
136 \r
137 /*-----------------------------------------------------------*/\r
138 \r
139 /*\r
140  * The queue send and receive tasks as described in the comments at the top of\r
141  * this file.\r
142  */\r
143 static void prvQueueReceiveTask( void *pvParameters );\r
144 static void prvQueueSendTask( void *pvParameters );\r
145 \r
146 /*\r
147  * The task that implements the UDP command interpreter using FreeRTOS+CLI.\r
148  */\r
149 extern void vUDPCommandInterpreterTask( void *pvParameters );\r
150 \r
151 /*\r
152  * Register commands that can be used with FreeRTOS+CLI through the UDP socket.\r
153  * The commands are defined in CLI-commands.c.\r
154  */\r
155 extern void vRegisterCLICommands( void );\r
156 \r
157 /* The queue used by both tasks. */\r
158 static xQueueHandle xQueue = NULL;\r
159 \r
160 /* The user trace event posted to the trace recording on each tick interrupt.\r
161 Note tick events will not appear in the trace recording with regular period\r
162 because this project runs in a Windows simulator, and does not therefore\r
163 exhibit deterministic behaviour. */\r
164 traceLabel xTickTraceUserEvent;\r
165 \r
166 /*-----------------------------------------------------------*/\r
167 \r
168 int main( void )\r
169 {\r
170 const uint32_t ulLongTime_ms = 250UL;\r
171 \r
172         /* Initialise the trace recorder and create the label used to post user\r
173         events to the trace recording on each tick interrupt. */\r
174         vTraceInitTraceData();\r
175         xTickTraceUserEvent = xTraceOpenLabel( "tick" );\r
176 \r
177         /* Create the queue used to pass messages from the queue send task to the\r
178         queue receive task. */\r
179         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
180 \r
181         /* Give the queue a name for the FreeRTOS+Trace log. */\r
182         vTraceSetQueueName( xQueue, "DemoQ" );\r
183 \r
184         /* Start the two tasks as described in the comments at the top of this\r
185         file. */\r
186         xTaskCreate( prvQueueReceiveTask,                               /* The function that implements the task. */\r
187                                 "Rx",                                                           /* The text name assigned to the task - for debug only as it is not used by the kernel. */\r
188                                 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
189                                 NULL,                                                           /* The parameter passed to the task - not used in this example. */\r
190                                 mainQUEUE_RECEIVE_TASK_PRIORITY,        /* The priority assigned to the task. */\r
191                                 NULL );                                                         /* The task handle is not required, so NULL is passed. */\r
192 \r
193         xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
194 \r
195         /* Create the task that handles the CLI on a UDP port.  The port number\r
196         is set using the configUDP_CLI_PORT_NUMBER setting in FreeRTOSConfig.h. */\r
197         xTaskCreate( vUDPCommandInterpreterTask, "CLI", configMINIMAL_STACK_SIZE, NULL, mainUDP_CLI_TASK_PRIORITY, NULL );\r
198 \r
199         /* Register commands with the FreeRTOS+CLI command interpreter. */\r
200         vRegisterCLICommands();\r
201 \r
202         /* Start the tasks and timer running. */\r
203         vTaskStartScheduler();\r
204 \r
205         /* If all is well, the scheduler will now be running, and the following\r
206         line will never be reached.  If the following line does execute, then\r
207         there was insufficient FreeRTOS heap memory available for the idle and/or\r
208         timer tasks     to be created.  See the memory management section on the\r
209         FreeRTOS web site for more details (this is standard text that is not not\r
210         really applicable to the Win32 simulator port). */\r
211         for( ;; )\r
212         {\r
213                 Sleep( ulLongTime_ms );\r
214         }\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \r
218 static void prvQueueSendTask( void *pvParameters )\r
219 {\r
220 TickType_t xNextWakeTime;\r
221 const unsigned long ulValueToSend = 100UL;\r
222 \r
223         /* Remove warning about unused parameters. */\r
224         ( void ) pvParameters;\r
225 \r
226         /* Initialise xNextWakeTime - this only needs to be done once. */\r
227         xNextWakeTime = xTaskGetTickCount();\r
228 \r
229         for( ;; )\r
230         {\r
231                 /* Place this task in the blocked state until it is time to run again.\r
232                 While in the Blocked state this task will not consume any CPU time. */\r
233                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
234 \r
235                 /* Send to the queue - causing the queue receive task to unblock and\r
236                 write a message to the display.  0 is used as the block time so the\r
237                 sending operation will not block - it shouldn't need to block as the\r
238                 queue should always     be empty at this point in the code, and it is an\r
239                 error if it is not. */\r
240                 xQueueSend( xQueue, &ulValueToSend, 0U );\r
241         }\r
242 }\r
243 /*-----------------------------------------------------------*/\r
244 \r
245 static void prvQueueReceiveTask( void *pvParameters )\r
246 {\r
247 unsigned long ulReceivedValue;\r
248 \r
249         /* Remove warning about unused parameters. */\r
250         ( void ) pvParameters;\r
251 \r
252         for( ;; )\r
253         {\r
254                 /* Wait until something arrives in the queue - this task will block\r
255                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
256                 FreeRTOSConfig.h. */\r
257                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
258 \r
259                 /*  To get here something must have been received from the queue, but\r
260                 is it the expected value?  If it is, write the message to the\r
261                 display before looping back to block on the queue again. */\r
262                 if( ulReceivedValue == 100UL )\r
263                 {\r
264                         printf( "Message received!\r\n" );\r
265                         ulReceivedValue = 0U;\r
266                 }\r
267         }\r
268 }\r
269 /*-----------------------------------------------------------*/\r
270 \r
271 void vApplicationIdleHook( void )\r
272 {\r
273 const unsigned long ulMSToSleep = 5;\r
274 \r
275         /* This function is called on each cycle of the idle task if\r
276         configUSE_IDLE_HOOK is set to 1 in FreeRTOSConfig.h.  Sleep to reduce CPU\r
277         load. */\r
278         Sleep( ulMSToSleep );\r
279 }\r
280 /*-----------------------------------------------------------*/\r
281 \r
282 void vAssertCalled( void )\r
283 {\r
284 const unsigned long ulLongSleep = 1000UL;\r
285 \r
286         taskDISABLE_INTERRUPTS();\r
287         for( ;; )\r
288         {\r
289                 Sleep( ulLongSleep );\r
290         }\r
291 }\r
292 /*-----------------------------------------------------------*/\r
293 \r
294 void vApplicationTickHook( void )\r
295 {\r
296         /* Write a user event to the trace log.\r
297         Note tick events will not appear in the trace recording with regular period\r
298         because this project runs in a Windows simulator, and does not therefore\r
299         exhibit deterministic behaviour. */\r
300         vTraceUserEvent( xTickTraceUserEvent );\r
301 }\r
302 \r