2011-06-03 21:25:57 +03:00
|
|
|
/*
|
|
|
|
* atrf-gpio/atrf-gpio.c - ATBEN/ATUSB GPIO test
|
|
|
|
*
|
|
|
|
* Written 2011 by Werner Almesberger
|
|
|
|
* Copyright 2011 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 <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2011-06-07 01:33:11 +03:00
|
|
|
#include <termios.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2011-06-03 21:25:57 +03:00
|
|
|
|
|
|
|
#include "at86rf230.h"
|
|
|
|
#include "atrf.h"
|
|
|
|
|
|
|
|
#include "atrf-gpio.h"
|
|
|
|
|
|
|
|
|
2011-06-07 01:33:11 +03:00
|
|
|
#define DEFAULT_DELAY_MS 10
|
|
|
|
|
|
|
|
|
|
|
|
/* ----- Board-specific drivers -------------------------------------------- */
|
|
|
|
|
|
|
|
|
2011-06-03 21:25:57 +03:00
|
|
|
static void atben(struct atrf_dsc *dsc, const char *pattern, const char *next)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_ATBEN
|
|
|
|
do_atben(dsc, pattern, next);
|
|
|
|
#else
|
|
|
|
fprintf(stderr, "not compiled with ATBEN support\n");
|
|
|
|
exit(1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void atusb(struct atrf_dsc *dsc, const char *pattern, const char *next)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_ATUSB
|
|
|
|
do_atusb(dsc, pattern, next);
|
|
|
|
#else
|
|
|
|
fprintf(stderr, "not compiled with ATUSB support\n");
|
|
|
|
exit(1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-07 01:33:11 +03:00
|
|
|
/* ----- Commands ---------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2011-06-07 01:38:42 +03:00
|
|
|
static void bad_command(const char *arg)
|
2011-06-03 21:25:57 +03:00
|
|
|
{
|
2011-06-07 01:38:42 +03:00
|
|
|
fprintf(stderr, "invalid command \"%s\"\n", arg);
|
2011-06-03 21:25:57 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-07 01:38:42 +03:00
|
|
|
static int command(struct atrf_dsc *dsc, const char *arg, int doit)
|
2011-06-03 21:25:57 +03:00
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
char *end;
|
2011-06-06 06:27:41 +03:00
|
|
|
unsigned long reg, value, mask = 0xff;
|
2011-06-03 21:25:57 +03:00
|
|
|
uint8_t got;
|
|
|
|
|
2011-06-06 06:27:41 +03:00
|
|
|
if (!strcmp(arg, "delay")) {
|
|
|
|
if (doit)
|
2011-06-07 01:33:11 +03:00
|
|
|
usleep(DEFAULT_DELAY_MS*1000);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!strncmp(arg, "delay=", 6)) {
|
|
|
|
value = strtoul(arg+6, &end, 0);
|
|
|
|
if (!value || *end)
|
2011-06-07 01:38:42 +03:00
|
|
|
bad_command(arg);
|
2011-06-07 01:33:11 +03:00
|
|
|
if (doit)
|
|
|
|
usleep(value*1000);
|
2011-06-06 06:27:41 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!strcmp(arg, "frame")) {
|
|
|
|
if (doit)
|
|
|
|
atrf_buf_write(dsc, "", 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!strcmp(arg, "reset")) {
|
|
|
|
if (doit)
|
|
|
|
atrf_reset_rf(dsc);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!strcmp(arg, "slp_tr")) {
|
|
|
|
if (doit)
|
|
|
|
atrf_slp_tr(dsc, 1, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-06-03 21:25:57 +03:00
|
|
|
p = strchr(arg, '=');
|
|
|
|
if (!p)
|
|
|
|
p = strchr(arg, ':');
|
|
|
|
if (!p)
|
|
|
|
p = strchr(arg, '!');
|
|
|
|
if (!p)
|
|
|
|
p = strchr(arg, '/');
|
|
|
|
if (!p)
|
|
|
|
return 0;
|
|
|
|
reg = strtoul(arg, &end, 0);
|
|
|
|
if (end != p || reg > 0xff)
|
2011-06-07 01:38:42 +03:00
|
|
|
bad_command(arg);
|
2011-06-03 21:25:57 +03:00
|
|
|
value = strtoul(p+1, &end, 0);
|
2011-06-06 06:27:41 +03:00
|
|
|
if (value > 0xff)
|
2011-06-07 01:38:42 +03:00
|
|
|
bad_command(arg);
|
2011-06-06 06:27:41 +03:00
|
|
|
if (*end) {
|
|
|
|
if (*p != ':')
|
2011-06-07 01:38:42 +03:00
|
|
|
bad_command(arg);
|
2011-06-06 06:27:41 +03:00
|
|
|
if (*end != '/')
|
2011-06-07 01:38:42 +03:00
|
|
|
bad_command(arg);
|
2011-06-06 06:27:41 +03:00
|
|
|
mask = strtoul(end+1, &end, 0);
|
|
|
|
if (*end || mask > 0xff)
|
2011-06-07 01:38:42 +03:00
|
|
|
bad_command(arg);
|
2011-06-06 06:27:41 +03:00
|
|
|
}
|
|
|
|
|
2011-06-03 21:25:57 +03:00
|
|
|
if (!doit)
|
|
|
|
return 1;
|
2011-06-06 06:27:41 +03:00
|
|
|
|
2011-06-03 21:25:57 +03:00
|
|
|
switch (*p) {
|
|
|
|
case '=':
|
|
|
|
atrf_reg_write(dsc, reg, value);
|
|
|
|
break;
|
|
|
|
case ':':
|
|
|
|
got = atrf_reg_read(dsc, reg);
|
2011-06-06 06:27:41 +03:00
|
|
|
if (end != p+1 && ((got ^ value) & mask)) {
|
2011-06-03 21:25:57 +03:00
|
|
|
fprintf(stderr,
|
2011-06-06 06:27:41 +03:00
|
|
|
"register 0x%02lx: got 0x%02x expected "
|
|
|
|
"0x%02lx/0x%02lx\n", reg, got, value, mask);
|
2011-06-03 21:25:57 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '!':
|
|
|
|
atrf_sram_write(dsc, reg, value);
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
got = atrf_sram_read(dsc, reg);
|
|
|
|
if (got != value) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"got 0x%02x expected 0x%02lx\n", got, value);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-07 01:33:11 +03:00
|
|
|
/* ----- Pass/Fail/Quit input ---------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
static struct termios old_term;
|
|
|
|
|
|
|
|
|
|
|
|
static void restore_term(void)
|
|
|
|
{
|
|
|
|
if (tcsetattr(0, TCSAFLUSH, &old_term) < 0)
|
|
|
|
perror("tcsetattr");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void raw(void)
|
|
|
|
{
|
|
|
|
struct termios term;
|
|
|
|
|
|
|
|
if (tcgetattr(0, &old_term) < 0) {
|
|
|
|
perror("tcgetattr");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
term = old_term;
|
|
|
|
cfmakeraw(&term);
|
|
|
|
if (tcsetattr(0, TCSAFLUSH, &term) < 0) {
|
|
|
|
perror("tcsetattr");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
atexit(restore_term);
|
|
|
|
if (fcntl(0, F_SETFL, O_NONBLOCK) < 0) {
|
|
|
|
perror("fcntl");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void pass_fail(void)
|
|
|
|
{
|
|
|
|
ssize_t got;
|
|
|
|
char ch;
|
|
|
|
|
|
|
|
got = read(0, &ch, 1);
|
|
|
|
if (got < 0) {
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
return;
|
|
|
|
perror("read");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
switch (ch) {
|
|
|
|
case 'P':
|
|
|
|
case 'p':
|
|
|
|
exit(0);
|
|
|
|
case 'F':
|
|
|
|
case 'f':
|
|
|
|
case 'Q':
|
|
|
|
case 'q':
|
|
|
|
case 3: /* Ctrl-C */
|
|
|
|
exit(1);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----- Command line processing and main loop ----------------------------- */
|
|
|
|
|
|
|
|
|
2011-06-03 21:25:57 +03:00
|
|
|
static void usage(const char *name)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2011-06-07 01:42:31 +03:00
|
|
|
"usage: %s [-c] [-d driver[:arg]] [-p] command|pattern ...\n\n"
|
2011-06-07 01:33:11 +03:00
|
|
|
" -c cycle, waiting for Pass/Fail/Quit input\n"
|
2011-06-07 01:42:31 +03:00
|
|
|
" -d driver[:arg] use the specified driver (default: %s)\n"
|
|
|
|
" -p stay in P_ON state instead of entering TRX_OFF\n\n"
|
2011-06-03 21:25:57 +03:00
|
|
|
" command is one of:\n"
|
|
|
|
" reg=value set transceiver register\n"
|
2011-06-06 06:27:41 +03:00
|
|
|
" reg:[value[/mask]]\n"
|
|
|
|
" read transceiver register and (optionally) verify value\n"
|
2011-06-03 21:25:57 +03:00
|
|
|
" addr!value write one byte to SRAM\n"
|
|
|
|
" addr/value read and verify one byte from SRAM\n"
|
2011-06-07 01:33:11 +03:00
|
|
|
" delay[=ms] wait the specified number of milliseconds (default: %d ms)\n"
|
2011-06-06 06:27:41 +03:00
|
|
|
" frame write a one-byte frame to the frame buffer\n"
|
|
|
|
" reset reset the transceiver\n"
|
|
|
|
" slp_tr pulse SLP_TR\n"
|
2011-06-03 21:25:57 +03:00
|
|
|
" #... comment\n\n"
|
|
|
|
" pattern is a sequence of the following characters:\n"
|
2011-06-06 03:47:02 +03:00
|
|
|
" 0 = output a strong 0 1 = output a strong 1\n"
|
|
|
|
" L = pull up, expect to read 0 H = pull up, expect to read 1\n"
|
|
|
|
" l/o = no pull-up, expect to read 0 h = no pull-up, expect to read 1\n"
|
|
|
|
" Z = pull up, don't read z = no pull-up, don't read\n"
|
|
|
|
" x = don't care . = separator\n"
|
2011-06-07 01:33:11 +03:00
|
|
|
, name, atrf_default_driver_name(), DEFAULT_DELAY_MS);
|
2011-06-03 21:25:57 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 0 strong 0 out
|
|
|
|
* 1 strong 1 out
|
|
|
|
* H pull-up, read 1
|
|
|
|
* L pull-up, read 0
|
|
|
|
* h no pull-up, read 1
|
2011-06-06 03:47:02 +03:00
|
|
|
* l/o no pull-up, read 0
|
2011-06-03 21:25:57 +03:00
|
|
|
* Z pull-up, don't read
|
|
|
|
* z no pull-up, don't read
|
|
|
|
* x don't care
|
|
|
|
* . separator
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *const *argv)
|
|
|
|
{
|
|
|
|
const char *driver = NULL;
|
|
|
|
struct atrf_dsc *dsc;
|
2011-06-07 01:33:11 +03:00
|
|
|
int cycle = 0;
|
2011-06-03 21:25:57 +03:00
|
|
|
int trx_off = 1;
|
|
|
|
int c, i;
|
|
|
|
const char *s;
|
|
|
|
|
2011-06-07 01:33:11 +03:00
|
|
|
while ((c = getopt(argc, argv, "cd:p")) != EOF)
|
2011-06-03 21:25:57 +03:00
|
|
|
switch (c) {
|
2011-06-07 01:33:11 +03:00
|
|
|
case 'c':
|
|
|
|
cycle = 1;
|
|
|
|
break;
|
2011-06-03 21:25:57 +03:00
|
|
|
case 'd':
|
|
|
|
driver = optarg;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
trx_off = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(*argv);
|
|
|
|
}
|
|
|
|
|
2011-06-07 01:42:31 +03:00
|
|
|
if (optind == argc)
|
|
|
|
usage(*argv);
|
|
|
|
|
2011-06-03 21:25:57 +03:00
|
|
|
for (i = optind; i != argc; i++) {
|
|
|
|
if (*argv[i] == '#')
|
|
|
|
continue;
|
2011-06-07 01:38:42 +03:00
|
|
|
if (command(NULL, argv[i], 0))
|
2011-06-03 21:25:57 +03:00
|
|
|
continue;
|
|
|
|
for (s = argv[i]; *s; s++)
|
2011-06-06 03:47:02 +03:00
|
|
|
if (!strchr("01HLhloZzx.", *s))
|
2011-06-03 21:25:57 +03:00
|
|
|
fprintf(stderr,
|
|
|
|
"invalid configuration '%c' in \"%s\"\n",
|
|
|
|
*s, argv[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
dsc = atrf_open(driver);
|
|
|
|
if (!dsc)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
atrf_reset_rf(dsc);
|
|
|
|
|
|
|
|
if (trx_off) {
|
|
|
|
atrf_reg_write(dsc, REG_TRX_STATE, TRX_CMD_TRX_OFF);
|
|
|
|
do usleep(100);
|
|
|
|
while ((atrf_reg_read(dsc, REG_TRX_STATUS) & TRX_STATUS_MASK)
|
|
|
|
!= TRX_STATUS_TRX_OFF);
|
|
|
|
}
|
|
|
|
|
2011-06-07 01:33:11 +03:00
|
|
|
if (cycle)
|
|
|
|
raw();
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
for (i = optind; i != argc; i++) {
|
|
|
|
if (*argv[i] == '#')
|
|
|
|
continue;
|
2011-06-07 01:38:42 +03:00
|
|
|
if (command(dsc, argv[i], 1))
|
2011-06-07 01:33:11 +03:00
|
|
|
continue;
|
|
|
|
if (atrf_usb_handle(dsc))
|
|
|
|
atusb(dsc, argv[i], argv[i+1]);
|
|
|
|
else
|
|
|
|
atben(dsc, argv[i], argv[i+1]);
|
|
|
|
}
|
|
|
|
if (cycle)
|
|
|
|
pass_fail();
|
2011-06-03 21:25:57 +03:00
|
|
|
else
|
2011-06-07 01:33:11 +03:00
|
|
|
break;
|
2011-06-03 21:25:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// atrf_close(dsc);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|