Wednesday, June 8, 2016

Arduino dsPIC30F programmer

This post talks about an ArduinoUNO based PIC programmer useful for dsPIC or similar devices, in particular the dsPIC30F which can be programmed with 5v. PIC devices like dsPIC33/PIC24 have a working voltage of 3.3v and requires a hardware level translator for connecting to Arduino. This will be covered towards the end of this article.

Why an Arduino based programmer ?


For a recent project I had to work with dsPIC30F2020 device. This is the first PIC that I ever worked with and is one off project. So I didn't want to buy a PICkit programmer. But if you are looking to work with various PIC devices, then I think PICkit is a good investment.

The dsPIC30F series is simpler to setup especially with the programming voltage same as Vdd. So no charge pump or similar solutions are required for Vpp. The flash programming specification document is an excellent reference and looks well suited for a bitbang implementation. I was already familiar with Arduino programming environment, so I decided to use it as the device programming interface to PC.

Disclaimer: Please note that if you use this code or hardware, then you are completely on your own. Myself and the original authors are not responsible for any direct or indirect consequences.


Software

The application software is modified to work with a serial device, just like it works with the usb programmer. The serial device can be any device which understands the usbpicprog programming protocol. In my case the serial device is provided by Arduino, so the Arduino code has to do the heavy lifting of understanding the protocol and converting the commands into signals.


Application

I didn't want to reinvent the wheel of developing software resulting in yet another PIC programmer. So I though of finding an opensource pic programmer to add arduino support to it. That left me usbpicprog as the only viable option as it was opensource, has a command line mode (later I found this needs X to work) and a decent GUI. The downside was that it is not actively maintained.

I just hacked the existing code to fit the serial support,  so please excuse the code organisation. The application source is maintained in the git repo here.


Arduino software

The original pic programmer uses a secondary PIC to program the device. I use ArduinoUNO instead of the secondary PIC. The Arduino program understands the programmer protocol and converts it into PIC ICSP instructions and drives the PGC and PGD accordingly. The  source for Arduino can be found here.

The binaries and hexfiles can be downloaded from here.


Hardware

dsPIC 5V Vpp devices


dsPIC30F devices has programming and operating voltage of 5v and programming current (Iddp) of 30mA. Apart from power, we need clock (PGC), data (PGD) and reset (MCLR) pins. The pins and power rails can be directly connected to Arduino as the DC characteristics of these pins also are well within the ArudinoUno's specification

The connection was as simple as connecting all the power and ground rails (Vdd, AVdd, Vss, AVss) to Arduino headers. Then identify 3 digital I/O ports (non PMW pins is sufficient) and connect them to MCLR, PGC and PGD of the chip. No crystal is required as we will be using the ICSP mode in which the system clock is provided externally through PGC. No decoupling caps were used as the device is powered directly from Arduino. Although a 100nf is recommended near the PIC device across the VCC and ground. 

My particular setup (and the software defaults) is shown below. I used a breadboard to test the programmer.
Programmer and LED test wiring
dsPIC30F2020 running LED blink program
Current (mA) measurement of 5V supply rail with blinking LED program

As you may have noticed, I have added a resistor and an LED for a LED blinking "helloworld" test program, but this is not essential if you just want to program the device. The ledblink program is included along with the Arduino source.


Modification for 3.3v devices [untested]

The  3V3 devices, dsPIC33E/F/PIC24 series has an operating voltage of 3.3v. Apart from Vdd, the MCLR, PGC and PGD pin also operates to a safe maximum of 3.3v. Fortunately the ArduinoUno (and may be others too) support a regulated 3.3v rail which can source upto 50mA. But again the digital I/O pins work on 5v which is a pity. So we need some circuitry to translate the 5v TTL logic levels to 3v3  level and back (for PGD pin).

There is an excellent article from microchip which explores the various options available for the conversion. Among the various options, I like the resistor divider which is simpler and cheaper to source. Whatever options you might choose, remember to get,
- Two 5V to 3V3 output pins for MCLR and PGC - a resistor divider network should be good here.
- A bidirectional for PGD - a simple solution is described here
- 3V3 power rail which can source enough current for programming (typically 30mA but refer datasheet of the chip to be sure). The ArduinoUno 3v3 rail should be good enough.

Please drop a message in the comments, in case you find this useful.  Any productive comments or suggestions are  welcome. Thanks for reading and happy hacking.

11 comments:

  1. Hi,
    I am trying to build your project. but cannot understand why you have a dspicprogrammer.elf file? I compile from your source and load via arduino it compiles and downloads fine, but when I run the usbpicprog with my hex file it fails with "usbpicprog not found"

    ReplyDelete
    Replies
    1. Enomem: Arduino Dspic30F Programmer >>>>> Download Now

      >>>>> Download Full

      Enomem: Arduino Dspic30F Programmer >>>>> Download LINK

      >>>>> Download Now

      Enomem: Arduino Dspic30F Programmer >>>>> Download Full

      >>>>> Download LINK nv

      Delete
  2. Hi Alistair,
    The dspicprogrammer.elf was just for debugging (in case somebody is interested) because once you turn it to a hex/bin file all the interesting stuff is hard to find. So you can completely ignore that file and just use the hex file for arduino.

    I hope you are using Linux. So you need to also use the modified usbpic programmer and provide the correct serial port to it. It is usually /dev/ttyUSB0 (which is the default) unless you have something sitting in that port. You can find the port by un-plugging and plugging the arduino and check for dmesg logs.

    ReplyDelete
    Replies
    1. Hi,
      Thanks for your response, The issue turned out to be a faulty cable. It now works with your LED example.Di you ever manage to get the USB Oscilloscope project working? I am trying to flash the hex file but I am not sure it is working?

      Delete
    2. Hi Alistair,
      I never get around doing the oscilloscope. Still stuck at routing the board. Meanwhile I was working with a FT232R based DSPIC programmer. It had the added advantage that the software is completely run at the host (PC) and the I/O rails can do 3.3 v and provide enough current to the device without an external regulator.

      Delete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Hi Andrew,
    Did the compilation work for you ? I tried only with Ubuntu 14.10. I do remember I had some trouble with wxWidgets library. I can find out if you want.

    ReplyDelete
  6. I found the solution. It ended up being a problem my upgraded C-version and a rule change about comparing ifstreams.
    Solution: Inside src/hexfile.cpp I had to change all instances of 'if (fp==NULL)' to 'if (!fp)'
    Thank you for your response.

    ReplyDelete
    Replies
    1. Thanks Andrew, for the problem and solution. I will schedule the mentioned changes with other fixes.

      Delete
  7. Enomem: Arduino Dspic30F Programmer >>>>> Download Now

    >>>>> Download Full

    Enomem: Arduino Dspic30F Programmer >>>>> Download LINK

    >>>>> Download Now

    Enomem: Arduino Dspic30F Programmer >>>>> Download Full

    >>>>> Download LINK x3

    ReplyDelete