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 내에 예제를 참고하시면 됩니다.
소스코드 전문 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
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. |