]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Demo/FreeRTOS_Plus_CLI_with_Trace_Windows_Simulator/main.c
a8e293dce0d199cdc79b19cb63d98fbb11363a45
[freertos] / FreeRTOS-Plus / Demo / FreeRTOS_Plus_CLI_with_Trace_Windows_Simulator / main.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29  ******************************************************************************\r
30  * -NOTE- The Win32 port is a simulation (or is that emulation?) only!  Do not\r
31  * expect to get real time behaviour from the Win32 port or this demo\r
32  * application.  It is provided as a convenient development and demonstration\r
33  * test bed only.  This was tested using Windows XP on a dual core laptop.\r
34  *\r
35  * Windows will not be running the FreeRTOS simulator threads continuously, so\r
36  * the timing information in the FreeRTOS+Trace logs have no meaningful units.\r
37  * See the documentation page for the Windows simulator for an explanation of\r
38  * the slow timing:\r
39  * http://www.freertos.org/FreeRTOS-Windows-Simulator-Emulator-for-Visual-Studio-and-Eclipse-MingW.html\r
40  * - READ THE WEB DOCUMENTATION FOR THIS PORT FOR MORE INFORMATION ON USING IT -\r
41  *\r
42  * Documentation for this demo can be found on:\r
43  * http://www.freertos.org/FreeRTOS-Plus/FreeRTOS_Plus_Trace/Free_RTOS_Plus_Trace_CLI_Example.shtml\r
44  ******************************************************************************\r
45  *\r
46  * This is a simple FreeRTOS Windows simulator project that makes it easy to\r
47  * evaluate FreeRTOS+CLI and FreeRTOS+Trace on a standard desktop PC, without\r
48  * any external hardware or interfaces being required.\r
49  *\r
50  * To keep everything as simple as possible, the command line interface is\r
51  * accessed through a UDP socket on the default Windows loopback IP address of\r
52  * 127.0.0.1.  Full instructions are provided on the documentation page\r
53  * referenced above.\r
54  *\r
55  * Commands are provided to both start and stop a FreeRTOS+Trace recording.\r
56  * Stopping a recording will result in the recorded data being saved to the\r
57  * hard disk, ready for viewing in the FreeRTOS+Trace graphical user interface.\r
58  * Again, full instructions are provided on the documentation page referenced\r
59  * above.\r
60  *\r
61  * A queue send task and a queue receive task are defined in this file.  The\r
62  * queue receive task spends most of its time blocked on the queue waiting for\r
63  * messages to arrive.  The queue send task periodically sends a message to the\r
64  * queue, causing the queue receive task to exit the Blocked state.  The\r
65  * priority of the queue receive task is above that of the queue send task, so\r
66  * it pre-empts the queue send task as soon as it leaves the Blocked state.  It\r
67  * then consumes the message from the queue and prints "message received" to\r
68  * the screen before returning to block on the queue once again.  This\r
69  * sequencing is clearly visible in the recorded FreeRTOS+Trace data.\r
70  *\r
71  */\r
72 \r
73 /* Standard includes. */\r
74 #include <stdio.h>\r
75 #include <stdint.h>\r
76 \r
77 /* FreeRTOS includes. */\r
78 #include <FreeRTOS.h>\r
79 #include "task.h"\r
80 #include "queue.h"\r
81 \r
82 /* Priorities at which the tasks are created. */\r
83 #define mainQUEUE_RECEIVE_TASK_PRIORITY         ( tskIDLE_PRIORITY + 2 )\r
84 #define mainQUEUE_SEND_TASK_PRIORITY            ( tskIDLE_PRIORITY + 1 )\r
85 #define mainUDP_CLI_TASK_PRIORITY                       ( tskIDLE_PRIORITY )\r
86 \r
87 /* The rate at which data is sent to the queue.  The (simulated) 250ms value is\r
88 converted to ticks using the portTICK_RATE_MS constant. */\r
89 #define mainQUEUE_SEND_FREQUENCY_MS                     ( 250 / portTICK_RATE_MS )\r
90 \r
91 /* The number of items the queue can hold.  This is 1 as the receive task\r
92 will remove items as they are added, meaning the send task should always find\r
93 the queue empty. */\r
94 #define mainQUEUE_LENGTH                                        ( 1 )\r
95 \r
96 /*-----------------------------------------------------------*/\r
97 \r
98 /*\r
99  * The queue send and receive tasks as described in the comments at the top of\r
100  * this file.\r
101  */\r
102 static void prvQueueReceiveTask( void *pvParameters );\r
103 static void prvQueueSendTask( void *pvParameters );\r
104 \r
105 /*\r
106  * The task that implements the UDP command interpreter using FreeRTOS+CLI.\r
107  */\r
108 extern void vUDPCommandInterpreterTask( void *pvParameters );\r
109 \r
110 /*\r
111  * Register commands that can be used with FreeRTOS+CLI through the UDP socket.\r
112  * The commands are defined in CLI-commands.c.\r
113  */\r
114 extern void vRegisterCLICommands( void );\r
115 \r
116 /* The queue used by both tasks. */\r
117 static xQueueHandle xQueue = NULL;\r
118 \r
119 /*-----------------------------------------------------------*/\r
120 \r
121 int main( void )\r
122 {\r
123 const uint32_t ulLongTime_ms = 250UL;\r
124 \r
125         /* Initialise the trace recorder and create the label used to post user\r
126         events to the trace recording on each tick interrupt. */\r
127         vTraceEnable( TRC_START );\r
128 \r
129         /* Create the queue used to pass messages from the queue send task to the\r
130         queue receive task. */\r
131         xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
132 \r
133         /* Give the queue a name for the FreeRTOS+Trace log. */\r
134         vTraceSetQueueName( xQueue, "DemoQ" );\r
135 \r
136         /* Start the two tasks as described in the comments at the top of this\r
137         file. */\r
138         xTaskCreate( prvQueueReceiveTask,                               /* The function that implements the task. */\r
139                                 "Rx",                                                           /* The text name assigned to the task - for debug only as it is not used by the kernel. */\r
140                                 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
141                                 NULL,                                                           /* The parameter passed to the task - not used in this example. */\r
142                                 mainQUEUE_RECEIVE_TASK_PRIORITY,        /* The priority assigned to the task. */\r
143                                 NULL );                                                         /* The task handle is not required, so NULL is passed. */\r
144 \r
145         xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
146 \r
147         /* Create the task that handles the CLI on a UDP port.  The port number\r
148         is set using the configUDP_CLI_PORT_NUMBER setting in FreeRTOSConfig.h. */\r
149         xTaskCreate( vUDPCommandInterpreterTask, "CLI", configMINIMAL_STACK_SIZE, NULL, mainUDP_CLI_TASK_PRIORITY, NULL );\r
150 \r
151         /* Register commands with the FreeRTOS+CLI command interpreter. */\r
152         vRegisterCLICommands();\r
153 \r
154         /* Start the tasks and timer running. */\r
155         vTaskStartScheduler();\r
156 \r
157         /* If all is well, the scheduler will now be running, and the following\r
158         line will never be reached.  If the following line does execute, then\r
159         there was insufficient FreeRTOS heap memory available for the idle and/or\r
160         timer tasks     to be created.  See the memory management section on the\r
161         FreeRTOS web site for more details (this is standard text that is not\r
162         really applicable to the Win32 simulator port). */\r
163         for( ;; )\r
164         {\r
165                 Sleep( ulLongTime_ms );\r
166         }\r
167 }\r
168 /*-----------------------------------------------------------*/\r
169 \r
170 static void prvQueueSendTask( void *pvParameters )\r
171 {\r
172 TickType_t xNextWakeTime;\r
173 const unsigned long ulValueToSend = 100UL;\r
174 \r
175         /* Remove warning about unused parameters. */\r
176         ( void ) pvParameters;\r
177 \r
178         /* Initialise xNextWakeTime - this only needs to be done once. */\r
179         xNextWakeTime = xTaskGetTickCount();\r
180 \r
181         for( ;; )\r
182         {\r
183                 /* Place this task in the blocked state until it is time to run again.\r
184                 While in the Blocked state this task will not consume any CPU time. */\r
185                 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
186 \r
187                 /* Send to the queue - causing the queue receive task to unblock and\r
188                 write a message to the display.  0 is used as the block time so the\r
189                 sending operation will not block - it shouldn't need to block as the\r
190                 queue should always     be empty at this point in the code, and it is an\r
191                 error if it is not. */\r
192                 xQueueSend( xQueue, &ulValueToSend, 0U );\r
193         }\r
194 }\r
195 /*-----------------------------------------------------------*/\r
196 \r
197 static void prvQueueReceiveTask( void *pvParameters )\r
198 {\r
199 unsigned long ulReceivedValue;\r
200 \r
201         /* Remove warning about unused parameters. */\r
202         ( void ) pvParameters;\r
203 \r
204         for( ;; )\r
205         {\r
206                 /* Wait until something arrives in the queue - this task will block\r
207                 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
208                 FreeRTOSConfig.h. */\r
209                 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
210 \r
211                 /*  To get here something must have been received from the queue, but\r
212                 is it the expected value?  If it is, write the message to the\r
213                 display before looping back to block on the queue again. */\r
214                 if( ulReceivedValue == 100UL )\r
215                 {\r
216                         printf( "Message received!\r\n" );\r
217                         ulReceivedValue = 0U;\r
218                 }\r
219         }\r
220 }\r
221 /*-----------------------------------------------------------*/\r
222 \r
223 void vApplicationIdleHook( void )\r
224 {\r
225 const unsigned long ulMSToSleep = 5;\r
226 \r
227         /* This function is called on each cycle of the idle task if\r
228         configUSE_IDLE_HOOK is set to 1 in FreeRTOSConfig.h.  Sleep to reduce CPU\r
229         load. */\r
230         Sleep( ulMSToSleep );\r
231 }\r
232 /*-----------------------------------------------------------*/\r
233 \r
234 void vAssertCalled( void )\r
235 {\r
236 const unsigned long ulLongSleep = 1000UL;\r
237 \r
238         taskDISABLE_INTERRUPTS();\r
239         for( ;; )\r
240         {\r
241                 Sleep( ulLongSleep );\r
242         }\r
243 }\r
244 /*-----------------------------------------------------------*/\r
245 \r
246 \r