Use union for simpler data representation

It is a bit hacky, but for this purpose it fits surprisingly well
This commit is contained in:
MCUdude 2022-02-19 20:15:52 +01:00
parent c345985928
commit 62f3b84eee
1 changed files with 21 additions and 15 deletions

View File

@ -23,6 +23,7 @@
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
//#include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <limits.h> #include <limits.h>
@ -400,24 +401,29 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
return -1; return -1;
} }
long write_val; // Union to represent the data to write to memory
int bytes_grown = 0; union Data {
float f;
int32_t i;
uint8_t a[4];
} data;
int32_t bytes_grown = 0;
for (i = start_offset; i < len + start_offset - bytes_grown; i++) { for (i = start_offset; i < len + start_offset - bytes_grown; i++) {
char* ptr = NULL; bool is_float = false;
// Handle the next argument // Handle the next argument
if (i < argc - start_offset + 3) { if (i < argc - start_offset + 3) {
// Try integers // Try integers
write_val = strtol(argv[i], &e, 0); data.i = strtol(argv[i], &e, 0);
if (*e || (e == argv[i])) { if (*e || (e == argv[i])) {
// Try float // Try float
float f = strtof(argv[i], &e); data.f = strtof(argv[i], &e);
ptr = (char*)&f; is_float = true;
write_val = ((char)*(ptr+3)<<24) + ((char)*(ptr+2)<<16) + ((char)*(ptr+1)<<8) + (char)*ptr;
if (*e || (e == argv[i])) { if (*e || (e == argv[i])) {
ptr = NULL; is_float = false;
// Try single character // Try single character
if (argv[i][0] == '\'' && argv[i][2] == '\'') { if (argv[i][0] == '\'' && argv[i][2] == '\'') {
write_val = argv[i][1]; data.i = argv[i][1];
} else { } else {
avrdude_message(MSG_INFO, "%s (write): can't parse data \"%s\"\n", avrdude_message(MSG_INFO, "%s (write): can't parse data \"%s\"\n",
progname, argv[i]); progname, argv[i]);
@ -427,12 +433,12 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
} }
} }
} }
buf[i - start_offset + bytes_grown] = (write_val >> 0) & 0xFF; buf[i - start_offset + bytes_grown] = data.a[0];
if (labs(write_val) > 0xFF || ptr) if (labs(data.i) > 0xFF || is_float)
buf[i - start_offset + ++bytes_grown] = (write_val >> 8) & 0xFF; buf[i - start_offset + ++bytes_grown] = data.a[1];
if (labs(write_val) > 0xFFFF || ptr) { if (labs(data.i) > 0xFFFF || is_float) {
buf[i - start_offset + ++bytes_grown] = (write_val >> 16) & 0xFF; buf[i - start_offset + ++bytes_grown] = data.a[2];
buf[i - start_offset + ++bytes_grown] = (write_val >> 24) & 0xFF; buf[i - start_offset + ++bytes_grown] = data.a[3];
} }
} }