Digital Me

Random contributions to digital noise

Linux USB Programming with Python

Wednesday October 13, 2010 @ 04:14 PM (PDT)

Recently I started doing some hardware design, just for fun. Anyway, I got my hands on one microcontroller with USB interface so I wanted to see how I can control it from my Linux box. It turns out to be quite easy and with this any general USB HID class device can be controlled (USB rocket launchers, USB toys and such I suppose).
Anyway, this post just lays out basics on how these devices can be used with Python on Linux. (Mind though that I’m Python, USB and Linux noob so don’t take this for granted – and of course no warranties whatsoever.)
First of all we need some library to access USBlibhid there should be Python bindings with it. I got both the library and the bindings with the following command (on my Ubuntu):

sudo apt-get install libhid-dev python-hid 

Then we need some hardware. I suppose any suitable Hid device can be used as long as communication protocol is known. I created my own device using HexWax expandIO-USB which is just a fancy name for (in my case) PIC18F2455 with HexWax’s firmware. I got mine from Farnell . The chip supports 10-bit A/D, digital I/O, counter input, interrupts, comparators, CCP, SPI, I2C so we can control loads of different peripherals if we wanted to.

The board is very simple and I used simplest reference schema from the documentation. It doesn’t follow any good USB design practices, I’m sure, but it can be assembled on a breadboard . Here’s the schema:

USB board schema

mind though, I used PIC18F2455 but it’s the same thing
And as I said it can be assembled on a bread board:

HexWax on breadboard

This image was made in Fritzing

Very simple – only 6 capacitors, 2 resistors, a LED, a crystal, the chip itself, USB connector and few wires. I assembled it quickly, and instead of using USB connector I cut one USB cable I wasn’t using and stripped off wires and plugged them into the breadboard – it looked ugly but it worked (of course use this approach on your own risk, as with anything else). Here’s USB pinout . Btw, I added a LED and one resistor just so I have some kind of a peripheral to play with.

After I finished testing the design on breadboard I used stripboard to create a more permanent board:

HexWax on stripboard

Basically the same thing, I just added some receptacles on both sides of the chip so I can connect other peripherals later.

Oh yes, since there’s HexWax firmware preloaded there’s no need to do any microcontroller programming, all’s done from PC.

Off to programming part and Python. First of all lets see how this board shows up to our system when we plug it in. Here’s output of lsusb command:

HexWax plugged in

It shows up as a device with ID 0b40:0132. This can be changed in code but I couldn’t be bothered. So lets put libhid into action. Here’s code that is initializing the library, finding the device, reading it’s details (so far all standard code from libhid), then reading device’s firmware, setting our LED pin to be output, and blink 10 times with it with 1 second pause. Simple. Comments are in the code :





This was a quick run through. Hopefully I get some time to play more with this in the near future, and will follow up with new posts.

Comments

Oh man, you got there first! It has been my plan for ages to get to that stuff. I’m a bit jealous now …

Gravatar icon
Stan
Tuesday October 26, 2010 @ 01:16 PM (PDT)

Thanks Stan, you’re not too bad yourself, playing with Android, you can’t have all the toys ;)

Gravatar icon
Wednesday October 27, 2010 @ 03:19 PM (PDT)

Hi, I’ve read your article, it’s very interesting. I would like to do something with python and USB through a PIC. I’m using Ubuntu 10.04 and I tried to work with pyusb 0.4 (the only one for linux) but it didn’t work.
I don’t understand something: with your python software you don’t have to program PIC? Your project work directly with python script? And Linux find device (with “lsusb” command in terminal) only if you installed a microcontroller?
Thanks
p

Gravatar icon
Pat
Friday May 27, 2011 @ 12:36 AM (PDT)

Hi Pat, this is not just a plain PIC18F2455 it was preloaded with HexWax’s firmware that allows communication with it without programming. You would need some USB HID device firmware implementation for your PIC, PIC should have something generic on their site. I’m normally using Atmel MCUs and I know that Atmel has this USB HID firmware available for free on their site, also there are open source implementations like LUFA – http://www.fourwalledcubicle.com/LUFA.php There is probably something similar for PIC.

Hope this helps,

Gravatar icon
Friday May 27, 2011 @ 02:51 AM (PDT)

Ok, thanks for the info. I try to explain my idea. I would like to simulate house with many LEDs as there are rooms. I would like to program this software in python and I can use only USB interface (notebook) with Ubuntu 10.04. I tried to install pyusb but there are no guides and the only available version for linux is 0.4.2. In any case I needed a PIC.
So I’ve read this article and find new way to do my project.
How could I achieve it? Can I use libhid?
Thanks again

Gravatar icon
Pat
Friday May 27, 2011 @ 07:19 AM (PDT)

Have you already created an USB board with your chip? If you haven’t you’ll need to do that first. Then you’ll have to program your chip with appropriate firmware. And only then you’ll be able to use libhid.
The board here is probably sufficient, however I already had firmware pre-loaded on the chip so I did not need this part. If you don’t have firmware already programmed into chip (and I’m guessing you don’t) you’ll need to find programmer to program the chip too.
In that case better example on how to do it is here:
http://www.waitingforfriday.com/index.php/Building_a_PIC18F_USB_device

Gravatar icon
Friday May 27, 2011 @ 09:28 AM (PDT)
This just made my day much brighter. Thanks a lot. Something else I across was this http://dicore.cn/ Take a look!
Gravatar icon
Monday May 30, 2011 @ 11:17 PM (PDT)

Cool Post! Very informative dude. I saw this site while browsing and think it may be relevant http://www.dicore.cn/ keep up the good work!

Gravatar icon
Tuesday June 07, 2011 @ 09:11 AM (PDT)
New comment

required, won't be displayed

optional

Don't type anything here unless you're an evil robot:


And especially don't type anything here:

Basic XHTML (including links) is allowed, just don't try anything fishy. Your comment will be auto-formatted unless you use your own <p> tags for formatting. You're also welcome to use Textile.

Copyright © 2012 Mihailo Lalevic. All rights reserved.
Powered by Thoth.