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