Subversion Repositories f9daq

Rev

Rev 135 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include "wienvme_dll.h"
  4.  
  5. #ifdef _CVI_
  6. #include <utility.h>
  7. #endif
  8.  
  9. #ifdef LINUX
  10. #include <dlfcn.h>
  11. #define LoadLibrary(x) dlopen( (x), RTLD_NOW );
  12. #define GetProcAddress(x,y) dlsym((x),(y))
  13. static int  GetCurrentPlatform(void ) {
  14.   return 0;
  15. };
  16. #define __stdcall
  17. #define UCHAR unsigned char
  18. #define HINSTANCE void *
  19. #define  _VI_FUNC
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <stdint.h>
  23. #include <string.h>
  24. #endif
  25.  
  26. //----------- DEFINES -----------------------------------------------------
  27. #define DEVICENAME_LINUX "/dev/pcivme_0"     // a device name 'template' for LINUX
  28. #define DEVICENAME_NT "\\\\.\\PCIVME:\\VMEMMxx"     // a device name 'template' for WINNT
  29. #define DEVICENAME_9X "\\\\.\\C:\\windows\\system\\VWIENVMED.vxd" // the same for WIN95/98
  30. #define MODULE_NUMBER     1 // number of connected CC32 module
  31.  
  32. #define VMEinit (*vme_init)
  33. static int VMEinit (const char*, unsigned short, unsigned char, int*);
  34. #define VMEread (*vme_read)
  35. static int VMEread (int, unsigned long, unsigned char, unsigned long, void*);
  36. #define VMEwrite (*vme_write)
  37. static int VMEwrite (int, unsigned long, unsigned char, unsigned long, void*);
  38. #define VMEreset (*vme_reset)
  39. static int VMEreset (int);
  40. #define VMEclose (*vme_close)
  41. static int VMEclose (int);
  42.  
  43. int hHandle24, hHandle32;
  44. int VMEmodule;
  45.  
  46. static HINSTANCE DLLHandle;
  47. void WIENVME_load (char* module_path) {
  48.   /*
  49.    int stat;
  50.  
  51.    if (module_path == NULL)
  52.      VMEmodule = LoadExternalModule
  53.        ("c:\\home\\CVI\\instr\\WIENVME_DLL\\pcivme_ni.lib");
  54.     else
  55.      VMEmodule = LoadExternalModule (module_path);
  56.  
  57.    vme_init = GetExternalModuleAddr (VMEmodule, "VMEinit", &stat);
  58.    vme_read = GetExternalModuleAddr (VMEmodule, "VMEread", &stat);
  59.    vme_write = GetExternalModuleAddr (VMEmodule, "VMEwrite", &stat);
  60.    vme_reset = GetExternalModuleAddr (VMEmodule, "VMEreset", &stat);
  61.    vme_close = GetExternalModuleAddr (VMEmodule, "VMEclose", &stat);
  62.   */
  63.   if (module_path == NULL) {
  64.     DLLHandle = LoadLibrary("c:\\home\\CVI\\instr\\WIENVME_DLL\\pcivme_ni.dll");
  65.   } else {
  66.     DLLHandle = LoadLibrary(module_path);
  67.   }
  68.   if (!DLLHandle) {
  69.     printf ("\n\nFailed to Open libxxusb.dll  \n");
  70.     return;
  71.   }
  72.  
  73.   vme_init = (void *) GetProcAddress (DLLHandle , "VMEinit");
  74.   vme_read = (void *) GetProcAddress (DLLHandle , "VMEread");
  75.   vme_write =(void *) GetProcAddress (DLLHandle , "VMEwrite");
  76.   vme_reset =(void *) GetProcAddress (DLLHandle , "VMEreset");
  77.   vme_close =(void *) GetProcAddress (DLLHandle , "VMEclose");
  78.  
  79.   return;
  80. }
  81.  
  82. int WIENVME_open (int* hHandle, unsigned char AddMod, char* device_name,
  83.                   unsigned short module_number) {
  84.   int result;
  85.  
  86.   /* open a path to a device. */
  87.   if (device_name == NULL)
  88.     switch (GetCurrentPlatform ()) {
  89.       case 0:
  90.         device_name = DEVICENAME_LINUX;
  91.         break;
  92.       case 2:
  93.         device_name = DEVICENAME_9X;
  94.         break;
  95.       case 3:
  96.         device_name = DEVICENAME_NT;
  97.         break;
  98.       default:
  99.         break;
  100.     }
  101.   result = VMEinit(device_name, module_number, AddMod, hHandle);
  102.   if (result) {
  103.     printf("Can't open interface \"%s\" to VMEMM-module \"%d\"!\n", DEVICENAME_NT, MODULE_NUMBER);
  104.   }
  105.   return(result);
  106. }
  107.  
  108. int WIENVME_open24 (void) {
  109.   return (WIENVME_open (&hHandle24, Std_NoPriv_Data, NULL, 1));
  110. }
  111.  
  112. int WIENVME_open32 (void) {
  113.   return (WIENVME_open (&hHandle32, Ext_NoPriv_Data, NULL, 1));
  114. }
  115.  
  116. int WIENVME_start (char* module_path) {
  117.   WIENVME_load(module_path);
  118.   WIENVME_open24();
  119.   WIENVME_open32();
  120.  
  121.   return 0;
  122. }
  123.  
  124. void WIENVME_unload () {
  125.   //UnloadExternalModule (VMEmodule);
  126.   return;
  127. }
  128.  
  129. int WIENVME_close (int hHandle) {
  130.   int result;
  131.  
  132.   /* close the opened path */
  133.   printf("hHandle %d\n",hHandle);
  134.  
  135.   result = VMEclose(hHandle);
  136.   if (result) {
  137.     printf("Can't close interface!\n");
  138.   }
  139.   return (result);
  140. }
  141.  
  142. int WIENVME_close24 () {
  143.   return (WIENVME_close (hHandle24));
  144. }
  145.  
  146. int WIENVME_close32 () {
  147.   return (WIENVME_close (hHandle32));
  148. }
  149.  
  150. int WIENVME_stop () {
  151.   WIENVME_close24();
  152.   WIENVME_close32();
  153.   WIENVME_unload();
  154.  
  155.   return 0;
  156. }
  157.  
  158. int WIENVME_reset () {
  159.   int result;
  160.  
  161.   /* close the opened path */
  162.   result = VMEreset(hHandle24);
  163.   if (result) {
  164.     printf("Can't reset interface!\n");
  165.   }
  166.   return (result);
  167. }
  168.  
  169. int WIENVME_read8 (int hHandle, unsigned long n, unsigned long at, void* buff) {
  170.   int result;
  171.  
  172.   /* D8 read */
  173.   result = VMEread(hHandle, at, 1, n, buff);
  174.   if (result) {
  175.     printf("D8 read at 0x%lX failed!\n", at);
  176.   }
  177. //  printf("0x%X, 0x%X, 0x%X, 0x%X\n", hHandle, at, n, * (unsigned short *) buff);
  178.   return (result);
  179. }
  180.  
  181. int WIENVME_read16 (int hHandle, unsigned long n, unsigned long at, void* buff) {
  182.   int result;
  183.  
  184.   /* D16 read */
  185.   result = VMEread(hHandle, at, 2, n, buff);
  186.   if (result) {
  187.     printf("D16 read at 0x%lX failed!\n", at);
  188.   }
  189. //  printf("0x%X, 0x%X, 0x%X, 0x%X\n", hHandle, at, n, * (unsigned short *) buff);
  190.   return (result);
  191. }
  192.  
  193. int WIENVME_read32 (int hHandle, unsigned long n, unsigned long at, void* buff) {
  194.   int result;
  195.  
  196.   /* D32 read */
  197.   result = VMEread(hHandle, at, 4, n, buff);
  198.   if (result) {
  199.     printf("D32 read at 0x%lX failed!\n", at);
  200.   }
  201.   //printf("0x%X, 0x%X, 0x%X, 0x%X\n", hHandle, at, n, * (unsigned short *) buff);
  202.   return (result);
  203. }
  204.  
  205. int WIENVME_write8 (int hHandle, unsigned long n, unsigned long at, void* buff) {
  206.   int result;
  207.  
  208.   /* D8 write */
  209.   result = VMEwrite(hHandle, at, 1, n, buff);
  210.   if (result) {
  211.     printf("D8 write at 0x%lX failed!\n", at);
  212.   }
  213.   return (result);
  214. }
  215.  
  216. int WIENVME_write16 (int hHandle, unsigned long n, unsigned long at, void* buff) {
  217.   int result;
  218.  
  219.   /* D16 write */
  220.   result = VMEwrite(hHandle, at, 2, n, buff);
  221.   if (result) {
  222.     printf("D16 write at 0x%lX failed!\n", at);
  223.   }
  224.   return (result);
  225. }
  226.  
  227. int WIENVME_write32 (int hHandle, unsigned long n, unsigned long at, void* buff) {
  228.   int result;
  229.  
  230.   /* D32 write */
  231.   result = VMEwrite(hHandle, at, 4, n, buff);
  232.   if (result) {
  233.     printf("D32 write at 0x%lX failed!\n", at);
  234.   }
  235.   //printf("D32 write at 0x%X buff=0x%X\n", at,((int*) buff)[0]);
  236.   return (result);
  237. }
  238.  
  239.  
  240. #ifndef VME_D32
  241.  
  242. #define VME_D8  0x1
  243. #define VME_D16 0x2
  244. #define VME_D32 0x4
  245. #endif
  246.  
  247. short __stdcall WIENVME_VME_R( uint16_t AM, uint16_t DW,  uint32_t VME_Address, uint32_t *Data) {
  248.   int hHandle=0, nb=0;
  249.   switch (AM) {
  250.     case Ext_NoPriv_Data:
  251.       hHandle = hHandle32;
  252.       break;
  253.     case Std_NoPriv_Data:
  254.       hHandle = hHandle24;
  255.       break;
  256.     default :
  257.       return 0;
  258.   }
  259.  
  260.  
  261.   switch (DW) {
  262.     case VME_D16:
  263.       nb=  WIENVME_read16 (hHandle, 1, (unsigned long) VME_Address, (void*) Data)  ;
  264.       break;
  265.     case VME_D32:
  266.       nb=  WIENVME_read32 (hHandle, 1, (unsigned long) VME_Address, (void*) Data)  ;
  267.       break;
  268.  
  269.     default:
  270.       return 0;
  271.   }
  272.   return (short)nb;
  273. }
  274.  
  275. short __stdcall WIENVME_VME_W( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t Data) {
  276.   int hHandle=0, nb=0;
  277.   switch (AM) {
  278.     case Ext_NoPriv_Data:
  279.       hHandle = hHandle32;
  280.       break;
  281.     case Std_NoPriv_Data:
  282.       hHandle = hHandle24;
  283.       break;
  284.     default :
  285.       return 0;
  286.   }
  287.  
  288.  
  289.   switch (DW) {
  290.     case VME_D16:
  291.       nb=  WIENVME_write16 (hHandle, 1, (unsigned long) VME_Address, (void*) &Data)  ;
  292.       break;
  293.     case VME_D32:
  294.       nb=  WIENVME_write32 (hHandle, 1, (unsigned long) VME_Address, (void*) &Data)  ;
  295.       break;
  296.  
  297.     default:
  298.       return 0;
  299.   }
  300.   return (short)nb;
  301. }
  302.  
  303. short __stdcall WIENVME_VME_MW( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t Data) {
  304.  
  305.   return WIENVME_VME_W( AM, DW, VME_Address, Data);
  306. }
  307. short __stdcall WIENVME_VME_MWRST( void ) {
  308.  
  309.  
  310.   return 0;
  311. }
  312. short __stdcall WIENVME_VME_MWEXEC( void ) {
  313.  
  314.  
  315.   return 0;
  316. }
  317.  
  318. short __stdcall WIENVME_VME_MR( uint16_t AM, uint16_t DW, uint32_t VME_Address, uint32_t *Data) {
  319.  
  320.  
  321.   return WIENVME_VME_R( AM, DW, VME_Address, Data);
  322. }
  323. short __stdcall WIENVME_VME_MRRST( void ) {
  324.  
  325.  
  326.   return 0;
  327. }
  328. short __stdcall WIENVME_VME_MREXEC(  uint32_t *Data  ) {
  329.  
  330.  
  331.   return 0;
  332. }
  333.