source: test2.git/Command.c

Last change on this file was b6aa0ba, checked in by andib <andi.b@…>, 2 years ago

test with more files

  • Property mode set to 100644
File size: 13.1 KB
Line 
1/*******************************************************************************
2MODULE : Commands.c
3 Processes commands from logging interface
4
5 MODULE IS ADAPTED FROM ANOTHER VERY OLD PROJECT
6 So some code may look really ugly. Even to my eyes ;-)
7
819xxxxxx AB Initial
920191219 AB Slightly updated for JADE EFM32JG1B test
10*******************************************************************************/
11
12// --- Includes ---------------------------------------------------------------
13#define LOGGING_TOKEN COMMAND_LOGGING
14#include "TraceLeUart.h"
15
16#include <efm32.h>
17#include <em_gpio.h>
18
19#include <stdio.h>
20#include <string.h>
21#include <stdio.h>
22#include <ctype.h>
23#include <stdlib.h>
24
25#include "types.h"
26
27#include "Command.h"
28#include "TraceLeUart.h"
29//#include "Nvm.h"
30//#include "Iic.h"
31#include "Adc.h"
32//#include "ComAnet.h"
33//#include "MasterAnet.h"
34//#include "SlaveAnet.h"
35#include "Leds.h"
36#include "Hardware.h"
37
38// --- Pin Definitions --------------------------------------------------------
39
40// --- Defines ----------------------------------------------------------------
41#define MAX_CHAR_COMPARE 3 /* max. number of characters valid for command compare */
42#define MAX_ARGS 5 /* max. number of arguments to be processed */
43#define MAX_COMMAND_LENGTH 15 /* max. length of command */
44#define MAX_LINE_LENGTH 50 /* max. length of on command line */
45#define MAX_ARG_LENGTH 6 /* max. length of one argument */
46#define BUFFER_SIZE 100 // max. size of line including command and args...
47
48/* Commands are defined as array of strings and as consecutive numbers for easier handling in command.c */
49#define DUMMY 0
50#define VERSION 1
51#define UPTIME 2
52#define ADCS 3
53#define HELP 4
54#define LIST_EEPROM 5
55#define CLEAR_EEPROM 6
56#define NVMSTATUS 7
57#define LOGGING 8
58#define STATUS 9
59#define ERRORS 10
60#define IRQS 11
61#define LED 12
62#define COVOLT 13
63#define RELAIS 14
64#define BOOT 15
65#define HWLOGGING 16
66#define NEW_LINE 17
67// have to be 2 items more than #defines above
68#ifndef NO_TRACE
69 // #commands length
70const char saCommands[20] [33] = {
71"DUMMy",
72"VERSion",
73"UPTIme",
74"ADCS [1|2|3|4]",
75"HELP",
76"LIST_eeprom",
77"CLEAR_eeprom (attention !!!)",
78"NVMStatus",
79"LOGGing",
80"STATus",
81"ERROrs",
82"IRQS",
83"LED num [0|1] (num=0|2)",
84"COVoltage correct. LINE in mV",
85"RELais 1|2|? toggle relais",
86"BOOT sw reset to booter",
87"HWLogging - toggle on/off",
88"\n",
89 "\0"};
90
91// --- Typedefs ---------------------------------------------------------------
92
93// --- PUBLIC Variables -------------------------------------------------------
94
95static BYTE bNewCommands; // number of commands waiting to be processed
96// incremented in uart receiver IRQ routine
97
98// --- Variables --------------------------------------------------------------
99
100static BYTE bArgc ; // number of arguments
101// static BYTE bLogLevel ; // level of logging messages
102
103static char sCommand[MAX_COMMAND_LENGTH + 1] ; // input command buffer
104static char sArg[MAX_ARGS][MAX_ARG_LENGTH + 1] ; // buffer for command arguments
105static char sTmpBuffer[BUFFER_SIZE] ; // temporary buffer
106
107// --- Macros -----------------------------------------------------------------
108
109#endif // NO_TRACE
110
111// --- Functionprototypes -----------------------------------------------------
112extern int StackCheck (void);
113
114void CommandProcessing(void);
115BYTE CommandCompare (void);
116
117// --- Code -------------------------------------------------------------------
118
119void CommandInit(void)
120 {
121#ifndef NO_TRACE
122 bNewCommands = 0;
123#endif // NO_TRACE
124 }
125
126/******************************************************************************
127void CommandProcessing (void)
128
129 Called whenever a new command arrived in input buffer. Or from main loop
130 every 200ms. Decodes commands an processes arguments. Notification about new
131 command via global variable. Input buffer read with direct fetch from uart.
132******************************************************************************/
133void CommandProcessing(void)
134{
135#ifndef NO_TRACE
136 if ( !TraceCommandAvail() ) return; // check if commands are ready
137
138 BYTE bChar, bTmp;
139 BYTE bCount = 0;
140// BYTE j, i;
141// char cBuf[81];
142// char cBuf1[10];
143// int iT0, iT1, iT2, iT3, iT4, iT5, iT6;//, iT7;
144 int iTemp;
145
146 do
147 {
148 if ( ! (bChar = TraceGetKey() ) ) // read byte from UART input buffer
149 {
150 if ( !bCount )
151 { /* if no characters are processed till now then error, else normal abortion of while loop */
152 TRACE_L2("CMD: Error - TraceGetKey returns 0 (false triggered new command?)");
153 TRACE_L2("CMD: bNewCommands: %d", bNewCommands);
154 }
155 break; /* error on getkey breaks */
156 }
157 if ( bChar == '\b' && bCount ) bCount--; /* Backspace processing */
158 else
159 {
160 if ( bChar != '\b' && bCount < (BUFFER_SIZE-1) )
161 {
162 if ( bChar != '\n' && bChar != '\r' )
163 { /* don't process CR or LF */
164 //sTmpBuffer[bCount] = toupper(bChar); // 20090116 AB make case sensitive (command is still insensitive, see command compare)
165 sTmpBuffer[bCount] = bChar;
166 bCount++;
167 }
168 }
169 }
170 if ( bChar != '\n' && bChar != '\r' ) TRACE_L2("Key '%c'", bChar);
171 }
172 while ( (( (bChar != '\r') && (bChar != '\n') ) && (bCount < (BUFFER_SIZE-1)) ) || (bChar == '\b') ); /* (the BUFFER_SIZE-1 character overflows bCount and breaks while loop)
173 if BUFFER_SIZE overflows then break while loop */
174 if ( bCount == (BUFFER_SIZE-1) ) /* sTmpBuffer overflow - line to long, read till no character or line end (\n or \r) */
175 {
176 while ( (bChar = TraceGetKey()) )
177 { /* characters available to read */
178 if ( bChar == '\n' || bChar == '\r' ) break; /* Read until end of command to flush too long commands */
179 }
180 }
181
182 if ( bCount > 0 ) sTmpBuffer[bCount] = '\0';
183 else
184 {
185 sTmpBuffer[0]= '\0';
186 }
187
188 TraceCommandAvailDec(); /* one command read from input buffer */
189
190 if ( !bCount )
191 {
192 TRACE_L2("CMD: Error - input stream to long or no input (only <CR> or <LF>");
193 return; /* input stream to long, bCount overflow */
194 }
195
196 TRACE_L2("%s\n", sTmpBuffer); /* debugging print complete Command Line */
197
198 if ( bCount < MAX_LINE_LENGTH ) bChar = CommandCompare (); /* only commands shorter than MAX_LINE_LENGTH characters are allowed */
199 else
200 {
201 TRACE_L2("\nCMD: Error - command line to long\n");
202 return;
203 }
204
205 bCount = 0; /* increment bCount whenever one argument was processed */
206
207 TRACE_L2("Command compare gives command %d", (int) bChar);
208
209 switch ( bChar )
210 {
211 case VERSION:
212 TracePrintFirmwareVersion();
213 //TRACE(" ZTM-BD Build: %s %s", TraGetBuildTime(), TraGetBuildDate());
214 // __DEBUG will be set by MPLAB for debug builds
215 #if defined (__DEBUG__) || defined (DEBUG) || defined (__DEBUG)
216 TRACE (" DEBUG");
217 #endif
218 TracePrintHwInfo();
219 //TracePrintKeypadInfo();
220 break;
221
222 case UPTIME:
223 TracePrintUptime();
224 TracePrintStartTime();
225 break;
226
227 case ADCS:
228 bTmp = 1;
229 switch (sArg[0][0])
230 {
231 case '0':
232 case '1':
233 case '2':
234 case '3':
235 case '4':
236 bTmp = atoi(&sArg[0][0]);
237 // fall through
238 default:
239 AdcTrace(bTmp);
240 break;
241 }
242 bCount--;
243 break;
244
245 case HELP:
246 TRACE(" Valid commands -");
247 bTmp = 1;
248 while ( saCommands[bTmp][0] != '\n' )
249 {
250 TRACE(" %s", saCommands[bTmp]);
251 bTmp++;
252 }
253 TRACE(" Only the first %d characters are decisive. Not case sensitive.", (int) MAX_CHAR_COMPARE);
254 break;
255
256/* 20180917 remmed out cause of FLASH space constraints case LIST_EEPROM:
257 j = 0;
258 do
259 {
260 cBuf[0] = '\0';
261 for (i = 0; i < 16; i++, j++)
262 {
263 sprintf(cBuf1, " 0x%02X", NvmGetChar(j));
264 strcat(cBuf, cBuf1);
265 }
266 TRACE("%s", cBuf);
267 }
268 while (j < 128); //EEPROM_SIZE);
269 break;
270
271 case CLEAR_EEPROM:
272 for (i = 0; i < 8; i++) NvmStoreChar(i, 0xFF);
273 TRACE("CMD: cleared first %d bytes in EEPROM", i);
274 break;
275*/
276 case NVMSTATUS:
277 TRACE(" ");
278// NvmTraceStatus();
279 break;
280
281 case LOGGING:
282 /*if (!bArgc)
283 {
284 TRACE("CMD: Logging is %s", bLogLevel ? "ON" : "OFF");
285 }
286 else // At least one Argument
287 {
288 bCount++;
289 if (!strncmp ( (char *) &sArg[0], "OFF", MAX_ARG_LENGTH) ) bLogLevel = FALSE;
290 if (!strncmp ( (char *) &sArg[0], "ON" , MAX_ARG_LENGTH) ) bLogLevel = TRUE;
291 TRACE("CMD: Switch logging to %s", (char *) &sArg[0]);
292 };
293 */break;
294
295 case STATUS:
296 if ( (iTemp = StackCheck()) >= 0 ) // check if stack is initialized to enable stack check
297 {
298 TRACE("CMD: stack - %d bytes free", iTemp );
299 }
300 else
301 {
302 TRACE("CMD: stack - can not be checked");
303 }
304// IicPrintStatus();
305// ComTraceErrors();
306 break;
307
308 case ERRORS:
309 //TRACE("CMD: Errors <- not implemented yet!");
310 break;
311
312 case IRQS:
313 TracePrintIRQs();
314// ComTraceIRQs();
315// MasTraceIRQs();
316// SlvTraceIRQs();
317// IicTraceIRQs();
318 break;
319
320 case LED:
321 if (bArgc)
322 {
323 if (bArgc == 1) LedToggle(sArg[0][0] - '0');
324 else {
325 switch (sArg[1][0])
326 {
327 case '0':
328 LedOff(sArg[0][0] - '0');
329 bCount--;
330 break;
331 case '1':
332 LedOn(sArg[0][0] - '0');
333 bCount--;
334 break;
335 }
336 }
337 bArgc--;
338 }
339 else
340 {
341 TRACE("COM: use f.i. LED 0 1");
342 }
343 break;
344
345 case COVOLT:
346 break;
347
348 case RELAIS:
349 break;
350
351
352 case BOOT:
353 // restart (jump into booter)
354 __disable_irq(); // __asm("CPSID i");
355// GPIO_PinModeSet(gpioPortB, 13, gpioModeInputPull, 0); // STOP_FAST_START
356// GPIO_PinModeSet(gpioPortC, 14, gpioModePushPull, 1); // START_FAST_START
357 _delay_us( 50000 );
358 NVIC_SystemReset();
359 break;
360
361 case NEW_LINE: /* ignore new line */
362 break;
363
364 case HWLOGGING: /* toggle Hardware logging */
365// HwLoggingToggle();
366 break;
367
368 default:
369 if ( strlen(sTmpBuffer) < 47 )
370 {
371 TRACE("CMD: Unknown command '%s'. Try 'HELP'", sTmpBuffer);
372 }
373 else
374 {
375 TRACE("CMD: Unknown command '%47s'. Try 'HELP'", sTmpBuffer);
376 }
377 break;
378 }
379
380 if ( bArgc > bCount ) /* all arguments processed ? */
381 {
382 TRACE("CMD: Arguments not processed -");
383 while ( bCount < bArgc )
384 {
385 TRACE(" '%s'", sArg[bCount]);
386 bCount++;
387 }
388 }
389
390 return;
391#endif // NO_TRACE
392 }
393
394
395
396BYTE CommandCompare ()
397{
398#ifndef NO_TRACE
399 BYTE bChar = 0, bCount = 0, bTmp = 0, k, l;
400 if ( bNewCommands > 20 ) /* if too much commands flush transmit buffer to get more processing time
401 and suppress further echos */
402 {
403 // SNT_FLU ();
404 bNewCommands = 0;
405 TRACE("CMD: Too much commands: %d", bNewCommands);
406 TRACE("CMD: Input buffer flushed\n");
407 }
408
409 if ( (l = strlen (sTmpBuffer)) )
410 {
411 bArgc = 0;
412 while ( sTmpBuffer[bCount] == ' ' && bCount < l && bTmp < MAX_COMMAND_LENGTH ) bCount++; /* remove leading blanks */
413 while ( sTmpBuffer[bCount] != ' ' && bCount < l && bTmp < MAX_COMMAND_LENGTH )
414 {
415 if ( sTmpBuffer[bCount] != '\n' && sTmpBuffer[bCount] != '\r' && sTmpBuffer[bCount] != ' ' )
416 {
417 sCommand[bTmp] = toupper(sTmpBuffer[bCount]); // 20090116 command case insensitive, args case sensitive
418 bTmp++;
419 }
420 bCount++;
421 }
422 sCommand[bTmp] = '\0';
423 TRACE_L2(" CMD: command_compare - Command string %s\n", sCommand);
424
425 for ( k = 0; k < MAX_ARGS; k++ )
426 {
427 while ( sTmpBuffer[bCount] == ' ' && bCount < l ) bCount++; /* remove blanks */
428 bTmp = 0;
429 while ( sTmpBuffer[bCount] != ' ' && bCount < l && bTmp < MAX_ARG_LENGTH )
430 {
431 if ( sTmpBuffer[bCount] != '\n' && sTmpBuffer[bCount] != '\r' && sTmpBuffer[bCount] != ' ' )
432 {
433 sArg[k][bTmp] = toupper(sTmpBuffer[bCount]); // 20101006 args case insensitive too
434 bTmp++;
435 }
436 bCount++;
437 }
438 sArg[k][bTmp] = '\0';
439 TRACE_L2(" CMD: command_compare ARG ");
440 TRACE_L2("%u bArgc: %u %u - %s\n", k, bArgc, bArgc, sArg[k]);
441 if ( bTmp ) bArgc++;
442 if ( bCount >= l ) break; /* end if no more input characters to process */
443 }
444
445 while ( saCommands[bChar][0] != '\0' && bChar < 50 ) // no more than 50 commands allowed
446 {
447 bChar++;
448 TRACE_L2(" CMD: comparing to %s", saCommands[bChar]);
449 if ( !strncmp (saCommands[bChar], (char *)sCommand, MAX_CHAR_COMPARE) ) break;
450 }
451
452 }
453
454 TRACE_L2(" CMD: command %d found ", (int) bChar);
455 return bChar;
456#else
457 return 1;
458#endif // NO_TRACE
459 }
460
461
Note: See TracBrowser for help on using the repository browser.