forked from nxp-imx/bcu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbcu.c
4051 lines (3666 loc) · 105 KB
/
bcu.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
/*
* Copyright 2019-2021 NXP.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* Neither the name of the NXP Semiconductor nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 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 COPYRIGHT HOLDER OR CONTRIBUTORS 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.
*
*/
#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS //in order to use strcpy without error
#include <windows.h>
#include <processthreadsapi.h>
#endif
#if defined(linux) || defined(__APPLE__)
#include <unistd.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <curses.h>
#include "ftdi.h"
#endif
//common library for both OS
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <ctype.h>
#include <math.h>
#include "port.h"
#include "bcu_parser.h"
#include "chip.h"
#include "board.h"
#include "version.h"
#include "bcu_yaml.h"
#include "bcu_https.h"
#include "bcu_ftdi_eeprom.h"
#define DONT_RESET 0
#define RESET_NOW 1
#define INIT_WITHOUT_BOOTMODE 2
#define DONT_INIT 0
#define INIT_NOW 1
#define LSBOOTMODE_NSHOWID 0
#define LSBOOTMODE_SHOWID 1
#define GET_COLUMN 0
#define GET_ROW 1
#define DISPLAY_WIDTH_MODE_1 70
#define DISPLAY_WIDTH_MODE_2 84
#define DISPLAY_WIDTH_MODE_3 99
#define DISPLAY_WIDTH_MODE_4 111
#define DISPLAY_WIDTH_MODE_5 138
extern int num_of_boards;
extern struct board_info board_list[];
int GV_MONITOR_TERMINATED = 0;
static int enable_exit_handler = 0;
char* g_vt_red = (char*)"\x1B[91m";
char* g_vt_green = (char*)"\x1B[92m";
char* g_vt_yellow = (char*)"\x1B[93m";
char* g_vt_kcyn = (char*)"\x1B[36m";
char* g_vt_white = (char*)"\x1B[97m";
char* g_vt_magenta = (char*)"\x1B[35m";
char* g_vt_blue = (char*)"\x1B[34m";
#ifdef _WIN32
char* g_vt_back_enable = (char*)"\x1B[4m";
char* g_vt_back_default = (char*)"\x1B[24m";
#endif
#if defined(linux) || defined(__APPLE__)
char* g_vt_back_enable = (char*)"\x1B[100m";
char* g_vt_back_default = (char*)"\x1B[49m";
#endif
char* g_vt_default = (char*)"\x1B[0m";
char* g_vt_clear = (char*)"\x1B[2J";
char* g_vt_clear_remain = (char*)"\x1B[0J";
char* g_vt_clear_line = (char*)"\x1B[K";
char* g_vt_return_last_line = (char*)"\x1B[1A";
char* g_vt_home = (char*)"\x1B[H";
void clean_vt_color()
{
g_vt_red = (char*)"";
g_vt_green = g_vt_red;
g_vt_yellow = g_vt_red;
g_vt_kcyn = g_vt_red;
g_vt_white = g_vt_red;
g_vt_magenta = g_vt_red;
g_vt_blue = g_vt_red;
g_vt_back_enable = g_vt_red;
g_vt_back_default = g_vt_red;
g_vt_default = g_vt_red;
g_vt_clear = (char*)"\n";
g_vt_clear_remain = (char*)"\n";
g_vt_clear_line = (char*)"\n";
g_vt_home = (char*)"\n";
}
static void print_version()
{
printf("version %s\n", GIT_VERSION);
}
#if defined(linux)
char* shellcmd(char* cmd, char* buff, int size)
{
char temp[256];
FILE* fp = NULL;
int offset = 0;
int len;
fp = popen(cmd, "r");
if(fp == NULL)
{
return NULL;
}
while(fgets(temp, sizeof(temp), fp) != NULL)
{
len = strlen(temp);
if(offset + len < size)
{
strcpy(buff+offset, temp);
offset += len;
}
else
{
buff[offset] = 0;
break;
}
}
if(fp != NULL)
{
pclose(fp);
}
return buff;
}
#endif
static void upgrade_bcu(struct options_setting* setting)
{
printf("now version %s\n", GIT_VERSION);
int res = 0;
char version[15];
struct latest_git_info bcu_download_info;
strcpy(bcu_download_info.download_url_base, "https://github.com/nxp-imx/bcu/releases/download/");
if (setting->download_pre_release == 1)
{
if (https_get_by_url("https://api.github.com/repos/nxp-imx/bcu/releases", &bcu_download_info))
{
printf("Fail to get the latest pre-released BCU!\n");
return;
}
}
else
{
if (https_get_by_url("https://api.github.com/repos/nxp-imx/bcu/releases/latest", &bcu_download_info))
{
printf("Fail to get the latest released BCU!\n");
return;
}
}
https_response_parse(&bcu_download_info);
if (setting->download_doc)
{
strcpy(bcu_download_info.download_name, "BCU");
strcpy(bcu_download_info.extension_name, ".pdf");
https_download(&bcu_download_info);
return;
}
strcpy(bcu_download_info.download_name, "bcu");
#if defined(linux)
char sysversion[15], sys_ver = 0;
memset(sysversion, 0, sizeof(sysversion));
shellcmd("lsb_release -r -s", sysversion, sizeof(sysversion));
sys_ver = (sysversion[0] - '0') * 10 + (sysversion[1] - '0');
if (sys_ver == 20)
strcpy(bcu_download_info.extension_name, "_Ubuntu20");
else if (sys_ver == 18)
strcpy(bcu_download_info.extension_name, "_Ubuntu18");
else {
printf("Unsupported OS version!\n");
return;
}
#elif defined(__APPLE__)
strcpy(bcu_download_info.extension_name, "_mac");
#else
strcpy(bcu_download_info.extension_name, ".exe");
#endif
strncpy(version, GIT_VERSION, 11);
if (compare_version(&bcu_download_info.tag_name[4], &GIT_VERSION[4]) > 0 || setting->force)
{
printf("\nRelease Note for %s:\n%s\n\n", bcu_download_info.tag_name, bcu_download_info.release_note);
res = https_download(&bcu_download_info);
#if defined(linux) || defined(__APPLE__)
if (!res)
{
char cmd[50] = { 0 }, filename[25] = { 0 };
strcat(filename, bcu_download_info.tag_name);
// strcat(filename, bcu_download_info.extension_name);
sprintf(cmd, "chmod a+x %s", filename);
system(cmd);
}
#endif
}
else
{
printf("Latest release version is %s, no need to upgrade\n", bcu_download_info.tag_name);
}
}
static void print_help(char* cmd)
{
if (cmd == NULL) {
printf("%s\n", "Usage:");
printf("%s\n\n", "bcu command [-options]");
printf("%s\n", "list of available commands:");
printf(" %s%-60s%s%s\n", g_vt_default, "reset [BOOTMODE_NAME] [-hold=] [-board=/-auto] [-id=]", g_vt_green, "reset the board, and then boot from BOOTMODE_NAME");
printf(" %s%-60s%s%s\n", g_vt_default, " [-boothex=] [-bootbin=]", g_vt_green, "or the boot mode value set by [-boothex=] [-bootbin=]");
printf(" %s%-60s%s%s\n", g_vt_default, "onoff [-hold=] [-board=/-auto] [-id=]", g_vt_green, "press the ON/OFF button once for -hold= time(ms)");
printf(" %s%-60s%s%s\n", g_vt_default, "init [BOOTMODE_NAME] [-board=/-auto] [-id=]", g_vt_green, "enable the remote control with a boot mode");
printf(" %s%-60s%s%s\n", g_vt_default, "deinit [BOOTMODE_NAME] [-board=/-auto] [-id=]", g_vt_green, "disable the remote control");
printf("\n");
printf(" %s%-60s%s%s\n", g_vt_default, "monitor [-board=/-auto] [-id=]", g_vt_green, "monitor power consumption");
printf(" %s%-60s%s%s\n", g_vt_default, " [-dump/-dump=] [-nodisplay] [-pmt] [-stats]", g_vt_green, "");
printf(" %s%-60s%s%s\n", g_vt_default, " [-hz=] [-rms]", g_vt_green, "");
printf(" %s%-60s%s%s\n", g_vt_default, " [-hwfilter] [-unipolar]", g_vt_green, "");
printf(" %s%-60s%s%s\n", g_vt_default, " [-temp]", g_vt_green, "");
printf("\n");
printf(" %s%-60s%s%s\n", g_vt_default, "server [-board=/-auto] [-id=]", g_vt_green, "monitor power consumption");
printf(" %s%-60s%s%s\n", g_vt_default, " [-hwfilter] [-unipolar]", g_vt_green, "");
printf("\n");
printf(" %s%-60s%s%s\n", g_vt_default, "eeprom [-w] [-r] [-erase]", g_vt_green, "EEPROM read and program");
printf(" %s%-60s%s%s\n", g_vt_default, " [-wsn=] [-brev=] [-srev=]", g_vt_green, "");
printf("\n");
printf(" %s%-60s%s%s\n", g_vt_default, "temp [-board=/-auto] [-id=]", g_vt_green, "Get temperature value");
printf(" %s%-60s%s%s\n", g_vt_default, "get_level [GPIO_NAME] [-board=/-auto] [-id=]", g_vt_green, "get level state of pin GPIO_NAME");
printf(" %s%-60s%s%s\n", g_vt_default, "set_gpio [GPIO_NAME] [1/0] [-board=/-auto] [-id=]", g_vt_green, "set pin GPIO_NAME to be high(1) or low(0)");
printf(" %s%-60s%s%s\n", g_vt_default, "set_boot_mode [BOOTMODE_NAME] [-board=/-auto] [-id=]", g_vt_green, "set BOOTMODE_NAME as boot mode");
printf(" %s%-60s%s%s\n", g_vt_default, " [-boothex=] [-bootbin=]", g_vt_green, "");
printf(" %s%-60s%s%s\n", g_vt_default, "get_boot_mode [-board=/-auto] [-id=]", g_vt_green, "read the boot mode set by BCU before");
printf("\n");
printf(" %s%-60s%s%s\n", g_vt_default, "lsftdi", g_vt_green, "list all boards connected by ftdi device");
printf(" %s%-60s%s%s\n", g_vt_default, "lsboard", g_vt_green, "list all supported board models");
printf(" %s%-60s%s%s\n", g_vt_default, "lsbootmode [-board=/-auto]", g_vt_green, "show a list of available BOOTMODE_NAME of a board");
printf(" %s%-60s%s%s\n", g_vt_default, "lsgpio [-board=/-auto]", g_vt_green, "show a list of available GPIO_NAME of a board");
printf("\n");
printf(" %s%-60s%s%s\n", g_vt_default, "upgrade [-doc] [-f] [-pre]", g_vt_green, "get the latest BCU release");
#ifndef __APPLE__
printf(" %s%-60s%s%s\n", g_vt_default, "uuu [-doc]", g_vt_green, "download the latest UUU");
#endif
printf("\n");
printf(" %s%-60s%s%s\n", g_vt_default, "version", g_vt_green, "print version number");
printf(" %s%-60s%s%s%s\n", g_vt_default, "-h, help", g_vt_green, "show command details", g_vt_default);
printf(" %s%-60s%s%s%s\n", g_vt_default, "-cp, conf_path", g_vt_green, "show config file path", g_vt_default);
// printf(" %s%-60s%s%s%s\n", g_vt_default, "help [COMMAND_NAME]", g_vt_green, "show details and options of COMMAND_NAME", g_vt_default);
#if defined(__linux__) || defined(__APPLE__)
printf("%s", g_vt_kcyn);
printf("\n***please remember to run BCU with sudo or config the udev rules%s\n\n\n", g_vt_default);
#endif
}
else
{
printf("not yet added the detail explanation of each command\n");
}
}
static void lsgpio(struct options_setting* setting)
{
struct board_info* board = get_board(setting->board);
if (board == NULL)
return;
int i = 0;
printf("\navailable gpio:\n\n");
while (board->mappings[i].name != NULL)
{
if (board->mappings[i].type == gpio)
{
printf(" %s\n", board->mappings[i].name);
}
i++;
}
}
static void lsboard(struct options_setting* setting)
{
printf("\nlist of supported board model:\n\n");
for (int i = 0; i < num_of_boards; i++)
{
if (strcmp(setting->board, board_list[i].name) == 0)
{
printf("%s", g_vt_green);
}
printf(" %s", board_list[i].name);
if (strcmp(setting->board, board_list[i].name) == 0)
{
printf(" (default)");
}
printf("%s\n", g_vt_default);
}
return;
}
static int lsbootmode(struct options_setting* setting, int show_id)
{
struct board_info* board = get_board(setting->board);
if (board == NULL)
return -1;
int i = 0;
printf("\navailable boot mode:\n\n");
while (board->boot_modes[i].name != NULL)
{
if (show_id)
printf("%d %s\n", i, board->boot_modes[i].name);
else
printf(" %s\n", board->boot_modes[i].name);
i++;
}
return i;
}
struct gpio_device* get_gpio(char* gpio_name, struct board_info* board)
{
int i = 0;
char path[MAX_PATH_LENGTH];
void* head = NULL;
void* end_point;
struct gpio_device* gpio = NULL;
if (get_path(path, gpio_name, board) == -1)
return NULL;
end_point = build_device_linkedlist_forward(&head, path);
if (end_point == NULL)
{
printf("get_gpio: error building device linked list\n");
return NULL;
}
gpio = end_point;
return gpio;
}
int get_gpio_id(char* gpio_name, struct board_info* board)
{
int id = 0;
char path[MAX_PATH_LENGTH];
id = get_path(path, gpio_name, board);
return id;
}
int set_gpiod(struct gpio_device* gpio, int onoff)
{
int ret = 0;
if (gpio == NULL)
return -1;
if (gpio->active_level == 0)
{
if (onoff == 0)
ret = gpio->gpio_write(gpio, 0xFF);
else
ret = gpio->gpio_write(gpio, 0x00);
}
else
{
if (onoff == 0)
ret = gpio->gpio_write(gpio, 0x00);
else
ret = gpio->gpio_write(gpio, 0xFF);
}
return ret;
}
int get_gpiod(struct gpio_device* gpio, unsigned char* data)
{
int ret = 0;
unsigned char tmp = -1;
if (gpio == NULL)
return -1;
if (gpio->active_level == 0)
{
ret = gpio->gpio_get_output(gpio, &tmp);
if (ret < 0)
return ret;
if (tmp > 0)
*data = 0;
else
*data = 1;
}
else
ret = gpio->gpio_get_output(gpio, data);
return ret;
}
void free_gpio(struct gpio_device* gpio)
{
void* end_point = (void*)gpio;
free_device_linkedlist_backward(end_point);
gpio = NULL;
return;
}
static void get_temp(struct options_setting* setting)//
{
struct board_info* board = get_board(setting->board);
if (board == NULL)
return;
void* head = NULL;
void* end_point;
char path[MAX_PATH_LENGTH];
int status = -1;
if (strlen(setting->path) == 0)
{
if (get_path(path, "temp", board) == -1) {
printf("temperature: failed to find temperature path\n"); return;
}
end_point = build_device_linkedlist_forward(&head, path);
}
else
{
end_point = build_device_linkedlist_forward(&head, setting->path);
}
if (end_point == NULL)
{
printf("temperature: error building device linked list\n");
return;
}
struct temp_device* temp = end_point;
float degrees = 0;
temp->temp_enable(temp, 1);
degrees = temp->temp_read(temp);
temp->temp_enable(temp, 0);
printf("Temperature is %.3f Celsius.\n", degrees);
free_device_linkedlist_backward(end_point);
}
static void set_gpio(struct options_setting* setting)
{
struct board_info* board = get_board(setting->board);
if (board == NULL)
return;
void* head = NULL;
void* end_point;
char path[MAX_PATH_LENGTH];
int status = -1;
if (setting->output_state == -1)
{
printf("please enter a valid output state, 1 to set logic level high, 0 to set it low\n");
return;
}
if (strlen(setting->path) == 0)
{
if (strlen(setting->gpio_name) == 0)
{
printf("could not detect a valid gpio name entered\n");
printf("please enter the name of the gpio pin,\n");
printf("to see a list of available gpio pin, please use command lsgpio\n");
return;
}
if (get_path(path, setting->gpio_name, board) == -1) {
printf("failed to find gpio path\n"); return;
}
end_point = build_device_linkedlist_forward(&head, path);
}
else
{
end_point = build_device_linkedlist_forward(&head, setting->path);
}
if (end_point == NULL)
{
printf("set_gpio: error building device linked list\n");
return;
}
//delay
msleep(setting->delay);
struct gpio_device* gpio = end_point;
if (setting->output_state == 1)
status = gpio->gpio_write(gpio, 0xFF);
else if (setting->output_state == 0)
status = gpio->gpio_write(gpio, 0x00);
else if (setting->output_state == 2)
status = gpio->gpio_toggle(gpio);
if (status)
printf("set gpio failed, error = 0x%x\n", status);
else
printf("set gpio successfully\n");
//hold time
msleep(setting->hold);
free_device_linkedlist_backward(end_point);
}
static void get_gpio_level(struct options_setting* setting)
{
struct board_info* board = get_board(setting->board);
if (board == NULL)
return;
void* head = NULL;
void* end_point;
char path[MAX_PATH_LENGTH];
int status = -1;
unsigned char buff = 0;
if (strlen(setting->path) == 0)
{
if (strlen(setting->gpio_name) == 0)
{
printf("could not detect a valid gpio name entered\n");
printf("please enter the name of the gpio pin,\n");
printf("to see a list of available gpio pin, please use command lsgpio\n");
return;
}
if (get_path(path, setting->gpio_name, board) == -1) {
printf("failed to find gpio path\n"); return;
}
end_point = build_device_linkedlist_forward(&head, path);
}
else
{
end_point = build_device_linkedlist_forward(&head, setting->path);
}
if (end_point == NULL)
{
printf("get_gpio_level: error building device linked list\n");
return;
}
struct gpio_device* gpio = end_point;
status = gpio->gpio_read(gpio, &buff);
if (status)
printf("get gpio failed, error = 0x%x\n", status);
else
printf("get %s level=%s\n", setting->gpio_name, buff ? "HIGH" : "LOW");
free_device_linkedlist_backward(end_point);
}
static void set_boot_config(struct options_setting* setting)
{
struct board_info* board = get_board(setting->board);
if (board == NULL)
return;
struct gpio_device* gpio = NULL;
int status = -1;
for (int config_num = 0; config_num < board->boot_cfg_byte_num; config_num++)
{
if (setting->boot_config_hex[config_num] == -1)
{
printf("could not detect a valid boot_config_hex_%d!\n", config_num);
printf("set_boot_config failed\n");
return;
}
char cfg_str[10] = "boot_cfg";
char num_str[2] = "0";
num_str[0] += config_num;
strcat(cfg_str, num_str);
gpio = get_gpio(cfg_str, board);
if (gpio == NULL)
{
printf("set_boot_config: No boot_cfg%d configuration!\n", config_num);
return;
}
if (get_boot_mode_offset(gpio->pin_bitmask) < 0)
{
free_gpio(gpio);
return;
}
unsigned char hex_with_offset = setting->boot_config_hex[config_num] << (get_boot_mode_offset(gpio->pin_bitmask));
status = gpio->gpio_write(gpio, hex_with_offset);
if (status)
printf("set boot config %d failed, error = 0x%x\n", config_num, status);
else
printf("set boot config %d successfully\n", config_num);
free_gpio(gpio);
}
}
static void set_boot_mode(struct options_setting* setting)
{
if (setting->boot_mode_hex == -1)
{
printf("could not detect a valid boot_mode,\nplease entered a valid boot mode\n");
printf("set_boot_mode failed\n");
return;
}
struct board_info* board = get_board(setting->board);
if (board == NULL)
return;
struct gpio_device* gpio = NULL;
int status = -1;
gpio = get_gpio("boot_mode", board);
if (gpio == NULL)
{
printf("set_boot_mode: No boot_mode configuration!\n");
return;
}
if (get_boot_mode_offset(gpio->pin_bitmask) < 0)
{
free_gpio(gpio);
return;
}
unsigned char hex_with_offset = setting->boot_mode_hex << (get_boot_mode_offset(gpio->pin_bitmask));
status = gpio->gpio_write(gpio, hex_with_offset);
if (status)
printf("set boot mode failed, error = 0x%x\n", status);
else
printf("set boot mode successfully\n");
free_gpio(gpio);
if (board->boot_cfg_byte_num > 0)
set_boot_config(setting);
}
static void get_boot_config(struct options_setting* setting, unsigned char boot_modehex)
{
struct board_info* board = get_board(setting->board);
if (board == NULL)
return;
struct gpio_device* gpio = NULL;
int status = -1;
unsigned char read_buf;
int read_boot_config_hex[MAX_BOOT_CONFIG_BYTE] = { 0 };
for (int config_num = 0; config_num < board->boot_cfg_byte_num; config_num++)
{
char cfg_str[10] = "boot_cfg";
char num_str[2] = "0";
num_str[0] += config_num;
strcat(cfg_str, num_str);
gpio = get_gpio(cfg_str, board);
if (gpio == NULL)
{
printf("get_boot_config: No boot_mode configuration!\n");
free_gpio(gpio);
return;
}
if (get_boot_mode_offset(gpio->pin_bitmask) < 0)
{
free_gpio(gpio);
return;
}
status = gpio->gpio_read(gpio, &read_buf);
read_buf = read_buf >> get_boot_mode_offset(gpio->pin_bitmask);
if (status)
printf("get_boot_config %d failed, error = 0x%x\n", config_num, status);
else
read_boot_config_hex[config_num] = read_buf;
free_gpio(gpio);
}
char *bootmodestr = get_boot_config_name_from_hex(board, read_boot_config_hex, boot_modehex);
if (bootmodestr == NULL)
printf("get_boot_config: cannot find the boot config string.\n");
else
{
printf("get_boot_mode: %s%s%s, ",
g_vt_red, bootmodestr, g_vt_default);
printf("boot_mode_hex: %s0x%x%s, ",
g_vt_red, boot_modehex, g_vt_default);
for (int i = 0; i < board->boot_cfg_byte_num; i++)
printf("boot_config_%d_hex: %s0x%x%s\n", i,
g_vt_red, read_boot_config_hex[i], g_vt_default);
}
}
static void get_boot_mode(struct options_setting* setting)
{
struct board_info* board = get_board(setting->board);
if (board == NULL)
return;
struct gpio_device* gpio = NULL;
int status = -1;
unsigned char read_buf;
gpio = get_gpio("bootmode_sel", board);
if (gpio != NULL)
{
status = gpio->gpio_read(gpio, &read_buf);
if (status)
printf("get_boot_mode failed, error = 0x%x\n", status);
if (read_buf > 0)
read_buf = 1;
if (read_buf != (board->mappings[get_gpio_id("bootmode_sel", board)].initinfo & 0xF))
{
printf("get_boot_mode: bootmode_sel is disabled, boot from BOOT SWITCH!\n");
free_gpio(gpio);
return;
}
free_gpio(gpio);
}
else
{
gpio = get_gpio("remote_en", board);
if (gpio == NULL)
{
printf("get_boot_mode: Cannot find gpio remote_en!\n");
free_gpio(gpio);
return;
}
status = gpio->gpio_read(gpio, &read_buf);
if (status)
printf("get_boot_mode failed, error = 0x%x\n", status);
if (read_buf > 0)
read_buf = 1;
if (read_buf != (board->mappings[get_gpio_id("remote_en", board)].initinfo & 0xF))
{
printf("get_boot_mode: remote_en is disabled, boot from BOOT SWITCH!\n");
free_gpio(gpio);
return;
}
free_gpio(gpio);
}
gpio = get_gpio("boot_mode", board);
if (gpio == NULL)
{
printf("get_boot_mode: No boot_mode configuration!\n");
free_gpio(gpio);
return;
}
if (get_boot_mode_offset(gpio->pin_bitmask) < 0)
{
free_gpio(gpio);
return;
}
status = gpio->gpio_read(gpio, &read_buf);
read_buf = read_buf >> get_boot_mode_offset(gpio->pin_bitmask);
free_gpio(gpio);
if (status)
printf("get_boot_mode failed, error = 0x%x\n", status);
else
{
if (get_boot_mode_name_from_hex(board, read_buf) == NULL)
printf("get_boot_mode hex value: %s0x%x%s, cannot find the boot mode string.\n",
g_vt_red, read_buf, g_vt_default);
else
{
if (!board->boot_cfg_byte_num)
printf("get_boot_mode: %s%s%s, hex value: %s0x%x%s\n",
g_vt_red, get_boot_mode_name_from_hex(board, read_buf),
g_vt_default, g_vt_red, read_buf, g_vt_default);
else
get_boot_config(setting, read_buf);
}
}
}
static void deinitialize(struct options_setting* setting)
{
struct board_info* board = get_board(setting->board);
if (board == NULL)
return;
struct gpio_device* gpio = NULL;
int status = -1;
int mask;
gpio = get_gpio("remote_en", board);
if (gpio == NULL)
{
printf("deinitialize: Cannot find gpio remote_en!\n");
return;
}
mask = board->mappings[get_gpio_id("remote_en", board)].initinfo & 0xF;
status = gpio->gpio_write(gpio, mask ? 0x00 : 0xFF); //set it off.
if (!status)
printf("%sDISABLE%s remote control: remote_en\n", g_vt_red, g_vt_default);
free_gpio(gpio);
gpio = get_gpio("bootmode_sel", board);
if (gpio != NULL)
{
mask = board->mappings[get_gpio_id("bootmode_sel", board)].initinfo & 0xF;
status = gpio->gpio_write(gpio, mask ? 0x00 : 0xFF); //set it off.
if (!status)
printf("%sDISABLE%s remote control: bootmode_sel\n", g_vt_red, g_vt_default);
free_gpio(gpio);
}
gpio = get_gpio("ft_reset_boot_mode", board);
if (gpio != NULL)
{
status = gpio->gpio_write(gpio, 0x00);
msleep(50);
status = gpio->gpio_write(gpio, 0xFF);
if (!status)
printf("%sRESET%s boot mode pin: ft_reset_boot_mode\n", g_vt_red, g_vt_default);
free_gpio(gpio);
}
}
static int initialize(struct options_setting* setting, int isreset)
{
struct board_info* board = get_board(setting->board);
if (board == NULL)
return -1;
void* head = NULL;
void* end_point;
char path[MAX_PATH_LENGTH];
char name[MAX_MAPPING_NAME_LENGTH];
int status = 0, initid = 1, output = 0, k = 0;
while (output >= 0)
{
output = get_gpio_info_by_initid(name, path, initid, board);
if (output < 0)
{
if (!isreset)
printf("board initialization finished\n");
break;
}
if (strcmp(name, "boot_mode") == 0)
{
if (setting->boot_mode_hex != -1)
set_boot_mode(setting);
else
{
if (isreset == RESET_NOW)
{
printf("will boot by %sBOOT SWITCH%s\n", g_vt_yellow, g_vt_default);
}
else if (isreset == DONT_RESET)
{
printf("please give boot_mode, assuming 'sd' this time\n");
while (board->boot_modes[k].name != NULL)
{
if (strcmp(board->boot_modes[k].name, "sd") == 0)
{
setting->boot_mode_hex = board->boot_modes[k].boot_mode_hex;
break;
}
k++;
}
if (setting->boot_mode_hex != -1)
set_boot_mode(setting);
else
{
printf("could not recognize boot mode: sd, please give boot_mode\n");
printf("initialization failed\n");
return -1;
}
}
else
{
printf("will not set boot mode\n");
}
}
initid++;
continue;
}
end_point = build_device_linkedlist_forward(&head, path);
if (end_point == NULL)
{
printf("initialize: error building device linked list\n");
break;
}
struct gpio_device* gpio = end_point;
if (output)
status = gpio->gpio_write(gpio, 0xFF);
else
status = gpio->gpio_write(gpio, 0x00);
if (status)
{
printf("set %s %s failed, error = 0x%x\n", name, output ? "high" : "low", status);
printf("%sboard initialization failed!%s\n", g_vt_red, g_vt_default);
return -1;
}
else
{
if (strcmp(name, "remote_en") == 0)
printf("%sENABLE%s remote control\n", g_vt_green, g_vt_default);
else
printf("set %s %s successfully\n", name, output ? "high" : "low");
}
initid++;
free_device_linkedlist_backward(end_point);
}
return 0;
}
static void reset(struct options_setting* setting)
{
struct board_info* board = get_board(setting->board);
if (board == NULL)
return;
struct gpio_device* gpio = NULL;
int status = -1;
int a = 0, mask = 0;
char sr_name[100];
status = initialize(setting, RESET_NOW);
if (status < 0)
{
printf("%sboard reset failed!%s\n", g_vt_red, g_vt_default);
return;
}
printf("Set %sALL%s sense resistances to %ssmaller%s ones\n", g_vt_yellow, g_vt_default, g_vt_yellow, g_vt_default);
while (board->mappings[a].name != NULL)
{
if (board->mappings[a].type == power)
{
strcpy(sr_name, "SR_");
strcat(sr_name, board->mappings[a].name);
GET_GPIO:
gpio = get_gpio(sr_name, board);
if (gpio == NULL)
{
a++;
continue;
}
status = set_gpiod(gpio, 1); //set it active, use bigger range
free_gpio(gpio);
if (strncmp(sr_name, "SR_", 3) == 0)
{
strcpy(sr_name, "SRD_");
strcat(sr_name, board->mappings[a].name);
goto GET_GPIO;
}