Commit b215f3d6 authored by Federico Vaga's avatar Federico Vaga

sw: configure TDC using the input tool

Signed-off-by: Federico Vaga's avatarFederico Vaga <federico.vaga@cern.ch>
parent bf443c54
......@@ -12,6 +12,7 @@
#include "tools-common.h"
char git_version[] = "git version: " GIT_VERSION;
char *prog;
void help(char *name)
{
......@@ -19,10 +20,15 @@ void help(char *name)
name, name);
fprintf(stderr, " options:\n"
" -d <dev> device ID (hexadecimal)\n"
"Configuration:\n"
" -e (0|1) disable or enable the channel\n"
" -t (0|1) disable or enable pulse timestamping\n"
"Timestamp:"
" -c <count> default is 0 and means forever\n"
" -n nonblocking: only empty buffer\n"
" -r raw mode: show hex timestamps\n"
" -f floating point (default): sec.nsec\n");
fprintf(stderr, "Note that the tool does not enforce any default configuration. Therefore, if not explicitly set, it will continue by using the current configuration.\n");
exit(1);
}
......@@ -36,16 +42,106 @@ void dump_input(struct fdelay_time *t, int np, int umode)
}
}
/**
* Configure the input channel
* @param[in] b device token
* @param[in] enable channel enable status: -1 unchanged, 0 disabled, 1 enabled
* @param[in] timestamp pulse timestamping: -1 unchanged, 0 disabled, 1 enable
* @return 0 on success, otherwise -1 and errno is appropriately set
*/
int input_config(struct fdelay_board *b, int enable, int timestamp)
{
int flags;
int err;
flags = fdelay_get_config_tdc(b);
if (flags < 0) {
fprintf(stderr, "%s: failed to get TDC status: %s\n", prog,
fdelay_strerror(errno));
return flags;
}
if (enable >= 0) {
if (enable)
flags &= ~FD_TDCF_DISABLE_INPUT;
else
flags |= FD_TDCF_DISABLE_INPUT;
}
if (timestamp >= 0) {
if (timestamp)
flags &= ~FD_TDCF_DISABLE_TSTAMP;
else
flags |= FD_TDCF_DISABLE_TSTAMP;
}
err = fdelay_set_config_tdc(b, flags);
if (err) {
fprintf(stderr, "%s: failed to set TDC status: %s\n", prog,
fdelay_strerror(errno));
return err;
}
return 0;
}
int input_timestamp(struct fdelay_board *b, int count, int umode, int nonblock)
{
int flags;
int err;
flags = fdelay_get_config_tdc(b);
if (flags < 0)
return flags;
if (flags & (FD_TDCF_DISABLE_INPUT | FD_TDCF_DISABLE_TSTAMP)) {
fputs("Can't show timestamps: channel and timestamping must be enabled\n",
stderr);
errno = ENODEV;
return -1;
}
err = 0;
/* now read pulses, "np" at a time */
while (1) {
struct fdelay_time pdata[16];
int ret, np = 16;
if (count && count < np)
np = count;
ret = fdelay_read(b, pdata, np, nonblock);
if (ret < 0) {
err = ret;
fprintf(stderr, "%s: fdelay_read: %s\n", prog,
strerror(errno));
break;
}
if (!ret)
continue;
dump_input(pdata, ret, umode);
if (nonblock) /* non blocking: nothing more to do */
break;
if (!count) /* no count: forever */
continue;
count -= ret;
if (!count) /* asked that many, we are done */
break;
}
return err;
}
int main(int argc, char **argv)
{
struct fdelay_board *b;
int opt, err, dev = -1;
int nonblock = 0, count = 0;
int umode = TOOLS_UMODE_USER, flags;
int umode = TOOLS_UMODE_USER;
int enable = -1, timestamp = -1;
int config = 0;
prog = argv[0];
/* Standard part of the file (repeated code) */
if (tools_need_help(argc, argv))
......@@ -61,7 +157,7 @@ int main(int argc, char **argv)
}
/* Parse our specific arguments, starting back from argv[1] */
while ((opt = getopt(argc, argv, "d:hc:nrf")) != -1) {
while ((opt = getopt(argc, argv, "d:hc:nrfe:t:")) != -1) {
switch (opt) {
char *rest;
case 'd':
......@@ -95,6 +191,34 @@ int main(int argc, char **argv)
case 'f':
umode = TOOLS_UMODE_FLOAT;
break;
case 'e':
enable = strtol(optarg, &rest, 0);
if (rest && *rest) {
fprintf(stderr, "%s: Not a number \"%s\"\n",
argv[0], optarg);
exit(1);
}
if (enable > 1) {
fprintf(stderr, "%s: invalid enable value \"%s\"\n",
argv[0], optarg);
exit(1);
}
config = 1;
break;
case 't':
timestamp = strtol(optarg, &rest, 0);
if (rest && *rest) {
fprintf(stderr, "%s: Not a number \"%s\"\n",
argv[0], optarg);
exit(1);
}
if (timestamp > 1) {
fprintf(stderr, "%s: invalid enable value \"%s\"\n",
argv[0], optarg);
exit(1);
}
config = 1;
break;
}
}
if (optind != argc)
......@@ -113,48 +237,13 @@ int main(int argc, char **argv)
exit(1);
}
flags = fdelay_get_config_tdc(b);
if (flags < 0) {
err = flags;
goto out;
}
flags &= ~(FD_TDCF_DISABLE_INPUT | FD_TDCF_DISABLE_TSTAMP);
err = fdelay_set_config_tdc(b, flags);
if (err) {
fprintf(stderr, "%s: failed to configure TDC: %s\n", argv[0],
fdelay_strerror(errno));
goto out;
}
/* now read pulses, "np" at a time */
while (1) {
struct fdelay_time pdata[16];
int ret, np = 16;
if (count && count < np)
np = count;
ret = fdelay_read(b, pdata, np, nonblock);
if (ret < 0) {
err = ret;
fprintf(stderr, "%s: fdelay_read: %s\n", argv[0],
strerror(errno));
break;
}
if (!ret)
continue;
dump_input(pdata, ret, umode);
if (nonblock) /* non blocking: nothing more to do */
break;
if (!count) /* no count: forever */
continue;
count -= ret;
if (!count) /* asked that many, we are done */
break;
if (config) {
err = input_config(b, enable, timestamp);
if (err)
goto out;
}
err = input_timestamp(b, count, umode, nonblock);
out:
fdelay_close(b);
fdelay_exit();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment