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