Libusb 를 Delphi에서 사용하기위해 usb.h 를 Delphi 용 코드로 변환을 완료하였습니다.

ADC 의 Eagle 칩에서 uClinux 를 사용할 때 U-Boot 와 uClinux(ADC 포팅버전)에서 USB를 통해 파일을 전송하는 USB Downloader 프로그램 대체하여 생산공정에 최적화된 소프트웨어를 만들었고, 테스트 결과 정상 동작하였습니다.

USB Downloader 를 대체할 수 있는 코드는 정리하여 연휴가 끝난 뒤 포스트하도록 하겠습니다.

다운로드 링크 :





추가로 이 코드를 사용하기 위해서는 libusb0.dll 이 필요합니다.
libusb-win32 는 http://libusb-win32.sourceforge.net/ 에서 받으시면 되고, 예제는 libusb-win32 내에 예제를 참고하시면 됩니다.

소스코드 전문 :

unit usb;

interface

uses
  Windows;

{
 * PATH_MAX from limits.h can't be used on Windows if the dll and
 * import libraries are build/used by different compilers
}

const LIBUSB_PATH_MAX = 512;

{
 * USB spec information
 *
 * This is all stuff grabbed from various USB specs and is pretty much
 * not subject to change
}

{
 * Device and/or Interface Class codes
}
const USB_CLASS_PER_INTERFACE = 0; { for DeviceClass }
const USB_CLASS_AUDIO         = 1;
const USB_CLASS_COMM          = 2;
const USB_CLASS_HID           = 3;
const USB_CLASS_PRINTER       = 7;
const USB_CLASS_MASS_STORAGE  = 8;
const USB_CLASS_HUB         = 9;
const USB_CLASS_DATA       = 10;
const USB_CLASS_VENDOR_SPEC  = $ff;

{
 * Descriptor types
}
const USB_DT_DEVICE   = $01;
const USB_DT_CONFIG   = $02;
const USB_DT_STRING   = $03;
const USB_DT_INTERFACE  = $04;
const USB_DT_ENDPOINT  = $05;

const USB_DT_HID   = $21;
const USB_DT_REPORT  = $22;
const USB_DT_PHYSICAL = $23;
const USB_DT_HUB   = $29;

{
 * Descriptor sizes per descriptor type
}
const USB_DT_DEVICE_SIZE  = 18;
const USB_DT_CONFIG_SIZE  = 9;
const USB_DT_INTERFACE_SIZE  = 9;
const USB_DT_ENDPOINT_SIZE  = 7;
const USB_DT_ENDPOINT_AUDIO_SIZE = 9; { Audio extension }
const USB_DT_HUB_NONVAR_SIZE  = 7;

{ Endpoint descriptor }
const USB_MAXENDPOINTS = 32;

const USB_ENDPOINT_ADDRESS_MASK = $0f;    { in bEndpointAddress }
const USB_ENDPOINT_DIR_MASK    = $80;

const USB_ENDPOINT_TYPE_MASK  = $03;    { in bmAttributes }
const USB_ENDPOINT_TYPE_CONTROL     = 0;
const USB_ENDPOINT_TYPE_ISOCHRONOUS = 1;
const USB_ENDPOINT_TYPE_BULK      = 2;
const USB_ENDPOINT_TYPE_INTERRUPT   = 3;

{ Interface descriptor }
const USB_MAXINTERFACES = 32;
const USB_MAXALTSETTING = 128; { Hard limit }

{ Configuration descriptor information.. }
const USB_MAXCONFIG  = 8;

{
 * Standard requests
}
const USB_REQ_GET_STATUS      = $00;
const USB_REQ_CLEAR_FEATURE     = $01;
{ 0x02 is reserved }
const USB_REQ_SET_FEATURE      = $03;
{ 0x04 is reserved }
const USB_REQ_SET_ADDRESS      = $05;
const USB_REQ_GET_DESCRIPTOR  = $06;
const USB_REQ_SET_DESCRIPTOR  = $07;
const USB_REQ_GET_CONFIGURATION = $08;
const USB_REQ_SET_CONFIGURATION = $09;
const USB_REQ_GET_INTERFACE    = $0A;
const USB_REQ_SET_INTERFACE    = $0B;
const USB_REQ_SYNCH_FRAME      = $0C;

