#include <ctype.h>
 
#include <stdio.h>
 
#include <stdlib.h>
 
#include <unistd.h>
 
#include "pcivme_ni.h"
 
#include "wienvme_dll.h"
 
//----------- DEFINES -----------------------------------------------------
 
#define DEVICENAME_LINUX "/dev/vmemm_1"     // a device name 'template' for WINNT
 
 
 
 
 
 
 
int hHandle24, hHandle32, hHandle16; 
 
int VMEerrors=0;
 
 
 
void VME_load (char* module_path)
 
{
 
  VMEerrors=0;
 
}
 
 
 
int VME_open (int* hHandle, unsigned char AddMod, char* device_name,
 
                 unsigned short module_number)
 
{
 
  int   result;
 
  char dname[256];
 
/* open a path to a device. */
 
  if (device_name 
== NULL
) sprintf(dname
,"%s", DEVICENAME_LINUX
);  
  else sprintf(dname
,"%s", device_name
);  
                
 
  result = VMEinit(dname, module_number, AddMod, hHandle);
 
  if (result) {
 
    printf("Can't open interface \"%s\" to VMEMM-module ! err=%d\n", dname
, result
);  
    printf("module_number %d, AddMod %d, hHandle %d\n",module_number
, AddMod
, *hHandle
);  
  }
 
  return(result);
 
}
 
 
 
int VME_open24 (void)
 
{
 
  return (VME_open (&hHandle24, Std_NoPriv_Data, NULL, 1));
 
}
 
 
 
int VME_open32 (void)
 
{
 
  return (VME_open (&hHandle32, Ext_NoPriv_Data, NULL, 1));
 
}
 
 
 
int VME_open16 (void)
 
{
 
  return (VME_open (&hHandle16, Short_NoPriv, NULL, 1));
 
}
 
 
 
int VME_start (char* module_path)
 
{
 
  VME_load(module_path);
 
  VME_open24();
 
  VME_open32();
 
  VME_open16();
 
  return 0;
 
}
 
 
 
void VME_unload ()
 
{
 
  
 
  return;
 
}
 
 
 
int VME_close (int hHandle)
 
{
 
  int result;
 
 
 
/* close the opened path */
 
  //printf("VME_Close at addr=%d\n",hHandle);
 
 
 
  result = VMEclose(hHandle); 
 
  if (result) {
 
    printf("Can't close interface!\n");  
  }
 
  return (result);
 
}
 
 
 
int VME_close24 ()
 
{
 
  return (VME_close (hHandle24));
 
}
 
 
 
int VME_close32 ()
 
{
 
  return (VME_close (hHandle32));
 
}
 
 
 
int VME_close16 ()
 
{
 
  return (VME_close (hHandle16));
 
}
 
 
 
 
 
int VME_stop ()
 
{
 
  VME_close24();
 
  VME_close32();
 
  VME_close16();
 
  VME_unload();
 
  
 
  return 0;
 
}
 
 
 
int VME_reset ()
 
{
 
  int result;
 
 
 
/* close the opened path */
 
  result = VMEreset(hHandle16); 
 
  if (result) {
 
    printf("Can't reset interface A16!\n");  
  }
 
  result = VMEreset(hHandle24); 
 
  if (result) {
 
    printf("Can't reset interface A24!\n");  
  }
 
  result = VMEreset(hHandle32); 
 
  if (result) {
 
    printf("Can't reset interface A32!\n");  
  }
 
  return (result);
 
}
 
 
 
int VME_read8 (int hHandle, unsigned long n, unsigned long at, void* buff)
 
{
 
  int result;
 
 
 
/* D8 read */
 
  result = VMEread(hHandle, at, 1, n, buff);
 
  if (result) {
 
    VMEerrors++;
 
    printf("D8 read at 0x%lX failed!\n", at
);  
  }
 
//  printf("0x%X, 0x%X, 0x%X, 0x%X\n", hHandle, at, n, * (unsigned short *) buff);
 
  return (result);
 
}
 
 
 
