It seems that you can just remove {$LINKLIB c} in libusb.pas.

The libusb from libusb.info cannot be used because the functions it provides are different, and if you try to recompile a version before 1.0 like 0.9.4, it fails because Windows target was not yet supported by the library.
But on  https://sourceforge.net/projects/libusb-win32/  you find a port to Windows of libusb-0.1 to Windows.
I downloaded libusb-win32-bin-1.2.6.0.zip and extracted it. From the subdirectory libusb-win32-bin-1.2.6.0\bin\amd64 I copied libusb0.dll to the directory of TestFirmware.pas.
In LibUSB.pas I changed {$LINKLIB usb} to {$LINKLIB libusb0.dll}.
I got a few errors on compilation because of some changes in libusb.
The variables usb_error_errno and usb_error_type doesn't exist anymore in the library, for now I just comment them out:
    usb_error_errno : LongInt; //cvar; external; { from libusb }
    usb_error_type  : LongInt; //cvar; external; { from libusb }

Two functions are not available any more, I commented them too:
 //Function usb_get_driver_np          (dev:PUSBDevHandle; TheInterface:Longint; Var name; namelen:LongInt) : Longint; cdecl; external;
 //Function usb_detach_kernel_driver_np(dev:PUSBDevHandle; TheInterface:Longint) : LongInt; cdecl; external;

This breaks in USB.pas the function USBGetDriver and the method TUSBInterface.Claim. For now I just made a small modification to comment the calls.
I join the diffs for libusb.pas and usb.pas .

With this modifications, I could compile successfully TestFirmware.pas. On execution I got this error:
E:\03_travail\libusb\test>TestFirmware.exe
Couldn't connect to device: Couldn't find firmware file "firmware.ihx" in search path ".:~/.testfirmware:(my path variable here):/etc/testfirmware". You might try to set the environment variable "TESTFIRMWAREFIRMWAREPATH".
which is normal because I don't have any test case with a firmware file for now.

 src/libusb.pas | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/src/libusb.pas b/src/libusb.pas
index 537f249..45331de 100644
--- a/src/libusb.pas
+++ b/src/libusb.pas
@@ -2,8 +2,9 @@ unit LibUSB;
 
 Interface
 
-{$LINKLIB c}
-{$LINKLIB usb}
+//{$LINKLIB c}
+//{$LINKLIB usb}
+{$LINKLIB libusb0.dll}
 
 {
   Automatically converted by H2Pas 0.99.15 from usb.h
@@ -265,8 +266,8 @@ Const
    USB_ERROR_BEGIN = 500000;
 
 Var (* errno : LongInt; cvar; external; { from libc } *)
-    usb_error_errno : LongInt; cvar; external; { from libusb }
-    usb_error_type  : LongInt; cvar; external; { from libusb }
+    usb_error_errno : LongInt; //cvar; external; { from libusb }
+    usb_error_type  : LongInt; //cvar; external; { from libusb }
     usb_error_str   : PChar;   cvar; external; { from libusb }
 
 Type PUSBBus = ^USBBus;
@@ -346,9 +347,9 @@ Function usb_device(Dev:PUSBDevHandle):PUSBDevice; cdecl; external;
 
 Function usb_get_busses : PUSBBus; cdecl; external;
 
-Function usb_get_driver_np          (dev:PUSBDevHandle; TheInterface:Longint; Var name; namelen:LongInt) : Longint; cdecl; external;
+//Function usb_get_driver_np          (dev:PUSBDevHandle; TheInterface:Longint; Var name; namelen:LongInt) : Longint; cdecl; external;
 
-Function usb_detach_kernel_driver_np(dev:PUSBDevHandle; TheInterface:Longint) : LongInt; cdecl; external;
+//Function usb_detach_kernel_driver_np(dev:PUSBDevHandle; TheInterface:Longint) : LongInt; cdecl; external;
 
 Implementation
 
 src/usb.pas | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/src/usb.pas b/src/usb.pas
index 62d2db3..8aa2e6e 100644
--- a/src/usb.pas
+++ b/src/usb.pas
@@ -28,7 +28,9 @@ Unit USB;
 Interface
 
 Uses
-  Classes, SysUtils, Baseunix, LibUSB;
+  Classes, SysUtils,
+  //Baseunix,
+  LibUSB;
 
 (*****************************************************************************)
 (**  Procedural Interface  ***************************************************)
@@ -241,6 +243,8 @@ Uses PasLibUsbUtils,Math;
 Type TUSBExceptionType = Class of Exception;
 
 Function USBException(Proc:String;Ret:LongInt) : Exception;
+//duplicated from errno.inc included in unit BaseUnix
+const ESysETIMEDOUT= 110;
 Var Ex : TUSBExceptionType;
 Begin
   Case -Ret of
@@ -515,6 +519,7 @@ Begin
 End;
 
 Function USB_Send(Hnd:PUSBDevHandle;EP:LongInt;Const Buf;Length,Timeout:LongInt):LongInt;
+const ESysEAGAIN=11;
 Var Loops  : Integer;
     Sent   : LongInt;
     ToSend : LongInt;
@@ -540,7 +545,8 @@ WriteLn('Problem with usb_bulk_write: Result = ',Result,', usb_error_errno = ',u
         if (Loops > 0) and (usb_error_errno = ESysEAGAIN) then
           Begin
             Again := True;
-            fpSelect(0,Nil,Nil,Nil,100);
+            //fpSelect(0,Nil,Nil,Nil,100);
+            Sleep(100);
           End;
       End
     else
@@ -573,11 +579,14 @@ Begin
 End;
 
 Function USBGetDriver(Handle:PUSBDevHandle;Intf:Integer):String;
+const ESysENODATA= 61;
 Var R : Integer;
 Const MaxLen = 100;
 Begin
+     result:= '';
+     exit;
   SetLength(Result,MaxLen);
-  R := usb_get_driver_np(Handle,Intf,Result[1],MaxLen);  // puts a 0-terminated string to St[1], returns 0 on success
+  //R := usb_get_driver_np(Handle,Intf,Result[1],MaxLen);  // puts a 0-terminated string to St[1], returns 0 on success
   if R = -ESysENODATA then
     Exit('');   // no data available, so no driver bound
   if R < 0 then
@@ -656,6 +665,7 @@ Begin
   usb_close(Handle);
 End;
 
+const ESysEBUSY=16;
 Procedure TUSBDevice.Open;
 Var R  : Integer;
     I  : Integer;
@@ -816,9 +826,10 @@ Begin
         raise EUSBError.CreateFmt('Couldn''t claim interface %d but no driver was reported.',[FInterface^.bInterfaceNumber]);
       WriteLn('Couldn''t claim interface because is claimed by another driver "',St,'", trying to detach.');
       { detach driver }
-      R := usb_detach_kernel_driver_np(FDevice.FHandle,FInterface^.bInterfaceNumber);
-      if R < 0 then
-        raise USBException('usb_detach_kernel_driver_np',R);
+      //R := usb_detach_kernel_driver_np(FDevice.FHandle,FInterface^.bInterfaceNumber);
+      //if R < 0 then
+      //  raise USBException('usb_detach_kernel_driver_np',R);
+
       { claim again }
       R := usb_claim_interface(FDevice.FHandle,FInterface^.bInterfaceNumber);
       if R < 0 then
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to