Sunday, May 25, 2014

PL-2303HX bit-bang AVR programmer

I've blogged before about PL-2303HX USB-TTL adapters.  In some ways they are even better than the expensive FTDI adapters - for example they work at 6mbps.  In conjunction with a cheap Pro Mini clone it's possible to get started with Arduino/AVR programming for $5.  For more advanced use such as burning bootloaders or directly programming AVR chips, I usually recommend a USBasp.  While writing picoboot-avrdude, I noticed avrdude supports bit-bang SPI programming not just with a parallel port, but also with a serial port.  Here's how hacked a PL-2303HX module so I could use it as a bit banging SPI AVR programmer.


The programmer type in the avrdude.conf file is "serbb".  Here's the description line from the "ponyser" programmer in avrdude.conf:
desc  = "design ponyprog serial, reset=!txd sck=rts mosi=dtr miso=cts";
I needed to break out the RTS, DTR, and CTS lines in order to make it work.  I found a pinout diagram of the PL2303HX, and marked the pins with a green star:
The modules come with a translucent shrink wrap protecting them, so I cut that off with a utility knife.  I tinned the ends of some pin headers, and attached them to the back side of the module with some contact cement - other types of glue would likely work fine as well.  Although I have the extra pins at the top of the board, it might be better to have them going out to the side so it doesn't cover the silk-screen labels for the Tx/Rx lines.  The hard part is soldering wires to the .65mm pitch pins on the SSOP-28 package.

I used 30AWG wire-wrap, and initially tried tinning the end of the wire.  I didn't have enough solder to make a good connection so I tried with a small ball of solder at the end of my soldering iron.  It was too much, and bridged a few of the pins, requiring solder wick to clean it up.  Eventually after a few more tries of tinning followed by attempts at soldering to the pin, I got a good connection.  I think it would be easier using solder paste.  Once I had the 3 wires connected, I used a multimeter to verify continuity and ensure I had no shorts.

With the hardware verified, I was ready to test it out.  I made the connections from the module to an ATtiny88 I had on a breadboard.  The USB-TTL adapter was com16, so I used the following avrdude options:
$ avrdude -C /etc/avrdude.conf -c ponyser -p t88 -P com16
But it didn't work:
avrdude.exe: ser_open(): can't open device "com16": The system cannot find the file specified.

I tried the same adapter with a pro mini with an arduino-compatible bootloader, and had no problems using the following command line:
$ avrdude -C /etc/avrdude.conf -c arduino -p m328p -P com16

A comparison of ser_win32.c and serbb_win32.c revealed some extra code at line 134 for dealing with high com port numbers.  It prepends "\\\\.\\" to the port name, so I did the same thing on the command line (I hope to clean up the avrdude serbb code in the longer term):
$ ./avrdude -C /etc/avrdude.conf -c ponyser -p t88 -P \\\\.\\com16
Now instead of "can't open device" I was getting a "device not responding" error.  I used an LED to verify that there were pulses on the SCK and MOSI lines, and could tell from the Tx LED on the module that Tx was being pulled low for the RST line.  I then remembered that TTL signals are inverted vs RS232, so I created a new programmer type in the avrdude.conf file:
programmer
  id    = "pl2303";
  desc  = "pl2303 serial, reset=!txd sck=!rts mosi=!dtr miso=!cts";
  type  = "serbb";
  connection_type = serial;
  reset = ~3;
  sck   = ~7;
  mosi  = ~4;
  miso  = ~8;
;
With that, combined with slowing down the communication speed by adding -i 500, I got it to work:

6 comments:

  1. Replies
    1. Yes, but you'd need a custom driver to use them. RI, DSR, and DCD can be used as general-purpose inputs.

      Delete
  2. It seems that in the datasheet of the PL2303-HX RevD the pins 13 and 14 are replaced by GP2 and GP3:

    http://wenku.baidu.com/view/5472e5f5f61fb7360b4c65b3

    If you have 4 GPIOs available, you could use those to do something like done with the FT232R:

    http://make.kosakalab.com/arduino/bootloader/index_en.html

    ReplyDelete
    Replies
    1. The revD seems to have 8 GPIOS:

      http://prolificusa.com/portfolio/pl2303hx-rev-d-usb-to-serial-bridge-controller/

      "Supports up to 8 GPIO pins (Rev D) – 4 General Purpose I/O pins & 4 Auxiliary General Purpose I/O"

      Delete
  3. I have an usb to rs232 port converter with the same chipset pl2303hx.... what pins from the serial port should i use to connect to the arduino?

    ReplyDelete
    Replies
    1. This project is to program a bare AVR using ICSP. For basic arduino questions I suggest the Arduino forums on arduino.cc.

      Delete