2 * This file is part of the sigrok project.
4 * Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libsigrok.h"
25 #include "libsigrok-internal.h"
27 #define MIN_NUM_SAMPLES 1
30 uint64_t limit_samples;
31 uint64_t limit_msec; /* TODO: Implement. */
34 struct sr_serial_dev_inst *serial;
35 uint8_t bytes[14 + 1];
37 gboolean synchronized;
38 gboolean got_14_bytes;
41 static const int hwcaps[] = {
42 SR_HWCAP_OSCILLOSCOPE,
43 SR_HWCAP_LIMIT_SAMPLES,
44 // SR_HWCAP_LIMIT_MSEC,
45 // SR_HWCAP_CONTINUOUS,
49 static const char *probe_names[] = {
54 static GSList *dev_insts = NULL;
56 /* Function prototypes. */
57 static int hw_dev_acquisition_stop(int dev_index, void *cb_data);
59 static int hw_init(const char *devinfo)
61 struct sr_dev_inst *sdi;
65 /* Avoid compiler warnings. */
66 (void)devinfo; /* TODO: This specifies the serial port to use. */
68 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
69 sr_err("va18b: %s: ctx malloc failed.", __func__);
73 ctx->limit_samples = 0;
76 ctx->session_dev_id = NULL;
78 memset(ctx->bytes, 0x00, 14 + 1);
79 ctx->byte_counter = 0;
80 ctx->synchronized = FALSE;
81 ctx->got_14_bytes = FALSE;
83 if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_ACTIVE,
84 "Mastech", "VA18B", ""))) {
85 sr_err("va18b: %s: sr_dev_inst_new returned NULL.", __func__);
91 /* TODO: Don't hardcode serial port. */
92 ctx->serial = sr_serial_dev_inst_new("/dev/ttyUSB0", -1);
94 dev_insts = g_slist_append(dev_insts, sdi);
101 static int hw_dev_open(int dev_index)
103 struct sr_dev_inst *sdi;
106 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
107 sr_err("va18b: %s: sdi was NULL.", __func__);
111 if (!(ctx = sdi->priv)) {
112 sr_err("va18b: %s: sdi->priv was NULL.", __func__);
116 sr_dbg("va18b: %s: Opening serial port '%s'.", __func__,
119 /* TODO: Check for != NULL. */
121 /* TODO: O_NONBLOCK? */
122 ctx->serial->fd = serial_open(ctx->serial->port, O_RDWR | O_NONBLOCK);
123 if (ctx->serial->fd == -1) {
124 sr_err("va18b: %s: Couldn't open serial port '%s'.", __func__,
129 serial_set_params(ctx->serial->fd, 2400, 8, 0, 1, 2 /* TODO */);
134 static int hw_dev_close(int dev_index)
136 struct sr_dev_inst *sdi;
139 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
140 sr_err("va18b: %s: sdi was NULL.", __func__);
144 if (!(ctx = sdi->priv)) {
145 sr_err("va18b: %s: sdi->priv was NULL.", __func__);
149 /* TODO: Check for != NULL. */
151 if (ctx->serial->fd != -1) {
152 serial_close(ctx->serial->fd);
153 ctx->serial->fd = -1;
154 sdi->status = SR_ST_INACTIVE;
160 static int hw_cleanup(void)
163 struct sr_dev_inst *sdi;
166 /* Properly close and free all devices. */
167 for (l = dev_insts; l; l = l->next) {
168 if (!(sdi = l->data)) {
169 /* Log error, but continue cleaning up the rest. */
170 sr_err("va18b: %s: sdi was NULL, continuing.",
174 if (!(ctx = sdi->priv)) {
175 /* Log error, but continue cleaning up the rest. */
176 sr_err("va18b: %s: sdi->priv was NULL, continuing.",
181 /* TODO: Check for serial != NULL. */
182 if (ctx->serial->fd != -1)
183 serial_close(ctx->serial->fd);
184 sr_serial_dev_inst_free(ctx->serial);
186 sr_dev_inst_free(sdi);
189 g_slist_free(dev_insts);
195 static const void *hw_dev_info_get(int dev_index, int dev_info_id)
197 struct sr_dev_inst *sdi;
203 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
204 sr_err("va18b: %s: sdi was NULL.", __func__);
208 if (!(ctx = sdi->priv)) {
209 sr_err("va18b: %s: sdi->priv was NULL.", __func__);
213 sr_spew("va18b: %s: dev_index %d, dev_info_id %d.", __func__,
214 dev_index, dev_info_id);
216 switch (dev_info_id) {
219 sr_spew("va18b: %s: Returning sdi.", __func__);
221 case SR_DI_NUM_PROBES:
223 info = (int *)tmpint;
224 sr_spew("va18b: %s: Returning number of probes: 1.", __func__);
226 case SR_DI_PROBE_NAMES:
228 sr_spew("va18b: %s: Returning probenames.", __func__);
230 case SR_DI_CUR_SAMPLERATE:
233 info = (uint64_t *)&tmp;
234 sr_spew("va18b: %s: Returning samplerate: %" PRIu64 "Hz.",
238 /* Unknown device info ID, return NULL. */
239 sr_err("va18b: %s: Unknown device info ID: %d.",
240 __func__, dev_info_id);
248 static int hw_dev_status_get(int dev_index)
250 struct sr_dev_inst *sdi;
252 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
253 sr_err("va18b: %s: sdi was NULL, device not found.", __func__);
254 return SR_ST_NOT_FOUND;
257 sr_dbg("va18b: Returning status: %d.", sdi->status);
262 static const int *hw_hwcap_get_all(void)
264 sr_spew("va18b: Returning list of device capabilities.");
269 static int hw_dev_config_set(int dev_index, int hwcap, const void *value)
271 struct sr_dev_inst *sdi;
274 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
275 sr_err("va18b: %s: sdi was NULL.", __func__);
279 if (!(ctx = sdi->priv)) {
280 sr_err("va18b: %s: sdi->priv was NULL.", __func__);
284 sr_spew("va18b: %s: dev_index %d, hwcap %d.", __func__,
288 case SR_HWCAP_PROBECONFIG:
289 /* TODO: Required? */
291 case SR_HWCAP_LIMIT_MSEC:
292 if (*(const uint64_t *)value == 0) {
293 sr_err("va18b: %s: LIMIT_MSEC can't be 0.", __func__);
296 ctx->limit_msec = *(const uint64_t *)value;
297 sr_dbg("va18b: Setting LIMIT_MSEC to %" PRIu64 ".",
300 case SR_HWCAP_LIMIT_SAMPLES:
301 if (*(const uint64_t *)value < MIN_NUM_SAMPLES) {
302 sr_err("va18b: %s: LIMIT_SAMPLES too small.", __func__);
305 ctx->limit_samples = *(const uint64_t *)value;
306 sr_dbg("va18b: Setting LIMIT_SAMPLES to %" PRIu64 ".",
310 /* Unknown capability, return SR_ERR. */
311 sr_err("va18b: %s: Unknown capability: %d.", __func__, hwcap);
319 static uint8_t get_digit(uint8_t b1, uint8_t b2)
323 b = ((b1 & 0x0f) << 4) | (b2 & 0x0f);
324 printf("0x%02x (b1: 0x%02x, b2: 0x%02x)\n", b, b1, b2);
349 return 0xff; /* Error */
353 static int receive_data(int fd, int revents, void *cb_data)
355 struct sr_dev_inst *sdi;
356 struct sr_datafeed_packet packet;
357 struct sr_datafeed_analog analog;
360 uint8_t b, left_nibble, right_nibble;
362 if (!(sdi = cb_data)) {
363 sr_err("va18b: %s: cb_data was NULL.", __func__);
367 if (!(ctx = sdi->priv)) {
368 sr_err("va18b: %s: sdi->priv was NULL.", __func__);
372 if (revents != G_IO_IN) {
373 sr_err("va18b: %s: No data?", __func__);
377 if (serial_read(fd, &b, 1) != 1) {
378 sr_err("va18b: %s: Could not read a byte from serial port.",
383 left_nibble = ((b & 0xf0) >> 4);
385 /* Upon starting, we wait until we're at byte 1. */
386 if (ctx->synchronized == FALSE && left_nibble != 1) {
387 sr_spew("va18b: Waiting for byte #1 in order to synchronize "
388 "(got byte #%d).", left_nibble);
389 return TRUE; // FALSE?
390 } else if (ctx->synchronized == FALSE && left_nibble == 1) {
391 sr_dbg("va18b: Successfully synchronized to data stream.");
392 ctx->synchronized = TRUE;
393 ctx->byte_counter = 1;
396 /* TODO: Check for left_nibble in (1, 14). */
398 ctx->bytes[ctx->byte_counter++] = b;
400 /// sr_dbg("va18b: bc: %d", ctx->byte_counter);
401 if (ctx->byte_counter == 14 + 1) {
402 sr_dbg("va18b: Received all 14 bytes for this packet.");
403 ctx->got_14_bytes = TRUE;
405 sr_spew("va18b: Didn't receive all 14 bytes, yet.");
406 return TRUE; // FALSE?
409 printf("0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x "
410 "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
411 ctx->bytes[1], ctx->bytes[2], ctx->bytes[3], ctx->bytes[4],
412 ctx->bytes[5], ctx->bytes[6], ctx->bytes[7], ctx->bytes[8],
413 ctx->bytes[9], ctx->bytes[10], ctx->bytes[11], ctx->bytes[12],
414 ctx->bytes[13], ctx->bytes[14]);
416 printf("0x%02x %d %d %d\n",
417 get_digit(ctx->bytes[2], ctx->bytes[3]),
418 get_digit(ctx->bytes[4], ctx->bytes[5]),
419 get_digit(ctx->bytes[6], ctx->bytes[7]),
420 get_digit(ctx->bytes[8], ctx->bytes[9]));
422 ctx->got_14_bytes = FALSE;
423 ctx->byte_counter = 0;
425 sr_dbg("va18b: Sending SR_DF_ANALOG packet with 1 sample.");
426 /* TODO: timestamp. */
428 packet.type = SR_DF_ANALOG;
429 packet.payload = &analog;
430 analog.num_samples = 1;
431 analog.mq = SR_MQ_VOLTAGE;
432 analog.unit = SR_UNIT_VOLT;
433 /* TODO: Check alloc return value. */
434 analog.data = g_try_malloc(analog.num_samples * sizeof(float) * num_probes);
435 // analog.data[0] = rand() % 42; /* Transmit dummy data for now. */
436 analog.data[0] = b; /* Transmit dummy data for now. */
437 sr_session_send(ctx->session_dev_id, &packet);
441 /* Support for SR_HWCAP_LIMIT_SAMPLES. */
442 if (ctx->limit_samples > 0 && ctx->num_samples > ctx->limit_samples) {
443 hw_dev_acquisition_stop(0 /* FIXME? */, cb_data);
450 static int hw_dev_acquisition_start(int dev_index, void *cb_data)
452 struct sr_datafeed_packet packet;
453 struct sr_datafeed_header header;
454 struct sr_datafeed_meta_analog meta;
455 struct sr_dev_inst *sdi;
458 if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
459 sr_err("va18b: %s: sdi was NULL.", __func__);
463 if (!(ctx = sdi->priv)) {
464 sr_err("va18b: %s: sdi->priv was NULL.", __func__);
468 sr_dbg("va18b: Starting acquisition.");
470 ctx->session_dev_id = cb_data;
472 /* Send header packet to the session bus. */
473 sr_dbg("va18b: Sending SR_DF_HEADER.");
474 packet.type = SR_DF_HEADER;
475 packet.payload = (uint8_t *)&header;
476 header.feed_version = 1;
477 gettimeofday(&header.starttime, NULL);
478 sr_session_send(ctx->session_dev_id, &packet);
480 /* Send metadata about the SR_DF_ANALOG packets to come. */
481 sr_dbg("va18b: Sending SR_DF_META_ANALOG.");
482 packet.type = SR_DF_META_ANALOG;
483 packet.payload = &meta;
485 sr_session_send(ctx->session_dev_id, &packet);
487 /* Hook up a dummy handler to receive data from the device. */
488 // sr_source_add(-1, G_IO_IN, 0, receive_data, sdi);
489 sr_source_add(ctx->serial->fd, G_IO_IN, -1, receive_data, sdi);
494 static int hw_dev_acquisition_stop(int dev_index, void *cb_data)
496 struct sr_datafeed_packet packet;
498 /* Avoid compiler warnings. */
501 sr_dbg("va18b: Stopping acquisition.");
503 /* Send end packet to the session bus. */
504 sr_dbg("va18b: Sending SR_DF_END.");
505 packet.type = SR_DF_END;
506 sr_session_send(cb_data, &packet);
511 SR_PRIV struct sr_dev_driver mastech_va18b_driver_info = {
512 .name = "mastech-va18b",
513 .longname = "Mastech VA18B",
516 .cleanup = hw_cleanup,
517 .dev_open = hw_dev_open,
518 .dev_close = hw_dev_close,
519 .dev_info_get = hw_dev_info_get,
520 .dev_status_get = hw_dev_status_get,
521 .hwcap_get_all = hw_hwcap_get_all,
522 .dev_config_set = hw_dev_config_set,
523 .dev_acquisition_start = hw_dev_acquisition_start,
524 .dev_acquisition_stop = hw_dev_acquisition_stop,