* * *

Author Topic: libusb 1.0 unit  (Read 17075 times)

medamarko

  • New member
  • *
  • Posts: 8
libusb 1.0 unit
« on: December 15, 2010, 07:56:54 am »
Hello I created simple libusb 1.0 wrapper.
It is not fully tested  %).
I worked with interrupt and bulk transfers without any problems.
And in following project i will use iso transfer as well.
Testing hw:
http://www.mikroe.com/eng/products/view/587/mikrommb-for-pic24-board/ - PIC24FJ256GB110 with simple usb comm firmware.

Unit is in attachment.

Best Regards
Marko Medic

medamarko

  • New member
  • *
  • Posts: 8
Re: libusb 1.0 unit
« Reply #1 on: December 20, 2010, 12:13:04 pm »
I've created simple HID example.
It is posted on blog:
http://mcuprogramming.blogspot.com/

Best Regards
Marko Medic

fredycc

  • Sr. Member
  • ****
  • Posts: 265
Re: libusb 1.0 unit
« Reply #2 on: December 21, 2010, 12:35:55 am »
Thank you..  :o

Regards

medamarko

  • New member
  • *
  • Posts: 8
Re: libusb 1.0 unit
« Reply #3 on: March 09, 2011, 12:23:29 pm »
I added new post on blog.
If someone is interested in MCU programming and prefer Pascal language.
http://mcuprogramming.blogspot.com/2011/03/intro-to-delphi-lazarus-for-embeded.html
Maybe someone can find this useful.

Best Regards
Marko Medic

warcode

  • New member
  • *
  • Posts: 36
Re: libusb 1.0 unit
« Reply #4 on: March 26, 2012, 10:03:57 pm »
Hi all

Im trying to using  this header convertion of libusb 1.0 but i can make it work for windows, can  someone  give me some tips for making it work
i wanna put the libusb-1.0.dll at the same folder that my application, Thnaks for any usefull help

unit libusb;
interface

{$LINKLIB /usr/lib/libusb-1.0.a}
{-- $LINKLIB libusb-1.0.dll}

//******************************************************************************
//
//  * Project name:
//       Translation of libusb 1.0 header for pascal
//  * Copyright:
//       (C) 2010 Marko Medic <medamarko@gmail.com>
//  * Revision History:
//       20101126:
//        - initial release;
//  * Test Configuration:
//       - Lazarus IDE 0.9.28.2
//       - fpc 2.4
//       - few different USB devices based on PIC MCU
//
//******************************************************************************
    { Pointers to basic pascal types}
    Type
      PLongint  = ^Longint;
      PSmallInt = ^SmallInt;
      PByte     = ^Byte;
      PWord     = ^Word;
      PDWord    = ^DWord;
      PDouble   = ^Double;
      uint8_t  = byte;
      uint16_t = word;
      uint32_t = dword;
      size_t    = uint32_t;

//------------------------------------------------------------------------------
// Constant
//------------------------------------------------------------------------------
{(* standard USB stuff *)}
{(* Device and/or Interface Class codes *)}
const LIBUSB_CLASS_PER_INTERFACE = 0;
const LIBUSB_CLASS_AUDIO = 1;
const LIBUSB_CLASS_COMM = 2;
const LIBUSB_CLASS_HID = 3;
const LIBUSB_CLASS_PRINTER = 7;
const LIBUSB_CLASS_PTP = 6;
const LIBUSB_CLASS_MASS_STORAGE = 8;
const LIBUSB_CLASS_HUB = 9;
const LIBUSB_CLASS_DATA = 10;
const LIBUSB_CLASS_WIRELESS = $e0;
const LIBUSB_CLASS_APPLICATION = $fe;
const LIBUSB_CLASS_VENDOR_SPEC = $ff;

{(* Descriptor types as defined by the USB specification. *)}
const LIBUSB_DT_DEVICE = $01;
const LIBUSB_DT_CONFIG = $02;
const LIBUSB_DT_STRING = $03;
const LIBUSB_DT_INTERFACE = $04;
const LIBUSB_DT_ENDPOINT = $05;
const LIBUSB_DT_HID = $21;
const LIBUSB_DT_REPORT = $22;
const LIBUSB_DT_PHYSICAL = $23;
const LIBUSB_DT_HUB = $29;

