]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_RZ_R7S72100_IAR_DS-5/Source/Full-Demo/UARTCommandConsole.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS / Demo / CORTEX_A9_RZ_R7S72100_IAR_DS-5 / Source / Full-Demo / UARTCommandConsole.c
1 /*\r
2     FreeRTOS V8.1.2 - Copyright (C) 2014 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     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS 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 #include "stdint.h"\r
70 \r
71 /* FreeRTOS includes. */\r
72 #include "FreeRTOS.h"\r
73 #include "task.h"\r
74 #include "semphr.h"\r
75 \r
76 /* Common demo includes. */\r
77 #include "serial.h"\r
78 \r
79 /* Example includes. */\r
80 #include "FreeRTOS_CLI.h"\r
81 #include "UARTCommandConsole.h"\r
82 \r
83 /* Dimensions the buffer into which input characters are placed. */\r
84 #define cmdMAX_INPUT_SIZE               50\r
85 \r
86 /* The maximum time in ticks to wait for the UART access mutex. */\r
87 #define cmdMAX_MUTEX_WAIT               ( 200 / portTICK_PERIOD_MS )\r
88 \r
89 /*-----------------------------------------------------------*/\r
90 \r
91 /*\r
92  * The task that implements the command console processing.\r
93  */\r
94 static void prvUARTCommandConsoleTask( void *pvParameters );\r
95 \r
96 /*-----------------------------------------------------------*/\r
97 \r
98 /* Const messages output by the command console. */\r
99 static const char * const pcWelcomeMessage = "FreeRTOS command server.\r\nType Help to view a list of registered commands.\r\n\r\n>";\r
100 static const char * const pcEndOfOutputMessage = "\r\n[Press ENTER to execute the previous command again]\r\n>";\r
101 static const char * const pcNewLine = "\r\n";\r
102 \r
103 /*-----------------------------------------------------------*/\r
104 \r
105 void vUARTCommandConsoleStart( uint16_t usStackSize, unsigned portBASE_TYPE uxPriority )\r
106 {\r
107         /* Create that task that handles the console itself. */\r
108         xTaskCreate(    prvUARTCommandConsoleTask,                      /* The task that implements the command console. */\r
109                                         "CLI",                                                          /* Text name assigned to the task.  This is just to assist debugging.  The kernel does not use this name itself. */\r
110                                         usStackSize,                                            /* The size of the stack allocated to the task. */\r
111                                         NULL,                                                           /* The parameter is not used, so NULL is passed. */\r
112                                         uxPriority,                                                     /* The priority allocated to the task. */\r
113                                         NULL );                                                         /* A handle is not required, so just pass NULL. */\r
114 }\r
115 /*-----------------------------------------------------------*/\r
116 \r
117 static void prvUARTCommandConsoleTask( void *pvParameters )\r
118 {\r
119 char cRxedChar, cInputIndex = 0, *pcOutputString;\r
120 static char cInputString[ cmdMAX_INPUT_SIZE ], cLastInputString[ cmdMAX_INPUT_SIZE ];\r
121 portBASE_TYPE xReturned;\r
122 \r
123         ( void ) pvParameters;\r
124 \r
125         /* Obtain the address of the output buffer.  Note there is no mutual\r
126         exclusion on this buffer as it is assumed only one command console\r
127         interface will be used at any one time. */\r
128         pcOutputString = FreeRTOS_CLIGetOutputBuffer();\r
129 \r
130         /* Send the welcome message. */\r
131         vSerialPutString( NULL, ( const signed char * ) pcWelcomeMessage, strlen( ( char * ) pcWelcomeMessage ) );\r
132 \r
133         for( ;; )\r
134         {\r
135                 /* Only interested in reading one character at a time. */\r
136                 while( xSerialGetChar( NULL, ( signed char * ) &cRxedChar, portMAX_DELAY ) == pdFALSE );\r
137 \r
138                 /* Echo the character back. */\r
139                 xSerialPutChar( NULL, cRxedChar, portMAX_DELAY );\r
140 \r
141                 /* Was it the end of the line? */\r
142                 if( cRxedChar == '\n' || cRxedChar == '\r' )\r
143                 {\r
144                         /* Just to space the output from the input. */\r
145                         vSerialPutString( NULL, ( const signed char * ) pcNewLine, strlen( ( char * ) pcNewLine ) );\r
146 \r
147                         /* See if the command is empty, indicating that the last command is\r
148                         to be executed again. */\r
149                         if( cInputIndex == 0 )\r
150                         {\r
151                                 /* Copy the last command back into the input string. */\r
152                                 strcpy( ( char * ) cInputString, ( char * ) cLastInputString );\r
153                         }\r
154 \r
155                         /* Pass the received command to the command interpreter.  The\r
156                         command interpreter is called repeatedly until it returns pdFALSE\r
157                         (indicating there is no more output) as it might generate more than\r
158                         one string. */\r
159                         do\r
160                         {\r
161                                 /* Get the next output string from the command interpreter. */\r
162                                 xReturned = FreeRTOS_CLIProcessCommand( cInputString, pcOutputString, configCOMMAND_INT_MAX_OUTPUT_SIZE );\r
163 \r
164                                 /* Write the generated string to the UART. */\r
165                                 vSerialPutString( NULL, ( const signed char * ) pcOutputString, strlen( ( char * ) pcOutputString ) );\r
166 \r
167                         } while( xReturned != pdFALSE );\r
168 \r
169                         /* All the strings generated by the input command have been sent.\r
170                         Clear the input string ready to receive the next command.  Remember\r
171                         the command that was just processed first in case it is to be\r
172                         processed again. */\r
173                         strcpy( ( char * ) cLastInputString, ( char * ) cInputString );\r
174                         cInputIndex = 0;\r
175                         memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );\r
176                         vSerialPutString( NULL, ( const signed char * ) pcEndOfOutputMessage, strlen( ( char * ) pcEndOfOutputMessage ) );\r
177                 }\r
178                 else\r
179                 {\r
180                         if( cRxedChar == '\r' )\r
181                         {\r
182                                 /* Ignore the character. */\r
183                         }\r
184                         else if( cRxedChar == '\b' )\r
185                         {\r
186                                 /* Backspace was pressed.  Erase the last character in the\r
187                                 string - if any. */\r
188                                 if( cInputIndex > 0 )\r
189                                 {\r
190                                         cInputIndex--;\r
191                                         cInputString[ cInputIndex ] = '\0';\r
192                                 }\r
193                         }\r
194                         else\r
195                         {\r
196                                 /* A character was entered.  Add it to the string\r
197                                 entered so far.  When a \n is entered the complete\r
198                                 string will be passed to the command interpreter. */\r
199                                 if( ( cRxedChar >= ' ' ) && ( cRxedChar <= '~' ) )\r
200                                 {\r
201                                         if( cInputIndex < cmdMAX_INPUT_SIZE )\r
202                                         {\r
203                                                 cInputString[ cInputIndex ] = cRxedChar;\r
204                                                 cInputIndex++;\r
205                                         }\r
206                                 }\r
207                         }\r
208                 }\r
209         }\r
210 }\r
211 /*-----------------------------------------------------------*/\r
212 \r