]> sigrok.org Git - libsigrok.git/commitdiff
kingst-la2016: renames to better reflect sequence of activities
authorGerhard Sittig <redacted>
Sun, 23 Jan 2022 18:11:41 +0000 (19:11 +0100)
committerGerhard Sittig <redacted>
Sun, 6 Feb 2022 17:53:53 +0000 (18:53 +0100)
The previous implementation started on a single model with lots of
experiments, and could benefit from a cleanup after more knowledge was
gathered. Rename variables and routines to better reflect what actually
is happening, use more suitable data types. Drop unused devc members.
Fixup typos in diagnostics, protect against MSB issues in bitmasks.

The USB scan just yields one of the supported devices, its model is
unknown at this time. The list of enabled channels is determined upon
acquisition start when the trigger configuration gets created. The
device executes captures in hardware to internal RAM, while the driver
waits for the acquisition to complete, before downloading sample data
to the PC. Feeding sample data to the session ends when the data is
exhausted (a good condition is yet to get found). An optional trigger
marker is put into the stream of samples but only if a trigger was
configured.

src/hardware/kingst-la2016/api.c
src/hardware/kingst-la2016/protocol.c
src/hardware/kingst-la2016/protocol.h

index 4337bd0a879a4b63e4b1f059744d156762b28016..7a18742095f2700762540163015d47aad87cbe85 100644 (file)
@@ -191,7 +191,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
                        continue;
 
                /* USB identification matches, a device was found. */
-               sr_dbg("Found a LA2016 device.");
+               sr_dbg("Found a device (USB identification).");
                sdi = g_malloc0(sizeof(struct sr_dev_inst));
                sdi->status = SR_ST_INITIALIZING;
                sdi->connection_id = g_strdup(connection_id);
@@ -553,14 +553,11 @@ static int configure_channels(const struct sr_dev_inst *sdi)
 
        devc = sdi->priv;
        devc->cur_channels = 0;
-       devc->num_channels = 0;
-
        for (GSList *l = sdi->channels; l; l = l->next) {
                struct sr_channel *ch = (struct sr_channel*)l->data;
                if (ch->enabled == FALSE)
                        continue;
                devc->cur_channels |= 1 << ch->index;
-               devc->num_channels++;
        }
 
        return SR_OK;
@@ -601,7 +598,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi)
                return ret;
        }
 
-       devc->have_trigger = 0;
+       devc->completion_seen = FALSE;
        usb_source_add(sdi->session, drvc->sr_ctx, 50,
                la2016_receive_data, (void *)sdi);
 
index fed6ccf6175168d18ebe6d91ab5aa404a961e65c..5ea9d688532e508e8fdc02dc92c4cc314c944ede 100644 (file)
@@ -474,7 +474,6 @@ static int set_defaults(const struct sr_dev_inst *sdi)
        devc = sdi->priv;
 
        devc->capture_ratio = LA2016_DFLT_CAPT_RATIO;
-       devc->cur_channels = 0xffff;
        devc->limit_samples = LA2016_DFLT_SAMPLEDEPTH;
        devc->cur_samplerate = LA2016_DFLT_SAMPLERATE;
 
@@ -532,7 +531,7 @@ static int set_trigger_config(const struct sr_dev_inst *sdi)
                channel = stage1->matches;
                while (channel) {
                        match = channel->data;
-                       ch_mask = 1 << match->channel->index;
+                       ch_mask = 1UL << match->channel->index;
 
                        switch (match->match) {
                        case SR_TRIGGER_ZERO:
@@ -572,7 +571,7 @@ static int set_trigger_config(const struct sr_dev_inst *sdi)
                "level-triggered 0x%04x, high/falling 0x%04x.",
                cfg.channels, cfg.enabled, cfg.level, cfg.high_or_falling);
 
-       devc->had_triggers_configured = cfg.enabled != 0;
+       devc->trigger_involved = cfg.enabled != 0;
 
        wrptr = buf;
        write_u32le_inc(&wrptr, cfg.channels);
@@ -754,7 +753,7 @@ static uint16_t run_state(const struct sr_dev_inst *sdi)
        return state;
 }
 
