Include files for libUSB Linux and Windows.

For issues with communication ports, protocols, etc.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Include files for libUSB Linux and Windows.

Post by D.J.Peters »

Include files for lib USB Linux and Windows.

Joshy

short test

Code: Select all

#ifdef __FB_WIN32__
#include once "lusb0_usb.bi"
#else
#include once "usb.bi"
#endif

usb_init()
usb_find_busses() 
usb_find_devices()

dim as usb_bus ptr aBus = usb_get_busses() 

while (aBus)
  ? "bus " & aBus->dirname
  dim as usb_device ptr aDev = aBus->devices
  while aDev
    ? "vendor 0x" & hex(aDev->descriptor.idVendor,4) & " product: 0x" & hex(aDev->descriptor.idProduct,4)
    aDev=aDev->next
  wend
   
  aBus = aBus->next
wend
? "done ..." : sleep
Linux "usb.bi"

Code: Select all

/'
 * Prototypes, structure definitions and macros.
 *
 * Copyright (c) 2000-2003 Johannes Erdfelt <johannes@erdfelt.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * This file (and only this file) may alternatively be licensed under the
 * BSD license. See the LICENSE file shipped with the libusb-compat-0.1 source
 * distribution for details.
 '/

#ifndef __USB_BI__
#define __USB_BI__

#inclib "usb"

/'
 * 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
 '/
#define USB_CLASS_PER_INTERFACE 0  /' for DeviceClass '/
#define USB_CLASS_AUDIO         1
#define USB_CLASS_COMM          2
#define USB_CLASS_HID           3
#define USB_CLASS_PTP           6
#define USB_CLASS_PRINTER       7
#define USB_CLASS_MASS_STORAGE  8
#define USB_CLASS_HUB           9
#define USB_CLASS_DATA         10
#define USB_CLASS_VENDOR_SPEC  &Hff

/'
 * Descriptor types
 '/
#define USB_DT_DEVICE    &H01
#define USB_DT_CONFIG    &H02
#define USB_DT_STRING    &H03
#define USB_DT_INTERFACE &H04
#define USB_DT_ENDPOINT  &H05

#define USB_DT_HID       &H21
#define USB_DT_REPORT    &H22
#define USB_DT_PHYSICAL  &H23
#define USB_DT_HUB       &H29

/'
 * Descriptor sizes per descriptor type
 '/
#define USB_DT_DEVICE_SIZE         18
#define USB_DT_CONFIG_SIZE          9
#define USB_DT_INTERFACE_SIZE       9
#define USB_DT_ENDPOINT_SIZE        7
#define USB_DT_ENDPOINT_AUDIO_SIZE  9  /' Audio extension '/
#define USB_DT_HUB_NONVAR_SIZE      7

/' All standard descriptors have these 2 fields in common '/
type usb_descriptor_header
  as ubyte  bLength
  as ubyte  bDescriptorType
end type

/' String descriptor '/
type usb_string_descriptor
  as ubyte  bLength
  as ubyte  bDescriptorType
  as ushort wData(0)
end type

/' HID descriptor '/
type usb_hid_descriptor
  as ubyte  bLength
  as ubyte  bDescriptorType
  as ushort bcdHID
  as ubyte  bCountryCode
  as ubyte  bNumDescriptors
  /' as ubyte  bReportDescriptorType '/
  /' as ushort wDescriptorLength '/
  /' ... '/
end type

/' Endpoint descriptor '/
#define USB_MAXENDPOINTS  32
type usb_endpoint_descriptor
  as ubyte  bLength
  as ubyte  bDescriptorType
  as ubyte  bEndpointAddress
  as ubyte  bmAttributes
  as ushort wMaxPacketSize
  as ubyte  bInterval
  as ubyte  bRefresh
  as ubyte  bSynchAddress

  as ubyte ptr extra  /' Extra descriptors '/
  as integer extralen
end type

#define USB_ENDPOINT_ADDRESS_MASK  &H0f    /' in bEndpointAddress '/
#define USB_ENDPOINT_DIR_MASK      &H80

#define USB_ENDPOINT_TYPE_MASK     &H03    /' in bmAttributes '/
#define USB_ENDPOINT_TYPE_CONTROL     0
#define USB_ENDPOINT_TYPE_ISOCHRONOUS 1
#define USB_ENDPOINT_TYPE_BULK        2
#define USB_ENDPOINT_TYPE_INTERRUPT   3

