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