diff --git a/dummyflasher.c b/dummyflasher.c index 89e8ba43e..6426ad2ed 100644 --- a/dummyflasher.c +++ b/dummyflasher.c @@ -54,6 +54,9 @@ struct emu_data { unsigned int emu_chip_size; int emu_modified; /* is the image modified since reading it? */ uint8_t emu_status; + /* If "freq" parameter is passed in from command line, commands will delay + * for this period before returning. */ + unsigned long int delay_us; unsigned int emu_max_byteprogram_size; unsigned int emu_max_aai_size; unsigned int emu_jedec_se_size; @@ -175,6 +178,7 @@ int dummy_init(void) return 1; } data->emu_chip = EMULATE_NONE; + data->delay_us = 0; spi_master_dummyflasher.data = data; msg_pspew("%s\n", __func__); @@ -289,6 +293,52 @@ int dummy_init(void) } free(tmp); + /* frequency to emulate in Hz (default), KHz, or MHz */ + tmp = extract_programmer_param("freq"); + if (tmp) { + unsigned long int freq; + char *units = tmp; + char *end = tmp + strlen(tmp); + + errno = 0; + freq = strtoul(tmp, &units, 0); + if (errno) { + msg_perr("Invalid frequency \"%s\", %s\n", + tmp, strerror(errno)); + free(tmp); + return 1; + } + + if ((units > tmp) && (units < end)) { + int units_valid = 0; + + if (units < end - 3) { + ; + } else if (units == end - 2) { + if (!strcasecmp(units, "hz")) + units_valid = 1; + } else if (units == end - 3) { + if (!strcasecmp(units, "khz")) { + freq *= 1000; + units_valid = 1; + } else if (!strcasecmp(units, "mhz")) { + freq *= 1000000; + units_valid = 1; + } + } + + if (!units_valid) { + msg_perr("Invalid units: %s\n", units); + free(tmp); + return 1; + } + } + + /* Assume we only work with bytes and transfer at 1 bit/Hz */ + data->delay_us = (1000000 * 8) / freq; + } + free(tmp); + #if EMULATE_CHIP #if EMULATE_SPI_CHIP tmp = extract_programmer_param("size"); @@ -963,6 +1013,8 @@ static int dummy_spi_send_command(const struct flashctx *flash, unsigned int wri for (i = 0; i < readcnt; i++) msg_pspew(" 0x%02x", readarr[i]); msg_pspew("\n"); + + programmer_delay((writecnt + readcnt) * emu_data->delay_us); return 0; }