/' Interface descriptor '/
#define USB_MAXINTERFACES  32
type usb_interface_descriptor
  as ubyte  bLength
  as ubyte  bDescriptorType
  as ubyte  bInterfaceNumber
  as ubyte  bAlternateSetting
  as ubyte  bNumEndpoints
  as ubyte  bInterfaceClass
  as ubyte  bInterfaceSubClass
  as ubyte  bInterfaceProtocol
  as ubyte  iInterface

  as usb_endpoint_descriptor ptr endpoint

  as ubyte ptr extra  /' Extra descriptors '/
  as integer extralen
end type

#define USB_MAXALTSETTING  128  /' Hard limit '/
type usb_interface
  as usb_interface_descriptor ptr altsetting
  as integer num_altsetting
end type

/' Configuration descriptor information.. '/
#define USB_MAXCONFIG    8
type usb_config_descriptor
  as ubyte  bLength
  as ubyte  bDescriptorType
  as ushort wTotalLength
  as ubyte  bNumInterfaces
  as ubyte  bConfigurationValue
  as ubyte  iConfiguration
  as ubyte  bmAttributes
  as ubyte  MaxPower

  as usb_interface ptr interface

  as ubyte ptr extra  /' Extra descriptors '/
  as integer extralen
end type

/' Device descriptor '/
type usb_device_descriptor
  as ubyte  bLength
  as ubyte  bDescriptorType
  as ushort bcdUSB
  as ubyte  bDeviceClass
  as ubyte  bDeviceSubClass
  as ubyte  bDeviceProtocol
  as ubyte  bMaxPacketSize0
  as ushort idVendor
  as ushort idProduct
  as ushort bcdDevice
  as ubyte  iManufacturer
  as ubyte  iProduct
  as ubyte  iSerialNumber
  as ubyte  bNumConfigurations
end type

type usb_ctrl_setup
  as ubyte  bRequestType
  as ubyte  bRequest
  as ushort wValue
  as ushort wIndex
  as ushort wLength
end type

/'
 * Standard requests
 '/
#define USB_REQ_GET_STATUS        &H00
#define USB_REQ_CLEAR_FEATURE     &H01
/' &H02 is reserved '/
#define USB_REQ_SET_FEATURE       &H03
/' &H04 is reserved '/
#define USB_REQ_SET_ADDRESS       &H05
#define USB_REQ_GET_DESCRIPTOR    &H06
#define USB_REQ_SET_DESCRIPTOR    &H07
#define USB_REQ_GET_CONFIGURATION &H08
#define USB_REQ_SET_CONFIGURATION &H09
#define USB_REQ_GET_INTERFACE     &H0A
#define USB_REQ_SET_INTERFACE     &H0B
#define USB_REQ_SYNCH_FRAME       &H0C

#define USB_TYPE_STANDARD        (&H00 shl 5)
#define USB_TYPE_CLASS           (&H01 shl 5)
#define USB_TYPE_VENDOR          (&H02 shl 5)
#define USB_TYPE_RESERVED        (&H03 shl 5)

#define USB_RECIP_DEVICE          &H00
#define USB_RECIP_INTERFACE       &H01
#define USB_RECIP_ENDPOINT        &H02
#define USB_RECIP_OTHER           &H03

/'
 * Various libusb API related stuff
 '/

#define USB_ENDPOINT_IN           &H80
#define USB_ENDPOINT_OUT          &H00

/' Error codes '/
#define USB_ERROR_BEGIN           500000


#ifndef PATH_MAX
#define PATH_MAX 4096
#endif

/' Data types '/
'type usb_device as any
type usb_bus_ as usb_bus

/'
 * To maintain compatibility with applications already built with libusb,
 * we must only add entries to the end of this structure. NEVER delete or
 * move members and only change types if you really know what you're doing.
 '/
type usb_device
  as  usb_device ptr next, prev
  as zstring * PATH_MAX + 1 filename
  as usb_bus_ ptr bus
  as usb_device_descriptor descriptor
  as usb_config_descriptor ptr config
  as any ptr dev    /' Darwin support '/
  as ubyte devnum
  as ubyte num_children
  as usb_device ptr ptr children
