/********************************************************************
Name: v1718.c
Created by: Jimmy Ngai
Contents: Midas VME standard (MVMESTD) layer for CAEN V1718
VME-USB2.0 Bridge using CAENVMElib Linux library
Based on sis3100.c by Stefan Ritt
$Id: $
\********************************************************************/
#ifdef __linux__
#ifndef OS_LINUX
#define OS_LINUX
#endif
#endif
#ifdef OS_LINUX
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include "CAENVMElib.h"
#endif // OS_LINUX
#include "mvmestd.h"
/*------------------------------------------------------------------*/
int mvme_open(MVME_INTERFACE **vme, int idx)
{
*vme = (MVME_INTERFACE *) malloc(sizeof(MVME_INTERFACE));
if (*vme == NULL)
return MVME_NO_MEM;
memset(*vme, 0, sizeof(MVME_INTERFACE));
/* open VME */
if (CAENVME_Init(cvV1718, 0, idx, &(*vme)->handle) != cvSuccess)
return MVME_NO_INTERFACE;
/* default values */
(*vme)->am = MVME_AM_DEFAULT;
(*vme)->dmode = MVME_DMODE_D32;
(*vme)->blt_mode = MVME_BLT_NONE;
(*vme)->table = NULL; // not used
return MVME_SUCCESS;
}
/*------------------------------------------------------------------*/
int mvme_close(MVME_INTERFACE *vme)
{
CAENVME_End(vme->handle);
return MVME_SUCCESS;
}
/*------------------------------------------------------------------*/
int mvme_sysreset(MVME_INTERFACE *vme)
{
CAENVME_SystemReset(vme->handle);
return MVME_SUCCESS;
}
/*------------------------------------------------------------------*/
int mvme_write(MVME_INTERFACE *vme, mvme_addr_t vme_addr, void *src, mvme_size_t n_bytes)
{
mvme_size_t n;
DWORD data;
int status=0;
int hvme;
hvme = vme->handle;
if (n_bytes <= 4) {
data = n = 0;
memcpy(&data, src, n_bytes);
/* D8 */
if (vme->dmode == MVME_DMODE_D8)
status = CAENVME_WriteCycle(hvme, vme_addr, &data, vme->am, cvD8);
/* D16 */
else if (vme->dmode == MVME_DMODE_D16)
status = CAENVME_WriteCycle(hvme, vme_addr, &data, vme->am, cvD16);
/* D32 */
else if (vme->dmode == MVME_DMODE_D32)
status = CAENVME_WriteCycle(hvme, vme_addr, &data, vme->am, cvD32);
if (status == cvSuccess)
n = n_bytes;
else
n = 0;
} else {
/* D32 BLT */
if (vme->blt_mode == MVME_BLT_BLT32)
status |= CAENVME_BLTWriteCycle(hvme, vme_addr, src, n_bytes, vme->am, cvD32, &n);
/* D64 MBLT */
else if (vme->blt_mode == MVME_BLT_MBLT64)
status |= CAENVME_MBLTWriteCycle(hvme, vme_addr, src, n_bytes, vme->am, &n);
if (status != cvSuccess)
n = 0;
}
return n;
}
/*------------------------------------------------------------------*/
int mvme_write_value(MVME_INTERFACE *vme, mvme_addr_t vme_addr, unsigned int value)
{
mvme_size_t n;
int status=0;
int hvme;
hvme = vme->handle;
if (vme->dmode == MVME_DMODE_D8)
n = 1;
else if (vme->dmode == MVME_DMODE_D16)
n = 2;
else
n = 4;
/* D8 */
if (vme->dmode == MVME_DMODE_D8)
status = CAENVME_WriteCycle(hvme, vme_addr, &value, vme->am, cvD8);
/* D16 */
else if (vme->dmode == MVME_DMODE_D16)
status = CAENVME_WriteCycle(hvme, vme_addr, &value, vme->am, cvD16);
/* D32 */
else if (vme->dmode == MVME_DMODE_D32)
status = CAENVME_WriteCycle(hvme, vme_addr, &value, vme->am, cvD32);
if (status != cvSuccess)
n = 0;
return n;
}
/*------------------------------------------------------------------*/
int mvme_read(MVME_INTERFACE *vme, void *dst, mvme_addr_t vme_addr, mvme_size_t n_bytes)
{
mvme_size_t i, n;
DWORD data;
int status=0, cfifo;
int hvme;
hvme = vme->handle;
if (n_bytes <= 4) {
data = 0;
/* D8 */
if (vme->dmode == MVME_DMODE_D8)
status = CAENVME_ReadCycle(hvme, vme_addr, &data, vme->am, cvD8);
/* D16 */
else if (vme->dmode == MVME_DMODE_D16)
status = CAENVME_ReadCycle(hvme, vme_addr, &data, vme->am, cvD16);
/* D32 */
else if (vme->dmode == MVME_DMODE_D32)
status = CAENVME_ReadCycle(hvme, vme_addr, &data, vme->am, cvD32);
memcpy(dst, &data, n_bytes);
if (status == cvSuccess)
n = n_bytes;
else
n = 0;
} else {
n = 0;
/* normal I/O */
if (vme->blt_mode == MVME_BLT_NONE) {
for (i=0 ; i<n_bytes ; i++)
status = CAENVME_ReadCycle(hvme, vme_addr, dst+i, vme->am, cvD8);
n = n_bytes;
/* D32 BLT */
} else if (vme->blt_mode == MVME_BLT_BLT32)
status |= CAENVME_BLTReadCycle(hvme, vme_addr, dst, n_bytes, vme->am, cvD32, &n);
/* D32 FIFO BLT */
else if (vme->blt_mode == MVME_BLT_BLT32FIFO) {
CAENVME_GetFIFOMode(hvme, &cfifo);
status = CAENVME_SetFIFOMode(hvme, 1);
status |= CAENVME_BLTReadCycle(hvme, vme_addr, dst, n_bytes, vme->am, cvD32, &n);
CAENVME_SetFIFOMode(hvme, cfifo);
/* D64 MBLT */
} else if (vme->blt_mode == MVME_BLT_MBLT64)
status |= CAENVME_MBLTReadCycle(hvme, vme_addr, dst, n_bytes, vme->am, &n);
/* D64 FIFO MBLT */
else if (vme->blt_mode == MVME_BLT_MBLT64FIFO) {
CAENVME_GetFIFOMode(hvme, &cfifo);
status = CAENVME_SetFIFOMode(hvme, 1);
status |= CAENVME_MBLTReadCycle(hvme, vme_addr, dst, n_bytes, vme->am, &n);
CAENVME_SetFIFOMode(hvme, cfifo);
}
if (status != cvSuccess)
n = 0;
}
return n;
}
/*------------------------------------------------------------------*/
unsigned int mvme_read_value(MVME_INTERFACE *vme, mvme_addr_t vme_addr)
{
unsigned int data;
int status=0;
int hvme;
hvme = vme->handle;
data = 0;
/* D8 */
if (vme->dmode == MVME_DMODE_D8)
status = CAENVME_ReadCycle(hvme, vme_addr, &data, vme->am, cvD8);
/* D16 */
else if (vme->dmode == MVME_DMODE_D16)
status = CAENVME_ReadCycle(hvme, vme_addr, &data, vme->am, cvD16);
/* D32 */
else if (vme->dmode == MVME_DMODE_D32)
status = CAENVME_ReadCycle(hvme, vme_addr, &data, vme->am, cvD32);
return data;
}
/*------------------------------------------------------------------*/
int mvme_set_am(MVME_INTERFACE *vme, int am)
{
vme->am = am;
return MVME_SUCCESS;
}
/*------------------------------------------------------------------*/
int mvme_get_am(MVME_INTERFACE *vme, int *am)
{
*am = vme->am;
return MVME_SUCCESS;
}
/*------------------------------------------------------------------*/
int mvme_set_dmode(MVME_INTERFACE *vme, int dmode)
{
vme->dmode = dmode;
return MVME_SUCCESS;
}
/*------------------------------------------------------------------*/
int mvme_get_dmode(MVME_INTERFACE *vme, int *dmode)
{
*dmode = vme->dmode;
return MVME_SUCCESS;
}
/*------------------------------------------------------------------*/
int mvme_set_blt(MVME_INTERFACE *vme, int mode)
{
vme->blt_mode = mode;
return MVME_SUCCESS;
}
/*------------------------------------------------------------------*/
int mvme_get_blt(MVME_INTERFACE *vme, int *mode)
{
*mode = vme->blt_mode;
return MVME_SUCCESS;
}
|