Merge pull request #914 from MCUdude/terminal-write-str

Add terminal write string functionality
This commit is contained in:
Jörg Wunsch 2022-04-05 20:54:20 +02:00 committed by GitHub
commit e84210c6b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 21 deletions

View File

@ -1553,14 +1553,16 @@ avrdude: AVR device initialized and ready to accept instructions
Reading | ################################################## | 100% 0.01s Reading | ################################################## | 100% 0.01s
avrdude: Device signature = 0x1e970c (probably avr128db48) avrdude: Device signature = 0x1e970c (probably avr128db48)
avrdude> write eeprom 0 1234567890 'A' 'V' 'R' 2.718282 avrdude> write eeprom 0 1234567890 'A' 'V' 'R' 2.718282 "Hello World!"
>>> write eeprom 0 1234567890 'A' 'V' 'R' 2.718282 >>> write eeprom 0 1234567890 'A' 'V' 'R' 2.718282 "Hello World!"
Warning: no size suffix specified for "1234567890". Writing 4 byte(s) Warning: no size suffix specified for "1234567890". Writing 4 byte(s)
Info: Writing 24 bytes starting from address 0x00
avrdude> dump eeprom 0 32 avrdude> dump eeprom 0 32
>>> dump eeprom 0 32 >>> dump eeprom 0 32
0000 d2 02 96 49 41 56 52 55 f8 2d 40 ff ff ff ff ff |...IAVRU.-@.....|
0010 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff |................| 0000 d2 02 96 49 41 56 52 55 f8 2d 40 48 65 6c 6c 6f |...IAVRU.-@Hello|
0010 20 57 6f 72 6c 64 21 00 ff ff ff ff ff ff ff ff | World!.........|
avrdude> q avrdude> q
@end cartouche @end cartouche

View File

@ -126,9 +126,15 @@ static int nexttok(char * buf, char ** tok, char ** next)
q++; q++;
/* isolate first token */ /* isolate first token */
n = q+1; n = q;
while (*n && !isspace((int)*n)) uint8_t quotes = 0;
while (*n && (!isspace((int)*n) || quotes)) {
if (*n == '\"')
quotes++;
else if (isspace((int)*n) && *(n-1) == '\"')
break;
n++; n++;
}
if (*n) { if (*n) {
*n = 0; *n = 0;
@ -397,13 +403,21 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
uint8_t size; uint8_t size;
bool is_float; bool is_float;
bool is_signed; bool is_signed;
char * str_ptr;
// Data union // Data union
union { union {
float f; float f;
int64_t ll; int64_t ll;
uint8_t a[8]; uint8_t a[8];
}; };
} data = {.bytes_grown = 0, .size = 0, .is_float = false, .ll = 0, .is_signed = false}; } data = {
.bytes_grown = 0,
.size = 0,
.is_float = false,
.is_signed = false,
.str_ptr = NULL,
.ll = 0
};
for (i = start_offset; i < len + start_offset; i++) { for (i = start_offset; i < len + start_offset; i++) {
data.is_float = false; data.is_float = false;
@ -411,6 +425,12 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
// Handle the next argument // Handle the next argument
if (i < argc - start_offset + 3) { if (i < argc - start_offset + 3) {
// Free string pointer if already allocated
if(data.str_ptr) {
free(data.str_ptr);
data.str_ptr = NULL;
}
// Get suffix if present // Get suffix if present
char suffix = argv[i][strlen(argv[i]) - 1]; char suffix = argv[i][strlen(argv[i]) - 1];
char lsuffix = argv[i][strlen(argv[i]) - 2]; char lsuffix = argv[i][strlen(argv[i]) - 2];
@ -446,13 +466,26 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
if (argv[i][0] == '\'' && argv[i][2] == '\'') { if (argv[i][0] == '\'' && argv[i][2] == '\'') {
data.ll = argv[i][1]; data.ll = argv[i][1];
} else { } else {
avrdude_message(MSG_INFO, "\n%s (write): can't parse data \"%s\"\n", // Try string that starts and ends with quotes
if (argv[i][0] == '\"' && argv[i][strlen(argv[i]) - 1] == '\"') {
data.str_ptr = calloc(strlen(argv[i]), sizeof(char));
if (data.str_ptr == NULL) {
avrdude_message(MSG_INFO, "%s (write str): out of memory\n", progname);
return -1;
}
// Strip start and end quotes
strncpy(data.str_ptr, argv[i] + 1, strlen(argv[i]) - 2);
} else {
avrdude_message(MSG_INFO, "\n%s (write): can't parse data '%s'\n",
progname, argv[i]); progname, argv[i]);
free(buf); free(buf);
if(data.str_ptr != NULL)
free(data.str_ptr);
return -1; return -1;
} }
} }
} }
}
// Print warning if data size might be ambiguous // Print warning if data size might be ambiguous
bool is_hex = (strncmp(argv[i], "0x", 2) == 0); bool is_hex = (strncmp(argv[i], "0x", 2) == 0);
@ -484,6 +517,10 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
data.size = 1; data.size = 1;
} }
} }
if(data.str_ptr) {
for(int16_t j = 0; j < strlen(data.str_ptr); j++)
buf[i - start_offset + data.bytes_grown++] = (uint8_t)data.str_ptr[j];
} else {
buf[i - start_offset + data.bytes_grown] = data.a[0]; buf[i - start_offset + data.bytes_grown] = data.a[0];
if (llabs(data.ll) > 0x000000FF || data.size >= 2 || data.is_float) if (llabs(data.ll) > 0x000000FF || data.size >= 2 || data.is_float)
buf[i - start_offset + ++data.bytes_grown] = data.a[1]; buf[i - start_offset + ++data.bytes_grown] = data.a[1];
@ -498,6 +535,7 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
buf[i - start_offset + ++data.bytes_grown] = data.a[7]; buf[i - start_offset + ++data.bytes_grown] = data.a[7];
} }
} }
}
// When in "fill" mode, the maximum size is already predefined // When in "fill" mode, the maximum size is already predefined
if (write_mode == WRITE_MODE_FILL) if (write_mode == WRITE_MODE_FILL)
@ -511,6 +549,9 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
return -1; return -1;
} }
if(data.str_ptr)
free(data.str_ptr);
avrdude_message(MSG_NOTICE, "\nInfo: Writing %d bytes starting from address 0x%02x", avrdude_message(MSG_NOTICE, "\nInfo: Writing %d bytes starting from address 0x%02x",
len + data.bytes_grown, addr); len + data.bytes_grown, addr);
if (write_mode == WRITE_MODE_FILL) if (write_mode == WRITE_MODE_FILL)