{(* Descriptor sizes per descriptor type *)}
const LIBUSB_DT_DEVICE_SIZE =          18;
const LIBUSB_DT_CONFIG_SIZE =          9;
const LIBUSB_DT_INTERFACE_SIZE =       9;
const LIBUSB_DT_ENDPOINT_SIZE =       7;
const LIBUSB_DT_ENDPOINT_AUDIO_SIZE =    9;   {* Audio extension *}
const LIBUSB_DT_HUB_NONVAR_SIZE =       7;
const LIBUSB_ENDPOINT_ADDRESS_MASK =    $0f;    {* in bEndpointAddress *}
const LIBUSB_ENDPOINT_DIR_MASK =       $80;

{* Endpoint direction. Values for bit 7 of the ref libusb_endpoint_descriptor.bEndpointAddress 'endpoint address' scheme. *}
const LIBUSB_ENDPOINT_IN = $80;
const LIBUSB_ENDPOINT_OUT = $00;

const LIBUSB_TRANSFER_TYPE_MASK =          $03;    {* in bmAttributes *}

{* Endpoint transfer cType. Values for bits 0:1 of the ref libusb_endpoint_descriptor.bmAttributes 'endpoint attributes' field.*}
const LIBUSB_TRANSFER_TYPE_CONTROL = 0;
const LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1;
const LIBUSB_TRANSFER_TYPE_BULK = 2;
const LIBUSB_TRANSFER_TYPE_INTERRUPT = 3;

{* Standard requests, as defined in table 9-3 of the USB2 specifications *}
const LIBUSB_REQUEST_GET_STATUS = $00;
const LIBUSB_REQUEST_CLEAR_FEATURE = $01;
const LIBUSB_REQUEST_SET_FEATURE = $03;
const LIBUSB_REQUEST_SET_ADDRESS = $05;
const LIBUSB_REQUEST_GET_DESCRIPTOR = $06;
const LIBUSB_REQUEST_SET_DESCRIPTOR = $07;
const LIBUSB_REQUEST_GET_CONFIGURATION = $08;
const LIBUSB_REQUEST_SET_CONFIGURATION = $09;
const LIBUSB_REQUEST_GET_INTERFACE = $0A;
const LIBUSB_REQUEST_SET_INTERFACE = $0B;
const LIBUSB_REQUEST_SYNCH_FRAME = $0C;

{* Request cType bits of the ref libusb_control_setup.bmRequestType 'bmRequestType' field in control transfers. *}
const LIBUSB_REQUEST_TYPE_STANDARD = $00 shl 5;
const LIBUSB_REQUEST_TYPE_CLASS = $01 shl 5;
const LIBUSB_REQUEST_TYPE_VENDOR = $02 shl 5;
const LIBUSB_REQUEST_TYPE_RESERVED = $03 shl 5;

{* Recipient bits of the ref libusb_control_setup.bmRequestType 'bmRequestType' field in control transfers. Values 4 through 31 are reserved. *}
const LIBUSB_RECIPIENT_DEVICE = $00;
const LIBUSB_RECIPIENT_INTERFACE = $01;
const LIBUSB_RECIPIENT_ENDPOINT = $02;
const LIBUSB_RECIPIENT_OTHER = $03;

const LIBUSB_ISO_SYNC_TYPE_MASK =       $0C;

{* Synchronization cType for isochronous endpoints. Values for bits 2:3 of the ref libusb_endpoint_descriptor.bmAttributes 'bmAttributes' field in libusb_endpoint_descriptor. *}
const LIBUSB_ISO_SYNC_TYPE_NONE = 0;
const LIBUSB_ISO_SYNC_TYPE_ASYNC = 1;
const LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2;
const LIBUSB_ISO_SYNC_TYPE_SYNC = 3;

const LIBUSB_ISO_USAGE_TYPE_MASK =  $30;

