forked from Dzenik/potatobox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdline.c
1131 lines (1016 loc) · 31.9 KB
/
cmdline.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdbool.h>
#include <stdint.h>
#include "driverlib/sysctl.h"
#include "driverlib/systick.h"
#include "driverlib/gpio.h"
#include "driverlib/uart.h"
#include "driverlib/interrupt.h"
#include "driverlib/fpu.h"
#include "driverlib/pin_map.h"
#include "inc/hw_memmap.h"
#include "inc/hw_ints.h"
#include "utils\uartstdio.h"
#include "cmdlib.h"
//*****************************************************************************
// Create instances of control structures
//*****************************************************************************
uart_info_t uartData;
uart_cmd_t uartCmd;
timing_info_t timingData;
stack_info_t stack;
//*****************************************************************************
// Contains previous commands or code to be executed in sequence
//*****************************************************************************
uart_cmd_t uartCmdStack[COMMAND_STACK_DEPTH];
//*****************************************************************************
// This table holds encoded string that are supported by this command line
// interface. MAX_COMMANDS refers to the size of this table.
//*****************************************************************************
uint16_t cmdTable[MAX_COMMANDS] = {0};
void (*cmdFuncTable[MAX_COMMANDS])(void) = {0};
//*****************************************************************************
// Simple Command for initializing hardware and command line scheduler
//*****************************************************************************
void InitCommandLineSystem (void) {
bool retCode, retFunc;
uint8_t componentNum = 0;
//
// Initialize device hardware
//
InitCortexHardware();
UARTprintf("\n\nHardware Initialized.\n");
//
// Initialize process scheduler
//
ProcSchedulerInit ();
UARTprintf("- Process scheduler enabled. \n");
//
// Initialize command line processor, must be
// called before registering components
//
CMDLineProcessInit();
UARTprintf("- Command line interpeter enabled.\n");
//
// Register Components
//
UARTprintf("- Registering components.\n");
retCode = RegisterCommandCode(0, EncodeCommand("exit", 4));
retFunc = RegisterCommandFunc(0, CmdExit);
componentNum++;
if (!retCode || !retFunc) {
UARTprintf("Failed to register component.\n");
componentNum--;
}
retCode = RegisterCommandCode(1, EncodeCommand("gpio", 4));
retFunc = RegisterCommandFunc(1, CmdGpio);
componentNum++;
if (!retCode || !retFunc) {
UARTprintf("Failed to register component.\n");
componentNum--;
}
retCode = RegisterCommandCode(2, EncodeCommand("stat", 4));
retFunc = RegisterCommandFunc(2, CmdStat);
componentNum++;
if (!retCode || !retFunc) {
UARTprintf("Failed to register component.\n");
componentNum--;
}
retCode = RegisterCommandCode(3, EncodeCommand("help", 4));
retFunc = RegisterCommandFunc(3, CmdHelp);
componentNum++;
if (!retCode || !retFunc) {
UARTprintf("Failed to register component.\n");
componentNum--;
}
retCode = RegisterCommandCode(4, EncodeCommand("time", 4));
retFunc = RegisterCommandFunc(4, CmdTime);
componentNum++;
if (!retCode || !retFunc) {
UARTprintf("Failed to register component.\n");
componentNum--;
}
retCode = RegisterCommandCode(5, EncodeCommand("print", 5));
retFunc = RegisterCommandFunc(5, CmdPrint);
componentNum++;
if (!retCode || !retFunc) {
UARTprintf("Failed to register component.\n");
componentNum--;
}
retCode = RegisterCommandCode(6, EncodeCommand("queue", 5));
retFunc = RegisterCommandFunc(6, CmdQueue);
componentNum++;
if (!retCode || !retFunc) {
UARTprintf("Failed to register component.\n");
componentNum--;
}
retCode = RegisterCommandCode(7, EncodeCommand("clear", 5));
retFunc = RegisterCommandFunc(7, CmdClear);
componentNum++;
if (!retCode || !retFunc) {
UARTprintf("Failed to register component.\n");
componentNum--;
}
retCode = RegisterCommandCode(8, EncodeCommand("proc", 4));
retFunc = RegisterCommandFunc(8, CmdProc);
componentNum++;
if (!retCode || !retFunc) {
UARTprintf("Failed to register component.\n");
componentNum--;
}
retCode = RegisterCommandCode(9, EncodeCommand("pool", 4));
retFunc = RegisterCommandFunc(9, CmdPool);
componentNum++;
if (!retCode || !retFunc) {
UARTprintf("Failed to register component.\n");
componentNum--;
}
retCode = RegisterCommandCode(10, EncodeCommand("adc", 3));
retFunc = RegisterCommandFunc(10, CmdAdc);
componentNum++;
if (!retCode || !retFunc) {
UARTprintf("Failed to register component.\n");
componentNum--;
}
retCode = RegisterCommandCode(11, EncodeCommand("jump", 4));
retFunc = RegisterCommandFunc(11, CmdJump);
componentNum++;
if (!retCode || !retFunc) {
UARTprintf("Failed to register component.\n");
componentNum--;
}
retCode = RegisterCommandCode(13, EncodeCommand("script", 6));
retFunc = RegisterCommandFunc(13, CMDScript);
componentNum++;
if (!retCode || !retFunc) {
UARTprintf("Failed to register component.\n");
componentNum--;
}
UARTprintf("- %d Components registered.\n", componentNum);
//
// Print prompt
//
UARTprintf(INPUT_PROMPT);
}
//*****************************************************************************
// This is the main routine of the command line processor, this routine should
// ideally run in the while loop in the main(), but it also can be run in
// a task/thread on a RTOS
//
// This function checks if enter key has been detected by the interrupt
// service routine and if it's true, the processing of the buffer and
// execution of the command will take place
//
//*****************************************************************************
void CMDLineScheduler (void) {
uart_info_t *packet = GetUartPointer();
uart_cmd_t *cmd = GetCmdPointer();
timing_info_t *timing = GetTimerPointer();
stack_info_t *stack = GetStackPointer();
if(packet->enterFlag) {
CalculateUsage(timing);
timing->cmdStart = SysTickValueGet();
cmd->currentCmd = UARTEncodeCommand(packet);
UARTGetArguments(packet, cmd);
if (!FindCommand(cmd)) {
if (packet->rxCount >= MIN_CMD_LENGTH) {
UARTprintf(CMD_NOT_FOUND);
}
}
else {
ExecuteCommand(cmd);
SaveToStack(cmd);
if (stack->execFromStack) {
ExecuteFromStack();
}
}
CMDResetCtrlStructure(cmd);
UARTResetCtrlStructure(packet);
timing->cmdStop = SysTickValueGet();
// display different input prompts based on mode
// during scripting, show current stack position
if (stack->saveCommands) {
UARTprintf(STACK_PROMPT, stack->stackIdx);
} else {
UARTprintf(INPUT_PROMPT);
}
}
}
//*****************************************************************************
// This function Calulates time spent on processing in microseconds
//
//*****************************************************************************
void CalculateUsage (timing_info_t *timing) {
//
// Calculate the number of us elapsed
// but first check if cmdStart is > cmdStop
// if not, systick has experienced an overflow
// correct for this.
//
if (timing->cmdStart > timing->cmdStop) {
timing->elapsedTime = timing->cmdStart - timing->cmdStop;
}
else {
timing->elapsedTime = (( timing->cmdStart +
( SYSTICK_MAX - timing->cmdStop )));
}
// push current value to profile array
// and update counter
timing->elapsedTimeProfile[timing->eTindex] = timing->elapsedTime;
timing->eTindex = timing->eTindex + 1;
}
//*****************************************************************************
// This function initializes the control structures to their default values
//
//*****************************************************************************
void CMDLineProcessInit (void) {
uart_info_t *packet = GetUartPointer();
uart_cmd_t *cmd = GetCmdPointer();
timing_info_t *timing = GetTimerPointer();
stack_info_t *stack = GetStackPointer();
UARTResetCtrlStructure(packet);
CMDResetCtrlStructure(cmd);
TimingResetCtrlStructure(timing);
StackResetCtrlStructure(stack);
packet->procBusy = false;
packet->bytesTotal = 0;
packet->displayStats = false;
cmd->cmdTblPtr = cmdTable;
CMDLineSplashScreen ();
}
//*****************************************************************************
// This function will reset the timing_info_t structure
//
//*****************************************************************************
void TimingResetCtrlStructure (timing_info_t *timing) {
uint8_t i = 0;
timing->miliSeconds = 0;
timing->seconds = 0;
timing->minutes = 0;
timing->hours = 0;
timing->days = 0;
timing->cmdStart = 0;
timing->cmdStop = 0;
timing->elapsedTime = 0;
timing->eTindex = 0;
for (i = 0; i < TIMING_PROFILE_DEPTH; i++) {
timing->elapsedTimeProfile[i] = 0;
}
}
//*****************************************************************************
// This function will reset the uart_info_t structure
//
//*****************************************************************************
void UARTResetCtrlStructure (uart_info_t *packet) {
uint8_t i = 0;
packet->procBusy = false;
packet->enterFlag = false;
packet->cmdFound = false;
packet->rxCount = 0;
packet->cmdPos = 0;
// clear buffer
for (i = 0; i < BUFFER_SIZE; i++){
packet->uartBuf[i] = 0;
}
}
//*****************************************************************************
// This function will reset the uart_cmd_t structure
//
//*****************************************************************************
void CMDResetCtrlStructure (uart_cmd_t *cmd) {
uint8_t i = 0;
cmd->currentCmd = 0;
cmd->cmdIdx = 0;
cmd->commandFound = false;
cmd->argsNum = 0;
cmd->ExecComplete = true;
// reset arguments
for (i = 0; i < MAX_ARGS; i++) {
cmd->currentArgs[i] = 0;
}
}
//*****************************************************************************
// This function will reset the stack_info_t structure
//
//*****************************************************************************
void StackResetCtrlStructure (stack_info_t *stackPtr) {
stackPtr->stackIdx = 0;
stackPtr->startIdx = 0;
stackPtr->stopIdx = 0;
stackPtr->currIdx = 0;
stackPtr->execFromStack = false;
stackPtr->saveCommands = false;
stackPtr->contExecution = false;
}
//*****************************************************************************
// This function will encode a string of arbitary size to int.
//
//*****************************************************************************
uint16_t EncodeCommand (char *command, uint8_t size) {
const uint16_t inc = 2;
uint16_t result = 0;
uint8_t i = 0;
for (i = 0; i <= size - 1 ; i++) {
result += (uint16_t)*command * (i + inc);
command++;
}
return result;
}
//*****************************************************************************
// This function will do the same as the function above, but it will check
// if valid command delimiter is found, in case the incoming data is garbage.
//
//*****************************************************************************
uint16_t UARTEncodeCommand (uart_info_t *packet){
if (packet->cmdFound) {
return EncodeCommand(packet->uartBuf, packet->cmdPos);
}
else {
return 0;
}
}
//*****************************************************************************
// This function will search the table pointed by pointer in the uart_cmt_t
// structure and if it finds anything it will set the appropriate flag and
// populate the cmdIdx within the structure.
//*****************************************************************************
bool FindCommand (uart_cmd_t *cmd) {
uint8_t i = 0;
uint16_t *cmdPtr = cmd->cmdTblPtr;
bool ret = false;
// check if currentCmd filed is populated
if (cmd->currentCmd){
// search supplied table for match
while (!ret) {
if (cmd->currentCmd == *cmdPtr) {
ret = true;
cmd->cmdIdx = i;
cmd->commandFound = true;
}
else {
cmdPtr++;
i++;
if (i > MAX_COMMANDS - 1) {
return ret;
}
}
}
}
return ret;
}
//*****************************************************************************
// This function will populate the argument table and argument count in the
// uart_cmd_t structure, if it finds anything.
//*****************************************************************************
uint8_t UARTGetArguments (uart_info_t *packet, uart_cmd_t *cmd) {
uint8_t i = packet->cmdPos;
cmd->argsNum = 0;
if (packet->cmdFound) {
while(i != packet->rxCount) {
if (packet->uartBuf[i] == KEY_DASH) {
cmd->currentArgs[cmd->argsNum] = packet->uartBuf[i + 1];
cmd->argsNum++;
if (cmd->argsNum >= MAX_ARGS) {
return cmd->argsNum;
}
}
i++;
}
}
return cmd->argsNum;
}
//*****************************************************************************
// Returns adress of control structure
//*****************************************************************************
uart_info_t *GetUartPointer(void) {
return &uartData;
}
//*****************************************************************************
// Returns adress of control structure
//*****************************************************************************
uart_cmd_t *GetCmdPointer(void) {
return &uartCmd;
}
//*****************************************************************************
// Returns adress of control structure
//*****************************************************************************
timing_info_t *GetTimerPointer(void) {
return &timingData;
}
//*****************************************************************************
// Returns adress of control structure
//*****************************************************************************
stack_info_t *GetStackPointer(void) {
return &stack;
}
//*****************************************************************************
// Returns pointer to uart stack structure
//*****************************************************************************
uart_cmd_t *GetUartStackPointer(void) {
return uartCmdStack;
}
//*****************************************************************************
// Returns argument char from control structure
//*****************************************************************************
char GetArgument(uint8_t argNum) {
uart_cmd_t *cmdPtr = GetCmdPointer();
if (argNum <= MAX_ARGS - 1) {
return cmdPtr->currentArgs[argNum];
}
else {
return 0;
}
}
//*****************************************************************************
// Timekeeping interrupt service routine, called by NVIC
//*****************************************************************************
void SysTickIntHandler (void) {
timing_info_t *timing;
bool clearProcTiming = false;
timing = GetTimerPointer();
// handle time
if (timing->miliSeconds >= 1000) {
timing->miliSeconds = 0;
timing->seconds++;
clearProcTiming = true;
}
else {
timing->miliSeconds += SYSTICK_TIME;
}
if (timing->seconds >= 59) {
timing->seconds = 0;
timing->minutes++;
}
if (timing->minutes >= 59) {
timing->minutes = 0;
timing->hours++;
}
if (timing->hours >= 23) {
timing->hours = 0;
timing->days++;
}
// clear process Scheduler Timing data
if (clearProcTiming) {
ProcClearTimer();
clearProcTiming = false;
}
//
// update heartbeat parameters
//
if ((timing->seconds)%HEARTBEAT_INTERVAL) {
switch (timing->miliSeconds) {
case 200: GPIOPinWrite(HEARTBEAT_BASE_PORT, HEARTBEAT_PIN, 0xFF);
break;
case 300: GPIOPinWrite(HEARTBEAT_BASE_PORT, HEARTBEAT_PIN, 0x00);
break;
case 500: GPIOPinWrite(HEARTBEAT_BASE_PORT, HEARTBEAT_PIN, 0x00);
break;
case 600: GPIOPinWrite(HEARTBEAT_BASE_PORT, HEARTBEAT_PIN, 0xFF);
break;
case 700: GPIOPinWrite(HEARTBEAT_BASE_PORT, HEARTBEAT_PIN, 0x00);
break;
default:
break;
}
}
}
//*****************************************************************************
// Saves current command to next available location in stack
//*****************************************************************************
void SaveToStack (uart_cmd_t *cmd) {
stack_info_t *stack = GetStackPointer();
int arg = cmd->argsNum;
bool save = stack->saveCommands;
bool exec = stack->execFromStack;
//
// before saving check if stack execution is taking place
// or save to stack option is disabled
//
if (save && !exec) {
//
// convert argument number to index number
// because if argument number is
// equal to 2, that means that they are
// stored at locations 0 and 1
//
arg--;
// handle stack position overflow
if (stack->stackIdx == COMMAND_STACK_DEPTH) {
stack->stackIdx = 0;
}
// copy relevant information
uartCmdStack[stack->stackIdx].currentCmd = cmd->currentCmd;
uartCmdStack[stack->stackIdx].cmdIdx = cmd->cmdIdx;
uartCmdStack[stack->stackIdx].cmdTblPtr = cmd->cmdTblPtr;
uartCmdStack[stack->stackIdx].commandFound = cmd->commandFound;
uartCmdStack[stack->stackIdx].argsNum = cmd->argsNum;
// do not copy arguments if none are found
if (cmd->argsNum) {
while (arg >= 0) {
uartCmdStack[stack->stackIdx].currentArgs[arg] = cmd->currentArgs[arg];
arg--;
}
}
stack->stackIdx++;
}
}
//*****************************************************************************
// Copies info from stack to command line
//*****************************************************************************
void GetFromStack (uint8_t position) {
uart_cmd_t *cmd = GetCmdPointer();
uint8_t arg = uartCmdStack[position].argsNum;
uint8_t i;
//
// convert argument number to index number
// because if argument number is
// equal to 2, that means that they are
// stored at locations 0 and 1
// prevent underflow if number of arguments
// is zero
//
//if (arg) {
// arg--;
//}
// copy relevant information
cmd->currentCmd = uartCmdStack[position].currentCmd;
cmd->cmdIdx = uartCmdStack[position].cmdIdx;
cmd->commandFound = uartCmdStack[position].commandFound;
cmd->argsNum = uartCmdStack[position].argsNum;
// THIS NEEDS TO BE FIXED, because sometimes
// when a command with 3 args is followed by a command
// with two args, this shit gets confused.
// try: for(i = 0; i < arg; i++)
//
// This is probably fixed in CMDResetCtrlStructure()
// all arguments are reset after call to function
// has been completed.
if (arg) {
for (i = 0; i <= arg; i++) {
cmd->currentArgs[i] = uartCmdStack[position].currentArgs[i];
}
}
}
//*****************************************************************************
// Executes commands within a adress range
//*****************************************************************************
void ExecuteFromStack(void) {
uart_cmd_t *cmd = GetCmdPointer();
stack_info_t *stack = GetStackPointer();
timing_info_t *timing = GetTimerPointer();
bool loop = stack->contExecution;
int j = stack->stopIdx;
//
// assign code entry point in stack
// NB!: entry point is always valid
// handled by cmdqueue commands
//
stack->currIdx = stack->startIdx;
//
// Execute code from stack, if user cancels execution
// finish executing current function, before returning
// cancel is handled from uart interrupt service routine
//
while (stack->currIdx != j && stack->execFromStack) {
// calculate usage if running a script
CalculateUsage(timing);
timing->cmdStart = SysTickValueGet();
// check if current stack i properly populated, skip overwise
if (uartCmdStack[stack->currIdx].commandFound) {
// copy command from stack to cmd
GetFromStack(stack->currIdx);
ExecuteCommand(cmd);
stack->currIdx++;
}
else {
// increment stack counter if command is not valid
stack->currIdx++;
}
// loop forever if requested
if (loop && stack->currIdx >= j) {
stack->currIdx = stack->startIdx;
}
// Get stop value for timing
timing->cmdStop = SysTickValueGet();
}
// reset stack control strucutre after
// execution has been completed or
// canceled
StackResetCtrlStructure(stack);
}
//*****************************************************************************
// Enables or disables execution from stack
//*****************************************************************************
bool StackExecution(bool enable) {
stack_info_t *stackPtr = GetStackPointer();
stackPtr->execFromStack = enable;
return enable;
}
//*****************************************************************************
// Enables or disables the use of stack
//*****************************************************************************
void SaveCommands(bool enable) {
stack_info_t *stackPtr = GetStackPointer();
stackPtr->saveCommands = enable;
}
//*****************************************************************************
// UART interrupt service routine, called by NVIC
//*****************************************************************************
void UartIntHandler (void) {
uint8_t i;
char c;
stack_info_t *stack = GetStackPointer();
uart_cmd_t *cmd = GetCmdPointer();
script_settings_t *s = GetScriptSettingsPointer();
//
// get current interrupt status and
// clear it
//
uartData.lastIntStatus = UARTIntStatus(UART0_BASE, true);
UARTIntClear(UART0_BASE, uartData.lastIntStatus);
// get current char for processing
c = (char)UARTCharGet(UART0_BASE);
// Script processing
if (s->scriptModeOn) {
ScriptCallBackHandler(c);
} else {
// check if previous processing is done yet
if (!uartData.procBusy) {
if (c == KEY_ENTER) {
// acknolowedge to user that packet is recieved
// I don't always use printf, but when i do, i do it
// from the interrupt handler :)
if (uartData.displayStats) {
UARTprintf("\nBytes recieved: %d\n", uartData.rxCount);
UARTprintf("Bytes sofar: %d\n", uartData.bytesTotal);
UARTprintf("Command: ");
for (i = 0; i <= uartData.rxCount; i++) {
UARTprintf("%x", uartData.uartBuf[i]);
}
UARTprintf("\nCmd ends at position %d\n", uartData.cmdPos);
}
// Tell string processing routine that packet is ready
// for processing
uartData.enterFlag = true;
uartData.procBusy = true;
}
else {
// save char and send it back to terminal
// TODO: do not print non-printable characters or
// control characters
uartData.currentChar = c;
UARTprintf("%c", c);
// space usually delimits cmd on first occurence
if (c == KEY_SPACE && !uartData.cmdFound) {
uartData.cmdPos = uartData.rxCount;
uartData.cmdFound = true;
}
// handle overflow
if (uartData.rxCount >= BUFFER_SIZE - 1) {
uartData.rxCount = 0;
UARTprintf(CMD_OVERFLOW);
}
else {
// handle backspace input to
// delete things from buffer
if (c == KEY_BACKSPACE) {
if (uartData.rxCount <= 0) {
uartData.rxCount = 0;
}
else {
uartData.rxCount--;
}
if (uartData.uartBuf[uartData.rxCount] == KEY_SPACE) {
uartData.cmdFound = false;
}
}
else {
uartData.uartBuf[uartData.rxCount] = c;
uartData.rxCount++;
}
}
}
}
else {
UARTprintf(CMD_PROC_BUSY);
if (c == KEY_BACKSPACE && stack->execFromStack) {
// cancel current stack execution
stack->contExecution = false;
stack->execFromStack = false;
}
}
// toggle led to indicate byte activity
if (uartData.ledFlag) {
uartData.ledFlag = false;
//GPIOPinWrite(HEARTBEAT_BASE_PORT, HEARTBEAT_PIN, 0xFF);
}
else {
uartData.ledFlag = true;
//GPIOPinWrite(HEARTBEAT_BASE_PORT, HEARTBEAT_PIN, 0x00);
}
// increment stat counter
uartData.bytesTotal++;
}
}
//*****************************************************************************
// Executes command
//*****************************************************************************
bool ExecuteCommand (uart_cmd_t *cmd){
// check if current command code
// is valid
if (!cmd->currentCmd) {
return false;
}
else {
cmd->ExecComplete = false;
}
// Execute Command
cmdFuncTable[cmd->cmdIdx]();
// Update flag
cmd->ExecComplete = true;
return cmd->ExecComplete;
}
//*****************************************************************************
// Registers a command code in a table if correct position is specified
//*****************************************************************************
bool RegisterCommandCode (uint8_t pos, uint16_t cmdCode) {
uart_cmd_t *cmd = GetCmdPointer();
uint16_t *cmdPtr = cmd->cmdTblPtr;
bool ret = false;
// Sanity check for correct pointer
if (!cmdPtr) {
UARTprintf(CMD_PTR_ERROR);
return false;
}
// advance pointer to entry
cmdPtr += pos;
// check bounds, set entry if valid
if (pos < MAX_COMMANDS - 1) {
*cmdPtr = cmdCode;
ret = true;
}
else {
UARTprintf(CMD_INV_IDX, pos);
}
return ret;
}
//*****************************************************************************
// Register a handler within a table if correct position is specified
//*****************************************************************************
bool RegisterCommandFunc (uint8_t pos, void (*handler)(void)) {
bool ret = false;
if (pos < MAX_COMMANDS - 1) {
cmdFuncTable[pos] = handler;
ret = true;
}
else {
UARTprintf(CMD_INV_IDX, pos);
}
return ret;
}
//*****************************************************************************
// Evaluates command just if it was entered via uart terminal.
// This command fills the appropriate fields of the uart structure,
// causing the program to think that the data was recieved via uart Terminal
// Example:
// EvaluateStringCommand("time -d -1\n", 11);
// Will cause a 1 second delay to be executed
//
// In order to execute several commands in series,
// the CMDLineScheduler () command must be executed
// each time EvaluateStringCommand () is executed.
//
//
// This command can be used to execute stream of chars that is coming
// from other source than uart, it can be from an udp stream or can or
// whatever.
//
//*****************************************************************************
bool EvaluateStringCommand (char *command) {
uart_info_t *packet = GetUartPointer();
char *c = command;
uint8_t i = 0;
bool ret = false;
// prepare structure parameters
packet->enterFlag = false;
packet->procBusy = false;
packet->cmdFound = false;
// identify command location, command is denoted by space
// only on first occurence, also count the number of chars
// so we know if there are any arguments after command.
while (!ret) {
if (*c == KEY_SPACE && packet->cmdFound == false) {
packet->cmdPos = i;
packet->cmdFound = true;
}
else {
// handle limit
if (i >= BUFFER_SIZE && !ret) {
return false;
}
}
// handle character size
if (*c == '\n') {
packet->rxCount = i;
ret = true;
}
c++;
i++;
}
//
// copy string to input buffer
// and force evaluation of the
// command
//
if (packet->cmdFound) {
c = command;
for (i = 0; i <= packet->rxCount - 1; i++) {
packet->uartBuf[i] = *c;
c++;
}
// force packet evaluation
packet->enterFlag = true;
}
// remember to clear buffer here to avoid
// unknown chars in buffer
return packet->cmdFound;
}
//*****************************************************************************
// Evaluates multiple string commands
// char *cmd[] should contain strings to be executed
// uint8_t cmdNum should contain the number of commands to execute
//*****************************************************************************
bool EvaluateMultipleCommands (char *cmd[], uint8_t cmdNum) {
uint8_t index = 0;
bool ret = false;
if (cmd == 0) {
UARTprintf(CMD_PTR_ERROR);
return ret;
}
for (index = 0; index <= cmdNum - 1; index++) {
ret = EvaluateStringCommand(cmd[index]);
CMDLineScheduler ();
}
UARTprintf(CMD_MULT_FINISH, index);
UARTprintf(INPUT_PROMPT);
return ret;
}
//*****************************************************************************
// Initilizes hardware
//*****************************************************************************
void InitCortexHardware(void) {
//
// Enable FPU
//
FPUEnable();
FPULazyStackingEnable();
// Set clocking to 50 MHz, due to REV_A1 being a total ass.
SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
//
// Configure UART0 for 115200-8n1
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
UARTStdioConfig(0, 115200, 16000000);
UARTFIFODisable(UART0_BASE);
UARTIntEnable(UART0_BASE, UART_INT_RX);
UARTprintf("\n\nSystem initializing.\n");
//
// Enable SysTick for periodic Interrupts
// systick is used by command line process
//
SysTickEnable();
SysTickPeriodSet(SysCtlClockGet()/(SYSTICK_TIME/10));
SysTickIntEnable();
UARTprintf("- Systick timer enabled.\n");