Sidetracked porting EasyAVR programmer to Linux
A silly notion of a wandering mind. Right now this is on the backburner, but I may come back to it one day soon when I get stuck on something else. Was a good intro to USB and Linux driver development.
Right now I have an old Windows box with the AVRFlashv2 programmer for the EasyAVR board running so I can program the board with my avr-gcc compiled programs built on my Linux workstation. Eventually I’d like to get rid of the Windows box. I’ll probably end up making a serial programmer that can plug into the PORTx pin headers on the board to side-step the USB programmer altogether but I thought I’d spend a couple of hours checking out USB and Linux.
Since the USB programmer on the EasyAVR is undocumented, I spent some time sniffing the USB packets with SnoopyPro. I ended up with a bunch of log files for each action of the programmer (erasing, writing the flash, writing the eprom, programming the fuse and lock bits, etc). I found each transaction rather convoluted, and probably unnecessary. For example, I flashed two different .hex files, and the USB packet transfers were almost identical apart from packets 64-66 where the program was sent to the device using an Isosynchronous transfer.
My next step was to get Linux to do some basic communicating with the dev board. I found this Linux Journal article explaining kernel USB driver basics. Initially I wrote a kernel USB device driver based on the USB skeleton driver, and got the driver to acknowledge the existence of the device. I then got it to send and receive some Bulk Transfers of things like “0501″ to see how the device would respond and it did so according to the previous USB packets I’d sniffed earlier.
A bit more googling and reading I found that I could talk to the USB bus using the userspace USB library libusb. I was all set to start writing a little programming or shell where I could send and receive packets as a test to the device (see source below), when I noticed that libusb v0.1 which comes with Ubuntu, did not support the asynchronous transfers. Looking at the USB packet logs, the program is flashed to the device using these Isosynchronous transfer packets. Doh!
The development branch of libusb (v1.0) does support asynchronous transfers via kernel callback functions. I’ve gotten the source (from the Sourceforge SVN) to compile with some difficulty but since it’s getting late and I haven’t finished other stuff, this will have to be explored at another time.
My initial easyavr1.c:
1 comment/*Â Â Â $Id: easyavr1.c 4 2008-05-14 07:29:31Z faulteh $
*
* Â Â Â Version 0.1 ($Rev: 4 $)
* Â Â Â $Author: faulteh $
* Â Â Â $Date: 2008-05-14 17:29:31 +1000 (Wed, 14 May 2008) $
*
* Â Â Â Description:
* Â Â Â Â Â Â Testing source for communication with EasyAVR over USB on Linux
*
*/#define EASYAVR_IDVENDOR 0×3e1a
#define EASYAVR_IDPRODUCT 0×0200#include <stdio.h>
#include <usb.h>static struct usb_device *findEasyAVR(uint16_t vendor, uint16_t product)
{
struct usb_bus *bus;
struct usb_device *dev;
struct usb_bus *busses;usb_init();
usb_find_busses();
usb_find_devices();busses = usb_get_busses();
for (bus = busses; bus; bus = bus->next)
for (dev = bus->devices; dev; dev = dev->next)
if ((dev->descriptor.idVendor == vendor) && (dev->descriptor.idProduct == product))
return dev;return NULL;
}int main (int argc,char **argv)
{
// Get hold of the USB device
struct usb_device *dev;dev = findEasyAVR(EASYAVR_IDVENDOR,EASYAVR_IDPRODUCT);
// We didn’t find it
if (dev == NULL) {
fprintf(stderr, “Dev Board not found.\n”);
return 1;
}// We found it, now we can send stuff to it.
printf(”Found Dev Board\n”);return 0;
}
![[del.icio.us]](http://www.scriptforge.org/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://www.scriptforge.org/wp-content/plugins/bookmarkify/digg.png)
![[Google]](http://www.scriptforge.org/wp-content/plugins/bookmarkify/google.png)
![[Technorati]](http://www.scriptforge.org/wp-content/plugins/bookmarkify/technorati.png)
![[Yahoo!]](http://www.scriptforge.org/wp-content/plugins/bookmarkify/yahoo.png)
![[Email]](http://www.scriptforge.org/wp-content/plugins/bookmarkify/email.png)