Archive for March, 2011

Open Source CDC USB Stack for PIC

Community gathered around Dangerous Prototypes site decided that it is time we have a decent USB stack for Microchip PIC micro controllers that has usable open source licence. The USB stack provided by Microchip as part of the MAL is great, works perfectly but has a huge flaw, and that is, it does not allow you to properly use it with open source projects. When you have your open source project that uses MAL you share your project on some public CVS/SVN/BZR/GIT.. repository but you have to remove all MAL files and add a readme to explain to users how to download mal, how to install it, where to put it etc … and it all makes a huge mess and is very unreliable. The worse part is that Microchip from time to time decides to make incompatible changes in the MAL so that your old code don’t work with new version of MAL. Of course Microchip don’t keep archive of old MAL releases so if you find open source project that is not using up to date version of MAL you can only cry as there’s no way you can get the same MAL developer of the project uses….

This brings us to the new open source stack being developed by the Dangerous Prototype community, namely Honken and JTR with help from everyone else. You can follow the progress on the Dangerous Prototype forum.

At the moment USB stack support only CDC, but hopefully soon we will have HID and some other profiles too. The latest version of CDC stack in form of simple echo application is available on the forum. I modified it a bit to work with 18F2550 and hid bootloader (this one is microchip’s) and you can find hid bootloader in my previous post where you can find bot HEX of the bootloader and the source for linux app to use it. The modified project for MPLAB.X that creates simple USB CDC device that echo back what you send it can be downloaded from here.

The example showing 18F2550 enumerate:

Mar 15 06:45:15 luckey kernel: [ 6694.873125] usb 5-2: new full speed USB device using uhci_hcd and address 8
Mar 15 06:45:15 luckey kernel: [ 6695.053649] usb 5-2: New USB device found, idVendor=04d8, idProduct=000a
Mar 15 06:45:15 luckey kernel: [ 6695.053653] usb 5-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Mar 15 06:45:15 luckey kernel: [ 6695.053655] usb 5-2: Product: CDC Test
Mar 15 06:45:15 luckey kernel: [ 6695.053657] usb 5-2: Manufacturer: Dangerous Prototypes
Mar 15 06:45:15 luckey kernel: [ 6695.053659] usb 5-2: SerialNumber: 00000001
Mar 15 06:45:15 luckey kernel: [ 6695.055690] cdc_acm 5-2:1.0: This device cannot do calls on its own. It is not a modem.
Mar 15 06:45:15 luckey kernel: [ 6695.055706] cdc_acm 5-2:1.0: ttyACM0: USB ACM device

Tags: , , ,

Microchip Application Libraries include pretty good HID Bootloader made by Microchip developers. It is fairly small and works even on small devices like 18F2550. Microchip also (inside the MAL) provides the application to upload HEX files to the microcontroller using bootloader. This is all nice and all works good, except that if you don’t have windows there is not much you can do with the hid bootloader as there is no linux version.

As I use Linux only (64bit Fedora Linux – currently v10 and v14 are used in my home) I decided to rewrite the bootloader code to work on Linux. I managed to get it working inside few hours. It is just a small shell application that you run passing it single parameter (your hex file), it will erase mcu and upload hex. It uses libusb-1.0 library and the project is using CMAKE (kdevelop4 is used as IDE).

You can download source code (and linux 64bit binary) of desktop application to upload firmware using hid bootloader here

[arhimed@luckey build]$ ./hid_bootloader ../cdc.hex
Family: PIC18
[arhimed@luckey build]$

If you use PIC18F2550 here you can download HEX file with the boot loader firmware. Note that I changed default behaviour of the boot loader so now you enter boot loader if PGC and PGD are connected instead by tying B4 to the Vss. I did this because all my boards have ICSP connector on them so it is easier to add a jumper over PGC/PGD then to waste a B4 pin to boot loader.

Tags: , , ,

If you use Microchip microcontrollers you really have to try out the new MPLAB.X ide Microchip is working on. It is already up to beta5 where all major problems and all major functionalities are fixed.

Major improvements MPLAB.X offers differently from MPLAB v8.x are

  • It is multiplatform so it works on Windows, Linux and OSX
  • It uses modern GUI that works and does not crash (like old ugly mplab 8 does) based on NetBeans (initially made by Sun, now owned by Oracle)
  • Bunch of stuff that comes with NetBeans (code coloring, code expansion, syntax checking, different plugins, multiple projects in project group, versioning …)

Major news in MPLAB.X beta5 compared to MPLAB.X beta4

  • The parsing of the “core” terms works properly now
  • PicKit2 is now supported
  • C18 works now on OSX too

Major problems with MPLAB.X (current beta5 version)

  • It does not work “properly” on 64bit Linux

This is why I’m writing this post. What does “properly” means here. If you have 64bit linux with 64bit java (jdk or jre) MPLAB.X will use this 64bit java and everything will work except MPLAB.X will not be able to see your PicKit2 or PicKit3 (I assume the same problem will be with other programmers but I use PicKit2 and PicKit3). The work around is to compile your own pk2cmd and use it outside of the MPLAB.X ide but it is really ugly solution, not to mention, there is still no open source version of pk3cmd so if you have PicKit3 there is no work around. The only way around this problem is to install 32bit java.

As I do use java a lot and I use it for memory hungry applications too I really need 64bit java to be default on my system (as 32bit java cannot use more then 2G of RAM) so I did following.

  1. Download 32bit java from Oracle
  2. Be sure to get .bin and not .rpm.bin even if you are using rpm based distro!
  3. Execute downloaded bin file in some directory and it will unpack itself
  4. Rename the extracted directory by adding a -32bit suffix to it (for e.g. mv jdk1.6.0_24 jdk1.6.0_24-32bit ) and move the new directory to a place of your choosing (for e.g. mv jdk1.6.0_24-32bit /usr/java/ )

Now create a script that will start the mplab_ide with 32bit java:


Start the script and voila, now your MPLAB.X detects your PicKit2 and your PicKit3 properly :)

MPLAB.X HW Tool Selector

MPLAB.X HW Tool Selector

Tags: , , ,
Back to top