bf16-mspcontrol.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. #include "bf16-mspcontrol.h"
  2. #include "bf16-communication.h"
  3. #include "bf16-ctrldevice.h"
  4. static bool check_crc(char* data)
  5. {
  6. uint8_t crc = 0;
  7. char crcdata[8];
  8. char* mid = strchr(data, '#');
  9. char* end = strchr(data, '\n');
  10. if (mid == NULL)
  11. return false;
  12. if (end == NULL)
  13. return false;
  14. while (data != mid) {
  15. crc += *data;
  16. data++;
  17. }
  18. uint8_t value_len = end - mid;
  19. memset(crcdata, 0, sizeof(crcdata));
  20. cg_memcpy(crcdata, mid + 1, value_len - 1);
  21. int expected_crc = atoi(crcdata);
  22. if (expected_crc != crc)
  23. return false;
  24. return true;
  25. }
  26. static char* get_int_val(char* data, char* name, char delim, int* value)
  27. {
  28. char rname[16];
  29. char rvalue[32];
  30. char* end = strchr(data, delim);
  31. if (end == NULL)
  32. return NULL;
  33. memset(rname, 0, sizeof(rname));
  34. memset(rvalue, 0, sizeof(rvalue));
  35. char* mid = strchr(data, ':');
  36. if (mid == NULL)
  37. return NULL;
  38. uint8_t name_len = mid - data;
  39. uint8_t value_len = end - mid;
  40. cg_memcpy(rname, data, name_len);
  41. cg_memcpy(rvalue, mid + 1, value_len - 1);
  42. if (strcmp(rname, name) == 0)
  43. *value = atoi(rvalue);
  44. else {
  45. *value = 0;
  46. return data;
  47. }
  48. return end + 1;
  49. }
  50. static char* get_str_val(char* data, char* name, char delim, char* value)
  51. {
  52. char rname[16];
  53. char rvalue[32];
  54. char* end = strchr(data, delim);
  55. if (end == NULL)
  56. return NULL;
  57. memset(rname, 0, sizeof(rname));
  58. memset(rvalue, 0, sizeof(rvalue));
  59. char* mid = strchr(data, ':');
  60. if (mid == NULL)
  61. return NULL;
  62. uint8_t name_len = mid - data;
  63. uint8_t value_len = end - mid;
  64. cg_memcpy(rname, data, name_len);
  65. cg_memcpy(rvalue, mid + 1, value_len - 1);
  66. if (strcmp(rname, name) == 0)
  67. cg_memcpy(value, rvalue, strlen(rvalue));
  68. else {
  69. memset(value, '0', 16);
  70. return data;
  71. }
  72. return end + 1;
  73. }
  74. void parse_board_detect(struct cgpu_info *bitfury, uint8_t board_id, char* data)
  75. {
  76. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  77. char* start = data;
  78. char val[4];
  79. memset(val, 0, sizeof(val));
  80. /* BRD_DET*/
  81. if (board_id == 0) {
  82. if (get_str_val(start, "CH1_DET", '\n', val) == NULL)
  83. quit(1, "%s: %s() failed to parse BOARD%d detect",
  84. bitfury->drv->name, __func__, board_id + 1);
  85. if (strcmp(val, "ON") == 0)
  86. info->chipboard[board_id].detected = true;
  87. else
  88. info->chipboard[board_id].detected = false;
  89. } else {
  90. if (get_str_val(start, "CH2_DET", '\n', val) == NULL)
  91. quit(1, "%s: %s() failed to parse BOARD%d detect",
  92. bitfury->drv->name, __func__, board_id + 1);
  93. if (strcmp(val, "ON") == 0)
  94. info->chipboard[board_id].detected = true;
  95. else
  96. info->chipboard[board_id].detected = false;
  97. }
  98. }
  99. static void parse_board_version(struct cgpu_info *bitfury, uint8_t board_id, char* data)
  100. {
  101. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  102. if (check_crc(data) == false)
  103. quit(1, "%s: %s() invalid BOARD%d version data recieved: [%s]",
  104. bitfury->drv->name, __func__, board_id + 1, data);
  105. /* BRD */
  106. char* start = data;
  107. start = get_int_val(start, "BRD", ';', (int*)&info->chipboard[board_id].board_ver);
  108. if (start == NULL)
  109. quit(1, "%s: %s() failed to parse BOARD%d BRD",
  110. bitfury->drv->name, __func__, board_id + 1);
  111. /* FW */
  112. start = get_int_val(start, "FW", ';', (int*)&info->chipboard[board_id].board_fwver);
  113. if (start == NULL)
  114. quit(1, "%s: %s() failed to parse BOARD%d FW",
  115. bitfury->drv->name, __func__, board_id + 1);
  116. /* ID */
  117. if (get_str_val(start, "ID", '#', info->chipboard[board_id].board_hwid) == NULL)
  118. quit(1, "%s: %s() failed to parse BOARD%d ID",
  119. bitfury->drv->name, __func__, board_id + 1);
  120. }
  121. void get_board_info(struct cgpu_info *bitfury, uint8_t board_id)
  122. {
  123. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  124. char buff[256];
  125. if (info->chipboard[board_id].detected == true) {
  126. memset(buff, 0, sizeof(buff));
  127. if (device_uart_txrx(board_id + 1, "V", buff) < 0)
  128. quit(1, "%s: %s() failed to get BOARD%d version",
  129. bitfury->drv->name, __func__, board_id + 1);
  130. parse_board_version(bitfury, board_id, buff);
  131. applog(LOG_INFO, "%s: BOARD%d version: [%d]", bitfury->drv->name,
  132. board_id + 1, info->chipboard[board_id].board_ver);
  133. applog(LOG_INFO, "%s: BOARD%d firmware version: [%d]", bitfury->drv->name,
  134. board_id + 1, info->chipboard[board_id].board_fwver);
  135. applog(LOG_INFO, "%s: BOARD%d hardware id: [%s]", bitfury->drv->name,
  136. board_id + 1, info->chipboard[board_id].board_hwid);
  137. }
  138. }
  139. int8_t parse_hwstats(struct bitfury16_info *info, uint8_t board_id, char* data)
  140. {
  141. uint8_t i;
  142. int32_t value;
  143. char strvalue[17];
  144. if (check_crc(data) == false) {
  145. applog(LOG_ERR, "BF16: invalid BOARD%d hwstats recieved: [%s]",
  146. board_id + 1, data);
  147. return -1;
  148. }
  149. /* T */
  150. char* start = data;
  151. start = get_int_val(start, "T", ';', (int *)&value);
  152. if (start != NULL)
  153. info->chipboard[board_id].temp = value / 10.0;
  154. else {
  155. info->chipboard[board_id].temp = 0.0;
  156. return -1;
  157. }
  158. /* UB */
  159. start = get_int_val(start, "UB", ';', (int *)&value);
  160. if (start != NULL)
  161. info->chipboard[board_id].u_board = value / 10.0;
  162. else {
  163. info->chipboard[board_id].u_board = 0.0;
  164. return -1;
  165. }
  166. /* P1 */
  167. start = get_int_val(start, "P1", ';', (int *)&value);
  168. if (start != NULL)
  169. info->chipboard[board_id].p_chain1_enabled = value;
  170. else {
  171. info->chipboard[board_id].p_chain1_enabled = 0;
  172. return -1;
  173. }
  174. /* P2 */
  175. start = get_int_val(start, "P2", ';', (int *)&value);
  176. if (start != NULL)
  177. info->chipboard[board_id].p_chain2_enabled = value;
  178. else {
  179. info->chipboard[board_id].p_chain2_enabled = 0;
  180. return -1;
  181. }
  182. /* U1 */
  183. start = get_int_val(start, "U1", ';', (int *)&value);
  184. if (start != NULL)
  185. info->chipboard[board_id].u_chain1 = value / 10.0;
  186. else {
  187. info->chipboard[board_id].u_chain1 = 0.0;
  188. return -1;
  189. }
  190. /* U2 */
  191. start = get_int_val(start, "U2", ';', (int *)&value);
  192. if (start != NULL)
  193. info->chipboard[board_id].u_chain2 = value / 10.0;
  194. else {
  195. info->chipboard[board_id].u_chain2 = 0.0;
  196. return -1;
  197. }
  198. /* I1 */
  199. start = get_int_val(start, "I1", ';', (int *)&value);
  200. if (start != NULL)
  201. info->chipboard[board_id].i_chain1 = value / 10.0;
  202. else {
  203. info->chipboard[board_id].i_chain1 = 0.0;
  204. return -1;
  205. }
  206. /* I2 */
  207. start = get_int_val(start, "I2", ';', (int *)&value);
  208. if (start != NULL)
  209. info->chipboard[board_id].i_chain2 = value / 10.0;
  210. else {
  211. info->chipboard[board_id].i_chain2 = 0.0;
  212. return -1;
  213. }
  214. /* RPM */
  215. start = get_int_val(start, "RPM", ';', (int *)&info->chipboard[board_id].rpm);
  216. if (start == NULL) {
  217. info->chipboard[board_id].rpm = 0;
  218. return -1;
  219. }
  220. /* A - alarms - may be absent */
  221. bool alarm = false;
  222. for (i = 0; i < 3; i++) {
  223. memset(strvalue, 0, sizeof(strvalue));
  224. start = get_str_val(start, "A", ';', strvalue);
  225. if (start == NULL)
  226. return -1;
  227. if (strcmp(strvalue, "T") == 0) {
  228. info->chipboard[board_id].a_temp = 1;
  229. alarm = true;
  230. } else if (strcmp(strvalue, "I1") == 0) {
  231. info->chipboard[board_id].a_ichain1 = 1;
  232. alarm = true;
  233. } else if (strcmp(strvalue, "I2") == 0) {
  234. info->chipboard[board_id].a_ichain2 = 1;
  235. alarm = true;
  236. }
  237. }
  238. if (alarm == false) {
  239. info->chipboard[board_id].a_temp = 0;
  240. info->chipboard[board_id].a_ichain1 = 0;
  241. info->chipboard[board_id].a_ichain2 = 0;
  242. }
  243. /* F */
  244. start = get_int_val(start, "F", ';', (int *)&value);
  245. if (start != NULL)
  246. info->chipboard[board_id].fan_speed = value;
  247. else {
  248. info->chipboard[board_id].fan_speed = 0;
  249. return -1;
  250. }
  251. /* AI */
  252. start = get_int_val(start, "AI", ';', (int *)&value);
  253. if (start != NULL)
  254. info->chipboard[board_id].i_alarm = value / 10.0;
  255. else {
  256. info->chipboard[board_id].i_alarm = 0.0;
  257. return -1;
  258. }
  259. /* AT */
  260. start = get_int_val(start, "AT", ';', (int *)&value);
  261. if (start != NULL)
  262. info->chipboard[board_id].t_alarm = value / 10.0;
  263. else {
  264. info->chipboard[board_id].t_alarm = 0.0;
  265. return -1;
  266. }
  267. /* AG */
  268. start = get_int_val(start, "AG", ';', (int *)&value);
  269. if (start != NULL)
  270. info->chipboard[board_id].t_gisteresis = value / 10.0;
  271. else {
  272. info->chipboard[board_id].t_gisteresis = 0.0;
  273. return -1;
  274. }
  275. /* FM */
  276. memset(strvalue, 0, sizeof(strvalue));
  277. start = get_str_val(start, "FM", ';', strvalue);
  278. if (start != NULL)
  279. info->chipboard[board_id].fan_mode = strvalue[0];
  280. else
  281. return -1;
  282. /* TT */
  283. start = get_int_val(start, "TT", '#', (int *)&value);
  284. if (start != NULL)
  285. info->chipboard[board_id].target_temp = value / 10.0;
  286. else {
  287. info->chipboard[board_id].target_temp = 0.0;
  288. return -1;
  289. }
  290. return 0;
  291. }
  292. int8_t enable_power_chain(struct cgpu_info *bitfury, uint8_t board_id, uint8_t chain)
  293. {
  294. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  295. if (info->chipboard[board_id].detected == true) {
  296. char uart_cmd[8];
  297. memset(uart_cmd, 0, sizeof(uart_cmd));
  298. switch (chain) {
  299. case 0:
  300. sprintf(uart_cmd, "P");
  301. break;
  302. case 1:
  303. case 2:
  304. sprintf(uart_cmd, "P:%d", chain);
  305. break;
  306. }
  307. if (device_uart_transfer(board_id + 1, uart_cmd) < 0) {
  308. switch (chain) {
  309. case 0:
  310. applog(LOG_ERR, "%s: failed to enable BOARD%d power chain",
  311. bitfury->drv->name, board_id + 1);
  312. break;
  313. case 1:
  314. case 2:
  315. applog(LOG_ERR, "%s: failed to enable BOARD%d power chain %d",
  316. bitfury->drv->name, board_id + 1, chain);
  317. break;
  318. }
  319. } else {
  320. switch (chain) {
  321. case 0:
  322. applog(LOG_NOTICE, "%s: enabled BOARD%d power chain",
  323. bitfury->drv->name, board_id + 1);
  324. break;
  325. case 1:
  326. case 2:
  327. applog(LOG_NOTICE, "%s: enabled BOARD%d power chain %d",
  328. bitfury->drv->name, board_id + 1, chain);
  329. break;
  330. }
  331. return 0;
  332. }
  333. }
  334. return -1;
  335. }
  336. int8_t disable_power_chain(struct cgpu_info *bitfury, uint8_t board_id, uint8_t chain)
  337. {
  338. struct bitfury16_info *info = (struct bitfury16_info *)(bitfury->device_data);
  339. if (info->chipboard[board_id].detected == true) {
  340. char uart_cmd[8];
  341. memset(uart_cmd, 0, sizeof(uart_cmd));
  342. switch (chain) {
  343. case 0:
  344. sprintf(uart_cmd, "O");
  345. break;
  346. case 1:
  347. case 2:
  348. sprintf(uart_cmd, "O:%d", chain);
  349. break;
  350. }
  351. if (device_uart_transfer(board_id + 1, uart_cmd) < 0) {
  352. switch (chain) {
  353. case 0:
  354. applog(LOG_ERR, "%s: failed to disable BOARD%d power chain",
  355. bitfury->drv->name, board_id + 1);
  356. break;
  357. case 1:
  358. case 2:
  359. applog(LOG_ERR, "%s: failed to disable BOARD%d power chain %d",
  360. bitfury->drv->name, board_id + 1, chain);
  361. break;
  362. }
  363. } else {
  364. switch (chain) {
  365. case 0:
  366. applog(LOG_NOTICE, "%s: disabled BOARD%d power chain",
  367. bitfury->drv->name, board_id + 1);
  368. break;
  369. case 1:
  370. case 2:
  371. applog(LOG_NOTICE, "%s: disabled BOARD%d power chain %d",
  372. bitfury->drv->name, board_id + 1, chain);
  373. break;
  374. }
  375. return 0;
  376. }
  377. }
  378. return -1;
  379. }
  380. void led_red_enable(struct bitfury16_info *info)
  381. {
  382. if (device_ctrl_transfer(SPI_CHANNEL1, 1, F_LED2) == 0) {
  383. info->led_red_enabled = true;
  384. gettimeofday(&info->led_red_switch, NULL);
  385. }
  386. }
  387. void led_red_disable(struct bitfury16_info *info)
  388. {
  389. if (device_ctrl_transfer(SPI_CHANNEL1, 0, F_LED2) == 0) {
  390. info->led_red_enabled = false;
  391. gettimeofday(&info->led_red_switch, NULL);
  392. }
  393. }
  394. void led_green_enable(struct bitfury16_info *info)
  395. {
  396. if (device_ctrl_transfer(SPI_CHANNEL1, 1, F_LED1) == 0) {
  397. info->led_green_enabled = true;
  398. gettimeofday(&info->led_green_switch, NULL);
  399. }
  400. }
  401. void led_green_disable(struct bitfury16_info *info)
  402. {
  403. if (device_ctrl_transfer(SPI_CHANNEL1, 0, F_LED1) == 0) {
  404. info->led_green_enabled = false;
  405. gettimeofday(&info->led_green_switch, NULL);
  406. }
  407. }
  408. void buzzer_enable(struct bitfury16_info *info)
  409. {
  410. if (device_ctrl_transfer(SPI_CHANNEL1, 1, F_BUZZER) == 0) {
  411. info->buzzer_enabled = true;
  412. gettimeofday(&info->buzzer_switch, NULL);
  413. }
  414. }
  415. void buzzer_disable(struct bitfury16_info *info)
  416. {
  417. if (device_ctrl_transfer(SPI_CHANNEL1, 0, F_BUZZER) == 0) {
  418. info->buzzer_enabled = false;
  419. gettimeofday(&info->buzzer_switch, NULL);
  420. }
  421. }