fmctdc-lib-private.h 2.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * The "official" fmc-tdc API
 *
 * Copyright (C) 2012-2013 CERN (www.cern.ch)
 * Author: Tomasz Włostowski <tomasz.wlostowski@cern.ch>
 * Author: Alessandro Rubini <rubini@gnudd.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2 as published by the Free Software Foundation or, at your
 * option, any later version.
 */

#ifndef __FMCTDC_LIB_PRIVATE_H__
#define __FMCTDC_LIB_PRIVATE_H__
16 17
#include <stdio.h>
#include <stdlib.h>
18 19 20 21 22 23 24 25 26 27 28 29 30
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <fmc-tdc.h>

/* Internal structure */
struct __fmctdc_board {
	int dev_id;
	char *devbase;
	char *sysbase;
Federico Vaga's avatar
Federico Vaga committed
31
	int fdcc[FMCTDC_NUM_CHANNELS]; /**< current controls */
32 33
	int fdc[5];		/* The 5 control channels */
	int fdd[5];		/* The 5 data channels */
34
	int ch_ref[FMCTDC_NUM_CHANNELS];  /**< reference channel */
35 36 37 38 39 40 41 42 43 44
};

static inline int fmctdc_is_verbose(void)
{
	return getenv("FMCTDC_LIB_VERBOSE") != 0;
}

#define __define_board(b, ub)	struct __fmctdc_board *b = (void *)(ub)

/* These two from ../tools/fdelay-raw.h, used internally */
45
static inline int __fmctdc_sysfs_get(char *path, uint32_t * resp)
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
{
	FILE *f = fopen(path, "r");

	if (!f)
		return -1;
	errno = 0;
	if (fscanf(f, "%i", resp) != 1) {
		fclose(f);
		if (!errno)
			errno = EINVAL;
		return -1;
	}
	fclose(f);
	return 0;
}

62
static inline int __fmctdc_sysfs_set(char *path, uint32_t * value)
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
{
	char s[16];
	int fd, ret, len;

	len = sprintf(s, "%i\n", *value);
	fd = open(path, O_WRONLY);
	if (fd < 0)
		return -1;
	ret = write(fd, s, len);
	close(fd);
	if (ret < 0)
		return -1;
	if (ret == len)
		return 0;
	errno = EINVAL;
	return -1;
}

/* And these two for the board structure */
static inline int fmctdc_sysfs_get(struct __fmctdc_board *b, char *name,
83
				   uint32_t * resp)
84 85 86 87 88 89 90 91
{
	char pathname[128];

	sprintf(pathname, "%s/%s", b->sysbase, name);
	return __fmctdc_sysfs_get(pathname, resp);
}

static inline int fmctdc_sysfs_set(struct __fmctdc_board *b, char *name,
92
				   uint32_t * value)
93 94 95 96 97 98 99 100 101 102 103 104 105
{
	char pathname[128];

	sprintf(pathname, "%s/%s", b->sysbase, name);
	return __fmctdc_sysfs_set(pathname, value);
}

static inline int __fmctdc_command(struct __fmctdc_board *b, uint32_t cmd)
{
	return fmctdc_sysfs_set(b, "command", &cmd);
}

#endif /* __FMCTDC_LIB_PRIVATE_H__ */