]> sigrok.org Git - libsigrok.git/commitdiff
colead-slm: SL-5868P support
authorBert Vermeulen <redacted>
Fri, 2 Nov 2012 14:21:43 +0000 (15:21 +0100)
committerBert Vermeulen <redacted>
Fri, 2 Nov 2012 14:24:30 +0000 (15:24 +0100)
hardware/colead-slm/api.c
hardware/colead-slm/protocol.c
hardware/colead-slm/protocol.h

index b85f831a4ffa3af4acabac24eedd714a0a527417..2330ad66f805a29543d95e1ec53c6634039b4379 100644 (file)
 #include "libsigrok.h"
 #include "libsigrok-internal.h"
 #include "protocol.h"
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <string.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_LIMIT_MSEC,
+       SR_HWCAP_CONTINUOUS,
+       0,
+};
+
+static const char *probe_names[] = {
+       "P1",
+       NULL,
+};
 
 SR_PRIV struct sr_dev_driver colead_slm_driver_info;
 static struct sr_dev_driver *di = &colead_slm_driver_info;
@@ -39,12 +63,9 @@ static int clear_instances(void)
                        continue;
                if (!(devc = sdi->priv))
                        continue;
-
-               /* TODO */
-
+               sr_serial_dev_inst_free(devc->serial);
                sr_dev_inst_free(sdi);
        }
-
        g_slist_free(drvc->instances);
        drvc->instances = NULL;
 
@@ -60,8 +81,6 @@ static int hw_init(void)
                return SR_ERR;
        }
 
-       /* TODO */
-
        di->priv = drvc;
 
        return SR_OK;
@@ -70,15 +89,54 @@ 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) {
+               opt = l->data;
+               switch (opt->hwopt) {
+               case SR_HWOPT_CONN:
+                       conn = opt->value;
+                       break;
+               case SR_HWOPT_SERIALCOMM:
+                       serialcomm = opt->value;
+                       break;
+               }
+       }
+       if (!conn)
+               return NULL;
+
+       if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Colead",
+                       "SL-5868P", NULL)))
+               return NULL;
+
+       if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
+               sr_dbg("failed to malloc devc");
+               return NULL;
+       }
+
+       if (!serialcomm)
+               /* The Colead SL-5868P uses this. */
+               serialcomm = "2400/8n1";
+
+       devc->serial = sr_serial_dev_inst_new(conn, -1);
+       devc->serialcomm = g_strdup(serialcomm);
+       sdi->priv = devc;
+       sdi->driver = di;
+       if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "P1")))
+               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;
 }
@@ -94,14 +152,43 @@ static GSList *hw_dev_list(void)
 
 static int hw_dev_open(struct sr_dev_inst *sdi)
 {
-       /* TODO */
+       struct dev_context *devc;
+
+       if (!(devc = sdi->priv)) {
+               sr_err("sdi->priv was NULL.");
+               return SR_ERR_BUG;
+       }
+
+       sr_dbg("opening %s with %s", devc->serial->port, devc->serialcomm);
+       devc->serial->fd = serial_open(devc->serial->port, O_RDWR | O_NONBLOCK);
+       if (devc->serial->fd == -1) {
+               sr_err("Couldn't open serial port '%s'.",
+                      devc->serial->port);
+               return SR_ERR;
+       }
+       if (serial_set_paramstr(devc->serial->fd, devc->serialcomm) != SR_OK) {
+               sr_err("Unable to set serial parameters.");
+               return SR_ERR;
+       }
+       sdi->status = SR_ST_ACTIVE;
 
        return SR_OK;
 }
 
 static int hw_dev_close(struct sr_dev_inst *sdi)
 {
-       /* TODO */
+       struct dev_context *devc;
+
+       if (!(devc = sdi->priv)) {
+               sr_err("sdi->priv was NULL.");
+               return SR_ERR_BUG;
+       }
+
+       if (devc->serial && devc->serial->fd != -1) {
+               serial_close(devc->serial->fd);
+               devc->serial->fd = -1;
+               sdi->status = SR_ST_INACTIVE;
+       }
 
        return SR_OK;
 }
@@ -110,18 +197,29 @@ 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;
        }
 
@@ -131,28 +229,76 @@ 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.");
+       if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR;
+
+       if (!(devc = sdi->priv)) {
+               sr_err("sdi->priv was NULL.");
+               return SR_ERR_BUG;
        }
 
-       ret = SR_OK;
        switch (hwcap) {
-       /* TODO */
+       case SR_HWCAP_LIMIT_MSEC:
+               /* TODO: not yet implemented */
+               if (*(const uint64_t *)value == 0) {
+                       sr_err("LIMIT_MSEC can't be 0.");
+                       return SR_ERR;
+               }
+               devc->limit_msec = *(const uint64_t *)value;
+               sr_dbg("Setting time limit to %" PRIu64 "ms.",
+                      devc->limit_msec);
+               break;
+       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;
+               sr_err("Unknown capability: %d.", hwcap);
+               return SR_ERR;
+               break;
        }
 
