mcp2210.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. #ifndef WIN32
  11. #include <dlfcn.h>
  12. typedef void *dlh_t;
  13. #else
  14. #include <winsock2.h>
  15. #include <windows.h>
  16. #define dlopen(lib, flags) LoadLibrary(lib)
  17. #define dlsym(h, sym) ((void*)GetProcAddress(h, sym))
  18. #define dlerror() "unknown"
  19. #define dlclose(h) FreeLibrary(h)
  20. typedef HMODULE dlh_t;
  21. #endif
  22. #include <stdbool.h>
  23. #include <stdint.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <hidapi.h>
  27. #include <utlist.h>
  28. #include "fpgautils.h"
  29. #include "logging.h"
  30. #include "lowlevel.h"
  31. #include "miner.h"
  32. #include "mcp2210.h"
  33. #define MCP2210_IDVENDOR 0x04d8
  34. #define MCP2210_IDPRODUCT 0x00de
  35. #ifdef WIN32
  36. #define HID_API_EXPORT __declspec(dllexport)
  37. #else
  38. #define HID_API_EXPORT /* */
  39. #endif
  40. struct hid_device_info HID_API_EXPORT *(*dlsym_hid_enumerate)(unsigned short, unsigned short);
  41. void HID_API_EXPORT (*dlsym_hid_free_enumeration)(struct hid_device_info *);
  42. hid_device * HID_API_EXPORT (*dlsym_hid_open_path)(const char *);
  43. void HID_API_EXPORT (*dlsym_hid_close)(hid_device *);
  44. int HID_API_EXPORT (*dlsym_hid_read)(hid_device *, unsigned char *, size_t);
  45. int HID_API_EXPORT (*dlsym_hid_write)(hid_device *, const unsigned char *, size_t);
  46. #define LOAD_SYM(sym) do { \
  47. if (!(dlsym_ ## sym = dlsym(dlh, #sym))) { \
  48. applog(LOG_DEBUG, "%s: Failed to load %s in %s", __func__, #sym, dlname); \
  49. goto fail; \
  50. } \
  51. } while(0)
  52. static
  53. bool hidapi_try_lib(const char * const dlname)
  54. {
  55. struct hid_device_info *hid_enum;
  56. dlh_t dlh;
  57. dlh = dlopen(dlname, RTLD_NOW);
  58. if (!dlh)
  59. {
  60. applog(LOG_DEBUG, "%s: Couldn't load %s: %s", __func__, dlname, dlerror());
  61. return false;
  62. }
  63. LOAD_SYM(hid_enumerate);
  64. LOAD_SYM(hid_free_enumeration);
  65. hid_enum = dlsym_hid_enumerate(0, 0);
  66. if (!hid_enum)
  67. {
  68. applog(LOG_DEBUG, "%s: Loaded %s, but no devices enumerated; trying other libraries", __func__, dlname);
  69. goto fail;
  70. }
  71. dlsym_hid_free_enumeration(hid_enum);
  72. LOAD_SYM(hid_open_path);
  73. LOAD_SYM(hid_close);
  74. LOAD_SYM(hid_read);
  75. LOAD_SYM(hid_write);
  76. applog(LOG_DEBUG, "%s: Successfully loaded %s", __func__, dlname);
  77. return true;
  78. fail:
  79. dlclose(dlh);
  80. return false;
  81. }
  82. #define hid_enumerate dlsym_hid_enumerate
  83. #define hid_free_enumeration dlsym_hid_free_enumeration
  84. #define hid_open_path dlsym_hid_open_path
  85. #define hid_close dlsym_hid_close
  86. #define hid_read dlsym_hid_read
  87. #define hid_write dlsym_hid_write
  88. static
  89. bool hidapi_load_library()
  90. {
  91. if (dlsym_hid_write)
  92. return true;
  93. const char **p;
  94. char dlname[23] = "libhidapi";
  95. const char *dltry[] = {
  96. "",
  97. "-0",
  98. "-hidraw",
  99. "-libusb",
  100. NULL
  101. };
  102. for (p = &dltry[0]; *p; ++p)
  103. {
  104. sprintf(&dlname[9], "%s.%s", *p,
  105. #ifdef WIN32
  106. "dll"
  107. #else
  108. "so"
  109. #endif
  110. );
  111. if (hidapi_try_lib(dlname))
  112. return true;
  113. }
  114. return false;
  115. }
  116. static
  117. void mcp2210_devinfo_free(struct lowlevel_device_info * const info)
  118. {
  119. free(info->lowl_data);
  120. }
  121. static
  122. char *wcs2str_dup(wchar_t *ws)
  123. {
  124. if (!ws)
  125. return NULL;
  126. char *rv;
  127. int clen, i;
  128. clen = wcslen(ws);
  129. ++clen;
  130. rv = malloc(clen);
  131. for (i = 0; i < clen; ++i)
  132. rv[i] = ws[i];
  133. return rv;
  134. }
  135. static
  136. struct lowlevel_device_info *mcp2210_devinfo_scan()
  137. {
  138. if (!hidapi_load_library())
  139. {
  140. applog(LOG_DEBUG, "%s: Failed to load any hidapi library", __func__);
  141. return NULL;
  142. }
  143. struct hid_device_info *hid_enum, *hid_item;
  144. struct lowlevel_device_info *info, *devinfo_list = NULL;
  145. hid_enum = hid_enumerate(MCP2210_IDVENDOR, MCP2210_IDPRODUCT);
  146. if (!hid_enum)
  147. {
  148. applog(LOG_DEBUG, "%s: No MCP2210 devices found", __func__);
  149. return NULL;
  150. }
  151. LL_FOREACH(hid_enum, hid_item)
  152. {
  153. info = malloc(sizeof(struct lowlevel_device_info));
  154. *info = (struct lowlevel_device_info){
  155. .lowl = &lowl_mcp2210,
  156. .lowl_data = strdup(hid_item->path),
  157. .product = wcs2str_dup(hid_item->product_string),
  158. .serial = wcs2str_dup(hid_item->serial_number),
  159. };
  160. LL_PREPEND(devinfo_list, info);
  161. applog(LOG_DEBUG, "%s: Found \"%s\" serial \"%s\"",
  162. __func__, info->product, info->serial);
  163. }
  164. hid_free_enumeration(hid_enum);
  165. return devinfo_list;
  166. }
  167. struct mcp2210_device {
  168. hid_device *hid;
  169. // http://ww1.microchip.com/downloads/en/DeviceDoc/22288A.pdf pg 34
  170. uint8_t cfg_spi[0x11];
  171. // http://ww1.microchip.com/downloads/en/DeviceDoc/22288A.pdf pg 40
  172. uint8_t cfg_gpio[0xf];
  173. };
  174. struct device_drv *bfg_claim_mcp2210(struct device_drv * const api, const bool verbose, const struct lowlevel_device_info * const info)
  175. {
  176. const char * const path = info->lowl_data;
  177. return bfg_claim_hid(api, verbose, path);
  178. }
  179. static
  180. bool mcp2210_io(hid_device * const hid, uint8_t * const cmd, uint8_t * const buf)
  181. {
  182. return likely(
  183. 0x41 == hid_write(hid, cmd, 0x41) &&
  184. 64 == hid_read(hid, buf, 64)
  185. );
  186. }
  187. static
  188. bool mcp2210_get_configs(struct mcp2210_device * const h)
  189. {
  190. hid_device * const hid = h->hid;
  191. uint8_t cmd[0x41] = {0,0x41}, buf[0x40];
  192. if (!mcp2210_io(hid, cmd, buf))
  193. {
  194. applog(LOG_ERR, "%s: Failed to get current %s config", __func__, "SPI");
  195. return false;
  196. }
  197. memcpy(h->cfg_spi, &buf[4], sizeof(h->cfg_spi));
  198. cmd[1] = 0x20;
  199. if (!mcp2210_io(hid, cmd, buf))
  200. {
  201. applog(LOG_ERR, "%s: Failed to get current %s config", __func__, "GPIO");
  202. return false;
  203. }
  204. memcpy(h->cfg_gpio, &buf[4], sizeof(h->cfg_gpio));
  205. return true;
  206. }
  207. struct mcp2210_device *mcp2210_open(struct lowlevel_device_info *info)
  208. {
  209. struct mcp2210_device *h;
  210. char * const path = info->lowl_data;
  211. hid_device * const hid = hid_open_path(path);
  212. if (unlikely(!hid))
  213. return NULL;
  214. h = malloc(sizeof(*h));
  215. h->hid = hid;
  216. if (!mcp2210_get_configs(h))
  217. goto fail;
  218. return h;
  219. fail:
  220. free(h);
  221. return NULL;
  222. }
  223. void mcp2210_close(struct mcp2210_device * const h)
  224. {
  225. hid_close(h->hid);
  226. free(h);
  227. }
  228. static
  229. bool mcp2210_set_cfg_spi(struct mcp2210_device * const h)
  230. {
  231. hid_device * const hid = h->hid;
  232. uint8_t cmd[0x41] = {0,0x40}, buf[0x40];
  233. memcpy(&cmd[5], h->cfg_spi, sizeof(h->cfg_spi));
  234. if (!mcp2210_io(hid, cmd, buf))
  235. {
  236. applog(LOG_ERR, "%s: Failed to set current %s config", __func__, "SPI");
  237. return false;
  238. }
  239. if (buf[1] != 0)
  240. {
  241. applog(LOG_ERR, "%s: Error setting current %s config (%d)", __func__, "SPI", buf[1]);
  242. return false;
  243. }
  244. return true;
  245. }
  246. 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)
  247. {
  248. uint8_t * const cfg = h->cfg_spi;
  249. cfg[0] = (bitrate >> 0x00) & 0xff;
  250. cfg[1] = (bitrate >> 0x08) & 0xff;
  251. cfg[2] = (bitrate >> 0x10) & 0xff;
  252. cfg[3] = (bitrate >> 0x18) & 0xff;
  253. cfg[4] = ( idlechipsel >> 0) & 0xff;
  254. cfg[5] = ( idlechipsel >> 8) & 0xff;
  255. cfg[6] = (activechipsel >> 0) & 0xff;
  256. cfg[7] = (activechipsel >> 8) & 0xff;
  257. cfg[8] = (chipseltodatadelay >> 0) & 0xff;
  258. cfg[9] = (chipseltodatadelay >> 8) & 0xff;
  259. cfg[0xa] = (lastbytetocsdelay >> 0) & 0xff;
  260. cfg[0xb] = (lastbytetocsdelay >> 8) & 0xff;
  261. cfg[0xc] = (midbytedelay >> 0) & 0xff;
  262. cfg[0xd] = (midbytedelay >> 8) & 0xff;
  263. return mcp2210_set_cfg_spi(h);
  264. }
  265. bool mcp2210_set_spimode(struct mcp2210_device * const h, const uint8_t spimode)
  266. {
  267. uint8_t * const cfg = h->cfg_spi;
  268. cfg[0x10] = spimode;
  269. return mcp2210_set_cfg_spi(h);
  270. }
  271. bool mcp2210_spi_transfer(struct mcp2210_device * const h, const void * const tx, void * const rx, uint8_t sz)
  272. {
  273. hid_device * const hid = h->hid;
  274. uint8_t * const cfg = h->cfg_spi;
  275. uint8_t cmd[0x41] = {0,0x42}, buf[0x40];
  276. uint8_t *p = rx;
  277. if (unlikely(sz > 60))
  278. {
  279. applog(LOG_ERR, "%s: SPI transfer too long (%d bytes)", __func__, sz);
  280. return false;
  281. }
  282. cfg[0xe] = sz;
  283. cfg[0xf] = 0;
  284. if (!mcp2210_set_cfg_spi(h))
  285. return false;
  286. cmd[2] = sz;
  287. memcpy(&cmd[5], tx, sz);
  288. if (unlikely(!mcp2210_io(hid, cmd, buf)))
  289. {
  290. applog(LOG_ERR, "%s: Failed to issue SPI transfer", __func__);
  291. return false;
  292. }
  293. while (true)
  294. {
  295. switch (buf[1])
  296. {
  297. case 0: // accepted
  298. cmd[2] = 0;
  299. break;
  300. case 0xf8: // transfer in progress
  301. applog(LOG_DEBUG, "%s: SPI transfer rejected temporarily (%d bytes remaining)", __func__, sz);
  302. cgsleep_ms(20);
  303. goto retry;
  304. default:
  305. applog(LOG_ERR, "%s: SPI transfer error (%d) (%d bytes remaining)", __func__, buf[1], sz);
  306. return false;
  307. }
  308. if (buf[2] >= sz)
  309. {
  310. if (buf[2] > sz)
  311. applog(LOG_WARNING, "%s: Received %d extra bytes in SPI transfer", __func__, sz - buf[2]);
  312. memcpy(p, &buf[4], sz);
  313. return true;
  314. }
  315. memcpy(p, &buf[4], buf[2]);
  316. p += buf[2];
  317. sz -= buf[2];
  318. retry:
  319. if (unlikely(!mcp2210_io(hid, cmd, buf)))
  320. {
  321. applog(LOG_ERR, "%s: Failed to continue SPI transfer (%d bytes remaining)", __func__, sz);
  322. return false;
  323. }
  324. }
  325. }
  326. static
  327. bool mcp2210_set_cfg_gpio(struct mcp2210_device * const h)
  328. {
  329. hid_device * const hid = h->hid;
  330. uint8_t cmd[0x41] = {0,0x21}, buf[0x40];
  331. // NOTE: NVRAM chip params access control is not set here
  332. memcpy(&cmd[5], h->cfg_gpio, 0xe);
  333. if (!mcp2210_io(hid, cmd, buf))
  334. {
  335. applog(LOG_ERR, "%s: Failed to set current %s config", __func__, "GPIO");
  336. return false;
  337. }
  338. if (buf[1] != 0)
  339. {
  340. applog(LOG_ERR, "%s: Error setting current %s config (%d)", __func__, "GPIO", buf[1]);
  341. return false;
  342. }
  343. return true;
  344. }
  345. bool mcp2210_set_gpio_output(struct mcp2210_device * const h, const int pin, const enum mcp2210_gpio_value d)
  346. {
  347. const int bit = 1 << (pin % 8);
  348. const int byte = (pin / 8);
  349. // Set pin to GPIO mode
  350. h->cfg_gpio[pin] = 0;
  351. // Set GPIO to output mode
  352. h->cfg_gpio[byte + 0xb] &= ~bit;
  353. // Set value for GPIO output
  354. if (d == MGV_HIGH)
  355. h->cfg_gpio[byte + 9] |= bit;
  356. else
  357. h->cfg_gpio[byte + 9] &= ~bit;
  358. return mcp2210_set_cfg_gpio(h);
  359. }
  360. enum mcp2210_gpio_value mcp2210_get_gpio_input(struct mcp2210_device * const h, const int pin)
  361. {
  362. hid_device * const hid = h->hid;
  363. uint8_t cmd[0x41] = {0,0x31}, buf[0x40];
  364. const int bit = 1 << (pin % 8);
  365. const int byte = (pin / 8);
  366. // Set pin to GPIO mode
  367. h->cfg_gpio[pin] = 0;
  368. // Set GPIO to input mode
  369. h->cfg_gpio[byte + 0xb] |= bit;
  370. if (!mcp2210_set_cfg_gpio(h))
  371. return MGV_ERROR;
  372. if (!mcp2210_io(hid, cmd, buf))
  373. {
  374. applog(LOG_ERR, "%s: Failed to get current GPIO input values", __func__);
  375. return MGV_ERROR;
  376. }
  377. if (buf[byte + 4] & bit)
  378. return MGV_HIGH;
  379. else
  380. return MGV_LOW;
  381. }
  382. struct lowlevel_driver lowl_mcp2210 = {
  383. .devinfo_scan = mcp2210_devinfo_scan,
  384. .devinfo_free = mcp2210_devinfo_free,
  385. };