]> sigrok.org Git - libsigrok.git/blobdiff - hardware/chronovu-la8/chronovu-la8.c
sr: Fix/document probe names.
[libsigrok.git] / hardware / chronovu-la8 / chronovu-la8.c
index fea43db859435e67bd8bba423075a57eb1afe7c7..60384a1d15affc822d6639e44ac6d581f086902d 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * This file is part of the sigrok project.
  *
- * Copyright (C) 2011 Uwe Hermann <uwe@hermann-uwe.de>
+ * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -42,6 +42,7 @@
 
 static GSList *dev_insts = NULL;
 
+/* Probes are numbered 0-7. */
 static const char *probe_names[NUM_PROBES + 1] = {
        "0",
        "1",
@@ -206,24 +207,17 @@ static uint8_t samplerate_to_divcount(uint64_t samplerate)
 /**
  * Write data of a certain length to the LA8's FTDI device.
  *
- * @param ctx The struct containing private per-device-instance data.
- * @param buf The buffer containing the data to write.
- * @param size The number of bytes to write.
+ * @param ctx The struct containing private per-device-instance data. Must not
+ *            be NULL. ctx->ftdic must not be NULL either.
+ * @param buf The buffer containing the data to write. Must not be NULL.
+ * @param size The number of bytes to write. Must be >= 0.
  * @return The number of bytes written, or a negative value upon errors.
  */
 static int la8_write(struct context *ctx, uint8_t *buf, int size)
 {
        int bytes_written;
 
-       if (!ctx) {
-               sr_err("la8: %s: ctx was NULL", __func__);
-               return SR_ERR_ARG;
-       }
-
-       if (!ctx->ftdic) {
-               sr_err("la8: %s: ctx->ftdic was NULL", __func__);
-               return SR_ERR_ARG;
-       }
+       /* Note: Caller checked that ctx and ctx->ftdic != NULL. */
 
        if (!buf) {
                sr_err("la8: %s: buf was NULL", __func__);
@@ -253,24 +247,18 @@ static int la8_write(struct context *ctx, uint8_t *buf, int size)
 /**
  * Read a certain amount of bytes from the LA8's FTDI device.
  *
- * @param ctx The struct containing private per-device-instance data.
- * @param buf The buffer where the received data will be stored.
- * @param size The number of bytes to read.
+ * @param ctx The struct containing private per-device-instance data. Must not
+ *            be NULL. ctx->ftdic must not be NULL either.
+ * @param buf The buffer where the received data will be stored. Must not
+ *            be NULL.
+ * @param size The number of bytes to read. Must be >= 1.
  * @return The number of bytes read, or a negative value upon errors.
  */
 static int la8_read(struct context *ctx, uint8_t *buf, int size)
 {
        int bytes_read;
 
-       if (!ctx) {
-               sr_err("la8: %s: ctx was NULL", __func__);
-               return SR_ERR_ARG;
-       }
-
-       if (!ctx->ftdic) {
-               sr_err("la8: %s: ctx->ftdic was NULL", __func__);
-               return SR_ERR_ARG;
-       }
+       /* Note: Caller checked that ctx and ctx->ftdic != NULL. */
 
        if (!buf) {
                sr_err("la8: %s: buf was NULL", __func__);
@@ -321,7 +309,7 @@ static int la8_close(struct context *ctx)
  * Close the ChronoVu LA8 USB port and reset the LA8 sequencer logic.
  *
  * @param ctx The struct containing private per-device-instance data.
- * @return SR_OK upon success, SR_ERR upon failure.
+ * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
  */
 static int la8_close_usb_reset_sequencer(struct context *ctx)
 {
@@ -341,12 +329,12 @@ static int la8_close_usb_reset_sequencer(struct context *ctx)
 
        if (ctx->ftdic->usb_dev) {
                /* Reset the LA8 sequencer logic, then wait 100ms. */
-               sr_dbg("la8: resetting sequencer logic");
+               sr_dbg("la8: Resetting sequencer logic.");
                (void) la8_write(ctx, buf, 8); /* Ignore errors. */
                g_usleep(100 * 1000);
 
                /* Purge FTDI buffers, then reset and close the FTDI device. */
-               sr_dbg("la8: purging buffers, resetting+closing FTDI device");
+               sr_dbg("la8: Purging buffers, resetting+closing FTDI device.");
 
                /* Log errors, but ignore them (i.e., don't abort). */
                if ((ret = ftdi_usb_purge_buffers(ctx->ftdic)) < 0)
@@ -360,6 +348,7 @@ static int la8_close_usb_reset_sequencer(struct context *ctx)
                               ret, ftdi_get_error_string(ctx->ftdic));
        }
 
+       /* Close USB device, deinitialize and free the FTDI context. */
        ftdi_free(ctx->ftdic); /* Returns void. */
        ctx->ftdic = NULL;
 
@@ -390,7 +379,7 @@ static int la8_reset(struct context *ctx)
                return SR_ERR_ARG;
        }
 
-       sr_dbg("la8: resetting the device");
+       sr_dbg("la8: Resetting the device.");
 
        /*
         * Purge pending read data from the FTDI hardware FIFO until
@@ -406,7 +395,7 @@ static int la8_reset(struct context *ctx)
        /* Reset the LA8 sequencer logic and close the USB port. */
        (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
 
-       sr_dbg("la8: device reset finished");
+       sr_dbg("la8: Device reset finished.");
 
        return SR_OK;
 }
@@ -418,6 +407,8 @@ static int configure_probes(struct context *ctx, GSList *probes)
        uint8_t probe_bit;
        char *tc;
 
+       /* Note: Caller checked that ctx != NULL. */
+
        ctx->trigger_pattern = 0;
        ctx->trigger_mask = 0; /* Default to "don't care" for all probes. */
 
@@ -462,8 +453,8 @@ static int configure_probes(struct context *ctx, GSList *probes)
                }
        }
 
-       sr_dbg("la8: %s: trigger_mask = 0x%x, trigger_pattern = 0x%x",
-              __func__, ctx->trigger_mask, ctx->trigger_pattern);
+       sr_dbg("la8: trigger_mask = 0x%x, trigger_pattern = 0x%x",
+              ctx->trigger_mask, ctx->trigger_pattern);
 
        return SR_OK;
 }
@@ -517,7 +508,8 @@ static int hw_init(const char *devinfo)
                (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
                goto err_free_ftdic;
        }
-       sr_dbg("la8: found device");
+       sr_dbg("la8: Found LA8 device (%04x:%04x).", USB_VENDOR_ID,
+              USB_PRODUCT_ID);
 
        /* Register the device with libsigrok. */
        sdi = sr_dev_inst_new(0, SR_ST_INITIALIZING,
@@ -531,7 +523,7 @@ static int hw_init(const char *devinfo)
 
        dev_insts = g_slist_append(dev_insts, sdi);
 
-       sr_spew("la8: %s finished successfully", __func__);
+       sr_spew("la8: Device init successful.");
 
        /* Close device. We'll reopen it again when we need it. */
        (void) la8_close(ctx); /* Log, but ignore errors. */
@@ -567,7 +559,8 @@ static int hw_dev_open(int dev_index)
                return SR_ERR; /* TODO: SR_ERR_ARG? */
        }
 
-       sr_dbg("la8: opening device");
+       sr_dbg("la8: Opening LA8 device (%04x:%04x).", USB_VENDOR_ID,
+              USB_PRODUCT_ID);
 
        /* Open the device. */
        if ((ret = ftdi_usb_open_desc(ctx->ftdic, USB_VENDOR_ID,
@@ -577,7 +570,7 @@ static int hw_dev_open(int dev_index)
                (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
                return SR_ERR;
        }
-       sr_dbg("la8: device opened successfully");
+       sr_dbg("la8: Device opened successfully.");
 
        /* Purge RX/TX buffers in the FTDI chip. */
        if ((ret = ftdi_usb_purge_buffers(ctx->ftdic)) < 0) {
@@ -586,7 +579,7 @@ static int hw_dev_open(int dev_index)
                (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
                goto err_dev_open_close_ftdic;
        }
-       sr_dbg("la8: FTDI buffers purged successfully");
+       sr_dbg("la8: FTDI buffers purged successfully.");
 
        /* Enable flow control in the FTDI chip. */
        if ((ret = ftdi_setflowctrl(ctx->ftdic, SIO_RTS_CTS_HS)) < 0) {
@@ -595,7 +588,7 @@ static int hw_dev_open(int dev_index)
                (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
                goto err_dev_open_close_ftdic;
        }
-       sr_dbg("la8: FTDI flow control enabled successfully");
+       sr_dbg("la8: FTDI flow control enabled successfully.");
 
        /* Wait 100ms. */
        g_usleep(100 * 1000);
@@ -613,17 +606,11 @@ static int set_samplerate(struct sr_dev_inst *sdi, uint64_t samplerate)
 {
        struct context *ctx;
 
-       if (!sdi) {
-               sr_err("la8: %s: sdi was NULL", __func__);
-               return SR_ERR_ARG;
-       }
+       /* Note: Caller checked that sdi and sdi->priv != NULL. */
 
-       if (!(ctx = sdi->priv)) {
-               sr_err("la8: %s: sdi->priv was NULL", __func__);
-               return SR_ERR_ARG;
-       }
+       ctx = sdi->priv;
 
-       sr_spew("la8: setting samplerate");
+       sr_spew("la8: Trying to set samplerate to %" PRIu64 "Hz.", samplerate);
 
        fill_supported_samplerates_if_needed();
 
@@ -634,7 +621,7 @@ static int set_samplerate(struct sr_dev_inst *sdi, uint64_t samplerate)
        /* Set the new samplerate. */
        ctx->cur_samplerate = samplerate;
 
-       sr_dbg("la8: samplerate set to %" PRIu64 "Hz", ctx->cur_samplerate);
+       sr_dbg("la8: Samplerate set to %" PRIu64 "Hz.", ctx->cur_samplerate);
 
        return SR_OK;
 }
@@ -654,19 +641,19 @@ static int hw_dev_close(int dev_index)
                return SR_ERR; /* TODO: SR_ERR_ARG? */
        }
 
-       sr_dbg("la8: closing device");
+       sr_dbg("la8: Closing device.");
 
        if (sdi->status == SR_ST_ACTIVE) {
-               sr_dbg("la8: %s: status ACTIVE, closing device", __func__);
+               sr_dbg("la8: Status ACTIVE, closing device.");
                /* TODO: Really ignore errors here, or return SR_ERR? */
                (void) la8_close_usb_reset_sequencer(ctx); /* Ignore errors. */
        } else {
-               sr_spew("la8: %s: status not ACTIVE, nothing to do", __func__);
+               sr_spew("la8: Status not ACTIVE, nothing to do.");
        }
 
        sdi->status = SR_ST_INACTIVE;
 
-       sr_dbg("la8: %s: freeing sample buffers", __func__);
+       sr_dbg("la8: Freeing sample buffer.");
        g_free(ctx->final_buf);
 
        return SR_OK;
@@ -700,8 +687,6 @@ static void *hw_dev_info_get(int dev_index, int dev_info_id)
        struct context *ctx;
        void *info;
 
-       sr_spew("la8: entering %s", __func__);
-
        if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
                sr_err("la8: %s: sdi was NULL", __func__);
                return NULL;
@@ -712,25 +697,37 @@ static void *hw_dev_info_get(int dev_index, int dev_info_id)
                return NULL;
        }
 
+       sr_spew("la8: %s: dev_index %d, dev_info_id %d.", __func__,
+               dev_index, dev_info_id);
+
        switch (dev_info_id) {
        case SR_DI_INST:
                info = sdi;
+               sr_spew("la8: %s: Returning sdi.", __func__);
                break;
        case SR_DI_NUM_PROBES:
                info = GINT_TO_POINTER(NUM_PROBES);
+               sr_spew("la8: %s: Returning number of probes: %d.", __func__,
+                       NUM_PROBES);
                break;
        case SR_DI_PROBE_NAMES:
                info = probe_names;
+               sr_spew("la8: %s: Returning probenames.", __func__);
                break;
        case SR_DI_SAMPLERATES:
                fill_supported_samplerates_if_needed();
                info = &samplerates;
+               sr_spew("la8: %s: Returning samplerates.", __func__);
                break;
        case SR_DI_TRIGGER_TYPES:
                info = (char *)TRIGGER_TYPES;
+               sr_spew("la8: %s: Returning trigger types: %s.", __func__,
+                       TRIGGER_TYPES);
                break;
        case SR_DI_CUR_SAMPLERATE:
                info = &ctx->cur_samplerate;
+               sr_spew("la8: %s: Returning samplerate: %" PRIu64 "Hz.",
+                       __func__, ctx->cur_samplerate);
                break;
        default:
                /* Unknown device info ID, return NULL. */
@@ -751,14 +748,14 @@ static int hw_dev_status_get(int dev_index)
                return SR_ST_NOT_FOUND;
        }
 
-       sr_dbg("la8: %s: returning status %d", __func__, sdi->status);
+       sr_dbg("la8: Returning status: %d.", sdi->status);
 
        return sdi->status;
 }
 
 static int *hw_hwcap_get_all(void)
 {
-       sr_spew("la8: entering %s", __func__);
+       sr_spew("la8: Returning list of device capabilities.");
 
        return hwcaps;
 }
@@ -768,8 +765,6 @@ static int hw_dev_config_set(int dev_index, int hwcap, void *value)
        struct sr_dev_inst *sdi;
        struct context *ctx;
 
-       sr_spew("la8: entering %s", __func__);
-
        if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
                sr_err("la8: %s: sdi was NULL", __func__);
                return SR_ERR; /* TODO: SR_ERR_ARG? */
@@ -780,21 +775,25 @@ static int hw_dev_config_set(int dev_index, int hwcap, void *value)
                return SR_ERR; /* TODO: SR_ERR_ARG? */
        }
 
+       sr_spew("la8: %s: dev_index %d, hwcap %d", __func__, dev_index, hwcap);
+
        switch (hwcap) {
        case SR_HWCAP_SAMPLERATE:
-               if (set_samplerate(sdi, *(uint64_t *)value) == SR_ERR)
+               if (set_samplerate(sdi, *(uint64_t *)value) == SR_ERR) {
+                       sr_err("la8: %s: setting samplerate failed.", __func__);
                        return SR_ERR;
+               }
                sr_dbg("la8: SAMPLERATE = %" PRIu64, ctx->cur_samplerate);
                break;
        case SR_HWCAP_PROBECONFIG:
                if (configure_probes(ctx, (GSList *)value) != SR_OK) {
-                       sr_err("la8: %s: probe config failed", __func__);
+                       sr_err("la8: %s: probe config failed.", __func__);
                        return SR_ERR;
                }
                break;
        case SR_HWCAP_LIMIT_MSEC:
                if (*(uint64_t *)value == 0) {
-                       sr_err("la8: %s: LIMIT_MSEC can't be 0", __func__);
+                       sr_err("la8: %s: LIMIT_MSEC can't be 0.", __func__);
                        return SR_ERR;
                }
                ctx->limit_msec = *(uint64_t *)value;
@@ -802,7 +801,7 @@ static int hw_dev_config_set(int dev_index, int hwcap, void *value)
                break;
        case SR_HWCAP_LIMIT_SAMPLES:
                if (*(uint64_t *)value < MIN_NUM_SAMPLES) {
-                       sr_err("la8: %s: LIMIT_SAMPLES too small", __func__);
+                       sr_err("la8: %s: LIMIT_SAMPLES too small.", __func__);
                        return SR_ERR;
                }
                ctx->limit_samples = *(uint64_t *)value;
@@ -810,7 +809,7 @@ static int hw_dev_config_set(int dev_index, int hwcap, void *value)
                break;
        default:
                /* Unknown capability, return SR_ERR. */
-               sr_err("la8: %s: Unknown capability", __func__);
+               sr_err("la8: %s: Unknown capability.", __func__);
                return SR_ERR;
                break;
        }
@@ -821,7 +820,8 @@ static int hw_dev_config_set(int dev_index, int hwcap, void *value)
 /**
  * Get a block of data from the LA8.
  *
- * @param ctx The struct containing private per-device-instance data.
+ * @param ctx The struct containing private per-device-instance data. Must not
+ *            be NULL. ctx->ftdic must not be NULL either.
  * @return SR_OK upon success, or SR_ERR upon errors.
  */
 static int la8_read_block(struct context *ctx)
@@ -829,24 +829,16 @@ static int la8_read_block(struct context *ctx)
        int i, byte_offset, m, mi, p, index, bytes_read;
        time_t now;
 
-       if (!ctx) {
-               sr_err("la8: %s: ctx was NULL", __func__);
-               return SR_ERR_ARG;
-       }
-
-       if (!ctx->ftdic) {
-               sr_err("la8: %s: ctx->ftdic was NULL", __func__);
-               return SR_ERR_ARG;
-       }
+       /* Note: Caller checked that ctx and ctx->ftdic != NULL. */
 
-       sr_spew("la8: %s: reading block %d", __func__, ctx->block_counter);
+       sr_spew("la8: Reading block %d.", ctx->block_counter);
 
        bytes_read = la8_read(ctx, ctx->mangled_buf, BS);
 
        /* If first block read got 0 bytes, retry until success or timeout. */
        if ((bytes_read == 0) && (ctx->block_counter == 0)) {
                do {
-                       sr_spew("la8: %s: reading block 0 again", __func__);
+                       sr_spew("la8: Reading block 0 (again).");
                        bytes_read = la8_read(ctx, ctx->mangled_buf, BS);
                        /* TODO: How to handle read errors here? */
                        now = time(NULL);
@@ -855,13 +847,13 @@ static int la8_read_block(struct context *ctx)
 
        /* Check if block read was successful or a timeout occured. */
        if (bytes_read != BS) {
-               sr_err("la8: %s: trigger timed out", __func__);
+               sr_err("la8: Trigger timed out. Bytes read: %d.", bytes_read);
                (void) la8_reset(ctx); /* Ignore errors. */
                return SR_ERR;
        }
 
        /* De-mangle the data. */
-       sr_spew("la8: de-mangling samples of block %d", ctx->block_counter);
+       sr_spew("la8: Demangling block %d.", ctx->block_counter);
        byte_offset = ctx->block_counter * BS;
        m = byte_offset / (1024 * 1024);
        mi = m * (1024 * 1024);
@@ -988,6 +980,11 @@ static int receive_data(int fd, int revents, void *session_data)
                return FALSE;
        }
 
+       if (!ctx->ftdic) {
+               sr_err("la8: %s: ctx->ftdic was NULL", __func__);
+               return FALSE;
+       }
+
        /* Get one block of data. */
        if ((ret = la8_read_block(ctx)) < 0) {
                sr_err("la8: %s: la8_read_block error: %d", __func__, ret);
@@ -1001,7 +998,7 @@ static int receive_data(int fd, int revents, void *session_data)
                return TRUE;
        }
 
-       sr_dbg("la8: sampling finished, sending data to session bus now");
+       sr_dbg("la8: Sampling finished, sending data to session bus now.");
 
        /* All data was received and demangled, send it to the session bus. */
        for (i = 0; i < NUM_BLOCKS; i++)
@@ -1022,8 +1019,6 @@ static int hw_dev_acquisition_start(int dev_index, gpointer session_data)
        uint8_t buf[4];
        int bytes_written;
 
-       sr_spew("la8: entering %s", __func__);
-
        if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
                sr_err("la8: %s: sdi was NULL", __func__);
                return SR_ERR; /* TODO: SR_ERR_ARG? */
@@ -1045,6 +1040,8 @@ static int hw_dev_acquisition_start(int dev_index, gpointer session_data)
                return SR_ERR;
        }
 
+       sr_dbg("la8: Starting acquisition.");
+
        /* Fill acquisition parameters into buf[]. */
        buf[0] = ctx->divcount;
        buf[1] = 0xff; /* This byte must always be 0xff. */
@@ -1055,19 +1052,19 @@ static int hw_dev_acquisition_start(int dev_index, gpointer session_data)
        bytes_written = la8_write(ctx, buf, 4);
 
        if (bytes_written < 0) {
-               sr_err("la8: acquisition failed to start");
+               sr_err("la8: Acquisition failed to start.");
                return SR_ERR;
        } else if (bytes_written != 4) {
-               sr_err("la8: acquisition failed to start");
+               sr_err("la8: Acquisition failed to start.");
                return SR_ERR; /* TODO: Other error and return code? */
        }
 
-       sr_dbg("la8: acquisition started successfully");
+       sr_dbg("la8: Acquisition started successfully.");
 
        ctx->session_id = session_data;
 
        /* Send header packet to the session bus. */
-       sr_dbg("la8: %s: sending SR_DF_HEADER", __func__);
+       sr_dbg("la8: Sending SR_DF_HEADER.");
        packet.type = SR_DF_HEADER;
        packet.payload = &header;
        header.feed_version = 1;
@@ -1094,7 +1091,7 @@ static int hw_dev_acquisition_stop(int dev_index, gpointer session_data)
        struct context *ctx;
        struct sr_datafeed_packet packet;
 
-       sr_dbg("la8: stopping acquisition");
+       sr_dbg("la8: Stopping acquisition.");
 
        if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) {
                sr_err("la8: %s: sdi was NULL", __func__);
@@ -1107,7 +1104,7 @@ static int hw_dev_acquisition_stop(int dev_index, gpointer session_data)
        }
 
        /* Send end packet to the session bus. */
-       sr_dbg("la8: %s: sending SR_DF_END", __func__);
+       sr_dbg("la8: Sending SR_DF_END.");
        packet.type = SR_DF_END;
        sr_session_bus(session_data, &packet);