-       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;
+
+       if (!(devc = sdi->priv)) {
+               sr_err("sdi->priv was NULL.");
+               return SR_ERR_BUG;
+       }
+
+       sr_dbg("Starting acquisition.");
+
+       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 150ms, or whenever some data comes in. */
+       sr_source_add(devc->serial->fd, G_IO_IN, 150, colead_slm_receive_data,
+                       (void *)sdi);
 
        return SR_OK;
 }
@@ -160,14 +306,26 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
 static int hw_dev_acquisition_stop(const struct sr_dev_inst *sdi,
                                   void *cb_data)
 {
-       (void)cb_data;
+       struct sr_datafeed_packet packet;
+       struct dev_context *devc;
 
-       if (sdi->status != SR_ST_ACTIVE) {
-               sr_err("Device inactive, can't stop acquisition.");
+       if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR;
+
+       if (!(devc = sdi->priv)) {
+               sr_err("sdi->priv was NULL.");
+               return SR_ERR_BUG;
        }
 
-       /* TODO */
+       sr_dbg("Stopping acquisition.");
+
+       sr_source_remove(devc->serial->fd);
+       hw_dev_close((struct sr_dev_inst *)sdi);
+
+       /* 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 SR_OK;
 }
index b749646e9de87c414056f622d9eda637ff1ae658..85ecc108afd07d7bf4f850f83e1a1124a4b0c9d6 100644 (file)
 #include "libsigrok.h"
 #include "libsigrok-internal.h"
 #include "protocol.h"
+#include <errno.h>
+#include <string.h>
+
+
+static void process_packet(const struct sr_dev_inst *sdi)
+{
+       struct dev_context *devc;
+       struct sr_datafeed_packet packet;
+       struct sr_datafeed_analog analog;
+       GString *dbg;
+       float fvalue;
+       int checksum, mode, i;
+
+       devc = sdi->priv;
+       if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
+               dbg = g_string_sized_new(128);
+               g_string_printf(dbg, "received packet:");
+               for (i = 0; i < 10; i++)
+                       g_string_append_printf(dbg, " %.2x", (devc->buf)[i]);
+               sr_spew("%s", dbg->str);
+               g_string_free(dbg, TRUE);
+       }
+
+       if (devc->buf[0] != 0x08 || devc->buf[1] != 0x04) {
+               sr_dbg("invalid packet header.");
+               return;
+       }
+
+       if (devc->buf[8] != 0x01) {
+               sr_dbg("invalid measurement.");
+               return;
+       }
+
+       checksum = 0;
+       for (i = 0; i < 9; i++)
+               checksum += devc->buf[i];
+       if ((checksum & 0xff) != devc->buf[9]) {
+               sr_dbg("invalid packet checksum.");
+               return;
+       }
+
+       fvalue = 0.0;
+       for (i = 3; i < 8; i++) {
+               if (devc->buf[i] > 0x09)
+                       continue;
+               fvalue *= 10;
+               fvalue += devc->buf[i];
+       }
+       fvalue /= 10;
+
+       memset(&analog, 0, sizeof(struct sr_datafeed_analog));
+       analog.mq = SR_MQ_SOUND_PRESSURE_LEVEL;
+       analog.unit = SR_UNIT_DECIBEL_SPL;
+       analog.num_samples = 1;
+       analog.data = &fvalue;
+
+       /* High nibble should only have 0x01 or 0x02. */
+       mode = (devc->buf[2] >> 4) & 0x0f;
+       if (mode == 0x02)
+               analog.mqflags |= SR_MQFLAG_HOLD;
+       else if (mode != 0x01) {
+               sr_dbg("unknown measurement mode 0x%.2x", mode);
+               return;
+       }
+
+       /* Low nibble has 14 combinations of direct/long-term average,
+        * time scale of that average, frequency weighting, and time
+        * weighting. */
+       mode = devc->buf[2] & 0x0f;
+       switch (mode) {
+               case 0x0:
+                       analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_A \
+                                       | SR_MQFLAG_SPL_TIME_WEIGHT_F;
+                       break;
+               case 0x1:
+                       analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_A \
+                                       | SR_MQFLAG_SPL_TIME_WEIGHT_S;
+                       break;
+               case 0x2:
+                       analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_C \
+                                       | SR_MQFLAG_SPL_TIME_WEIGHT_F;
+                       break;
+               case 0x3:
+                       analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_C \
+                                       | SR_MQFLAG_SPL_TIME_WEIGHT_S;
+                       break;
+               case 0x4:
+                       analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_FLAT \
+                                       | SR_MQFLAG_SPL_TIME_WEIGHT_F;
+                       break;
+               case 0x5:
+                       analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_FLAT \
+                                       | SR_MQFLAG_SPL_TIME_WEIGHT_S;
+                       break;
+               case 0x6:
+                       analog.mqflags |= SR_MQFLAG_SPL_PCT_OVER_ALARM \
+                                       | SR_MQFLAG_SPL_FREQ_WEIGHT_A \
+                                       | SR_MQFLAG_SPL_TIME_WEIGHT_F;
+                       break;
+               case 0x7:
+                       analog.mqflags |= SR_MQFLAG_SPL_PCT_OVER_ALARM \
+                                       | SR_MQFLAG_SPL_FREQ_WEIGHT_A \
+                                       | SR_MQFLAG_SPL_TIME_WEIGHT_S;
+                       break;
+               case 0x8:
+                       /* 10-second mean, but we don't have MQ flags to express it. */
+                       analog.mqflags |= SR_MQFLAG_SPL_LAT \
+                                       | SR_MQFLAG_SPL_FREQ_WEIGHT_A \
+                                       | SR_MQFLAG_SPL_TIME_WEIGHT_F;
+                       break;
+               case 0x9:
+                       /* Mean over a time period between 11 seconds and 24 hours.
+                        * Which is so silly that there's no point in expressing
+                        * either this or the previous case.  */
+                       analog.mqflags |= SR_MQFLAG_SPL_LAT \
+                                       | SR_MQFLAG_SPL_FREQ_WEIGHT_A \
+                                       | SR_MQFLAG_SPL_TIME_WEIGHT_F;
+                       break;
+               case 0xa:
+                       /* 10-second mean. */
+                       analog.mqflags |= SR_MQFLAG_SPL_LAT \
+                                       | SR_MQFLAG_SPL_FREQ_WEIGHT_A \
+                                       | SR_MQFLAG_SPL_TIME_WEIGHT_S;
+                       break;
+               case 0xb:
+                       /* Mean over a time period between 11 seconds and 24 hours. */
+                       analog.mqflags |= SR_MQFLAG_SPL_LAT \
+                                       | SR_MQFLAG_SPL_FREQ_WEIGHT_A \
+                                       | SR_MQFLAG_SPL_TIME_WEIGHT_S;
+                       break;
+               case 0xc:
+                       /* Internal calibration on 1kHz sine at 94dB, not useful
+                        * to anything but the device. */
+                       analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_FLAT;
+                       break;
+               case 0xd:
+                       /* Internal calibration on 1kHz sine at 94dB, not useful
+                        * to anything but the device. */
+                       analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_FLAT;
+                       break;
+               default:
+                       sr_dbg("unknown configuration 0x%.2x", mode);
+                       return;
+                       break;
+       }
+
+       packet.type = SR_DF_ANALOG;
+       packet.payload = &analog;
+       sr_session_send(devc->cb_data, &packet);
+
+       devc->num_samples++;
+
+}
 
 SR_PRIV int colead_slm_receive_data(int fd, int revents, void *cb_data)
 {
        const struct sr_dev_inst *sdi;
        struct dev_context *devc;
+       int len;
+       char buf[128];
 
        if (!(sdi = cb_data))
                return TRUE;
@@ -34,9 +189,41 @@ SR_PRIV int colead_slm_receive_data(int fd, int revents, void *cb_data)
        if (!(devc = sdi->priv))
                return TRUE;
 
-       if (revents == G_IO_IN) {
-               /* TODO */
+       if (revents != G_IO_IN)
+               /* Timeout event. */
+               return TRUE;
+
+       if (devc->state == IDLE) {
+               if (serial_read(fd, buf, 128) != 1 || buf[0] != 0x10)
+                       /* Nothing there, or caught the tail end of a previous packet,
+                        * or some garbage. Unless it's a single "data ready" byte,
+                        * we don't want it. */
+                       return TRUE;
+               /* Got 0x10, "measurement ready". */
+               if (serial_write(fd, "\x20", 1) == -1)
+                       sr_err("unable to send command: %s", strerror(errno));
+               else {
+                       devc->state = COMMAND_SENT;
+                       devc->buflen = 0;
+               }
+       } else {
+               len = serial_read(fd, devc->buf + devc->buflen, 10 - devc->buflen);
+               if (len < 1)
+                       return TRUE;
+               devc->buflen += len;
+               if (devc->buflen > 10) {
+                       sr_dbg("buffer overrun");
+                       devc->state = IDLE;
+                       return TRUE;
+               }
+               if (devc->buflen == 10) {
+                       /* If we got here, we're sure the device sent a "data ready"
+                        * notification, we asked for data, and got it. */
+                       process_packet(sdi);
+                       devc->state = IDLE;
+               }
        }
 
        return TRUE;
 }
+
index 5bf2d5ad3c44be65eb74b85366588c9aa64dc339..7963babb451ccd4103bad16665d68d608c107aa0 100644 (file)
 #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
 #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
 
+enum {
+       IDLE,
+       COMMAND_SENT,
+};
+
 /** Private, per-device-instance driver context. */
 struct dev_context {
        /** The current sampling limit (in number of samples). */
@@ -46,6 +51,11 @@ struct dev_context {
 
        /** The current number of already received samples. */
        uint64_t num_samples;
+       struct sr_serial_dev_inst *serial;
+       char *serialcomm;
+       int state;
+       char buf[10];
+       int buflen;
 };
 
 SR_PRIV int colead_slm_receive_data(int fd, int revents, void *cb_data);