From: Uwe Hermann Date: Fri, 9 Nov 2012 02:40:53 +0000 (+0100) Subject: Add initial Tondaj SL-814 protocol handling. X-Git-Tag: dsupstream~575 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=c073af80f89d8bce8675068d084c96e0cf37bb4f;p=libsigrok.git Add initial Tondaj SL-814 protocol handling. --- diff --git a/hardware/tondaj-sl-814/api.c b/hardware/tondaj-sl-814/api.c index 8e79a81b..2247e75a 100644 --- a/hardware/tondaj-sl-814/api.c +++ b/hardware/tondaj-sl-814/api.c @@ -18,11 +18,30 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +#include #include #include "libsigrok.h" #include "libsigrok-internal.h" #include "protocol.h" +static const int hwopts[] = { + SR_HWOPT_CONN, + SR_HWOPT_SERIALCOMM, + 0, +}; + +static const int hwcaps[] = { + SR_HWCAP_SOUNDLEVELMETER, + SR_HWCAP_LIMIT_SAMPLES, + SR_HWCAP_CONTINUOUS, + 0, +}; + +static const char *probe_names[] = { + "P1", + NULL, +}; + SR_PRIV struct sr_dev_driver tondaj_sl_814_driver_info; static struct sr_dev_driver *di = &tondaj_sl_814_driver_info; @@ -42,9 +61,7 @@ static int clear_instances(void) continue; if (!(devc = sdi->priv)) continue; - - /* TODO */ - + sr_serial_dev_inst_free(devc->serial); sr_dev_inst_free(sdi); } @@ -63,8 +80,6 @@ static int hw_init(void) return SR_ERR_MALLOC; } - /* TODO */ - di->priv = drvc; return SR_OK; @@ -73,15 +88,69 @@ static int hw_init(void) static GSList *hw_scan(GSList *options) { struct drv_context *drvc; - GSList *devices; - - (void)options; + struct dev_context *devc; + struct sr_dev_inst *sdi; + struct sr_hwopt *opt; + struct sr_probe *probe; + GSList *devices, *l; + const char *conn, *serialcomm; devices = NULL; drvc = di->priv; drvc->instances = NULL; - /* TODO */ + conn = serialcomm = NULL; + for (l = options; l; l = l->next) { + if (!(opt = l->data)) { + sr_err("Invalid option data, skipping."); + continue; + } + switch (opt->hwopt) { + case SR_HWOPT_CONN: + conn = opt->value; + break; + case SR_HWOPT_SERIALCOMM: + serialcomm = opt->value; + break; + default: + sr_err("Unknown option %d, skipping.", opt->hwopt); + break; + } + } + if (!conn) { + sr_dbg("Couldn't determine connection options."); + return NULL; + } + + if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Tondaj", + "SL-814", NULL))) { + sr_err("Failed to create device instance."); + return NULL; + } + + if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) { + sr_err("Device context malloc failed."); + return NULL; + } + + if (!serialcomm) + serialcomm = "9600/8e1"; + + if (!(devc->serial = sr_serial_dev_inst_new(conn, -1))) { + sr_err("Failed to create serial device instance."); + return NULL; + } + devc->serialcomm = g_strdup(serialcomm); + sdi->priv = devc; + sdi->driver = di; + probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, probe_names[0]); + if (!probe) { + sr_err("Failed to create probe."); + return NULL; + } + sdi->probes = g_slist_append(sdi->probes, probe); + drvc->instances = g_slist_append(drvc->instances, sdi); + devices = g_slist_append(devices, sdi); return devices; } @@ -97,14 +166,55 @@ static GSList *hw_dev_list(void) static int hw_dev_open(struct sr_dev_inst *sdi) { - /* TODO */ + int ret; + struct dev_context *devc; + + devc = sdi->priv; + + sr_dbg("Opening '%s' with '%s'.", devc->serial->port, devc->serialcomm); + + ret = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK); + if (ret < 0) { + sr_err("Unable to open serial port: %d.", ret); + return SR_ERR; + } + devc->serial->fd = ret; + + ret = serial_set_paramstr(devc->serial->fd, devc->serialcomm); + if (ret < 0) { + sr_err("Unable to set serial parameters: %d.", ret); + return SR_ERR; + } + + sdi->status = SR_ST_ACTIVE; return SR_OK; } static int hw_dev_close(struct sr_dev_inst *sdi) { - /* TODO */ + int ret; + struct dev_context *devc; + + devc = sdi->priv; + + if (!devc->serial) { + sr_err("Invalid serial device instance, cannot close."); + return SR_ERR_BUG; + } + + if (devc->serial->fd == -1) { + sr_dbg("Serial device instance FD was -1, no need to close."); + return SR_OK; + } + + if ((ret = serial_close(devc->serial->fd)) < 0) { + sr_err("Error closing serial port: %d.", ret); + return SR_ERR; + } + + devc->serial->fd = -1; + sdi->status = SR_ST_INACTIVE; return SR_OK; } @@ -113,16 +223,27 @@ static int hw_cleanup(void) { clear_instances(); - /* TODO */ - return SR_OK; } static int hw_info_get(int info_id, const void **data, const struct sr_dev_inst *sdi) { + (void)sdi; + switch (info_id) { - /* TODO */ + case SR_DI_HWOPTS: + *data = hwopts; + break; + case SR_DI_HWCAPS: + *data = hwcaps; + break; + case SR_DI_NUM_PROBES: + *data = GINT_TO_POINTER(1); + break; + case SR_DI_PROBE_NAMES: + *data = probe_names; + break; default: sr_err("Unknown info_id: %d.", info_id); return SR_ERR_ARG; @@ -134,45 +255,93 @@ static int hw_info_get(int info_id, const void **data, static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap, const void *value) { - int ret; + struct dev_context *devc; if (sdi->status != SR_ST_ACTIVE) { sr_err("Device inactive, can't set config options."); return SR_ERR; } - ret = SR_OK; + devc = sdi->priv; + switch (hwcap) { - /* TODO */ + case SR_HWCAP_LIMIT_SAMPLES: + devc->limit_samples = *(const uint64_t *)value; + sr_dbg("Setting sample limit to %" PRIu64 ".", + devc->limit_samples); + break; default: sr_err("Unknown hardware capability: %d.", hwcap); - ret = SR_ERR_ARG; + return SR_ERR_ARG; } - return ret; + return SR_OK; } static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) { - /* TODO */ + struct sr_datafeed_packet packet; + struct sr_datafeed_header header; + struct sr_datafeed_meta_analog meta; + struct dev_context *devc; + + devc = sdi->priv; + devc->cb_data = cb_data; + + /* Send header packet to the session bus. */ + sr_dbg("Sending SR_DF_HEADER."); + packet.type = SR_DF_HEADER; + packet.payload = (uint8_t *)&header; + header.feed_version = 1; + gettimeofday(&header.starttime, NULL); + sr_session_send(devc->cb_data, &packet); + + /* Send metadata about the SR_DF_ANALOG packets to come. */ + sr_dbg("Sending SR_DF_META_ANALOG."); + packet.type = SR_DF_META_ANALOG; + packet.payload = &meta; + meta.num_probes = 1; + sr_session_send(devc->cb_data, &packet); + + /* Poll every 500ms, or whenever some data comes in. */ + sr_source_add(devc->serial->fd, G_IO_IN, 500, + tondaj_sl_814_receive_data, (void *)sdi); return SR_OK; } -static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi, - void *cb_data) +static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) { - (void)cb_data; + int ret, final_ret; + struct sr_datafeed_packet packet; + struct dev_context *devc; + + final_ret = SR_OK; if (sdi->status != SR_ST_ACTIVE) { sr_err("Device inactive, can't stop acquisition."); return SR_ERR; } - /* TODO */ + devc = sdi->priv; - return SR_OK; + if ((ret = sr_source_remove(devc->serial->fd)) < 0) { + sr_err("Error removing source: %d.", ret); + final_ret = SR_ERR; + } + + if ((ret = hw_dev_close(sdi)) < 0) { + sr_err("Error closing device: %d.", ret); + final_ret = SR_ERR; + } + + /* Send end packet to the session bus. */ + sr_dbg("Sending SR_DF_END."); + packet.type = SR_DF_END; + sr_session_send(cb_data, &packet); + + return final_ret; } SR_PRIV struct sr_dev_driver tondaj_sl_814_driver_info = { diff --git a/hardware/tondaj-sl-814/protocol.c b/hardware/tondaj-sl-814/protocol.c index ef2a7b94..c164289c 100644 --- a/hardware/tondaj-sl-814/protocol.c +++ b/hardware/tondaj-sl-814/protocol.c @@ -18,25 +18,186 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#include +#include #include #include "libsigrok.h" #include "libsigrok-internal.h" #include "protocol.h" -SR_PRIV int tondaj_sl_814_receive_data(int fd, int revents, void *cb_data) +/* States */ +enum { + SEND_INIT, + GET_INIT_REPLY, + SEND_PACKET_REQUEST, + GET_PACKET, +}; + +static void parse_packet(const uint8_t *buf, float *floatval, + struct sr_datafeed_analog *analog) +{ + gboolean is_a, is_fast; + uint16_t intval; + uint8_t level = 0, level_bits; + + /* Byte 0 [7:7]: 0 = A, 1 = C */ + is_a = ((buf[0] & (1 << 7)) == 0); + + /* Byte 0 [6:6]: Unknown/unused? */ + + /* Byte 0 [5:4]: Level (00 = 40, 01 = 60, 10 = 80, 11 = 100) */ + level_bits = (buf[0] >> 4) & 0x03; + if (level_bits == 0) + level = 40; + else if (level_bits == 1) + level = 60; + else if (level_bits == 2) + level = 80; + else if (level_bits == 3) + level = 100; + + /* Byte 0 [3:3]: 0 = fast, 1 = slow */ + is_fast = ((buf[0] & (1 << 3)) == 0); + + /* Byte 0 [2:0]: value[10..8] */ + /* Byte 1 [7:0]: value[7..0] */ + intval = (buf[0] & 0x7) << 8; + intval |= buf[1]; + + *floatval = (float)intval; + + /* The value on the display always has one digit after the comma. */ + *floatval /= 10; + + analog->mq = SR_MQ_SOUND_PRESSURE_LEVEL; + analog->unit = SR_UNIT_DECIBEL_SPL; + + if (is_a) + analog->mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_A; + else + analog->mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_C; + + if (is_fast) + analog->mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_F; + else + analog->mqflags |= SR_MQFLAG_SPL_TIME_WEIGHT_S; + + /* TODO: How to handle level? */ + (void)level; +} + +static void decode_packet(struct dev_context *devc) { - const struct sr_dev_inst *sdi; + struct sr_datafeed_packet packet; + struct sr_datafeed_analog analog; + float floatval; + + memset(&analog, 0, sizeof(struct sr_datafeed_analog)); + + parse_packet(devc->buf, &floatval, &analog); + + /* Send a sample packet with one analog value. */ + analog.num_samples = 1; + analog.data = &floatval; + packet.type = SR_DF_ANALOG; + packet.payload = &analog; + sr_session_send(devc->cb_data, &packet); + + devc->num_samples++; +} + +int tondaj_sl_814_receive_data(int fd, int revents, void *cb_data) +{ + struct sr_dev_inst *sdi; struct dev_context *devc; + uint8_t buf[3]; + int ret; + + (void)revents; + + sdi = cb_data; + devc = sdi->priv; - if (!(sdi = cb_data)) - return TRUE; + /* TODO: Parts of this code need to be improved later. */ - if (!(devc = sdi->priv)) - return TRUE; + /* State machine. */ + if (devc->state == SEND_INIT) { + /* On the first run, send the "init" command. */ + buf[0] = 0x10; + buf[1] = 0x04; + buf[2] = 0x0d; + sr_spew("Sending init command: %02x %02x %02x.", + buf[0], buf[1], buf[2]); + if ((ret = serial_write(fd, buf, 3)) < 0) { + sr_err("Error sending init command: %d.", ret); + return FALSE; + } + devc->state = GET_INIT_REPLY; + } else if (devc->state == GET_INIT_REPLY) { + /* If we just sent the "init" command, get its reply. */ + if ((ret = serial_read(fd, buf, 2)) < 0) { + sr_err("Error reading init reply: %d.", ret); + return FALSE; + } + sr_spew("Received init reply: %02x %02x.", buf[0], buf[1]); + /* Expected reply: 0x05 0x0d */ + if (buf[0] != 0x05 || buf[1] != 0x0d) { + sr_err("Received incorrect init reply, retrying."); + devc->state = SEND_INIT; + return TRUE; + } + devc->state = SEND_PACKET_REQUEST; + } else if (devc->state == SEND_PACKET_REQUEST) { + /* Request a packet (send 0x30 ZZ 0x0d). */ + buf[0] = 0x30; + buf[1] = 0x00; /* ZZ */ + buf[2] = 0x0d; + sr_spew("Sending data request command: %02x %02x %02x.", + buf[0], buf[1], buf[2]); + if ((ret = serial_write(fd, buf, 3)) < 0) { + sr_err("Error sending request command: %d.", ret); + return FALSE; + } + devc->buflen = 0; + devc->state = GET_PACKET; + } else if (devc->state == GET_PACKET) { + /* Read a packet from the device. */ + ret = serial_read(fd, devc->buf + devc->buflen, + 4 - devc->buflen); + if (ret < 0) { + sr_err("Error reading packet: %d.", ret); + return TRUE; + } + + devc->buflen += ret; + + /* Didn't receive all 4 bytes, yet. */ + if (devc->buflen != 4) + return TRUE; + + sr_spew("Received packet: %02x %02x %02x %02x.", devc->buf[0], + devc->buf[1], devc->buf[2], devc->buf[3]); + + /* Expected reply: AA BB ZZ+1 0x0d */ + if (devc->buf[2] != 0x01 || devc->buf[3] != 0x0d) { + sr_err("Received incorrect request reply, retrying."); + devc->state = SEND_PACKET_REQUEST; + return TRUE; + } + + decode_packet(devc); + + devc->state = SEND_PACKET_REQUEST; + } else { + sr_err("Invalid state: %d.", devc->state); + return FALSE; + } - if (revents == G_IO_IN) { - /* TODO */ + /* Stop acquisition if we acquired enough samples. */ + if (devc->limit_samples > 0) { + if (devc->num_samples >= devc->limit_samples) { + sr_info("Requested number of samples reached."); + sdi->driver->dev_acquisition_stop(sdi, cb_data); + } } return TRUE; diff --git a/hardware/tondaj-sl-814/protocol.h b/hardware/tondaj-sl-814/protocol.h index f7877db7..69b7c68f 100644 --- a/hardware/tondaj-sl-814/protocol.h +++ b/hardware/tondaj-sl-814/protocol.h @@ -47,6 +47,15 @@ struct dev_context { /** The current number of already received samples. */ uint64_t num_samples; + + struct sr_serial_dev_inst *serial; + + char *serialcomm; + + int state; + + uint8_t buf[4]; + uint8_t buflen; }; SR_PRIV int tondaj_sl_814_receive_data(int fd, int revents, void *cb_data);