]> sigrok.org Git - libsigrok.git/commitdiff
conrad-digi-35-cpu: Implemented driver.
authorMatthias Heidbrink <redacted>
Sat, 8 Feb 2014 13:26:26 +0000 (14:26 +0100)
committerUwe Hermann <redacted>
Wed, 12 Feb 2014 14:10:07 +0000 (15:10 +0100)
hardware/conrad-digi-35-cpu/api.c
hardware/conrad-digi-35-cpu/protocol.c
hardware/conrad-digi-35-cpu/protocol.h

index 8407b3a058ab3060421a86ac314b894b51927db7..5aac3cff943d9a0caef5fd786e39eaa745541ba8 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+/** @file
+ *  <em>Conrad DIGI 35 CPU</em> power supply driver
+ *  @internal
+ */
+
 #include "protocol.h"
 
+static const int32_t hwopts[] = {
+       SR_CONF_CONN,
+       SR_CONF_SERIALCOMM,
+};
+
+static const int32_t hwcaps[] = {
+       SR_CONF_POWER_SUPPLY,
+       SR_CONF_OUTPUT_VOLTAGE,
+       SR_CONF_OUTPUT_CURRENT,
+       /* There is no SR_CONF_OUTPUT_ENABLED; cannot know or set status remotely. */
+       SR_CONF_OVER_CURRENT_PROTECTION,
+};
+
+
+#define SERIALCOMM "9600/8n1"
+
+
 SR_PRIV struct sr_dev_driver conrad_digi_35_cpu_driver_info;
 static struct sr_dev_driver *di = &conrad_digi_35_cpu_driver_info;
 
@@ -29,17 +51,62 @@ static int init(struct sr_context *sr_ctx)
 
 static GSList *scan(GSList *options)
 {
+       struct sr_dev_inst *sdi;
        struct drv_context *drvc;
-       GSList *devices;
-
-       (void)options;
+       struct sr_config *src;
+       struct sr_probe *probe;
+       struct sr_serial_dev_inst *serial;
+       GSList *l, *devices;
+       const char *conn, *serialcomm;
 
        devices = NULL;
        drvc = di->priv;
        drvc->instances = NULL;
+       conn = serialcomm = NULL;
+
+       for (l = options; l; l = l->next) {
+               src = l->data;
+               switch (src->key) {
+               case SR_CONF_CONN:
+                       conn = g_variant_get_string(src->data, NULL);
+                       break;
+               case SR_CONF_SERIALCOMM:
+                       serialcomm = g_variant_get_string(src->data, NULL);
+                       break;
+               }
+       }
+       if (!conn)
+               return NULL;
+       if (!serialcomm)
+               serialcomm = SERIALCOMM;
+
+       /* We cannot scan for this device because it is write-only.
+        * So just check that the port parameters are valid and assume that
+        * the device is there. */
+
+       if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
+               return NULL;
+
+       if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
+               return NULL;
 
-       /* TODO: scan for devices, either based on a SR_CONF_CONN option
-        * or on a USB scan. */
+       serial_flush(serial);
+       serial_close(serial);
+
+       sr_spew("Conrad DIGI 35 CPU assumed at %s", conn);
+
+       if (!(sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, "Conrad", "DIGI 35 CPU", "")))
+               return NULL;
+
+       sdi->conn = serial;
+       sdi->priv = NULL;
+       sdi->driver = di;
+       if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "CH1")))
+               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;
 }
@@ -54,62 +121,19 @@ static int dev_clear(void)
        return std_dev_clear(di, NULL);
 }
 
-static int dev_open(struct sr_dev_inst *sdi)
-{
-       (void)sdi;
-
-       /* TODO: get handle from sdi->conn and open it. */
-
-       sdi->status = SR_ST_ACTIVE;
-
-       return SR_OK;
-}
-
-static int dev_close(struct sr_dev_inst *sdi)
-{
-       (void)sdi;
-
-       /* TODO: get handle from sdi->conn and close it. */
-
-       sdi->status = SR_ST_INACTIVE;
-
-       return SR_OK;
-}
-
 static int cleanup(void)
 {
        dev_clear();
 
-       /* TODO: free other driver resources, if any. */
-
        return SR_OK;
 }
 
-static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
-               const struct sr_probe_group *probe_group)
-{
-       int ret;
-
-       (void)sdi;
-       (void)data;
-       (void)probe_group;
-
-       ret = SR_OK;
-       switch (key) {
-       /* TODO */
-       default:
-               return SR_ERR_NA;
-       }
-
-       return ret;
-}
-
 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
                const struct sr_probe_group *probe_group)
 {
        int ret;
+       double dblval;
 
-       (void)data;
        (void)probe_group;
 
        if (sdi->status != SR_ST_ACTIVE)
@@ -117,7 +141,29 @@ static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
 
        ret = SR_OK;
        switch (key) {
-       /* TODO */
+       case SR_CONF_OUTPUT_VOLTAGE:
+               dblval = g_variant_get_double(data);
+               if ((dblval < 0.0) || (dblval > 35.0)) {
+                       sr_err("Voltage out of range (0 - 35.0)!");
+                       return SR_ERR_ARG;
+               }
+               ret = send_msg1(sdi, 'V', (int) (dblval * 10 + 0.5));
+               break;
+       case SR_CONF_OUTPUT_CURRENT:
+               dblval = g_variant_get_double(data);
+               if ((dblval < 0.01) || (dblval > 2.55)) {
+                       sr_err("Current out of range (0 - 2.55)!");
+                       return SR_ERR_ARG;
+               }
+               ret = send_msg1(sdi, 'C', (int) (dblval * 100 + 0.5));
+               break;
+       /* No SR_CONF_OUTPUT_ENABLED :-( . */
+       case SR_CONF_OVER_CURRENT_PROTECTION:
+               if (g_variant_get_boolean(data))
+                       ret = send_msg1(sdi, 'V', 900);
+               else /* Constant current mode */
+                       ret = send_msg1(sdi, 'V', 901);
+               break;
        default:
                ret = SR_ERR_NA;
        }
@@ -131,12 +177,18 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
        int ret;
 
        (void)sdi;
-       (void)data;
        (void)probe_group;
 
        ret = SR_OK;
        switch (key) {
-       /* TODO */
+       case SR_CONF_SCAN_OPTIONS:
+               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
+                               hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
+               break;
+       case SR_CONF_DEVICE_OPTIONS:
+               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
+                               hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
+               break;
        default:
                return SR_ERR_NA;
        }
@@ -144,30 +196,24 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
        return ret;
 }
 
