RasPI 3 - Trouble accessing HID custom made device.

Discussion in 'HelpDesk' started by Especialista, Oct 5, 2018.

  1. Especialista

    Especialista is a Trusted Warez PosterEspecialista B4A Enforcer Staff Member Super Moderator DEV Guild Reverser Translator

    Joined:
    Jul 15, 2011
    Messages:
    14,759
    Likes Received:
    40,933
    I have a thing disturbing me and since I'm not that "Linux Guy", I ask for help from you.

    Workbench Situation:
    • Raspberry PI 3-B, with Raspbian Stretch updated and running Ok.
    • Free Pascal Compiler version 2.6.4+dfsg-4+rpi1 [2014/10/21] for arm
    • Lazarus 1.2.4+dfsg2-1
    • Custom Made Device based on PIC18F2550, connected via USB to RasPi 3. (For information, it performs as predicted if I connect it to Windows, and I can access it via sw made with Delphi X10.2)
    What I'm sure at the moment:
    • RasPi reconizes my custom made device:
    [​IMG]

    DMesg Command Output:
    Code:
    https://www67.zippyshare.com/v/0kNgBPVm/file.html
    
    • I've created a file named /etc/udev/rules.d/55-pic-usbhid.rules with this contents:
    Code:
    # This is a sample udev file for HIDAPI devices which changes the permissions
    # to 0666 (world readable/writable) for a specified device on Linux systems.
    
    
    # If you are using the libusb implementation of hidapi (hid-libusb.c), then
    # use something like the following line, substituting the VID and PID with
    # those of your device. Note that for kernels before 2.6.24, you will need
    # to substitute "usb" with "usb_device". It shouldn't hurt to use two lines
    # (one each way) for compatibility with older systems.
    
    # HIDAPI/libusb
    #SUBSYSTEM=="usb", ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="000b", MODE="0666", GROUP="plugdev"
    SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", SYSFS{idVendor}=="04d8" , SYSFS{idProduct}=="000b", MODE="0666", GROUP="plugdev"
    
    # If you are using the hidraw implementation, then do something like the
    # following, substituting the VID and PID with your device. Busnum 1 is USB.
    
    # HIDAPI/hidraw
    KERNEL=="hidraw*", ATTRS{busnum}=="1", ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="000b", MODE="0666", GROUP="plugdev*
    
    # Once done, optionally rename this file for your device, and drop it into
    # /etc/udev/rules.d and unplug and re-plug your device. This is all that is
    # necessary to see the new permissions. Udev does not have to be restarted.
    
    # Note that the hexadecimal values for VID and PID are case sensitive and
    # must be lower case.
    
    # If you think permissions of 0666 are too loose, then see:
    # http://reactivated.net/writing_udev_rules.html for more information on finer
    # grained permission setting. For example, it might be sufficient to just
    # set the group or user owner for specific devices (for example the plugdev
    # group on some systems).
    
    
    • Using LibUsb Wrapper for Lazarus.
    • My Lazarus Test program compiles with no warnings.
    Problem:

    When I run my test program:
    Code:
    procedure OpenLibusb();
    begin
       ctx := nil;
    
      returncode:=libusb_init(@ctx);       //init libusb API
      libusb_set_debug(ctx,3);             //set debug
    
      cnt := libusb_get_device_list(ctx,@devs);
      dev_handle:= libusb_open_device_with_vid_pid(ctx, $04D8, $000B); //hid lib test
      libusb_free_device_list(devs, 1); //free the list, unref the devices in it
    
       //kernel driver attaching problem
       if (libusb_kernel_driver_active(dev_handle, 0) = 1) then  //find out if kernel driver is attached >>> Error Occurs Here!!!
    
    ....
    
    variable cnt gets 4 (meaning it found 4 USB related devices - as shown in lsusb command) but the variable dev_handle receives NIL, causing an Acces Violation on the next line - libusb_kernel_driver_active(dev_handle, 0)

    Questions:
    1. How to solve this?
    2. Is there any easier way to make it run in Lazarus?
    Please, remember I'm not that "Linux Guy" and any advice is welcomed!

    Thanks in advance!

    PS: @Ph!d , you've saved me before.... Maybe you can pull another rabbit from that old hat of yours.. ;)
     
  2. Ph!d

    Ph!d is a Trusted Warez PosterPh!d Board Protector Staff Member Administrator DEV Guild Reverser Translator

    Joined:
    Sep 11, 2008
    Messages:
    62,371
    Likes Received:
    126,110
    At first sight, it seems like a permissions issue.
    Can you verify you have the rights to read/write to it? Did you run the test program with sudo?

    Searching on the net I read that libusb_open_device_with_vid_pid finds and opens the device but doesn't return an error message if it fails. Maybe you could use another function which could return a more detailed error message and hence have a better idea of what could be the issue?
     
  3. Especialista

    Especialista is a Trusted Warez PosterEspecialista B4A Enforcer Staff Member Super Moderator DEV Guild Reverser Translator

    Joined:
    Jul 15, 2011
    Messages:
    14,759
    Likes Received:
    40,933
    "Non Linux Guy" Question: How to do it? o_O

    I've opened a terminal and used "gksudo" to run it. Seems it found and attached my device to the program, but gave me anoter acces violation in another part I cannot verify right now, since I did not run it from Lazarus.

    Another question: How shall I proceed to run Lazarus with root permissions, so I'll be able to debug?
    Or how shall I proceed to give permitions to the device to non root users?
     
  4. Especialista

    Especialista is a Trusted Warez PosterEspecialista B4A Enforcer Staff Member Super Moderator DEV Guild Reverser Translator

    Joined:
    Jul 15, 2011
    Messages:
    14,759
    Likes Received:
    40,933
    Hey, @Ph!d !

    After 9837 coffes, It seems I got it workng.
    There was a typo in my udev rules file. Here's the correct one:
    Code:
    ###########################################################################
    ## this file must be stored in: /etc/udev/rules.d
    ###########################################################################
    
    # This is a sample udev file for HIDAPI devices which changes the permissions
    # to 0666 (world readable/writable) for a specified device on Linux systems.
    
    # If you are using the libusb implementation of hidapi (hid-libusb.c), then
    # use something like the following line, substituting the VID and PID with
    # those of your device. Note that for kernels before 2.6.24, you will need
    # to substitute "usb" with "usb_device". It shouldn't hurt to use two lines
    # (one each way) for compatibility with older systems.
    
    # HIDAPI/libusb
    SUBSYSTEM=="usb", ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="000b", MODE="0666", GROUP="plugdev"
    
    # If you are using the hidraw implementation, then do something like the
    # following, substituting the VID and PID with your device. Busnum 1 is USB.
    
    # HIDAPI/hidraw
    KERNEL=="hidraw*", ATTRS{busnum}=="1", ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="000b", MODE="0666", GROUP="plugdev"
     
    # Once done, optionally rename this file for your device, and drop it into
    # /etc/udev/rules.d and unplug and re-plug your device. This is all that is
    # necessary to see the new permissions. Udev does not have to be restarted.
    
    # Note that the hexadecimal values for VID and PID are case sensitive and
    # must be lower case.
    
    # If you think permissions of 0666 are too loose, then see:
    # http://reactivated.net/writing_udev_rules.html for more information on finer
    # grained permission setting. For example, it might be sufficient to just
    # set the group or user owner for specific devices (for example the plugdev
    # group on some systems).
    
    Now I do not need sudo anymore to access the device.
    I'll test it reading/writing stuff and see if it is really working!

    Thanks!
     
  5. Ph!d

    Ph!d is a Trusted Warez PosterPh!d Board Protector Staff Member Administrator DEV Guild Reverser Translator

    Joined:
    Sep 11, 2008
    Messages:
    62,371
    Likes Received:
    126,110
    That's a lot of coffees from 10:20 to 15:36... :eek:
    Anyway, glad you got it working now! I mentioned running as sudo since it's the higher privilege, you make sure it is communicating with the device and go down from there. ;)
     
  6. Especialista

    Especialista is a Trusted Warez PosterEspecialista B4A Enforcer Staff Member Super Moderator DEV Guild Reverser Translator

    Joined:
    Jul 15, 2011
    Messages:
    14,759
    Likes Received:
    40,933
    @Ph!d , new problems right now.

    I'm trying to access the same mentioned device using LibUSB. I can send the data, but the data arrives at the device corrupted and I do not know why (yet).

    Could you recommend me an easy way to access USB HID devices, using lazarus in Unix?
     
  7. Ph!d

    Ph!d is a Trusted Warez PosterPh!d Board Protector Staff Member Administrator DEV Guild Reverser Translator

    Joined:
    Sep 11, 2008
    Messages:
    62,371
    Likes Received:
    126,110
    Sorry, I don't know of any... I stopped interfacing USB devices with Linux a long time ago (unless is a pen drive or gamepad or similar) due to headaches getting things to work.

    Is there any specific reason to interface the RPi through USB?
     
  8. Especialista

    Especialista is a Trusted Warez PosterEspecialista B4A Enforcer Staff Member Super Moderator DEV Guild Reverser Translator

    Joined:
    Jul 15, 2011
    Messages:
    14,759
    Likes Received:
    40,933
    Since I'm using a 5" touch display and it consumes most of GPIO pins, I tought it would be the smart way to do that.
    But the data exchange between RPi and PIC18 is very simple, something like "How are you?" and "I'm fine thanks".

    Technically speaking, I'll use the PIC18 as a Digital Input card. RPi asks it the status of its ports and it will send back to RPi 4 bytes, one for each port.

    I'm sure it is not like rocket science, but I'm failing to find where I'm missing the point.