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