1 /* Copyright (C) 2013 Czech Technical University in Prague
4 * - Carlos Jenkins <carlos@jenkins.co.cr>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * RPP test suite command processor.
28 #include <ctype.h> // isprint()
29 #include <string.h> // strncmp()
35 /// Command processor task -----------------------------------------------------
38 static char in_buffer[BUF_SIZE];
39 static const char* prompt = "--> ";
40 static const char* newline = "\r\n";
44 * Compare user string and execute command.
46 void execute_cmd(char* string)
48 if( strncmp(string, "ain" , BUF_SIZE) == 0) {
50 } else if(strncmp(string, "aout", BUF_SIZE) == 0) {
52 } else if(strncmp(string, "can" , BUF_SIZE) == 0) {
54 } else if(strncmp(string, "din" , BUF_SIZE) == 0) {
56 } else if(strncmp(string, "eth" , BUF_SIZE) == 0) {
58 } else if(strncmp(string, "fr" , BUF_SIZE) == 0) {
60 } else if(strncmp(string, "hbr" , BUF_SIZE) == 0) {
62 } else if(strncmp(string, "hout", BUF_SIZE) == 0) {
64 } else if(strncmp(string, "lin" , BUF_SIZE) == 0) {
66 } else if(strncmp(string, "lout", BUF_SIZE) == 0) {
68 } else if(strncmp(string, "mout", BUF_SIZE) == 0) {
70 } else if(strncmp(string, "sci" , BUF_SIZE) == 0) {
72 } else if(strncmp(string, "sdc" , BUF_SIZE) == 0) {
74 } else if(strncmp(string, "sdr" , BUF_SIZE) == 0) {
76 } else if(strncmp(string, "help", BUF_SIZE) == 0) {
77 rpp_sci_printf((const char*)"Available commands:\r\n");
78 rpp_sci_printf((const char*)"\thelp - Display this help.\r\n");
79 rpp_sci_printf((const char*)"\tain - Test Analog Input.\r\n");
80 rpp_sci_printf((const char*)"\taout - Test Analog Output.\r\n");
81 rpp_sci_printf((const char*)"\tcan - Test CAN communication.\r\n");
82 rpp_sci_printf((const char*)"\tdin - Test Digital Inputs.\r\n");
83 rpp_sci_printf((const char*)"\teth - Test Ethernet communication.\r\n");
84 rpp_sci_printf((const char*)"\tfr - Test Frame Relay communication.\r\n");
85 rpp_sci_printf((const char*)"\thbr - Test H-Bridge.\r\n");
86 rpp_sci_printf((const char*)"\thout - Test High Power Output.\r\n");
87 rpp_sci_printf((const char*)"\tlin - Test LIN communication.\r\n");
88 rpp_sci_printf((const char*)"\tlout - Test Digital Outputs.\r\n");
89 rpp_sci_printf((const char*)"\tmout - Test Power Outputs.\r\n");
90 rpp_sci_printf((const char*)"\tsci - Test Serial Communication Interface.\r\n");
91 rpp_sci_printf((const char*)"\tsdc - Test SD-Card.\r\n");
92 rpp_sci_printf((const char*)"\tsdr - Test SD-RAM.\r\n");
94 // Unknown command, print buffer back
95 rpp_sci_printf((const char*)
96 "ERROR: Unknown command \"%s\"\r\n", string
103 * Test suite command processor.
105 void test_cmdproc(void* par)
108 rpp_sci_printf((const char*)
109 "RPP Library Test Suite.\r\n"
111 rpp_sci_printf((const char*)
112 "===========================================================\r\n"
114 rpp_sci_printf((const char*)
115 "[Type a module to test or 'help']\r\n"
118 rpp_sci_printf((const char*)"%s", prompt);
121 uint8_t buff_index = 0;
122 boolean_t flush = FALSE;
125 // Get one character from the user
126 if(rpp_sci_read_nb(1, &input) != SUCCESS) {
127 vTaskDelay(INPUT_IDLE / portTICK_RATE_MS);
131 // Backspace and Delete
132 if(input == 8 || input == 127) {
141 // Line feed or Carriage return
142 } else if(input == 10 || input == 13) {
147 // If is any printable character
148 } else if(isprint(input)) {
150 // Store character and increment buffer index
151 in_buffer[buff_index] = input;
155 // Check if buffer is full and force flush
156 if(buff_index == BUF_SIZE - 1) {
160 // All other character are ignored
167 in_buffer[buff_index] = '\0';
170 if(buff_index != 0) {
171 execute_cmd((char*)&in_buffer);
173 rpp_sci_printf((const char*)"%s", newline);
174 rpp_sci_printf((const char*)"%s", prompt);
186 /// Utilities ------------------------------------------------------------------
188 * Wait for user input "q" to quit the loop.
192 while(rpp_sci_getc() < 0) {
193 vTaskDelay(INPUT_IDLE / portTICK_RATE_MS);
199 * Infinite busy loop.
201 * This function is called after an error has being detected to "stop"
204 void busy_infinite_loop()
213 /// Main functions -------------------------------------------------------------
215 * Application main function
217 * This function is called after startup.
221 // Initialize library
225 rpp_sci_setup(115200);
228 portBASE_TYPE task_created =
229 xTaskCreate(test_cmdproc,
230 (const signed char*)"test_cmdproc",
231 1024, NULL, TEST_TASK_PRIORITY, NULL
234 if(task_created != pdPASS) {
235 rpp_sci_printf((const char*)
236 "ERROR: Problem allocating memory for command processor to "
237 "start. Error code: %d\r\n", (uint32_t)task_created
239 busy_infinite_loop();
243 vTaskStartScheduler();
245 // Catch memory problems
246 rpp_sci_printf((const char*)
247 "ERROR: Problem allocating memory for scheduler to start.\r\n"
249 busy_infinite_loop();
256 * FreeRTOS malloc() failed hook.
258 void vApplicationMallocFailedHook(void)
260 rpp_sci_printf((const char*)
261 "ERROR: Manual memory allocation failed.\r\n"
267 * FreeRTOS stack overflow hook.
269 void vApplicationStackOverflowHook(xTaskHandle xTask,
270 signed portCHAR *pcTaskName)
272 rpp_sci_printf((const char*)
273 "ERROR: Stack overflow : \"%s\".\r\n", pcTaskName