[fpc-pascal] USB Human Interface Devices

James Richters james at productionautomation.net
Sat Aug 10 21:09:49 CEST 2019


Hi Hansi, 

Thanks for the sample programs and information.   I'm going to try to do as you suggest and try to translate to the new libusb-1.0.. I'm not sure I'll understand it enough but it seems like it's heading in the right directions so would be worth a try.

James

-----Original Message-----
From: fpc-pascal <fpc-pascal-bounces at lists.freepascal.org> On Behalf Of Johann Glaser
Sent: Friday, August 9, 2019 7:33 PM
To: fpc-pascal at lists.freepascal.org
Subject: Re: [fpc-pascal] USB Human Interface Devices

Hi!

Am Freitag, den 09.08.2019, 08:08 -0400 schrieb James Richters:
> Hi Hansi,
> Thanks for the explanation.
>
> Here's the python code I’m trying to port over to FPC:
> https://github.com/wolfmanjm/kivy-smoothie-host/blob/master/modules/hb
> 04.py
>
> it uses functions like hid.open, hid.close, hid.read, hid.write, and
> hid.send_feature_report   I'm trying to figure out how to somehow get
> similar functions to those in FPC.   Some example of how to perform
> those kinds of functions would be extremely helpful!

These hid.open, hid.close, ... functions are all methods of the object "hid", which was previously assigned to from devices[0] (line 68), which itself was returned from en.find(), where en is an object of
Enumeration() from the package easyhid.

To communicate with USB HID devices, please peek into the outdated branch “master” of pas-libust (for legacy libusb-0.1) and open file src/usb.pas. This is the old analogous to the new libusboop.pas.

It has the class TUSBPseudoHIDInterface which implements basic communication with USB HID devices.

I've used that for a project with a barcode scanner. Please find attached the two files snapi.pas (implementing the "standard" SNAPI for barcode scanners, which is based on USB HID), and barcodescannersymbolms4407.pas (implementing a few more specific methods for a Symbol MS4407 barcode scanner). The latter file uses several units of my project, therefore it will not compile on your host. It is just to see how to use the SNAPI devices. I release these files with the same license as libusboop.pas.

Please excuse that these files are still based on the outdated pas- libusb for the old libusb-0.1. Therefore they would need translation for the newer libusb-1.0 stuff. Unfortunately I've never head time to transition the project. Actually you could copy the class TUSBPseudoHIDInterface (plus the types THIDReport and TIntrReportFunc) from usb.pas to libusboop.pas and change everything necessary.

At least one difference is that all names change from TUSB* to TLibUsb*. I'm sure there are more, but I don't know by heart.

Please also excuse that I can’t recall why its just “PseudoHID” and not a fully-fledged HID. Probably I didn’t implement all HID functions, or the SNAPI HID interface just isn't registered with descriptor type $21.

This brings me to the second topic, where you got the message
  Unknown descriptor type $21 with length 9 from test2controlsync. That's a HID descriptor (see LIBUSB_DT_HID). It is just not decoded in test2controlsync.

And finally, the third point regarding the bug with PortPath index.
I've fixed that now and pushed to Github. Thanks for pointing this out!

Thanks
  Hansi

> -----Original Message-----
> From: fpc-pascal <fpc-pascal-bounces at lists.freepascal.org> On Behalf 
> Of Johann Glaser
> Sent: Friday, August 9, 2019 7:17 AM
> To: fpc-pascal at lists.freepascal.org
> Subject: Re: [fpc-pascal] USB Human Interface Devices
>
> Hi!
>
> Just a quick reply while at work, I'll reply to all EMails in the 
> evening.
>
> First of all: Thanks for finding the bug with the index variable I vs. 
> J. I'll fix that in the evening.
>
> Secondly: You are correct, the "EZ-USB" is a specific chip family by 
> Cypress, e.g., AN2131, without Flash/ROM but with SRAM for the 
> firmware. I built a few devices with that chip, therefore I also 
> implemented the Pascal "driver" to download the firmware into the 
> "naked" device. There are also many (old) devices on the marked which 
> use that chip, e.g., the Keil uLink (1st generation).
>
> To understand how USB actually works (endpoints, in and out transfers, 
> control/bulk/interrupt/isochronous transfers, ...), I have learned a 
> lot from the Technical Reference Manual (TRM) of the EZ- USB. Just 
> search for it, there are plenty of sources. Chapter 1 is the 
> interesting one. If you directly search at the Cypress page, they will 
> redirect you to the successor product FX2, but its chapter 1 is nearly 
> identical to that of EZ-USB.
>
> The EZ-USB firmware download is performed with control transfers to 
> EP0. So you can find examples for control transfers in ezusb.pas.
>
> You can find example code for bulk and interrupt endpoints e.g., in 
> https://github.com/hansiglaser/pas-gpib/blob/master/usb/usbtmc.pas.
>
> I can provide more examples in the evening.
>
> Bye
>   Hansi
>
> > Gesendet: Freitag, 09. August 2019 um 12:09 Uhr
> > Von: "James Richters" <james at productionautomation.net>
> > An: "'FPC-Pascal users discussions'" < 
> > fpc-pascal at lists.freepascal.org>
> > Betreff: Re: [fpc-pascal] USB Human Interface Devices
> >
> > Maybe I am going down the wrong path trying to get 
> > test_arduino_with_bmp280.pas to work with my device...  I think I am 
> > confusing easyusb with easyhid which is what is used to communicate 
> > with the device in the python code I am trying to use.  It looks 
> > like easyusb is to send firmware to a specific usb device that has 
> > no rom in it... and that's not exactly what I'm trying to do...
> >
> > I'm trying to figure out how to open the device and read data and 
> > write data to this device.  Is there an example of how to do that 
> > with pas-libusb?
> >
> > Sorry I am so confused by all this.  I feel like I'm almost to where 
> > I will understand things again but I'm just missing something.
> >
> > James
> > _______________________________________________
> > fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org 
> > https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
> >
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org 
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org 
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


More information about the fpc-pascal mailing list