2 * This file is part of the libsigrok project.
4 * Copyright (C) 2015 Daniel Glöckner <daniel-gl@gmx.net>
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.
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.
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/>.
25 SR_PRIV struct sr_dev_driver hung_chang_dso_2100_driver_info;
27 static const uint32_t scanopts[] = {
31 static const uint32_t drvopts[] = {
35 static const uint32_t devopts[] = {
36 SR_CONF_CONN | SR_CONF_GET,
37 SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
38 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
39 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
40 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
41 SR_CONF_BUFFERSIZE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
44 static const uint32_t cgopts[] = {
45 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
46 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
47 SR_CONF_PROBE_FACTOR | SR_CONF_GET | SR_CONF_SET,
50 static const uint64_t samplerates[] = {
51 SR_MHZ(100), SR_MHZ(50), SR_MHZ(25), SR_MHZ(20),
52 SR_MHZ(10), SR_MHZ(5), SR_KHZ(2500), SR_MHZ(2),
53 SR_MHZ(1), SR_KHZ(500), SR_KHZ(250), SR_KHZ(200),
54 SR_KHZ(100), SR_KHZ(50), SR_KHZ(25), SR_KHZ(20),
55 SR_KHZ(10), SR_KHZ(5), SR_HZ(2500), SR_KHZ(2),
56 SR_KHZ(1), SR_HZ(500), SR_HZ(250), SR_HZ(200),
57 SR_HZ(100), SR_HZ(50), SR_HZ(25), SR_HZ(20)
60 /* must be in sync with readout_steps[] in protocol.c */
61 static const uint64_t buffersizes[] = {
62 2 * 500, 3 * 500, 4 * 500, 5 * 500,
63 6 * 500, 7 * 500, 8 * 500, 9 * 500, 10 * 500,
64 12 * 500 - 2, 14 * 500 - 2, 16 * 500 - 2,
65 18 * 500 - 2, 20 * 500 - 2, 10240 - 2
68 static const uint64_t vdivs[][2] = {
80 /* Bits 4 and 5 enable relays that add /10 filters to the chain
81 * Bits 0 and 1 select an output from a resistor array */
82 static const uint8_t vdivs_map[] = {
83 0x01, 0x02, 0x03, 0x21, 0x22, 0x23, 0x31, 0x32, 0x33
87 static const char *trigger_sources[] = {
91 static const uint8_t trigger_sources_map[] = {
95 static const char *trigger_slopes[] = {
99 static const char *coupling[] = {
103 static const uint8_t coupling_map[] = {
107 static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
109 return std_init(sr_ctx, di, LOG_PREFIX);
112 static GSList *scan_port(GSList *devices, struct sr_dev_driver *di,
113 struct parport *port)
115 struct sr_dev_inst *sdi;
116 struct sr_channel *ch;
117 struct sr_channel_group *cg;
118 struct dev_context *devc;
119 struct drv_context *drvc;
122 if (ieee1284_open(port, 0, &i) != E1284_OK) {
123 sr_err("Can't open parallel port %s", port->name);
127 if ((i & (CAP1284_RAW | CAP1284_BYTE)) != (CAP1284_RAW | CAP1284_BYTE)) {
128 sr_err("Parallel port %s does not provide low-level bidirection access",
133 if (ieee1284_claim(port) != E1284_OK) {
134 sr_err("Parallel port %s already in use", port->name);
138 if (!hung_chang_dso_2100_check_id(port))
141 sdi = g_malloc0(sizeof(struct sr_dev_inst));
142 sdi->status = SR_ST_INACTIVE;
143 sdi->vendor = g_strdup("Hung-Chang");
144 sdi->model = g_strdup("DSO-2100");
147 sdi->inst_type = 0; /* FIXME */
151 for (i = 0; i < NUM_CHANNELS; i++) {
152 cg = g_malloc0(sizeof(struct sr_channel_group));
153 cg->name = g_strdup(trigger_sources[i]);
154 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, FALSE, trigger_sources[i]);
155 cg->channels = g_slist_append(cg->channels, ch);
156 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
159 devc = g_malloc0(sizeof(struct dev_context));
160 devc->enabled_channel = g_slist_append(NULL, NULL);
165 devc->cctl[0] = 0x31; /* 1V/div, DC coupling, trigger on channel A*/
166 devc->cctl[1] = 0x31; /* 1V/div, DC coupling, no tv sync trigger */
171 devc->offset[0] = 0x80;
172 devc->offset[1] = 0x80;
173 devc->gain[0] = 0x80;
174 devc->gain[1] = 0x80;
175 devc->frame_limit = 0;
176 devc->last_step = 0; /* buffersize = 1000 */
179 drvc->instances = g_slist_append(drvc->instances, sdi);
180 devices = g_slist_append(devices, sdi);
183 ieee1284_release(port);
185 ieee1284_close(port);
190 static GSList *scan(struct sr_dev_driver *di, GSList *options)
192 struct parport_list ports;
193 struct sr_config *src;
194 const char *conn = NULL;
195 GSList *devices, *option;
200 for (option = options; option; option = option->next) {
202 if (src->key == SR_CONF_CONN) {
203 conn = g_variant_get_string(src->data, NULL);
211 if (ieee1284_find_ports(&ports, 0) != E1284_OK)
216 for (i = 0; i < ports.portc; i++)
217 if (!strcmp(ports.portv[i]->name, conn)) {
219 devices = scan_port(devices, di, ports.portv[i]);
223 sr_err("Parallel port %s not found. Valid names are:", conn);
224 for (i = 0; i < ports.portc; i++)
225 sr_err("\t%s", ports.portv[i]->name);
228 ieee1284_free_ports(&ports);
233 static GSList *dev_list(const struct sr_dev_driver *di)
235 return ((struct drv_context *)(di->context))->instances;
238 static void clear_private(void *priv)
240 struct dev_context *devc = priv;
242 g_slist_free(devc->enabled_channel);
245 static int dev_clear(const struct sr_dev_driver *di)
247 struct drv_context *drvc = di->context;
248 struct sr_dev_inst *sdi;
252 for (l = drvc->instances; l; l = l->next) {
254 ieee1284_unref(sdi->conn);
258 return std_dev_clear(di, clear_private);
261 static int dev_open(struct sr_dev_inst *sdi)
263 struct dev_context *devc = sdi->priv;
266 if (sdi->status != SR_ST_INACTIVE)
269 if (ieee1284_open(sdi->conn, 0, &i) != E1284_OK)
272 if (ieee1284_claim(sdi->conn) != E1284_OK)
275 if (ieee1284_data_dir(sdi->conn, 1) != E1284_OK)
278 if (hung_chang_dso_2100_move_to(sdi, 1))
281 devc->samples = g_try_malloc(1000 * sizeof(*devc->samples));
285 sdi->status = SR_ST_ACTIVE;
290 hung_chang_dso_2100_reset_port(sdi->conn);
291 ieee1284_release(sdi->conn);
293 ieee1284_close(sdi->conn);
298 static int dev_close(struct sr_dev_inst *sdi)
300 struct dev_context *devc = sdi->priv;
302 if (sdi->status != SR_ST_ACTIVE)
305 g_free(devc->samples);
306 hung_chang_dso_2100_reset_port(sdi->conn);
307 ieee1284_release(sdi->conn);
308 ieee1284_close(sdi->conn);
309 sdi->status = SR_ST_INACTIVE;
314 static int find_in_array(GVariant *data, const GVariantType *type,
315 const void *arr, int n)
317 const char * const *sarr;
319 const uint64_t *u64arr;
320 const uint8_t *u8arr;
325 if (!g_variant_is_of_type(data, type))
328 switch (g_variant_classify(data)) {
329 case G_VARIANT_CLASS_STRING:
330 s = g_variant_get_string(data, NULL);
333 for (i = 0; i < n; i++)
334 if (!strcmp(s, sarr[i]))
337 case G_VARIANT_CLASS_UINT64:
338 u64 = g_variant_get_uint64(data);
341 for (i = 0; i < n; i++)
342 if (u64 == u64arr[i])
345 case G_VARIANT_CLASS_BYTE:
346 u8 = g_variant_get_byte(data);
349 for (i = 0; i < n; i++)
359 static int reverse_map(uint8_t u, const uint8_t *arr, int n)
361 GVariant *v = g_variant_new_byte(u);
362 int i = find_in_array(v, G_VARIANT_TYPE_BYTE, arr, n);
367 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
368 const struct sr_channel_group *cg)
370 struct dev_context *devc = sdi->priv;
371 struct parport *port;
374 if (cg) /* sr_config_get will validate cg using config_list */
375 ch = ((struct sr_channel *)cg->channels->data)->index;
381 *data = g_variant_new_string(port->name);
383 case SR_CONF_LIMIT_FRAMES:
384 *data = g_variant_new_uint64(devc->frame_limit);
386 case SR_CONF_SAMPLERATE:
387 *data = g_variant_new_uint64(samplerates[devc->rate]);
389 case SR_CONF_TRIGGER_SOURCE:
390 i = reverse_map(devc->cctl[0] & 0xC0, trigger_sources_map,
391 ARRAY_SIZE(trigger_sources_map));
395 *data = g_variant_new_string(trigger_sources[i]);
397 case SR_CONF_TRIGGER_SLOPE:
398 if (devc->edge >= ARRAY_SIZE(trigger_slopes))
401 *data = g_variant_new_string(trigger_slopes[devc->edge]);
403 case SR_CONF_BUFFERSIZE:
404 *data = g_variant_new_uint64(buffersizes[devc->last_step]);
408 ret = SR_ERR_CHANNEL_GROUP;
410 i = reverse_map(devc->cctl[ch] & 0x33, vdivs_map,
411 ARRAY_SIZE(vdivs_map));
415 *data = g_variant_new("(tt)", vdivs[i][0],
419 case SR_CONF_COUPLING:
421 ret = SR_ERR_CHANNEL_GROUP;
423 i = reverse_map(devc->cctl[ch] & 0x0C, coupling_map,
424 ARRAY_SIZE(coupling_map));
428 *data = g_variant_new_string(coupling[i]);
431 case SR_CONF_PROBE_FACTOR:
433 ret = SR_ERR_CHANNEL_GROUP;
435 *data = g_variant_new_uint64(devc->probe[ch]);
444 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
445 const struct sr_channel_group *cg)
447 struct dev_context *devc = sdi->priv;
451 if (cg) /* sr_config_set will validate cg using config_list */
452 ch = ((struct sr_channel *)cg->channels->data)->index;
454 if (sdi->status != SR_ST_ACTIVE)
455 return SR_ERR_DEV_CLOSED;
459 case SR_CONF_LIMIT_FRAMES:
460 devc->frame_limit = g_variant_get_uint64(data);
462 case SR_CONF_SAMPLERATE:
463 i = find_in_array(data, G_VARIANT_TYPE_UINT64,
464 samplerates, ARRAY_SIZE(samplerates));
470 case SR_CONF_TRIGGER_SOURCE:
471 i = find_in_array(data, G_VARIANT_TYPE_STRING,
472 trigger_sources, ARRAY_SIZE(trigger_sources));
476 devc->cctl[0] = (devc->cctl[0] & 0x3F)
477 | trigger_sources_map[i];
479 case SR_CONF_TRIGGER_SLOPE:
480 i = find_in_array(data, G_VARIANT_TYPE_STRING,
481 trigger_slopes, ARRAY_SIZE(trigger_slopes));
487 case SR_CONF_BUFFERSIZE:
488 i = find_in_array(data, G_VARIANT_TYPE_UINT64,
489 buffersizes, ARRAY_SIZE(buffersizes));
497 ret = SR_ERR_CHANNEL_GROUP;
498 } else if (!g_variant_is_of_type(data, G_VARIANT_TYPE("(tt)"))) {
501 g_variant_get(data, "(tt)", &u, &v);
502 for (i = 0; i < (int)ARRAY_SIZE(vdivs); i++)
503 if (vdivs[i][0] == u && vdivs[i][1] == v)
505 if (i == ARRAY_SIZE(vdivs))
508 devc->cctl[ch] = (devc->cctl[ch] & 0xCC)
512 case SR_CONF_COUPLING:
514 ret = SR_ERR_CHANNEL_GROUP;
516 i = find_in_array(data, G_VARIANT_TYPE_STRING,
517 coupling, ARRAY_SIZE(coupling));
521 devc->cctl[ch] = (devc->cctl[ch] & 0xF3)
525 case SR_CONF_PROBE_FACTOR:
527 ret = SR_ERR_CHANNEL_GROUP;
529 u = g_variant_get_uint64(data);
543 static int config_channel_set(const struct sr_dev_inst *sdi,
544 struct sr_channel *ch,
545 unsigned int changes)
547 struct dev_context *devc = sdi->priv;
550 if (changes & SR_CHANNEL_SET_ENABLED) {
552 v = devc->channel | (1 << ch->index);
556 devc->enabled_channel->data = ch;
558 devc->channel &= ~(1 << ch->index);
564 static int config_commit(const struct sr_dev_inst *sdi)
566 uint8_t state = hung_chang_dso_2100_read_mbox(sdi->conn, 0.02);
569 if (sdi->status != SR_ST_ACTIVE)
570 return SR_ERR_DEV_CLOSED;
576 /* we will travel the complete config path on our way to state 1 */
581 ret = hung_chang_dso_2100_move_to(sdi, 1);
585 hung_chang_dso_2100_write_mbox(sdi->conn, 4);
587 ret = hung_chang_dso_2100_move_to(sdi, 1);
590 return hung_chang_dso_2100_move_to(sdi, state);
593 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
594 const struct sr_channel_group *cg)
597 GVariant *gvar, *rational[2];
602 case SR_CONF_SCAN_OPTIONS:
603 case SR_CONF_DEVICE_OPTIONS:
605 case SR_CONF_SAMPLERATE:
606 case SR_CONF_TRIGGER_SOURCE:
607 case SR_CONF_TRIGGER_SLOPE:
608 case SR_CONF_BUFFERSIZE:
613 case SR_CONF_COUPLING:
617 return SR_ERR_CHANNEL_GROUP;
618 l = g_slist_find(sdi->channel_groups, cg);
627 case SR_CONF_SCAN_OPTIONS:
628 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
629 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
631 case SR_CONF_DEVICE_OPTIONS:
633 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
634 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
636 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
637 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
639 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
640 cgopts, ARRAY_SIZE(cgopts), sizeof(uint32_t));
642 case SR_CONF_SAMPLERATE:
643 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
644 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
645 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
646 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
647 *data = g_variant_builder_end(&gvb);
649 case SR_CONF_TRIGGER_SOURCE:
650 *data = g_variant_new_strv(trigger_sources, ARRAY_SIZE(trigger_sources));
652 case SR_CONF_TRIGGER_SLOPE:
653 *data = g_variant_new_strv(trigger_slopes, ARRAY_SIZE(trigger_slopes));
655 case SR_CONF_BUFFERSIZE:
656 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
657 buffersizes, ARRAY_SIZE(buffersizes), sizeof(uint64_t));
660 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
661 for (i = 0; i < (int)ARRAY_SIZE(vdivs); i++) {
662 rational[0] = g_variant_new_uint64(vdivs[i][0]);
663 rational[1] = g_variant_new_uint64(vdivs[i][1]);
664 gvar = g_variant_new_tuple(rational, 2);
665 g_variant_builder_add_value(&gvb, gvar);
667 *data = g_variant_builder_end(&gvb);
669 case SR_CONF_COUPLING:
670 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
677 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
679 struct dev_context *devc = sdi->priv;
682 if (sdi->status != SR_ST_ACTIVE)
683 return SR_ERR_DEV_CLOSED;
686 static const float res_array[] = {0.5, 1, 2, 5};
687 static const uint8_t relays[] = {100, 10, 10, 1};
688 devc->factor = devc->probe[devc->channel - 1] / 32.0;
689 devc->factor *= res_array[devc->cctl[devc->channel - 1] & 0x03];
690 devc->factor /= relays[(devc->cctl[devc->channel - 1] >> 4) & 0x03];
693 devc->state_known = TRUE;
696 devc->retries = MAX_RETRIES;
698 ret = hung_chang_dso_2100_move_to(sdi, 0x21);
702 std_session_send_df_header(sdi, LOG_PREFIX);
704 sr_session_source_add(sdi->session, -1, 0, 8,
705 hung_chang_dso_2100_poll, (void *)sdi);
710 SR_PRIV int hung_chang_dso_2100_dev_acquisition_stop(const struct sr_dev_inst *sdi)
712 if (sdi->status != SR_ST_ACTIVE)
713 return SR_ERR_DEV_CLOSED;
715 std_session_send_df_end(sdi, LOG_PREFIX);
716 sr_session_source_remove(sdi->session, -1);
717 hung_chang_dso_2100_move_to(sdi, 1);
722 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
724 return hung_chang_dso_2100_dev_acquisition_stop(sdi);
727 SR_PRIV struct sr_dev_driver hung_chang_dso_2100_driver_info = {
728 .name = "hung-chang-dso-2100",
729 .longname = "Hung-Chang DSO-2100",
732 .cleanup = std_cleanup,
734 .dev_list = dev_list,
735 .dev_clear = dev_clear,
736 .config_get = config_get,
737 .config_set = config_set,
738 .config_channel_set = config_channel_set,
739 .config_commit = config_commit,
740 .config_list = config_list,
741 .dev_open = dev_open,
742 .dev_close = dev_close,
743 .dev_acquisition_start = dev_acquisition_start,
744 .dev_acquisition_stop = dev_acquisition_stop,