diff --git a/swuart/Makefile b/swuart/Makefile index 4eec199..ce24083 100644 --- a/swuart/Makefile +++ b/swuart/Makefile @@ -4,7 +4,7 @@ LDFLAGS = -static LDLIBS = -L../libubb -lubb MAIN = swuart -OBJS = swuart.o +OBJS = test.o swuart.o .PHONY: all clean spotless diff --git a/swuart/swuart.c b/swuart/swuart.c index b5f380c..23dfd25 100644 --- a/swuart/swuart.c +++ b/swuart/swuart.c @@ -12,17 +12,13 @@ #include -#include -#include -#include #include #include #include +#include "swuart.h" -#define RX UBB_DAT0 -#define TX UBB_DAT1 #define TIMER 7 @@ -121,11 +117,7 @@ static void enable_interrupts(void) /* ----- Transmit a block -------------------------------------------------- */ -struct rx_err { - unsigned glitch; /* unstable start bit */ - unsigned framing; /* stop bit wasn't "1" */ - unsigned overflow; /* buffer overflow */ -} rx_err; +static struct swuart_err errors; static int trx(uint8_t *out, int out_size, uint8_t *in, int in_size, @@ -182,11 +174,11 @@ full: rx = (rx >> 1) | (PIN(rx_mask) ? 0x200 : 0); if (!--rx_bits) { if (rx & 1) { - rx_err.glitch++; + errors.glitch++; } else if (!(rx & 0x200)) { - rx_err.framing++; + errors.framing++; } else if (in_len == in_size) { - rx_err.overflow++; + errors.overflow++; } else { *in++ = rx >> 1; in_len++; @@ -217,6 +209,25 @@ int swuart_trx(void *out, int out_size, void *in, int in_size, } +/* ----- Access error counters --------------------------------------------- */ + + +unsigned swuart_get_errors(struct swuart_err *res) +{ + unsigned sum = errors.glitch+errors.framing+errors.overflow; + + if (res) + *res = errors; + return sum; +} + + +void swuart_clear_errors(void) +{ + memset(&errors, 0, sizeof(errors)); +} + + /* ----- Open/close the software UART -------------------------------------- */ @@ -242,35 +253,3 @@ void swuart_close(void) { ubb_close(0); } - - -/* ----- Main -------------------------------------------------------------- */ - - -static void at_exit(void) -{ - swuart_close(); -} - - -int main(int argc, char **argv) -{ - uint8_t buf[40]; - int got, i; - - swuart_open(TX, RX, atoi(argv[1])); - atexit(at_exit); - while (1) { - got = swuart_trx(argv[2], strlen(argv[2]), buf, sizeof(buf), - 10000, 100); - printf("%d (%d %d %d): ", got, - rx_err.glitch, rx_err.framing, rx_err.overflow); - for (i = 0; i != got; i++) - if (buf[i] >= ' ' && buf[i] <= '~') - printf("%c", buf[i]); - else - printf("\\%02o", buf[i]); - printf("\n"); - usleep(100); - } -} diff --git a/swuart/swuart.h b/swuart/swuart.h new file mode 100644 index 0000000..fd126ef --- /dev/null +++ b/swuart/swuart.h @@ -0,0 +1,35 @@ +/* + * include/ubb/swuart.h - Software-implemented UART for UBB + * + * Written 2012 by Werner Almesberger + * Copyright 2012 Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + +#ifndef UBB_SWUART_H +#define UBB_SWUART_H + +#include + + +struct swuart_err { + unsigned glitch; /* unstable start bit */ + unsigned framing; /* stop bit wasn't "1" */ + unsigned overflow; /* buffer overflow */ +}; + + +int swuart_trx(void *out, int out_size, void *in, int in_size, + int wait_bits, int idle_bits); + +unsigned swuart_get_errors(struct swuart_err *res); +void swuart_clear_errors(void); + +int swuart_open(uint32_t tx, uint32_t rx, int bps); +void swuart_close(void); + +#endif /* !UBB_SWUART_H */ diff --git a/swuart/test.c b/swuart/test.c new file mode 100644 index 0000000..18c8b2e --- /dev/null +++ b/swuart/test.c @@ -0,0 +1,57 @@ + +/* + * swuart/test.c - Software-implemented UART for UBB + * + * Written 2012 by Werner Almesberger + * Copyright 2012 Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + + +#include +#include +#include +#include + +#include + +#include "swuart.h" + + +#define RX UBB_DAT0 +#define TX UBB_DAT1 + + +static void at_exit(void) +{ + swuart_close(); +} + + +int main(int argc, char **argv) +{ + uint8_t buf[40]; + struct swuart_err err; + int got, i; + + swuart_open(TX, RX, atoi(argv[1])); + atexit(at_exit); + while (1) { + got = swuart_trx(argv[2], strlen(argv[2]), buf, sizeof(buf), + 10000, 100); + swuart_get_errors(&err); + printf("%d (%d %d %d): ", got, + err.glitch, err.framing, err.overflow); + for (i = 0; i != got; i++) + if (buf[i] >= ' ' && buf[i] <= '~') + printf("%c", buf[i]); + else + printf("\\%02o", buf[i]); + printf("\n"); + usleep(100); + } +}