end type

type usb_bus
  as usb_bus ptr next, prev
  as zstring * PATH_MAX + 1  dirname 
  as usb_device ptr devices
  as uinteger location
  as usb_device ptr root_dev
end type

type usb_dev_handle as any 

#define LIBUSB_HAS_GET_DRIVER_NP 1
#define LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP 1

/' Variables '/
extern usb_busses as usb_bus ptr

extern "C"

/' Function prototypes '/

/' usb.c '/
declare function usb_open             (dev as usb_device ptr) as usb_dev_handle ptr
declare function usb_close            (dev as usb_dev_handle ptr) as integer
declare function usb_get_string       (dev as usb_dev_handle ptr, index as integer, langid as integer, buf as ubyte ptr, buflen as integer) as integer
declare function usb_get_string_simple(dev as usb_dev_handle ptr, index as integer, buf as ubyte ptr, buflen as integer) as integer

/' descriptors.c '/
declare function usb_get_descriptor_by_endpoint(udev as usb_dev_handle ptr, ep as integer, typ as ubyte, index as ubyte, buf as any ptr, size as integer) as integer
declare function usb_get_descriptor            (udev as usb_dev_handle ptr,                typ as ubyte, index as ubyte, buf as any ptr, size as integer) as integer

/' <arch>.c '/
declare function usb_bulk_write       (dev as usb_dev_handle ptr, ep as integer, bytes as byte ptr, size as integer, timeout as integer) as integer
declare function usb_bulk_read        (dev as usb_dev_handle ptr, ep as integer, bytes as byte ptr, size as integer, timeout as integer) as integer
declare function usb_interrupt_write  (dev as usb_dev_handle ptr, ep as integer, bytes as byte ptr, size as integer, timeout as integer) as integer
declare function usb_interrupt_read   (dev as usb_dev_handle ptr, ep as integer, bytes as byte ptr, size as integer, timeout as integer) as integer
declare function usb_control_msg      (dev as usb_dev_handle ptr, requesttype as integer, request as integer, value as integer, index as integer, bytes as byte ptr, size as integer, timeout as integer) as integer
declare function usb_set_configuration(dev as usb_dev_handle ptr, configuration as integer) as integer
declare function usb_claim_interface  (dev as usb_dev_handle ptr, interface as integer) as integer
declare function usb_release_interface(dev as usb_dev_handle ptr, interface as integer) as integer
declare function usb_set_altinterface (dev as usb_dev_handle ptr, alternate as integer) as integer
declare function usb_resetep          (dev as usb_dev_handle ptr, ep as uinteger) as integer
declare function usb_clear_halt       (dev as usb_dev_handle ptr, ep as uinteger) as integer
declare function usb_reset            (dev as usb_dev_handle ptr) as integer

declare function usb_get_driver_np(dev as usb_dev_handle ptr, interface as integer, name_ as zstring ptr, namelen as uinteger) as integer

declare function usb_detach_kernel_driver_np(dev as usb_dev_handle ptr, interface as integer) as integer

declare function usb_strerror() as zstring ptr

declare sub      usb_init()
declare sub      usb_set_debug(level as integer)
declare function usb_find_busses() as integer
declare function usb_find_devices() as integer
declare function usb_device(dev as usb_dev_handle ptr) as usb_device ptr
declare function usb_get_busses() as usb_bus ptr

end extern

#endif /' __USB_BI__ '/
Windows "lusb0_usb.bi" (not tested)

Code: Select all

#ifndef __USB_BI__
#define __USB_BI__

#include once "crt.bi"
#include once "windows.bi"

#inclib "usb"

' 'interface' is defined somewhere in the Windows header files. This macro is deleted here to avoid conflicts and compile errors.
#ifdef interface
#undef interface
#endif

' PATH_MAX from limits.h can't be used on Windows if the dll and import libraries are build/used by different compilers
#define 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
#define USB_CLASS_PER_INTERFACE    0  ' for DeviceClass
#define USB_CLASS_AUDIO            1
#define USB_CLASS_COMM             2
#define USB_CLASS_HID              3
#define USB_CLASS_PRINTER          7
#define USB_CLASS_MASS_STORAGE     8
#define USB_CLASS_HUB              9
#define USB_CLASS_DATA            10
#define USB_CLASS_VENDOR_SPEC     &Hff

