Commit 61a6d23d authored by Federico Vaga's avatar Federico Vaga

Merge branch 'release/v2.5.0'

parents 903ba410 37a35337
-include Makefile.specific
all:
clean:
install:
python setup.py install
.PHONY: all clean install
from PyAdcLib import ADC_Generic
class AdcGenericFake(ADC_Generic):
BOARD_NAME = b"adc-genericfake"
def __init__(self, devid):
super(AdcGenericFake, self).__init__(devid)
from ctypes import c_int
from ctypes import c_uint
from ctypes import c_void_p
from ctypes import c_char_p
from ctypes import c_long
from ctypes import c_ulong
from ctypes import c_short
from ctypes import byref
from ctypes import memset
from ctypes import sizeof
from ctypes import Structure
from ctypes import POINTER
from ctypes import CDLL
from ctypes import get_errno
""" Wrapper for ADC C library using ctypes
All ctypes functions have exactly the same names as the ones in the
C library. To each function corresponds python function that hides
C specific operations from the user. The names of these functions are
the sames as the ctypes functions without ADC prefix."""
class adc_conf(Structure):
__ADC_CONF_LEN = 64 # fixme what to do about it
_fields_ = [("type", c_int),
("dev_type", c_uint),
("route_to", c_uint),
("flags", c_uint),
("mask", c_ulong),
("value", c_uint * __ADC_CONF_LEN)]
def __init__(self):
memset(byref(self), 0, sizeof(adc_conf))
class adc_buf(Structure):
_fields_ = [("data", POINTER(c_short)),
("metadata", c_void_p),
("samplesize", c_int),
("nsamples", c_int),
("dev", c_void_p),
("mapaddr", c_void_p),
("maplen", c_uint),
("flags", c_uint)]
class timeval(Structure):
_fields_ = [("sec", c_long),
("usec", c_long)]
class adc_timestamp(Structure):
_fields_ = [("secs", c_ulong),
("ticks", c_ulong),
("bins", c_ulong)]
class ADC_Generic():
ADC_F_USERMASK = 0xffff0000
ADC_F_FLUSH = 0x00010000
ADC_F_VERBOSE = 0x00020000
ADC_F_FIXUP = 0x00400000
ADC_TRG_POL_POS = 0
ADC_TRG_POL_NEG = 1
ADC_CONF_TRG_EXT_ENABLE = 0
ADC_CONF_TRG_EXT_POLARITY = 1
ADC_CONF_TRG_EXT_DELAY = 2
__ADC_CONF_TRG_EXT_LAST_INDEX = 3
ADC_CONF_TRG_THR_ENABLE = 0
ADC_CONF_TRG_THR_POLARITY = 1
ADC_CONF_TRG_THR_DELAY = 2
ADC_CONF_TRG_THR_THRESHOLD = 3
ADC_CONF_TRG_THR_HYSTERESIS = 4
__ADC_CONF_TRG_THR_LAST_INDEX = 5
ADC_CONF_TRG_TIM_ENABLE = 0
__ADC_CONF_TRG_TIM_LAST_INDEX = 1
ADC_CONF_ACQ_N_SHOTS = 0
ADC_CONF_ACQ_POST_SAMP = 1
ADC_CONF_ACQ_PRE_SAMP = 2
ADC_CONF_ACQ_UNDERSAMPLE = 3
ADC_CONF_ACQ_FREQ_HZ = 4
ADC_CONF_ACQ_N_BITS = 5
__ADC_CONF_ACQ_ATTRIBUTE_LAST_INDEX = 6
ADC_CONF_CHN_RANGE = 0
ADC_CONF_CHN_TERMINATION = 1
ADC_CONF_CHN_OFFSET = 2
ADC_CONF_CHN_SATURATION = 3
ADC_CONF_CHN_GAIN = 4
__ADC_CONF_CHN_ATTRIBUTE_LAST_INDEX = 5
ADC_CONF_BRD_STATUS = 0
ADC_CONF_BRD_MAX_FREQ_HZ = 1
ADC_CONF_BRD_MIN_FREQ_HZ = 2
ADC_CONF_BRD_STATE_MACHINE_STATUS = 3
ADC_CONF_BRD_N_CHAN = 4
ADC_CONF_BRD_N_TRG_EXT = 5
ADC_CONF_BRD_N_TRG_THR = 6
ADC_CONF_BRD_N_TRG_TIM = 7
ADC_CONF_UTC_TIMING_BASE_S = 8
ADC_CONF_UTC_TIMING_BASE_T = 9
ADC_CONF_UTC_TIMING_BASE_B = 10
__ADC_CONF_BRD_ATTRIBUTE_LAST_INDEX = 11
ADC_CONF_TYPE_BRD = 0
ADC_CONF_TYPE_CHN = 1
ADC_CONF_TYPE_ACQ = 2
ADC_CONF_TYPE_CUS = 3
ADC_CONF_TYPE_TRG_EXT = 4
ADC_CONF_TYPE_TRG_THR = 5
ADC_CONF_TYPE_TRG_TIM = 6
__ADC_CONF_TYPE_LAST_INDEX = 7
def __errcheck_int(self, ret, func, args):
"""Generic error checker for functions returning 0 as success
and -1 as error"""
if ret < 0:
raise OSError(get_errno(),
self.strerror(get_errno()), "")
else:
return ret
def __errcheck_pointer(self, ret, func, args):
"""Generic error handler for functions returning pointers"""
if ret is None:
raise OSError(get_errno(),
self.strerror(get_errno()), "")
else:
return ret
def print_version(self):
self.adc_print_version()
def __init__(self, devid, board=None):
self.__ADC_CONF_LEN = 64
"""number of allocated items in each structure"""
if board is not None:
self.board = board
elif hasattr(self, 'BOARD_NAME'):
self.board = self.BOARD_NAME
else:
raise Exception("Missing board name")
self.libc = CDLL("libadc.so", use_errno=True)
self.adc_print_version = self.libc.adc_print_version
self.adc_print_version.restype = None
self.adc_print_version.argtypes = None
self.adc_init = self.libc.adc_init
self.adc_init.restype = c_int
self.adc_init.argtypes = None
self.adc_init.errcheck = self.__errcheck_int
self.adc_exit = self.libc.adc_exit
self.adc_exit.restype = None
self.adc_exit.argtypes = None
self.adc_strerror = self.libc.adc_strerror
self.adc_strerror.restype = c_char_p
self.adc_strerror.argtypes = [c_int]
self.adc_strerror.errcheck = self.__errcheck_pointer
self.adc_get_driver_type = self.libc.adc_get_driver_type
self.adc_get_driver_type.restype = c_char_p
self.adc_get_driver_type.argtypes = [c_void_p]
self.adc_open = self.libc.adc_open
self.adc_open.restype = c_void_p
self.adc_open.argtypes = [c_char_p, c_uint, c_ulong, c_uint, c_ulong]
self.adc_open.errcheck = self.__errcheck_pointer
self.adc_open_by_lun = self.libc.adc_open_by_lun
self.adc_open_by_lun.restype = c_void_p
self.adc_open_by_lun.argtypes = [c_char_p, c_int, c_ulong, c_uint,
c_ulong]
self.adc_open_by_lun.errcheck = self.__errcheck_pointer
self.adc_request_buffer = self.libc.adc_request_buffer
self.adc_request_buffer.restype = POINTER(adc_buf)
self.adc_request_buffer.argtypes = [c_void_p, c_int, c_void_p, c_uint]
self.adc_request_buffer.errcheck = self.__errcheck_pointer
self.adc_set_conf = self.libc.adc_set_conf
self.adc_set_conf.restype = None
self.adc_set_conf.argtypes = [c_void_p, c_uint, c_uint]
self.adc_set_conf_mask = self.libc.adc_set_conf_mask
self.adc_set_conf_mask.restype = None
self.adc_set_conf_mask.argtypes = [c_void_p, c_uint]
self.adc_get_conf = self.libc.adc_get_conf
self.adc_get_conf.restype = c_int
self.adc_get_conf.argtypes = [c_void_p, c_uint, c_void_p]
"""self.adc_get_conf.argtypes = [c_void_p, c_uint,
POINTER(c_uint)]"""
self.adc_get_conf.errcheck = self.__errcheck_int
self.adc_apply_config = self.libc.adc_apply_config
self.adc_apply_config.restype = c_int
self.adc_apply_config.argtypes = [c_void_p, c_uint, c_void_p]
self.adc_apply_config.errcheck = self.__errcheck_int
self.adc_acq_start = self.libc.adc_acq_start
self.adc_acq_start.restype = c_int
self.adc_acq_start.argtypes = [c_void_p, c_uint, c_void_p]
self.adc_acq_start.errcheck = self.__errcheck_int
self.adc_zio_get_file_descriptor =\
self.libc.adc_zio_get_file_descriptor
self.adc_zio_get_file_descriptor.restype = c_int
self.adc_zio_get_file_descriptor.argtypes = [c_void_p]
self.adc_zio_get_file_descriptor.errcheck = self.__errcheck_int
self.adc_acq_poll = self.libc.adc_acq_poll
self.adc_acq_poll.restype = c_int
self.adc_acq_poll.argtypes = [c_void_p, c_uint, c_void_p]
self.adc_acq_poll.errcheck = self.__errcheck_int
self.adc_fill_buffer = self.libc.adc_fill_buffer
self.adc_fill_buffer.restype = c_int
self.adc_fill_buffer.argtypes = [c_void_p, c_void_p, c_uint, c_void_p]
self.adc_fill_buffer.errcheck = self.__errcheck_int
self.adc_acq_stop = self.libc.adc_acq_stop
self.adc_acq_stop.restype = c_int
self.adc_acq_stop.argtypes = [c_void_p, c_uint]
self.adc_acq_stop.errcheck = self.__errcheck_int
self.adc_tstamp_buffer = self.libc.adc_tstamp_buffer
self.adc_tstamp_buffer.restype = POINTER(adc_timestamp)
self.adc_tstamp_buffer.argtypes = [c_void_p, c_void_p]
self.adc_tstamp_buffer.errcheck = self.__errcheck_pointer
self.adc_release_buffer = self.libc.adc_release_buffer
self.adc_release_buffer.restype = c_int
self.adc_release_buffer.argtypes = [c_void_p, c_void_p, c_void_p]
self.adc_release_buffer.errcheck = self.__errcheck_int
self.adc_close = self.libc.adc_close
self.adc_close.restype = c_int
self.adc_close.argtypes = [c_void_p]
self.adc_close.errcheck = self.__errcheck_int
self.adc_trigger_fire = self.libc.adc_trigger_fire
self.adc_trigger_fire.restype = c_int
self.adc_trigger_fire.argtypes = [c_void_p]
self.adc_trigger_fire.errcheck = self.__errcheck_int
self.adc_has_trigger_fire = self.libc.adc_has_trigger_fire
self.adc_has_trigger_fire.restype = c_int
self.adc_has_trigger_fire.argtypes = [c_void_p]
"""this function doesn't have body
self.adc_reset_conf = self.libc.adc_reset_conf
self.adc_reset_conf.restype = c_int
self.adc_reset_conf.argtypes = [c_void_p, c_uint, c_void_p]
self.adc_reset_conf.errcheck = self.__errcheck_int"""
self.adc_retrieve_config = self.libc.adc_retrieve_config
self.adc_retrieve_config.restype = c_int
self.adc_retrieve_config.argtypes = [c_void_p, c_void_p]
self.adc_retrieve_config.errcheck = self.__errcheck_int
self.adc_get_capabilities = self.libc.adc_get_capabilities
self.adc_get_capabilities.restype = c_ulong
self.adc_get_capabilities.argtypes = [c_void_p, c_int]
self.adc_get_capabilities.errcheck = self.__errcheck_int
self.adc_get_param = self.libc.adc_get_param
self.adc_get_param.restype = c_int
self.adc_get_param.argtypes = [c_void_p, c_char_p, c_char_p, c_void_p]
self.adc_get_param.errcheck = self.__errcheck_int
self.adc_set_param = self.libc.adc_set_param
self.adc_set_param.restype = c_int
self.adc_set_param.argtypes = [c_void_p, c_char_p, c_char_p, c_void_p]
self.adc_set_param.errcheck = self.__errcheck_int
self.adc_set_conf_mask_all = self.libc.adc_set_conf_mask_all
self.adc_set_conf_mask_all.restype = None
self.adc_set_conf_mask_all.argtypes = [c_void_p, c_void_p]
self.__init()
self.__adc_ptr = self.__open(devid, 0, 0, self.ADC_F_FLUSH)
self._adc_conf = adc_conf()
def __del__(self):
self.__close()
self.__exit()
def __init(self):
self.adc_init()
def __exit(self):
self.adc_exit()
def strerror(self, errnum):
return self.adc_strerror(errnum)
def get_driver_type(self):
return self.adc_get_driver_type(self.__adc_ptr)
def __open(self, dev_id, totalsamples, nbuffer, flags):
return self.adc_open(self.board, dev_id, totalsamples, nbuffer, flags)
def __open_by_lun(self, name, lun, totalsamples, nbuffer, flags):
return self.adc_open_by_lun(self.board, lun, totalsamples, nbuffer, flags)
def request_buffer(self, nsamples, alloc, flags):
return self.adc_request_buffer(self.__adc_ptr, nsamples, alloc, flags)
def set_conf(self, conf_index, val):
self.adc_set_conf(byref(self._adc_conf), conf_index, val)
def set_conf_mask(self, conf_index):
self.adc_set_conf_mask(byref(self._adc_conf), conf_index)
def get_conf(self, conf_index, val):
self.adc_get_conf(byref(self._adc_conf), conf_index, val)
def acq_start(self, flags, timeout):
self.adc_acq_start(self.__adc_ptr, flags, timeout)
# NON API
def zio_get_file_descriptor(self):
return self.adc_zio_get_file_descriptor(self.__adc_ptr)
def acq_poll(self, flags, timeout):
self.adc_acq_poll(self.__adc_ptr, flags, timeout)
def fill_buffer(self, buf, flags, timeout):
self.adc_fill_buffer(self.__adc_ptr, buf, flags, timeout)
def acq_stop(self, flags):
self.adc_acq_stop(self.__adc_ptr, flags)
def tstamp_buffer(self, buf, ts):
self.adc_tstamp_buffer(buf, ts)
def release_buffer(self, buf, free):
self.adc_release_buffer(self.__adc_ptr, buf, free)
def __close(self):
if not self.__adc_ptr:
self.adc_close(self.__adc_ptr)
self.__adc_ptr = 0
def trigger_fire(self):
self.adc_trigger_fire(self.__adc_ptr)
def has_trigger_fire(self):
return self.adc_has_trigger_fire(self.__adc_ptr)
def apply_config(self, flags):
self.adc_apply_config(self.__adc_ptr, flags, byref(self._adc_conf))
memset(byref(self._adc_conf), 0, sizeof(adc_conf))
def retrieve_config(self):
self.adc_retrieve_config(self.__adc_ptr, byref(self._adc_conf))
def get_capabilities(self, type):
self.adc_get_capabilities(self.__adc_ptr, type)
def get_param(self, name, sptr, iptr):
self.adc_get_param(self.__adc_ptr, name, sptr, iptr)
def set_param(self, name, sptr, iptr):
self.adc_set_param(self.__adc_ptr, name, sptr, iptr)
def set_conf_mask_all(self):
self.adc_set_conf_mask_all(byref(self._adc_conf), self.__adc_ptr)
from .PyAdcLib import ADC_Generic
class FmcAdc100m14b4ch(ADC_Generic):
BOARD_NAME = b"fmc-adc-100m14b4cha"
ADC_CONF_100M14B4CHA_CHN_RANGE_N = 3
ADC_CONF_100M14B4CHA_CHN_RANGE_OPEN_DRAIN = 0
ADC_CONF_100M14B4CHA_CHN_RANGE_100mV = 0x23
ADC_CONF_100M14B4CHA_CHN_RANGE_1V = 0x11
ADC_CONF_100M14B4CHA_CHN_RANGE_10V = 0x45
ADC_CONF_100M14B4CHA_CHN_RANGE_100mV_CAL = 0x42
ADC_CONF_100M14B4CHA_CHN_RANGE_1V_CAL = 0x40
ADC_CONF_100M14B4CHA_CHN_RANGE_10V_CAL = 0x44
ADC_CONF_100M14B4CHA_BUF_KMALLOC = 0
ADC_CONF_100M14B4CHA_BUF_VMALLOC = 1
ADC_CONF_100M14B4CHA_BUF_TYPE = 0
ADC_CONF_100M14B4CHA_TRG_SW_EN = 1
ADC_CONF_100M14B4CHA_ACQ_MSHOT_MAX = 2
ADC_CONF_100M14B4CHA_BUF_SIZE_KB = 3
ADC_CONF_100M14B4CHA_TRG_ALT_EN = 4
__ADC_CONF_100M14B4CHA_LAST_INDEX = 5
def __init__(self, devid):
super(FmcAdc100m14b4ch, self).__init__(devid)
from PyAdcLib import ADC_Generic
class AdcZioFake(ADC_Generic):
BOARD_NAME = b"adc-ziofake"
def __init__(self, devid):
super(AdcZioFake, self).__init__(devid)
"""
@package docstring
@author: Federico Vaga <federico.vaga@cern.ch>
@copyright: Copyright (c) 2019 CERN (home.cern)
@license: GNU Library General Public License version 2 or later
SPDX-License-Identifier: LGPL-3.0-or-later
"""
__all__ = (
)
#!/usr/bin/env python
from distutils.core import setup
setup(name='PyAdcLib',
version='1.0',
description='Python Module to handle ADC lib devices',
author='Milosz Malczak, Federico Vaga',
author_email='milosz.malczak@cern.ch, federico.vaga@cern.ch',
maintainer="Federico Vaga",
maintainer_email="federico.vaga@cern.ch",
url='https://www.ohwr.org/projects/adc-lib',
packages=['PyAdcLib'],
license='LGPLv3',
)
......@@ -362,51 +362,13 @@ extern int adc_acq_flush(struct adc_dev *dev, unsigned int flags);
* Device configuration functions
* @{
*/
/**
* It sets the bit mask for the given option
* @param[in] conf configuration structure where apply this operation
* @param[in] conf_index configuration value to set (see configuration enum)
*/
static inline void adc_set_conf_mask(struct adc_conf *conf,
unsigned int conf_index)
{
conf->mask |= (1LL << conf_index);
}
/**
* It assigns a configuration item, and its mask
* @param[in] conf configuration structure where apply this operation
* @param[in] conf_index configuration value to set (see configuration enum)
* @param[in] val value to set
*/
static inline void adc_set_conf(struct adc_conf *conf,
unsigned int conf_index, uint32_t val)
{
conf->value[conf_index] = val;
adc_set_conf_mask(conf, conf_index);
}
/**
* It retrieve a configuration value from the configuration descriptor
* @param[in] conf configuration structure where apply this operation
* @param[in] conf_index configuration value to set (see configuration enum)
* @param[out] val value read
* @return 0 in success, -1 on error
*/
static inline int adc_get_conf(struct adc_conf *conf,
unsigned int conf_index,
uint32_t *val)
{
if (conf->mask & (1LL << conf_index)) {
*val = conf->value[conf_index];
return 0;
} else {
return -1;
}
}
extern void adc_set_conf_mask(struct adc_conf *conf, unsigned int conf_index);
extern void adc_set_conf(struct adc_conf *conf, unsigned int conf_index,
uint32_t val);
extern int adc_get_conf(struct adc_conf *conf, unsigned int conf_index,
uint32_t *val);
extern void adc_set_conf_mask_all(struct adc_conf *conf, struct adc_dev *dev);
/**@}*/
extern int adc_reset_conf(struct adc_dev *dev, unsigned int flags,
struct adc_conf *conf);
......@@ -426,17 +388,6 @@ extern int adc_get_param(struct adc_dev *dev, char *name,
extern int adc_set_param(struct adc_dev *dev, char *name,
char *sptr, int *iptr);
/**
* It enables the configuration for all the supported field by the given device
* At least the `type` must be set in `conf`
* @param[in] conf configuration structure to use
* @param[in] dev the ADC device token
*/
static inline void adc_set_conf_mask_all(struct adc_conf *conf, struct adc_dev *dev)
{
conf->mask = adc_get_capabilities(dev, conf->type);
}
/**@}*/
/**
* @defgroup buf Buffer
......
......@@ -11,6 +11,7 @@
*/
#include <errno.h>
#include <string.h>
#define ADC_INLINE
#include "adc-lib.h"
#include "adc-lib-int.h"
......@@ -95,6 +96,58 @@ uint64_t adc_get_capabilities(struct adc_dev *dev,
return b->board->capabilities[type];
}
/**
* It sets the bit mask for the given option
* @param[in] conf configuration structure where apply this operation
* @param[in] conf_index configuration value to set (see configuration enum)
*/
void adc_set_conf_mask(struct adc_conf *conf, unsigned int conf_index)
{
conf->mask |= (1LL << conf_index);
}
/**
* It assigns a configuration item, and its mask
* @param[in] conf configuration structure where apply this operation
* @param[in] conf_index configuration value to set (see configuration enum)
* @param[in] val value to set
*/
void adc_set_conf(struct adc_conf *conf, unsigned int conf_index,
uint32_t val)
{
conf->value[conf_index] = val;
adc_set_conf_mask(conf, conf_index);
}
/**
* It retrieve a configuration value from the configuration descriptor
* @param[in] conf configuration structure where apply this operation
* @param[in] conf_index configuration value to set (see configuration enum)
* @param[out] val value read
* @return 0 in success, -1 on error
*/
int adc_get_conf(struct adc_conf *conf, unsigned int conf_index,
uint32_t *val)
{
if (conf->mask & (1LL << conf_index)) {
*val = conf->value[conf_index];
return 0;
} else {
return -1;
}
}
/**
* It enables the configuration for all the supported field by the given device
* At least the `type` must be set in `conf`
* @param[in] conf configuration structure to use
* @param[in] dev the ADC device token
*/
void adc_set_conf_mask_all(struct adc_conf *conf, struct adc_dev *dev)
{
conf->mask = adc_get_capabilities(dev, conf->type);
}
/**
* It computes what are the necessary offsets to apply on channels
* in order to clear a constant offset.
......
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