const USB_TYPE_STANDARD  = ($00 shl 5);
const USB_TYPE_CLASS   = ($01 shl 5);
const USB_TYPE_VENDOR   = ($02 shl 5);
const USB_TYPE_RESERVED  = ($03 shl 5);

const USB_RECIP_DEVICE  = $00;
const USB_RECIP_INTERFACE = $01;
const USB_RECIP_ENDPOINT = $02;
const USB_RECIP_OTHER   = $03;

{
 * Various libusb API related stuff
}

const USB_ENDPOINT_IN   = $80;
const USB_ENDPOINT_OUT  = $00;

{ Error codes }
const USB_ERROR_BEGIN   = 500000;

{ All standard descriptors have these 2 fields in common }
type
  Tusb_descriptor_header = record
    bLength         : byte;
    bDescriptorType : byte;
  end;

{ String descriptor }
  Tusb_string_descriptor = record
    bLength         : byte;
    bDescriptorType : byte;
    wData           : array[0..0] of word;
  end;

{ HID descriptor }
  Tusb_hid_descriptor = record
    bLength         : byte;
    bDescriptorType : byte;
    bcdHID          : word;
    bCountryCode    : byte;
    bNumDescriptors : byte;
  end;

{ Endpoint descriptor }
  Tusb_endpoint_descriptor = record
    bLength : byte;
    bDescriptorType : byte;
    bEndpointAddress : byte;
    bmAttributes : byte;
    wMaxPacketSize : word;
    bInterval : byte;
    bRefresh : byte;
    bSynchAddress : byte;
    extra : ^byte; { Extra descriptors }
    extralen : integer;
  end;

{ Interface descriptor }
  Tusb_interface_descriptor = record
    bLength : byte;
    bDescriptorType     : byte;
    bInterfaceNumber    : byte;
    bAlternateSetting   : byte;
    bNumEndpoints       : byte;
    bInterfaceClass     : byte;
    bInterfaceSubClass  : byte;
    bInterfaceProtocol  : byte;
    iInterface          : byte;

    endpoint : ^Tusb_endpoint_descriptor;

    extra     : ^byte; { Extra descriptors }
    extralen  : integer;
  end;

  Tusb_interface = record
    altsetting : ^Tusb_interface_descriptor;
    num_altsetting : integer;
  end;

{ Configuration descriptor information.. }
  Tusb_config_descriptor  = record
    bLength             : byte;
    bDescriptorType     : byte;
    wTotalLength        : word;
    bNumInterfaces      : byte;
    bConfigurationValue : byte;
    iConfiguration      : byte;
    bmAttributes        : byte;
    MaxPower            : byte;

    interface_ : ^Tusb_interface;

    extra     : ^byte; { Extra descriptors }
    extralen  : integer;
  end;

{ Device descriptor }
  Tusb_device_descriptor = record
    bLength         : byte;
    bDescriptorType : byte;
    bcdUSB          : word;
    bDeviceClass    : byte;
    bDeviceSubClass : byte;
    bDeviceProtocol : byte;
    bMaxPacketSize0 : byte;
    idVendor        : word;
    idProduct       : word;
    bcdDevice       : word;
    iManufacturer   : byte;
    iProduct        : byte;
    iSerialNumber   : byte;
    bNumConfigurations  : byte;
  end;

  Tusb_ctrl_setup = record
    bRequestType  : byte;
    bRequest      : byte;
    wValue        : word;
    wIndex        : word;
    wLength       : word;
  end;