' Descriptor types
#define USB_DT_DEVICE    &H01
#define USB_DT_CONFIG    &H02
#define USB_DT_STRING    &H03
#define USB_DT_INTERFACE &H04
#define USB_DT_ENDPOINT  &H05

#define USB_DT_HID      &H21
#define USB_DT_REPORT   &H22
#define USB_DT_PHYSICAL &H23
#define USB_DT_HUB      &H29

' Descriptor sizes per descriptor type
#define USB_DT_DEVICE_SIZE        18
#define USB_DT_CONFIG_SIZE         9
#define USB_DT_INTERFACE_SIZE      9
#define USB_DT_ENDPOINT_SIZE       7
#define USB_DT_ENDPOINT_AUDIO_SIZE 9  ' Audio extension
#define USB_DT_HUB_NONVAR_SIZE     7


' All standard descriptors have these 2 fields in common
type usb_descriptor_header
  as ubyte  bLength
  as ubyte  bDescriptorType
end type

' String descriptor
type usb_string_descriptor
  as ubyte  bLength
  as ubyte  bDescriptorType
  as ushort wData(0)
end type

' HID descriptor
type usb_hid_descriptor
  as ubyte  bLength
  as ubyte  bDescriptorType
  as ushort bcdHID
  as ubyte  bCountryCode
  as ubyte  bNumDescriptors
end type

' Endpoint descriptor
#define USB_MAXENDPOINTS  32
type usb_endpoint_descriptor
  as ubyte  bLength
  as ubyte  bDescriptorType
  as ubyte  bEndpointAddress
  as ubyte  bmAttributes
  as ushort wMaxPacketSize
  as ubyte  bInterval
  as ubyte  bRefresh
  as ubyte  bSynchAddress
  as ubyte ptr extra
  as integer extralen
end type

#define USB_ENDPOINT_ADDRESS_MASK  &H0f    ' in bEndpointAddress
#define USB_ENDPOINT_DIR_MASK      &H80
#define USB_ENDPOINT_TYPE_MASK     &H03    ' in bmAttributes
#define USB_ENDPOINT_TYPE_CONTROL     0
#define USB_ENDPOINT_TYPE_ISOCHRONOUS 1
#define USB_ENDPOINT_TYPE_BULK        2
#define USB_ENDPOINT_TYPE_INTERRUPT   3

' Interface descriptor
#define USB_MAXINTERFACES  32

type usb_interface_descriptor
  as ubyte  bLength
  as ubyte  bDescriptorType
  as ubyte  bInterfaceNumber
  as ubyte  bAlternateSetting
  as ubyte  bNumEndpoints
  as ubyte  bInterfaceClass
  as ubyte  bInterfaceSubClass
  as ubyte  bInterfaceProtocol
  as ubyte  iInterface
  as usb_endpoint_descriptor ptr endpoint
  as ubyte ptr extra  ' Extra descriptors
  as integer extralen
end type

#define USB_MAXALTSETTING  128  ' Hard limit

type usb_interface
  as usb_interface_descriptor  ptr altsetting
  as integer num_altsetting
end type

' Configuration descriptor information..
#define USB_MAXCONFIG    8
type usb_config_descriptor
  as ubyte  bLength
  as ubyte  bDescriptorType
  as ushort wTotalLength
  as ubyte  bNumInterfaces
  as ubyte  bConfigurationValue
  as ubyte  iConfiguration
  as ubyte  bmAttributes
  as ubyte  MaxPower
  as usb_interface ptr interface
  as ubyte ptr extra  ' Extra descriptors
  as integer extralen
end type

' Device descriptor
type usb_device_descriptor
  as ubyte  bLength
  as ubyte  bDescriptorType
  as ushort bcdUSB
  as ubyte  bDeviceClass
  as ubyte  bDeviceSubClass
  as ubyte  bDeviceProtocol
  as ubyte  bMaxPacketSize0
  as ushort idVendor
  as ushort idProduct
  as ushort bcdDevice
  as ubyte  iManufacturer
  as ubyte  iProduct
  as ubyte  iSerialNumber
  as ubyte  bNumConfigurations
end type

type usb_ctrl_setup
  as ubyte  bRequestType
  as ubyte  bRequest
  as ushort wValue
  as ushort wIndex
  as ushort wLength
