2013-01-06 13:57:12 +02:00
|
|
|
/*
|
|
|
|
* ubbctl.c - Set and query UBB signals
|
|
|
|
*
|
2014-03-06 16:34:28 +02:00
|
|
|
* Written 2013-2014 by Werner Almesberger
|
|
|
|
* Copyright 2013-2014 Werner Almesberger
|
2013-01-06 13:57:12 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2013-01-04 10:40:19 +02:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
2013-01-06 14:54:54 +02:00
|
|
|
#include <stdlib.h>
|
2013-01-04 10:40:19 +02:00
|
|
|
#include <stdio.h>
|
2013-01-06 14:54:54 +02:00
|
|
|
#include <unistd.h>
|
2013-01-06 16:30:48 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h> /* for strcasecmp, strncasecmp */
|
2013-01-04 10:40:19 +02:00
|
|
|
|
|
|
|
#include <ubb/ubb.h>
|
|
|
|
|
|
|
|
|
|
|
|
static struct pin {
|
|
|
|
const char *name;
|
|
|
|
uint32_t mask;
|
|
|
|
} pins[] = {
|
|
|
|
{ "nPWR", UBB_nPWR },
|
|
|
|
{ "DAT2", UBB_DAT2 },
|
|
|
|
{ "DAT3", UBB_DAT3 },
|
|
|
|
{ "CMD", UBB_CMD },
|
|
|
|
{ "CLK", UBB_CLK },
|
|
|
|
{ "DAT0", UBB_DAT0 },
|
|
|
|
{ "DAT1", UBB_DAT1 },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-01-06 14:07:16 +02:00
|
|
|
static void show_pins(void)
|
2013-01-04 10:40:19 +02:00
|
|
|
{
|
|
|
|
const struct pin *p;
|
2013-01-06 15:00:52 +02:00
|
|
|
int pin, set;
|
2013-01-04 10:40:19 +02:00
|
|
|
|
|
|
|
for (p = pins; p->name; p++) {
|
|
|
|
printf("%s%s=", p == pins ? "" : " ", p->name);
|
2013-01-06 15:00:52 +02:00
|
|
|
pin = PIN(p->mask);
|
|
|
|
if (PDFUN & p->mask) {
|
2013-01-06 15:47:31 +02:00
|
|
|
putchar('F');
|
2014-03-06 16:34:28 +02:00
|
|
|
if (PDSEL & p->mask)
|
|
|
|
putchar('b');
|
|
|
|
} else if (PDSEL & p->mask) {
|
|
|
|
putchar('I');
|
|
|
|
if (PDTRG & p->mask) {
|
|
|
|
if (PDDIR & p->mask)
|
|
|
|
putchar('r');
|
|
|
|
else
|
|
|
|
putchar('f');
|
|
|
|
} else {
|
|
|
|
if (PDDIR & p->mask)
|
|
|
|
putchar('h');
|
|
|
|
else
|
|
|
|
putchar('l');
|
|
|
|
}
|
|
|
|
if (!(PDPULL & p->mask))
|
|
|
|
putchar('R');
|
2013-01-06 15:00:52 +02:00
|
|
|
} else if (PDDIR & p->mask) {
|
|
|
|
set = !!(PDDAT & p->mask);
|
|
|
|
if (pin != set)
|
|
|
|
printf("%d!", set);
|
|
|
|
|
|
|
|
} else {
|
2013-01-04 10:40:19 +02:00
|
|
|
putchar(PDPULL & p->mask ? 'Z' : 'R');
|
2013-01-06 15:00:52 +02:00
|
|
|
}
|
|
|
|
printf("%d", pin);
|
2013-01-04 10:40:19 +02:00
|
|
|
}
|
2013-01-06 14:07:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-06 16:30:48 +02:00
|
|
|
/*
|
|
|
|
* The order of the IO operations below is important to avoid glitches.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int setup_pin(const char *s, int doit)
|
|
|
|
{
|
2014-03-06 16:34:28 +02:00
|
|
|
static const char trigger[] = "lhfrLHFR";
|
2013-01-06 16:30:48 +02:00
|
|
|
const struct pin *p;
|
2014-03-06 16:34:28 +02:00
|
|
|
const char *eq, *t;
|
|
|
|
uint8_t trig;
|
2013-01-06 16:30:48 +02:00
|
|
|
|
2013-01-06 16:38:01 +02:00
|
|
|
if (!strcasecmp(s, "on"))
|
|
|
|
s = "nPWR=0";
|
|
|
|
else if (!strcasecmp(s, "off"))
|
|
|
|
s = "nPWR=1";
|
|
|
|
|
2013-01-06 16:30:48 +02:00
|
|
|
eq = strchr(s, '=');
|
|
|
|
if (!eq)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (p = pins; p->name; p++)
|
|
|
|
if (strlen(p->name) == eq-s && !strncasecmp(p->name, s, eq-s))
|
|
|
|
break;
|
|
|
|
if (!p->name)
|
|
|
|
return 0;
|
|
|
|
|
2014-03-06 16:34:28 +02:00
|
|
|
if (!strcasecmp(eq+1, "f") || !strcasecmp(eq+1, "fa")) {
|
|
|
|
if (doit) {
|
|
|
|
PDFUNS = p->mask;
|
|
|
|
PDSELC = p->mask;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!strcasecmp(eq+1, "fb")) {
|
|
|
|
if (doit) {
|
2013-01-06 16:30:48 +02:00
|
|
|
PDFUNS = p->mask;
|
2014-03-06 16:34:28 +02:00
|
|
|
PDSELS = p->mask;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!strcmp(eq+1, "0")) {
|
2013-01-06 16:30:48 +02:00
|
|
|
if (doit) {
|
|
|
|
PDDATC = p->mask;
|
|
|
|
PDDIRS = p->mask;
|
|
|
|
PDFUNC = p->mask;
|
|
|
|
}
|
2014-03-06 16:34:28 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!strcmp(eq+1, "1")) {
|
2013-01-06 16:30:48 +02:00
|
|
|
if (doit) {
|
|
|
|
PDDATS = p->mask;
|
|
|
|
PDDIRS = p->mask;
|
|
|
|
PDFUNC = p->mask;
|
|
|
|
}
|
2014-03-06 16:34:28 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!strcasecmp(eq+1, "r")) {
|
2013-01-06 16:30:48 +02:00
|
|
|
if (doit) {
|
|
|
|
PDPULLC = p->mask;
|
|
|
|
PDDIRC = p->mask;
|
|
|
|
PDFUNC = p->mask;
|
2014-03-06 16:34:28 +02:00
|
|
|
PDSELC = p->mask;
|
2013-01-06 16:30:48 +02:00
|
|
|
}
|
2014-03-06 16:34:28 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!strcasecmp(eq+1, "z")) {
|
2013-01-06 16:30:48 +02:00
|
|
|
if (doit) {
|
|
|
|
PDPULLS = p->mask;
|
|
|
|
PDDIRC = p->mask;
|
|
|
|
PDFUNC = p->mask;
|
2014-03-06 16:34:28 +02:00
|
|
|
PDSELC = p->mask;
|
2013-01-06 16:30:48 +02:00
|
|
|
}
|
2014-03-06 16:34:28 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (eq[1] != 'i' && eq[1] != 'I')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
t = strchr(trigger, eq[2]);
|
|
|
|
if (!t || !*t)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!eq[3] || eq[3] == 'z' || eq[3] == 'Z') {
|
|
|
|
if (doit)
|
|
|
|
PDPULLS = p->mask;
|
|
|
|
} else if (eq[3] == 'r' || eq[3] == 'R') {
|
|
|
|
if (doit)
|
|
|
|
PDPULLC = p->mask;
|
2013-01-06 16:30:48 +02:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-03-06 16:34:28 +02:00
|
|
|
|
|
|
|
if (!doit)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
PDFUNC = p->mask;
|
|
|
|
PDSELS = p->mask;
|
|
|
|
trig = (t-trigger) & 3;
|
|
|
|
if (trig & 1)
|
|
|
|
PDDIRS = p->mask;
|
|
|
|
else
|
|
|
|
PDDIRC = p->mask;
|
|
|
|
if (trig & 2)
|
|
|
|
PDTRGS = p->mask;
|
|
|
|
else
|
|
|
|
PDTRGC = p->mask;
|
|
|
|
|
2013-01-06 16:30:48 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-06 14:54:54 +02:00
|
|
|
static void usage(const char *name)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2013-01-06 18:24:56 +02:00
|
|
|
"usage: %s [-c]\n"
|
2013-01-06 16:38:01 +02:00
|
|
|
" %s name=value|action ...\n\n"
|
2013-01-06 18:24:56 +02:00
|
|
|
" -c continously update the pin status (until user interrupts)\n\n"
|
2013-01-06 16:30:48 +02:00
|
|
|
"Names: nPWR, CMD, CLK, DAT0, DAT1, DAT2, DAT3\n"
|
2014-03-06 16:34:28 +02:00
|
|
|
"Values: F, Fa, Fb, 0, 1, Z, R, Ir[R|Z], If[R|Z], Ih[R|Z], Il[R|Z]\n"
|
2013-01-06 16:38:01 +02:00
|
|
|
"Actions: ON, OFF\n"
|
2013-01-06 16:30:48 +02:00
|
|
|
, name, name);
|
2013-01-06 14:54:54 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2013-01-06 16:30:48 +02:00
|
|
|
|
2013-01-06 14:07:16 +02:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2013-01-06 18:24:56 +02:00
|
|
|
int continuous = 0;
|
2013-01-06 16:30:48 +02:00
|
|
|
int c, i;
|
2013-01-06 14:54:54 +02:00
|
|
|
|
2013-01-06 18:24:56 +02:00
|
|
|
while ((c = getopt(argc, argv, "c")) != EOF)
|
2013-01-06 14:54:54 +02:00
|
|
|
switch (c) {
|
2013-01-06 18:24:56 +02:00
|
|
|
case 'c':
|
|
|
|
continuous = 1;
|
|
|
|
break;
|
2013-01-06 14:54:54 +02:00
|
|
|
default:
|
|
|
|
usage(*argv);
|
|
|
|
}
|
|
|
|
|
2013-01-06 18:24:56 +02:00
|
|
|
if (argc != optind && continuous)
|
|
|
|
usage(*argv);
|
|
|
|
|
2013-01-06 16:30:48 +02:00
|
|
|
for (i = optind; i != argc; i++)
|
|
|
|
if (!setup_pin(argv[i], 0))
|
|
|
|
usage(*argv);
|
2013-01-06 14:54:54 +02:00
|
|
|
|
2013-01-06 14:07:16 +02:00
|
|
|
ubb_open(UBB_ALL);
|
2013-01-06 16:30:48 +02:00
|
|
|
if (argc == optind) {
|
2013-01-06 18:24:56 +02:00
|
|
|
if (continuous) {
|
|
|
|
while (1) {
|
|
|
|
show_pins();
|
|
|
|
printf("%*s\r", sizeof(pins)/sizeof(*pins)*2,
|
|
|
|
"");
|
|
|
|
fflush(stdout);
|
|
|
|
usleep(200*1000);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
show_pins();
|
|
|
|
putchar('\n');
|
|
|
|
}
|
2013-01-06 16:30:48 +02:00
|
|
|
} else {
|
|
|
|
for (i = optind; i != argc; i++)
|
|
|
|
setup_pin(argv[i], 1);
|
|
|
|
}
|
2013-01-04 10:40:19 +02:00
|
|
|
return 0;
|
|
|
|
}
|