Recent

Author Topic: USB HID  (Read 9991 times)

folkeu08

  • Full Member
  • ***
  • Posts: 106
Re: USB HID
« Reply #15 on: June 07, 2020, 01:57:10 pm »
Hello,
Is what I am trying to do achievable at least?
I'm still looking and can't find the solution.
Thanks

DonAlfredo

  • Hero Member
  • *****
  • Posts: 1738
Re: USB HID
« Reply #16 on: June 07, 2020, 02:24:04 pm »
Ok. Look into usb2.pas.

Code: Pascal  [Select][+][-]
  1.   Vendor                        = $04d8;
  2.   Product                       = $000b;
Changes these to suit your hardware.

procedure TUSB.DeviceChange(Sender:TObject);

This procedure gets called when HID hardware is added or removed.
You can see inside this procedure that HID-Device Vendor and Product are checked against the above values.
If match, then DeviceArrival(HidDev) will be called.

procedure TUSB.DeviceArrival(HidDev: TJvHidDevice);

This procedure add a new controller:
      NewUSBController := TUSBController.Create(HidDev);
And  this controller gets added into the lists of USB devices:
      AUSBList.Items[newboard]:=NewUSBController;

function TUSB.HidReadWrite(Ctrl: TUSBController; ReadOnly:boolean):boolean;

Use this function to read and/or write towards your USB HID Device.
(in this example, I do send some calibration data towards the HID MicroChip controller.)

Code: Pascal  [Select][+][-]
  1.     with AUSBList.Items[board] do
  2.     begin
  3.  
  4.       FillChar(LocalData, SizeOf(LocalData), 0);
  5.  
  6.       LocalData.Data[0] := byte(CMD_set_cal);
  7.       LocalData.Data[1] := position-1;
  8.       LocalData.Data[2] := 7;
  9.       LocalData.Data[3] := whattocal;
  10.  
  11.       temp:=calval;
  12.       LocalData.Data[4] :=(temp MOD 256);
  13.       temp:=(temp DIV 256);
  14.       LocalData.Data[5] :=(temp MOD 256);
  15.  
  16.       LocalData.Data[6] := DayOfTheMonth(Now);
  17.       LocalData.Data[7] := MonthOfTheYear(Now);
  18.       LocalData.Data[8] := (YearOf(Now) DIV 256);
  19.       LocalData.Data[9] := (YearOf(Now) MOD 256);
  20.  
  21.     end;
  22.  
  23.     error:=HidReadWrite(AUSBList.Items[board],False);
  24.  
  25.     if (NOT error) then with AUSBList.Items[board].LocalData do
  26.     begin
  27.       if ( data[0]=byte(CMD_set_cal) ) AND ( data[1]=(position-1) ) then
  28.       begin
  29.         //do something with the data received
  30.       end else error:=True;
  31.     end;
  32.  

My setup to show you why this is done like described above:
I have 20+MicroChip USB MCU connected to a single PC through USB.
These are all added into the list. And always have the same [board]number due to their unique HID serial number.
Each of these MCU connect through I2C towards another 20 MCU measuring devices. Totalling 400 connected MCU by USB onto a single PC. Running 24/7 for 6 months.
IMHO, USB is very good and stable and can be used just like a RS485 network on steroids.

folkeu08

  • Full Member
  • ***
  • Posts: 106
Re: USB HID
« Reply #17 on: June 07, 2020, 11:59:17 pm »
Good evening,
I'm looking at this this week or the next W-E.
With this help and the next novices like me, I will create a tutorial that you can add to the component.
Proteus (https://www.labcenter.com/usb/) allows a 100% virtual simulation with the examples in the demo version of the software. With that, I will be able to create this tutorial for beginners who do not have a PCB at pic18f4550.
I went to the wrong directions because I was trying with TUSB and MyHID.
François
« Last Edit: June 08, 2020, 12:03:01 am by folkeu08 »

folkeu08

  • Full Member
  • ***
  • Posts: 106
Re: USB HID
« Reply #18 on: August 17, 2020, 08:57:49 am »
Hi all,
I suppose it is this part of code in the USB2 unit that identifies whether the hard USB is connected or not
Code: Pascal  [Select][+][-]
  1. if HidDev.IsPluggedIn AND NOT HidDev.IsCheckedOut then
  2.       begin
  3.         AddInfo('New device that has not been checked out.');
  4.         DeviceArrival(HidDev);
  5.       end;
  6.       if NOT HidDev.IsPluggedIn AND HidDev.IsCheckedOut then
  7.       begin
  8.         AddInfo('Checkedout device that has been unplugged.');
  9.         DeviceRemoval(HidDev);
  10.       end;                                      
  11.  

I took this piece of code to insert it in my Main_Form by modifying it as follows :
Code: Pascal  [Select][+][-]
  1. if HidDev.IsPluggedIn AND NOT HidDev.IsCheckedOut then
  2.       begin
  3.        Detection_Label.Caption := Detected';
  4.      end;
  5.      if NOT HidDev.IsPluggedIn AND HidDev.IsCheckedOut then
  6.      begin
  7.       Detection_Label.Caption := 'Not Detected';
  8.      end;                                      
  9.  

At compilation, I was told that the HidDev identifier was not found.  >:(
@+

folkeu08

  • Full Member
  • ***
  • Posts: 106
Re: USB HID
« Reply #19 on: August 19, 2020, 06:27:30 pm »
Hi all,
I am basically an electronics engineer and not a computer scientist. I would like to be able to dialogue with a PC by the usb.
I have been looking for how to do it since April and I am blocked by the lack of knowledge and my inexperience. I do not despair of getting there.
I do not know if I am doing it well but I can already start the application to modify the label 'Detection_label' if the card is connected or not. For that I added in the procedure procedure (in USB2) "TUSB.DeviceChange (Sender: TObject);" a variable whose string is different if the hard is present or not.
My difficulty is to modify the label if the card is added or removed while the software is running.
does anyone have a lead?
I have set a timer but even that does nothing.

My project is in link because too big to be attached to the post : http://www.club.ac-news.fr/download/USBHID_Test.rar
My third-party components are attached to the post
Thanks
François

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: USB HID
« Reply #20 on: August 19, 2020, 07:58:54 pm »
I am basically an electronics engineer and not a computer scientist. I would like to be able to dialogue with a PC by the usb.
I have been looking for how to do it since April and I am blocked by the lack of knowledge and my inexperience.
Is there a project requirement to use USB HID? If not, and you just want a 2-way communication between microcontroller and pc then you can save your self a lot of trouble if you use simple serial communication like explained here:
https://wiki.freepascal.org/Arduino#Serial_communication

Right now I am in the middle of the STM32 project with composite USB CDC, HID MOUSE, HID KEYBOARD and HID JOYSTICK, and it is many, many times more complex then the link above, so I would not recommend it to a newbie. Above example can get you started in half and hour if you already have a working environment.

You might also want to take a look at this blog:
https://bigdanzblog.wordpress.com/?s=arduino+lazarus
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: USB HID
« Reply #21 on: August 19, 2020, 11:01:13 pm »
Is there a project requirement to use USB HID? If not, and you just want a 2-way communication between microcontroller and pc then you can save your self a lot of trouble if you use simple serial communication

I agree.

When I did /my/ electronics degree we were expected to be able to understand arbitrary technical documentation.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

folkeu08

  • Full Member
  • ***
  • Posts: 106
Re: USB HID
« Reply #22 on: August 19, 2020, 11:42:33 pm »
Hi,
I specialize in analog (audio). I do my hand with the PICs to control audio switching by software.
Hence my difficulties due to a lack of digital experience

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: USB HID
« Reply #23 on: August 21, 2020, 06:08:16 pm »
Well, good analogue guys are hard to find.

Have you investigated DonAlfredo's suggestion of June 7th?

Do you have a pointer to a source of this development kit (or whatever it is) that you're using, so we can see what it is?

The only PIC I've interfaced to was a Velleman kit, but World+dog uses Arduino-style devices in the way suggested by Avra.

Do you appreciate that HID is quite a complex standard sitting on top of USB (etc.), and that one of the things it does is generate a descriptor that gives the OS etc. some of its functional details? Not sure how you'd get at that on Windows... on Linux it's saved in the /sys tree.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: USB HID
« Reply #24 on: August 21, 2020, 09:18:41 pm »
I do my hand with the PICs to control audio switching by software.
So why don't you try simple serial with PIC like here:
https://simple-circuit.com/mplab-xc8-uart-example-pic-mcu/
and try to adapt it to work synapse serial example I showed?

You just need FT232 or similar serial to usb module as shown in the link (if your PIC model does not already come with USB interface and implement CDC).

If you insist on HID then take a look at these:
https://github.com/JoshyFun/VUSBRelayPascal
https://github.com/Zaaphod/HIDAPI.pas
https://github.com/Zaaphod/pas-libusb
https://github.com/Zaaphod/libusbxhid
https://mcuprogramming.blogspot.com/2010/12/example-in-lazarus-for-libusb.html
https://mcuprogramming.blogspot.com/2010/12/libusb-10-wrapper-for-fpc.html?q=usb
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Lulu

  • Full Member
  • ***
  • Posts: 226
Re: USB HID
« Reply #25 on: August 26, 2020, 06:54:46 pm »
Hi, until now I use PIC micro-controller + FT232 circuit like Avra says. But FT232 is very expensive compared to PIC16F1454.
I am interested by the PIC16F1454 and after some research I found this:
https://github.com/jgeisler0303/PIC16F1454_USB2Serial
I have never used this project before so I can't tell you if it work and how to modify it.
wishing you a nice life

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: USB HID
« Reply #26 on: August 26, 2020, 07:27:57 pm »
In that case use something cheaper like the CH340. Or learn how to interpret the existing datasheets for the PIC etc., and the relevant libraries.

It occurs to me that since OP basically wants something that allows him to ackle a few GPIO bits under USB control, it might be worth looking for a chip that does that off the shelf rather than messing about with a PIC or whatever. Apart from anything else, that would presumably mean that the rest of us would be able to source exactly the device that he was using, and that there could be other people who'd find it useful which would give them an incentive to help out.

The article referenced below is oriented towards Linux, I think that I recall OP implying that he used Windows and I'd expect somebody to have an appropriate driver.

https://hackaday.com/2018/02/21/linux-adds-ch341-gpio/

MarkMLl
« Last Edit: August 27, 2020, 09:03:08 am by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

MarkMLl

  • Hero Member
  • *****
  • Posts: 6647
Re: USB HID
« Reply #27 on: September 26, 2020, 10:20:40 pm »
I thought I'd post an update in case it was useful to anybody.

I've now got a CH341 board as sold as item 401298217576 on eBay, it cost me £7.34 but the basic chip is much cheaper. This has a jumper which controls whether it appears as a serial port (supported by the Linux kernel) or an I2C+GPIO+etc. port, that has the effect of changing the USB PID but discussion elsewhere suggests that that is the only change (i.e. within limits a suitable driver might be able to "mix'n'match" the capabilities it wanted).

I can't speak for Windows, but on Linux the driver from https://github.com/gschorcht/i2c-ch341-usb compiles and loads without problem, resulting in individually-controllable GPIO bits in /sys/class/gpio. I've not explored the chip's I2C/SPI capabilities, but I've used the Linux kernel to access those from Pascal code on an RPi without problems in the past.

At present the driver is "out of tree", but on Debian derivatives it is apparently possible to integrate it using DKMS... I've not tried this yet but have found the mechanism a "no brainer" in the past for things like ZFS.

For somebody who simply needs to access a few control bits, I'd suggest that this is a viable alternative to an Arduino or some other microcontroller since it doesn't rely on a program that has to be maintained and downloaded onto the slave device.

MarkMLl




MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

folkeu08

  • Full Member
  • ***
  • Posts: 106
Re: USB HID
« Reply #28 on: September 26, 2020, 11:10:00 pm »
@MarkMLl

I have taken note of your map for future developments.
My hardware is already developed and it only remains for me this problem of USB dialogue to be resolved with Lazarus and the card.
My project is to make an FM transmitter controllable by a USB port.
The information is displayed by 6 7-segment displays. The following version will be displayed by an LCD, then by a graphic touchscreen LCD.
Your card has too few ports for this project.
In addition, I do not wish to limit myself to software under Linux.
I continue my research how to make this dialogue
Francois

folkeu08

  • Full Member
  • ***
  • Posts: 106
Re: USB HID
« Reply #29 on: October 10, 2020, 02:25:07 pm »
Hi,
I have some free time today, I will try new things.
@DonAlfredo: If I put a timer and every second I run a refresh, it will allow me to display in a label if the hard usb interface is connected or not ?
Thanks

 

TinyPortal © 2005-2018