-static int dev_acquisition_start(const struct sr_dev_inst *sdi,
+static int dev_acquisition_start_dummy(const struct sr_dev_inst *sdi,
                                    void *cb_data)
 {
-       (void)sdi;
        (void)cb_data;
 
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
-       /* TODO: configure hardware, reset acquisition state, set up
-        * callbacks and send header packet. */
-
        return SR_OK;
 }
 
-static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
+static int dev_acquisition_stop_dummy(struct sr_dev_inst *sdi, void *cb_data)
 {
        (void)cb_data;
 
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
-       /* TODO: stop acquisition. */
-
        return SR_OK;
 }
 
@@ -180,12 +226,12 @@ SR_PRIV struct sr_dev_driver conrad_digi_35_cpu_driver_info = {
        .scan = scan,
        .dev_list = dev_list,
        .dev_clear = dev_clear,
-       .config_get = config_get,
+       .config_get = NULL,
        .config_set = config_set,
        .config_list = config_list,
-       .dev_open = dev_open,
-       .dev_close = dev_close,
-       .dev_acquisition_start = dev_acquisition_start,
-       .dev_acquisition_stop = dev_acquisition_stop,
+       .dev_open = std_serial_dev_open,
+       .dev_close = std_serial_dev_close,
+       .dev_acquisition_start = dev_acquisition_start_dummy,
+       .dev_acquisition_stop = dev_acquisition_stop_dummy,
        .priv = NULL,
 };
index ce8668804fa880ab71ed2094adcb68ede3f21dbc..0937bf6d9081cb374197bc5504ea2771a7d2c9ee 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+/** @file
+ *  <em>Conrad DIGI 35 CPU</em> power supply driver
+ *  @internal
+ */
+
 #include "protocol.h"
 
-SR_PRIV int conrad_digi_35_cpu_receive_data(int fd, int revents, void *cb_data)
+#include <errno.h>
+#include <string.h>
+
+/** Send command with parameter.
+ *
+ *  @param[in] cmd Command
+ *  @param[in] param Parameter (0..999, depending on command).
+ *
+ *  @retval SR_OK Success
+ *  @retval SR_ERR_ARG Invalid argument.
+ *  @retval SR_ERR Error.
+ */
+SR_PRIV int send_msg1(const struct sr_dev_inst *sdi, char cmd, int param)
 {
-       const struct sr_dev_inst *sdi;
-       struct dev_context *devc;
+       struct sr_serial_dev_inst *serial;
+       char buf[5];
 
-       (void)fd;
+       if (!sdi || !(serial = sdi->conn))
+               return SR_ERR_ARG;
 
-       if (!(sdi = cb_data))
-               return TRUE;
+       snprintf(buf, sizeof(buf), "%c%03d", cmd, param);
+       buf[4] = '\r';
 
-       if (!(devc = sdi->priv))
-               return TRUE;
+       sr_spew("send_msg1(): %c%c%c%c\\r", buf[0], buf[1], buf[2], buf[3]);
 
-       if (revents == G_IO_IN) {
-               /* TODO */
+       if (serial_write(serial, buf, sizeof(buf)) == -1) {
+               sr_err("Write error for cmd=%c: %d %s", cmd, errno, strerror(errno));
+               return SR_ERR;
        }
+       g_usleep(50000); /* Wait 50 ms to ensure that the device does not swallow following commands. */
 
-       return TRUE;
+       return SR_OK;
 }
index c363d9b7f29aa0bcbf879254298c04a99558bd1b..301a80262cd48fd6be287be106ca9232eb566a6d 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+/** @file
+ *  <em>Conrad DIGI 35 CPU</em> power supply driver
+ *  @internal
+ */
+
 #ifndef LIBSIGROK_HARDWARE_CONRAD_DIGI_35_CPU_PROTOCOL_H
 #define LIBSIGROK_HARDWARE_CONRAD_DIGI_35_CPU_PROTOCOL_H
 
 #include <stdint.h>
 #include <glib.h>
+
 #include "libsigrok.h"
 #include "libsigrok-internal.h"
 
 /* Message logging helpers with subsystem-specific prefix string. */
 #define LOG_PREFIX "conrad-digi-35-cpu"
 
-/** Private, per-device-instance driver context. */
-struct dev_context {
-       /* Model-specific information */
-
-       /* Acquisition settings */
-
-       /* Operational state */
-
-       /* Temporary state across callbacks */
-
-};
-
-SR_PRIV int conrad_digi_35_cpu_receive_data(int fd, int revents, void *cb_data);
+SR_PRIV int send_msg1(const struct sr_dev_inst *sdi, char cmd, int param);
 
 #endif