mcp2210.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. return likely(
  53. 0x41 == hid_write(hid, cmd, 0x41) &&
  54. 64 == hid_read(hid, buf, 64)
  55. );
  56. }
  57. static
  58. bool mcp2210_get_configs(struct mcp2210_device * const h)
  59. {
  60. hid_device * const hid = h->hid;
  61. uint8_t cmd[0x41] = {0,0x41}, buf[0x40];
  62. if (!mcp2210_io(hid, cmd, buf))
  63. {
  64. applog(LOG_ERR, "%s: Failed to get current %s config", __func__, "SPI");
  65. return false;
  66. }
  67. memcpy(h->cfg_spi, &buf[4], sizeof(h->cfg_spi));
  68. cmd[1] = 0x20;
  69. if (!mcp2210_io(hid, cmd, buf))
  70. {
  71. applog(LOG_ERR, "%s: Failed to get current %s config", __func__, "GPIO");
  72. return false;
  73. }
  74. memcpy(h->cfg_gpio, &buf[4], sizeof(h->cfg_gpio));
  75. return true;
  76. }
  77. struct mcp2210_device *mcp2210_open(const struct lowlevel_device_info * const info)
  78. {
  79. struct mcp2210_device *h;
  80. char * const path = info->path;
  81. hid_device * const hid = hid_open_path(path);
  82. if (unlikely(!hid))
  83. return NULL;
  84. h = malloc(sizeof(*h));
  85. h->hid = hid;
  86. if (!mcp2210_get_configs(h))
  87. goto fail;
  88. return h;
  89. fail:
  90. free(h);
  91. return NULL;
  92. }
  93. void mcp2210_close(struct mcp2210_device * const h)
  94. {
  95. hid_close(h->hid);
  96. free(h);
  97. }
  98. static
  99. bool mcp2210_set_cfg_spi(struct mcp2210_device * const h)
  100. {
  101. hid_device * const hid = h->hid;
  102. uint8_t cmd[0x41] = {0,0x40}, buf[0x40];
  103. memcpy(&cmd[5], h->cfg_spi, sizeof(h->cfg_spi));
  104. if (!mcp2210_io(hid, cmd, buf))
  105. {
  106. applog(LOG_ERR, "%s: Failed to set current %s config", __func__, "SPI");
  107. return false;
  108. }
  109. if (buf[1] != 0)
  110. {
  111. applog(LOG_ERR, "%s: Error setting current %s config (%d)", __func__, "SPI", buf[1]);
  112. return false;
  113. }
  114. return true;
  115. }
  116. 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)
  117. {
  118. uint8_t * const cfg = h->cfg_spi;
  119. cfg[0] = (bitrate >> 0x00) & 0xff;
  120. cfg[1] = (bitrate >> 0x08) & 0xff;
  121. cfg[2] = (bitrate >> 0x10) & 0xff;
  122. cfg[3] = (bitrate >> 0x18) & 0xff;
  123. cfg[4] = ( idlechipsel >> 0) & 0xff;
  124. cfg[5] = ( idlechipsel >> 8) & 0xff;
  125. cfg[6] = (activechipsel >> 0) & 0xff;
  126. cfg[7] = (activechipsel >> 8) & 0xff;
  127. cfg[8] = (chipseltodatadelay >> 0) & 0xff;
  128. cfg[9] = (chipseltodatadelay >> 8) & 0xff;
  129. cfg[0xa] = (lastbytetocsdelay >> 0) & 0xff;
  130. cfg[0xb] = (lastbytetocsdelay >> 8) & 0xff;
  131. cfg[0xc] = (midbytedelay >> 0) & 0xff;
  132. cfg[0xd] = (midbytedelay >> 8) & 0xff;
  133. return mcp2210_set_cfg_spi(h);
  134. }
  135. bool mcp2210_set_spimode(struct mcp2210_device * const h, const uint8_t spimode)
  136. {
  137. uint8_t * const cfg = h->cfg_spi;
  138. cfg[0x10] = spimode;
  139. return mcp2210_set_cfg_spi(h);
  140. }
  141. bool mcp2210_spi_transfer(struct mcp2210_device * const h, const void * const tx, void * const rx, uint8_t sz)
  142. {
  143. hid_device * const hid = h->hid;
  144. uint8_t * const cfg = h->cfg_spi;
  145. uint8_t cmd[0x41] = {0,0x42}, buf[0x40];
  146. uint8_t *p = rx;
  147. if (unlikely(sz > 60))
  148. {
  149. applog(LOG_ERR, "%s: SPI transfer too long (%d bytes)", __func__, sz);
  150. return false;
  151. }
  152. cfg[0xe] = sz;
  153. cfg[0xf] = 0;
  154. if (!mcp2210_set_cfg_spi(h))
  155. return false;
  156. cmd[2] = sz;
  157. memcpy(&cmd[5], tx, sz);
  158. if (unlikely(!mcp2210_io(hid, cmd, buf)))
  159. {
  160. applog(LOG_ERR, "%s: Failed to issue SPI transfer", __func__);
  161. return false;
  162. }
  163. while (true)
  164. {
  165. switch (buf[1])
  166. {
  167. case 0: // accepted
  168. cmd[2] = 0;
  169. break;
  170. case 0xf8: // transfer in progress
  171. applog(LOG_DEBUG, "%s: SPI transfer rejected temporarily (%d bytes remaining)", __func__, sz);
  172. cgsleep_ms(20);
  173. goto retry;
  174. default:
  175. applog(LOG_ERR, "%s: SPI transfer error (%d) (%d bytes remaining)", __func__, buf[1], sz);
  176. return false;
  177. }
  178. if (buf[2] >= sz)
  179. {
  180. if (buf[2] > sz)
  181. applog(LOG_WARNING, "%s: Received %d extra bytes in SPI transfer", __func__, sz - buf[2]);
  182. memcpy(p, &buf[4], sz);
  183. return true;
  184. }
  185. memcpy(p, &buf[4], buf[2]);
  186. p += buf[2];
  187. sz -= buf[2];
  188. retry:
  189. if (unlikely(!mcp2210_io(hid, cmd, buf)))
  190. {
  191. applog(LOG_ERR, "%s: Failed to continue SPI transfer (%d bytes remaining)", __func__, sz);
  192. return false;
  193. }
  194. }
  195. }
  196. bool mcp2210_spi_cancel(struct mcp2210_device * const h)
  197. {
  198. hid_device * const hid = h->hid;
  199. uint8_t cmd[0x41] = {0,0x11}, buf[0x40];
  200. if (!mcp2210_io(hid, cmd, buf))
  201. return false;
  202. return (buf[1] == 0);
  203. }
  204. static
  205. bool mcp2210_set_cfg_gpio(struct mcp2210_device * const h)
  206. {
  207. hid_device * const hid = h->hid;
  208. uint8_t cmd[0x41] = {0,0x21}, buf[0x40];
  209. // NOTE: NVRAM chip params access control is not set here
  210. memcpy(&cmd[5], h->cfg_gpio, 0xe);
  211. if (!mcp2210_io(hid, cmd, buf))
  212. {
  213. applog(LOG_ERR, "%s: Failed to set current %s config", __func__, "GPIO");
  214. return false;
  215. }
  216. if (buf[1] != 0)
  217. {
  218. applog(LOG_ERR, "%s: Error setting current %s config (%d)", __func__, "GPIO", buf[1]);
  219. return false;
  220. }
  221. return true;
  222. }
  223. bool mcp2210_set_gpio_output(struct mcp2210_device * const h, const int pin, const enum mcp2210_gpio_value d)
  224. {
  225. const int bit = 1 << (pin % 8);
  226. const int byte = (pin / 8);
  227. // Set pin to GPIO mode
  228. h->cfg_gpio[pin] = 0;
  229. // Set GPIO to output mode
  230. h->cfg_gpio[byte + 0xb] &= ~bit;
  231. // Set value for GPIO output
  232. if (d == MGV_HIGH)
  233. h->cfg_gpio[byte + 9] |= bit;
  234. else
  235. h->cfg_gpio[byte + 9] &= ~bit;
  236. return mcp2210_set_cfg_gpio(h);
  237. }
  238. enum mcp2210_gpio_value mcp2210_get_gpio_input(struct mcp2210_device * const h, const int pin)
  239. {
  240. hid_device * const hid = h->hid;
  241. uint8_t cmd[0x41] = {0,0x31}, buf[0x40];
  242. const int bit = 1 << (pin % 8);
  243. const int byte = (pin / 8);
  244. // Set pin to GPIO mode
  245. h->cfg_gpio[pin] = 0;
  246. // Set GPIO to input mode
  247. h->cfg_gpio[byte + 0xb] |= bit;
  248. if (!mcp2210_set_cfg_gpio(h))
  249. return MGV_ERROR;
  250. if (!mcp2210_io(hid, cmd, buf))
  251. {
  252. applog(LOG_ERR, "%s: Failed to get current GPIO input values", __func__);
  253. return MGV_ERROR;
  254. }
  255. if (buf[byte + 4] & bit)
  256. return MGV_HIGH;
  257. else
  258. return MGV_LOW;
  259. }
  260. struct lowlevel_driver lowl_mcp2210 = {
  261. .dname = "mcp2210",
  262. .devinfo_scan = mcp2210_devinfo_scan,
  263. };