Using the Hardkernel USB IO board under python

Post Reply
admin
Site Admin
Posts: 2
Joined: Tue Feb 06, 2007 9:36 pm
Has thanked: 0
Been thanked: 0
Contact:

Using the Hardkernel USB IO board under python

Unread post by admin » Sat Feb 23, 2013 3:40 am

This post was generated by mlinuxguy.

This is a short howto on controlling the IO board sold by HK under python from the odroid-x2

(1) first remove any old python-usb package (we will use the pyusb-1 package)
$ sudo apt-get remove python-usb

(2) download and install pyusb-1 http://sourceforge.net/projects/pyusb/f ... USB%201.0/
$ tar -xvf pyusb-1.0.0a3.tar.gz
$ cd pyusb-1.0.0a3
$ sudo python setup.py install

(3) Follow the USB IO instructions on the IO board wiki for how to load the usbdemo hex file to the IO board

(4) attach your USB IO board to the odroid-x2

(5) run the following python program

Code: Select all

#!/usr/bin/python
import usb.core
import usb.util
import sys
import time

# find our device
dev = usb.core.find(idVendor=0x04d8, idProduct=0x003f)
# was it found
if dev is None:
        raise ValueError('Device not found')

# handle if device is busy
if dev.is_kernel_driver_active(0) is True:
   dev.detach_kernel_driver(0)

# set the active configuration.  No args the first config
# will become the active one
dev.set_configuration()

# toggle LED by sending toggle_led command
print "Toggling LED"
dev.write(1, [0x80], 0, 100)
time.sleep(1)
dev.write(1, [0x80], 0, 100)

# reading switch status
print "Reading switch status  1=not pressed,  0=pressed"
dev.write(1, [0x81], 0, 100)
ret = dev.read(0x81, 64, 0, 100)
print ret[1]

dev.reset()
This program will toggle the LED's and read the status of the pushbutton on the USB IO board
Note: The USB IO board demo hex file has a loop that you can replace with your own code, look in their main.c

admin
Site Admin
Posts: 2
Joined: Tue Feb 06, 2007 9:36 pm
Has thanked: 0
Been thanked: 0
Contact:

Re: Using the Hardkernel USB IO board under python

Unread post by admin » Sat Feb 23, 2013 4:04 am

Customizing the HK USB IO board demo program

Project name: USB Device - HID - Simple Custom Demo
File: main.c
Function: ProcessIO()

USB commands switch statement in the IO board ROM

Code: Select all

case 0x80:  //Toggle LEDs command
case 0x81:  //Get push button state
case 0x37:   //Read POT command. 
Replace or add your USB command to this switch statement in the ProcessIO() function

Example: case 0x82: // read a GPIO pin
Implement your code in main.c, and use 0x82 in python to read the GPIO pin state

Function: ProcessIO()

Code: Select all

       switch(ReceivedDataBuffer[0])            //Look at the data the host sent, to see what kind of application specific command it sent.
        {
            case 0x80:  //Toggle LEDs command
              blinkStatusValid = FALSE;         //Stop blinking the LEDs automatically, going to manually control them now.
                if(mGetLED_1() == mGetLED_2())
                {
                    mLED_1_Toggle();
                    mLED_2_Toggle();
                }
                else
                {
                    if(mGetLED_1())
                    {
                        mLED_2_On();
                    }
                    else
                    {
                        mLED_2_Off();
                    }
                }
                break;
            case 0x81:  //Get push button state
                //Check to make sure the endpoint/buffer is free before we modify the contents
                if(!HIDTxHandleBusy(USBInHandle))
                {
                    ToSendDataBuffer[0] = 0x81;            //Echo back to the host PC the command we are fulfilling in the first byte.  In this case, the Get Pushbutton State command.
                if(sw3 == 1)                     //pushbutton not pressed, pull up resistor on circuit board is pulling the PORT pin high
                {
                   ToSendDataBuffer[1] = 0x01;         
                }
                else                           //sw3 must be == 0, pushbutton is pressed and overpowering the pull up resistor
                {
                   ToSendDataBuffer[1] = 0x00;
                }
                //Prepare the USB module to send the data packet to the host
                    USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64);
                }
                break;

            case 0x37:   //Read POT command.  Uses ADC to measure an analog voltage on one of the ANxx I/O pins, and returns the result to the host
                {
                    WORD_VAL w;
                    
                    //Check to make sure the endpoint/buffer is free before we modify the contents
                   if(!HIDTxHandleBusy(USBInHandle))
                   {
                       w = ReadPOT();               //Use ADC to read the I/O pin voltage.  See the relevant HardwareProfile - xxxxx.h file for the I/O pin that it will measure.
                                          //Some demo boards, like the PIC18F87J50 FS USB Plug-In Module board, do not have a potentiometer (when used stand alone).
                                          //This function call will still measure the analog voltage on the I/O pin however.  To make the demo more interesting, it
                                          //is suggested that an external adjustable analog voltage should be applied to this pin.
                  ToSendDataBuffer[0] = 0x37;     //Echo back to the host the command we are fulfilling in the first byte.  In this case, the Read POT (analog voltage) command.
                  ToSendDataBuffer[1] = w.v[0];     //Measured analog voltage LSB
                  ToSendDataBuffer[2] = w.v[1];     //Measured analog voltage MSB

                        //Prepare the USB module to send the data packet to the host
                       USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64);
                   }               
                }
                break;
        }
A proposal for a generic HK USB IO board rom

Concept: Modify the usbdemo code for the USB IO board to provide some generic I/O functionality

Implementation:
Modify the USBDEMO switch statement to add the following:
(1) generic GPIO IN/OUT via 0x82=in and 0x83=out
(2) SPI read and write via 0x84 and 0x85
(3) I2C read and write via 0x86 and 0x87
(4) ADC conversion via 0x88
(5) PWM out via 0x89
(6) UART in and out, 0x8a and 0x8b

Benefits:
The PIC18F docs and sample code have a fairly steep learning curve, let alone the challenge of getting the programming environment setup properly.
By extending the PIC demo code to include some basic functionality such as GPIO pin I/O, the utilization of the HK USB IO board would be much easier for a broader audience.

First pass implementation
Generic GPIO implementation seems the easiest to start with

Notes: I'm a bit snowed under with other Linux projects currently but did manage to get the PIC programming env setup under linux. Getting the env properly setup to find all include files was the hardest part.

mlinuxguy
Posts: 840
Joined: Thu Feb 28, 2013 10:28 am
languages_spoken: english
ODROIDs: X, X2, XU, XU3, XU4, C1, C1+, C2, N1, USB-IO
Has thanked: 0
Been thanked: 0
Contact:

Re: Using the Hardkernel USB IO board under python

Unread post by mlinuxguy » Thu Feb 28, 2013 11:20 am

I have done a lot of updates to the HK USB IO board
Please refer to this forum post for a brief update on some of the features I have added:
http://forum.odroid.com/viewtopic.php?f=53&t=174

I need some updates on the Android side
I have added many features to both the basic rom and demonstrated via SFR register calls even more features. All of those
have been for Linux and Python, I have done nothing for Android. If someone wishes they could port some of my python demo programs to Android to provide such features as:
Generic GPIO configuration and control
SPI and I2C communication
PWM output
and many more

Post Reply

Return to “Hardware and peripherals”

Who is online

Users browsing this forum: No registered users and 0 guests