end type

' Standard requests
#define USB_REQ_GET_STATUS        &H00
#define USB_REQ_CLEAR_FEATURE     &H01
' &H02 is reserved
#define USB_REQ_SET_FEATURE       &H03
' &H04 is reserved
#define USB_REQ_SET_ADDRESS       &H05
#define USB_REQ_GET_DESCRIPTOR    &H06
#define USB_REQ_SET_DESCRIPTOR    &H07
#define USB_REQ_GET_CONFIGURATION &H08
#define USB_REQ_SET_CONFIGURATION &H09
#define USB_REQ_GET_INTERFACE     &H0A
#define USB_REQ_SET_INTERFACE     &H0B
#define USB_REQ_SYNCH_FRAME       &H0C

#define USB_TYPE_STANDARD   (&H00 shl 5)
#define USB_TYPE_CLASS      (&H01 shl 5)
#define USB_TYPE_VENDOR     (&H02 shl 5)
#define USB_TYPE_RESERVED   (&H03 shl 5)

#define USB_RECIP_DEVICE     &H00
#define USB_RECIP_INTERFACE  &H01
#define USB_RECIP_ENDPOINT   &H02
#define USB_RECIP_OTHER      &H03

' Various libusb API related stuff
#define USB_ENDPOINT_IN      &H80
#define USB_ENDPOINT_OUT     &H00

' Error codes
#define USB_ERROR_BEGIN      500000

' This is supposed to look weird. This file is generated from autoconf and I didn't want to make this too complicated.
#define USB_LE16_TO_CPU(x)

' Device reset types for usb_reset_ex.
' http://msdn.microsoft.com/en-us/library/ff537269%28VS.85%29.aspx
' http://msdn.microsoft.com/en-us/library/ff537243%28v=vs.85%29.aspx
#define USB_RESET_TYPE_RESET_PORT (1 shl 0)
#define USB_RESET_TYPE_CYCLE_PORT (1 shl 1)
#define USB_RESET_TYPE_FULL_RESET (USB_RESET_TYPE_CYCLE_PORT or USB_RESET_TYPE_RESET_PORT)

type usb_bus_ as usb_bus

' Data types
type usb_device
  as usb_device ptr               next, prev
  as zstring * LIBUSB_PATH_MAX -1 filename
  as usb_bus_ ptr                 bus
  as usb_device_descriptor        descriptor
  as usb_config_descriptor ptr    config
  as any ptr                      dev    ' Darwin support '
  as ubyte                        devnum
  as ubyte                        num_children
  as usb_device ptr ptr           children
end type

type usb_bus
  as usb_bus ptr                   next, prev
  as zstring * LIBUSB_PATH_MAX - 1 dirname
  as usb_device ptr                devices
  as ulong                         location
  as usb_device ptr                root_dev
end type


' Version information, Windows specific
type dll_
  as integer major
  as integer minor
  as integer micro
  as integer nano
end type
type driver_
  as integer major
  as integer minor
  as integer micro
  as integer nano
end type
type usb_version
  as dll_    dll
  as driver_ driver
end type

type usb_dev_handle as any 

' Variables
#ifndef __USB_C__
#define usb_busses usb_get_busses()
#endif

#define LIBUSB_HAS_INSTALL_SERVICE_NP 1
#define LIBUSB_HAS_UNINSTALL_SERVICE_NP 1
#define LIBUSB_HAS_INSTALL_DRIVER_NP 1
#define LIBUSB_HAS_TOUCH_INF_FILE_NP 1
#define LIBUSB_HAS_INSTALL_NEEDS_RESTART_NP 1
#define LIBUSB_HAS_INSTALL_NP 1

#define usb_install_np usb_install_npA

declare sub usb_install_np_rundll          (wnd as HWND, instance as HINSTANCE, cmd_line as zstring ptr, cmd_show as integer)
declare sub usb_install_driver_np_rundll   (wnd as HWND, instance as HINSTANCE, cmd_line as zstring ptr, cmd_show as integer)
declare sub usb_touch_inf_file_np_rundll   (wnd as HWND, instance as HINSTANCE, cmd_line as zstring ptr, cmd_show as integer)
declare sub usb_install_service_np_rundll  (wnd as HWND, instance as HINSTANCE, cmd_line as zstring ptr, cmd_show as integer)
declare sub usb_uninstall_service_np_rundll(wnd as HWND, instance as HINSTANCE, cmd_line as zstring ptr, cmd_show as integer)

