Commit dc89bcb1 authored by Federico Vaga's avatar Federico Vaga

lib: channel range is now 0, 4

Signed-off-by: Federico Vaga's avatarFederico Vaga <federico.vaga@cern.ch>
parent 8ed2f344
...@@ -280,7 +280,7 @@ int fmctdc_set_termination(struct fmctdc_board *userb, unsigned int channel, ...@@ -280,7 +280,7 @@ int fmctdc_set_termination(struct fmctdc_board *userb, unsigned int channel,
if (channel >= FMCTDC_NUM_CHANNELS) if (channel >= FMCTDC_NUM_CHANNELS)
return -EINVAL; return -EINVAL;
snprintf(attr, sizeof(attr), "ft-ch%d/termination", channel); snprintf(attr, sizeof(attr), "ft-ch%d/termination", channel + 1);
val = on ? 1 : 0; val = on ? 1 : 0;
return fmctdc_sysfs_set(b, attr, &val); return fmctdc_sysfs_set(b, attr, &val);
...@@ -304,7 +304,7 @@ int fmctdc_get_termination(struct fmctdc_board *userb, unsigned int channel) ...@@ -304,7 +304,7 @@ int fmctdc_get_termination(struct fmctdc_board *userb, unsigned int channel)
if (channel >= FMCTDC_NUM_CHANNELS) if (channel >= FMCTDC_NUM_CHANNELS)
return -EINVAL; return -EINVAL;
snprintf(attr, sizeof(attr), "ft-ch%d/termination", channel); snprintf(attr, sizeof(attr), "ft-ch%d/termination", channel + 1);
ret = fmctdc_sysfs_get(b, attr, &val); ret = fmctdc_sysfs_get(b, attr, &val);
if (ret) if (ret)
...@@ -350,18 +350,19 @@ int fmctdc_set_acquisition(struct fmctdc_board *userb, int on) ...@@ -350,18 +350,19 @@ int fmctdc_set_acquisition(struct fmctdc_board *userb, int on)
/** /**
* It opens the zio control channel of a TDC board * It opens the zio control channel of a TDC board
* @param[in] b TDC board instance token * @param[in] b TDC board instance token
* @param[in] channel channel to open * @param[in] channel channel to open [0, 4]
* @return a file descriptor, otherwise -1 and errno is set appropriately * @return a file descriptor, otherwise -1 and errno is set appropriately
*/ */
static int __fmctdc_open_channel(struct __fmctdc_board *b, unsigned int channel) static int __fmctdc_open_channel(struct __fmctdc_board *b, unsigned int channel)
{ {
char fname[128]; char fname[128];
if (b->fdc[channel - 1] <= 0) {
if (b->fdc[channel] <= 0) {
snprintf(fname, sizeof(fname), "%s-%d-0-ctrl", b->devbase, snprintf(fname, sizeof(fname), "%s-%d-0-ctrl", b->devbase,
channel - 1); channel);
b->fdc[channel - 1] = open(fname, O_RDONLY | O_NONBLOCK); b->fdc[channel] = open(fname, O_RDONLY | O_NONBLOCK);
} }
return b->fdc[channel - 1]; return b->fdc[channel];
} }
...@@ -374,6 +375,7 @@ static int __fmctdc_open_channel(struct __fmctdc_board *b, unsigned int channel) ...@@ -374,6 +375,7 @@ static int __fmctdc_open_channel(struct __fmctdc_board *b, unsigned int channel)
int fmctdc_fileno_channel(struct fmctdc_board *userb, unsigned int channel) int fmctdc_fileno_channel(struct fmctdc_board *userb, unsigned int channel)
{ {
__define_board(b, userb); __define_board(b, userb);
return __fmctdc_open_channel(b, channel); return __fmctdc_open_channel(b, channel);
} }
...@@ -381,7 +383,7 @@ int fmctdc_fileno_channel(struct fmctdc_board *userb, unsigned int channel) ...@@ -381,7 +383,7 @@ int fmctdc_fileno_channel(struct fmctdc_board *userb, unsigned int channel)
/** /**
* this "read" behaves like the system call and obeys O_NONBLOCK * this "read" behaves like the system call and obeys O_NONBLOCK
* @param[in] userb TDC board instance token * @param[in] userb TDC board instance token
* @param[in] channel channel to use * @param[in] channel channel to use [0, 4]
* @param[out] t array of time-stamps * @param[out] t array of time-stamps
* @param[in] n number of elements to save in the array * @param[in] n number of elements to save in the array
* @param[in] flags tune the behaviour of the function. * @param[in] flags tune the behaviour of the function.
...@@ -524,6 +526,7 @@ int fmctdc_get_time(struct fmctdc_board *userb, struct fmctdc_time *t) ...@@ -524,6 +526,7 @@ int fmctdc_get_time(struct fmctdc_board *userb, struct fmctdc_time *t)
int fmctdc_set_host_time(struct fmctdc_board *userb) int fmctdc_set_host_time(struct fmctdc_board *userb)
{ {
__define_board(b, userb); __define_board(b, userb);
return __fmctdc_command(b, FT_CMD_SET_HOST_TIME); return __fmctdc_command(b, FT_CMD_SET_HOST_TIME);
} }
...@@ -537,6 +540,7 @@ int fmctdc_set_host_time(struct fmctdc_board *userb) ...@@ -537,6 +540,7 @@ int fmctdc_set_host_time(struct fmctdc_board *userb)
int fmctdc_wr_mode(struct fmctdc_board *userb, int on) int fmctdc_wr_mode(struct fmctdc_board *userb, int on)
{ {
__define_board(b, userb); __define_board(b, userb);
if (on) if (on)
__fmctdc_command(b, FT_CMD_WR_ENABLE); __fmctdc_command(b, FT_CMD_WR_ENABLE);
else else
...@@ -554,6 +558,7 @@ int fmctdc_wr_mode(struct fmctdc_board *userb, int on) ...@@ -554,6 +558,7 @@ int fmctdc_wr_mode(struct fmctdc_board *userb, int on)
extern int fmctdc_check_wr_mode(struct fmctdc_board *userb) extern int fmctdc_check_wr_mode(struct fmctdc_board *userb)
{ {
__define_board(b, userb); __define_board(b, userb);
if (__fmctdc_command(b, FT_CMD_WR_QUERY) == 0) if (__fmctdc_command(b, FT_CMD_WR_QUERY) == 0)
return 0; return 0;
return errno; return errno;
...@@ -576,12 +581,13 @@ int fmctdc_reference_set(struct fmctdc_board *userb, ...@@ -576,12 +581,13 @@ int fmctdc_reference_set(struct fmctdc_board *userb,
uint32_t ch_ref = ch_reference; uint32_t ch_ref = ch_reference;
char path[64]; char path[64];
if (ch_target >= FMCTDC_NUM_CHANNELS || ch_ref >= FMCTDC_NUM_CHANNELS ) { if (ch_target >= FMCTDC_NUM_CHANNELS || ch_reference >= FMCTDC_NUM_CHANNELS ) {
errno = EINVAL; errno = EINVAL;
return -1; return -1;
} }
snprintf(path, sizeof(path), "ft-ch%d/diff-reference", ch_target + 1); snprintf(path, sizeof(path), "ft-ch%d/diff-reference", ch_target + 1);
ch_ref++; ch_ref++; /* for the driver channel interval is [1, 5] */
return fmctdc_sysfs_set(b, path, &ch_ref); return fmctdc_sysfs_set(b, path, &ch_ref);
} }
...@@ -608,14 +614,14 @@ int fmctdc_reference_get(struct fmctdc_board *userb, unsigned int ch_target) ...@@ -608,14 +614,14 @@ int fmctdc_reference_get(struct fmctdc_board *userb, unsigned int ch_target)
err = fmctdc_sysfs_get(b, path, &ch_ref); err = fmctdc_sysfs_get(b, path, &ch_ref);
if (err) if (err)
return -1; return -1;
return ch_ref - 1; return ch_ref - 1; /* For the driver channel interval is [1, 5]*/
} }
/** /**
* It removes all samples from the channel buffer * It removes all samples from the channel buffer
* @param[in] userb TDC board instance token * @param[in] userb TDC board instance token
* @param[in] channel target channel [1, 5] * @param[in] channel target channel [0, 4]
* @return 0 on success, otherwise -1 and errno is set appropriately * @return 0 on success, otherwise -1 and errno is set appropriately
*/ */
int fmctdc_flush(struct fmctdc_board *userb, unsigned int channel) int fmctdc_flush(struct fmctdc_board *userb, unsigned int channel)
...@@ -645,7 +651,7 @@ int fmctdc_flush(struct fmctdc_board *userb, unsigned int channel) ...@@ -645,7 +651,7 @@ int fmctdc_flush(struct fmctdc_board *userb, unsigned int channel)
} while (i > 0); } while (i > 0);
/* Flush ZIO buffer */ /* Flush ZIO buffer */
snprintf(path, sizeof(path), "ft-ch%d/chan0/buffer/flush", channel); snprintf(path, sizeof(path), "ft-ch%d/chan0/buffer/flush", channel + 1);
err = fmctdc_sysfs_set(b, path, &channel); err = fmctdc_sysfs_set(b, path, &channel);
if (err) { if (err) {
return err; return err;
......
...@@ -16,12 +16,12 @@ ...@@ -16,12 +16,12 @@
#include <stdint.h> #include <stdint.h>
enum fmctdc_channel { enum fmctdc_channel {
FMCTDC_CH_1 = 1, FMCTDC_CH_1 = 0,
FMCTDC_CH_2 = 2, FMCTDC_CH_2,
FMCTDC_CH_3 = 3, FMCTDC_CH_3,
FMCTDC_CH_4 = 4, FMCTDC_CH_4,
FMCTDC_CH_5 = 5, FMCTDC_CH_5,
FMCTDC_CH_LAST = 5, FMCTDC_CH_LAST = 4,
FMCTDC_NUM_CHANNELS = 5 FMCTDC_NUM_CHANNELS = 5
}; };
......
...@@ -119,7 +119,8 @@ int main(int argc, char **argv) ...@@ -119,7 +119,8 @@ int main(int argc, char **argv)
exit(1); exit(1);
} }
memset(ref, 0, sizeof(ref)); for (i = 0; i < FMCTDC_NUM_CHANNELS; ++i)
ref[i] = -1;
/* Parse Options */ /* Parse Options */
while ((opt = getopt(argc, argv, "hwns:d:")) != -1) { while ((opt = getopt(argc, argv, "hwns:d:")) != -1) {
...@@ -140,21 +141,21 @@ int main(int argc, char **argv) ...@@ -140,21 +141,21 @@ int main(int argc, char **argv)
break; break;
case 'd': case 'd':
sscanf(optarg, "%i,%i", &a, &b); sscanf(optarg, "%i,%i", &a, &b);
if (a < 1 || a > FMCTDC_NUM_CHANNELS) { if (a < 0 || a > FMCTDC_CH_LAST) {
fprintf(stderr, fprintf(stderr,
"%s: invalid reference channel %d\n", "%s: invalid reference channel %d\n",
argv[0], a); argv[0], a);
help(argv[0]); help(argv[0]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (b < 0 || b > FMCTDC_NUM_CHANNELS) { if (b < 0 || b > FMCTDC_CH_LAST) {
fprintf(stderr, fprintf(stderr,
"%s: invalid target channel %d\n", "%s: invalid target channel %d\n",
argv[0], b); argv[0], b);
help(argv[0]); help(argv[0]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
ref[b - 1] = a; ref[b] = a;
break; break;
} }
} }
...@@ -188,27 +189,28 @@ int main(int argc, char **argv) ...@@ -188,27 +189,28 @@ int main(int argc, char **argv)
fmctdc_close(brd); fmctdc_close(brd);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
channels[ch - FMCTDC_CH_1] = fmctdc_fileno_channel(brd, ch); channels[ch] = fmctdc_fileno_channel(brd, ch);
chan_count++; chan_count++;
optind++; optind++;
} }
/* If there are not channels, then dump them all */ /* If there are not channels, then dump them all */
if (!chan_count) { if (!chan_count) {
for (i = FMCTDC_CH_1; i <= FMCTDC_CH_LAST; i++) { for (i = 0; i < FMCTDC_NUM_CHANNELS; i++) {
channels[i - FMCTDC_CH_1] = channels[i] =
fmctdc_fileno_channel(brd, i); fmctdc_fileno_channel(brd, i);
ret = fmctdc_reference_set(brd, i, ref[i - 1]); ret = fmctdc_reference_set(brd, i, ref[i]);
if (ret) { if (ret) {
fprintf(stderr, fprintf(stderr,
"%s: cannot set reference mode\n", "%s: cannot set reference mode: %s\n",
argv[0]); argv[0], fmctdc_strerror(errno));
fprintf(stderr, fprintf(stderr,
"%s: continue in normal mode\n", "%s: continue in normal mode: %s\n",
argv[0]); argv[0], fmctdc_strerror(errno));
ref[i] = -1;
} }
} }
chan_count = FMCTDC_NUM_CHANNELS; chan_count = i;
} }
...@@ -219,8 +221,8 @@ int main(int argc, char **argv) ...@@ -219,8 +221,8 @@ int main(int argc, char **argv)
/* Prepare the list of channel to observe */ /* Prepare the list of channel to observe */
FD_ZERO(&rfds); FD_ZERO(&rfds);
for (i = FMCTDC_CH_1; i <= FMCTDC_CH_LAST; i++) { for (i = 0; i <= FMCTDC_CH_LAST; i++) {
fd = channels[i - FMCTDC_CH_1]; fd = channels[i];
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "Can't open channel %d\n", i); fprintf(stderr, "Can't open channel %d\n", i);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
...@@ -242,8 +244,8 @@ int main(int argc, char **argv) ...@@ -242,8 +244,8 @@ int main(int argc, char **argv)
} }
/* Now we can read the timestamp */ /* Now we can read the timestamp */
for (i = FMCTDC_CH_1; i <= FMCTDC_CH_LAST; i++) { for (i = 0; i <= FMCTDC_CH_LAST; i++) {
fd = channels[i - FMCTDC_CH_1]; fd = channels[i];
if (fd < 0) if (fd < 0)
continue; continue;
...@@ -253,7 +255,7 @@ int main(int argc, char **argv) ...@@ -253,7 +255,7 @@ int main(int argc, char **argv)
byte_read = fmctdc_read(brd, i, &ts, 1, byte_read = fmctdc_read(brd, i, &ts, 1,
nblock ? O_NONBLOCK : 0); nblock ? O_NONBLOCK : 0);
if (byte_read > 0) { if (byte_read > 0) {
dump(i, &ts, fmt_wr, !!ref[i - 1]); dump(i, &ts, fmt_wr, ref[i] < 0 ? 0 : 1);
ts_prev[i] = ts; ts_prev[i] = ts;
n++; n++;
...@@ -262,9 +264,9 @@ int main(int argc, char **argv) ...@@ -262,9 +264,9 @@ int main(int argc, char **argv)
} }
/* Restore default time-stamping */ /* Restore default time-stamping */
for (i = FMCTDC_CH_1; i <= FMCTDC_CH_LAST; i++) { for (i = 0; i <= FMCTDC_CH_LAST; i++) {
if (channels[i - FMCTDC_CH_1] > 0) if (channels[i] > 0)
fmctdc_reference_clear(brd, i); fmctdc_reference_clear(brd, -1);
} }
fmctdc_close(brd); fmctdc_close(brd);
......
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