diff --git a/tools/atspi-txrx/Makefile b/tools/atspi-txrx/Makefile index e7cec81..43b8cc1 100644 --- a/tools/atspi-txrx/Makefile +++ b/tools/atspi-txrx/Makefile @@ -14,3 +14,5 @@ MAIN = atspi-txrx include ../Makefile.common + +LDLIBS += -lm diff --git a/tools/atspi-txrx/atspi-txrx.c b/tools/atspi-txrx/atspi-txrx.c index 7da32fa..8a5e0b6 100644 --- a/tools/atspi-txrx/atspi-txrx.c +++ b/tools/atspi-txrx/atspi-txrx.c @@ -16,6 +16,8 @@ #include #include #include +#include +#include #include "at86rf230.h" #include "atspi.h" @@ -46,6 +48,9 @@ static double tx_pwr[] = { }; +static volatile int run = 1; + + static struct atspi_dsc *init_txrx(int trim) { struct atspi_dsc *dsc; @@ -83,6 +88,8 @@ static void set_power(struct atspi_dsc *dsc, double power) for (n = 0; n != sizeof(tx_pwr)/sizeof(*tx_pwr)-1; n++) if (tx_pwr[n] <= power) break; + if (fabs(tx_pwr[n]-power) > 0.01) + fprintf(stderr, "TX power %.1f dBm\n", tx_pwr[n]); atspi_reg_write(dsc, REG_PHY_TX_PWR, TX_AUTO_CRC_ON | n); } @@ -103,6 +110,8 @@ static void receive(struct atspi_dsc *dsc) fprintf(stderr, "Ready.\n"); wait_for_interrupt(dsc, IRQ_TRX_END, IRQ_TRX_END | IRQ_RX_START, 10, 0); + if (!run) + return; n = atspi_buf_read(dsc, buf, sizeof(buf)); if (n < 0) @@ -122,7 +131,7 @@ static void receive(struct atspi_dsc *dsc) } -static void transmit(struct atspi_dsc *dsc, const char *msg) +static void transmit(struct atspi_dsc *dsc, const char *msg, int times) { uint8_t buf[MAX_PSDU]; @@ -139,19 +148,27 @@ static void transmit(struct atspi_dsc *dsc, const char *msg) strcpy((void *) buf, msg); atspi_buf_write(dsc, buf, strlen(msg)+2); - /* @@@ should wait for clear channel */ - atspi_reg_write(dsc, REG_TRX_STATE, TRX_CMD_TX_START); + while (run && times--) { + /* @@@ should wait for clear channel */ + atspi_reg_write(dsc, REG_TRX_STATE, TRX_CMD_TX_START); - /* wait up to 10 ms (nominally) */ - wait_for_interrupt(dsc, IRQ_TRX_END, IRQ_TRX_END | IRQ_PLL_LOCK, - 10, 1000); + /* wait up to 10 ms (nominally) */ + wait_for_interrupt(dsc, IRQ_TRX_END, + IRQ_TRX_END | IRQ_PLL_LOCK, 10, 1000); + } +} + + +static void die(int sig) +{ + run = 0; } static void usage(const char *name) { fprintf(stderr, -"usage: %s [-c channel] [-p power] [-t trim] [message]\n" +"usage: %s [-c channel] [-p power] [-t trim] [message [repetitions]]\n" " -c channel channel number, 11 to 26 (default %d)\n" " -p power transmit power, -17.2 to 3.0 dBm (default %.1f)\n" " -t trim trim capacitor, 0 to 15 (default 0)\n" @@ -164,7 +181,7 @@ int main(int argc, char *const *argv) { int channel = DEFAULT_CHANNEL; double power = DEFAULT_POWER; - int trim = 0; + int trim = 0, times = 1; char *end; int c; struct atspi_dsc *dsc; @@ -194,17 +211,24 @@ int main(int argc, char *const *argv) usage(*argv); } + signal(SIGINT, die); + switch (argc-optind) { case 0: dsc = init_txrx(trim); set_channel(dsc, channel); receive(dsc); break; + case 2: + times = strtoul(argv[optind+1], &end, 0); + if (*end) + usage(*argv); + /* fall through */ case 1: dsc = init_txrx(trim); set_channel(dsc, channel); set_power(dsc, power); - transmit(dsc, argv[optind]); + transmit(dsc, argv[optind], times); break; default: usage(*argv); diff --git a/tools/lib/misctxrx.c b/tools/lib/misctxrx.c index daf5a33..7ea8492 100644 --- a/tools/lib/misctxrx.c +++ b/tools/lib/misctxrx.c @@ -15,19 +15,32 @@ #include #include #include +#include #include "at86rf230.h" #include "atspi.h" #include "misctxrx.h" +static volatile int run = 1; + + +static void die(int sig) +{ + run = 0; +} + + uint8_t wait_for_interrupt(struct atspi_dsc *dsc, uint8_t wait_for, uint8_t ignore, int sleep_us, int timeout) { - uint8_t irq, show; + uint8_t irq = 0, show; + void (*old_sig)(int); - while (1) { - while (!atspi_interrupt(dsc)) { + run = 1; + old_sig = signal(SIGINT, die); + while (run) { + while (run && !atspi_interrupt(dsc)) { usleep(sleep_us); if (timeout && !--timeout) return 0; @@ -57,5 +70,8 @@ uint8_t wait_for_interrupt(struct atspi_dsc *dsc, uint8_t wait_for, if (irq & wait_for) break; } + signal(SIGINT, old_sig); + if (!run) + raise(SIGINT); return irq; }