]> sigrok.org Git - libsigrok.git/commitdiff
asix-sigma: eliminate magic numbers in firmware file references
authorGerhard Sittig <redacted>
Tue, 12 May 2020 05:20:17 +0000 (07:20 +0200)
committerGerhard Sittig <redacted>
Fri, 29 May 2020 05:50:18 +0000 (07:50 +0200)
Use symbolic identifiers to select firmware images, which eliminates
magic 0/1/2 position numbers in the list of files, improves readability
and also improves robustness. Move 'devc' to 'ctx' and before other
arguments in routine signatures while we are here.

src/hardware/asix-sigma/api.c
src/hardware/asix-sigma/protocol.c
src/hardware/asix-sigma/protocol.h

index 388148725d852eeb1e142c6b9acf98d3c8041445..42d1d4a403bf25aaaa42d57d10ac9e08c9f87588 100644 (file)
@@ -235,7 +235,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
                devc->id.type = dev_type;
                devc->samplerate = samplerates[0];
                sr_sw_limits_init(&devc->cfg_limits);
-               devc->cur_firmware = -1;
+               devc->firmware_idx = SIGMA_FW_NONE;
                devc->capture_ratio = 50;
                devc->use_triggers = 0;
        }
index 7405c8fdfdd51e7b920acdc9726e07e74a22f870..9819e892508632e73bb2c8a7326b083acefed5c6 100644 (file)
@@ -48,11 +48,11 @@ SR_PRIV const uint64_t samplerates[] = {
 SR_PRIV const size_t samplerates_count = ARRAY_SIZE(samplerates);
 
 static const char *firmware_files[] = {
-       "asix-sigma-50.fw", /* Up to 50MHz sample rate, 8bit divider. */
-       "asix-sigma-100.fw", /* 100MHz sample rate, fixed. */
-       "asix-sigma-200.fw", /* 200MHz sample rate, fixed. */
-       "asix-sigma-50sync.fw", /* Synchronous clock from external pin. */
-       "asix-sigma-phasor.fw", /* Frequency counter. */
+       [SIGMA_FW_50MHZ] = "asix-sigma-50.fw", /* 50MHz, 8bit divider. */
+       [SIGMA_FW_100MHZ] = "asix-sigma-100.fw", /* 100MHz, fixed. */
+       [SIGMA_FW_200MHZ] = "asix-sigma-200.fw", /* 200MHz, fixed. */
+       [SIGMA_FW_SYNC] = "asix-sigma-50sync.fw", /* Sync from external pin. */
+       [SIGMA_FW_FREQ] = "asix-sigma-phasor.fw", /* Frequency counter. */
 };
 
 #define SIGMA_FIRMWARE_SIZE_LIMIT (256 * 1024)
@@ -496,7 +496,7 @@ static int sigma_fw_2_bitbang(struct sr_context *ctx, const char *name,
 }
 
 static int upload_firmware(struct sr_context *ctx,
-               int firmware_idx, struct dev_context *devc)
+       struct dev_context *devc, enum sigma_firmware_idx firmware_idx)
 {
        int ret;
        unsigned char *buf;
@@ -504,9 +504,15 @@ static int upload_firmware(struct sr_context *ctx,
        size_t buf_size;
        const char *firmware;
 
-       /* Avoid downloading the same firmware multiple times. */
+       /* Check for valid firmware file selection. */
+       if (firmware_idx >= ARRAY_SIZE(firmware_files))
+               return SR_ERR_ARG;
        firmware = firmware_files[firmware_idx];
-       if (devc->cur_firmware == firmware_idx) {
+       if (!firmware || !*firmware)
+               return SR_ERR_ARG;
+
+       /* Avoid downloading the same firmware multiple times. */
+       if (devc->firmware_idx == firmware_idx) {
                sr_info("Not uploading firmware file '%s' again.", firmware);
                return SR_OK;
        }
@@ -564,7 +570,7 @@ static int upload_firmware(struct sr_context *ctx,
 
        /* Keep track of successful firmware download completion. */
        devc->state.state = SIGMA_IDLE;
-       devc->cur_firmware = firmware_idx;
+       devc->firmware_idx = firmware_idx;
        sr_info("Firmware uploaded.");
 
        return SR_OK;
@@ -705,13 +711,13 @@ SR_PRIV int sigma_set_samplerate(const struct sr_dev_inst *sdi)
         */
        num_channels = devc->num_channels;
        if (samplerate <= SR_MHZ(50)) {
-               ret = upload_firmware(drvc->sr_ctx, 0, devc);
+               ret = upload_firmware(drvc->sr_ctx, devc, SIGMA_FW_50MHZ);
                num_channels = 16;
        } else if (samplerate == SR_MHZ(100)) {
-               ret = upload_firmware(drvc->sr_ctx, 1, devc);
+               ret = upload_firmware(drvc->sr_ctx, devc, SIGMA_FW_100MHZ);
                num_channels = 8;
        } else if (samplerate == SR_MHZ(200)) {
-               ret = upload_firmware(drvc->sr_ctx, 2, devc);
+               ret = upload_firmware(drvc->sr_ctx, devc, SIGMA_FW_200MHZ);
                num_channels = 4;
        }
 
index f1ffcd7c7e777f8e7ccd37aa64b261f2ca33294c..cd7bc23953ccc2cc71d46cf9f28eb482f30fe3e1 100644 (file)
@@ -294,6 +294,15 @@ struct sigma_state {
        uint16_t lastsample;
 };
 
+enum sigma_firmware_idx {
+       SIGMA_FW_NONE,
+       SIGMA_FW_50MHZ,
+       SIGMA_FW_100MHZ,
+       SIGMA_FW_200MHZ,
+       SIGMA_FW_SYNC,
+       SIGMA_FW_FREQ,
+};
+
 struct submit_buffer;
 
 struct dev_context {
@@ -308,7 +317,7 @@ struct dev_context {
        struct sr_sw_limits cfg_limits; /* Configured limits (user specified). */
        struct sr_sw_limits acq_limits; /* Acquisition limits (internal use). */
        struct sr_sw_limits feed_limits; /* Datafeed limits (internal use). */
-       int cur_firmware;
+       enum sigma_firmware_idx firmware_idx;
        int num_channels;
        int samples_per_event;
        uint64_t capture_ratio;