int VME_read16 (int hHandle, unsigned long n, unsigned long at, void* buff)
 
{
 
  int result;
 
 
 
/* D16 read */
 
  result = VMEread(hHandle, at, 2, n, buff);
 
  if (result) {
 
    VMEerrors++;
 
    printf("D16 read at 0x%lX failed!\n", at
);  
  }
 
//  printf("0x%X, 0x%X, 0x%X, 0x%X\n", hHandle, at, n, * (unsigned short *) buff);
 
  return (result);
 
}
 
 
 
int VME_read32 (int hHandle, unsigned long n, unsigned long at, void* buff)
 
{
 
  int result;
 
 
 
/* D32 read */
 
  result = VMEread(hHandle, at, 4, n, buff);
 
  if (result) {
 
    VMEerrors++;
 
    printf("D32 read at 0x%lX failed!\n", at
);  
  }
 
  //printf("0x%X, 0x%X, 0x%X, 0x%X\n", hHandle, at, n, * (unsigned short *) buff);
 
  return (result);
 
}
 
 
 
int VME_write8 (int hHandle, unsigned long n, unsigned long at, void* buff)
 
{
 
  int result;
 
 
 
/* D8 write */
 
  result = VMEwrite(hHandle, at, 1, n, buff);
 
  if (result) {
 
    VMEerrors++;
 
    printf("D8 write at 0x%lX failed!\n", at
);  
  }
 
  return (result);
 
}
 
 
 
int VME_write16 (int hHandle, unsigned long n, unsigned long at, void* buff)
 
{
 
  int result;
 
 
 
/* D16 write */
 
  result = VMEwrite(hHandle, at, 2, n, buff);
 
  if (result) {
 
    VMEerrors++;
 
    printf("D16 write at 0x%lX failed!\n", at
);  
  }
 
  return (result);
 
}
 
 
 
int VME_write32 (int hHandle, unsigned long n, unsigned long at, void* buff)
 
{
 
  int result;
 
 
 
/* D32 write */
 
  result = VMEwrite(hHandle, at, 4, n, buff);
 
  if (result) {
 
    VMEerrors++;
 
    printf("D32 write at 0x%lX failed!\n", at
);  
  }
 
  //printf("D32 write at 0x%X buff=0x%X\n", at,((int*) buff)[0]);
 
  return (result);
 
}
 
 
 
 
 
#ifdef MAIN
 
int GetHandle(int w){
 
switch (w){
 
case 16: return hHandle16;
 
case 24: return hHandle24;
 
case 32: return hHandle24;
 
 
 
}
 
return 24;
 
}
 
 
 
int main(int argc, char **argv){
 
 unsigned long address=0x32100000, value;
 
 unsigned char modifier=24;
 
 int hHandle, result=0;
 
 int width=4;
 
 int c;
 
     
 
       opterr = 0;
 
       VME_start(NULL);
 
       while ((c = getopt (argc, argv, "a:w:rm:osd:")) != -1)
 
         switch (c)
 
           {
 
          case 's':
 
            VME_reset();
 
            break;             // reset
 
          case 'a':
 
            break;             // address
 
          case 'm':
 
            modifier 
= strtoul (optarg
,NULL
,0); 
            break;             // modifier
 
          case 'd':
 
            break;             // data width
 
          case 'w':
 
             hHandle = GetHandle(modifier);
 
             result = VMEwrite(hHandle, address, width, 1, &value);
 
             if (result
) printf("A%d D%d write at 0x%lX failed! result=%d err=%d\n", modifier
, width
*8, address
, result
, GetLastError
(hHandle
));  
             break;            // write
 
          case 'r':
 
           hHandle = GetHandle(modifier);
 
           result = VMEread(hHandle, address, width, 1, &value);
 
           if (result
) printf("A%d D%d read at 0x%lX failed! result=%d err=%d\n", modifier
,width
*8, address
, result
, GetLastError
(hHandle
));  
           break;              // read
 
           default:
 
             printf("Unknown command %c\n",c
);  
           }
 
 
 
      VME_stop();
 
}
 
#endif