{* Usage cType for isochronous endpoints. Values for bits 4:5 of the ref libusb_endpoint_descriptor.bmAttributes 'bmAttributes' field in libusb_endpoint_descriptor. *}
const LIBUSB_ISO_USAGE_TYPE_DATA = 0;
const LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1;
const LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2;

{* Error codes. Most libusb functions result:= 0 on success or one of these codes on failure.*}
const LIBUSB_SUCCESS = 0;
const LIBUSB_ERROR_IO = -1;
const LIBUSB_ERROR_INVALID_PARAM = -2;
const LIBUSB_ERROR_ACCESS = -3;
const LIBUSB_ERROR_NO_DEVICE = -4;
const LIBUSB_ERROR_NOT_FOUND = -5;
const LIBUSB_ERROR_BUSY = -6;
const LIBUSB_ERROR_TIMEOUT = -7;
const LIBUSB_ERROR_OVERFLOW = -8;
const LIBUSB_ERROR_PIPE = -9;
const LIBUSB_ERROR_INTERRUPTED = -10;
const LIBUSB_ERROR_NO_MEM = -11;
const LIBUSB_ERROR_NOT_SUPPORTED = -12;
const LIBUSB_ERROR_OTHER = -99;

//------------------------------------------------------------------------------
// Structures
//------------------------------------------------------------------------------
{ * Transfer status codes */}
type libusb_transfer_status = (
   //** Transfer completed without error. Note that this does not indicate that the entire amount of requested data was transferred. */
   LIBUSB_TRANSFER_COMPLETED,
   //** Transfer failed */
   LIBUSB_TRANSFER_ERROR,
   //** Transfer timed out */
   LIBUSB_TRANSFER_TIMED_OUT,
   //** Transfer was cancelled */
   LIBUSB_TRANSFER_CANCELLED,
   //** For bulk/interrupt endpoints: halt condition detected (endpoint stalled). For control endpoints: control request not supported. */
   LIBUSB_TRANSFER_STALL,
   //** Device was disconnected */
   LIBUSB_TRANSFER_NO_DEVICE,
   //** Device sent more data than requested */
   LIBUSB_TRANSFER_OVERFLOW
);

{* A structure representing the standard USB device descriptor. This descriptor is documented in section 9.6.1 of the USB 2.0 specification. All multiple-byte fields are represented in host-endian format.*}
type
   Plibusb_device_descriptor = ^libusb_device_descriptor;
   PPlibusb_device_descriptor = ^Plibusb_device_descriptor;
   PPPlibusb_device_descriptor = ^PPlibusb_device_descriptor;
   libusb_device_descriptor = record
      bLength: uint8_t;
      bDescriptorType: uint8_t;
      bcdUSB: uint16_t;
      bDeviceClass: uint8_t;
      bDeviceSubClass: uint8_t;
      bDeviceProtocol: uint8_t;
      bMaxPacketSize0: uint8_t;
      idVendor: uint16_t;
      idProduct: uint16_t;
      bcdDevice: uint16_t;
      iManufacturer: uint8_t;
      iProduct: uint8_t;
      iSerialNumber: uint8_t;
      bNumConfigurations: uint8_t;
   end;

{* A structure representing the standard USB endpoint descriptor. This descriptor is documented in section 9.6.3 of the USB 2.0 specification. All multiple-byte fields are represented in host-endian format.*}
type
    Plibusb_endpoint_descriptor = ^libusb_endpoint_descriptor;
   libusb_endpoint_descriptor = record
      bLength: uint8_t;
      bDescriptorType: uint8_t;
      bEndpointAddress: uint8_t;
      bmAttributes: uint8_t;
      wMaxPacketSize: uint16_t;
      bInterval: uint8_t;
      bRefresh: uint8_t;
      bSynchAddress: uint8_t;
      extra: PByte;
      extra_length: Integer;
   end;

{* A structure representing the standard USB interface descriptor. This descriptor is documented in section 9.6.5 of the USB 2.0 specification. All multiple-byte fields are represented in host-endian format.*}
type
    Plibusb_interface_descriptor = ^libusb_interface_descriptor;
   libusb_interface_descriptor = record
      bLength: uint8_t;
      bDescriptorType: uint8_t;
      bInterfaceNumber: uint8_t;
      bAlternateSetting: uint8_t;
      bNumEndpoints: uint8_t;
      bInterfaceClass: uint8_t;
      bInterfaceSubClass: uint8_t;
      bInterfaceProtocol: uint8_t;
      iInterface: uint8_t;
      endpoint: Plibusb_endpoint_descriptor;
      extra: PByte;
      extra_length: Integer;
   end;

