mcp2210.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright 2012-2013 Luke Dashjr
  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. #include "config.h"
  10. #include <stdbool.h>
  11. #include <stdint.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <utlist.h>
  15. #include "logging.h"
  16. #include "lowlevel.h"
  17. #include "lowl-hid.h"
  18. #include "miner.h"
  19. #include "mcp2210.h"
  20. #define MCP2210_IDVENDOR 0x04d8
  21. #define MCP2210_IDPRODUCT 0x00de
  22. static
  23. bool _mcp2210_devinfo_scan_cb(struct lowlevel_device_info * const usbinfo, void * const userp)
  24. {
  25. struct lowlevel_device_info **devinfo_list_p = userp, *info;
  26. info = malloc(sizeof(*info));
  27. *info = (struct lowlevel_device_info){
  28. .lowl = &lowl_mcp2210,
  29. };
  30. lowlevel_devinfo_semicpy(info, usbinfo);
  31. LL_PREPEND(*devinfo_list_p, info);
  32. // Never *consume* the lowl_usb entry - especially since this is during the scan!
  33. return false;
  34. }
  35. static
  36. struct lowlevel_device_info *mcp2210_devinfo_scan()
  37. {
  38. struct lowlevel_device_info *devinfo_list = NULL;
  39. lowlevel_detect_id(_mcp2210_devinfo_scan_cb, &devinfo_list, &lowl_hid, MCP2210_IDVENDOR, MCP2210_IDPRODUCT);
  40. return devinfo_list;
  41. }
  42. struct mcp2210_device {
  43. hid_device *hid;
  44. // http://ww1.microchip.com/downloads/en/DeviceDoc/22288A.pdf pg 34
  45. uint8_t cfg_spi[0x11];
  46. // http://ww1.microchip.com/downloads/en/DeviceDoc/22288A.pdf pg 40
  47. uint8_t cfg_gpio[0xf];
  48. };
  49. static
  50. bool mcp2210_io(hid_device * const hid, uint8_t * const cmd, uint8_t * const buf)
  51. {
  52. char hexcmd[(0x41 * 2) + 1];
  53. if (opt_dev_protocol)
  54. bin2hex(hexcmd, cmd, 0x41);
  55. const bool rv = likely(
  56. 0x41 == hid_write(hid, cmd, 0x41) &&
  57. 64 == hid_read(hid, buf, 64)
  58. );
  59. if (opt_dev_protocol)
  60. {
  61. char hexbuf[(0x40 * 2) + 1];
  62. bin2hex(hexbuf, buf, 0x40);
  63. applog(LOG_DEBUG, "mcp2210_io(%p, %s, %s)", hid, hexcmd, hexbuf);
  64. }
  65. return rv;
  66. }
  67. static
  68. bool mcp2210_get_configs(struct mcp2210_device * const h)
  69. {
  70. hid_device * const hid = h->hid;
  71. uint8_t cmd[0x41] = {0,0x41}, buf[0x40];
  72. if (!mcp2210_io(hid, cmd, buf))
  73. {
  74. applog(LOG_ERR, "%s: Failed to get current %s config", __func__, "SPI");
  75. return false;
  76. }
  77. memcpy(h->cfg_spi, &buf[4], sizeof(h->cfg_spi));
  78. cmd[1] = 0x20;
  79. if (!mcp2210_io(hid, cmd, buf))
  80. {
  81. applog(LOG_ERR, "%s: Failed to get current %s config", __func__, "GPIO");
  82. return false;
  83. }
  84. memcpy(h->cfg_gpio, &buf[4], sizeof(h->cfg_gpio));
  85. return true;
  86. }
  87. struct mcp2210_device *mcp2210_open(const struct lowlevel_device_info * const info)
  88. {
  89. struct mcp2210_device *h;
  90. char * const path = info->path;
  91. hid_device * const hid = hid_open_path(path);
  92. if (unlikely(!hid))
  93. return NULL;
  94. h = malloc(sizeof(*h));
  95. h->hid = hid;
  96. if (!mcp2210_get_configs(h))
  97. goto fail;
  98. return h;
  99. fail:
  100. free(h);
  101. return NULL;
  102. }
  103. void mcp2210_close(struct mcp2210_device * const h)
  104. {
  105. hid_close(h->hid);
  106. free(h);
  107. }
  108. static
  109. bool mcp2210_set_cfg_spi(struct mcp2210_device * const h)
  110. {
  111. hid_device * const hid = h->hid;
  112. uint8_t cmd[0x41] = {0,0x40}, buf[0x40];
  113. memcpy(&cmd[5], h->cfg_spi, sizeof(h->cfg_spi));
  114. if (!mcp2210_io(hid, cmd, buf))
  115. {
  116. applog(LOG_ERR, "%s: Failed to set current %s config", __func__, "SPI");
  117. return false;
  118. }
  119. if (buf[1] != 0)
  120. {
  121. applog(LOG_ERR, "%s: Error setting current %s config (%d)", __func__, "SPI", buf[1]);
  122. return false;
  123. }
  124. return true;
  125. }
  126. bool mcp2210_configure_spi(struct mcp2210_device * const h, const uint32_t bitrate, const uint16_t idlechipsel, const uint16_t activechipsel, const uint16_t chipseltodatadelay, const uint16_t lastbytetocsdelay, const uint16_t midbytedelay)
  127. {
  128. uint8_t * const cfg = h->cfg_spi;
  129. cfg[0] = (bitrate >> 0x00) & 0xff;
  130. cfg[1] = (bitrate >> 0x08) & 0xff;
  131. cfg[2] = (bitrate >> 0x10) & 0xff;
  132. cfg[3] = (bitrate >> 0x18) & 0xff;
  133. cfg[4] = ( idlechipsel >> 0) & 0xff;
  134. cfg[5] = ( idlechipsel >> 8) & 0xff;
  135. cfg[6] = (activechipsel >> 0) & 0xff;
  136. cfg[7] = (activechipsel >> 8) & 0xff;
  137. cfg[8] = (chipseltodatadelay >> 0) & 0xff;
  138. cfg[9] = (chipseltodatadelay >> 8) & 0xff;
  139. cfg[0xa] = (lastbytetocsdelay >> 0) & 0xff;
  140. cfg[0xb] = (lastbytetocsdelay >> 8) & 0xff;
  141. cfg[0xc] = (midbytedelay >> 0) & 0xff;
  142. cfg[0xd] = (midbytedelay >> 8) & 0xff;
  143. return mcp2210_set_cfg_spi(h);
  144. }
  145. bool mcp2210_set_spimode(struct mcp2210_device * const h, const uint8_t spimode)
  146. {
  147. uint8_t * const cfg = h->cfg_spi;
  148. cfg[0x10] = spimode;
  149. return mcp2210_set_cfg_spi(h);
  150. }
  151. bool mcp2210_spi_transfer(struct mcp2210_device * const h, const void * const tx, void * const rx, uint8_t sz)
  152. {
  153. hid_device * const hid = h->hid;
  154. uint8_t * const cfg = h->cfg_spi;
  155. uint8_t cmd[0x41] = {0,0x42}, buf[0x40];
  156. uint8_t *p = rx;
  157. if (unlikely(sz > 60))
  158. {
  159. applog(LOG_ERR, "%s: SPI transfer too long (%d bytes)", __func__, sz);
  160. return false;
  161. }
  162. cfg[0xe] = sz;
  163. cfg[0xf] = 0;
  164. if (!mcp2210_set_cfg_spi(h))
  165. return false;
  166. cmd[2] = sz;
  167. memcpy(&cmd[5], tx, sz);
  168. if (unlikely(!mcp2210_io(hid, cmd, buf)))
  169. {
  170. applog(LOG_ERR, "%s: Failed to issue SPI transfer", __func__);
  171. return false;
  172. }
  173. while (true)
  174. {
  175. switch (buf[1])
  176. {
  177. case 0: // accepted
  178. cmd[2] = 0;
  179. break;
  180. case 0xf8: // transfer in progress
  181. applog(LOG_DEBUG, "%s: SPI transfer rejected temporarily (%d bytes remaining)", __func__, sz);
  182. cgsleep_ms(20);
  183. goto retry;
  184. default:
  185. applog(LOG_ERR, "%s: SPI transfer error (%d) (%d bytes remaining)", __func__, buf[1], sz);
  186. return false;
  187. }
  188. if (buf[2] >= sz)
  189. {
  190. if (buf[2] > sz)
  191. applog(LOG_WARNING, "%s: Received %d extra bytes in SPI transfer", __func__, sz - buf[2]);
  192. memcpy(p, &buf[4], sz);
  193. return true;
  194. }
  195. memcpy(p, &buf[4], buf[2]);
  196. p += buf[2];
  197. sz -= buf[2];
  198. retry:
  199. if (unlikely(!mcp2210_io(hid, cmd, buf)))
  200. {
  201. applog(LOG_ERR, "%s: Failed to continue SPI transfer (%d bytes remaining)", __func__, sz);
  202. return false;
  203. }
  204. }
  205. }
  206. bool mcp2210_spi_cancel(struct mcp2210_device * const h)
  207. {
  208. hid_device * const hid = h->hid;
  209. uint8_t cmd[0x41] = {0,0x11}, buf[0x40];
  210. if (!mcp2210_io(hid, cmd, buf))
  211. return false;
  212. return (buf[1] == 0);
  213. }
  214. static
  215. bool mcp2210_set_cfg_gpio(struct mcp2210_device * const h)
  216. {
  217. hid_device * const hid = h->hid;
  218. uint8_t cmd[0x41] = {0,0x21}, buf[0x40];
  219. // NOTE: NVRAM chip params access control is not set here
  220. memcpy(&cmd[5], h->cfg_gpio, 0xe);
  221. if (!mcp2210_io(hid, cmd, buf))
  222. {
  223. applog(LOG_ERR, "%s: Failed to set current %s config", __func__, "GPIO");
  224. return false;
  225. }
  226. if (buf[1] != 0)
  227. {
  228. applog(LOG_ERR, "%s: Error setting current %s config (%d)", __func__, "GPIO", buf[1]);
  229. return false;
  230. }
  231. return true;
  232. }
  233. bool mcp2210_set_gpio_output(struct mcp2210_device * const h, const int pin, const enum bfg_gpio_value d)
  234. {
  235. const int bit = 1 << (pin % 8);
  236. const int byte = (pin / 8);
  237. // Set pin to GPIO mode
  238. h->cfg_gpio[pin] = 0;
  239. // Set GPIO to output mode
  240. h->cfg_gpio[byte + 0xb] &= ~bit;
  241. // Set value for GPIO output
  242. if (d == BGV_HIGH)
  243. h->cfg_gpio[byte + 9] |= bit;
  244. else
  245. h->cfg_gpio[byte + 9] &= ~bit;
  246. return mcp2210_set_cfg_gpio(h);
  247. }
  248. enum bfg_gpio_value mcp2210_get_gpio_input(struct mcp2210_device * const h, const int pin)
  249. {
  250. hid_device * const hid = h->hid;
  251. uint8_t cmd[0x41] = {0,0x31}, buf[0x40];
  252. const int bit = 1 << (pin % 8);
  253. const int byte = (pin / 8);
  254. // Set pin to GPIO mode
  255. h->cfg_gpio[pin] = 0;
  256. // Set GPIO to input mode
  257. h->cfg_gpio[byte + 0xb] |= bit;
  258. if (!mcp2210_set_cfg_gpio(h))
  259. return BGV_ERROR;
  260. if (!mcp2210_io(hid, cmd, buf))
  261. {
  262. applog(LOG_ERR, "%s: Failed to get current GPIO input values", __func__);
  263. return BGV_ERROR;
  264. }
  265. if (buf[byte + 4] & bit)
  266. return BGV_HIGH;
  267. else
  268. return BGV_LOW;
  269. }
  270. struct lowlevel_driver lowl_mcp2210 = {
  271. .dname = "mcp2210",
  272. .devinfo_scan = mcp2210_devinfo_scan,
  273. };