extern "C"

' usb.c
declare function usb_open(dev as usb_device ptr) as usb_dev_handle ptr
declare function usb_close(dev as usb_dev_handle ptr) as integer
declare function usb_get_string(dev as usb_dev_handle ptr, index as integer, langid as integer, buf as byte ptr, buflen as integer) as integer
declare function usb_get_string_simple(dev as usb_dev_handle ptr, index as integer, buf as byte ptr, buflen as integer) as integer

' descriptors.c
declare function usb_get_descriptor_by_endpoint(dev as usb_dev_handle ptr,ep as integer, type_ as ubyte, index as ubyte, buf as byte ptr, size as integer) as integer
declare function usb_get_descriptor            (dev as usb_dev_handle ptr,               type_ as ubyte, index as ubyte, buf as byte ptr, size as integer) as integer

' <arch>.c
declare function usb_bulk_write       (dev as usb_dev_handle ptr, ep as integer, bytes as byte ptr, size as integer, timeout as integer) as integer
declare function usb_bulk_read        (dev as usb_dev_handle ptr, ep as integer, bytes as byte ptr, size as integer, timeout as integer) as integer
declare function usb_interrupt_write  (dev as usb_dev_handle ptr, ep as integer, bytes as byte ptr, size as integer, timeout as integer) as integer
declare function usb_interrupt_read   (dev as usb_dev_handle ptr, ep as integer, bytes as byte ptr, size as integer, timeout as integer) as integer
declare function usb_control_msg      (dev as usb_dev_handle ptr, requesttype as integer, request as integer, value as integer, index as integer, bytes as byte ptr, size as integer, timeout as integer) as integer
declare function usb_set_configuration(dev as usb_dev_handle ptr, configuration as integer) as integer
declare function usb_claim_interface  (dev as usb_dev_handle ptr, interface as integer) as integer
declare function usb_release_interface(dev as usb_dev_handle ptr, interface as integer) as integer
declare function usb_set_altinterface (dev as usb_dev_handle ptr, alternate_ as integer) as integer
declare function usb_resetep          (dev as usb_dev_handle ptr, ep as uinteger) as integer
declare function usb_clear_halt       (dev as usb_dev_handle ptr, ep as uinteger) as integer
declare function usb_reset            (dev as usb_dev_handle ptr) as integer
declare function usb_reset_ex         (dev as usb_dev_handle ptr, reset_type as uinteger) as integer
declare function usb_strerror() as byte ptr

declare sub      usb_init()
declare sub      usb_set_debug(level as integer)
declare function usb_find_busses() as integer
declare function usb_find_devices() as integer
declare function usb_device(dev as usb_dev_handle ptr) as usb_device ptr 
declare function usb_get_busses() as usb_bus ptr


' Windows specific functions
declare function usb_install_service_np() as integer
declare function usb_uninstall_service_np() as integer
declare function usb_install_driver_np(inf_file as const zstring ptr) as integer
declare function usb_touch_inf_file_np(inf_file as const zstring ptr) as integer
declare function usb_install_needs_restart_np() as integer
declare function usb_install_npW(hwnd as HWND, instance as HINSTANCE, cmd_line as zstring ptr, starg_arg as integer) as integer
declare function usb_install_npA(hwnd as HWND, instance as HINSTANCE, cmd_line as zstring ptr, starg_arg as integer) as integer

declare function usb_get_version() as const usb_version ptr

declare function usb_isochronous_setup_async(dev as usb_dev_handle ptr, context as any ptr ptr, ep as ubyte, pktsize as integer) as integer
declare function usb_bulk_setup_async       (dev as usb_dev_handle ptr, context as any ptr ptr, ep as ubyte) as integer
declare function usb_interrupt_setup_async  (dev as usb_dev_handle ptr, context as any ptr ptr, ep as ubyte) as integer

declare function usb_submit_async       (context as any ptr, bytes as byte ptr, size as integer) as integer
declare function usb_reap_async         (context as any ptr, timeout as integer) as integer
declare function usb_reap_async_nocancel(context as any ptr, timeout as integer) as integer
declare function usb_cancel_async       (context as any ptr) as integer
declare function usb_free_async         (context as any ptr ptr) as integer

