usart: use \n instead of \r\n

This commit is contained in:
S.J.R. van Schaik 2017-03-11 00:14:57 +00:00
parent c28ddec92b
commit b0dd5a9abc
3 changed files with 28 additions and 21 deletions

View file

@ -75,7 +75,7 @@ static void print_hex_ascii(FILE *fp, const char *buf, size_t len)
fprintf(fp, "%c", isalnum(c) ? c : '.'); fprintf(fp, "%c", isalnum(c) ? c : '.');
} }
fprintf(fp, "\r\n"); fprintf(fp, "\n");
} }
} }
@ -92,7 +92,7 @@ static char *prompt(FILE *fp, const char *prefix)
s = line; s = line;
while ((c = fgetc(fp)) && c != '\r') { while ((c = fgetc(fp)) && c != '\n') {
fputc(c, fp); fputc(c, fp);
if (nbytes + 1 >= nalloc_bytes) { if (nbytes + 1 >= nalloc_bytes) {
@ -108,7 +108,7 @@ static char *prompt(FILE *fp, const char *prefix)
nbytes++; nbytes++;
} }
fprintf(fp, "\r\n"); fprintf(fp, "\n");
*s = '\0'; *s = '\0';
return line; return line;
@ -126,7 +126,7 @@ static void do_probe(FILE *fp, const char *s)
(void)s; (void)s;
spi_send_cmd(SPI1, cmd, 1, buf, 6); spi_send_cmd(SPI1, cmd, 1, buf, 6);
fprintf(fp, "JEDEC ID: %02x%02x%02x\r\n", buf[0], buf[1], buf[2]); fprintf(fp, "JEDEC ID: %02x%02x%02x\n", buf[0], buf[1], buf[2]);
} }
static void do_read(FILE *fp, const char *s) static void do_read(FILE *fp, const char *s)
@ -143,7 +143,7 @@ static void do_read(FILE *fp, const char *s)
if (errno == EINVAL || errno == ERANGE) if (errno == EINVAL || errno == ERANGE)
return; return;
s += strspn(s, " \r\n"); s += strspn(s, " \n");
if (strncmp(s, "0x", 2) == 0) if (strncmp(s, "0x", 2) == 0)
s += 2; s += 2;
@ -181,7 +181,7 @@ static void do_write(FILE *fp, const char *s)
if (errno == EINVAL || errno == ERANGE) if (errno == EINVAL || errno == ERANGE)
return; return;
s += strspn(s, " \r\n"); s += strspn(s, " \n");
if (strncmp(s, "0x", 2) == 0) if (strncmp(s, "0x", 2) == 0)
s += 2; s += 2;
@ -198,7 +198,7 @@ static void do_write(FILE *fp, const char *s)
while (len) { while (len) {
nbytes = (len > 256) ? 256 : len; nbytes = (len > 256) ? 256 : len;
parse_hex(fp, buf, nbytes); parse_hex(fp, buf, nbytes);
fprintf(fp, "\r\n"); fprintf(fp, "\n");
print_hex_ascii(fp, buf, nbytes); print_hex_ascii(fp, buf, nbytes);
spi_flash_write(SPI1, addr, buf, nbytes); spi_flash_write(SPI1, addr, buf, nbytes);
@ -223,7 +223,7 @@ static void do_erase(FILE *fp, const char *s)
if (errno == EINVAL || errno == ERANGE) if (errno == EINVAL || errno == ERANGE)
return; return;
s += strspn(s, " \r\n"); s += strspn(s, " \n");
if (strncmp(s, "0x", 2) == 0) if (strncmp(s, "0x", 2) == 0)
s += 2; s += 2;
@ -247,11 +247,11 @@ static void parse_cmd(FILE *fp, const char *s)
char *key; char *key;
size_t n; size_t n;
n = strcspn(s, " \r\n"); n = strcspn(s, " \n");
key = strndup(s, n); key = strndup(s, n);
args = s + n; args = s + n;
args += strspn(args, " \r\n"); args += strspn(args, " \n");
for (cmd = cmds; cmd->key; cmd++) { for (cmd = cmds; cmd->key; cmd++) {
if (strcmp(cmd->key, key) == 0) if (strcmp(cmd->key, key) == 0)

View file

@ -144,7 +144,7 @@ void spi_flash_erase(FILE *fp, uint32_t dev, uint32_t addr, size_t len)
default: goto err_disable_write; default: goto err_disable_write;
}; };
fprintf(fp, "addr: %lx; size: %u\r\n", addr, size); fprintf(fp, "addr: %lx; size: %u\n", addr, size);
spi_flash_addr(cmd, addr); spi_flash_addr(cmd, addr);
spi_send_cmd(dev, cmd, 4, NULL, 0); spi_send_cmd(dev, cmd, 4, NULL, 0);
} }

View file

@ -17,27 +17,34 @@
static ssize_t usart_read(void *cookie, char *buf, size_t n) static ssize_t usart_read(void *cookie, char *buf, size_t n)
{ {
uint32_t dev = (uint32_t)cookie; uint32_t dev = (uint32_t)cookie;
size_t nbytes = 0; size_t i;
while (n-- > 0) { for (i = 0; i < n; ++i) {
*buf++ = (char)usart_recv_blocking(dev); buf[i] = (char)usart_recv_blocking(dev);
nbytes++;
if (buf[i] == '\r') {
buf[i] = '\n';
++i;
break;
}
} }
return nbytes; return i;
} }
static ssize_t usart_write(void *cookie, const char *buf, size_t n) static ssize_t usart_write(void *cookie, const char *buf, size_t n)
{ {
uint32_t dev = (uint32_t)cookie; uint32_t dev = (uint32_t)cookie;
size_t nbytes = 0; size_t i;
while (n-- > 0) { for (i = 0; i < n; ++i) {
usart_send_blocking(dev, *buf++); if (buf[i] == '\n')
nbytes++; usart_send_blocking(dev, '\r');
usart_send_blocking(dev, buf[i]);
}; };
return nbytes; return i;
} }
FILE *init_usart(uint32_t dev) FILE *init_usart(uint32_t dev)