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 cleanup(const struct sr_dev_driver *di)
316 struct drv_context *drvc = di->context;
326 static int find_in_array(GVariant *data, const GVariantType *type,
327 const void *arr, int n)
329 const char * const *sarr;
331 const uint64_t *u64arr;
332 const uint8_t *u8arr;
337 if (!g_variant_is_of_type(data, type))
340 switch (g_variant_classify(data)) {
341 case G_VARIANT_CLASS_STRING:
342 s = g_variant_get_string(data, NULL);
345 for (i = 0; i < n; i++)
346 if (!strcmp(s, sarr[i]))
349 case G_VARIANT_CLASS_UINT64:
350 u64 = g_variant_get_uint64(data);
353 for (i = 0; i < n; i++)
354 if (u64 == u64arr[i])
357 case G_VARIANT_CLASS_BYTE:
358 u8 = g_variant_get_byte(data);
361 for (i = 0; i < n; i++)
371 static int reverse_map(uint8_t u, const uint8_t *arr, int n)
373 GVariant *v = g_variant_new_byte(u);
374 int i = find_in_array(v, G_VARIANT_TYPE_BYTE, arr, n);
379 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
380 const struct sr_channel_group *cg)
382 struct dev_context *devc = sdi->priv;
383 struct parport *port;
386 if (cg) /* sr_config_get will validate cg using config_list */
387 ch = ((struct sr_channel *)cg->channels->data)->index;
393 *data = g_variant_new_string(port->name);
395 case SR_CONF_LIMIT_FRAMES:
396 *data = g_variant_new_uint64(devc->frame_limit);
398 case SR_CONF_SAMPLERATE:
399 *data = g_variant_new_uint64(samplerates[devc->rate]);
401 case SR_CONF_TRIGGER_SOURCE:
402 i = reverse_map(devc->cctl[0] & 0xC0, trigger_sources_map,
403 ARRAY_SIZE(trigger_sources_map));
407 *data = g_variant_new_string(trigger_sources[i]);
409 case SR_CONF_TRIGGER_SLOPE:
410 if (devc->edge >= ARRAY_SIZE(trigger_slopes))
413 *data = g_variant_new_string(trigger_slopes[devc->edge]);
415 case SR_CONF_BUFFERSIZE:
416 *data = g_variant_new_uint64(buffersizes[devc->last_step]);
420 ret = SR_ERR_CHANNEL_GROUP;
422 i = reverse_map(devc->cctl[ch] & 0x33, vdivs_map,
423 ARRAY_SIZE(vdivs_map));
427 *data = g_variant_new("(tt)", vdivs[i][0],
431 case SR_CONF_COUPLING:
433 ret = SR_ERR_CHANNEL_GROUP;
435 i = reverse_map(devc->cctl[ch] & 0x0C, coupling_map,
436 ARRAY_SIZE(coupling_map));
440 *data = g_variant_new_string(coupling[i]);
443 case SR_CONF_PROBE_FACTOR:
445 ret = SR_ERR_CHANNEL_GROUP;
447 *data = g_variant_new_uint64(devc->probe[ch]);
456 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
457 const struct sr_channel_group *cg)
459 struct dev_context *devc = sdi->priv;
463 if (cg) /* sr_config_set will validate cg using config_list */
464 ch = ((struct sr_channel *)cg->channels->data)->index;
466 if (sdi->status != SR_ST_ACTIVE)
467 return SR_ERR_DEV_CLOSED;
471 case SR_CONF_LIMIT_FRAMES:
472 devc->frame_limit = g_variant_get_uint64(data);
474 case SR_CONF_SAMPLERATE:
475 i = find_in_array(data, G_VARIANT_TYPE_UINT64,
476 samplerates, ARRAY_SIZE(samplerates));
482 case SR_CONF_TRIGGER_SOURCE:
483 i = find_in_array(data, G_VARIANT_TYPE_STRING,
484 trigger_sources, ARRAY_SIZE(trigger_sources));
488 devc->cctl[0] = (devc->cctl[0] & 0x3F)
489 | trigger_sources_map[i];
491 case SR_CONF_TRIGGER_SLOPE:
492 i = find_in_array(data, G_VARIANT_TYPE_STRING,
493 trigger_slopes, ARRAY_SIZE(trigger_slopes));
499 case SR_CONF_BUFFERSIZE:
500 i = find_in_array(data, G_VARIANT_TYPE_UINT64,
501 buffersizes, ARRAY_SIZE(buffersizes));
509 ret = SR_ERR_CHANNEL_GROUP;
510 } else if (!g_variant_is_of_type(data, G_VARIANT_TYPE("(tt)"))) {
513 g_variant_get(data, "(tt)", &u, &v);
514 for (i = 0; i < (int)ARRAY_SIZE(vdivs); i++)
515 if (vdivs[i][0] == u && vdivs[i][1] == v)
517 if (i == ARRAY_SIZE(vdivs))
520 devc->cctl[ch] = (devc->cctl[ch] & 0xCC)
524 case SR_CONF_COUPLING:
526 ret = SR_ERR_CHANNEL_GROUP;
528 i = find_in_array(data, G_VARIANT_TYPE_STRING,
529 coupling, ARRAY_SIZE(coupling));
533 devc->cctl[ch] = (devc->cctl[ch] & 0xF3)
537 case SR_CONF_PROBE_FACTOR:
539 ret = SR_ERR_CHANNEL_GROUP;
541 u = g_variant_get_uint64(data);
555 static int config_channel_set(const struct sr_dev_inst *sdi,
556 struct sr_channel *ch,
557 unsigned int changes)
559 struct dev_context *devc = sdi->priv;
562 if (changes & SR_CHANNEL_SET_ENABLED) {
564 v = devc->channel | (1 << ch->index);
568 devc->enabled_channel->data = ch;
570 devc->channel &= ~(1 << ch->index);
576 static int config_commit(const struct sr_dev_inst *sdi)
578 uint8_t state = hung_chang_dso_2100_read_mbox(sdi->conn, 0.02);
581 if (sdi->status != SR_ST_ACTIVE)
582 return SR_ERR_DEV_CLOSED;
588 /* we will travel the complete config path on our way to state 1 */
593 ret = hung_chang_dso_2100_move_to(sdi, 1);
597 hung_chang_dso_2100_write_mbox(sdi->conn, 4);
599 ret = hung_chang_dso_2100_move_to(sdi, 1);
602 return hung_chang_dso_2100_move_to(sdi, state);
605 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
606 const struct sr_channel_group *cg)
609 GVariant *gvar, *rational[2];
614 case SR_CONF_SCAN_OPTIONS:
615 case SR_CONF_DEVICE_OPTIONS:
617 case SR_CONF_SAMPLERATE:
618 case SR_CONF_TRIGGER_SOURCE:
619 case SR_CONF_TRIGGER_SLOPE:
620 case SR_CONF_BUFFERSIZE:
625 case SR_CONF_COUPLING:
629 return SR_ERR_CHANNEL_GROUP;
630 l = g_slist_find(sdi->channel_groups, cg);
639 case SR_CONF_SCAN_OPTIONS:
640 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
641 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
643 case SR_CONF_DEVICE_OPTIONS:
645 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
646 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
648 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
649 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
651 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
652 cgopts, ARRAY_SIZE(cgopts), sizeof(uint32_t));
654 case SR_CONF_SAMPLERATE:
655 g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}"));
656 gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"),
657 samplerates, ARRAY_SIZE(samplerates), sizeof(uint64_t));
658 g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar);
659 *data = g_variant_builder_end(&gvb);
661 case SR_CONF_TRIGGER_SOURCE:
662 *data = g_variant_new_strv(trigger_sources, ARRAY_SIZE(trigger_sources));
664 case SR_CONF_TRIGGER_SLOPE:
665 *data = g_variant_new_strv(trigger_slopes, ARRAY_SIZE(trigger_slopes));
667 case SR_CONF_BUFFERSIZE:
668 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64,
669 buffersizes, ARRAY_SIZE(buffersizes), sizeof(uint64_t));
672 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
673 for (i = 0; i < (int)ARRAY_SIZE(vdivs); i++) {
674 rational[0] = g_variant_new_uint64(vdivs[i][0]);
675 rational[1] = g_variant_new_uint64(vdivs[i][1]);
676 gvar = g_variant_new_tuple(rational, 2);
677 g_variant_builder_add_value(&gvb, gvar);
679 *data = g_variant_builder_end(&gvb);
681 case SR_CONF_COUPLING:
682 *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
689 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
692 struct dev_context *devc = sdi->priv;
695 if (sdi->status != SR_ST_ACTIVE)
696 return SR_ERR_DEV_CLOSED;
699 static const float res_array[] = {0.5, 1, 2, 5};
700 static const uint8_t relays[] = {100, 10, 10, 1};
701 devc->factor = devc->probe[devc->channel - 1] / 32.0;
702 devc->factor *= res_array[devc->cctl[devc->channel - 1] & 0x03];
703 devc->factor /= relays[(devc->cctl[devc->channel - 1] >> 4) & 0x03];
706 devc->cb_data = cb_data;
707 devc->state_known = TRUE;
710 devc->retries = MAX_RETRIES;
712 ret = hung_chang_dso_2100_move_to(sdi, 0x21);
716 std_session_send_df_header(cb_data, LOG_PREFIX);
718 sr_session_source_add(sdi->session, 0, 0, 8,
719 hung_chang_dso_2100_poll, (void *)sdi);
724 SR_PRIV int hung_chang_dso_2100_dev_acquisition_stop(const struct sr_dev_inst *sdi,
727 struct sr_datafeed_packet packet = { .type = SR_DF_END };
729 if (sdi->status != SR_ST_ACTIVE)
730 return SR_ERR_DEV_CLOSED;
732 sr_session_send(cb_data, &packet);
733 sr_session_source_remove(sdi->session, 0);
735 hung_chang_dso_2100_move_to(sdi, 1);
740 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
742 return hung_chang_dso_2100_dev_acquisition_stop(sdi, cb_data);
745 SR_PRIV struct sr_dev_driver hung_chang_dso_2100_driver_info = {
746 .name = "hung-chang-dso-2100",
747 .longname = "Hung-Chang DSO-2100",
752 .dev_list = dev_list,
753 .dev_clear = dev_clear,
754 .config_get = config_get,
755 .config_set = config_set,
756 .config_channel_set = config_channel_set,
757 .config_commit = config_commit,
758 .config_list = config_list,
759 .dev_open = dev_open,
760 .dev_close = dev_close,
761 .dev_acquisition_start = dev_acquisition_start,
762 .dev_acquisition_stop = dev_acquisition_stop,