api.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. /*
  2. * This file is part of the libsigrok project.
  3. *
  4. * Copyright (C) 2012 Bert Vermeulen <bert@biot.com>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <stdio.h>
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25. #include <unistd.h>
  26. #include <string.h>
  27. #include <sys/time.h>
  28. #include <inttypes.h>
  29. #include <glib.h>
  30. #include <libusb.h>
  31. #include "libsigrok.h"
  32. #include "libsigrok-internal.h"
  33. #include "dso.h"
  34. /* Max time in ms before we want to check on USB events */
  35. /* TODO tune this properly */
  36. #define TICK 1
  37. #define NUM_TIMEBASE 10
  38. #define NUM_VDIV 8
  39. static const int32_t scanopts[] = {
  40. SR_CONF_CONN,
  41. };
  42. static const int32_t devopts[] = {
  43. SR_CONF_OSCILLOSCOPE,
  44. SR_CONF_LIMIT_FRAMES,
  45. SR_CONF_CONTINUOUS,
  46. SR_CONF_TIMEBASE,
  47. SR_CONF_BUFFERSIZE,
  48. SR_CONF_TRIGGER_SOURCE,
  49. SR_CONF_TRIGGER_SLOPE,
  50. SR_CONF_HORIZ_TRIGGERPOS,
  51. SR_CONF_FILTER,
  52. SR_CONF_VDIV,
  53. SR_CONF_COUPLING,
  54. SR_CONF_NUM_TIMEBASE,
  55. SR_CONF_NUM_VDIV,
  56. };
  57. static const char *channel_names[] = {
  58. "CH1", "CH2",
  59. NULL,
  60. };
  61. static const uint64_t buffersizes_32k[] = {
  62. 10240, 32768,
  63. };
  64. static const uint64_t buffersizes_512k[] = {
  65. 10240, 524288,
  66. };
  67. static const uint64_t buffersizes_14k[] = {
  68. 10240, 14336,
  69. };
  70. static const struct dso_profile dev_profiles[] = {
  71. { 0x04b4, 0x2090, 0x04b5, 0x2090,
  72. "Hantek", "DSO-2090",
  73. buffersizes_32k,
  74. FIRMWARE_DIR "/hantek-dso-2090.fw" },
  75. { 0x04b4, 0x2150, 0x04b5, 0x2150,
  76. "Hantek", "DSO-2150",
  77. buffersizes_32k,
  78. FIRMWARE_DIR "/hantek-dso-2150.fw" },
  79. { 0x04b4, 0x2250, 0x04b5, 0x2250,
  80. "Hantek", "DSO-2250",
  81. buffersizes_512k,
  82. FIRMWARE_DIR "/hantek-dso-2250.fw" },
  83. { 0x04b4, 0x5200, 0x04b5, 0x5200,
  84. "Hantek", "DSO-5200",
  85. buffersizes_14k,
  86. FIRMWARE_DIR "/hantek-dso-5200.fw" },
  87. { 0x04b4, 0x520a, 0x04b5, 0x520a,
  88. "Hantek", "DSO-5200A",
  89. buffersizes_512k,
  90. FIRMWARE_DIR "/hantek-dso-5200A.fw" },
  91. { 0, 0, 0, 0, 0, 0, 0, 0 },
  92. };
  93. static const uint64_t timebases[][2] = {
  94. /* microseconds */
  95. { 10, 1000000 },
  96. { 20, 1000000 },
  97. { 40, 1000000 },
  98. { 100, 1000000 },
  99. { 200, 1000000 },
  100. { 400, 1000000 },
  101. /* milliseconds */
  102. { 1, 1000 },
  103. { 2, 1000 },
  104. { 4, 1000 },
  105. { 10, 1000 },
  106. { 20, 1000 },
  107. { 40, 1000 },
  108. { 100, 1000 },
  109. { 200, 1000 },
  110. { 400, 1000 },
  111. };
  112. static const uint64_t vdivs[][2] = {
  113. /* millivolts */
  114. { 10, 1000 },
  115. { 20, 1000 },
  116. { 50, 1000 },
  117. { 100, 1000 },
  118. { 200, 1000 },
  119. { 500, 1000 },
  120. /* volts */
  121. { 1, 1 },
  122. { 2, 1 },
  123. { 5, 1 },
  124. };
  125. static const char *trigger_sources[] = {
  126. "CH1",
  127. "CH2",
  128. "EXT",
  129. /* TODO: forced */
  130. };
  131. static const char *filter_targets[] = {
  132. "CH1",
  133. "CH2",
  134. /* TODO: "TRIGGER", */
  135. };
  136. static const char *coupling[] = {
  137. "AC",
  138. "DC",
  139. "GND",
  140. };
  141. SR_PRIV struct sr_dev_driver hantek_dso_driver_info;
  142. static struct sr_dev_driver *di = &hantek_dso_driver_info;
  143. static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
  144. static struct sr_dev_inst *dso_dev_new(int index, const struct dso_profile *prof)
  145. {
  146. struct sr_dev_inst *sdi;
  147. struct sr_channel *ch;
  148. struct drv_context *drvc;
  149. struct dev_context *devc;
  150. int i;
  151. sdi = sr_dev_inst_new(index, SR_ST_INITIALIZING,
  152. prof->vendor, prof->model, NULL);
  153. if (!sdi)
  154. return NULL;
  155. sdi->driver = di;
  156. /*
  157. * Add only the real channels -- EXT isn't a source of data, only
  158. * a trigger source internal to the device.
  159. */
  160. for (i = 0; channel_names[i]; i++) {
  161. if (!(ch = sr_channel_new(i, SR_CHANNEL_ANALOG, TRUE,
  162. channel_names[i])))
  163. return NULL;
  164. sdi->channels = g_slist_append(sdi->channels, ch);
  165. }
  166. if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
  167. sr_err("Device context malloc failed.");
  168. return NULL;
  169. }
  170. devc->profile = prof;
  171. devc->dev_state = IDLE;
  172. devc->timebase = DEFAULT_TIMEBASE;
  173. devc->ch1_enabled = TRUE;
  174. devc->ch2_enabled = TRUE;
  175. devc->voltage_ch1 = DEFAULT_VOLTAGE;
  176. devc->voltage_ch2 = DEFAULT_VOLTAGE;
  177. devc->coupling_ch1 = DEFAULT_COUPLING;
  178. devc->coupling_ch2 = DEFAULT_COUPLING;
  179. devc->voffset_ch1 = DEFAULT_VERT_OFFSET;
  180. devc->voffset_ch2 = DEFAULT_VERT_OFFSET;
  181. devc->voffset_trigger = DEFAULT_VERT_TRIGGERPOS;
  182. devc->framesize = DEFAULT_FRAMESIZE;
  183. devc->triggerslope = SLOPE_POSITIVE;
  184. devc->triggersource = g_strdup(DEFAULT_TRIGGER_SOURCE);
  185. devc->triggerposition = DEFAULT_HORIZ_TRIGGERPOS;
  186. sdi->priv = devc;
  187. drvc = di->priv;
  188. drvc->instances = g_slist_append(drvc->instances, sdi);
  189. return sdi;
  190. }
  191. static int configure_channels(const struct sr_dev_inst *sdi)
  192. {
  193. struct dev_context *devc;
  194. struct sr_channel *ch;
  195. const GSList *l;
  196. int p;
  197. devc = sdi->priv;
  198. g_slist_free(devc->enabled_channels);
  199. devc->ch1_enabled = devc->ch2_enabled = FALSE;
  200. for (l = sdi->channels, p = 0; l; l = l->next, p++) {
  201. ch = l->data;
  202. if (p == 0)
  203. devc->ch1_enabled = ch->enabled;
  204. else
  205. devc->ch2_enabled = ch->enabled;
  206. if (ch->enabled)
  207. devc->enabled_channels = g_slist_append(devc->enabled_channels, ch);
  208. }
  209. return SR_OK;
  210. }
  211. static void clear_dev_context(void *priv)
  212. {
  213. struct dev_context *devc;
  214. devc = priv;
  215. g_free(devc->triggersource);
  216. g_slist_free(devc->enabled_channels);
  217. }
  218. static int dev_clear(void)
  219. {
  220. return std_dev_clear(di, clear_dev_context);
  221. }
  222. static int init(struct sr_context *sr_ctx)
  223. {
  224. return std_init(sr_ctx, di, LOG_PREFIX);
  225. }
  226. static GSList *scan(GSList *options)
  227. {
  228. struct drv_context *drvc;
  229. struct dev_context *devc;
  230. struct sr_dev_inst *sdi;
  231. struct sr_usb_dev_inst *usb;
  232. struct sr_config *src;
  233. const struct dso_profile *prof;
  234. GSList *l, *devices, *conn_devices;
  235. struct libusb_device_descriptor des;
  236. libusb_device **devlist;
  237. int devcnt, ret, i, j;
  238. const char *conn;
  239. drvc = di->priv;
  240. devcnt = 0;
  241. devices = 0;
  242. conn = NULL;
  243. for (l = options; l; l = l->next) {
  244. src = l->data;
  245. if (src->key == SR_CONF_CONN) {
  246. conn = g_variant_get_string(src->data, NULL);
  247. break;
  248. }
  249. }
  250. if (conn)
  251. conn_devices = sr_usb_find(drvc->sr_ctx->libusb_ctx, conn);
  252. else
  253. conn_devices = NULL;
  254. /* Find all Hantek DSO devices and upload firmware to all of them. */
  255. libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
  256. for (i = 0; devlist[i]; i++) {
  257. if (conn) {
  258. usb = NULL;
  259. for (l = conn_devices; l; l = l->next) {
  260. usb = l->data;
  261. if (usb->bus == libusb_get_bus_number(devlist[i])
  262. && usb->address == libusb_get_device_address(devlist[i]))
  263. break;
  264. }
  265. if (!l)
  266. /* This device matched none of the ones that
  267. * matched the conn specification. */
  268. continue;
  269. }
  270. if ((ret = libusb_get_device_descriptor(devlist[i], &des))) {
  271. sr_err("Failed to get device descriptor: %s.",
  272. libusb_error_name(ret));
  273. continue;
  274. }
  275. prof = NULL;
  276. for (j = 0; dev_profiles[j].orig_vid; j++) {
  277. if (des.idVendor == dev_profiles[j].orig_vid
  278. && des.idProduct == dev_profiles[j].orig_pid) {
  279. /* Device matches the pre-firmware profile. */
  280. prof = &dev_profiles[j];
  281. sr_dbg("Found a %s %s.", prof->vendor, prof->model);
  282. sdi = dso_dev_new(devcnt, prof);
  283. devices = g_slist_append(devices, sdi);
  284. devc = sdi->priv;
  285. if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION,
  286. prof->firmware) == SR_OK)
  287. /* Remember when the firmware on this device was updated */
  288. devc->fw_updated = g_get_monotonic_time();
  289. else
  290. sr_err("Firmware upload failed for "
  291. "device %d.", devcnt);
  292. /* Dummy USB address of 0xff will get overwritten later. */
  293. sdi->conn = sr_usb_dev_inst_new(
  294. libusb_get_bus_number(devlist[i]), 0xff, NULL);
  295. devcnt++;
  296. break;
  297. } else if (des.idVendor == dev_profiles[j].fw_vid
  298. && des.idProduct == dev_profiles[j].fw_pid) {
  299. /* Device matches the post-firmware profile. */
  300. prof = &dev_profiles[j];
  301. sr_dbg("Found a %s %s.", prof->vendor, prof->model);
  302. sdi = dso_dev_new(devcnt, prof);
  303. sdi->status = SR_ST_INACTIVE;
  304. devices = g_slist_append(devices, sdi);
  305. devc = sdi->priv;
  306. sdi->inst_type = SR_INST_USB;
  307. sdi->conn = sr_usb_dev_inst_new(
  308. libusb_get_bus_number(devlist[i]),
  309. libusb_get_device_address(devlist[i]), NULL);
  310. devcnt++;
  311. break;
  312. }
  313. }
  314. if (!prof)
  315. /* not a supported VID/PID */
  316. continue;
  317. }
  318. libusb_free_device_list(devlist, 1);
  319. return devices;
  320. }
  321. static GSList *dev_list(void)
  322. {
  323. return ((struct drv_context *)(di->priv))->instances;
  324. }
  325. static int dev_open(struct sr_dev_inst *sdi)
  326. {
  327. struct dev_context *devc;
  328. struct sr_usb_dev_inst *usb;
  329. int64_t timediff_us, timediff_ms;
  330. int err;
  331. devc = sdi->priv;
  332. usb = sdi->conn;
  333. /*
  334. * If the firmware was recently uploaded, wait up to MAX_RENUM_DELAY_MS
  335. * for the FX2 to renumerate.
  336. */
  337. err = SR_ERR;
  338. if (devc->fw_updated > 0) {
  339. sr_info("Waiting for device to reset.");
  340. /* Takes >= 300ms for the FX2 to be gone from the USB bus. */
  341. g_usleep(300 * 1000);
  342. timediff_ms = 0;
  343. while (timediff_ms < MAX_RENUM_DELAY_MS) {
  344. if ((err = dso_open(sdi)) == SR_OK)
  345. break;
  346. g_usleep(100 * 1000);
  347. timediff_us = g_get_monotonic_time() - devc->fw_updated;
  348. timediff_ms = timediff_us / 1000;
  349. sr_spew("Waited %" PRIi64 " ms.", timediff_ms);
  350. }
  351. sr_info("Device came back after %d ms.", timediff_ms);
  352. } else {
  353. err = dso_open(sdi);
  354. }
  355. if (err != SR_OK) {
  356. sr_err("Unable to open device.");
  357. return SR_ERR;
  358. }
  359. err = libusb_claim_interface(usb->devhdl, USB_INTERFACE);
  360. if (err != 0) {
  361. sr_err("Unable to claim interface: %s.",
  362. libusb_error_name(err));
  363. return SR_ERR;
  364. }
  365. return SR_OK;
  366. }
  367. static int dev_close(struct sr_dev_inst *sdi)
  368. {
  369. dso_close(sdi);
  370. return SR_OK;
  371. }
  372. static int cleanup(void)
  373. {
  374. return dev_clear();
  375. }
  376. static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
  377. const struct sr_channel_group *cg)
  378. {
  379. struct sr_usb_dev_inst *usb;
  380. char str[128];
  381. (void)cg;
  382. switch (id) {
  383. case SR_CONF_CONN:
  384. if (!sdi || !sdi->conn)
  385. return SR_ERR_ARG;
  386. usb = sdi->conn;
  387. if (usb->address == 255)
  388. /* Device still needs to re-enumerate after firmware
  389. * upload, so we don't know its (future) address. */
  390. return SR_ERR;
  391. snprintf(str, 128, "%d.%d", usb->bus, usb->address);
  392. *data = g_variant_new_string(str);
  393. break;
  394. case SR_CONF_NUM_TIMEBASE:
  395. *data = g_variant_new_int32(NUM_TIMEBASE);
  396. break;
  397. case SR_CONF_NUM_VDIV:
  398. *data = g_variant_new_int32(NUM_VDIV);
  399. break;
  400. default:
  401. return SR_ERR_NA;
  402. }
  403. return SR_OK;
  404. }
  405. static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
  406. const struct sr_channel_group *cg)
  407. {
  408. struct dev_context *devc;
  409. double tmp_double;
  410. uint64_t tmp_u64, p, q;
  411. int tmp_int, ret;
  412. unsigned int i;
  413. const char *tmp_str;
  414. char **targets;
  415. (void)cg;
  416. if (sdi->status != SR_ST_ACTIVE)
  417. return SR_ERR_DEV_CLOSED;
  418. ret = SR_OK;
  419. devc = sdi->priv;
  420. switch (id) {
  421. case SR_CONF_LIMIT_FRAMES:
  422. devc->limit_frames = g_variant_get_uint64(data);
  423. break;
  424. case SR_CONF_TRIGGER_SLOPE:
  425. tmp_str = g_variant_get_string(data, NULL);
  426. if (!tmp_str || !(tmp_str[0] == 'f' || tmp_str[0] == 'r'))
  427. return SR_ERR_ARG;
  428. devc->triggerslope = (tmp_str[0] == 'r')
  429. ? SLOPE_POSITIVE : SLOPE_NEGATIVE;
  430. break;
  431. case SR_CONF_HORIZ_TRIGGERPOS:
  432. tmp_double = g_variant_get_double(data);
  433. if (tmp_double < 0.0 || tmp_double > 1.0) {
  434. sr_err("Trigger position should be between 0.0 and 1.0.");
  435. ret = SR_ERR_ARG;
  436. } else
  437. devc->triggerposition = tmp_double;
  438. break;
  439. case SR_CONF_BUFFERSIZE:
  440. tmp_u64 = g_variant_get_uint64(data);
  441. for (i = 0; i < 2; i++) {
  442. if (devc->profile->buffersizes[i] == tmp_u64) {
  443. devc->framesize = tmp_u64;
  444. break;
  445. }
  446. }
  447. if (i == 2)
  448. ret = SR_ERR_ARG;
  449. break;
  450. case SR_CONF_TIMEBASE:
  451. g_variant_get(data, "(tt)", &p, &q);
  452. tmp_int = -1;
  453. for (i = 0; i < ARRAY_SIZE(timebases); i++) {
  454. if (timebases[i][0] == p && timebases[i][1] == q) {
  455. tmp_int = i;
  456. break;
  457. }
  458. }
  459. if (tmp_int >= 0)
  460. devc->timebase = tmp_int;
  461. else
  462. ret = SR_ERR_ARG;
  463. break;
  464. case SR_CONF_TRIGGER_SOURCE:
  465. tmp_str = g_variant_get_string(data, NULL);
  466. for (i = 0; trigger_sources[i]; i++) {
  467. if (!strcmp(tmp_str, trigger_sources[i])) {
  468. devc->triggersource = g_strdup(tmp_str);
  469. break;
  470. }
  471. }
  472. if (trigger_sources[i] == 0)
  473. ret = SR_ERR_ARG;
  474. break;
  475. case SR_CONF_FILTER:
  476. tmp_str = g_variant_get_string(data, NULL);
  477. devc->filter_ch1 = devc->filter_ch2 = devc->filter_trigger = 0;
  478. targets = g_strsplit(tmp_str, ",", 0);
  479. for (i = 0; targets[i]; i++) {
  480. if (targets[i] == '\0')
  481. /* Empty filter string can be used to clear them all. */
  482. ;
  483. else if (!strcmp(targets[i], "CH1"))
  484. devc->filter_ch1 = TRUE;
  485. else if (!strcmp(targets[i], "CH2"))
  486. devc->filter_ch2 = TRUE;
  487. else if (!strcmp(targets[i], "TRIGGER"))
  488. devc->filter_trigger = TRUE;
  489. else {
  490. sr_err("Invalid filter target %s.", targets[i]);
  491. ret = SR_ERR_ARG;
  492. }
  493. }
  494. g_strfreev(targets);
  495. break;
  496. case SR_CONF_VDIV:
  497. /* TODO: Not supporting vdiv per channel yet. */
  498. g_variant_get(data, "(tt)", &p, &q);
  499. tmp_int = -1;
  500. for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
  501. if (vdivs[i][0] == p && vdivs[i][1] == q) {
  502. tmp_int = i;
  503. break;
  504. }
  505. }
  506. if (tmp_int >= 0) {
  507. devc->voltage_ch1 = tmp_int;
  508. devc->voltage_ch2 = tmp_int;
  509. } else
  510. ret = SR_ERR_ARG;
  511. break;
  512. case SR_CONF_COUPLING:
  513. tmp_str = g_variant_get_string(data, NULL);
  514. /* TODO: Not supporting coupling per channel yet. */
  515. for (i = 0; coupling[i]; i++) {
  516. if (!strcmp(tmp_str, coupling[i])) {
  517. devc->coupling_ch1 = i;
  518. devc->coupling_ch2 = i;
  519. break;
  520. }
  521. }
  522. if (coupling[i] == 0)
  523. ret = SR_ERR_ARG;
  524. break;
  525. default:
  526. ret = SR_ERR_NA;
  527. break;
  528. }
  529. return ret;
  530. }
  531. static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
  532. const struct sr_channel_group *cg)
  533. {
  534. struct dev_context *devc;
  535. GVariant *tuple, *rational[2];
  536. GVariantBuilder gvb;
  537. unsigned int i;
  538. (void)cg;
  539. switch (key) {
  540. case SR_CONF_SCAN_OPTIONS:
  541. *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
  542. scanopts, ARRAY_SIZE(scanopts), sizeof(int32_t));
  543. break;
  544. case SR_CONF_DEVICE_OPTIONS:
  545. *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
  546. devopts, ARRAY_SIZE(devopts), sizeof(int32_t));
  547. break;
  548. case SR_CONF_BUFFERSIZE:
  549. if (!sdi)
  550. return SR_ERR_ARG;
  551. devc = sdi->priv;
  552. *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
  553. devc->profile->buffersizes, 2, sizeof(uint64_t));
  554. break;
  555. case SR_CONF_COUPLING:
  556. *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
  557. break;
  558. case SR_CONF_VDIV:
  559. g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
  560. for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
  561. rational[0] = g_variant_new_uint64(vdivs[i][0]);
  562. rational[1] = g_variant_new_uint64(vdivs[i][1]);
  563. tuple = g_variant_new_tuple(rational, 2);
  564. g_variant_builder_add_value(&gvb, tuple);
  565. }
  566. *data = g_variant_builder_end(&gvb);
  567. break;
  568. case SR_CONF_FILTER:
  569. *data = g_variant_new_strv(filter_targets,
  570. ARRAY_SIZE(filter_targets));
  571. break;
  572. case SR_CONF_TIMEBASE:
  573. g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
  574. for (i = 0; i < ARRAY_SIZE(timebases); i++) {
  575. rational[0] = g_variant_new_uint64(timebases[i][0]);
  576. rational[1] = g_variant_new_uint64(timebases[i][1]);
  577. tuple = g_variant_new_tuple(rational, 2);
  578. g_variant_builder_add_value(&gvb, tuple);
  579. }
  580. *data = g_variant_builder_end(&gvb);
  581. break;
  582. case SR_CONF_TRIGGER_SOURCE:
  583. *data = g_variant_new_strv(trigger_sources,
  584. ARRAY_SIZE(trigger_sources));
  585. break;
  586. default:
  587. return SR_ERR_NA;
  588. }
  589. return SR_OK;
  590. }
  591. static void send_chunk(struct sr_dev_inst *sdi, unsigned char *buf,
  592. int num_samples)
  593. {
  594. struct sr_datafeed_packet packet;
  595. struct sr_datafeed_analog analog;
  596. struct dev_context *devc;
  597. float ch1, ch2, range;
  598. int num_channels, data_offset, i;
  599. devc = sdi->priv;
  600. num_channels = (devc->ch1_enabled && devc->ch2_enabled) ? 2 : 1;
  601. packet.type = SR_DF_ANALOG;
  602. packet.payload = &analog;
  603. /* TODO: support for 5xxx series 9-bit samples */
  604. analog.channels = devc->enabled_channels;
  605. analog.num_samples = num_samples;
  606. analog.mq = SR_MQ_VOLTAGE;
  607. analog.unit = SR_UNIT_VOLT;
  608. /* TODO: Check malloc return value. */
  609. analog.data = g_try_malloc(analog.num_samples * sizeof(float) * num_channels);
  610. data_offset = 0;
  611. for (i = 0; i < analog.num_samples; i++) {
  612. /*
  613. * The device always sends data for both channels. If a channel
  614. * is disabled, it contains a copy of the enabled channel's
  615. * data. However, we only send the requested channels to
  616. * the bus.
  617. *
  618. * Voltage values are encoded as a value 0-255 (0-512 on the
  619. * DSO-5200*), where the value is a point in the range
  620. * represented by the vdiv setting. There are 8 vertical divs,
  621. * so e.g. 500mV/div represents 4V peak-to-peak where 0 = -2V
  622. * and 255 = +2V.
  623. */
  624. /* TODO: Support for DSO-5xxx series 9-bit samples. */
  625. if (devc->ch1_enabled) {
  626. range = ((float)vdivs[devc->voltage_ch1][0] / vdivs[devc->voltage_ch1][1]) * 8;
  627. ch1 = range / 255 * *(buf + i * 2 + 1);
  628. /* Value is centered around 0V. */
  629. ch1 -= range / 2;
  630. analog.data[data_offset++] = ch1;
  631. }
  632. if (devc->ch2_enabled) {
  633. range = ((float)vdivs[devc->voltage_ch2][0] / vdivs[devc->voltage_ch2][1]) * 8;
  634. ch2 = range / 255 * *(buf + i * 2);
  635. ch2 -= range / 2;
  636. analog.data[data_offset++] = ch2;
  637. }
  638. }
  639. sr_session_send(devc->cb_data, &packet);
  640. }
  641. /*
  642. * Called by libusb (as triggered by handle_event()) when a transfer comes in.
  643. * Only channel data comes in asynchronously, and all transfers for this are
  644. * queued up beforehand, so this just needs to chuck the incoming data onto
  645. * the libsigrok session bus.
  646. */
  647. static void receive_transfer(struct libusb_transfer *transfer)
  648. {
  649. struct sr_datafeed_packet packet;
  650. struct sr_dev_inst *sdi;
  651. struct dev_context *devc;
  652. int num_samples, pre;
  653. sdi = transfer->user_data;
  654. devc = sdi->priv;
  655. sr_spew("receive_transfer(): status %d received %d bytes.",
  656. transfer->status, transfer->actual_length);
  657. if (transfer->actual_length == 0)
  658. /* Nothing to send to the bus. */
  659. return;
  660. num_samples = transfer->actual_length / 2;
  661. sr_spew("Got %d-%d/%d samples in frame.", devc->samp_received + 1,
  662. devc->samp_received + num_samples, devc->framesize);
  663. /*
  664. * The device always sends a full frame, but the beginning of the frame
  665. * doesn't represent the trigger point. The offset at which the trigger
  666. * happened came in with the capture state, so we need to start sending
  667. * from there up the session bus. The samples in the frame buffer
  668. * before that trigger point came after the end of the device's frame
  669. * buffer was reached, and it wrapped around to overwrite up until the
  670. * trigger point.
  671. */
  672. if (devc->samp_received < devc->trigger_offset) {
  673. /* Trigger point not yet reached. */
  674. if (devc->samp_received + num_samples < devc->trigger_offset) {
  675. /* The entire chunk is before the trigger point. */
  676. memcpy(devc->framebuf + devc->samp_buffered * 2,
  677. transfer->buffer, num_samples * 2);
  678. devc->samp_buffered += num_samples;
  679. } else {
  680. /*
  681. * This chunk hits or overruns the trigger point.
  682. * Store the part before the trigger fired, and
  683. * send the rest up to the session bus.
  684. */
  685. pre = devc->trigger_offset - devc->samp_received;
  686. memcpy(devc->framebuf + devc->samp_buffered * 2,
  687. transfer->buffer, pre * 2);
  688. devc->samp_buffered += pre;
  689. /* The rest of this chunk starts with the trigger point. */
  690. sr_dbg("Reached trigger point, %d samples buffered.",
  691. devc->samp_buffered);
  692. /* Avoid the corner case where the chunk ended at
  693. * exactly the trigger point. */
  694. if (num_samples > pre)
  695. send_chunk(sdi, transfer->buffer + pre * 2,
  696. num_samples - pre);
  697. }
  698. } else {
  699. /* Already past the trigger point, just send it all out. */
  700. send_chunk(sdi, transfer->buffer,
  701. num_samples);
  702. }
  703. devc->samp_received += num_samples;
  704. /* Everything in this transfer was either copied to the buffer or
  705. * sent to the session bus. */
  706. g_free(transfer->buffer);
  707. libusb_free_transfer(transfer);
  708. if (devc->samp_received >= devc->framesize) {
  709. /* That was the last chunk in this frame. Send the buffered
  710. * pre-trigger samples out now, in one big chunk. */
  711. sr_dbg("End of frame, sending %d pre-trigger buffered samples.",
  712. devc->samp_buffered);
  713. send_chunk(sdi, devc->framebuf, devc->samp_buffered);
  714. /* Mark the end of this frame. */
  715. packet.type = SR_DF_FRAME_END;
  716. sr_session_send(devc->cb_data, &packet);
  717. if (devc->limit_frames && ++devc->num_frames == devc->limit_frames) {
  718. /* Terminate session */
  719. devc->dev_state = STOPPING;
  720. } else {
  721. devc->dev_state = NEW_CAPTURE;
  722. }
  723. }
  724. }
  725. static int handle_event(int fd, int revents, void *cb_data)
  726. {
  727. const struct sr_dev_inst *sdi;
  728. struct sr_datafeed_packet packet;
  729. struct timeval tv;
  730. struct dev_context *devc;
  731. struct drv_context *drvc = di->priv;
  732. int num_channels;
  733. uint32_t trigger_offset;
  734. uint8_t capturestate;
  735. (void)fd;
  736. (void)revents;
  737. sdi = cb_data;
  738. devc = sdi->priv;
  739. if (devc->dev_state == STOPPING) {
  740. /* We've been told to wind up the acquisition. */
  741. sr_dbg("Stopping acquisition.");
  742. /*
  743. * TODO: Doesn't really cancel pending transfers so they might
  744. * come in after SR_DF_END is sent.
  745. */
  746. usb_source_remove(drvc->sr_ctx);
  747. packet.type = SR_DF_END;
  748. sr_session_send(sdi, &packet);
  749. devc->dev_state = IDLE;
  750. return TRUE;
  751. }
  752. /* Always handle pending libusb events. */
  753. tv.tv_sec = tv.tv_usec = 0;
  754. libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
  755. /* TODO: ugh */
  756. if (devc->dev_state == NEW_CAPTURE) {
  757. if (dso_capture_start(sdi) != SR_OK)
  758. return TRUE;
  759. if (dso_enable_trigger(sdi) != SR_OK)
  760. return TRUE;
  761. // if (dso_force_trigger(sdi) != SR_OK)
  762. // return TRUE;
  763. sr_dbg("Successfully requested next chunk.");
  764. devc->dev_state = CAPTURE;
  765. return TRUE;
  766. }
  767. if (devc->dev_state != CAPTURE)
  768. return TRUE;
  769. if ((dso_get_capturestate(sdi, &capturestate, &trigger_offset)) != SR_OK)
  770. return TRUE;
  771. sr_dbg("Capturestate %d.", capturestate);
  772. sr_dbg("Trigger offset 0x%.6x.", trigger_offset);
  773. switch (capturestate) {
  774. case CAPTURE_EMPTY:
  775. if (++devc->capture_empty_count >= MAX_CAPTURE_EMPTY) {
  776. devc->capture_empty_count = 0;
  777. if (dso_capture_start(sdi) != SR_OK)
  778. break;
  779. if (dso_enable_trigger(sdi) != SR_OK)
  780. break;
  781. // if (dso_force_trigger(sdi) != SR_OK)
  782. // break;
  783. sr_dbg("Successfully requested next chunk.");
  784. }
  785. break;
  786. case CAPTURE_FILLING:
  787. /* No data yet. */
  788. break;
  789. case CAPTURE_READY_8BIT:
  790. /* Remember where in the captured frame the trigger is. */
  791. devc->trigger_offset = trigger_offset;
  792. num_channels = (devc->ch1_enabled && devc->ch2_enabled) ? 2 : 1;
  793. /* TODO: Check malloc return value. */
  794. devc->framebuf = g_try_malloc(devc->framesize * num_channels * 2);
  795. devc->samp_buffered = devc->samp_received = 0;
  796. /* Tell the scope to send us the first frame. */
  797. if (dso_get_channeldata(sdi, receive_transfer) != SR_OK)
  798. break;
  799. /*
  800. * Don't hit the state machine again until we're done fetching
  801. * the data we just told the scope to send.
  802. */
  803. devc->dev_state = FETCH_DATA;
  804. /* Tell the frontend a new frame is on the way. */
  805. packet.type = SR_DF_FRAME_BEGIN;
  806. sr_session_send(sdi, &packet);
  807. break;
  808. case CAPTURE_READY_9BIT:
  809. /* TODO */
  810. sr_err("Not yet supported.");
  811. break;
  812. case CAPTURE_TIMEOUT:
  813. /* Doesn't matter, we'll try again next time. */
  814. break;
  815. default:
  816. sr_dbg("Unknown capture state: %d.", capturestate);
  817. break;
  818. }
  819. return TRUE;
  820. }
  821. static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
  822. {
  823. struct dev_context *devc;
  824. struct drv_context *drvc = di->priv;
  825. if (sdi->status != SR_ST_ACTIVE)
  826. return SR_ERR_DEV_CLOSED;
  827. devc = sdi->priv;
  828. devc->cb_data = cb_data;
  829. if (configure_channels(sdi) != SR_OK) {
  830. sr_err("Failed to configure channels.");
  831. return SR_ERR;
  832. }
  833. if (dso_init(sdi) != SR_OK)
  834. return SR_ERR;
  835. if (dso_capture_start(sdi) != SR_OK)
  836. return SR_ERR;
  837. devc->dev_state = CAPTURE;
  838. usb_source_add(drvc->sr_ctx, TICK, handle_event, (void *)sdi);
  839. /* Send header packet to the session bus. */
  840. std_session_send_df_header(cb_data, LOG_PREFIX);
  841. return SR_OK;
  842. }
  843. static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
  844. {
  845. struct dev_context *devc;
  846. (void)cb_data;
  847. if (sdi->status != SR_ST_ACTIVE)
  848. return SR_ERR;
  849. devc = sdi->priv;
  850. devc->dev_state = STOPPING;
  851. return SR_OK;
  852. }
  853. SR_PRIV struct sr_dev_driver hantek_dso_driver_info = {
  854. .name = "hantek-dso",
  855. .longname = "Hantek DSO",
  856. .api_version = 1,
  857. .init = init,
  858. .cleanup = cleanup,
  859. .scan = scan,
  860. .dev_list = dev_list,
  861. .dev_clear = dev_clear,
  862. .config_get = config_get,
  863. .config_set = config_set,
  864. .config_list = config_list,
  865. .dev_open = dev_open,
  866. .dev_close = dev_close,
  867. .dev_acquisition_start = dev_acquisition_start,
  868. .dev_acquisition_stop = dev_acquisition_stop,
  869. .priv = NULL,
  870. };