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 parport *port)
107 struct sr_dev_inst *sdi;
108 struct sr_channel *ch;
109 struct sr_channel_group *cg;
110 struct dev_context *devc;
113 if (ieee1284_open(port, 0, &i) != E1284_OK) {
114 sr_err("Can't open parallel port %s", port->name);
118 if ((i & (CAP1284_RAW | CAP1284_BYTE)) != (CAP1284_RAW | CAP1284_BYTE)) {
119 sr_err("Parallel port %s does not provide low-level bidirection access",
124 if (ieee1284_claim(port) != E1284_OK) {
125 sr_err("Parallel port %s already in use", port->name);
129 if (!hung_chang_dso_2100_check_id(port))
132 sdi = g_malloc0(sizeof(struct sr_dev_inst));
133 sdi->status = SR_ST_INACTIVE;
134 sdi->vendor = g_strdup("Hung-Chang");
135 sdi->model = g_strdup("DSO-2100");
136 sdi->inst_type = 0; /* FIXME */
140 for (i = 0; i < NUM_CHANNELS; i++) {
141 cg = g_malloc0(sizeof(struct sr_channel_group));
142 cg->name = g_strdup(trigger_sources[i]);
143 ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, FALSE, trigger_sources[i]);
144 cg->channels = g_slist_append(cg->channels, ch);
145 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
148 devc = g_malloc0(sizeof(struct dev_context));
149 devc->enabled_channel = g_slist_append(NULL, NULL);
154 devc->cctl[0] = 0x31; /* 1V/div, DC coupling, trigger on channel A*/
155 devc->cctl[1] = 0x31; /* 1V/div, DC coupling, no tv sync trigger */
160 devc->offset[0] = 0x80;
161 devc->offset[1] = 0x80;
162 devc->gain[0] = 0x80;
163 devc->gain[1] = 0x80;
164 devc->frame_limit = 0;
165 devc->last_step = 0; /* buffersize = 1000 */
168 devices = g_slist_append(devices, sdi);
171 ieee1284_release(port);
173 ieee1284_close(port);
178 static GSList *scan(struct sr_dev_driver *di, GSList *options)
180 struct parport_list ports;
181 struct sr_config *src;
182 const char *conn = NULL;
183 GSList *devices, *option;
188 for (option = options; option; option = option->next) {
190 if (src->key == SR_CONF_CONN) {
191 conn = g_variant_get_string(src->data, NULL);
199 if (ieee1284_find_ports(&ports, 0) != E1284_OK)
204 for (i = 0; i < ports.portc; i++)
205 if (!strcmp(ports.portv[i]->name, conn)) {
207 devices = scan_port(devices, ports.portv[i]);
211 sr_err("Parallel port %s not found. Valid names are:", conn);
212 for (i = 0; i < ports.portc; i++)
213 sr_err("\t%s", ports.portv[i]->name);
216 ieee1284_free_ports(&ports);
218 return std_scan_complete(di, devices);
221 static void clear_private(void *priv)
223 struct dev_context *devc = priv;
225 g_slist_free(devc->enabled_channel);
228 static int dev_clear(const struct sr_dev_driver *di)
230 struct drv_context *drvc = di->context;
231 struct sr_dev_inst *sdi;
235 for (l = drvc->instances; l; l = l->next) {
237 ieee1284_unref(sdi->conn);
241 return std_dev_clear(di, clear_private);
244 static int dev_open(struct sr_dev_inst *sdi)
246 struct dev_context *devc = sdi->priv;
249 if (sdi->status != SR_ST_INACTIVE)
252 if (ieee1284_open(sdi->conn, 0, &i) != E1284_OK)
255 if (ieee1284_claim(sdi->conn) != E1284_OK)
258 if (ieee1284_data_dir(sdi->conn, 1) != E1284_OK)
261 if (hung_chang_dso_2100_move_to(sdi, 1))
264 devc->samples = g_try_malloc(1000 * sizeof(*devc->samples));
268 sdi->status = SR_ST_ACTIVE;
273 hung_chang_dso_2100_reset_port(sdi->conn);
274 ieee1284_release(sdi->conn);
276 ieee1284_close(sdi->conn);
281 static int dev_close(struct sr_dev_inst *sdi)
283 struct dev_context *devc = sdi->priv;
285 g_free(devc->samples);
286 hung_chang_dso_2100_reset_port(sdi->conn);
287 ieee1284_release(sdi->conn);
288 ieee1284_close(sdi->conn);
290 sdi->status = SR_ST_INACTIVE;
295 static int find_in_array(GVariant *data, const GVariantType *type,
296 const void *arr, int n)
298 const char * const *sarr;
300 const uint64_t *u64arr;
301 const uint8_t *u8arr;
306 if (!g_variant_is_of_type(data, type))
309 switch (g_variant_classify(data)) {
310 case G_VARIANT_CLASS_STRING:
311 s = g_variant_get_string(data, NULL);
314 for (i = 0; i < n; i++)
315 if (!strcmp(s, sarr[i]))
318 case G_VARIANT_CLASS_UINT64:
319 u64 = g_variant_get_uint64(data);
322 for (i = 0; i < n; i++)
323 if (u64 == u64arr[i])
326 case G_VARIANT_CLASS_BYTE:
327 u8 = g_variant_get_byte(data);
330 for (i = 0; i < n; i++)
340 static int reverse_map(uint8_t u, const uint8_t *arr, int n)
342 GVariant *v = g_variant_new_byte(u);
343 int i = find_in_array(v, G_VARIANT_TYPE_BYTE, arr, n);
348 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
349 const struct sr_channel_group *cg)
351 struct dev_context *devc = sdi->priv;
352 struct parport *port;
355 if (cg) /* sr_config_get will validate cg using config_list */
356 ch = ((struct sr_channel *)cg->channels->data)->index;
362 *data = g_variant_new_string(port->name);
364 case SR_CONF_LIMIT_FRAMES:
365 *data = g_variant_new_uint64(devc->frame_limit);
367 case SR_CONF_SAMPLERATE:
368 *data = g_variant_new_uint64(samplerates[devc->rate]);
370 case SR_CONF_TRIGGER_SOURCE:
371 i = reverse_map(devc->cctl[0] & 0xC0, trigger_sources_map,
372 ARRAY_SIZE(trigger_sources_map));
376 *data = g_variant_new_string(trigger_sources[i]);
378 case SR_CONF_TRIGGER_SLOPE:
379 if (devc->edge >= ARRAY_SIZE(trigger_slopes))
382 *data = g_variant_new_string(trigger_slopes[devc->edge]);
384 case SR_CONF_BUFFERSIZE:
385 *data = g_variant_new_uint64(buffersizes[devc->last_step]);
389 ret = SR_ERR_CHANNEL_GROUP;
391 i = reverse_map(devc->cctl[ch] & 0x33, vdivs_map,
392 ARRAY_SIZE(vdivs_map));
396 *data = g_variant_new("(tt)", vdivs[i][0],
400 case SR_CONF_COUPLING:
402 ret = SR_ERR_CHANNEL_GROUP;
404 i = reverse_map(devc->cctl[ch] & 0x0C, coupling_map,
405 ARRAY_SIZE(coupling_map));
409 *data = g_variant_new_string(coupling[i]);
412 case SR_CONF_PROBE_FACTOR:
414 ret = SR_ERR_CHANNEL_GROUP;
416 *data = g_variant_new_uint64(devc->probe[ch]);
425 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
426 const struct sr_channel_group *cg)
428 struct dev_context *devc = sdi->priv;
432 if (cg) /* sr_config_set will validate cg using config_list */
433 ch = ((struct sr_channel *)cg->channels->data)->index;
437 case SR_CONF_LIMIT_FRAMES:
438 devc->frame_limit = g_variant_get_uint64(data);
440 case SR_CONF_SAMPLERATE:
441 i = find_in_array(data, G_VARIANT_TYPE_UINT64,
442 samplerates, ARRAY_SIZE(samplerates));
448 case SR_CONF_TRIGGER_SOURCE:
449 i = find_in_array(data, G_VARIANT_TYPE_STRING,
450 trigger_sources, ARRAY_SIZE(trigger_sources));
454 devc->cctl[0] = (devc->cctl[0] & 0x3F)
455 | trigger_sources_map[i];
457 case SR_CONF_TRIGGER_SLOPE:
458 i = find_in_array(data, G_VARIANT_TYPE_STRING,
459 trigger_slopes, ARRAY_SIZE(trigger_slopes));
465 case SR_CONF_BUFFERSIZE:
466 i = find_in_array(data, G_VARIANT_TYPE_UINT64,
467 buffersizes, ARRAY_SIZE(buffersizes));
475 ret = SR_ERR_CHANNEL_GROUP;
476 } else if (!g_variant_is_of_type(data, G_VARIANT_TYPE("(tt)"))) {
479 g_variant_get(data, "(tt)", &u, &v);
480 for (i = 0; i < (int)ARRAY_SIZE(vdivs); i++)
481 if (vdivs[i][0] == u && vdivs[i][1] == v)
483 if (i == ARRAY_SIZE(vdivs))
486 devc->cctl[ch] = (devc->cctl[ch] & 0xCC)
490 case SR_CONF_COUPLING:
492 ret = SR_ERR_CHANNEL_GROUP;
494 i = find_in_array(data, G_VARIANT_TYPE_STRING,
495 coupling, ARRAY_SIZE(coupling));
499 devc->cctl[ch] = (devc->cctl[ch] & 0xF3)
503 case SR_CONF_PROBE_FACTOR:
505 ret = SR_ERR_CHANNEL_GROUP;
507 u = g_variant_get_uint64(data);
521 static int config_channel_set(const struct sr_dev_inst *sdi,
522 struct sr_channel *ch,
523 unsigned int changes)
525 struct dev_context *devc = sdi->priv;
528 if (changes & SR_CHANNEL_SET_ENABLED) {
530 v = devc->channel | (1 << ch->index);
534 devc->enabled_channel->data = ch;
536 devc->channel &= ~(1 << ch->index);
542 static int config_commit(const struct sr_dev_inst *sdi)
544 uint8_t state = hung_chang_dso_2100_read_mbox(sdi->conn, 0.02);
551 /* we will travel the complete config path on our way to state 1 */
557 ret = hung_chang_dso_2100_move_to(sdi, 1);
562 hung_chang_dso_2100_write_mbox(sdi->conn, 4);
564 ret = hung_chang_dso_2100_move_to(sdi, 1);
567 return hung_chang_dso_2100_move_to(sdi, state);
570 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
571 const struct sr_channel_group *cg)
574 GVariant *gvar, *rational[2];
579 case SR_CONF_SCAN_OPTIONS:
580 case SR_CONF_DEVICE_OPTIONS:
582 case SR_CONF_SAMPLERATE:
583 case SR_CONF_TRIGGER_SOURCE:
584 case SR_CONF_TRIGGER_SLOPE:
585 case SR_CONF_BUFFERSIZE:
590 case SR_CONF_COUPLING:
594 return SR_ERR_CHANNEL_GROUP;
595 l = g_slist_find(sdi->channel_groups, cg);
604 case SR_CONF_SCAN_OPTIONS:
605 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
606 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
608 case SR_CONF_DEVICE_OPTIONS:
610 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
611 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
613 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
614 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
616 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
617 cgopts, ARRAY_SIZE(cgopts), sizeof(uint32_t));
619 case SR_CONF_SAMPLERATE:
620 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
621 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
622 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
623 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
624 *data = g_variant_builder_end(&gvb);
626 case SR_CONF_TRIGGER_SOURCE:
627 *data = g_variant_new_strv(trigger_sources, ARRAY_SIZE(trigger_sources));
629 case SR_CONF_TRIGGER_SLOPE:
630 *data = g_variant_new_strv(trigger_slopes, ARRAY_SIZE(trigger_slopes));
632 case SR_CONF_BUFFERSIZE:
633 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
634 buffersizes, ARRAY_SIZE(buffersizes), sizeof(uint64_t));
637 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
638 for (i = 0; i < (int)ARRAY_SIZE(vdivs); i++) {
639 rational[0] = g_variant_new_uint64(vdivs[i][0]);
640 rational[1] = g_variant_new_uint64(vdivs[i][1]);
641 gvar = g_variant_new_tuple(rational, 2);
642 g_variant_builder_add_value(&gvb, gvar);
644 *data = g_variant_builder_end(&gvb);
646 case SR_CONF_COUPLING:
647 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
654 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
656 struct dev_context *devc = sdi->priv;
660 static const float res_array[] = {0.5, 1, 2, 5};
661 static const uint8_t relays[] = {100, 10, 10, 1};
662 devc->factor = devc->probe[devc->channel - 1] / 32.0;
663 devc->factor *= res_array[devc->cctl[devc->channel - 1] & 0x03];
664 devc->factor /= relays[(devc->cctl[devc->channel - 1] >> 4) & 0x03];
667 devc->state_known = TRUE;
670 devc->retries = MAX_RETRIES;
672 ret = hung_chang_dso_2100_move_to(sdi, 0x21);
676 std_session_send_df_header(sdi);
678 sr_session_source_add(sdi->session, -1, 0, 8,
679 hung_chang_dso_2100_poll, (void *)sdi);
684 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
686 std_session_send_df_end(sdi);
687 sr_session_source_remove(sdi->session, -1);
688 hung_chang_dso_2100_move_to(sdi, 1);
693 static struct sr_dev_driver hung_chang_dso_2100_driver_info = {
694 .name = "hung-chang-dso-2100",
695 .longname = "Hung-Chang DSO-2100",
698 .cleanup = std_cleanup,
700 .dev_list = std_dev_list,
701 .dev_clear = dev_clear,
702 .config_get = config_get,
703 .config_set = config_set,
704 .config_channel_set = config_channel_set,
705 .config_commit = config_commit,
706 .config_list = config_list,
707 .dev_open = dev_open,
708 .dev_close = dev_close,
709 .dev_acquisition_start = dev_acquisition_start,
710 .dev_acquisition_stop = dev_acquisition_stop,
713 SR_REGISTER_DEV_DRIVER(hung_chang_dso_2100_driver_info);