mcp2210.c 8.0 KB

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