]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_SmartFusion2_M2S050_SoftConsole/RTOSDemo/Full-Demo/UARTCommandConsole.c
ac80734751a3de6f6c702b1f2ea7485cfe57838a
[freertos] / FreeRTOS / Demo / CORTEX_SmartFusion2_M2S050_SoftConsole / RTOSDemo / Full-Demo / UARTCommandConsole.c
1 /*\r
2     FreeRTOS V8.2.1 - Copyright (C) 2015 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 /* Standard includes. */\r
71 #include "string.h"\r
72 #include "stdio.h"\r
73 \r
74 /* FreeRTOS includes. */\r
75 #include "FreeRTOS.h"\r
76 #include "task.h"\r
77 #include "queue.h"\r
78 \r
79 /* Driver includes. */\r
80 #include "drivers/mss_uart/mss_uart.h"\r
81 \r
82 /* Example includes. */\r
83 #include "FreeRTOS_CLI.h"\r
84 #include "UARTCommandConsole.h"\r
85 \r
86 /* Dimensions the buffer into which input characters are placed. */\r
87 #define cmdMAX_INPUT_SIZE               50\r
88 \r
89 /* The maximum time in ticks to wait for the UART access mutex. */\r
90 #define cmdMAX_MUTEX_WAIT               ( 200 / portTICK_PERIOD_MS )\r
91 \r
92 /* Characters are only ever received slowly on the CLI so it is ok to pass\r
93 received characters from the UART interrupt to the task on a queue.  This sets\r
94 the length of the queue used for that purpose. */\r
95 #define cmdRXED_CHARS_QUEUE_LENGTH                      ( 10 )\r
96 \r
97 /*-----------------------------------------------------------*/\r
98 \r
99 /*\r
100  * The task that implements the command console processing.\r
101  */\r
102 static void prvUARTCommandConsoleTask( void *pvParameters );\r
103 \r
104 /*\r
105  * Ensure a previous interrupt driven Tx has completed before sending the next\r
106  * data block to the UART.\r
107  */\r
108 static void prvSendBuffer( const char * pcBuffer, size_t xBufferLength );\r
109 \r
110 /*\r
111  * A UART is used for printf() output and CLI input and output.  Configure the\r
112  * UART and register prvUARTRxNotificationHandler() to handle UART Rx events.\r
113  */\r
114 static void prvConfigureUART( void );\r
115 static void prvUARTRxNotificationHandler( mss_uart_instance_t * this_uart );\r
116 \r
117 /*-----------------------------------------------------------*/\r
118 \r
119 /* Const messages output by the command console. */\r
120 static const char * const pcWelcomeMessage = "\r\n\r\nFreeRTOS command server.\r\nType Help to view a list of registered commands.\r\n\r\n>";\r
121 static const char * const pcEndOfOutputMessage = "\r\n[Press ENTER to execute the previous command again]\r\n>";\r
122 static const char * const pcNewLine = "\r\n";\r
123 \r
124 /* The UART used by the CLI. */\r
125 #if configBUILD_FOR_DEVELOPMENT_KIT == 1\r
126         static const mss_uart_instance_t * const pxUART = &g_mss_uart1;\r
127         static const IRQn_Type xUART_IRQ = UART1_IRQn;\r
128 #else\r
129         static const mss_uart_instance_t * const pxUART = &g_mss_uart0;\r
130         static const IRQn_Type xUART_IRQ = UART0_IRQn;\r
131 #endif\r
132 \r
133 /* Because characters are received slowly (at the speed somebody can type) then\r
134 it is ok to pass received characters from the Rx interrupt to the task on a\r
135 queue.  This is the queue used for that purpose. */\r
136 static QueueHandle_t xRxedChars = NULL;\r
137 \r
138 /*-----------------------------------------------------------*/\r
139 \r
140 void vUARTCommandConsoleStart( uint16_t usStackSize, unsigned portBASE_TYPE uxPriority )\r
141 {\r
142         /* A UART is used for printf() output and CLI input and output.  Note there\r
143         is no mutual exclusion on the UART, but the demo as it stands does not\r
144         require mutual exclusion. */\r
145         prvConfigureUART();\r
146 \r
147         /* Create that task that handles the console itself. */\r
148         xTaskCreate(    prvUARTCommandConsoleTask,      /* The task that implements the command console. */\r
149                                         "CLI",                                          /* Text name assigned to the task.  This is just to assist debugging.  The kernel does not use this name itself. */\r
150                                         usStackSize,                            /* The size of the stack allocated to the task. */\r
151                                         NULL,                                           /* The parameter is not used, so NULL is passed. */\r
152                                         uxPriority,                                     /* The priority allocated to the task. */\r
153                                         NULL );                                         /* A handle is not required, so just pass NULL. */\r
154 }\r
155 /*-----------------------------------------------------------*/\r
156 \r
157 static void prvUARTCommandConsoleTask( void *pvParameters )\r
158 {\r
159 char cRxedChar, *pcOutputString;\r
160 unsigned char cInputIndex = 0;\r
161 static char cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];\r
162 portBASE_TYPE xReturned;\r
163 \r
164         ( void ) pvParameters;\r
165 \r
166         /* Obtain the address of the output buffer.  Note there is no mutual\r
167         exclusion on this buffer as it is assumed only one command console\r
168         interface will be used at any one time. */\r
169         pcOutputString = FreeRTOS_CLIGetOutputBuffer();\r
170 \r
171         /* Send the welcome message. */\r
172         prvSendBuffer( pcWelcomeMessage, strlen( pcWelcomeMessage ) );\r
173 \r
174         for( ;; )\r
175         {\r
176                 /* Wait for the next character to arrive. */\r
177                 if( xQueueReceive( xRxedChars, &cRxedChar, portMAX_DELAY ) == pdPASS )\r
178                 {\r
179                         /* Echo the character back. */\r
180                         prvSendBuffer( &cRxedChar, sizeof( cRxedChar ) );\r
181 \r
182                         /* Was it the end of the line? */\r
183                         if( cRxedChar == '\n' || cRxedChar == '\r' )\r
184                         {\r
185                                 /* Just to space the output from the input. */\r
186                                 prvSendBuffer( pcNewLine, strlen( pcNewLine ) );\r
187 \r
188                                 /* See if the command is empty, indicating that the last command is\r
189                                 to be executed again. */\r
190                                 if( cInputIndex == 0 )\r
191                                 {\r
192                                         /* Copy the last command back into the input string. */\r
193                                         strcpy( cInputString, cLastInputString );\r
194                                 }\r
195 \r
196                                 /* Pass the received command to the command interpreter.  The\r
197                                 command interpreter is called repeatedly until it returns pdFALSE\r
198                                 (indicating there is no more output) as it might generate more than\r
199                                 one string. */\r
200                                 do\r
201                                 {\r
202                                         /* Get the next output string from the command interpreter. */\r
203                                         xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );\r
204 \r
205                                         /* Write the generated string to the UART. */\r
206                                         prvSendBuffer( pcOutputString, strlen( pcOutputString ) );\r
207 \r
208                                 } while( xReturned != pdFALSE );\r
209 \r
210                                 /* All the strings generated by the input command have been sent.\r
211                                 Clear the input string ready to receive the next command.  Remember\r
212                                 the command that was just processed first in case it is to be\r
213                                 processed again. */\r
214                                 strcpy( cLastInputString, cInputString );\r
215                                 cInputIndex = 0;\r
216                                 memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
217 \r
218                                 prvSendBuffer( pcEndOfOutputMessage, strlen( pcEndOfOutputMessage ) );\r
219                         }\r
220                         else\r
221                         {\r
222                                 if( cRxedChar == '\r' )\r
223                                 {\r
224                                         /* Ignore the character. */\r
225                                 }\r
226                                 else if( cRxedChar == '\b' )\r
227                                 {\r
228                                         /* Backspace was pressed.  Erase the last character in the\r
229                                         string - if any. */\r
230                                         if( cInputIndex > 0 )\r
231                                         {\r
232                                                 cInputIndex--;\r
233                                                 cInputString[ cInputIndex ] = '\0';\r
234                                         }\r
235                                 }\r
236                                 else\r
237                                 {\r
238                                         /* A character was entered.  Add it to the string\r
239                                         entered so far.  When a \n is entered the complete\r
240                                         string will be passed to the command interpreter. */\r
241                                         if( ( cRxedChar >= ' ' ) && ( cRxedChar <= '~' ) )\r
242                                         {\r
243                                                 if( cInputIndex < cmdMAX_INPUT_SIZE )\r
244                                                 {\r
245                                                         cInputString[ cInputIndex ] = cRxedChar;\r
246                                                         cInputIndex++;\r
247                                                 }\r
248                                         }\r
249                                 }\r
250                         }\r
251                 }\r
252         }\r
253 }\r
254 /*-----------------------------------------------------------*/\r
255 \r
256 static void prvSendBuffer( const char * pcBuffer, size_t xBufferLength )\r
257 {\r
258 const TickType_t xVeryShortDelay = 2UL;\r
259 \r
260         if( xBufferLength > 0 )\r
261         {\r
262                 MSS_UART_irq_tx( ( mss_uart_instance_t * ) pxUART, ( uint8_t * ) pcBuffer, xBufferLength );\r
263 \r
264                 /* Ensure any previous transmissions have completed.  The default UART\r
265                 interrupt does not provide an event based method of     signally the end of a Tx\r
266                 - this is therefore a crude poll of the Tx end status.  Replacing the\r
267                 default UART handler with one that 'gives' a semaphore when the Tx is\r
268                 complete would allow this poll loop to be replaced by a simple semaphore\r
269                 block. */\r
270                 while( MSS_UART_tx_complete( ( mss_uart_instance_t * ) pxUART ) == pdFALSE )\r
271                 {\r
272                         vTaskDelay( xVeryShortDelay );\r
273                 }\r
274         }\r
275 }\r
276 /*-----------------------------------------------------------*/\r
277 \r
278 static void prvConfigureUART( void )\r
279 {\r
280         /* Initialise the UART which is used for printf() and CLI IO. */\r
281         MSS_UART_init( ( mss_uart_instance_t * ) pxUART, MSS_UART_115200_BAUD, MSS_UART_DATA_8_BITS | MSS_UART_NO_PARITY | MSS_UART_ONE_STOP_BIT );\r
282 \r
283         /* Characters are only ever received slowly on the CLI so it is ok to pass\r
284         received characters from the UART interrupt to the task on a queue.  Create\r
285         the queue used for that purpose. */\r
286         xRxedChars = xQueueCreate( cmdRXED_CHARS_QUEUE_LENGTH, sizeof( char ) );\r
287 \r
288         /* The interrupt handler makes use of FreeRTOS API functions, so its\r
289         priority must be at or below the configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY\r
290         setting (the higher the numeric priority, the lower the logical priority). */\r
291         NVIC_SetPriority( xUART_IRQ, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );\r
292 \r
293         /* Set the UART Rx notification function. */\r
294         MSS_UART_set_rx_handler( ( mss_uart_instance_t * ) pxUART, prvUARTRxNotificationHandler, MSS_UART_FIFO_SINGLE_BYTE );\r
295 }\r
296 /*-----------------------------------------------------------*/\r
297 \r
298 static void prvUARTRxNotificationHandler( mss_uart_instance_t * pxUART )\r
299 {\r
300 char cRxed;\r
301 portBASE_TYPE xHigherPriorityTaskWoken;\r
302 \r
303         /* The command console receives data very slowly (at the speed of somebody\r
304         typing), therefore it is ok to just handle one character at a time and use\r
305         a queue to send the characters to the task. */\r
306         if( MSS_UART_get_rx( pxUART, ( uint8_t * ) &cRxed, sizeof( cRxed ) ) == sizeof( cRxed ) )\r
307         {\r
308                 xHigherPriorityTaskWoken = pdFALSE;\r
309                 xQueueSendFromISR( xRxedChars, &cRxed, &xHigherPriorityTaskWoken );\r
310 \r
311                 /* portEND_SWITCHING_ISR() or portYIELD_FROM_ISR() can be used here. */\r
312                 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
313         }\r
314 }\r
315 \r