Saturday, June 13, 2009

PIC32 Devlopement Board

This post is about the board i assembled for experimenting with pic32. You must be wondering about why not use PIC32 USB starter board from microchip ? Well the answer is bcoz it doesn't have enought IO pins. I have to shell out another $72 for pic32 io expansion board. As I need io pins for interfacing more stuff to the board like MMC card etc. so i chose to assemble the board myself at home.

It isn't very difficult to do so. Only tricky part is soldering pic32 chip on to a tqfp64 pin breakout board. Breakout board for 64pin tqfp chips available cheaply. It cost around Rs 60/- ie. around $1.5 :-).

Here is the circuit diagram from the same :


Here is a snap of the finished board. Well it doesn't look pretty but it works :-)







Have fun !!!

NOTE :
1. According to pic32 manual, 10uF connected to VCAP should be a tantalum. In my board it's not a tantalum.
2. USB_JP needs to be configured correctly as per the host or device mode. This board doesn't support auto-detection for Dual-role. For that you need to use USBID & VBUSON to switch 5v intelligently.

Wednesday, June 10, 2009

Interfacing usb webcam to PIC32 microcontroller.

I recently started experimenting with pic32 as it has a USB OTG host mode that can be used to connect cheap peripherals. First peripheral i tried was a 4 yrs old USB camera that was lying around unused. However getting documentation for it was a problem as most of the cheap webcams vendors don't even have a website. The only documentation i could find about it was from http://mxhaard.free.fr/spca5xx.html. Since I knew that webcam worked with my laptop (running linux) so i was sure that this driver is already incorporated into linux kernel source tree. Only approach left now was to go through the driver code and port it to pic32.


Finding source code for driver didn't took long. Basically, hook up camera to laptop and note down the vendor & product id in output of "lsusb". For my camera vendor id was : 0x04fc and product id was : 0x0561. Download linux kernel source code and grep with product id in drivers/media/video. It throws up a file names for sunplus driver. Cross check it with the driver in the output of "lsmod" output and voila we have the original sources for the driver.


However, driver source had lot of linux specific stuff for video and v4l support so next step was to write a new bare bone driver in linux from the original source. This new driver was to dump more USB specific information about cam like interfaces, end-points and alternate configs to use and also the steps required to configure the webcam.


Webcam USB interface details:
Webcam exposed few i2c and reg read/write interfaces through the control end-point. These were used for writing the initial startup-code, configuring various params like brightness etc. and also reading auto-gain settings. It also presented iso end-points with different buffer size in different alternate config to meeting the bandwidth requirements. For my purpose i used the alternate config 0x07 with end-point buffer size of 1024. There are other end-point with smaller buffer-size like 256 also. Camera streaming is initiated by writing few config setting through i2c interface and once started frames are received on iso endpoints. A single image is broken down into multiple frames of 1024 size and sent through iso end-point. Frames are of different types (FIRST_FRAME, INTER_FRAME, LAST_FRAME, DISCARD_FRAME) and driver on host is responsible for stitching up all the frames together to form a complete image. The image transferred by camera is in a propitiatory format and needs to be converted before a imaging software can be used to display it.


A user-application to read frame grabbed by the driver and dump it in a bmp format was also required. This however posed a problem as camera frame format wasn't documented much. Little fiddling in the original source code and googling threw a possible solution to use a open-source library libv4lconvert. It was a smooth ride from there-on to dumping the bitmap. So finally we had the code & more importantly initialization and configuration steps for reading a image from camera.


Porting it to pic32 with microchip usb library didn't took much time except for few things like setting desired alternate configs to use in pic32 USB library.


Image captured from webcam:



Complete source code for user-application & pic32 firmware are available for download.
Source code is embedded into the following image files using OpenStego availabe at http://openstego.sourceforge.net.

For detail procedure on how to recover the source file take a look at http://bytewerx.blogspot.com/2009/07/uploading-files-to-blogger.html.

Do make sure to check the md5sum of recovered source files.

application_zip.png


Image_grabber_hex.png


Firmware_tar_gz.png




md5sum :
application_zip.png : 708548fb26119aeec77c0dc7cce3ac02
firmware_tar_gz.png : da76ba3bf1394be861690bb11c7cc22e
image_grabber_hex.png : 25cb132b3c45cd06a5ed6dc827c4dcda

application.zip : c6876b534fb98c222eebc01a1af8ba7c
firmware.tar.gz : d01cae86832a4e87fa50e92df783bef5
image_grabber.hex : 0656b15fecd78c5f7662c9bc3c18513c

You can alternatively download the applications from my dropbox (www.dropbox.com) public folders :
application.zip
firware.tar.gz
image_grabber.hex