forked from bitmaintech/cgminer-ltc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver-btm-L3.c
3960 lines (3429 loc) · 115 KB
/
driver-btm-L3.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 "config.h"
#include <assert.h>
#include <limits.h>
#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/file.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#include <assert.h>
#include <unistd.h>
#include <math.h>
#ifndef WIN32
#include <sys/select.h>
#include <termios.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>
#ifndef O_CLOEXEC
#define O_CLOEXEC 0
#endif
#else
#include "compat.h"
#include <windows.h>
#include <io.h>
#endif
#include "elist.h"
#include "miner.h"
#include "usbutils.h"
#include "hexdump.c"
#include "util.h"
#include "driver-btm-L3.h"
#define SET_ASIC_RST_0 "echo 0 > /sys/class/gpio/gpio%d/value"
#define SET_ASIC_RST_1 "echo 1 > /sys/class/gpio/gpio%d/value"
struct thr_info *read_nonce_reg_id; // thread id for read nonce and register
struct thr_info *check_miner_status_id; // thread id for check system
struct thr_info *check_fan_id;
struct thr_info *read_temp_id;
struct thr_info *read_hash_rate;
struct thr_info *pic_heart_beat;
struct thr_info *scan_reg_id;
uint64_t h = 0;
int const plug[BITMAIN_MAX_CHAIN_NUM] = {51,48,47,44};
int const tty[BITMAIN_MAX_CHAIN_NUM] = {1,2,4,5};
int const beep = 20;
int const red_led = 45;
int const green_led = 23;
int const fan_speed[BITMAIN_MAX_FAN_NUM] = {112,110};
int const i2c_slave_addr[BITMAIN_MAX_CHAIN_NUM] = {0xa0,0xa2,0xa4,0xa6};
struct dev_info dev_info[BITMAIN_MAX_CHAIN_NUM];
int opt_bitmain_L3_freq = 100;
int opt_bitmain_L3_voltage = 176;
int opt_bitmain_fan_pwm = 30;
int last_temperature = 0, temp_highest = 0;
int8_t opt_bitmain_L3_core_temp = 2;
bool update_asic_num = false;
bool opt_bitmain_fan_ctrl = false;
bool need_recheck[BITMAIN_MAX_CHAIN_NUM] = {false, false, false, false};
extern void rev(unsigned char *s, size_t l);
extern void cg_logwork(struct work *work, unsigned char *nonce_bin, bool ok);
pthread_mutex_t i2c_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t work_queue_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t reg_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t nonce_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t reg_read_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t iic_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t tty_write_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t temp_buf_mutex = PTHREAD_MUTEX_INITIALIZER;
static bool start_send = false;
bool once_error = false;
bool status_error = false;
bool check_rate = false;
bool gBegin_get_nonce = false;
bool send_heart = true;
bool new_block[BITMAIN_MAX_CHAIN_NUM] = {false, false, false, false};
uint64_t rate[BITMAIN_MAX_CHAIN_NUM] = {0};
int rate_error[BITMAIN_MAX_CHAIN_NUM] = {0};
char displayed_rate[BITMAIN_MAX_CHAIN_NUM][16];
unsigned char pic_version[BITMAIN_MAX_CHAIN_NUM] = {0};
#define FANINT 1
#define FAN0 "256:"
#define FAN1 "254:"
#define PROCFILENAME "/proc/interrupts"
struct nonce_buf nonce_fifo;
struct reg_buf reg_fifo;
struct all_parameters dev;
struct timeval tv_send_job = {0, 0};
static int g_gpio_data[BITMAIN_MAX_CHAIN_NUM] = {5, 4, 27, 22};
speed_t tiospeed_t(int baud)
{
switch (baud)
{
case 9600:
return B9600;
case 19200:
return B19200;
case 38400:
return B38400;
case 57600:
return B57600;
case 115200:
return B115200;
case 230400:
return B230400;
case 460800:
return B460800;
case 921600:
return B921600;
case 3000000:
return B3000000;
default:
return B0;
}
}
/** CRC table for the CRC ITU-T V.41 0x0x1021 (x^16 + x^12 + x^5 + 1) */
const uint16_t crc_itu_t_table[256] =
{
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
};
static inline uint16_t crc_itu_t_byte(uint16_t crc, const uint8_t data)
{
return (crc << 8) ^ crc_itu_t_table[((crc >> 8) ^ data) & 0xff];
}
static unsigned char TempChipAddr[BITMAIN_MAX_TEMP_CHIP_NUM] = {HAVE_TEMP, HAVE_TEMP_2, HAVE_TEMP_3};
typedef enum
{
BUF_START,
BUF_READY,
BUF_READING,
BUF_WRITING,
BUF_IDLE
} BUF_STATE_E;
typedef struct
{
BUF_STATE_E state;
unsigned char data;
} ASIC_TEMP_T;
static ASIC_TEMP_T TempBuffer[BITMAIN_MAX_CHAIN_NUM][BITMAIN_MAX_TEMP_CHIP_NUM] = {{{0},{0}}};
uint16_t crc_itu_t(uint16_t crc, const uint8_t *buffer, int len)
{
while (len--)
crc = crc_itu_t_byte(crc, *buffer++);
return crc;
}
unsigned char CRC5(unsigned char *ptr, unsigned char len)
{
unsigned char i, j, k;
unsigned char crc = 0x1f;
unsigned char crcin[5] = {1, 1, 1, 1, 1};
unsigned char crcout[5] = {1, 1, 1, 1, 1};
unsigned char din = 0;
j = 0x80;
k = 0;
for (i = 0; i < len; i++)
{
if (*ptr & j)
{
din = 1;
}
else
{
din = 0;
}
crcout[0] = crcin[4] ^ din;
crcout[1] = crcin[0];
crcout[2] = crcin[1] ^ crcin[4] ^ din;
crcout[3] = crcin[2];
crcout[4] = crcin[3];
j = j >> 1;
k++;
if (k == 8)
{
j = 0x80;
k = 0;
ptr++;
}
memcpy(crcin, crcout, 5);
}
crc = 0;
if(crcin[4])
{
crc |= 0x10;
}
if(crcin[3])
{
crc |= 0x08;
}
if(crcin[2])
{
crc |= 0x04;
}
if(crcin[1])
{
crc |= 0x02;
}
if(crcin[0])
{
crc |= 0x01;
}
return crc;
}
int L3_write(int fd, const void *buf, size_t bufLen)
{
size_t ret;
if (unlikely(fd == -1))
ret = -1;
// mutex_lock(&tty_write_mutex);
flock(fd,LOCK_EX);
ret = write(fd, buf, bufLen);
// mutex_unlock(&tty_write_mutex);
if(unlikely(ret != bufLen))
{
applog(LOG_ERR,"write error!!");
}
flock(fd,LOCK_UN);
cgsleep_us(500);
return ret;
}
int L3_read(int fd, unsigned char *buf, size_t bufLen)
{
size_t ret;
if (unlikely(fd == -1))
ret = -1;
ret = read(fd, buf, bufLen);
if(unlikely(ret != bufLen))
applog(LOG_ERR,"read error!!");
return ret;
}
static void get_bitmain_statline_before(char *buf, size_t bufsiz, struct cgpu_info *bitmain_L3)
{
}
static void reset_asic()
{
char rstBuf[128] = "";
int i = 0;
for (i = 0; i < BITMAIN_MAX_CHAIN_NUM; ++i )
{
sprintf(rstBuf, SET_ASIC_RST_0, g_gpio_data[i]);
system(rstBuf);
}
cgsleep_ms(500);
for (i = 0; i < BITMAIN_MAX_CHAIN_NUM; ++i )
{
sprintf(rstBuf, SET_ASIC_RST_1, g_gpio_data[i]);
system(rstBuf);
}
}
static void suffix_string_L3(uint64_t val, char *buf, size_t bufsiz, int sigdigits,bool display)
{
const double dkilo = 1000.0;
const uint64_t kilo = 1000ull;
const uint64_t mega = 1000000ull;
const uint64_t giga = 1000000000ull;
char suffix[2] = "";
bool decimal = true;
double dval;
/*
if (val >= exa)
{
val /= peta;
dval = (double)val / dkilo;
strcpy(suffix, "E");
}
else if (val >= peta)
{
val /= tera;
dval = (double)val / dkilo;
strcpy(suffix, "P");
}
else if (val >= tera)
{
val /= giga;
dval = (double)val / dkilo;
strcpy(suffix, "T");
}
else */if (val >= giga)
{
val /= mega;
dval = (double)val / dkilo;
strcpy(suffix, "G");
}
else if (val >= mega)
{
val /= kilo;
dval = (double)val / dkilo;
strcpy(suffix, "M");
}
else if (val >= kilo)
{
dval = (double)val / dkilo;
strcpy(suffix, "K");
}
else
{
dval = val;
decimal = false;
}
if (!sigdigits)
{
if (decimal)
snprintf(buf, bufsiz, "%.3g%s", dval, suffix);
else
snprintf(buf, bufsiz, "%d%s", (unsigned int)dval, suffix);
}
else
{
/* Always show sigdigits + 1, padded on right with zeroes
* followed by suffix */
int ndigits = sigdigits - 1 - (dval > 0.0 ? floor(log10(dval)) : 0);
if(display)
snprintf(buf, bufsiz, "%*.*f%s", sigdigits + 1, ndigits, dval, suffix);
else
snprintf(buf, bufsiz, "%*.*f", sigdigits + 1, ndigits, dval);
}
}
void clear_register_value_buf()
{
pthread_mutex_lock(®_mutex);
reg_fifo.p_wr = 0;
reg_fifo.p_rd = 0;
reg_fifo.reg_value_num = 0;
pthread_mutex_unlock(®_mutex);
}
void *bitmain_scanhash(void *arg)
{
struct thr_info *thr = (struct thr_info*)arg;
struct cgpu_info *bitmain_L3 = thr->cgpu;
struct bitmain_L3_info *info = bitmain_L3->device_data;
struct timeval current;
bool loged = false;
uint8_t nonce_bin[4],crc_check,which_asic_nonce;
uint32_t nonce;
int submitfull = 0;
bool submitnonceok = true;
struct work *work = NULL;
cgtime(¤t);
h = 0;
pthread_mutex_lock(&nonce_mutex);
cg_rlock(&info->update_lock);
while(nonce_fifo.nonce_num)
{
nonce_fifo.nonce_num--;
crc_check = CRC5((uint8_t *)&(nonce_fifo.nonce_buffer[nonce_fifo.p_rd]), 7*8-5);
if(crc_check != (nonce_fifo.nonce_buffer[nonce_fifo.p_rd].crc5 & 0x1f))
{
applog(LOG_ERR,"crc5 error,should be %02x,but check as %02x",nonce_fifo.nonce_buffer[nonce_fifo.p_rd].crc5 & 0x1f,crc_check);
applog(LOG_NOTICE,"get nonce %02x%02x%02x%02x wc %02x diff %02x crc5 %02x chainid %02x",nonce_fifo.nonce_buffer[nonce_fifo.p_rd].nonce[0], \
nonce_fifo.nonce_buffer[nonce_fifo.p_rd].nonce[1],nonce_fifo.nonce_buffer[nonce_fifo.p_rd].nonce[2], \
nonce_fifo.nonce_buffer[nonce_fifo.p_rd].nonce[3],nonce_fifo.nonce_buffer[nonce_fifo.p_rd].diff, \
nonce_fifo.nonce_buffer[nonce_fifo.p_rd].wc, nonce_fifo.nonce_buffer[nonce_fifo.p_rd].crc5, \
nonce_fifo.nonce_buffer[nonce_fifo.p_rd].chainid);
//if signature enabled,check SIG_INFO register
goto crc_error;
}
memcpy(nonce_bin,nonce_fifo.nonce_buffer[nonce_fifo.p_rd].nonce,4);
uint8_t work_id = nonce_fifo.nonce_buffer[nonce_fifo.p_rd].wc;
uint8_t chain_id = nonce_fifo.nonce_buffer[nonce_fifo.p_rd].chainid;
memcpy((uint8_t *)&nonce,nonce_bin,4);
nonce = htobe32(nonce);
pthread_mutex_lock(&work_queue_mutex);
work = info->work_queue[work_id];
pthread_mutex_unlock(&work_queue_mutex);
if(work)
{
submitfull = 0;
if(submit_nonce_1(thr, work, nonce, &submitfull))
{
submitnonceok = true;
submit_nonce_2(work);
}
else
{
if(submitfull)
{
submitnonceok = true;
}
else
{
submitnonceok = false;
if ( chain_id > BITMAIN_MAX_CHAIN_NUM ) applog( LOG_ERR, "Chain_ID [%d] Error!", chain_id);
dev.chain_hw[chain_id] ++;
}
}
if(submitnonceok)
{
which_asic_nonce = (((nonce >> (20)) & 0xff) / dev.addrInterval);
applog(LOG_DEBUG,"%s: chain %d which_asic_nonce %d ", __FUNCTION__, chain_id, which_asic_nonce);
if ((chain_id > BITMAIN_MAX_CHAIN_NUM) || (!dev.chain_exist[chain_id]))
{
if(!loged)
{
applog(LOG_ERR, "ChainID Cause Error! ChainID:[%d]", chain_id);
loged = true;
}
goto crc_error;
}
if ( which_asic_nonce >= CHAIN_ASIC_NUM )
{
applog(LOG_ERR, "Which Nonce Cause Err![%d]", which_asic_nonce);
goto crc_error;
}
h += 0x1UL << DEVICE_DIFF;
dev.chain_asic_nonce[chain_id][which_asic_nonce]++;
}
cg_logwork(work, nonce_bin, submitnonceok);
}
else
{
applog(LOG_ERR, "%s%d: work %02x not find error", bitmain_L3->drv->name, bitmain_L3->device_id, work_id);
}
crc_error:
if(nonce_fifo.p_rd < MAX_NONCE_NUMBER_IN_FIFO - 1)
{
nonce_fifo.p_rd++;
}
else
{
nonce_fifo.p_rd = 0;
}
}
cg_runlock(&info->update_lock);
pthread_mutex_unlock(&nonce_mutex);
cgsleep_ms(1);
if(h != 0)
{
applog(LOG_DEBUG,"%s: hashes %"PRIu64"...", __FUNCTION__,h * 0x0000ffffull);
}
h = h * 0x0000ffffull;
return 0;
}
static int64_t bitmain_L3_scanhash(struct thr_info *thr)
{
h = 0;
pthread_t send_id;
pthread_create(&send_id, NULL, bitmain_scanhash, (void*)thr);
pthread_join(send_id, NULL);
return h;
}
void set_led(bool stop)
{
static bool blink = true;
char cmd[100];
blink = !blink;
if(stop)
{
sprintf(cmd,LED_CTRL_TEMPLATE,0,green_led);
system(cmd);
sprintf(cmd,LED_CTRL_TEMPLATE,(blink)?1:0,red_led);
system(cmd);
}
else
{
sprintf(cmd,LED_CTRL_TEMPLATE,0,red_led);
system(cmd);
sprintf(cmd,LED_CTRL_TEMPLATE,(blink)?1:0,green_led);
system(cmd);
}
}
void set_PWM(unsigned char pwm_percent)
{
int temp_pwm_percent = 0;
char buf[128];
temp_pwm_percent = pwm_percent;
if(temp_pwm_percent < MIN_PWM_PERCENT)
{
temp_pwm_percent = MIN_PWM_PERCENT;
}
if(temp_pwm_percent > MAX_PWM_PERCENT)
{
temp_pwm_percent = MAX_PWM_PERCENT;
}
dev.duty_ns = PWM_PERIOD_NS * temp_pwm_percent /100;
dev.pwm_percent = temp_pwm_percent;
applog(LOG_DEBUG,"set pwm duty_ns %d",dev.duty_ns);
sprintf(buf,PWM_CTRL_TEMPLATE,dev.duty_ns);
system(buf);
}
void set_PWM_according_to_temperature()
{
int pwm_percent = 0, temp_change = 0;
temp_highest = dev.temp_top1;
if(temp_highest >= MAX_FAN_TEMP)
{
applog(LOG_DEBUG,"%s: Temperature is higher than %d 'C", __FUNCTION__, temp_highest);
}
if(dev.fan_eft)
{
if((dev.fan_pwm >= 0) && (dev.fan_pwm <= 100))
{
set_PWM(dev.fan_pwm);
return;
}
}
temp_change = temp_highest - last_temperature;
if(temp_highest >= MAX_FAN_TEMP || temp_highest == 0)
{
set_PWM(MAX_PWM_PERCENT);
dev.fan_pwm = MAX_PWM_PERCENT;
applog(LOG_DEBUG,"%s: Set PWM percent : MAX_PWM_PERCENT", __FUNCTION__);
return;
}
if(temp_highest <= MIN_FAN_TEMP)
{
set_PWM(MIN_PWM_PERCENT);
dev.fan_pwm = MIN_PWM_PERCENT;
applog(LOG_DEBUG,"%s: Set PWM percent : MIN_PWM_PERCENT", __FUNCTION__);
return;
}
if(temp_change >= TEMP_INTERVAL || temp_change <= -TEMP_INTERVAL)
{
pwm_percent = MIN_PWM_PERCENT + (temp_highest -MIN_FAN_TEMP) * PWM_ADJUST_FACTOR;
if(pwm_percent < 0)
{
pwm_percent = 0;
}
dev.fan_pwm = pwm_percent;
applog(LOG_DEBUG,"%s: Set PWM percent : %d", __FUNCTION__, pwm_percent);
set_PWM(pwm_percent);
last_temperature = temp_highest;
}
}
static inline void send_pic_command()
{
uint8_t cmd[2];
cmd[0] = PIC_COMMAND_1;
cmd[1] = PIC_COMMAND_2;
write(dev.i2c_fd,&cmd[0],1);
cgsleep_us(200);// need ?
write(dev.i2c_fd,&cmd[1],1);
cgsleep_us(200);// need ?
}
void pic_dac_ctrl(uint8_t value)
{
uint8_t cmd[2];
cmd[0] = ENABLE_VOLTAGE;
cmd[1] = value;
send_pic_command();
write(dev.i2c_fd,&cmd[0],1);
cgsleep_us(200);
write(dev.i2c_fd,&cmd[1],1);
cgsleep_us(200);
}
void set_beep(bool flag)
{
char cmd[128] = {0};
sprintf(cmd,BEEP_CTRL_TEMPLATE,flag?1:0,beep);
system(cmd);
}
static unsigned int getNum(const char* buffer)
{
char* pos = strstr(buffer, ":");
while(*(++pos) == ' ');
char* startPos = pos;
while(*(++pos) != ' ');
*pos = '\0';
return (atoi(startPos));
}
void check_fan_speed(void)
{
uint32_t fan0SpeedHist = 0;
uint32_t fan1SpeedHist = 0;
uint32_t fan0SpeedCur = 0;
uint32_t fan1SpeedCur = 0;
uint32_t fan0Speed = 0, fan0Speed_ok = 0;
uint32_t fan1Speed = 0, fan1Speed_ok = 0;
uint32_t fan0_exist = 0;
uint32_t fan1_exist = 0;
unsigned char ok_counter = 0;
char buffer[256] = "";
char* pos = NULL;
FILE* fanpfd = fopen(PROCFILENAME, "r");
if(fanpfd == NULL)
{
while(1)
{
applog(LOG_ERR, "open /proc/interrupt error");
sleep(3);
}
}
fseek(fanpfd, 0, SEEK_SET);
while(fgets(buffer, 256, fanpfd))
{
if ( ((pos = strstr(buffer, FAN0)) != 0) && (strstr(buffer, "gpiolib") != 0 ) )
{
applog(LOG_DEBUG, "find fan1.");
fan0SpeedHist = fan0SpeedCur = getNum(buffer);
}
if (((pos = strstr(buffer, FAN1)) != 0) && (strstr(buffer, "gpiolib") != 0 ))
{
applog(LOG_DEBUG, "find fan2.");
fan1SpeedHist = fan1SpeedCur = getNum(buffer);
}
}
cgsleep_ms(500);
while(1)
{
applog(LOG_DEBUG, "test_loop = %d", ok_counter);
fseek(fanpfd, 0, SEEK_SET);
while(fgets(buffer, 256, fanpfd))
{
if ( ((pos = strstr(buffer, FAN0)) != 0) && (strstr(buffer, "gpiolib") != 0 ) )
{
applog(LOG_DEBUG, "find fan1");
fan0SpeedCur = getNum(buffer);
if (fan0SpeedHist > fan0SpeedCur)
{
fan0Speed = (0xffffffff - fan0SpeedHist + fan0SpeedCur);
}
else
{
fan0Speed = ( fan0SpeedCur - fan0SpeedHist );
}
fan0Speed = fan0Speed * 60 / 2 * 2; // 60: 1 minute; 2: 1 interrupt has 2 edges; FANINT: check fan speed every FANINT senconds
fan0SpeedHist = fan0SpeedCur;
applog(LOG_DEBUG, "fan1Speed = %d", fan0Speed);
if( fan0Speed > FAN1_MAX_SPEED * FAN_SPEED_OK_PERCENT)
{
fan0Speed_ok++;
}
}
if (((pos = strstr(buffer, FAN1)) != 0) && (strstr(buffer, "gpiolib") != 0 ))
{
applog(LOG_DEBUG, "find fan2");
fan1SpeedCur = getNum(buffer);
if (fan1SpeedHist > fan1SpeedCur)
{
fan1Speed = (0xffffffff - fan1SpeedHist + fan1SpeedCur);
}
else
{
fan1Speed = ( fan1SpeedCur - fan1SpeedHist );
}
fan1SpeedHist = fan1SpeedCur;
fan1Speed = fan1Speed * 60 / 2 * 2; // 60: 1 minute; 2: 1 interrupt has 2 edges; FANINT: check fan speed every FANINT senconds
fan1SpeedHist = fan1SpeedCur;
applog(LOG_DEBUG, "fan2Speed = %d", fan1Speed);
if( fan1Speed > FAN2_MAX_SPEED * FAN_SPEED_OK_PERCENT)
{
fan1Speed_ok++;
}
}
}
if((fan0Speed_ok >= 3) && (fan1Speed_ok >= 3))
{
applog(LOG_WARNING, "%s OK", __FUNCTION__);
return;
}
cgsleep_ms(500);
}
}
void *check_fan_thr(void *arg)
{
uint32_t fan0SpeedHist = 0;
uint32_t fan1SpeedHist = 0;
uint32_t fan0SpeedCur = 0;
uint32_t fan1SpeedCur = 0;
uint32_t fan0Speed = 0;
uint32_t fan1Speed = 0;
uint32_t fan0_exist = 0;
uint32_t fan1_exist = 0;
FILE* fanpfd = fopen(PROCFILENAME, "r");
while( 1 )
{
fseek(fanpfd, 0, SEEK_SET);
char buffer[256] = "";
char* pos = NULL;
while( fgets(buffer, 256, fanpfd) )
{
if ( ((pos = strstr(buffer, FAN0)) != 0) && (strstr(buffer, "gpiolib") != 0 ) )
{
fan0SpeedCur = getNum(buffer);
if (fan0SpeedHist > fan0SpeedCur)
{
fan0Speed = (0xffffffff - fan0SpeedHist + fan0SpeedCur);
}
else
{
fan0Speed = ( fan0SpeedCur - fan0SpeedHist );
}
fan0Speed = fan0Speed* 60 / 2 / FANINT;
if ( fan0Speed )
{
fan0_exist = 1;
if( fan0Speed > 6600)
fan0Speed = 6600;
}
else
{
fan0_exist = 0;
}
dev.fan_speed_value[0] = fan0Speed;
fan0SpeedHist = fan0SpeedCur;
//if (dev.fan_speed_top1 < fan0Speed)
{
dev.fan_speed_top1 = fan0Speed;
}
}
else if (((pos = strstr(buffer, FAN1)) != 0) && (strstr(buffer, "gpiolib") != 0 ))
{
fan1SpeedCur = getNum(buffer);
if (fan1SpeedHist > fan1SpeedCur)
{
fan1Speed = (0xffffffff - fan1SpeedHist + fan1SpeedCur);
}
else
{
fan1Speed = ( fan1SpeedCur - fan1SpeedHist );
}
fan1SpeedHist = fan1SpeedCur;
fan1Speed = fan1Speed* 60 / 2 / FANINT;
if ( fan1Speed )
{
fan1_exist = 1;
if( fan1Speed > 6600)
fan1Speed = 6600;
}
else
{
fan1_exist = 0;
}
dev.fan_speed_value[1] = fan1Speed;
{
dev.fan_speed_low1 = fan1Speed;
}
}
}
//dev.fan_speed_low1 = (fan1Speed > fan0Speed) ? fan0Speed : fan1Speed;
dev.fan_num = fan1_exist + fan0_exist;
sleep(FANINT);
}
}
int fan_error_num = 0;
inline int check_fan_ok()
{
int ret = 0;
if(dev.fan_num < MIN_FAN_NUM)
{
ret = 1;
goto err;
}
if(dev.fan_speed_top1 < (FAN1_MAX_SPEED * dev.fan_pwm / 130))
{
ret = 2;
goto err;
}
if(dev.fan_speed_low1 < (FAN2_MAX_SPEED * dev.fan_pwm / 130))
{
ret = 3;
goto err;
}
if((dev.pwm_percent == 100) && (dev.fan_speed_top1 < (FAN1_MAX_SPEED * 90 / 100) || dev.fan_speed_low1 < (FAN2_MAX_SPEED * 90 / 100)))
{
ret = 4;
goto err;
}
err:
if(ret != 0)
{
fan_error_num++;
if(fan_error_num > (FANINT * 10))
return ret;
}
else
{
fan_error_num = 0;
return 0;
}
}
void *check_miner_status(void *arg)
{
//asic status,fan,led
struct timeval tv_start = {0, 0}, tv_end,tv_send;
double ghs = 0;
int i = 0, j = 0, fan_ret;
bool loged = false;
cgtime(&tv_end);
cgtime(&tv_send);
copy_time(&tv_start, &tv_end);
copy_time(&tv_send_job,&tv_send);
bool stop = false;
int asic_num = 0, error_asic = 0, avg_num = 0;
while(1)
{
struct timeval diff;
cgtime(&tv_end);
cgtime(&tv_send);
timersub(&tv_end, &tv_start, &diff);
if (diff.tv_sec > 600)
{
asic_num = 0, error_asic = 0, avg_num = 0;
for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
{
if(dev.chain_exist[i])
{
asic_num += dev.chain_asic_num[i];
for(j=0; j<dev.chain_asic_num[i]; j++)
{
avg_num += dev.chain_asic_nonce[i][j];
applog(LOG_DEBUG,"%s: chain %d asic %d asic_nonce_num %d", __FUNCTION__, i,j,dev.chain_asic_nonce[i][j]);
}
}
}
if (asic_num != 0)
{
applog(LOG_DEBUG,"%s: avg_num %d asic_num %d", __FUNCTION__, avg_num,asic_num);
avg_num = avg_num / asic_num / 8;
avg_num = 0;
}
else
{
avg_num = 1;
}
for(i=0; i<BITMAIN_MAX_CHAIN_NUM; i++)
{
if(dev.chain_exist[i])
{
int offset = 0;
for(j=0; j<dev.chain_asic_num[i]; j++)
{
if(j%8 == 0)
{
if ( ( j + offset ) > (CHAIN_ASIC_NUM + 16) )
applog(LOG_ERR, "asic num err![%d]", (j + offset));
dev.chain_asic_status_string[i][j+offset] = ' ';
offset++;
}
if(dev.chain_asic_nonce[i][j]>avg_num)
{
if ( ( j +offset ) > (CHAIN_ASIC_NUM + 16) )
applog(LOG_ERR, "asic num err![%d]", (j + offset));
dev.chain_asic_status_string[i][j+offset] = 'o';
}
else
{
if ( ( j + offset ) > (CHAIN_ASIC_NUM + 16) )
applog(LOG_ERR, "asic num err![%d]", (j + offset));
dev.chain_asic_status_string[i][j+offset] = 'x';
error_asic++;
}
if ( ( j ) > (CHAIN_ASIC_NUM + 16) )
applog(LOG_ERR, "asic num err![%d]", (j));
dev.chain_asic_nonce[i][j] = 0;