]> sigrok.org Git - libsigrok.git/blame_incremental - src/hardware/hung-chang-dso-2100/api.c
drivers: Consistently use the exact driver name as LOG_PREFIX.
[libsigrok.git] / src / hardware / hung-chang-dso-2100 / api.c
... / ...
CommitLineData
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2015 Daniel Glöckner <daniel-gl@gmx.net>
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
20#include <config.h>
21#include <ieee1284.h>
22#include <string.h>
23#include "protocol.h"
24
25static const uint32_t scanopts[] = {
26 SR_CONF_CONN,
27};
28
29static const uint32_t drvopts[] = {
30 SR_CONF_OSCILLOSCOPE,
31};
32
33static 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,
40};
41
42static const uint32_t devopts_cg[] = {
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,
46};
47
48static 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)
56};
57
58/* must be in sync with readout_steps[] in protocol.c */
59static 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
64};
65
66static const uint64_t vdivs[][2] = {
67 { 10, 1000 },
68 { 20, 1000 },
69 { 50, 1000 },
70 { 100, 1000 },
71 { 200, 1000 },
72 { 500, 1000 },
73 { 1, 1 },
74 { 2, 1 },
75 { 5, 1 },
76};
77
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 */
80static const uint8_t vdivs_map[] = {
81 0x01, 0x02, 0x03, 0x21, 0x22, 0x23, 0x31, 0x32, 0x33
82};
83
84
85static const char *trigger_sources[] = {
86 "A", "B", "EXT"
87};
88
89static const uint8_t trigger_sources_map[] = {
90 0x00, 0x80, 0x40
91};
92
93static const char *trigger_slopes[] = {
94 "f", "r"
95};
96
97static const char *coupling[] = {
98 "DC", "AC", "GND"
99};
100
101static const uint8_t coupling_map[] = {
102 0x00, 0x08, 0x04
103};
104
105static GSList *scan_port(GSList *devices, struct parport *port)
106{
107 struct sr_dev_inst *sdi;
108 struct sr_channel *ch;
109 struct sr_channel_group *cg;
110 struct dev_context *devc;
111 int i;
112
113 if (ieee1284_open(port, 0, &i) != E1284_OK) {
114 sr_err("Can't open parallel port %s", port->name);
115 goto fail1;
116 }
117
118 if ((i & (CAP1284_RAW | CAP1284_BYTE)) != (CAP1284_RAW | CAP1284_BYTE)) {
119 sr_err("Parallel port %s does not provide low-level bidirection access",
120 port->name);
121 goto fail2;
122 }
123
124 if (ieee1284_claim(port) != E1284_OK) {
125 sr_err("Parallel port %s already in use", port->name);
126 goto fail2;
127 }
128
129 if (!hung_chang_dso_2100_check_id(port))
130 goto fail3;
131
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 */
137 sdi->conn = port;
138 ieee1284_ref(port);
139
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);
146 }
147
148 devc = g_malloc0(sizeof(struct dev_context));
149 devc->enabled_channel = g_slist_append(NULL, NULL);
150 devc->channel = 0;
151 devc->rate = 0;
152 devc->probe[0] = 10;
153 devc->probe[1] = 10;
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 */
156 devc->edge = 0;
157 devc->tlevel = 0x80;
158 devc->pos[0] = 0x80;
159 devc->pos[1] = 0x80;
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 */
166 sdi->priv = devc;
167
168 devices = g_slist_append(devices, sdi);
169
170fail3:
171 ieee1284_release(port);
172fail2:
173 ieee1284_close(port);
174fail1:
175 return devices;
176}
177
178static GSList *scan(struct sr_dev_driver *di, GSList *options)
179{
180 struct parport_list ports;
181 struct sr_config *src;
182 const char *conn = NULL;
183 GSList *devices, *option;
184 gboolean port_found;
185 int i;
186
187
188 for (option = options; option; option = option->next) {
189 src = option->data;
190 if (src->key == SR_CONF_CONN) {
191 conn = g_variant_get_string(src->data, NULL);
192 break;
193 }
194 }
195
196 if (!conn)
197 return NULL;
198
199 if (ieee1284_find_ports(&ports, 0) != E1284_OK)
200 return NULL;
201
202 devices = NULL;
203 port_found = FALSE;
204 for (i = 0; i < ports.portc; i++)
205 if (!strcmp(ports.portv[i]->name, conn)) {
206 port_found = TRUE;
207 devices = scan_port(devices, ports.portv[i]);
208 }
209
210 if (!port_found) {
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);
214 }
215
216 ieee1284_free_ports(&ports);
217
218 return std_scan_complete(di, devices);
219}
220
221static void clear_helper(struct dev_context *devc)
222{
223 g_slist_free(devc->enabled_channel);
224}
225
226static int dev_clear(const struct sr_dev_driver *di)
227{
228 struct drv_context *drvc = di->context;
229 struct sr_dev_inst *sdi;
230 GSList *l;
231
232 if (drvc) {
233 for (l = drvc->instances; l; l = l->next) {
234 sdi = l->data;
235 ieee1284_unref(sdi->conn);
236 }
237 }
238
239 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
240}
241
242static int dev_open(struct sr_dev_inst *sdi)
243{
244 struct dev_context *devc = sdi->priv;
245 int i;
246
247 if (ieee1284_open(sdi->conn, 0, &i) != E1284_OK)
248 goto fail1;
249
250 if (ieee1284_claim(sdi->conn) != E1284_OK)
251 goto fail2;
252
253 if (ieee1284_data_dir(sdi->conn, 1) != E1284_OK)
254 goto fail3;
255
256 if (hung_chang_dso_2100_move_to(sdi, 1))
257 goto fail3;
258
259 devc->samples = g_try_malloc(1000 * sizeof(*devc->samples));
260 if (!devc->samples)
261 goto fail3;
262
263 return SR_OK;
264
265fail3:
266 hung_chang_dso_2100_reset_port(sdi->conn);
267 ieee1284_release(sdi->conn);
268fail2:
269 ieee1284_close(sdi->conn);
270fail1:
271 return SR_ERR;
272}
273
274static int dev_close(struct sr_dev_inst *sdi)
275{
276 struct dev_context *devc = sdi->priv;
277
278 g_free(devc->samples);
279 hung_chang_dso_2100_reset_port(sdi->conn);
280 ieee1284_release(sdi->conn);
281 ieee1284_close(sdi->conn);
282
283 return SR_OK;
284}
285
286static int find_in_array(GVariant *data, const GVariantType *type,
287 const void *arr, int n)
288{
289 const char * const *sarr;
290 const char *s;
291 const uint64_t *u64arr;
292 const uint8_t *u8arr;
293 uint64_t u64;
294 uint8_t u8;
295 int i;
296
297 if (!g_variant_is_of_type(data, type))
298 return -1;
299
300 switch (g_variant_classify(data)) {
301 case G_VARIANT_CLASS_STRING:
302 s = g_variant_get_string(data, NULL);
303 sarr = arr;
304
305 for (i = 0; i < n; i++)
306 if (!strcmp(s, sarr[i]))
307 return i;
308 break;
309 case G_VARIANT_CLASS_UINT64:
310 u64 = g_variant_get_uint64(data);
311 u64arr = arr;
312
313 for (i = 0; i < n; i++)
314 if (u64 == u64arr[i])
315 return i;
316 break;
317 case G_VARIANT_CLASS_BYTE:
318 u8 = g_variant_get_byte(data);
319 u8arr = arr;
320
321 for (i = 0; i < n; i++)
322 if (u8 == u8arr[i])
323 return i;
324 default:
325 break;
326 }
327
328 return -1;
329}
330
331static int reverse_map(uint8_t u, const uint8_t *arr, int n)
332{
333 GVariant *v = g_variant_new_byte(u);
334 int i = find_in_array(v, G_VARIANT_TYPE_BYTE, arr, n);
335 g_variant_unref(v);
336 return i;
337}
338
339static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
340 const struct sr_channel_group *cg)
341{
342 struct dev_context *devc = sdi->priv;
343 struct parport *port;
344 int i, ch = -1;
345
346 if (cg) /* sr_config_get will validate cg using config_list */
347 ch = ((struct sr_channel *)cg->channels->data)->index;
348
349 switch (key) {
350 case SR_CONF_CONN:
351 port = sdi->conn;
352 *data = g_variant_new_string(port->name);
353 break;
354 case SR_CONF_LIMIT_FRAMES:
355 *data = g_variant_new_uint64(devc->frame_limit);
356 break;
357 case SR_CONF_SAMPLERATE:
358 *data = g_variant_new_uint64(samplerates[devc->rate]);
359 break;
360 case SR_CONF_TRIGGER_SOURCE:
361 i = reverse_map(devc->cctl[0] & 0xC0, ARRAY_AND_SIZE(trigger_sources_map));
362 if (i == -1)
363 return SR_ERR;
364 else
365 *data = g_variant_new_string(trigger_sources[i]);
366 break;
367 case SR_CONF_TRIGGER_SLOPE:
368 if (devc->edge >= ARRAY_SIZE(trigger_slopes))
369 return SR_ERR;
370 else
371 *data = g_variant_new_string(trigger_slopes[devc->edge]);
372 break;
373 case SR_CONF_BUFFERSIZE:
374 *data = g_variant_new_uint64(buffersizes[devc->last_step]);
375 break;
376 case SR_CONF_VDIV:
377 if (ch == -1) {
378 return SR_ERR_CHANNEL_GROUP;
379 } else {
380 i = reverse_map(devc->cctl[ch] & 0x33, ARRAY_AND_SIZE(vdivs_map));
381 if (i == -1)
382 return SR_ERR;
383 else
384 *data = g_variant_new("(tt)", vdivs[i][0],
385 vdivs[i][1]);
386 }
387 break;
388 case SR_CONF_COUPLING:
389 if (ch == -1) {
390 return SR_ERR_CHANNEL_GROUP;
391 } else {
392 i = reverse_map(devc->cctl[ch] & 0x0C, ARRAY_AND_SIZE(coupling_map));
393 if (i == -1)
394 return SR_ERR;
395 else
396 *data = g_variant_new_string(coupling[i]);
397 }
398 break;
399 case SR_CONF_PROBE_FACTOR:
400 if (ch == -1)
401 return SR_ERR_CHANNEL_GROUP;
402 else
403 *data = g_variant_new_uint64(devc->probe[ch]);
404 break;
405 default:
406 return SR_ERR_NA;
407 }
408
409 return SR_OK;
410}
411
412static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
413 const struct sr_channel_group *cg)
414{
415 struct dev_context *devc = sdi->priv;
416 int i, ch = -1;
417 uint64_t u, v;
418
419 if (cg) /* sr_config_set will validate cg using config_list */
420 ch = ((struct sr_channel *)cg->channels->data)->index;
421
422 switch (key) {
423 case SR_CONF_LIMIT_FRAMES:
424 devc->frame_limit = g_variant_get_uint64(data);
425 break;
426 case SR_CONF_SAMPLERATE:
427 i = find_in_array(data, G_VARIANT_TYPE_UINT64,
428 ARRAY_AND_SIZE(samplerates));
429 if (i == -1)
430 return SR_ERR_ARG;
431 else
432 devc->rate = i;
433 break;
434 case SR_CONF_TRIGGER_SOURCE:
435 i = find_in_array(data, G_VARIANT_TYPE_STRING,
436 ARRAY_AND_SIZE(trigger_sources));
437 if (i == -1)
438 return SR_ERR_ARG;
439 else
440 devc->cctl[0] = (devc->cctl[0] & 0x3F)
441 | trigger_sources_map[i];
442 break;
443 case SR_CONF_TRIGGER_SLOPE:
444 i = find_in_array(data, G_VARIANT_TYPE_STRING,
445 ARRAY_AND_SIZE(trigger_slopes));
446 if (i == -1)
447 return SR_ERR_ARG;
448 else
449 devc->edge = i;
450 break;
451 case SR_CONF_BUFFERSIZE:
452 i = find_in_array(data, G_VARIANT_TYPE_UINT64,
453 ARRAY_AND_SIZE(buffersizes));
454 if (i == -1)
455 return SR_ERR_ARG;
456 else
457 devc->last_step = i;
458 break;
459 case SR_CONF_VDIV:
460 if (ch == -1) {
461 return SR_ERR_CHANNEL_GROUP;
462 } else if (!g_variant_is_of_type(data, G_VARIANT_TYPE("(tt)"))) {
463 return SR_ERR_ARG;
464 } else {
465 g_variant_get(data, "(tt)", &u, &v);
466 for (i = 0; i < (int)ARRAY_SIZE(vdivs); i++)
467 if (vdivs[i][0] == u && vdivs[i][1] == v)
468 break;
469 if (i == ARRAY_SIZE(vdivs))
470 return SR_ERR_ARG;
471 else
472 devc->cctl[ch] = (devc->cctl[ch] & 0xCC)
473 | vdivs_map[i];
474 }
475 break;
476 case SR_CONF_COUPLING:
477 if (ch == -1) {
478 return SR_ERR_CHANNEL_GROUP;
479 } else {
480 i = find_in_array(data, G_VARIANT_TYPE_STRING,
481 ARRAY_AND_SIZE(coupling));
482 if (i == -1)
483 return SR_ERR_ARG;
484 else
485 devc->cctl[ch] = (devc->cctl[ch] & 0xF3)
486 | coupling_map[i];
487 }
488 break;
489 case SR_CONF_PROBE_FACTOR:
490 if (ch == -1) {
491 return SR_ERR_CHANNEL_GROUP;
492 } else {
493 u = g_variant_get_uint64(data);
494 if (!u)
495 return SR_ERR_ARG;
496 else
497 devc->probe[ch] = u;
498 }
499 break;
500 default:
501 return SR_ERR_NA;
502 }
503
504 return SR_OK;
505}
506
507static int config_channel_set(const struct sr_dev_inst *sdi,
508 struct sr_channel *ch,
509 unsigned int changes)
510{
511 struct dev_context *devc = sdi->priv;
512 uint8_t v;
513
514 if (changes & SR_CHANNEL_SET_ENABLED) {
515 if (ch->enabled) {
516 v = devc->channel | (1 << ch->index);
517 if (v & (v - 1))
518 return SR_ERR;
519 devc->channel = v;
520 devc->enabled_channel->data = ch;
521 } else {
522 devc->channel &= ~(1 << ch->index);
523 }
524 }
525 return SR_OK;
526}
527
528static int config_commit(const struct sr_dev_inst *sdi)
529{
530 uint8_t state = hung_chang_dso_2100_read_mbox(sdi->conn, 0.02);
531 int ret;
532
533 switch (state) {
534 case 0x03:
535 case 0x14:
536 case 0x21:
537 /* we will travel the complete config path on our way to state 1 */
538 break;
539 case 0x00:
540 state = 0x01;
541 /* Fallthrough */
542 default:
543 ret = hung_chang_dso_2100_move_to(sdi, 1);
544 if (ret != SR_OK)
545 return ret;
546 /* Fallthrough */
547 case 0x01:
548 hung_chang_dso_2100_write_mbox(sdi->conn, 4);
549 }
550 ret = hung_chang_dso_2100_move_to(sdi, 1);
551 if (ret != SR_OK)
552 return ret;
553 return hung_chang_dso_2100_move_to(sdi, state);
554}
555
556static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
557 const struct sr_channel_group *cg)
558{
559 GSList *l;
560
561 switch (key) {
562 case SR_CONF_SCAN_OPTIONS:
563 case SR_CONF_DEVICE_OPTIONS:
564 break;
565 case SR_CONF_SAMPLERATE:
566 case SR_CONF_TRIGGER_SOURCE:
567 case SR_CONF_TRIGGER_SLOPE:
568 case SR_CONF_BUFFERSIZE:
569 if (!sdi || cg)
570 return SR_ERR_NA;
571 break;
572 case SR_CONF_VDIV:
573 case SR_CONF_COUPLING:
574 if (!sdi)
575 return SR_ERR_NA;
576 if (!cg)
577 return SR_ERR_CHANNEL_GROUP;
578 l = g_slist_find(sdi->channel_groups, cg);
579 if (!l)
580 return SR_ERR_ARG;
581 break;
582 default:
583 return SR_ERR_NA;
584 }
585
586 switch (key) {
587 case SR_CONF_SCAN_OPTIONS:
588 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, NULL, NULL);
589 case SR_CONF_DEVICE_OPTIONS:
590 if (!cg)
591 return STD_CONFIG_LIST(key, data, sdi, cg, NULL, drvopts, devopts);
592 *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg));
593 break;
594 case SR_CONF_SAMPLERATE:
595 *data = std_gvar_samplerates(ARRAY_AND_SIZE(samplerates));
596 break;
597 case SR_CONF_TRIGGER_SOURCE:
598 *data = g_variant_new_strv(ARRAY_AND_SIZE(trigger_sources));
599 break;
600 case SR_CONF_TRIGGER_SLOPE:
601 *data = g_variant_new_strv(ARRAY_AND_SIZE(trigger_slopes));
602 break;
603 case SR_CONF_BUFFERSIZE:
604 *data = std_gvar_array_u64(ARRAY_AND_SIZE(buffersizes));
605 break;
606 case SR_CONF_VDIV:
607 *data = std_gvar_tuple_array(&vdivs, ARRAY_SIZE(vdivs));
608 break;
609 case SR_CONF_COUPLING:
610 *data = g_variant_new_strv(ARRAY_AND_SIZE(coupling));
611 break;
612 }
613
614 return SR_OK;
615}
616
617static int dev_acquisition_start(const struct sr_dev_inst *sdi)
618{
619 struct dev_context *devc = sdi->priv;
620 int ret;
621
622 if (devc->channel) {
623 static const float res_array[] = {0.5, 1, 2, 5};
624 static const uint8_t relays[] = {100, 10, 10, 1};
625 devc->factor = devc->probe[devc->channel - 1] / 32.0;
626 devc->factor *= res_array[devc->cctl[devc->channel - 1] & 0x03];
627 devc->factor /= relays[(devc->cctl[devc->channel - 1] >> 4) & 0x03];
628 }
629 devc->frame = 0;
630 devc->state_known = TRUE;
631 devc->step = 0;
632 devc->adc2 = FALSE;
633 devc->retries = MAX_RETRIES;
634
635 ret = hung_chang_dso_2100_move_to(sdi, 0x21);
636 if (ret != SR_OK)
637 return ret;
638
639 std_session_send_df_header(sdi);
640
641 sr_session_source_add(sdi->session, -1, 0, 8,
642 hung_chang_dso_2100_poll, (void *)sdi);
643
644 return SR_OK;
645}
646
647static int dev_acquisition_stop(struct sr_dev_inst *sdi)
648{
649 std_session_send_df_end(sdi);
650 sr_session_source_remove(sdi->session, -1);
651 hung_chang_dso_2100_move_to(sdi, 1);
652
653 return SR_OK;
654}
655
656static struct sr_dev_driver hung_chang_dso_2100_driver_info = {
657 .name = "hung-chang-dso-2100",
658 .longname = "Hung-Chang DSO-2100",
659 .api_version = 1,
660 .init = std_init,
661 .cleanup = std_cleanup,
662 .scan = scan,
663 .dev_list = std_dev_list,
664 .dev_clear = dev_clear,
665 .config_get = config_get,
666 .config_set = config_set,
667 .config_channel_set = config_channel_set,
668 .config_commit = config_commit,
669 .config_list = config_list,
670 .dev_open = dev_open,
671 .dev_close = dev_close,
672 .dev_acquisition_start = dev_acquisition_start,
673 .dev_acquisition_stop = dev_acquisition_stop,
674 .context = NULL,
675};
676SR_REGISTER_DEV_DRIVER(hung_chang_dso_2100_driver_info);