Formatting

Use enums for write mode, and change datatypes from int/long/char to int32_t/uint8_t where possible
This commit is contained in:
MCUdude 2022-02-20 19:08:30 +01:00
parent a9b756e5c7
commit bb99a36a14
2 changed files with 30 additions and 35 deletions

View File

@ -23,7 +23,7 @@
#include <ctype.h> #include <ctype.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
//#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <limits.h> #include <limits.h>
@ -329,18 +329,6 @@ static int cmd_dump(PROGRAMMER * pgm, struct avrpart * p,
static int cmd_write(PROGRAMMER * pgm, struct avrpart * p, static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
int argc, char * argv[]) int argc, char * argv[])
{ {
char * e;
int len, maxsize;
char * memtype;
long addr;
long i;
unsigned char * buf;
unsigned char b;
int rc;
int werror;
int write_mode, start_offset;
AVRMEM * mem;
if (argc < 4) { if (argc < 4) {
avrdude_message(MSG_INFO, avrdude_message(MSG_INFO,
"Usage: write <memtype> <start addr> <data1> <data2> <dataN>\n" "Usage: write <memtype> <start addr> <data1> <data2> <dataN>\n"
@ -350,19 +338,22 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
return -1; return -1;
} }
memtype = argv[1]; int32_t i;
uint8_t write_mode; // Operation mode, "standard" or "fill"
mem = avr_locate_mem(p, memtype); uint8_t start_offset; // Which argc argument
int32_t len; // Number of bytes to write to memory
char * memtype = argv[1]; // Memory name string
AVRMEM * mem = avr_locate_mem(p, memtype);
if (mem == NULL) { if (mem == NULL) {
avrdude_message(MSG_INFO, "\"%s\" memory type not defined for part \"%s\"\n", avrdude_message(MSG_INFO, "\"%s\" memory type not defined for part \"%s\"\n",
memtype, p->desc); memtype, p->desc);
return -1; return -1;
} }
uint32_t maxsize = mem->size;
maxsize = mem->size; char * end_ptr;
int32_t addr = strtoul(argv[2], &end_ptr, 0);
addr = strtoul(argv[2], &e, 0); if (*end_ptr || (end_ptr == argv[2])) {
if (*e || (e == argv[2])) {
avrdude_message(MSG_INFO, "%s (write): can't parse address \"%s\"\n", avrdude_message(MSG_INFO, "%s (write): can't parse address \"%s\"\n",
progname, argv[2]); progname, argv[2]);
return -1; return -1;
@ -377,8 +368,8 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
// Figure out how many bytes there is to write to memory // Figure out how many bytes there is to write to memory
if (strcmp(argv[argc - 1], "...") == 0) { if (strcmp(argv[argc - 1], "...") == 0) {
write_mode = WRITE_MODE_FILL; write_mode = WRITE_MODE_FILL;
len = strtoul(argv[3], &e, 0); len = strtoul(argv[3], &end_ptr, 0);
if (*e || (e == argv[3])) { if (*end_ptr || (end_ptr == argv[3])) {
avrdude_message(MSG_INFO, "%s (write ...): can't parse address \"%s\"\n", avrdude_message(MSG_INFO, "%s (write ...): can't parse address \"%s\"\n",
progname, argv[3]); progname, argv[3]);
return -1; return -1;
@ -387,7 +378,7 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
write_mode = WRITE_MODE_STANDARD; write_mode = WRITE_MODE_STANDARD;
} }
buf = malloc(mem->size); uint8_t * buf = malloc(mem->size);
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;
@ -442,13 +433,14 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
} else if (suffix == '\'') { } else if (suffix == '\'') {
data.size = 1; data.size = 1;
} }
// Try integers // Try integers
data.ll = strtoll(argv[i], &e, 0); data.ll = strtoll(argv[i], &end_ptr, 0);
if (*e || (e == argv[i])) { if (*end_ptr || (end_ptr == argv[i])) {
// Try float // Try float
data.f = strtof(argv[i], &e); data.f = strtof(argv[i], &end_ptr);
data.is_float = true; data.is_float = true;
if (*e || (e == argv[i])) { if (*end_ptr || (end_ptr == argv[i])) {
data.is_float = false; data.is_float = false;
// Try single character // Try single character
if (argv[i][0] == '\'') { if (argv[i][0] == '\'') {
@ -466,7 +458,7 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
(((strncmp(argv[i], "0x", 2) == 0) && strlen(argv[i]) > 3) || \ (((strncmp(argv[i], "0x", 2) == 0) && strlen(argv[i]) > 3) || \
(data.ll > 0xFF && strlen(argv[i]) > 2))) { (data.ll > 0xFF && strlen(argv[i]) > 2))) {
avrdude_message(MSG_INFO, "Warning: no size suffix specified for \"%s\". " avrdude_message(MSG_INFO, "Warning: no size suffix specified for \"%s\". "
"Writing %d bytes\n", "Writing %d byte(s)\n",
argv[i], argv[i],
llabs(data.ll) > 0xFFFFFFFF ? 8 : llabs(data.ll) > 0xFFFFFFFF ? 8 :
llabs(data.ll) > 0x0000FFFF || data.is_float ? 4 : \ llabs(data.ll) > 0x0000FFFF || data.is_float ? 4 : \
@ -500,22 +492,24 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
} }
pgm->err_led(pgm, OFF); pgm->err_led(pgm, OFF);
for (werror=0, i=0; i < (len + data.bytes_grown); i++) { bool werror = false;
rc = avr_write_byte(pgm, p, mem, addr+i, buf[i]); for (i = 0; i < (len + data.bytes_grown); i++) {
int32_t 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",
progname, buf[i], addr+i, rc); progname, buf[i], addr+i, rc);
if (rc == -1) if (rc == -1)
avrdude_message(MSG_INFO, "write operation not supported on memory type \"%s\"\n", avrdude_message(MSG_INFO, "write operation not supported on memory type \"%s\"\n",
mem->desc); mem->desc);
werror = 1; werror = true;
} }
uint8_t b;
rc = pgm->read_byte(pgm, p, mem, addr+i, &b); rc = pgm->read_byte(pgm, p, mem, addr+i, &b);
if (b != buf[i]) { if (b != buf[i]) {
avrdude_message(MSG_INFO, "%s (write): error writing 0x%02x at 0x%05lx cell=0x%02x\n", avrdude_message(MSG_INFO, "%s (write): error writing 0x%02x at 0x%05lx cell=0x%02x\n",
progname, buf[i], addr+i, b); progname, buf[i], addr+i, b);
werror = 1; werror = true;
} }
if (werror) { if (werror) {

View File

@ -27,9 +27,10 @@
extern "C" { extern "C" {
#endif #endif
// Macros for determining write mode typedef enum {
#define WRITE_MODE_STANDARD 0 WRITE_MODE_STANDARD = 0,
#define WRITE_MODE_FILL 1 WRITE_MODE_FILL = 1,
} mode;
int terminal_mode(PROGRAMMER * pgm, struct avrpart * p); int terminal_mode(PROGRAMMER * pgm, struct avrpart * p);
char * terminal_get_input(const char *prompt); char * terminal_get_input(const char *prompt);