{ Data types }

  Pusb_bus = ^Tusb_bus;
  Pusb_device = ^Tusb_device;

  Tusb_device = record
    next, prev : Pusb_device;

    filename : array [0..LIBUSB_PATH_MAX-1] of Char;

    bus : Pusb_bus;

    descriptor : Tusb_device_descriptor;
    config : ^Tusb_config_descriptor;

    dev : Pointer;  { Darwin support }

    devnum : byte;

    num_children : byte;
    children : ^Pusb_device;
  end;

  Tusb_bus = record
    next, prev : Pusb_bus;

    dirname : array[0..LIBUSB_PATH_MAX - 1] of Char;

    devices : Pusb_device;
    location : Cardinal;

    root_dev : Pusb_device;
  end;

{ Version information, Windows specific }
  Tusb_version = record
    dll : record
      major : integer;
      minor : integer;
      micro : integer;
      nano : integer;
    end;
    driver : record
      major : integer;
      minor : integer;
      micro : integer;
      nano : integer;
    end;
  end;

  Pusb_dev_handle = ^Tusb_dev_handle;
  Tusb_dev_handle = record
    fd  : integer;
    bus : Pusb_bus;
    device  : Pusb_device;
    config  : integer;
    interface_ : integer;
    altestting  : integer;
    impl_info : Pointer;
  end;


{ Function prototypes }

  { usb.c }
  function usb_open(dev : Pusb_device) : Pusb_dev_handle; cdecl; external 'libusb0.dll';
  function usb_close(dev : Pusb_dev_handle) : integer; cdecl; external 'libusb0.dll';
  function usb_get_string(dev: Pusb_dev_handle; index: integer; langid: integer; buf: PChar; buflen: Cardinal): integer; cdecl; external 'libusb0.dll';
  function usb_get_string_simple(dev : Pusb_dev_handle; index: integer; buf: PChar; buflen: Cardinal): integer; cdecl; external 'libusb0.dll';

  { descriptors.c }
  function usb_get_descriptor_by_endpoint(udev: Pusb_dev_handle; ep: integer; type_: Byte; index: Byte; buf: Pointer; size: integer): integer; cdecl; external 'libusb0.dll';
  function usb_get_descriptor(udev: Pusb_dev_handle; type_: Byte; index: Byte; buf : Pointer; size: integer): integer; cdecl; external 'libusb0.dll';

  { <arch>.c }
  function usb_bulk_write(dev: Pusb_dev_handle; ep: integer; bytes: PChar; size: integer; timeout: integer): integer; cdecl; external 'libusb0.dll';
  function usb_bulk_read(dev: Pusb_dev_handle; ep: integer; bytes: PChar; size: integer; timeout: integer): integer; cdecl; external 'libusb0.dll';
  function usb_interrupt_write(dev: Pusb_dev_handle; ep: integer; bytes: PChar; size: integer; timeout: integer): integer; cdecl; external 'libusb0.dll';
  function usb_interrupt_read(dev: Pusb_dev_handle; ep: integer; bytes: PChar; size: integer; timeout: integer): integer; cdecl; external 'libusb0.dll';
  function usb_control_msg(dev: Pusb_dev_handle; requesttype: integer; request: integer; value: integer; index: integer; bytes: PChar; size: integer; timeout: integer): integer; cdecl; external 'libusb0.dll';
  function usb_set_configuration(dev: Pusb_dev_handle; configuration: integer): integer; cdecl; external 'libusb0.dll';
  function usb_claim_interface(dev: Pusb_dev_handle; interface_: integer): integer; cdecl; external 'libusb0.dll';
  function usb_release_interface(dev: Pusb_dev_handle; interface_: integer): integer; cdecl; external 'libusb0.dll';
  function usb_set_altinterface(dev: Pusb_dev_handle; alternate: integer): integer; cdecl; external 'libusb0.dll';
  function usb_resetep(dev: Pusb_dev_handle; ep: Cardinal): integer; cdecl; external 'libusb0.dll';
  function usb_clear_halt(dev: Pusb_dev_handle; ep: Cardinal): integer; cdecl; external 'libusb0.dll';
  function usb_reset(dev: Pusb_dev_handle): integer; cdecl; external 'libusb0.dll';

  function usb_strerror: PChar; cdecl; external 'libusb0.dll';

  procedure usb_init; cdecl; external 'libusb0.dll'
  procedure usb_set_debug(level: integer); cdecl; external 'libusb0.dll';
  function usb_find_busses: integer; cdecl; external 'libusb0.dll';
  function usb_find_devices: integer; cdecl; external 'libusb0.dll';
  function usb_device(dev: Pusb_dev_handle): Pusb_device; cdecl; external 'libusb0.dll';
  function usb_get_busses: Pusb_bus; cdecl; external 'libusb0.dll';

  { Windows specific functions }

  const LIBUSB_HAS_INSTALL_SERVICE_NP = 1;
  function usb_install_service_np: integer; stdcall; external 'libusb0.dll';
  procedure usb_install_service_np_rundll(wnd: HWND; instance: HINST; cmd_line: PChar; cmd_show: integer); stdcall; external 'libusb0.dll';

  const LIBUSB_HAS_UNINSTALL_SERVICE_NP = 1;
  function usb_uninstall_service_np: integer; stdcall; external 'libusb0.dll';
  procedure usb_uninstall_service_np_rundll(wnd:HWND; instance: HINST; cmd_line: PChar; cmd_show: Integer); stdcall; external 'libusb0.dll';

  const LIBUSB_HAS_INSTALL_DRIVER_NP = 1;
  function usb_install_driver_np(const inf_file : PChar): integer; stdcall; external 'libusb0.dll';
  procedure usb_install_driver_np_rundll(wnd: HWND; instance: HINST; cmd_line: PChar; cmd_show: integer); stdcall; external 'libusb0.dll';

  const LIBUSB_HAS_TOUCH_INF_FILE_NP = 1;
  function usb_touch_inf_file_np(const inf_file: PChar): integer; stdcall; external 'libusb0.dll';
  procedure usb_touch_inf_file_np_rundll(wnd: HWND; instance: HINST; cmd_line: PChar; cmd_show: Integer); stdcall; external 'libusb0.dll';

  const LIBUSB_HAS_INSTALL_NEEDS_RESTART_NP = 1;
  function usb_install_needs_restart_np: integer; stdcall; external 'libusb0.dll';

