driver-avalon.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright 2013 Xiangfu <xiangfu@openmobilefree.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #ifndef AVALON_H
  10. #define AVALON_H
  11. struct avalon_task {
  12. uint8_t reset :1;
  13. uint8_t flush_fifo :1;
  14. uint8_t fan_eft :1;
  15. uint8_t timer_eft :1;
  16. uint8_t chip_num :4;
  17. uint8_t fan_pwm_data;
  18. uint8_t timeout_data;
  19. uint8_t miner_num; // Word[0]
  20. uint8_t nonce_elf :1;
  21. uint32_t pad0_miner_ctrl :31;
  22. uint32_t pad1_miner_ctrl; //Word[2:1]
  23. uint32_t midstate[8];
  24. uint32_t data[3];
  25. // nonce_range: Word[??:14]
  26. } __attribute__((packed));
  27. struct avalon_result {
  28. uint32_t nonce;
  29. uint32_t data[3];
  30. uint32_t midstate[8];
  31. uint32_t reserved[2];
  32. } __attribute__((packed));
  33. struct AVALON_HISTORY {
  34. struct timeval finish;
  35. double sumXiTi;
  36. double sumXi;
  37. double sumTi;
  38. double sumXi2;
  39. uint32_t values;
  40. uint32_t hash_count_min;
  41. uint32_t hash_count_max;
  42. };
  43. enum timing_mode { MODE_DEFAULT, MODE_SHORT, MODE_LONG, MODE_VALUE };
  44. #define INFO_HISTORY 10
  45. struct AVALON_INFO {
  46. // time to calculate the golden_ob
  47. uint64_t golden_hashes;
  48. struct timeval golden_tv;
  49. struct AVALON_HISTORY history[INFO_HISTORY+1];
  50. uint32_t min_data_count;
  51. // seconds per Hash
  52. double Hs;
  53. int read_count;
  54. enum timing_mode timing_mode;
  55. bool do_avalon_timing;
  56. double fullnonce;
  57. int count;
  58. double W;
  59. uint32_t values;
  60. uint64_t hash_count_range;
  61. // Determine the cost of history processing
  62. // (which will only affect W)
  63. uint64_t history_count;
  64. struct timeval history_time;
  65. // avalon-options
  66. int baud;
  67. int work_division;
  68. int asic_count;
  69. uint32_t nonce_mask;
  70. };
  71. #define AVALON_MINER_THREADS 1
  72. #define AVALON_GET_WORK_COUNT 20
  73. #define AVALON_IO_SPEED 115200
  74. #define AVALON_SEND_WORK_PITCH 8000
  75. #define AVALON_WRITE_SIZE (sizeof(struct avalon_task))
  76. #define AVALON_READ_SIZE (sizeof(struct avalon_result))
  77. // Ensure the sizes are correct for the Serial read
  78. #define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1]
  79. ASSERT1(sizeof(uint32_t) == 4);
  80. #define AVALON_READ_TIME(baud) ((double)AVALON_READ_SIZE * (double)8.0 / (double)(baud))
  81. // Fraction of a second, USB timeout is measured in
  82. // i.e. 10 means 1/10 of a second
  83. #define TIME_FACTOR 10
  84. // It's 10 per second, thus value = 10/TIME_FACTOR =
  85. #define AVALON_RESET_FAULT_DECISECONDS 1
  86. // In timing mode: Default starting value until an estimate can be obtained
  87. // 5 seconds allows for up to a ~840MH/s device
  88. #define AVALON_READ_COUNT_TIMING (5 * TIME_FACTOR)
  89. // For a standard Avalon REV3 (to 5 places)
  90. // Since this rounds up a the last digit - it is a slight overestimate
  91. // Thus the hash rate will be a VERY slight underestimate
  92. // (by a lot less than the displayed accuracy)
  93. #define AVALON_REV3_HASH_TIME 0.0000000026316
  94. #define NANOSEC 1000000000.0
  95. #define HISTORY_SEC 60
  96. // Minimum how many points a single AVALON_HISTORY should have
  97. #define MIN_DATA_COUNT 5
  98. // The value above used is doubled each history until it exceeds:
  99. #define MAX_MIN_DATA_COUNT 100
  100. #define END_CONDITION 0x0000ffff
  101. #define AVA_GETS_ERROR -1
  102. #define AVA_GETS_OK 0
  103. #define AVA_GETS_RESTART 1
  104. #define AVA_GETS_TIMEOUT 2
  105. #define AVA_GETS_DONE 3
  106. #define AVA_SEND_ERROR -1
  107. #define AVA_SEND_OK 0
  108. #define AVA_SEND_FULL 1
  109. #define avalon_open2(devpath, baud, purge) serial_open(devpath, baud, AVALON_RESET_FAULT_DECISECONDS, purge)
  110. #define avalon_open(devpath, baud) avalon_open2(devpath, baud, false)
  111. #define avalon_init_default_task(at) avalon_init_task(at, 0, 0, 0, 0, 0, 0)
  112. #define avalon_close(fd) close(fd)
  113. #define AVA_BUFFER_FULL 0
  114. #define AVA_BUFFER_EMPTY 1
  115. #define avalon_buffer_full(fd) get_serial_cts(fd)
  116. static inline void rev(uint8_t *s, size_t l)
  117. {
  118. size_t i, j;
  119. uint8_t t;
  120. for (i = 0, j = l - 1; i < j; i++, j--) {
  121. t = s[i];
  122. s[i] = s[j];
  123. s[j] = t;
  124. }
  125. }
  126. #endif /* AVALON_H */