dso.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /*
  2. * This file is part of the libsigrok project.
  3. *
  4. * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
  5. * With protocol information from the hantekdso project,
  6. * Copyright (C) 2008 Oleg Khudyakov <prcoder@gmail.com>
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include "libsigrok.h"
  22. #include "libsigrok-internal.h"
  23. #include "dso.h"
  24. #include <string.h>
  25. #include <glib.h>
  26. #include <libusb.h>
  27. extern struct sr_dev_driver hantek_dso_driver_info;
  28. static int send_begin(const struct sr_dev_inst *sdi)
  29. {
  30. struct sr_usb_dev_inst *usb;
  31. int ret;
  32. unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x68, 0xac, 0xfe,
  33. 0x00, 0x01, 0x00};
  34. sr_dbg("Sending CTRL_BEGINCOMMAND.");
  35. usb = sdi->conn;
  36. if ((ret = libusb_control_transfer(usb->devhdl,
  37. LIBUSB_REQUEST_TYPE_VENDOR, CTRL_BEGINCOMMAND,
  38. 0, 0, buffer, sizeof(buffer), 200)) != sizeof(buffer)) {
  39. sr_err("Failed to send begincommand: %s.",
  40. libusb_error_name(ret));
  41. return SR_ERR;
  42. }
  43. return SR_OK;
  44. }
  45. static int send_bulkcmd(const struct sr_dev_inst *sdi, uint8_t *cmdstring, int cmdlen)
  46. {
  47. struct sr_usb_dev_inst *usb;
  48. int ret, tmp;
  49. usb = sdi->conn;
  50. if (send_begin(sdi) != SR_OK)
  51. return SR_ERR;
  52. if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT, cmdstring,
  53. cmdlen, &tmp, 200)) != 0)
  54. return SR_ERR;
  55. return SR_OK;
  56. }
  57. static int dso_getmps(libusb_device *dev)
  58. {
  59. struct libusb_device_descriptor des;
  60. struct libusb_config_descriptor *conf_dsc;
  61. const struct libusb_interface_descriptor *intf_dsc;
  62. int mps;
  63. if (libusb_get_device_descriptor(dev, &des) != 0)
  64. return 0;
  65. if (des.bNumConfigurations != 1)
  66. return 0;
  67. if (libusb_get_config_descriptor(dev, 0, &conf_dsc) != 0)
  68. return 0;
  69. mps = 0;
  70. intf_dsc = &(conf_dsc->interface[0].altsetting[0]);
  71. if (intf_dsc->bNumEndpoints != 2)
  72. goto err;
  73. if ((intf_dsc->endpoint[0].bEndpointAddress & 0x8f) !=
  74. (2 | LIBUSB_ENDPOINT_OUT))
  75. /* The first endpoint should be 2 (outbound). */
  76. goto err;
  77. if ((intf_dsc->endpoint[1].bEndpointAddress & 0x8f) !=
  78. (6 | LIBUSB_ENDPOINT_IN))
  79. /* The second endpoint should be 6 (inbound). */
  80. goto err;
  81. mps = intf_dsc->endpoint[1].wMaxPacketSize;
  82. err:
  83. if (conf_dsc)
  84. libusb_free_config_descriptor(conf_dsc);
  85. return mps;
  86. }
  87. SR_PRIV int dso_open(struct sr_dev_inst *sdi)
  88. {
  89. struct dev_context *devc;
  90. struct drv_context *drvc = hantek_dso_driver_info.priv;
  91. struct sr_usb_dev_inst *usb;
  92. struct libusb_device_descriptor des;
  93. libusb_device **devlist;
  94. int err, skip, i;
  95. devc = sdi->priv;
  96. usb = sdi->conn;
  97. if (sdi->status == SR_ST_ACTIVE)
  98. /* already in use */
  99. return SR_ERR;
  100. skip = 0;
  101. libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
  102. for (i = 0; devlist[i]; i++) {
  103. if ((err = libusb_get_device_descriptor(devlist[i], &des))) {
  104. sr_err("Failed to get device descriptor: %s.",
  105. libusb_error_name(err));
  106. continue;
  107. }
  108. if (des.idVendor != devc->profile->fw_vid
  109. || des.idProduct != devc->profile->fw_pid)
  110. continue;
  111. if (sdi->status == SR_ST_INITIALIZING) {
  112. if (skip != sdi->index) {
  113. /* Skip devices of this type that aren't the one we want. */
  114. skip += 1;
  115. continue;
  116. }
  117. } else if (sdi->status == SR_ST_INACTIVE) {
  118. /*
  119. * This device is fully enumerated, so we need to find
  120. * this device by vendor, product, bus and address.
  121. */
  122. if (libusb_get_bus_number(devlist[i]) != usb->bus
  123. || libusb_get_device_address(devlist[i]) != usb->address)
  124. /* this is not the one */
  125. continue;
  126. }
  127. if (!(err = libusb_open(devlist[i], &usb->devhdl))) {
  128. if (usb->address == 0xff)
  129. /*
  130. * first time we touch this device after firmware upload,
  131. * so we don't know the address yet.
  132. */
  133. usb->address = libusb_get_device_address(devlist[i]);
  134. if (!(devc->epin_maxpacketsize = dso_getmps(devlist[i])))
  135. sr_err("Wrong endpoint profile.");
  136. else {
  137. sdi->status = SR_ST_ACTIVE;
  138. sr_info("Opened device %d on %d.%d interface %d.",
  139. sdi->index, usb->bus,
  140. usb->address, USB_INTERFACE);
  141. }
  142. } else {
  143. sr_err("Failed to open device: %s.",
  144. libusb_error_name(err));
  145. }
  146. /* If we made it here, we handled the device (somehow). */
  147. break;
  148. }
  149. libusb_free_device_list(devlist, 1);
  150. if (sdi->status != SR_ST_ACTIVE)
  151. return SR_ERR;
  152. return SR_OK;
  153. }
  154. SR_PRIV void dso_close(struct sr_dev_inst *sdi)
  155. {
  156. struct sr_usb_dev_inst *usb;
  157. usb = sdi->conn;
  158. if (usb->devhdl == NULL)
  159. return;
  160. sr_info("Closing device %d on %d.%d interface %d.", sdi->index,
  161. usb->bus, usb->address, USB_INTERFACE);
  162. libusb_release_interface(usb->devhdl, USB_INTERFACE);
  163. libusb_close(usb->devhdl);
  164. usb->devhdl = NULL;
  165. sdi->status = SR_ST_INACTIVE;
  166. }
  167. static int get_channel_offsets(const struct sr_dev_inst *sdi)
  168. {
  169. struct dev_context *devc;
  170. struct sr_usb_dev_inst *usb;
  171. GString *gs;
  172. int chan, v, ret;
  173. sr_dbg("Getting channel offsets.");
  174. devc = sdi->priv;
  175. usb = sdi->conn;
  176. ret = libusb_control_transfer(usb->devhdl,
  177. LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR,
  178. CTRL_READ_EEPROM, EEPROM_CHANNEL_OFFSETS, 0,
  179. (unsigned char *)&devc->channel_levels,
  180. sizeof(devc->channel_levels), 200);
  181. if (ret != sizeof(devc->channel_levels)) {
  182. sr_err("Failed to get channel offsets: %s.",
  183. libusb_error_name(ret));
  184. return SR_ERR;
  185. }
  186. /* Comes in as 16-bit numbers with the second byte always 0 on
  187. * the DSO-2090. Guessing this is supposed to be big-endian,
  188. * since that's how voltage offsets are submitted back to the DSO.
  189. * Convert to host order now, so we can use them natively.
  190. */
  191. for (chan = 0; chan < 2; chan++) {
  192. for (v = 0; v < 9; v++) {
  193. devc->channel_levels[chan][v][0] =
  194. g_ntohs(devc->channel_levels[chan][v][0]);
  195. devc->channel_levels[chan][v][1] =
  196. g_ntohs(devc->channel_levels[chan][v][1]);
  197. }
  198. }
  199. if (sr_log_loglevel_get() >= SR_LOG_DBG) {
  200. gs = g_string_sized_new(128);
  201. for (chan = 0; chan < 2; chan++) {
  202. g_string_printf(gs, "CH%d:", chan + 1);
  203. for (v = 0; v < 9; v++) {
  204. g_string_append_printf(gs, " %.4x-%.4x",
  205. devc->channel_levels[chan][v][0],
  206. devc->channel_levels[chan][v][1]);
  207. }
  208. sr_dbg("%s", gs->str);
  209. }
  210. g_string_free(gs, TRUE);
  211. }
  212. return SR_OK;
  213. }
  214. static int dso_set_trigger_samplerate(const struct sr_dev_inst *sdi)
  215. {
  216. struct dev_context *devc;
  217. struct sr_usb_dev_inst *usb;
  218. int ret, tmp;
  219. uint8_t cmdstring[12];
  220. uint16_t timebase_small[] = { 0xffff, 0xfffc, 0xfff7, 0xffe8, 0xffce,
  221. 0xff9c, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79, 0xd8f1 };
  222. uint16_t timebase_large[] = { 0xffff, 0x0000, 0xfffc, 0xfff7, 0xffe8,
  223. 0xffce, 0xff9d, 0xff07, 0xfe0d, 0xfc19, 0xf63d, 0xec79 };
  224. sr_dbg("Preparing CMD_SET_TRIGGER_SAMPLERATE.");
  225. devc = sdi->priv;
  226. usb = sdi->conn;
  227. memset(cmdstring, 0, sizeof(cmdstring));
  228. /* Command */
  229. cmdstring[0] = CMD_SET_TRIGGER_SAMPLERATE;
  230. /* Trigger source */
  231. sr_dbg("Trigger source %s.", devc->triggersource);
  232. if (!strcmp("CH2", devc->triggersource))
  233. tmp = 0;
  234. else if (!strcmp("CH1", devc->triggersource))
  235. tmp = 1;
  236. else if (!strcmp("EXT", devc->triggersource))
  237. tmp = 2;
  238. else {
  239. sr_err("Invalid trigger source: '%s'.", devc->triggersource);
  240. return SR_ERR_ARG;
  241. }
  242. cmdstring[2] = tmp;
  243. /* Frame size */
  244. sr_dbg("Frame size: %d.", devc->framesize);
  245. cmdstring[2] |= (devc->framesize == FRAMESIZE_SMALL ? 0x01 : 0x02) << 2;
  246. /* Timebase fast */
  247. sr_dbg("Time base index: %d.", devc->timebase);
  248. if (devc->framesize == FRAMESIZE_SMALL) {
  249. if (devc->timebase < TIME_20us)
  250. tmp = 0;
  251. else if (devc->timebase == TIME_20us)
  252. tmp = 1;
  253. else if (devc->timebase == TIME_40us)
  254. tmp = 2;
  255. else if (devc->timebase == TIME_100us)
  256. tmp = 3;
  257. else if (devc->timebase >= TIME_200us)
  258. tmp = 4;
  259. } else {
  260. if (devc->timebase < TIME_40us) {
  261. sr_err("Timebase < 40us only supported with 10K buffer.");
  262. return SR_ERR_ARG;
  263. }
  264. else if (devc->timebase == TIME_40us)
  265. tmp = 0;
  266. else if (devc->timebase == TIME_100us)
  267. tmp = 2;
  268. else if (devc->timebase == TIME_200us)
  269. tmp = 3;
  270. else if (devc->timebase >= TIME_400us)
  271. tmp = 4;
  272. }
  273. cmdstring[2] |= (tmp & 0x07) << 5;
  274. /* Enabled channels: 00=CH1 01=CH2 10=both */
  275. sr_dbg("Channels CH1=%d CH2=%d", devc->ch1_enabled, devc->ch2_enabled);
  276. tmp = (((devc->ch2_enabled ? 1 : 0) << 1) + (devc->ch1_enabled ? 1 : 0)) - 1;
  277. cmdstring[3] = tmp;
  278. /* Fast rates channel */
  279. /* TODO: Is this right? */
  280. tmp = devc->timebase < TIME_10us ? 1 : 0;
  281. cmdstring[3] |= tmp << 2;
  282. /* Trigger slope: 0=positive 1=negative */
  283. /* TODO: Does this work? */
  284. sr_dbg("Trigger slope: %d.", devc->triggerslope);
  285. cmdstring[3] |= (devc->triggerslope == SLOPE_NEGATIVE ? 1 : 0) << 3;
  286. /* Timebase slow */
  287. if (devc->timebase < TIME_100us)
  288. tmp = 0;
  289. else if (devc->timebase > TIME_400ms)
  290. tmp = 0xffed;
  291. else {
  292. if (devc->framesize == FRAMESIZE_SMALL)
  293. tmp = timebase_small[devc->timebase - 3];
  294. else
  295. tmp = timebase_large[devc->timebase - 3];
  296. }
  297. cmdstring[4] = tmp & 0xff;
  298. cmdstring[5] = (tmp >> 8) & 0xff;
  299. /* Horizontal trigger position */
  300. sr_dbg("Trigger position: %3.2f.", devc->triggerposition);
  301. tmp = 0x77fff + 0x8000 * devc->triggerposition;
  302. cmdstring[6] = tmp & 0xff;
  303. cmdstring[7] = (tmp >> 8) & 0xff;
  304. cmdstring[10] = (tmp >> 16) & 0xff;
  305. if (send_begin(sdi) != SR_OK)
  306. return SR_ERR;
  307. if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
  308. cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
  309. sr_err("Failed to set trigger/samplerate: %s.",
  310. libusb_error_name(ret));
  311. return SR_ERR;
  312. }
  313. sr_dbg("Sent CMD_SET_TRIGGER_SAMPLERATE.");
  314. return SR_OK;
  315. }
  316. static int dso_set_filters(const struct sr_dev_inst *sdi)
  317. {
  318. struct dev_context *devc;
  319. struct sr_usb_dev_inst *usb;
  320. int ret, tmp;
  321. uint8_t cmdstring[8];
  322. sr_dbg("Preparing CMD_SET_FILTERS.");
  323. devc = sdi->priv;
  324. usb = sdi->conn;
  325. memset(cmdstring, 0, sizeof(cmdstring));
  326. cmdstring[0] = CMD_SET_FILTERS;
  327. cmdstring[1] = 0x0f;
  328. if (devc->filter_ch1) {
  329. sr_dbg("Turning on CH1 filter.");
  330. cmdstring[2] |= 0x80;
  331. }
  332. if (devc->filter_ch2) {
  333. sr_dbg("Turning on CH2 filter.");
  334. cmdstring[2] |= 0x40;
  335. }
  336. if (devc->filter_trigger) {
  337. /* TODO: supported on the DSO-2090? */
  338. sr_dbg("Turning on trigger filter.");
  339. cmdstring[2] |= 0x20;
  340. }
  341. if (send_begin(sdi) != SR_OK)
  342. return SR_ERR;
  343. if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
  344. cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
  345. sr_err("Failed to set filters: %s.", libusb_error_name(ret));
  346. return SR_ERR;
  347. }
  348. sr_dbg("Sent CMD_SET_FILTERS.");
  349. return SR_OK;
  350. }
  351. static int dso_set_voltage(const struct sr_dev_inst *sdi)
  352. {
  353. struct dev_context *devc;
  354. struct sr_usb_dev_inst *usb;
  355. int ret, tmp;
  356. uint8_t cmdstring[8];
  357. sr_dbg("Preparing CMD_SET_VOLTAGE.");
  358. devc = sdi->priv;
  359. usb = sdi->conn;
  360. memset(cmdstring, 0, sizeof(cmdstring));
  361. cmdstring[0] = CMD_SET_VOLTAGE;
  362. cmdstring[1] = 0x0f;
  363. cmdstring[2] = 0x30;
  364. /* CH1 volts/div is encoded in bits 0-1 */
  365. sr_dbg("CH1 vdiv index: %d.", devc->voltage_ch1);
  366. switch (devc->voltage_ch1) {
  367. case VDIV_1V:
  368. case VDIV_100MV:
  369. case VDIV_10MV:
  370. cmdstring[2] |= 0x00;
  371. break;
  372. case VDIV_2V:
  373. case VDIV_200MV:
  374. case VDIV_20MV:
  375. cmdstring[2] |= 0x01;
  376. break;
  377. case VDIV_5V:
  378. case VDIV_500MV:
  379. case VDIV_50MV:
  380. cmdstring[2] |= 0x02;
  381. break;
  382. }
  383. /* CH2 volts/div is encoded in bits 2-3 */
  384. sr_dbg("CH2 vdiv index: %d.", devc->voltage_ch2);
  385. switch (devc->voltage_ch2) {
  386. case VDIV_1V:
  387. case VDIV_100MV:
  388. case VDIV_10MV:
  389. cmdstring[2] |= 0x00;
  390. break;
  391. case VDIV_2V:
  392. case VDIV_200MV:
  393. case VDIV_20MV:
  394. cmdstring[2] |= 0x04;
  395. break;
  396. case VDIV_5V:
  397. case VDIV_500MV:
  398. case VDIV_50MV:
  399. cmdstring[2] |= 0x08;
  400. break;
  401. }
  402. if (send_begin(sdi) != SR_OK)
  403. return SR_ERR;
  404. if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
  405. cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
  406. sr_err("Failed to set voltage: %s.", libusb_error_name(ret));
  407. return SR_ERR;
  408. }
  409. sr_dbg("Sent CMD_SET_VOLTAGE.");
  410. return SR_OK;
  411. }
  412. static int dso_set_relays(const struct sr_dev_inst *sdi)
  413. {
  414. struct dev_context *devc;
  415. struct sr_usb_dev_inst *usb;
  416. GString *gs;
  417. int ret, i;
  418. uint8_t relays[17] = { 0x00, 0x04, 0x08, 0x02, 0x20, 0x40, 0x10, 0x01,
  419. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  420. sr_dbg("Preparing CTRL_SETRELAYS.");
  421. devc = sdi->priv;
  422. usb = sdi->conn;
  423. if (devc->voltage_ch1 < VDIV_1V)
  424. relays[1] = ~relays[1];
  425. if (devc->voltage_ch1 < VDIV_100MV)
  426. relays[2] = ~relays[2];
  427. sr_dbg("CH1 coupling: %d.", devc->coupling_ch1);
  428. if (devc->coupling_ch1 != COUPLING_AC)
  429. relays[3] = ~relays[3];
  430. if (devc->voltage_ch2 < VDIV_1V)
  431. relays[4] = ~relays[4];
  432. if (devc->voltage_ch2 < VDIV_100MV)
  433. relays[5] = ~relays[5];
  434. sr_dbg("CH2 coupling: %d.", devc->coupling_ch1);
  435. if (devc->coupling_ch2 != COUPLING_AC)
  436. relays[6] = ~relays[6];
  437. if (!strcmp(devc->triggersource, "EXT"))
  438. relays[7] = ~relays[7];
  439. if (sr_log_loglevel_get() >= SR_LOG_DBG) {
  440. gs = g_string_sized_new(128);
  441. g_string_printf(gs, "Relays:");
  442. for (i = 0; i < 17; i++)
  443. g_string_append_printf(gs, " %.2x", relays[i]);
  444. sr_dbg("%s", gs->str);
  445. g_string_free(gs, TRUE);
  446. }
  447. if ((ret = libusb_control_transfer(usb->devhdl,
  448. LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETRELAYS,
  449. 0, 0, relays, 17, 100)) != sizeof(relays)) {
  450. sr_err("Failed to set relays: %s.", libusb_error_name(ret));
  451. return SR_ERR;
  452. }
  453. sr_dbg("Sent CTRL_SETRELAYS.");
  454. return SR_OK;
  455. }
  456. static int dso_set_voffsets(const struct sr_dev_inst *sdi)
  457. {
  458. struct dev_context *devc;
  459. struct sr_usb_dev_inst *usb;
  460. int offset, ret;
  461. uint16_t *ch_levels;
  462. uint8_t offsets[17];
  463. sr_dbg("Preparing CTRL_SETOFFSET.");
  464. devc = sdi->priv;
  465. usb = sdi->conn;
  466. memset(offsets, 0, sizeof(offsets));
  467. /* Channel 1 */
  468. ch_levels = devc->channel_levels[0][devc->voltage_ch1];
  469. offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch1 + ch_levels[0];
  470. offsets[0] = (offset >> 8) | 0x20;
  471. offsets[1] = offset & 0xff;
  472. sr_dbg("CH1 offset: %3.2f (%.2x%.2x).", devc->voffset_ch1,
  473. offsets[0], offsets[1]);
  474. /* Channel 2 */
  475. ch_levels = devc->channel_levels[1][devc->voltage_ch2];
  476. offset = (ch_levels[1] - ch_levels[0]) * devc->voffset_ch2 + ch_levels[0];
  477. offsets[2] = (offset >> 8) | 0x20;
  478. offsets[3] = offset & 0xff;
  479. sr_dbg("CH2 offset: %3.2f (%.2x%.2x).", devc->voffset_ch2,
  480. offsets[2], offsets[3]);
  481. /* Trigger */
  482. offset = MAX_VERT_TRIGGER * devc->voffset_trigger;
  483. offsets[4] = (offset >> 8) | 0x20;
  484. offsets[5] = offset & 0xff;
  485. sr_dbg("Trigger offset: %3.2f (%.2x%.2x).", devc->voffset_trigger,
  486. offsets[4], offsets[5]);
  487. if ((ret = libusb_control_transfer(usb->devhdl,
  488. LIBUSB_REQUEST_TYPE_VENDOR, CTRL_SETOFFSET,
  489. 0, 0, offsets, sizeof(offsets), 100)) != sizeof(offsets)) {
  490. sr_err("Failed to set offsets: %s.", libusb_error_name(ret));
  491. return SR_ERR;
  492. }
  493. sr_dbg("Sent CTRL_SETOFFSET.");
  494. return SR_OK;
  495. }
  496. SR_PRIV int dso_enable_trigger(const struct sr_dev_inst *sdi)
  497. {
  498. struct sr_usb_dev_inst *usb;
  499. int ret, tmp;
  500. uint8_t cmdstring[2];
  501. sr_dbg("Sending CMD_ENABLE_TRIGGER.");
  502. usb = sdi->conn;
  503. memset(cmdstring, 0, sizeof(cmdstring));
  504. cmdstring[0] = CMD_ENABLE_TRIGGER;
  505. cmdstring[1] = 0x00;
  506. if (send_begin(sdi) != SR_OK)
  507. return SR_ERR;
  508. if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
  509. cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
  510. sr_err("Failed to enable trigger: %s.", libusb_error_name(ret));
  511. return SR_ERR;
  512. }
  513. return SR_OK;
  514. }
  515. SR_PRIV int dso_force_trigger(const struct sr_dev_inst *sdi)
  516. {
  517. struct sr_usb_dev_inst *usb;
  518. int ret, tmp;
  519. uint8_t cmdstring[2];
  520. sr_dbg("Sending CMD_FORCE_TRIGGER.");
  521. usb = sdi->conn;
  522. memset(cmdstring, 0, sizeof(cmdstring));
  523. cmdstring[0] = CMD_FORCE_TRIGGER;
  524. cmdstring[1] = 0x00;
  525. if (send_begin(sdi) != SR_OK)
  526. return SR_ERR;
  527. if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_OUT,
  528. cmdstring, sizeof(cmdstring), &tmp, 100)) != 0) {
  529. sr_err("Failed to force trigger: %s.", libusb_error_name(ret));
  530. return SR_ERR;
  531. }
  532. return SR_OK;
  533. }
  534. SR_PRIV int dso_init(const struct sr_dev_inst *sdi)
  535. {
  536. sr_dbg("Initializing DSO.");
  537. if (get_channel_offsets(sdi) != SR_OK)
  538. return SR_ERR;
  539. if (dso_set_trigger_samplerate(sdi) != SR_OK)
  540. return SR_ERR;
  541. if (dso_set_filters(sdi) != SR_OK)
  542. return SR_ERR;
  543. if (dso_set_voltage(sdi) != SR_OK)
  544. return SR_ERR;
  545. if (dso_set_relays(sdi) != SR_OK)
  546. return SR_ERR;
  547. if (dso_set_voffsets(sdi) != SR_OK)
  548. return SR_ERR;
  549. if (dso_enable_trigger(sdi) != SR_OK)
  550. return SR_ERR;
  551. return SR_OK;
  552. }
  553. SR_PRIV int dso_get_capturestate(const struct sr_dev_inst *sdi,
  554. uint8_t *capturestate, uint32_t *trigger_offset)
  555. {
  556. struct sr_usb_dev_inst *usb;
  557. int ret, tmp, i;
  558. unsigned int bitvalue, toff;
  559. uint8_t cmdstring[2], inbuf[512];
  560. sr_dbg("Sending CMD_GET_CAPTURESTATE.");
  561. usb = sdi->conn;
  562. cmdstring[0] = CMD_GET_CAPTURESTATE;
  563. cmdstring[1] = 0;
  564. if ((ret = send_bulkcmd(sdi, cmdstring, sizeof(cmdstring))) != SR_OK) {
  565. sr_dbg("Failed to send get_capturestate command: %s.",
  566. libusb_error_name(ret));
  567. return SR_ERR;
  568. }
  569. if ((ret = libusb_bulk_transfer(usb->devhdl, DSO_EP_IN,
  570. inbuf, 512, &tmp, 100)) != 0) {
  571. sr_dbg("Failed to get capturestate: %s.",
  572. libusb_error_name(ret));
  573. return SR_ERR;
  574. }
  575. *capturestate = inbuf[0];
  576. toff = (inbuf[1] << 16) | (inbuf[3] << 8) | inbuf[2];
  577. /*
  578. * This conversion comes from the openhantek project.
  579. * Each set bit in the 24-bit value inverts all bits with a lower
  580. * value. No idea why the device reports the trigger point this way.
  581. */
  582. bitvalue = 1;
  583. for (i = 0; i < 24; i++) {
  584. /* Each set bit inverts all bits with a lower value. */
  585. if(toff & bitvalue)
  586. toff ^= bitvalue - 1;
  587. bitvalue <<= 1;
  588. }
  589. *trigger_offset = toff;
  590. return SR_OK;
  591. }
  592. SR_PRIV int dso_capture_start(const struct sr_dev_inst *sdi)
  593. {
  594. int ret;
  595. uint8_t cmdstring[2];
  596. sr_dbg("Sending CMD_CAPTURE_START.");
  597. cmdstring[0] = CMD_CAPTURE_START;
  598. cmdstring[1] = 0;
  599. if ((ret = send_bulkcmd(sdi, cmdstring, sizeof(cmdstring))) != SR_OK) {
  600. sr_err("Failed to send capture_start command: %s.",
  601. libusb_error_name(ret));
  602. return SR_ERR;
  603. }
  604. return SR_OK;
  605. }
  606. SR_PRIV int dso_get_channeldata(const struct sr_dev_inst *sdi,
  607. libusb_transfer_cb_fn cb)
  608. {
  609. struct dev_context *devc;
  610. struct sr_usb_dev_inst *usb;
  611. struct libusb_transfer *transfer;
  612. int num_transfers, ret, i;
  613. uint8_t cmdstring[2];
  614. unsigned char *buf;
  615. sr_dbg("Sending CMD_GET_CHANNELDATA.");
  616. devc = sdi->priv;
  617. usb = sdi->conn;
  618. cmdstring[0] = CMD_GET_CHANNELDATA;
  619. cmdstring[1] = 0;
  620. if ((ret = send_bulkcmd(sdi, cmdstring, sizeof(cmdstring))) != SR_OK) {
  621. sr_err("Failed to get channel data: %s.",
  622. libusb_error_name(ret));
  623. return SR_ERR;
  624. }
  625. /* TODO: DSO-2xxx only. */
  626. num_transfers = devc->framesize *
  627. sizeof(unsigned short) / devc->epin_maxpacketsize;
  628. sr_dbg("Queueing up %d transfers.", num_transfers);
  629. for (i = 0; i < num_transfers; i++) {
  630. if (!(buf = g_try_malloc(devc->epin_maxpacketsize))) {
  631. sr_err("Failed to malloc USB endpoint buffer.");
  632. return SR_ERR_MALLOC;
  633. }
  634. transfer = libusb_alloc_transfer(0);
  635. libusb_fill_bulk_transfer(transfer, usb->devhdl, DSO_EP_IN, buf,
  636. devc->epin_maxpacketsize, cb, (void *)sdi, 40);
  637. if ((ret = libusb_submit_transfer(transfer)) != 0) {
  638. sr_err("Failed to submit transfer: %s.",
  639. libusb_error_name(ret));
  640. /* TODO: Free them all. */
  641. libusb_free_transfer(transfer);
  642. g_free(buf);
  643. return SR_ERR;
  644. }
  645. }
  646. return SR_OK;
  647. }