forked from ddk/ddk-arm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.c
933 lines (773 loc) · 30.6 KB
/
cli.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
/*
* Copyright (c) 2013, The DDK Project
* Dmitry Nedospasov <dmitry at nedos dot net>
* Thorsten Schroeder <ths at modzero dot ch>
*
* All rights reserved.
*
* This file is part of Die Datenkrake (DDK).
*
* "THE BEER-WARE LICENSE" (Revision 42):
* <dmitry at nedos dot net> and <ths at modzero dot ch> wrote this file. As
* long as you retain this notice you can do whatever you want with this stuff.
* If we meet some day, and you think this stuff is worth it, you can buy us a
* beer in return. Die Datenkrake Project.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE DDK PROJECT BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* File: cli.c
* Description: Implementation of Command Line Interface.
*
* *********************************************************************** * * * * * * * * * * *
* Parts of the argument dispatching routines were taken from the LPC2148 demo code v1.44
* available at http://jcwren.com/arm/. Credits and thanks to jcwren _-at-_ jcwren.com -
* i didn't even know her/his real name. -- Thorsten, Berlin, Sept. 8th 2010
* *********************************************************************** * * * * * * * * * * *
*/
#include "FreeRTOS.h"
#include "task.h"
#include "portable.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include "main.h"
#include "cli.h"
#include "helper.h"
#include "dpuser.h"
#include "io.h"
#include "buffer.h"
#include "led.h"
#include "tests.h"
#include "uart.h"
void vPortUsedMem(int *, int *, int *);
commandList_t *activeCommandList;
extern size_t read(int, unsigned char *, size_t );
static int __attribute__ ((unused)) prv_cli_led_set (int , portCHAR **);
static int __attribute__ ((unused)) prv_cli_led_clr (int , portCHAR **);
static int __attribute__ ((unused)) prv_cli_fpga(int , portCHAR **);
static int __attribute__ ((unused)) prv_cli_fpga_halt(int , portCHAR **);
static int __attribute__ ((unused)) prv_cli_fpga_reset(int , portCHAR **);
static int __attribute__ ((unused)) prv_cli_fpga_off(int , portCHAR **);
static int __attribute__ ((unused)) prv_cli_fpga_on(int , portCHAR **);
static int __attribute__ ((unused)) prv_cli_buf_en(int , portCHAR **);
static int __attribute__ ((unused)) prv_cli_buf_dis(int , portCHAR **);
static int __attribute__ ((unused)) prv_cli_buf_all_en(int , portCHAR **);
static int __attribute__ ((unused)) prv_cli_buf_all_dis(int , portCHAR **);
static int __attribute__ ((unused)) prv_cli_adv_erase(int , portCHAR **);
static int __attribute__ ((unused)) prv_cli_adv_test(int , portCHAR **);
static int __attribute__ ((unused)) prv_cli_adv_prog(int , portCHAR **);
static int __attribute__ ((unused)) prv_cli_led_ctl(int , portCHAR **);
static int __attribute__ ((unused)) prv_cli_led_ctl_all(int , portCHAR **);
static int __attribute__ ((unused)) prv_cli_data_write(int , portCHAR **);
static int __attribute__ ((unused)) prv_cli_data_read(int , portCHAR **);
static int __attribute__ ((unused)) prv_cli_buf_on(int , portCHAR **);
static int __attribute__ ((unused)) prv_cli_buf_off(int , portCHAR **);
static int prv_cli_help(int , portCHAR **);
static int prv_cli_version(int , portCHAR **);
static int prv_cli_reboot(int , portCHAR **);
static int prv_cli_credits(int , portCHAR **);
static int prv_mem_task(int , portCHAR **);
static int prv_mem_free(int , portCHAR **);
/* CLI sub-command example... */
static const commandList_t commandListMem [] =
{
{ "help", 0, 0, CMDTYPE_FUNCTION, { prv_cli_help }, "This help list", "'help' has no parameters" },
{ "task", 0, 0, CMDTYPE_FUNCTION, { prv_mem_task }, "Show FreeRTOS tasks & stacks", "'task' has no parameters" },
{ "free", 0, 0, CMDTYPE_FUNCTION, { prv_mem_free }, "Show heap memory statistics", "'heap' has no parameters" },
{ NULL, 0, 0, CMDTYPE_FUNCTION, { NULL }, NULL, NULL },
};
static const commandList_t commandListLED [] =
{
{ "help", 0, 0, CMDTYPE_FUNCTION, { prv_cli_help }, "This help list", "'help' has no parameters" },
{ "set", 0, 1, CMDTYPE_FUNCTION, { prv_cli_led_set }, "set [led] - if no led is specified, all LEDs are set.", "'set' has 1 parameters" },
{ "clear", 0, 1, CMDTYPE_FUNCTION, { prv_cli_led_clr }, "clear [led] - if no led is specified, all LEDs are cleared.", "'clear' has 1 parameters" },
{ NULL, 0, 0, CMDTYPE_FUNCTION, { NULL }, NULL, NULL },
};
static const commandList_t commandListFPGA [] =
{
{ "help", 0, 0, CMDTYPE_FUNCTION, { prv_cli_help }, "This help list", "'help' has no parameters" },
{ "reset", 0, 0, CMDTYPE_FUNCTION, { prv_cli_fpga_reset }, "reset - reset FPGA by pulling reset high and low", "'reset' has no parameters" },
{ "halt", 0, 0, CMDTYPE_FUNCTION, { prv_cli_fpga_halt }, "halt - pull reset line low", "'halt' has no parameters" },
{ "on", 0, 0, CMDTYPE_FUNCTION, { prv_cli_fpga_on }, "on - power on FPGA", "'on' has no parameters" },
{ "off", 0, 0, CMDTYPE_FUNCTION, { prv_cli_fpga_off }, "off - power off FPGA", "'off' has no parameters" },
{ NULL, 0, 0, CMDTYPE_FUNCTION, { NULL }, NULL, NULL },
};
static const commandList_t commandListBUF [] =
{
{ "help", 0, 0, CMDTYPE_FUNCTION, { prv_cli_help }, "This help list", "'help' has no parameters" },
{ "enable", 0, 1, CMDTYPE_FUNCTION, { prv_cli_buf_on }, "enable [buf] - if no buffer is specified, all buffers are enabled.", "'enable' has 1 parameters" },
{ "disable", 0, 1, CMDTYPE_FUNCTION, { prv_cli_buf_off }, "disable [buf] - if no buffer is specified, all buffers are disabled.", "'disable' has 1 parameters" },
{ NULL, 0, 0, CMDTYPE_FUNCTION, { NULL }, NULL, NULL },
};
static const commandList_t commandListADV [] =
{
{ "help", 0, 0, CMDTYPE_FUNCTION, { prv_cli_help }, "This help list", "'help' has no parameters" },
{ "test", 0, 0, CMDTYPE_FUNCTION, { prv_cli_adv_test }, "manually perform HW checks.", "'test' has no parameters" },
{ "prog", 0, 0, CMDTYPE_FUNCTION, { prv_cli_adv_prog }, "Force reprogramming of the FPGA flash ROM. (!!! RTFM !!!)", "'prog' has no parameters" },
{ "erase", 0, 0, CMDTYPE_FUNCTION, { prv_cli_adv_erase}, "Force reasure of the FPGA flash ROM. (!!! RTFM !!!)", "'erase' has no parameters" },
{ NULL, 0, 0, CMDTYPE_FUNCTION, { NULL }, NULL, NULL },
};
static const commandList_t commandList [] =
{
{ "mem", 0, 1, CMDTYPE_CMDLIST, { commandListMem }, "Various memory functions", "'mem help' for help list" },
{ "led", 1, 2, CMDTYPE_CMDLIST, { commandListLED }, "LED control commands", "'led help' for help list" },
{ "fpga", 0, 0, CMDTYPE_CMDLIST, { commandListFPGA }, "FPGA control commands", "'fpga help' for help list" },
{ "buffer", 0, 1, CMDTYPE_CMDLIST, { commandListBUF }, "Buffer/channel control commands", "'buffer help' for help list" },
{ "adv", 0, 0, CMDTYPE_CMDLIST, { commandListADV }, "Advanced commands", "'adv help' for help list" },
{ "help", 0, 0, CMDTYPE_FUNCTION, { prv_cli_help }, "This help list", "'help' has no parameters" },
{ "credits", 0, 0, CMDTYPE_FUNCTION, { prv_cli_credits }, "Display credits, greets & shoutz", "'version' has no parameters" },
{ "reboot", 0, 0, CMDTYPE_FUNCTION, { prv_cli_reboot }, "Reset Datenkrake", "'reboot' has no parameters" },
// aliases
{ "R", 0, 0, CMDTYPE_FUNCTION, { prv_cli_fpga_reset }, "FPGA reset", "'R' reset FPGA" },
{ "H", 0, 0, CMDTYPE_FUNCTION, { prv_cli_fpga_halt }, "FPGA halt (reset high)", "'H' halt FPGA - no params" },
{ "o", 0, 0, CMDTYPE_FUNCTION, { prv_cli_fpga_off }, "Power off FPGA", "'o' Power off FPGA - no params" },
{ "O", 0, 0, CMDTYPE_FUNCTION, { prv_cli_fpga_on }, "Power on FPGA", "'O' Power on FPGA - no params" },
{ "e", 1, 1, CMDTYPE_FUNCTION, { prv_cli_buf_en }, "Buffer enable", "'e <num>' enable buffer <num>" },
{ "d", 1, 1, CMDTYPE_FUNCTION, { prv_cli_buf_dis }, "Buffer disable", "'d <num>' disable buffer <num>" },
{ "E", 0, 0, CMDTYPE_FUNCTION, { prv_cli_buf_all_en }, "Buffer enable all", "'E' enable all - no params" },
{ "D", 0, 0, CMDTYPE_FUNCTION, { prv_cli_buf_all_dis }, "Buffer disable all", "'D' disable all - no params" },
{ "0", 0, 0, CMDTYPE_FUNCTION, { prv_cli_adv_erase }, "Advanced - FPGA erase flash ROM", "'0' Erase FPGA - no params" },
{ "t", 0, 0, CMDTYPE_FUNCTION, { prv_cli_adv_test }, "Advanced - HW test", "'t' HW tests - no params" },
{ "p", 0, 0, CMDTYPE_FUNCTION, { prv_cli_adv_prog }, "Advanced - Program FPGA flash ROM", "'p' program FPGA - no params" },
{ "l", 2, 2, CMDTYPE_FUNCTION, { prv_cli_led_ctl }, "LED control single", "'l <num> <status>' control LEDs - status = 0 or 1" },
{ "L", 1, 1, CMDTYPE_FUNCTION, { prv_cli_led_ctl_all }, "LED control all", "'L <status>' - control all LEDs - status = 0 or 1" },
{ "w", 2, 2, CMDTYPE_FUNCTION, { prv_cli_data_write }, "Data write", "'w <reg> <data>' write 8-bit data <data> to register <reg>" },
{ "r", 1, 1, CMDTYPE_FUNCTION, { prv_cli_data_read }, "Data read", "'r <reg>' read register <reg>" },
{ NULL, 0, 0, CMDTYPE_FUNCTION, { NULL }, NULL, NULL },
};
// ******************************************************** ** ** ** ** * * * *
static int __attribute__ ((unused)) prv_cli_buf_on (int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
unsigned int buf = 0;
if(argc < 1) {
buf1_enable();
buf2_enable();
buf3_enable();
buf4_enable();
buf5_enable();
buf6_enable();
buf7_enable();
buf8_enable();
puts("All buffers enabled.");
} else {
buf = strtol(argv[0], NULL, 10) & 0xff;
switch(buf) {
case 1:
buf1_enable(); break;
case 2:
buf2_enable(); break;
case 3:
buf3_enable(); break;
case 4:
buf4_enable(); break;
case 5:
buf5_enable(); break;
case 6:
buf6_enable(); break;
case 7:
buf7_enable(); break;
case 8:
buf8_enable(); break;
default:
break;
}
printf("Buffer %d enabled.\n", buf);
}
return 0;
}
// ******************************************************** ** ** ** ** * * * *
static int __attribute__ ((unused)) prv_cli_buf_off (int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
unsigned int buf = 0;
if(argc < 1) {
buf1_disable();
buf2_disable();
buf3_disable();
buf4_disable();
buf5_disable();
buf6_disable();
buf7_disable();
buf8_disable();
puts("All buffers disabled.");
} else {
buf = strtol(argv[0], NULL, 10) & 0xff;
switch(buf) {
case 1:
buf1_disable(); break;
case 2:
buf2_disable(); break;
case 3:
buf3_disable(); break;
case 4:
buf4_disable(); break;
case 5:
buf5_disable(); break;
case 6:
buf6_disable(); break;
case 7:
buf7_disable(); break;
case 8:
buf8_disable(); break;
default:
break;
}
printf("Buffer %d disabled.\n", buf);
}
return 0;
}
// ******************************************************** ** ** ** ** * * * *
static int __attribute__ ((unused)) prv_cli_led_set (int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
unsigned int led = 0;
if(argc < 1) {
led1_set();
led2_set();
led3_set();
led4_set();
} else {
led = strtol(argv[0], NULL, 10) & 0xff;
switch(led) {
case 1:
led1_set();
break;
case 2:
led2_set();
break;
case 3:
led3_set();
break;
case 4:
led4_set();
break;
default:
break;
}
}
return 0;
}
// ******************************************************** ** ** ** ** * * * *
static int __attribute__ ((unused)) prv_cli_led_clr (int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
unsigned int led = 0;
if(argc < 1) {
led1_clr();
led2_clr();
led3_clr();
led4_clr();
} else {
led = strtol(argv[0], NULL, 10) & 0xff;
switch(led) {
case 1:
led1_clr();
break;
case 2:
led2_clr();
break;
case 3:
led3_clr();
break;
case 4:
led4_clr();
break;
default:
break;
}
}
return 0;
}
// ******************************************************** ** ** ** ** * * * *
static int __attribute__ ((unused)) prv_cli_fpga (int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
printf("ERR: Implement me.\n");
return 0;
}
// ******************************************************** ** ** ** ** * * * *
static int __attribute__ ((unused)) prv_cli_fpga_reset (int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
fpga_rst_high();
vTaskDelay(100 / portTICK_RATE_MS);
fpga_rst_low();
puts("FPGA reset done.");
return 0;
}
// ******************************************************** ** ** ** ** * * * *
static int __attribute__ ((unused)) prv_cli_fpga_halt (int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
fpga_rst_high();
puts("FPGA halted.");
return 0;
}
// ******************************************************** ** ** ** ** * * * *
static int __attribute__ ((unused)) prv_cli_fpga_off (int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
io_fpga_disable();
puts("FPGA powered off");
return 0;
}
// ******************************************************** ** ** ** ** * * * *
static int __attribute__ ((unused)) prv_cli_fpga_on (int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
io_fpga_enable();
puts("FPGA powered on");
return 0;
}
// ******************************************************** ** ** ** ** * * * *
static int __attribute__ ((unused)) prv_cli_buf_en (int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
unsigned int buf = strtol(argv[0], NULL, 10) & 0xff;
switch(buf) {
case 1:
buf1_enable(); break;
case 2:
buf2_enable(); break;
case 3:
buf3_enable(); break;
case 4:
buf4_enable(); break;
case 5:
buf5_enable(); break;
case 6:
buf6_enable(); break;
case 7:
buf7_enable(); break;
case 8:
buf8_enable(); break;
default:
break;
}
printf("Buffer %d enabled.\n", buf);
return 0;
}
// ******************************************************** ** ** ** ** * * * *
static int __attribute__ ((unused)) prv_cli_buf_dis (int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
unsigned int buf = strtol(argv[0], NULL, 10) & 0xff;
switch(buf) {
case 1:
buf1_disable(); break;
case 2:
buf2_disable(); break;
case 3:
buf3_disable(); break;
case 4:
buf4_disable(); break;
case 5:
buf5_disable(); break;
case 6:
buf6_disable(); break;
case 7:
buf7_disable(); break;
case 8:
buf8_disable(); break;
default:
break;
}
printf("Buffer %d disabled.\n", buf);
return 0;
}
// ******************************************************** ** ** ** ** * * * *
static int __attribute__ ((unused)) prv_cli_buf_all_en (int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
buf1_enable();
buf2_enable();
buf3_enable();
buf4_enable();
buf5_enable();
buf6_enable();
buf7_enable();
buf8_enable();
puts("All buffers enabled.");
return 0;
}
// ******************************************************** ** ** ** ** * * * *
static int __attribute__ ((unused)) prv_cli_buf_all_dis (int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
buf1_disable();
buf2_disable();
buf3_disable();
buf4_disable();
buf5_disable();
buf6_disable();
buf7_disable();
buf8_disable();
puts("All buffers disabled.");
return 0;
}
// ******************************************************** ** ** ** ** * * * *
static int __attribute__ ((unused)) prv_cli_adv_erase (int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
puts("Erasing FPGA flash ROM.");
dp_erase_main();
puts("FPGA flash ROM erased.");
return 0;
}
// ******************************************************** ** ** ** ** * * * *
static int __attribute__ ((unused)) prv_cli_adv_test (int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
ddk_manual_tests();
return 0;
}
// ******************************************************** ** ** ** ** * * * *
static int __attribute__ ((unused)) prv_cli_adv_prog (int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
dp_force_prog_main();
return 0;
}
// ******************************************************** ** ** ** ** * * * *
static int __attribute__ ((unused)) prv_cli_led_ctl (int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
unsigned int led = strtol(argv[0], NULL, 10) & 0xff;
unsigned int val = strtol(argv[1], NULL, 10) & 0xff;
printf("Setting LED %d to %d\n", led, val);
switch(led) {
case 1:
if(val)
led1_set();
else
led1_clr();
break;
case 2:
if(val)
led2_set();
else
led2_clr();
break;
case 3:
if(val)
led3_set();
else
led3_clr();
break;
case 4:
if(val)
led4_set();
else
led4_clr();
break;
default:
break;
}
return 0;
}
// ******************************************************** ** ** ** ** * * * *
static int __attribute__ ((unused)) prv_cli_led_ctl_all (int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
unsigned int val = strtol(argv[0], NULL, 10) & 0xff;
if(val) {
led1_set();
led2_set();
led3_set();
led4_set();
} else {
led1_clr();
led2_clr();
led3_clr();
led4_clr();
}
printf("All LEDs switched %s.\n", (val ? "on" : "off") );
return 0;
}
// ******************************************************** ** ** ** ** * * * *
static int __attribute__ ((unused)) prv_cli_data_write (int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
unsigned char reg = 0;
unsigned char val = 0;
reg = strtol(argv[0], NULL, 16) & 0xff;
val = strtol(argv[1], NULL, 16) & 0xff;
io_fpga_register_write(reg, val);
return 0;
}
// ******************************************************** ** ** ** ** * * * *
static int __attribute__ ((unused)) prv_cli_data_read (int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
unsigned char reg = 0;
reg = strtol(argv[0], NULL, 16) & 0xff;
io_fpga_register_read(reg);
return 0;
}
/* *** MEMORY Command Handler *** */
static int prv_mem_task(int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
#if configUSE_TRACE_FACILITY == 1
signed portCHAR buffer[TASKHANDLE_LAST * 42];
int bytesFree;
int bytesUsed;
int blocksFree;
vTaskList (buffer);
vPortUsedMem (&bytesFree, &bytesUsed, &blocksFree);
//
// Stack is the minimum amount of stack there has been available at any time since the task started executing.
//
printf ("Task\t\tState\tPrty\tStack\tTask#\n");
printf ("%s\n", buffer);
#else
printf ("Not implemented (requires configUSE_TRACE_FACILITY in FreeRTOSConfig.h)\n");
#endif
return 0;
}
static int prv_mem_free(int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
#if configUSE_TRACE_FACILITY == 1
int bytesFree;
int bytesUsed;
int blocksFree;
vPortUsedMem (&bytesFree, &bytesUsed, &blocksFree);
//
// Stack is the minimum amount of stack there has been available at any time since the task started executing.
//
printf ("Heap size=%ld, used=%d, free=%d (%d blocks)\n", configTOTAL_HEAP_SIZE, bytesUsed, bytesFree, blocksFree);
#else
printf ("Not implemented (requires configUSE_TRACE_FACILITY in FreeRTOSConfig.h)\n");
#endif
return 0;
}
// ******************************************************** ** ** ** ** * * * *
static int __attribute__ ((unused)) prv_dummy_lala (int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
printf("ERR: Implement me.\n");
return 0;
}
// ******************************************************** ** ** ** ** * * * *
/* *** CLI Command Handler *** */
static int prv_cli_version(int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
printf("Die Datenkrake Version 1.0\n");
return 0;
}
static int prv_cli_credits(int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
printf("******************** *** ** ** ** * * * *\n");
printf("* Die Datenkrake was developed by Dmitry Nedospasov & Thorsten Schroeder *\n");
printf("* web: http://datenkrake.org -- twitter: @DieDatenkrake *\n");
printf("* mail: datenkrake at dev dot io -- irc: #DDK on freenode *\n");
printf("******************** *** ** ** ** * * * *\n");
printf("* 3rd party code or ideas that were used in this project: *\n");
printf("* > FreeRTOS 7, Real Time Kernel by Richard Barry *\n");
printf("* Copyright (C) 2012 Real Time Engineers Ltd. *\n");
printf("* -> http://www.freertos.org/ *\n");
printf("* > CLI command handling basics were developed & published by *\n");
printf("* J.C.Wren <jcwren-at-jcwren.com> in his LPC2148 demo code v1.44 *\n");
printf("* -> http://jcwren.com/arm/ *\n");
printf("**** THANK YOU! **** *** ** ** ** * * * *\n");
printf("* Special thanks & greetings to: *\n");
printf("* Hugo Fortier & REcon crew *\n");
printf("* Colleagues at SECT & modzero AG *\n");
printf("* Daniel Mack, Joachim Steiger, Felix von Leitner *\n");
printf("* Microsemi Corporation - http://www.microsemi.com/ *\n");
printf("******************** *** ** ** ** * * * *\n");
return 0;
}
static int prv_cli_reboot(int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
printf("Rebooting now...\n");
uart0_shutdown();
NVIC_SystemReset();
return 0;
}
static int prv_cli_help(int argc __attribute__ ((unused)), portCHAR **argv __attribute__ ((unused)))
{
unsigned int i;
int t;
int longestCmd;
portCHAR spaces [32];
memset (spaces, ' ', sizeof (spaces));
for (longestCmd = 0, i = 0; activeCommandList [i].command; i++)
if ((t = strlen (activeCommandList [i].command)) > longestCmd)
longestCmd = t;
spaces [longestCmd] = '\0';
for (i = 0; activeCommandList [i].command; i++)
{
const commandList_t *cl = &activeCommandList [i];
printf ("%s%s -- %s\n", cl->command, &spaces [strlen (cl->command)], cl->description);
}
printf ("\nUse '<command> ?' for details on parameters to command\n");
return 0;
}
//
// bufferLength includes the space reserved for the \0
//
int argsGetLine (int fd __attribute__ ((unused)), unsigned char *buffer, int bufferLength, const char *prompt)
{
unsigned char *p;
p = buffer;
*p = '\0';
printf ("%s", prompt);
while (1)
{
unsigned char c;
c = getchar();
//if(read(0, &c, sizeof(c)) ==sizeof(c))
if (c != EOF)
{
switch (c)
{
case '\n' :
case '\r' :
printf ("\n");
return strlen ((char *) buffer);
// treat DEL as BS
case 0x7f:
if (p > buffer)
*--p = '\0';
printf ("\x08 \x08");
break;
// BS
case 0x08:
if (p > buffer)
*--p = '\0';
printf ("\x08 \x08");
break;
case 0x15 : // CTRL-U
while (p != buffer)
{
printf ("\b \b");
--p;
}
*p = '\0';
break;
case 0xfe :
case 0xff :
*buffer++ = c;
*buffer = '\0';
return 1;
default :
if (p < buffer + bufferLength - 1 && c >= ' ' && c < 0x7f)
{
*p++ = c;
*p = '\0';
printf ("%c", c);
}
else
printf ("%c", c);
break;
}
}
}
return 0;
}
//
//
//
typedef enum
{
P_EATWHITESPACE = 0,
P_GETCHARFIRST,
P_GETCHAR,
P_QUOTEDGETCHAR
}
PSTATE;
int argsParse (char *cmd, char **argv, int sizeofArgv, int *argc)
{
int maxArgs = (sizeofArgv / sizeof (argv [0])) - 1;
char *s = strtrim (cmd);
PSTATE pstate = P_EATWHITESPACE;
*argc = 0;
memset (argv, 0, sizeofArgv);
while (*s)
{
switch (pstate)
{
case P_EATWHITESPACE :
{
if (!isspace (*s))
pstate = P_GETCHARFIRST;
else
s++;
}
break;
case P_GETCHARFIRST :
{
*argv++ = s;
if (++*argc == maxArgs)
return 1;
if (*s == '"')
pstate = P_QUOTEDGETCHAR;
else
pstate = P_GETCHAR;
s++;
}
break;
case P_GETCHAR :
{
if (isspace (*s)) {
pstate = P_EATWHITESPACE;
*s = '\0';
}
else if (*s == '"')
pstate = P_QUOTEDGETCHAR;
s++;
}
break;
case P_QUOTEDGETCHAR :
{
if (*s == '"')
pstate = P_GETCHAR;
s++;
}
break;
}
}
return 0;
}
int argsDispatch (const commandList_t *cl, int argc, char **argv)
{
int ret = 0;
activeCommandList = (commandList_t *)cl;
while (cl->command) {
if (!strcmp (cl->command, argv [0])) {
ret = 0;
if ((argc == 2) && !strcmp (argv [1], "?"))
printf ("%s\n", cl->parameters);
else if ((argc - 1) < cl->minArgs)
printf ("Too few arguments to command (%d expected)\n", cl->minArgs);
else if (cl->cmdType == CMDTYPE_CMDLIST)
argsDispatch (cl->commandList, argc - 1, &argv [1]);
else if ((argc - 1) > cl->maxArgs)
printf ("Too many arguments to command (%d maximum)\n", cl->maxArgs);
else
ret = (*cl->handler) (argc - 1, &argv [1]);
return ret;
}
cl++;
}
if (!cl->command)
printf ("Unknown command \"%s\"\n", argv [0]);
return 0;
}
//-----------------------------------------------------------------------------
// Main task for CLI
//-----------------------------------------------------------------------------
void vCLITask( void *pvParameters )
{
const portTickType xDelay = 100 / portTICK_RATE_MS;
static unsigned char buffer[256];
static portCHAR *argv[34];
int argc;
/* Just to prevent compiler warnings about the unused parameter. */
( void ) pvParameters;
printf("[*] Starting CLI task.\n");
for (;;) {
int l;
if ((l = argsGetLine (0, buffer, sizeof (buffer), PROMPT))) {
if (argsParse ((char *) buffer, argv, sizeof (argv), &argc))
printf ("Too many arguments (max %ld)\n", sizeof (argv));
else if (argv[0])
argsDispatch (commandList, argc, &argv [0]);
}
vTaskDelay( xDelay );
}
for(;;) {
vTaskDelay( xDelay );
}
}