{* A collection of alternate settings for a particular USB interface.*}
type
    Plibusb_interface = ^libusb_interface;
   libusb_interface = record
      altsetting: Plibusb_interface_descriptor;
      num_altsetting: Integer;
   end;

{* A structure representing the standard USB configuration descriptor. This descriptor is documented in section 9.6.3 of the USB 2.0 specification. All multiple-byte fields are represented in host-endian format.*}
type
    Plibusb_config_descriptor = ^libusb_config_descriptor;
    PPlibusb_config_descriptor = ^Plibusb_config_descriptor;
   libusb_config_descriptor = record
      bLength: uint8_t;
      bDescriptorType: uint8_t;
      wTotalLength: uint16_t;
      bNumInterfaces: uint8_t;
      bConfigurationValue: uint8_t;
      iConfiguration: uint8_t;
      bmAttributes: uint8_t;
      MaxPower: uint8_t;
      theinterface: Plibusb_interface;// theInterface (Interface is reserved word)
      extra: PByte;
      extra_length: Integer;
   end;

{ * Setup packet for control transfers. *}
type
   Plibusb_control_setup = ^libusb_control_setup;
   libusb_control_setup = record
      bmRequestType: uint8_t;
      bRequest: uint8_t;
      wValue: uint16_t;
      wIndex: uint16_t;
      wLength: uint16_t;
   end;

const LIBUSB_CONTROL_SETUP_SIZE =  (SizeOf( libusb_control_setup));

{* libusb *}
type
    Plibusb_context = ^libusb_context;
         PPlibusb_context = Plibusb_context;
    libusb_context = record
    {undefined structure}
    end;
type
   Plibusb_device = ^libusb_device;
        PPlibusb_device = ^Plibusb_device;
        PPPlibusb_device = ^PPlibusb_device;
   libusb_device = record
   {undefined structure}
   end;
type
   Plibusb_device_handle = ^libusb_device_handle;
        PPlibusb_device_handle = ^Plibusb_device_handle;
   libusb_device_handle = record
   {undefined structure}
   end;

{* libusb_transfer.flags values *}
const LIBUSB_TRANSFER_SHORT_NOT_OK = 1 shl 0;
const LIBUSB_TRANSFER_FREE_BUFFER = 1 shl 1;
const LIBUSB_TRANSFER_FREE_TRANSFER = 1 shl 2;

{*Isochronous packet descriptor. *}
type
    Plibusb_iso_packet_descriptor = ^libusb_iso_packet_descriptor;
    libusb_iso_packet_descriptor = record
    length: DWORD;
    actual_length: DWORD;
    status: libusb_transfer_status;
    end;

type
    Pstructlibusb_transfer = ^structlibusb_transfer;
    libusb_transfer_cb_fn= procedure(transfer : Pstructlibusb_transfer);
    Plibusb_transfer_cb_fn = ^libusb_transfer_cb_fn;
    structlibusb_transfer = record
         dev_handle: ^libusb_device_handle;
    flags: uint8_t;
    endpoint:byte;
    ctype: byte; // type is reserved word
    timeout: DWORD ;
    status: libusb_transfer_status;
    length: integer;
    actual_length: integer ;
    callback: Plibusb_transfer_cb_fn;
         user_data: pointer;
    buffer:PChar;
    num_iso_packets: integer ;
    iso_packet_desc: plibusb_iso_packet_descriptor;
    end;

//******************************************************************************
//------------------------------------------------------------------------------
// Library functions and procedures
//------------------------------------------------------------------------------
//******************************************************************************
function libusb_init(ctx:PPlibusb_context):longint;cdecl;external;
procedure libusb_exit(ctx:Plibusb_context);cdecl;external;
procedure libusb_set_debug(ctx:Plibusb_context; level:integer);cdecl;external;