//  #define struct usb_version *usb_get_version(void);

  type PPointer = ^Pointer;
  function usb_isochronous_setup_async(dev: Pusb_dev_handle; context: PPointer; ep: Byte; pktsize: integer): integer; stdcall; external 'libusb0.dll';
  function usb_bulk_setup_async(dev: Pusb_dev_handle; context: PPointer; ep: Byte): integer; stdcall; external 'libusb0.dll';
  function usb_interrupt_setup_async(dev: Pusb_dev_handle; context: PPointer; ep: Byte): integer; stdcall; external 'libusb0.dll';

  function usb_submit_async(context: Pointer; bytes: PChar; size: Integer): integer; stdcall; external 'libusb0.dll';
  function usb_reap_async(context: Pointer; timeout: integer): integer; stdcall; external 'libusb0.dll';
  function usb_reap_async_nocancel(context: Pointer; timeout: integer): integer; stdcall; external 'libusb0.dll';
  function usb_cancel_async(context: Pointer): integer; stdcall; external 'libusb0.dll';
  function usb_free_async(context: PPointer): integer; stdcall; external 'libusb0.dll';

implementation


end.


이올린에 북마크하기(0) 이올린에 추천하기(0)
2009/04/30 21:57 2009/04/30 21:57
Posted by TylorSTYLE™

트랙백 보낼 주소 : http://www.tylorstyle.net/blog/trackback/171

댓글을 달아주세요

[로그인][오픈아이디란?]

<< PREV : [1] : ... [20] : [21] : [22] : [23] : [24] : [25] : [26] : [27] : [28] : ... [144] : NEXT >>

BLOG main image

카테고리

전체 (144)
Freeware (3)
Embedded (27)
신변잡기 (51)
디지털 회로 (2)
Programming (20)
무선 네트워크 (15)
Computer (18)
사용기 (7)

글 보관함

262

178

-35 days

today : 70

Daum 블로거뉴스
믹시