From: Gerhard Sittig Date: Tue, 12 May 2020 05:20:17 +0000 (+0200) Subject: asix-sigma: eliminate magic numbers in firmware file references X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=80e717b3cfac82d63c98614fe6179fb0e4fdaf8d;p=libsigrok.git asix-sigma: eliminate magic numbers in firmware file references 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. --- diff --git a/src/hardware/asix-sigma/api.c b/src/hardware/asix-sigma/api.c index 38814872..42d1d4a4 100644 --- a/src/hardware/asix-sigma/api.c +++ b/src/hardware/asix-sigma/api.c @@ -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; } diff --git a/src/hardware/asix-sigma/protocol.c b/src/hardware/asix-sigma/protocol.c index 7405c8fd..9819e892 100644 --- a/src/hardware/asix-sigma/protocol.c +++ b/src/hardware/asix-sigma/protocol.c @@ -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; } diff --git a/src/hardware/asix-sigma/protocol.h b/src/hardware/asix-sigma/protocol.h index f1ffcd7c..cd7bc239 100644 --- a/src/hardware/asix-sigma/protocol.h +++ b/src/hardware/asix-sigma/protocol.h @@ -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;