function libusb_get_device_list(ctx:Plibusb_context; list: PPPlibusb_device): size_t;cdecl;external;
procedure libusb_free_device_list(list:PPlibusb_device; unref_devices:integer);cdecl;external;
function libusb_get_configuration(dev: Plibusb_device_handle; config: PLongint): Integer;cdecl;external;

function libusb_get_device_descriptor(dev: Plibusb_device; desc:Plibusb_device_descriptor): Integer;cdecl;external;
function libusb_get_active_config_descriptor(dev: Plibusb_device; config:PPlibusb_config_descriptor): Integer;cdecl;external;

function libusb_get_config_descriptor(dev: Plibusb_device;config_index:uint8_t; config: PPlibusb_config_descriptor):Integer;cdecl;external;

function libusb_get_max_packet_size(dev: Plibusb_device; endpoint: Byte): Integer;cdecl;external;
function libusb_get_max_iso_packet_size(dev: Plibusb_device; endpoint: Byte): Integer;cdecl;external;

function libusb_open(dev: Plibusb_device; handle: PPlibusb_device_handle): Integer;cdecl;external;
procedure libusb_close(dev_handle:Plibusb_device_handle);cdecl;external;
function libusb_get_device(dev_handle: Plibusb_device_handle): libusb_device;cdecl;external;

function libusb_set_configuration(dev: Plibusb_device_handle; configuration: Integer): Integer;cdecl;external;

function libusb_claim_interface(dev: Plibusb_device_handle; iface: Integer): Integer;cdecl;external;
function libusb_release_interface(dev: Plibusb_device_handle; iface: Integer): Integer;cdecl;external;

function libusb_open_device_with_vid_pid(ctx: Plibusb_context; vendor_id: uint16_t; product_id: uint16_t): Plibusb_device_handle;cdecl;external;

function libusb_set_interface_alt_setting(dev: Plibusb_device_handle;interface_number: Integer;alternate_setting: Integer): Integer;cdecl;external;
function libusb_clear_halt(dev: Plibusb_device_handle; endpoint: BYTE): Integer;cdecl;external;
function libusb_reset_device(dev: Plibusb_device_handle): Integer;cdecl;external;

function libusb_kernel_driver_active(dev: Plibusb_device_handle; theinterface: Integer): Integer;cdecl;external;
function libusb_detach_kernel_driver(dev: Plibusb_device_handle; theinterface: Integer): Integer;cdecl;external;
function libusb_attach_kernel_driver(dev: Plibusb_device_handle; theinterface: Integer): Integer;cdecl;external;
//------------------------------------------------------------------------------
// Async I/O
//------------------------------------------------------------------------------
function libusb_submit_transfer(transfer: Pstructlibusb_transfer):Integer;cdecl;external;
function libusb_cancel_transfer(transfer: Pstructlibusb_transfer):Integer;cdecl;external;
procedure libusb_free_transfer(transfer: Pstructlibusb_transfer);cdecl;external;
function libusb_alloc_transfer(iso_packets:integer):Pstructlibusb_transfer;cdecl;external;
//------------------------------------------------------------------------------
// Sync I/O
//------------------------------------------------------------------------------
function libusb_control_transfer(dev_handle: Plibusb_device_handle; request_type: uint8_t; request: uint8_t; value: uint16_t; index: uint16_t; data: PChar; length:uint16_t;timeout: DWORD):Integer;cdecl;external;
function libusb_bulk_transfer(dev_handle: Plibusb_device_handle; endpoint:BYTE; data: Pchar; length:integer;actual_length:Plongint; timeout:DWORD):Integer;cdecl;external; //tested
function libusb_interrupt_transfer(dev_handle: Plibusb_device_handle; endpoint:byte; data:Pchar; length:integer; actual_length:Plongint; timeout:DWORD):Integer;cdecl;external;    //tested

function libusb_get_string_descriptor_ascii(dev:plibusb_device_handle ;index:uint8_t; data:Pchar; length:integer):Integer;cdecl;external;

