Add support for writing floats

This commit is contained in:
MCUdude 2022-02-17 22:40:26 +01:00
parent 2a92b8cce4
commit 551046052e
1 changed files with 49 additions and 30 deletions

View File

@ -381,25 +381,18 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
} }
} else { } else {
write_mode = WRITE_MODE_STANDARD; write_mode = WRITE_MODE_STANDARD;
len = argc - 3;
} }
if ((addr + len) > maxsize) { buf = malloc(mem->size);
avrdude_message(MSG_INFO, "%s (write): selected address and # bytes exceed "
"range for %s memory\n",
progname, memtype);
return -1;
}
buf = malloc(len);
if (buf == NULL) { if (buf == NULL) {
avrdude_message(MSG_INFO, "%s (write): out of memory\n", progname); avrdude_message(MSG_INFO, "%s (write): out of memory\n", progname);
return -1; return -1;
} }
if (write_mode == WRITE_MODE_STANDARD) if (write_mode == WRITE_MODE_STANDARD) {
start_offset = 3; // Data to write from argument no. 3 start_offset = 3; // Argument number where data to write starts
else if (write_mode == WRITE_MODE_FILL) len = argc - start_offset;
} else if (write_mode == WRITE_MODE_FILL)
start_offset = 4; start_offset = 4;
else { else {
avrdude_message(MSG_INFO, "%s (write): invalid write mode %d\n", avrdude_message(MSG_INFO, "%s (write): invalid write mode %d\n",
@ -407,29 +400,55 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
return -1; return -1;
} }
unsigned char write_val; long write_val;
for (i = start_offset; i < argc - start_offset + 3; i++) { int bytes_grown = 0;
write_val = strtoul(argv[i], &e, 0); for (i = start_offset; i < len + start_offset - bytes_grown; i++) {
char* ptr = NULL;
// Handle the next argument
if (i < argc - start_offset + 3) {
// Try integers
write_val = strtol(argv[i], &e, 0);
if (*e || (e == argv[i])) { if (*e || (e == argv[i])) {
// Accept if passed argument is a single character // Try float
float f = strtof(argv[i], &e);
ptr = (char*)&f;
write_val = ((char)*(ptr+3)<<24) + ((char)*(ptr+2)<<16) + ((char)*(ptr+1)<<8) + (char)*ptr;
if (*e || (e == argv[i])) {
ptr = NULL;
// Try single character
if (argv[i][1] == '\0') { if (argv[i][1] == '\0') {
write_val = argv[i][0]; write_val = argv[i][0];
} else { } else {
avrdude_message(MSG_INFO, "%s (write ...): can't parse byte \"%s\"\n", avrdude_message(MSG_INFO, "%s (write): can't parse data \"%s\"\n",
progname, argv[i]); progname, argv[i]);
free(buf); free(buf);
return -1; return -1;
} }
} }
buf[i - start_offset] = write_val;
} }
for (; i < len + start_offset; i++) { }
buf[i - start_offset] = write_val; buf[i - start_offset + bytes_grown] = (write_val >> 0) & 0xFF;
if (write_val > 0xFF || ptr)
buf[i - start_offset + ++bytes_grown] = (write_val >> 8) & 0xFF;
if (write_val > 0xFFFF || ptr) {
buf[i - start_offset + ++bytes_grown] = (write_val >> 16) & 0xFF;
buf[i - start_offset + ++bytes_grown] = (write_val >> 24) & 0xFF;
}
}
// When in "fill" mode, the maximum size is already predefined
if (write_mode == WRITE_MODE_FILL)
bytes_grown = 0;
if ((addr + len + bytes_grown) > maxsize) {
avrdude_message(MSG_INFO, "%s (write): selected address and # bytes exceed "
"range for %s memory\n",
progname, memtype);
return -1;
} }
pgm->err_led(pgm, OFF); pgm->err_led(pgm, OFF);
for (werror=0, i=0; i<len; i++) { for (werror=0, i=0; i < (len + bytes_grown); i++) {
rc = avr_write_byte(pgm, p, mem, addr+i, buf[i]); rc = avr_write_byte(pgm, p, mem, addr+i, buf[i]);
if (rc) { if (rc) {
avrdude_message(MSG_INFO, "%s (write): error writing 0x%02x at 0x%05lx, rc=%d\n", avrdude_message(MSG_INFO, "%s (write): error writing 0x%02x at 0x%05lx, rc=%d\n",