-static int la2016_has_triggered(const struct sr_dev_inst *sdi)
+static int la2016_is_idle(const struct sr_dev_inst *sdi)
 {
        uint16_t state;
 
@@ -796,7 +795,7 @@ static int get_capture_info(const struct sr_dev_inst *sdi)
        devc->info.n_rep_packets_before_trigger = read_u32le_inc(&rdptr);
        devc->info.write_pos = read_u32le_inc(&rdptr);
 
-       sr_dbg("Capture info: n_rep_packets: 0x%08x/%d, before_trigger: 0x%08x/%d, write_pos: 0x%08x%d.",
+       sr_dbg("Capture info: n_rep_packets: 0x%08x/%d, before_trigger: 0x%08x/%d, write_pos: 0x%08x/%d.",
                devc->info.n_rep_packets, devc->info.n_rep_packets,
                devc->info.n_rep_packets_before_trigger,
                devc->info.n_rep_packets_before_trigger,
@@ -886,7 +885,7 @@ SR_PRIV int la2016_abort_acquisition(const struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-static int la2016_start_retrieval(const struct sr_dev_inst *sdi,
+static int la2016_start_download(const struct sr_dev_inst *sdi,
        libusb_transfer_cb_fn cb)
 {
        struct dev_context *devc;
@@ -971,7 +970,7 @@ static void send_chunk(struct sr_dev_inst *sdi,
        struct sr_datafeed_packet sr_packet;
        unsigned int max_samples, n_samples, total_samples, free_n_samples;
        unsigned int i, j, k;
-       int do_signal_trigger;
+       gboolean do_signal_trigger;
        uint8_t *wp;
        const uint8_t *rp;
        uint16_t state;
@@ -990,11 +989,11 @@ static void send_chunk(struct sr_dev_inst *sdi,
        n_samples = 0;
        wp = devc->convbuffer;
        total_samples = 0;
-       do_signal_trigger = 0;
+       do_signal_trigger = FALSE;
 
-       if (devc->had_triggers_configured && devc->reading_behind_trigger == 0 && devc->info.n_rep_packets_before_trigger == 0) {
+       if (devc->trigger_involved && !devc->trigger_marked && devc->info.n_rep_packets_before_trigger == 0) {
                std_session_send_df_trigger(sdi);
-               devc->reading_behind_trigger = 1;
+               devc->trigger_marked = TRUE;
        }
 
        rp = packets;
@@ -1008,7 +1007,7 @@ static void send_chunk(struct sr_dev_inst *sdi,
                                wp = devc->convbuffer;
                                if (do_signal_trigger) {
                                        std_session_send_df_trigger(sdi);
-                                       do_signal_trigger = 0;
+                                       do_signal_trigger = FALSE;
                                }
                        }
 
@@ -1023,11 +1022,10 @@ static void send_chunk(struct sr_dev_inst *sdi,
                        n_samples += repetitions;
                        total_samples += repetitions;
                        devc->total_samples += repetitions;
-                       if (!devc->reading_behind_trigger) {
-                               devc->n_reps_until_trigger--;
-                               if (devc->n_reps_until_trigger == 0) {
-                                       devc->reading_behind_trigger = 1;
-                                       do_signal_trigger = 1;
+                       if (devc->trigger_involved && !devc->trigger_marked) {
+                               if (!--devc->n_reps_until_trigger) {
+                                       devc->trigger_marked = TRUE;
+                                       do_signal_trigger = TRUE;
                                        sr_dbg("Trigger position after %" PRIu64 " samples, %.6fms.",
                                                devc->total_samples,
                                                (double)devc->total_samples / devc->cur_samplerate * 1e3);
@@ -1062,7 +1060,7 @@ static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
 
        if (transfer->status == LIBUSB_TRANSFER_TIMED_OUT) {
                sr_err("USB bulk transfer timeout.");
-               devc->transfer_finished = 1;
+               devc->download_finished = TRUE;
        }
        send_chunk(sdi, transfer->buffer, transfer->actual_length / TRANSFER_PACKET_LENGTH);
 
@@ -1091,7 +1089,7 @@ static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
 
        g_free(transfer->buffer);
        libusb_free_transfer(transfer);
-       devc->transfer_finished = 1;
+       devc->download_finished = TRUE;
 }
 
 SR_PRIV int la2016_receive_data(int fd, int revents, void *cb_data)
@@ -1108,17 +1106,17 @@ SR_PRIV int la2016_receive_data(int fd, int revents, void *cb_data)
        devc = sdi->priv;
        drvc = sdi->driver->context;
 
-       if (devc->have_trigger == 0) {
-               if (la2016_has_triggered(sdi) == 0) {
+       if (!devc->completion_seen) {
+               if (!la2016_is_idle(sdi)) {
                        /* Not yet ready for sample data download. */
                        return TRUE;
                }
-               devc->have_trigger = 1;
-               devc->transfer_finished = 0;
-               devc->reading_behind_trigger = 0;
+               devc->completion_seen = TRUE;
+               devc->download_finished = FALSE;
+               devc->trigger_marked = FALSE;
                devc->total_samples = 0;
                /* We can start downloading sample data. */
-               if (la2016_start_retrieval(sdi, receive_transfer) != SR_OK) {
+               if (la2016_start_download(sdi, receive_transfer) != SR_OK) {
                        sr_err("Cannot start acquisition data download.");
                        return FALSE;
                }
@@ -1131,7 +1129,7 @@ SR_PRIV int la2016_receive_data(int fd, int revents, void *cb_data)
        tv.tv_sec = tv.tv_usec = 0;
        libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
 
-       if (devc->transfer_finished) {
+       if (devc->download_finished) {
                sr_dbg("Download finished, post processing.");
                std_session_send_df_frame_end(sdi);
 
index 76a1cc9f755999649faa07cfafca69be0170bc94..c396d036c446d2f547cac6345018f966347f1bd6 100644 (file)
@@ -116,17 +116,16 @@ struct dev_context {
        uint64_t limit_samples;
        uint64_t capture_ratio;
        uint16_t cur_channels;
-       int num_channels;
 
        /* Internal acquisition and download state. */
-       int had_triggers_configured;
-       int have_trigger;
-       int transfer_finished;
+       gboolean trigger_involved;
+       gboolean completion_seen;
+       gboolean download_finished;
        capture_info_t info;
        unsigned int n_transfer_packets_to_read; /* each with 5 acq packets */
        unsigned int n_bytes_to_read;
        unsigned int n_reps_until_trigger;
-       unsigned int reading_behind_trigger;
+       gboolean trigger_marked;
        uint64_t total_samples;
        uint32_t read_pos;