{/* polling and timeouts */}
function libusb_try_lock_events(ctx:Plibusb_context):integer;cdecl;external;
procedure libusb_lock_events(ctx:Plibusb_context);cdecl;external;
procedure libusb_unlock_events(ctx:Plibusb_context);cdecl;external;
function libusb_event_handling_ok(ctx:Plibusb_context):integer;cdecl;external;
function libusb_event_handler_active(ctx:Plibusb_context):integer;cdecl;external;
procedure libusb_lock_event_waiters(ctx:Plibusb_context); cdecl;external;
procedure libusb_unlock_event_waiters(ctx:Plibusb_context); cdecl;external;

implementation

end.

makgab

  • Jr. Member
  • **
  • Posts: 55
libusb 1.0 unit hid?
« Reply #5 on: March 27, 2012, 12:23:49 pm »
Hi!
This unit can access USB HID devices?
The hidapi is not necessary?

(I cannot convert hidapi to pascal...)


makgab

  • Jr. Member
  • **
  • Posts: 55
kernel attach driver
« Reply #6 on: March 28, 2012, 09:25:24 am »
I can compile the example program.
At run time, the program raised exception at "if ( libusb_kernel_driver_active() ) ..." with SIGSEGV violation.

The next packages are installed:
libusb1-1.0.9-0.3.rc1.fc16.x86_64
libusb-devel-0.1.3-9.fc16.x86_64
libusb-0.1.3-9.fc16.x86_64
libusb-static-0.1.3-9.fc16.x86_64
libusb1-static-1.0.9-0.3.rc1.fc16.x86_64
libusb1-devel.x86_64 0:1.0.9-0.3.rc1.fc16

How can attach the driver?

Linux parameters:
~# lsusb
Bus 002 Device 007: ID 04d8:ff91 Microchip Technology, Inc.

~# dmesg
[ 3655.026936] usb 2-1.3: new full-speed USB device number 7 using ehci_hcd
[ 3655.116072] usb 2-1.3: New USB device found, idVendor=04d8, idProduct=ff91
[ 3655.116080] usb 2-1.3: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 3655.116086] usb 2-1.3: Product: PC-DMX Interface 
[ 3655.116090] usb 2-1.3: Manufacturer: Microchip Technology Inc.
[ 3655.118415] generic-usb 0003:04D8:FF91.0002: hiddev0,hidraw1: USB HID v1.11 Device [Microchip Technology Inc. PC-DMX Interface  ] on usb-0000:00:1d.0-1.3/input0

The Linux can see the device.

« Last Edit: April 01, 2012, 11:34:54 am by makgab »

martinrame

  • Jr. Member
  • **
  • Posts: 52
Re: libusb 1.0 unit
« Reply #7 on: January 28, 2013, 09:02:48 pm »
Hi, I'm trying to get a list of USB devices, is this the right way to get the device list?.


Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  lRes: Integer;
  lTotal: size_t;
  I: Integer;
  list: PPPlibusb_device;
  lDevice: Plibusb_device;
  lDesc: Plibusb_device_descriptor;
  lDescriptor: libusb_device_descriptor;
  lMan: byte;

begin
  Memo1.Lines.Clear;
  lRes := libusb_init(nil);
  lTotal := libusb_get_device_list(nil, @list);
  for I := 0 to lTotal - 1 do
  begin
    lDevice := @list[I]^;
    libusb_get_device_descriptor(lDevice, @lDesc);
    lDescriptor := Plibusb_device_descriptor(@lDesc)^;
    lMan := lDescriptor.iManufacturer;
    Memo1.Lines.Add(IntToStr(lMan));
  end;
end;

mai

  • Full Member
  • ***
  • Posts: 133
  • truther
Re: libusb 1.0 unit
« Reply #8 on: October 18, 2017, 09:39:55 pm »
unit libusb;
interface

// {$LINKLIB                             /usr/lib/libusb-1.0.a}
   {$LINKLIB /usr/lib/x86_64-linux-gnu/libusb-1.0.a}

Projekt kompilieren, Ziel: libusbtest: Exit code 256, Fehler: 1
libusbtest.lpr(20,1) Error: Error while linking
Nachrichten, Fehler: 1
Error: Build not successful, skipping signing.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus