mcp2210.c 7.5 KB

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