-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathbcu.c
More file actions
4592 lines (4164 loc) · 120 KB
/
bcu.c
File metadata and controls
4592 lines (4164 loc) · 120 KB
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 <winsock2.h>
#include <ws2tcpip.h> // must be included before <windows.h>
#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 <limits.h>
#include "port.h"
#include "bcu_parser.h"
#include "chip.h"
#include "board.h"
#include "version.h"
#include "bcu_yaml.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[0;91m";
char* g_vt_dark_red = (char*)"\x1B[2;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 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=] [-keep]", 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=] [-keep]", 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(" %s%-60s%s%s\n", g_vt_default, " [-autoranging]", 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(" %s%-60s%s%s\n", g_vt_default, " [-w_ftdi_sn=] ", g_vt_green, "Change serial_no (max. 6 hexadecimal chars). Visible after USB replug.");
printf("\n");
printf(" %s%-60s%s%s\n", g_vt_default, "ptc_set_target [-ptc_temp] [-ptc_onoff] [-ptc_sensor]", g_vt_green, "Set PTC target temperature and control");
printf(" %s%-60s%s%s\n", g_vt_default, " [-board=/-auto] [-id=]", g_vt_green, "");
printf(" %s%-60s%s%s\n", g_vt_default, "ptc_set_curr_temp [-ptc_temp] [-board=/-auto] [-id=]", g_vt_green, "Set current PTC temperature if sensor chosen is 0");
printf(" %s%-60s%s%s\n", g_vt_default, "ptc_get_temp [-board=/-auto] [-id=]", g_vt_green, "Get current PTC temperature");
printf(" %s%-60s%s%s\n", g_vt_default, "ptc_get_is_stable [-board=/-auto] [-id=]", g_vt_green, "Get if PTC temperature is stable");
printf(" %s%-60s%s%s\n", g_vt_default, "ptc_get_is_enable [-board=/-auto] [-id=]", g_vt_green, "Get if PTC is enabled");
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, " [-cfg_addr=]", g_vt_green, "set start address to write from in RCON EEPROM (if target supports it)");
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, "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)
{
setting->no_release_pins = 1;
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)
{
setting->no_release_pins = 1;
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 ptc_set_target_temperature(struct options_setting *setting)
{
setting->no_release_pins = 1;
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, "ptc", 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 ptc_device *ptc = end_point;
unsigned char data_buffer[2] = {0};
if (setting->ptc_temp != -100)
{
float degree = 0;
unsigned short temperature = setting->ptc_temp * 100;
////////////////////////////////////////////////
data_buffer[1] = (char)(temperature >> 8);
data_buffer[0] = (char)temperature;
ptc->ptc_write(ptc, data_buffer, 0x00, 2); // set target temperature
degree = ptc->ptc_read(ptc, 0x00) / 100.0;
if (setting->ptc_temp != degree)
{
printf("Temperature cannot be set!\n");
exit(-1);
}
else
{
printf("Temperature was set to %.3f Celsius.\n", degree);
}
}
////////////////////////////////////////////////
if (setting->ptc_sensor != -1)
{
data_buffer[1] = (char)(setting->ptc_sensor >> 8);
data_buffer[0] = (char)setting->ptc_sensor;
ptc->ptc_write(ptc, data_buffer, 0x02, 2); // chose which sensor will be used in PID loop
}
////////////////////////////////////////////////
if (setting->ptc_onoff != -1)
{
if (setting->ptc_onoff == 1)
{
data_buffer[1] = 0;
data_buffer[0] = 1;
printf("PTC is power on!\n");
}
else
{
data_buffer[1] = 0;
data_buffer[0] = 0;
printf("PTC is power off!\n");
}
ptc->ptc_write(ptc, data_buffer, 0x03, 2); // activate the PID loop
}
free_device_linkedlist_backward(end_point);
}
static void ptc_set_current_temperature(struct options_setting *setting)
{
setting->no_release_pins = 1;
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, "ptc", 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 ptc_device *ptc = end_point;
unsigned char data_buffer[2] = {0};
float degree = 0;
unsigned short temperature = setting->ptc_temp * 100;
////////////////////////////////////////////////
data_buffer[1] = (char)(temperature >> 8);
data_buffer[0] = (char)temperature;
ptc->ptc_write(ptc, data_buffer, 0x01, 2); // set current temperature
free_device_linkedlist_backward(end_point);
}
static void ptc_get_temperature(struct options_setting *setting) //
{
setting->no_release_pins = 1;
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, "ptc", 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 ptc_device *ptc = end_point;
float degree = 0;
degree = ptc->ptc_read(ptc, 0x01) / 100;
printf("PTC Temperature is %.3f Celsius.\n", degree);
free_device_linkedlist_backward(end_point);
}
static void ptc_get_is_stable(struct options_setting *setting) //
{
setting->no_release_pins = 1;
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, "ptc", 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 ptc_device *ptc = end_point;
unsigned short is_stable = 0;
is_stable = ptc->ptc_read(ptc, 0x04);
if (is_stable == 1)
{
printf("PTC is stable!\r\n");
}
else
{
printf("PTC is not stable!\r\n");
}
free_device_linkedlist_backward(end_point);
}
static void ptc_get_is_enable(struct options_setting *setting) //
{
setting->no_release_pins = 1;
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, "ptc", 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 ptc_device *ptc = end_point;
unsigned short is_stable = 0;
is_stable = ptc->ptc_read(ptc, 0x03);
if (is_stable == 1)
{
printf("PTC is enable!\r\n");
}
else
{
printf("PTC is disable!\r\n");
}
free_device_linkedlist_backward(end_point);
}
static void get_temp(struct options_setting* setting)//
{
setting->no_release_pins = 1;
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("%s gpio set to %d successfully\n", setting->gpio_name, setting->output_state);
//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_gpio_boot_mode(struct options_setting* setting)
{
struct board_info* board = get_board(setting->board);
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 set_eeprom_boot_mode(struct options_setting* setting)
{
struct board_info* board = get_board(setting->board);
void* head = NULL;
struct eeprom_device* end_point;
char path[MAX_PATH_LENGTH] = "";
int i = 0, j = 0;
if (get_path(path, "boot_mode", board) == -1)
{
printf("Failed to find boot_mode path\n");
return;
}
end_point = (struct eeprom_device*)build_device_linkedlist_forward(&head, path);
while (board->boot_modes[i].name != NULL)
{
if (board->boot_modes[i].boot_mode_hex == setting->boot_mode_hex)
{
break;
}
i++;
}
unsigned char found = 0;
unsigned char* data_buffer;
unsigned int eeprom_start_address;
int data_buffer_size;
while (board->boot_configs[j].name != NULL)
{
if (strcmp(board->boot_configs[j].name, board->boot_modes[i].name) == 0)
{
if (setting->eeprom_cfg_addr == -1)
{
eeprom_start_address = board->boot_configs[j].boot_config_hex[0];
}
else
{
eeprom_start_address = setting->eeprom_cfg_addr;
}
if (strcmp(board->boot_configs[j].name, "custom") == 0)
{
data_buffer_size = setting->boot_config_hex_size;
data_buffer = malloc(data_buffer_size * sizeof(unsigned char));
for (int k = 0; k < data_buffer_size; k++)
{
*(data_buffer + k) = (unsigned char)setting->boot_config_hex[k];
if ( *(data_buffer + k) != setting->boot_config_hex[k] )
{
*(data_buffer + k) = (unsigned char)setting->boot_config_hex[k];
printf("%X must be hex. no. less or equal to 0XFF.", setting->boot_config_hex[k]);
}
}
}
else
{
data_buffer_size = board->boot_configs[j].actual_length - 1;
data_buffer = malloc(data_buffer_size * sizeof(unsigned char));
for (int k = 0; k < data_buffer_size; k++)
{
*(data_buffer + k) = board->boot_configs[j].boot_config_hex[k + 1];
}
}
end_point->eeprom_write(end_point, data_buffer, eeprom_start_address, data_buffer_size, data_buffer);
printf("boot_mode: %s", board->boot_modes[i].name);
printf("\n");
printf("addr: 0X%X", eeprom_start_address);
printf("\n");
printf("eeprom_data: ");
for (i = 0; i < data_buffer_size; i++)
{
printf("0X%X ", *(data_buffer + i));
}
free(data_buffer);
printf("\nPlease check data was written correctly using get_boot_mode option!\n");
break;
}
j++;
}
if (board->boot_configs[j].name == NULL) {
printf("set_boot_mode: No correct boot configuration provided for boot_mode %s!\n", board->boot_modes[i].name);
}
free_device_linkedlist_backward(end_point);
}
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;
if (get_mapping_type("boot_mode", board) == boot_eeprom)
{
set_eeprom_boot_mode(setting);
}
else {
set_gpio_boot_mode(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, ",