end extern

#endif ' __USB_BI__
Last edited by D.J.Peters on Jan 31, 2013 14:01, edited 2 times in total.
nobozoz
Posts: 238
Joined: Nov 17, 2005 6:24
Location: Chino Hills, CA, USA

Re: Include file: usb.bi (tested on linux)

Post by nobozoz »

No lib or dll?

G:\fb.24\usb_\usb.bi(310) warning 15(0): No explicit BYREF or BYVAL, at parameter 1 (dev) of usb_device()
.
.
.
G:\fb.24\bin\win32\ld.exe: cannot find -lusb
linking failed: 'G:\fb.24\bin\win32\ld.exe' terminated with exit code 1
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Include file: usb.bi (tested on linux)

Post by D.J.Peters »

libusb is a part of Linux distros
for Windows you must use the bin package from http://sourceforge.net/projects/libusb- ... s/1.2.6.0/

On Linux the include file is "usb.bi" on Windows "lusb0_usb.bi" (the file "usb.bi" is a part of the windows driver tollkit)

Joshy
Last edited by D.J.Peters on Jan 30, 2013 19:49, edited 1 time in total.
nobozoz
Posts: 238
Joined: Nov 17, 2005 6:24
Location: Chino Hills, CA, USA

Re: Include file: usb.bi (tested on linux)

Post by nobozoz »

Thanks, Joshy.

The example code compiled and ran on WIN XP. Now to figure out what to do with this.

Jim
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Include file: usb.bi (tested on linux)

Post by D.J.Peters »

@nobozoz
do you know how to calculate the crc5 and crc16 via software ?

I mean from page 198 usb_20.pdf from here http://www.usb.org/developers/docs/usb_20.zip

Before i do it with an micro controller i will test it with FB or C first.

My first try but i don't know is it right.

Code: Select all

const as ubyte  polynomial5_mask = &H1F
const as ubyte  polynomial5  = (2^5 + 2^2 + 1) and polynomial5_mask

const as ushort polynomial16_mask = &HFFFF
const as ushort polynomial16 = (2^16 + 2^15 + 2^2 + 1) and polynomial16_mask

' calculate 5 bit CRC from 7 or 11 bit input
function usb_crc5(in as ushort, nBits as integer) as ubyte
  dim as ushort polynomial_msb = polynomial5 shl 11
  ' shift input to msb
  in shl= (16 - nBits)
  dim as ushort crc = polynomial5_mask shl 11 ' start all=1
  while (nBits > 0)
    ' msb = 1
    if ((in xor crc) and &H8000) then
      crc shl= 1
      crc xor= polynomial_msb 
    else
      ' msb = 0
      crc shl= 1
    end if
    in shl= 1
    nBits-=1
  wend
  crc shr= 11
  return crc xor polynomial5_mask
end function

' calculate 16 bit CRC from nBits input buffer
function usb_crc16(pIn as ubyte ptr, nBits as integer) as ushort 
  dim as integer   nBytes   = ((nBits-1) shr 3) + 1
  dim as ubyte ptr polynomial_msb = callocate(nBytes)
  dim as ubyte ptr crc = callocate(nBytes)
  dim as ubyte ptr in  = callocate(nBytes)
  
  ' polynomial = &H8005
  polynomial_msb[nBytes - 2] = lobyte(polynomial16) : polynomial_msb[nBytes - 1] = hibyte(polynomial16)
  ' start all = 1
  crc[nBytes - 2] = lobyte(polynomial16_mask) : crc[nBytes - 1] = hibyte(polynomial16_mask)

  for i as integer=0 to nBytes-1
    in[i] = pIn[i]
  next
  while (nBits > 0)
    ' msb = 1
    if ((in[nBytes - 1] xor crc[nBytes - 1]) and &H80) then
      for i as integer= nBytes - 1 to 0 step -1
        crc[i] = (crc[i] shl 1) or (crc[i-1] shr 7)
      next
      crc[0] shl= 1
      for i as integer = 0 to nBytes-1
        crc[i] xor= polynomial_msb[i]
      next
    else 
      ' msb = 0
      for i as integer = nBytes - 1 to 0 step -1
        crc[i] = (crc[i] shl 1) or (crc[i-1] shr 7)
      next
      crc[0] shl= 1
    end if
    
    for i as integer = nBytes - 1 to 0 step -1
      in[i] = (in[i] shl 1) or (in[i-1] shr 7)
    next
    in[0] shl= 1
    nBits-=1
  wend
  dim as ushort result = (crc[nBytes - 1] shl 8) or crc[nBytes - 2]
  function = result and polynomial16_mask
  deallocate polynomial_msb
  deallocate crc
  deallocate in
