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 static const uint32_t scanopts[] = {
29 static const uint32_t drvopts[] = {
33 static const uint32_t devopts[] = {
34 SR_CONF_CONN | SR_CONF_GET,
35 SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
36 SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
37 SR_CONF_TRIGGER_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
38 SR_CONF_TRIGGER_SLOPE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
39 SR_CONF_BUFFERSIZE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
42 static const uint32_t cgopts[] = {
43 SR_CONF_VDIV | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
44 SR_CONF_COUPLING | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
45 SR_CONF_PROBE_FACTOR | SR_CONF_GET | SR_CONF_SET,
48 static const uint64_t samplerates[] = {
49 SR_MHZ(100), SR_MHZ(50), SR_MHZ(25), SR_MHZ(20),
50 SR_MHZ(10), SR_MHZ(5), SR_KHZ(2500), SR_MHZ(2),
51 SR_MHZ(1), SR_KHZ(500), SR_KHZ(250), SR_KHZ(200),
52 SR_KHZ(100), SR_KHZ(50), SR_KHZ(25), SR_KHZ(20),
53 SR_KHZ(10), SR_KHZ(5), SR_HZ(2500), SR_KHZ(2),
54 SR_KHZ(1), SR_HZ(500), SR_HZ(250), SR_HZ(200),
55 SR_HZ(100), SR_HZ(50), SR_HZ(25), SR_HZ(20)
58 /* must be in sync with readout_steps[] in protocol.c */
59 static const uint64_t buffersizes[] = {
60 2 * 500, 3 * 500, 4 * 500, 5 * 500,
61 6 * 500, 7 * 500, 8 * 500, 9 * 500, 10 * 500,
62 12 * 500 - 2, 14 * 500 - 2, 16 * 500 - 2,
63 18 * 500 - 2, 20 * 500 - 2, 10240 - 2
66 static const uint64_t vdivs[][2] = {
78 /* Bits 4 and 5 enable relays that add /10 filters to the chain
79 * Bits 0 and 1 select an output from a resistor array */
80 static const uint8_t vdivs_map[] = {
81 0x01, 0x02, 0x03, 0x21, 0x22, 0x23, 0x31, 0x32, 0x33
85 static const char *trigger_sources[] = {
89 static const uint8_t trigger_sources_map[] = {
93 static const char *trigger_slopes[] = {
97 static const char *coupling[] = {
101 static const uint8_t coupling_map[] = {
105 static GSList *scan_port(GSList *devices, struct sr_dev_driver *di,
106 struct parport *port)
108 struct sr_dev_inst *sdi;
109 struct sr_channel *ch;
110 struct sr_channel_group *cg;
111 struct dev_context *devc;
112 struct drv_context *drvc;
115 if (ieee1284_open(port, 0, &i) != E1284_OK) {
116 sr_err("Can't open parallel port %s", port->name);
120 if ((i & (CAP1284_RAW | CAP1284_BYTE)) != (CAP1284_RAW | CAP1284_BYTE)) {
121 sr_err("Parallel port %s does not provide low-level bidirection access",
126 if (ieee1284_claim(port) != E1284_OK) {
127 sr_err("Parallel port %s already in use", port->name);
131 if (!hung_chang_dso_2100_check_id(port))
134 sdi = g_malloc0(sizeof(struct sr_dev_inst));
135 sdi->status = SR_ST_INACTIVE;
136 sdi->vendor = g_strdup("Hung-Chang");
137 sdi->model = g_strdup("DSO-2100");
140 sdi->inst_type = 0; /* FIXME */
144 for (i = 0; i < NUM_CHANNELS; i++) {
145 cg = g_malloc0(sizeof(struct sr_channel_group));
146 cg->name = g_strdup(trigger_sources[i]);
147 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, FALSE, trigger_sources[i]);
148 cg->channels = g_slist_append(cg->channels, ch);
149 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
152 devc = g_malloc0(sizeof(struct dev_context));
153 devc->enabled_channel = g_slist_append(NULL, NULL);
158 devc->cctl[0] = 0x31; /* 1V/div, DC coupling, trigger on channel A*/
159 devc->cctl[1] = 0x31; /* 1V/div, DC coupling, no tv sync trigger */
164 devc->offset[0] = 0x80;
165 devc->offset[1] = 0x80;
166 devc->gain[0] = 0x80;
167 devc->gain[1] = 0x80;
168 devc->frame_limit = 0;
169 devc->last_step = 0; /* buffersize = 1000 */
172 drvc->instances = g_slist_append(drvc->instances, sdi);
173 devices = g_slist_append(devices, sdi);
176 ieee1284_release(port);
178 ieee1284_close(port);
183 static GSList *scan(struct sr_dev_driver *di, GSList *options)
185 struct parport_list ports;
186 struct sr_config *src;
187 const char *conn = NULL;
188 GSList *devices, *option;
193 for (option = options; option; option = option->next) {
195 if (src->key == SR_CONF_CONN) {
196 conn = g_variant_get_string(src->data, NULL);
204 if (ieee1284_find_ports(&ports, 0) != E1284_OK)
209 for (i = 0; i < ports.portc; i++)
210 if (!strcmp(ports.portv[i]->name, conn)) {
212 devices = scan_port(devices, di, ports.portv[i]);
216 sr_err("Parallel port %s not found. Valid names are:", conn);
217 for (i = 0; i < ports.portc; i++)
218 sr_err("\t%s", ports.portv[i]->name);
221 ieee1284_free_ports(&ports);
226 static void clear_private(void *priv)
228 struct dev_context *devc = priv;
230 g_slist_free(devc->enabled_channel);
233 static int dev_clear(const struct sr_dev_driver *di)
235 struct drv_context *drvc = di->context;
236 struct sr_dev_inst *sdi;
240 for (l = drvc->instances; l; l = l->next) {
242 ieee1284_unref(sdi->conn);
246 return std_dev_clear(di, clear_private);
249 static int dev_open(struct sr_dev_inst *sdi)
251 struct dev_context *devc = sdi->priv;
254 if (sdi->status != SR_ST_INACTIVE)
257 if (ieee1284_open(sdi->conn, 0, &i) != E1284_OK)
260 if (ieee1284_claim(sdi->conn) != E1284_OK)
263 if (ieee1284_data_dir(sdi->conn, 1) != E1284_OK)
266 if (hung_chang_dso_2100_move_to(sdi, 1))
269 devc->samples = g_try_malloc(1000 * sizeof(*devc->samples));
273 sdi->status = SR_ST_ACTIVE;
278 hung_chang_dso_2100_reset_port(sdi->conn);
279 ieee1284_release(sdi->conn);
281 ieee1284_close(sdi->conn);
286 static int dev_close(struct sr_dev_inst *sdi)
288 struct dev_context *devc = sdi->priv;
290 if (sdi->status != SR_ST_ACTIVE)
293 g_free(devc->samples);
294 hung_chang_dso_2100_reset_port(sdi->conn);
295 ieee1284_release(sdi->conn);
296 ieee1284_close(sdi->conn);
297 sdi->status = SR_ST_INACTIVE;
302 static int find_in_array(GVariant *data, const GVariantType *type,
303 const void *arr, int n)
305 const char * const *sarr;
307 const uint64_t *u64arr;
308 const uint8_t *u8arr;
313 if (!g_variant_is_of_type(data, type))
316 switch (g_variant_classify(data)) {
317 case G_VARIANT_CLASS_STRING:
318 s = g_variant_get_string(data, NULL);
321 for (i = 0; i < n; i++)
322 if (!strcmp(s, sarr[i]))
325 case G_VARIANT_CLASS_UINT64:
326 u64 = g_variant_get_uint64(data);
329 for (i = 0; i < n; i++)
330 if (u64 == u64arr[i])
333 case G_VARIANT_CLASS_BYTE:
334 u8 = g_variant_get_byte(data);
337 for (i = 0; i < n; i++)
347 static int reverse_map(uint8_t u, const uint8_t *arr, int n)
349 GVariant *v = g_variant_new_byte(u);
350 int i = find_in_array(v, G_VARIANT_TYPE_BYTE, arr, n);
355 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
356 const struct sr_channel_group *cg)
358 struct dev_context *devc = sdi->priv;
359 struct parport *port;
362 if (cg) /* sr_config_get will validate cg using config_list */
363 ch = ((struct sr_channel *)cg->channels->data)->index;
369 *data = g_variant_new_string(port->name);
371 case SR_CONF_LIMIT_FRAMES:
372 *data = g_variant_new_uint64(devc->frame_limit);
374 case SR_CONF_SAMPLERATE:
375 *data = g_variant_new_uint64(samplerates[devc->rate]);
377 case SR_CONF_TRIGGER_SOURCE:
378 i = reverse_map(devc->cctl[0] & 0xC0, trigger_sources_map,
379 ARRAY_SIZE(trigger_sources_map));
383 *data = g_variant_new_string(trigger_sources[i]);
385 case SR_CONF_TRIGGER_SLOPE:
386 if (devc->edge >= ARRAY_SIZE(trigger_slopes))
389 *data = g_variant_new_string(trigger_slopes[devc->edge]);
391 case SR_CONF_BUFFERSIZE:
392 *data = g_variant_new_uint64(buffersizes[devc->last_step]);
396 ret = SR_ERR_CHANNEL_GROUP;
398 i = reverse_map(devc->cctl[ch] & 0x33, vdivs_map,
399 ARRAY_SIZE(vdivs_map));
403 *data = g_variant_new("(tt)", vdivs[i][0],
407 case SR_CONF_COUPLING:
409 ret = SR_ERR_CHANNEL_GROUP;
411 i = reverse_map(devc->cctl[ch] & 0x0C, coupling_map,
412 ARRAY_SIZE(coupling_map));
416 *data = g_variant_new_string(coupling[i]);
419 case SR_CONF_PROBE_FACTOR:
421 ret = SR_ERR_CHANNEL_GROUP;
423 *data = g_variant_new_uint64(devc->probe[ch]);
432 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
433 const struct sr_channel_group *cg)
435 struct dev_context *devc = sdi->priv;
439 if (cg) /* sr_config_set will validate cg using config_list */
440 ch = ((struct sr_channel *)cg->channels->data)->index;
442 if (sdi->status != SR_ST_ACTIVE)
443 return SR_ERR_DEV_CLOSED;
447 case SR_CONF_LIMIT_FRAMES:
448 devc->frame_limit = g_variant_get_uint64(data);
450 case SR_CONF_SAMPLERATE:
451 i = find_in_array(data, G_VARIANT_TYPE_UINT64,
452 samplerates, ARRAY_SIZE(samplerates));
458 case SR_CONF_TRIGGER_SOURCE:
459 i = find_in_array(data, G_VARIANT_TYPE_STRING,
460 trigger_sources, ARRAY_SIZE(trigger_sources));
464 devc->cctl[0] = (devc->cctl[0] & 0x3F)
465 | trigger_sources_map[i];
467 case SR_CONF_TRIGGER_SLOPE:
468 i = find_in_array(data, G_VARIANT_TYPE_STRING,
469 trigger_slopes, ARRAY_SIZE(trigger_slopes));
475 case SR_CONF_BUFFERSIZE:
476 i = find_in_array(data, G_VARIANT_TYPE_UINT64,
477 buffersizes, ARRAY_SIZE(buffersizes));
485 ret = SR_ERR_CHANNEL_GROUP;
486 } else if (!g_variant_is_of_type(data, G_VARIANT_TYPE("(tt)"))) {
489 g_variant_get(data, "(tt)", &u, &v);
490 for (i = 0; i < (int)ARRAY_SIZE(vdivs); i++)
491 if (vdivs[i][0] == u && vdivs[i][1] == v)
493 if (i == ARRAY_SIZE(vdivs))
496 devc->cctl[ch] = (devc->cctl[ch] & 0xCC)
500 case SR_CONF_COUPLING:
502 ret = SR_ERR_CHANNEL_GROUP;
504 i = find_in_array(data, G_VARIANT_TYPE_STRING,
505 coupling, ARRAY_SIZE(coupling));
509 devc->cctl[ch] = (devc->cctl[ch] & 0xF3)
513 case SR_CONF_PROBE_FACTOR:
515 ret = SR_ERR_CHANNEL_GROUP;
517 u = g_variant_get_uint64(data);
531 static int config_channel_set(const struct sr_dev_inst *sdi,
532 struct sr_channel *ch,
533 unsigned int changes)
535 struct dev_context *devc = sdi->priv;
538 if (changes & SR_CHANNEL_SET_ENABLED) {
540 v = devc->channel | (1 << ch->index);
544 devc->enabled_channel->data = ch;
546 devc->channel &= ~(1 << ch->index);
552 static int config_commit(const struct sr_dev_inst *sdi)
554 uint8_t state = hung_chang_dso_2100_read_mbox(sdi->conn, 0.02);
557 if (sdi->status != SR_ST_ACTIVE)
558 return SR_ERR_DEV_CLOSED;
564 /* we will travel the complete config path on our way to state 1 */
569 ret = hung_chang_dso_2100_move_to(sdi, 1);
573 hung_chang_dso_2100_write_mbox(sdi->conn, 4);
575 ret = hung_chang_dso_2100_move_to(sdi, 1);
578 return hung_chang_dso_2100_move_to(sdi, state);
581 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
582 const struct sr_channel_group *cg)
585 GVariant *gvar, *rational[2];
590 case SR_CONF_SCAN_OPTIONS:
591 case SR_CONF_DEVICE_OPTIONS:
593 case SR_CONF_SAMPLERATE:
594 case SR_CONF_TRIGGER_SOURCE:
595 case SR_CONF_TRIGGER_SLOPE:
596 case SR_CONF_BUFFERSIZE:
601 case SR_CONF_COUPLING:
605 return SR_ERR_CHANNEL_GROUP;
606 l = g_slist_find(sdi->channel_groups, cg);
615 case SR_CONF_SCAN_OPTIONS:
616 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
617 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
619 case SR_CONF_DEVICE_OPTIONS:
621 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
622 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
624 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
625 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
627 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
628 cgopts, ARRAY_SIZE(cgopts), sizeof(uint32_t));
630 case SR_CONF_SAMPLERATE:
631 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
632 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
633 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
634 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
635 *data = g_variant_builder_end(&gvb);
637 case SR_CONF_TRIGGER_SOURCE:
638 *data = g_variant_new_strv(trigger_sources, ARRAY_SIZE(trigger_sources));
640 case SR_CONF_TRIGGER_SLOPE:
641 *data = g_variant_new_strv(trigger_slopes, ARRAY_SIZE(trigger_slopes));
643 case SR_CONF_BUFFERSIZE:
644 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
645 buffersizes, ARRAY_SIZE(buffersizes), sizeof(uint64_t));
648 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
649 for (i = 0; i < (int)ARRAY_SIZE(vdivs); i++) {
650 rational[0] = g_variant_new_uint64(vdivs[i][0]);
651 rational[1] = g_variant_new_uint64(vdivs[i][1]);
652 gvar = g_variant_new_tuple(rational, 2);
653 g_variant_builder_add_value(&gvb, gvar);
655 *data = g_variant_builder_end(&gvb);
657 case SR_CONF_COUPLING:
658 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
665 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
667 struct dev_context *devc = sdi->priv;
670 if (sdi->status != SR_ST_ACTIVE)
671 return SR_ERR_DEV_CLOSED;
674 static const float res_array[] = {0.5, 1, 2, 5};
675 static const uint8_t relays[] = {100, 10, 10, 1};
676 devc->factor = devc->probe[devc->channel - 1] / 32.0;
677 devc->factor *= res_array[devc->cctl[devc->channel - 1] & 0x03];
678 devc->factor /= relays[(devc->cctl[devc->channel - 1] >> 4) & 0x03];
681 devc->state_known = TRUE;
684 devc->retries = MAX_RETRIES;
686 ret = hung_chang_dso_2100_move_to(sdi, 0x21);
690 std_session_send_df_header(sdi, LOG_PREFIX);
692 sr_session_source_add(sdi->session, -1, 0, 8,
693 hung_chang_dso_2100_poll, (void *)sdi);
698 SR_PRIV int hung_chang_dso_2100_dev_acquisition_stop(const struct sr_dev_inst *sdi)
700 if (sdi->status != SR_ST_ACTIVE)
701 return SR_ERR_DEV_CLOSED;
703 std_session_send_df_end(sdi, LOG_PREFIX);
704 sr_session_source_remove(sdi->session, -1);
705 hung_chang_dso_2100_move_to(sdi, 1);
710 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
712 return hung_chang_dso_2100_dev_acquisition_stop(sdi);
715 static struct sr_dev_driver hung_chang_dso_2100_driver_info = {
716 .name = "hung-chang-dso-2100",
717 .longname = "Hung-Chang DSO-2100",
720 .cleanup = std_cleanup,
722 .dev_list = std_dev_list,
723 .dev_clear = dev_clear,
724 .config_get = config_get,
725 .config_set = config_set,
726 .config_channel_set = config_channel_set,
727 .config_commit = config_commit,
728 .config_list = config_list,
729 .dev_open = dev_open,
730 .dev_close = dev_close,
731 .dev_acquisition_start = dev_acquisition_start,
732 .dev_acquisition_stop = dev_acquisition_stop,
735 SR_REGISTER_DEV_DRIVER(hung_chang_dso_2100_driver_info);