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