From b0dd5a9abc3605999420424b828fe328010b0fa5 Mon Sep 17 00:00:00 2001 From: "S.J.R. van Schaik" Date: Sat, 11 Mar 2017 00:14:57 +0000 Subject: [PATCH] usart: use \n instead of \r\n --- source/shell.c | 20 ++++++++++---------- source/spi_flash.c | 2 +- source/usart.c | 27 +++++++++++++++++---------- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/source/shell.c b/source/shell.c index accd327..5bf5b3b 100644 --- a/source/shell.c +++ b/source/shell.c @@ -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, "\r\n"); + fprintf(fp, "\n"); } } @@ -92,7 +92,7 @@ static char *prompt(FILE *fp, const char *prefix) s = line; - while ((c = fgetc(fp)) && c != '\r') { + while ((c = fgetc(fp)) && c != '\n') { fputc(c, fp); if (nbytes + 1 >= nalloc_bytes) { @@ -108,7 +108,7 @@ static char *prompt(FILE *fp, const char *prefix) nbytes++; } - fprintf(fp, "\r\n"); + fprintf(fp, "\n"); *s = '\0'; return line; @@ -126,7 +126,7 @@ static void do_probe(FILE *fp, const char *s) (void)s; 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) @@ -143,7 +143,7 @@ static void do_read(FILE *fp, const char *s) if (errno == EINVAL || errno == ERANGE) return; - s += strspn(s, " \r\n"); + s += strspn(s, " \n"); if (strncmp(s, "0x", 2) == 0) s += 2; @@ -181,7 +181,7 @@ static void do_write(FILE *fp, const char *s) if (errno == EINVAL || errno == ERANGE) return; - s += strspn(s, " \r\n"); + s += strspn(s, " \n"); if (strncmp(s, "0x", 2) == 0) s += 2; @@ -198,7 +198,7 @@ static void do_write(FILE *fp, const char *s) while (len) { nbytes = (len > 256) ? 256 : len; parse_hex(fp, buf, nbytes); - fprintf(fp, "\r\n"); + fprintf(fp, "\n"); print_hex_ascii(fp, 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) return; - s += strspn(s, " \r\n"); + s += strspn(s, " \n"); if (strncmp(s, "0x", 2) == 0) s += 2; @@ -247,11 +247,11 @@ static void parse_cmd(FILE *fp, const char *s) char *key; size_t n; - n = strcspn(s, " \r\n"); + n = strcspn(s, " \n"); key = strndup(s, n); args = s + n; - args += strspn(args, " \r\n"); + args += strspn(args, " \n"); for (cmd = cmds; cmd->key; cmd++) { if (strcmp(cmd->key, key) == 0) diff --git a/source/spi_flash.c b/source/spi_flash.c index fc1fab7..b392d5f 100644 --- a/source/spi_flash.c +++ b/source/spi_flash.c @@ -144,7 +144,7 @@ void spi_flash_erase(FILE *fp, uint32_t dev, uint32_t addr, size_t len) 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_send_cmd(dev, cmd, 4, NULL, 0); } diff --git a/source/usart.c b/source/usart.c index eaa426f..c08d5ff 100644 --- a/source/usart.c +++ b/source/usart.c @@ -17,27 +17,34 @@ static ssize_t usart_read(void *cookie, char *buf, size_t n) { uint32_t dev = (uint32_t)cookie; - size_t nbytes = 0; + size_t i; - while (n-- > 0) { - *buf++ = (char)usart_recv_blocking(dev); - nbytes++; + for (i = 0; i < n; ++i) { + buf[i] = (char)usart_recv_blocking(dev); + + 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) { uint32_t dev = (uint32_t)cookie; - size_t nbytes = 0; + size_t i; - while (n-- > 0) { - usart_send_blocking(dev, *buf++); - nbytes++; + for (i = 0; i < n; ++i) { + if (buf[i] == '\n') + usart_send_blocking(dev, '\r'); + + usart_send_blocking(dev, buf[i]); }; - return nbytes; + return i; } FILE *init_usart(uint32_t dev)