end function
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: Include files for libUSB Linux and Widows.

Post by vdecampo »

@Joshy

This is cool. I recently used libUSB in C++ for a project at work interfacing to an MCP2210 for a product we are making. The library was straight forward and easy to use. My only issue was creating a stand-alone installer for the libUSB driver.

-Vince
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Include files for libUSB Linux and Widows.

Post by D.J.Peters »

vdecampo wrote:My only issue was creating a stand-alone installer for the libUSB driver.
Why was it a problem ?

Normaly Inf-Wizard.exe from the binary package does this job for you.

I selected the USB device and the wizard created two installer exe one for x86 and the other for 64bit OS with the right VID and PID from my Device.
(but i never tested the created installer)

Joshy
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: Include files for libUSB Linux and Widows.

Post by vdecampo »

D.J.Peters wrote:
vdecampo wrote:My only issue was creating a stand-alone installer for the libUSB driver.
Why was it a problem ?

Normaly Inf-Wizard.exe from the binary package does this job for you.

I selected the USB device and the wizard created two installer exe one for x86 and the other for 64bit OS with the right VID and PID from my Device.
(but i never tested the created installer)

Joshy
Yes I did that, but I could never get the created installers to work. I had to run the inf-wizard executable to get it to work, selecting the install option.

-Vince
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Include files for libUSB Linux and Widows.

Post by D.J.Peters »

vdecampo wrote:Yes I did that, but I could never get the created installers to work.
Do you run the created installer exe as normal user or as admin ?

I ask before i run in to same trouble.

Joshy
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: Include files for libUSB Linux and Widows.

Post by vdecampo »

D.J.Peters wrote:
vdecampo wrote:Yes I did that, but I could never get the created installers to work.
Do you run the created installer exe as normal user or as admin ?

I ask before i run in to same trouble.

Joshy
Yes. (Windows 7). I did not try it under XP.

-Vince
telinks
Posts: 1
Joined: Apr 12, 2013 9:59

Re: Include files for libUSB Linux and Windows.

Post by telinks »

Good day,

I wonder where I can get the usb library (Windows DDK) for FreeBASIC...?
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Include files for libUSB Linux and Windows.

Post by counting_pine »

Hi. I'm not an expert, but I searched for lusb0_usb and found a Win32 SourceForge page.
The downloads there might give you what you want:
http://sf.net/projects/libusb-win32/fil ... -releases/
jmg
Posts: 89
Joined: Mar 11, 2009 3:42

Re: Include file: usb.bi (tested on linux)

Post by jmg »

D.J.Peters wrote:@nobozoz
I mean from page 198 usb_20.pdf from here http://www.usb.org/developers/docs/usb_20.zip
Before i do it with an micro controller i will test it with FB or C first.

My first try but i don't know is it right.
This is great, but when I scan this with some 16 bit data fields, it seems to always output only either 00 or 32769 ?
(roughly 50:50 semi random)

When I print the in inside the function, it seems to scan as I expect, so I think the call is ok ?

I'm trying to find a 16 bit 'seed' that will then give a known CRC out, so thought I'd just scan all choices.
Easier than trying to work the maths.. :)
jmg
Posts: 89
Joined: Mar 11, 2009 3:42

Re: Include files for libUSB Linux and Windows.

Post by jmg »

Further to the above, I also found this
http://en.wikipedia.org/wiki/Computatio ... s_checking
which says if you append the CRC and then run over all bits including CRC, you should get 0000 for the 2nd larger span CRC
- so it seems a useful reality check for any CRC algortihms.
lisoformio
Posts: 9
Joined: Sep 16, 2015 15:57

Re: Include files for libUSB Linux and Windows.

Post by lisoformio »

Where to find include files for libusb-1.0?
I'm using FreeBASIC 1.05.0 x86_64.
TIA!
Post Reply