X-Git-Url: http://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Fchronovu-la8%2Fchronovu-la8.c;h=06de6340090be57b57b17ba5adbc80d9e10c8329;hb=b5750520bb0e2726ad6248e23f52a854535bc9a2;hp=28aea6b2c2927bff4d5287bda4c514cad807e05f;hpb=47671b0f26cc6b3e7cf16257f458eadefe623d9f;p=libsigrok.git diff --git a/hardware/chronovu-la8/chronovu-la8.c b/hardware/chronovu-la8/chronovu-la8.c index 28aea6b2..06de6340 100644 --- a/hardware/chronovu-la8/chronovu-la8.c +++ b/hardware/chronovu-la8/chronovu-la8.c @@ -42,6 +42,7 @@ static GSList *dev_insts = NULL; +/* Probes are numbered 0-7. */ static const char *probe_names[NUM_PROBES + 1] = { "0", "1", @@ -69,7 +70,7 @@ struct context { uint64_t limit_samples; /** TODO */ - gpointer session_id; + void *session_dev_id; /** * A buffer containing some (mangled) samples from the device. @@ -137,7 +138,7 @@ static int hwcaps[] = { /* Function prototypes. */ static int la8_close_usb_reset_sequencer(struct context *ctx); -static int hw_dev_acquisition_stop(int dev_index, gpointer session_data); +static int hw_dev_acquisition_stop(int dev_index, void *cb_data); static int la8_reset(struct context *ctx); static void fill_supported_samplerates_if_needed(void) @@ -308,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) { @@ -328,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) @@ -347,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; @@ -377,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 @@ -393,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; } @@ -451,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; } @@ -477,7 +479,7 @@ static int hw_init(const char *devinfo) ctx->cur_samplerate = SR_MHZ(100); /* 100MHz == max. samplerate */ ctx->limit_msec = 0; ctx->limit_samples = 0; - ctx->session_id = NULL; + ctx->session_dev_id = NULL; memset(ctx->mangled_buf, 0, BS); ctx->final_buf = NULL; ctx->trigger_pattern = 0x00; /* Value irrelevant, see trigger_mask. */ @@ -506,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, @@ -520,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. */ @@ -548,15 +551,16 @@ static int hw_dev_open(int dev_index) 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? */ + return SR_ERR_BUG; } if (!(ctx = sdi->priv)) { sr_err("la8: %s: sdi->priv was NULL", __func__); - return SR_ERR; /* TODO: SR_ERR_ARG? */ + return SR_ERR_BUG; } - 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, @@ -566,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) { @@ -575,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) { @@ -584,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); @@ -606,7 +610,7 @@ static int set_samplerate(struct sr_dev_inst *sdi, uint64_t samplerate) ctx = sdi->priv; - sr_spew("la8: setting samplerate"); + sr_spew("la8: Trying to set samplerate to %" PRIu64 "Hz.", samplerate); fill_supported_samplerates_if_needed(); @@ -617,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; } @@ -629,27 +633,27 @@ static int hw_dev_close(int dev_index) 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? */ + return SR_ERR_BUG; } if (!(ctx = sdi->priv)) { sr_err("la8: %s: sdi->priv was NULL", __func__); - return SR_ERR; /* TODO: SR_ERR_ARG? */ + return SR_ERR_BUG; } - 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; @@ -683,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; @@ -695,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. */ @@ -734,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; } @@ -751,33 +765,35 @@ 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? */ + return SR_ERR_BUG; } if (!(ctx = sdi->priv)) { sr_err("la8: %s: sdi->priv was NULL", __func__); - return SR_ERR; /* TODO: SR_ERR_ARG? */ + return SR_ERR_BUG; } + 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; @@ -785,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; @@ -793,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; } @@ -804,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) @@ -814,14 +831,14 @@ static int la8_read_block(struct context *ctx) /* 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); @@ -830,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); @@ -895,7 +912,7 @@ static void send_block_to_session_bus(struct context *ctx, int block) logic.length = BS; logic.unitsize = 1; logic.data = ctx->final_buf + (block * BS); - sr_session_bus(ctx->session_id, &packet); + sr_session_send(ctx->session_dev_id, &packet); return; } @@ -918,7 +935,7 @@ static void send_block_to_session_bus(struct context *ctx, int block) logic.length = trigger_point; logic.unitsize = 1; logic.data = ctx->final_buf + (block * BS); - sr_session_bus(ctx->session_id, &packet); + sr_session_send(ctx->session_dev_id, &packet); } /* Send the SR_DF_TRIGGER packet to the session bus. */ @@ -926,7 +943,7 @@ static void send_block_to_session_bus(struct context *ctx, int block) (block * BS) + trigger_point); packet.type = SR_DF_TRIGGER; packet.payload = NULL; - sr_session_bus(ctx->session_id, &packet); + sr_session_send(ctx->session_dev_id, &packet); /* If at least one sample is located after the trigger... */ if (trigger_point < (BS - 1)) { @@ -939,11 +956,11 @@ static void send_block_to_session_bus(struct context *ctx, int block) logic.length = BS - trigger_point; logic.unitsize = 1; logic.data = ctx->final_buf + (block * BS) + trigger_point; - sr_session_bus(ctx->session_id, &packet); + sr_session_send(ctx->session_dev_id, &packet); } } -static int receive_data(int fd, int revents, void *session_data) +static int receive_data(int fd, int revents, void *cb_data) { int i, ret; struct sr_dev_inst *sdi; @@ -953,8 +970,8 @@ static int receive_data(int fd, int revents, void *session_data) (void)fd; (void)revents; - if (!(sdi = session_data)) { - sr_err("la8: %s: session_data was NULL", __func__); + if (!(sdi = cb_data)) { + sr_err("la8: %s: cb_data was NULL", __func__); return FALSE; } @@ -971,7 +988,7 @@ static int receive_data(int fd, int revents, void *session_data) /* Get one block of data. */ if ((ret = la8_read_block(ctx)) < 0) { sr_err("la8: %s: la8_read_block error: %d", __func__, ret); - hw_dev_acquisition_stop(sdi->index, session_data); + hw_dev_acquisition_stop(sdi->index, sdi); return FALSE; } @@ -981,19 +998,19 @@ 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++) send_block_to_session_bus(ctx, i); - hw_dev_acquisition_stop(sdi->index, session_data); + hw_dev_acquisition_stop(sdi->index, sdi); // return FALSE; /* FIXME? */ return TRUE; } -static int hw_dev_acquisition_start(int dev_index, gpointer session_data) +static int hw_dev_acquisition_start(int dev_index, void *cb_data) { struct sr_dev_inst *sdi; struct context *ctx; @@ -1002,21 +1019,19 @@ 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? */ + return SR_ERR_BUG; } if (!(ctx = sdi->priv)) { sr_err("la8: %s: sdi->priv was NULL", __func__); - return SR_ERR; /* TODO: SR_ERR_ARG? */ + return SR_ERR_BUG; } if (!ctx->ftdic) { sr_err("la8: %s: ctx->ftdic was NULL", __func__); - return SR_ERR_ARG; + return SR_ERR_BUG; } ctx->divcount = samplerate_to_divcount(ctx->cur_samplerate); @@ -1025,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. */ @@ -1035,26 +1052,26 @@ 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; + ctx->session_dev_id = cb_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; gettimeofday(&header.starttime, NULL); header.samplerate = ctx->cur_samplerate; header.num_logic_probes = NUM_PROBES; - sr_session_bus(session_data, &packet); + sr_session_send(ctx->session_dev_id, &packet); /* Time when we should be done (for detecting trigger timeouts). */ ctx->done = (ctx->divcount + 1) * 0.08388608 + time(NULL) @@ -1068,13 +1085,13 @@ static int hw_dev_acquisition_start(int dev_index, gpointer session_data) return SR_OK; } -static int hw_dev_acquisition_stop(int dev_index, gpointer session_data) +static int hw_dev_acquisition_stop(int dev_index, void *cb_data) { struct sr_dev_inst *sdi; 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__); @@ -1087,14 +1104,14 @@ 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); + sr_session_send(cb_data, &packet); return SR_OK; } -SR_PRIV struct sr_dev_plugin chronovu_la8_plugin_info = { +SR_PRIV struct sr_dev_driver chronovu_la8_driver_info = { .name = "chronovu-la8", .longname = "ChronoVu LA8", .api_version = 1,