bf16-communication.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. #include "bf16-communication.h"
  2. #include "bf16-ctrldevice.h"
  3. #include "bf16-gpiodevice.h"
  4. #include "miner.h"
  5. #define MSP_BUFF_SIZE 96
  6. static device_t* spi0_device;
  7. static device_t* spi1_device;
  8. static device_t* uart1_device;
  9. static device_t* uart2_device;
  10. static device_t* ctrl_device;
  11. static device_t* gpio_device;
  12. /* transfer functions */
  13. int8_t device_spi_transfer(spi_channel_id_t channel_id, uint8_t* data, int size)
  14. {
  15. switch (channel_id) {
  16. case SPI_CHANNEL1:
  17. memset(spi0_device->tx, 0, spi0_device->size);
  18. memset(spi0_device->rx, 0, spi0_device->size);
  19. spi0_device->datalen = size;
  20. cg_memcpy(spi0_device->tx, data, size);
  21. spi_transfer(spi0_device);
  22. cg_memcpy(data, spi0_device->rx, size);
  23. break;
  24. case SPI_CHANNEL2:
  25. memset(spi1_device->tx, 0, spi1_device->size);
  26. memset(spi1_device->rx, 0, spi1_device->size);
  27. spi1_device->datalen = size;
  28. cg_memcpy(spi1_device->tx, data, size);
  29. spi_transfer(spi1_device);
  30. cg_memcpy(data, spi1_device->rx, size);
  31. break;
  32. }
  33. return 0;
  34. }
  35. int8_t device_spi_txrx(spi_channel_id_t channel_id, uint8_t* tx, uint8_t* rx, int size)
  36. {
  37. switch (channel_id) {
  38. case SPI_CHANNEL1:
  39. memset(spi0_device->tx, 0, spi0_device->size);
  40. memset(spi0_device->rx, 0, spi0_device->size);
  41. spi0_device->datalen = size;
  42. cg_memcpy(spi0_device->tx, tx, size);
  43. spi_transfer(spi0_device);
  44. cg_memcpy(rx, spi0_device->rx, size);
  45. break;
  46. case SPI_CHANNEL2:
  47. memset(spi1_device->tx, 0, spi1_device->size);
  48. memset(spi1_device->rx, 0, spi1_device->size);
  49. spi1_device->datalen = size;
  50. cg_memcpy(spi1_device->tx, tx, size);
  51. spi_transfer(spi1_device);
  52. cg_memcpy(rx, spi1_device->rx, size);
  53. break;
  54. }
  55. return 0;
  56. }
  57. static void add_crc(char* data)
  58. {
  59. uint8_t crc = 0;
  60. while (*data) {
  61. crc += *data;
  62. data++;
  63. }
  64. sprintf(data, "#%d\n", crc);
  65. }
  66. int8_t device_uart_transfer(uart_channel_id_t channel_id, char* cmd)
  67. {
  68. uint8_t buff[MSP_BUFF_SIZE];
  69. memset(buff, 0, MSP_BUFF_SIZE);
  70. uint16_t cmdlen = strlen(cmd);
  71. if (cmdlen > 0) {
  72. (cmdlen == 1) ? sprintf((char *)buff, "%s:", cmd) : sprintf((char *)buff, "%s", cmd);
  73. add_crc((char *)buff);
  74. }
  75. switch (channel_id) {
  76. case UART_CHANNEL1:
  77. memset(uart1_device->tx, 0, uart1_device->size);
  78. memset(uart1_device->rx, 0, uart1_device->size);
  79. uart1_device->datalen = strlen((char *)buff);
  80. cg_memcpy(uart1_device->tx, buff, uart1_device->datalen);
  81. return uart_transfer(uart1_device);
  82. break;
  83. case UART_CHANNEL2:
  84. memset(uart2_device->tx, 0, uart2_device->size);
  85. memset(uart2_device->rx, 0, uart2_device->size);
  86. uart2_device->datalen = strlen((char *)buff);
  87. cg_memcpy(uart2_device->tx, buff, uart2_device->datalen);
  88. return uart_transfer(uart2_device);
  89. break;
  90. }
  91. return 0;
  92. }
  93. int16_t device_uart_txrx(uart_channel_id_t channel_id, char* cmd, char* data)
  94. {
  95. uint8_t buff[MSP_BUFF_SIZE];
  96. memset(buff, 0, MSP_BUFF_SIZE);
  97. uint16_t cmdlen = strlen(cmd);
  98. if (cmdlen > 0) {
  99. (cmdlen == 1) ? sprintf((char *)buff, "%s:", cmd) : sprintf((char *)buff, "%s", cmd);
  100. add_crc((char *)buff);
  101. }
  102. switch (channel_id) {
  103. case UART_CHANNEL1:
  104. memset(uart1_device->tx, 0, uart1_device->size);
  105. memset(uart1_device->rx, 0, uart1_device->size);
  106. uart1_device->datalen = strlen((char *)buff);
  107. cg_memcpy(uart1_device->tx, buff, uart1_device->datalen);
  108. if (uart_transfer(uart1_device) < 0)
  109. return -1;
  110. cg_memcpy(data, uart1_device->rx, uart1_device->datalen);
  111. return uart1_device->datalen;
  112. break;
  113. case UART_CHANNEL2:
  114. memset(uart2_device->tx, 0, uart2_device->size);
  115. memset(uart2_device->rx, 0, uart2_device->size);
  116. uart2_device->datalen = strlen((char *)buff);
  117. cg_memcpy(uart2_device->tx, buff, uart2_device->datalen);
  118. if (uart_transfer(uart2_device) < 0)
  119. return -1;
  120. cg_memcpy(data, uart2_device->rx, uart2_device->datalen);
  121. return uart2_device->datalen;
  122. break;
  123. }
  124. return 0;
  125. }
  126. int8_t device_ctrl_transfer(uint8_t channel_id, int state, int fn)
  127. {
  128. int8_t ret = 0;
  129. char* cmd = get_ctrl_data(channel_id, state, fn);
  130. memset(ctrl_device->tx, 0, ctrl_device->size);
  131. memset(ctrl_device->rx, 0, ctrl_device->size);
  132. ctrl_device->datalen = strlen(cmd) + 1;
  133. cg_memcpy(ctrl_device->tx, cmd, ctrl_device->datalen);
  134. ret = ctrl_transfer(ctrl_device);
  135. free(cmd);
  136. return ret;
  137. }
  138. int8_t device_ctrl_txrx(uint8_t channel_id, int state, int fn, char* data)
  139. {
  140. int8_t ret = 0;
  141. char* cmd = get_ctrl_data(channel_id, state, fn);
  142. memset(ctrl_device->tx, 0, ctrl_device->size);
  143. memset(ctrl_device->rx, 0, ctrl_device->size);
  144. ctrl_device->datalen = strlen(cmd) + 1;
  145. cg_memcpy(ctrl_device->tx, cmd, ctrl_device->datalen);
  146. ret = ctrl_transfer(ctrl_device);
  147. cg_memcpy(data, ctrl_device->rx, ctrl_device->datalen);
  148. free(cmd);
  149. return ret;
  150. }
  151. /* open device functions */
  152. int8_t open_spi_device(spi_channel_id_t channel_id)
  153. {
  154. switch (channel_id) {
  155. case SPI_CHANNEL1:
  156. if ((spi0_device = malloc(sizeof(device_t))) == NULL)
  157. quit(1, "Failed to allocate spi_device1 memory: %s", strerror(errno));
  158. memset(spi0_device, 0, sizeof(device_t));
  159. return spi_init(spi0_device, channel_id, 1, SPI_SPEED, SPI_BUFFER_SIZE);
  160. break;
  161. case SPI_CHANNEL2:
  162. if ((spi1_device = malloc(sizeof(device_t))) == NULL)
  163. quit(1, "Failed to allocate spi_device2 memory: %s", strerror(errno));
  164. memset(spi1_device, 0, sizeof(device_t));
  165. return spi_init(spi1_device, channel_id, 1, SPI_SPEED, SPI_BUFFER_SIZE);
  166. break;
  167. }
  168. return 0;
  169. }
  170. int8_t open_uart_device(uart_channel_id_t channel_id)
  171. {
  172. switch (channel_id) {
  173. case UART_CHANNEL1:
  174. if ((uart1_device = malloc(sizeof(device_t))) == NULL)
  175. quit(1, "Failed to allocate uart_device1 memory: %s", strerror(errno));
  176. memset(uart1_device, 0, sizeof(device_t));
  177. return uart_init(uart1_device, channel_id, 0, B115200, UART_BUFFER_SIZE);
  178. break;
  179. case UART_CHANNEL2:
  180. if ((uart2_device = malloc(sizeof(device_t))) == NULL)
  181. quit(1, "Failed to allocate uart_device2 memory: %s", strerror(errno));
  182. memset(uart2_device, 0, sizeof(device_t));
  183. return uart_init(uart2_device, channel_id, 0, B115200, UART_BUFFER_SIZE);
  184. break;
  185. }
  186. return 0;
  187. }
  188. int8_t open_ctrl_device(void)
  189. {
  190. if ((ctrl_device = malloc(sizeof(device_t))) == NULL)
  191. quit(1, "Failed to allocate ctrl_device memory: %s", strerror(errno));
  192. memset(ctrl_device, 0, sizeof(device_t));
  193. if ((gpio_device = malloc(sizeof(device_t))) == NULL)
  194. quit(1, "Failed to allocate gpio_device memory: %s", strerror(errno));
  195. memset(gpio_device, 0, sizeof(device_t));
  196. if (gpio_init(gpio_device, gpio_device_name, GPIO_BUFFER_SIZE) < 0)
  197. quit(1, "Failed to open [%s] device in open_ctrl_device", gpio_device_name);
  198. applog(LOG_INFO, "BF16: opened [%s] device", gpio_device_name);
  199. return ctrl_init(ctrl_device, ctrl_device_name, CTRL_BUFFER_SIZE);
  200. }
  201. /* close device functions */
  202. int8_t close_spi_device(spi_channel_id_t channel_id)
  203. {
  204. switch (channel_id) {
  205. case SPI_CHANNEL1:
  206. spi_release(spi0_device);
  207. free(spi0_device);
  208. break;
  209. case SPI_CHANNEL2:
  210. spi_release(spi1_device);
  211. free(spi1_device);
  212. break;
  213. }
  214. return 0;
  215. }
  216. int8_t close_uart_device(uart_channel_id_t channel_id)
  217. {
  218. switch (channel_id) {
  219. case UART_CHANNEL1:
  220. uart_release(uart1_device);
  221. free(uart1_device);
  222. break;
  223. case UART_CHANNEL2:
  224. uart_release(uart2_device);
  225. free(uart2_device);
  226. break;
  227. }
  228. return 0;
  229. }
  230. int8_t close_ctrl_device(void)
  231. {
  232. ctrl_release(ctrl_device);
  233. gpio_release(gpio_device);
  234. free(ctrl_device);
  235. free(gpio_device);
  236. return 0;
  237. }