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.

Saturday, April 30, 2016

Battery holder for cellphone batteries

In this write up I will show a simple construction that I did to hold my cellphone battery for charging. The construction uses only few items which could be easily salvaged.

Why

The problem is that the cellphone batteries come in various sizes and to fit these batteries in a holder outside the phone is tricky.

This holder comes handy if you have a phone battery that you want to charge outside the phone especially if you are involved in servicing. One could solder the contacts of the battery but it is usually not advised to heat the Lithium battery contacts as they could explode. Also the soldering will leave a tinned surface which might not look good.

Disclaimer

Lithium rechargeable batteries are very dangerous if you are not handling them carefully. It could heat up or even explode if you reverse the polarity or short the contacts. I am not responsible for any damage, injuries, loss or anything caused directly or indirectly as a result of following these steps.

How

Here are the list of items you need,

  1. 10cm long threaded screws and nut x 4 (or 2)
  2. A base plate which I used an old PCB, 5x3cm
  3. Another piece to keep in perpendicular with the base.
  4. A prototype PCB 
  5. Two spring contacts like pogo pins or custom made as we will see later.

Step 1: Long screw


  • Find long screw (2 or 4) that is suitable for your battery length.
  • If a long screw is not available, you can make one from smaller screws. I had to use 3 smaller screws to make for my required length. I chopped the head of 2 screws and joined them using solder as shown in the pictures below. 
  • You can use glue to hold them together and/or a heat shrink. 
  • With the custom made screws, the nut will likely stay on the top screw and wont go through the joint. So it is easier to build for a bigger battery and pad it from the bottom to fit smaller ones.
  • You can join the screws without chopping the head, but in that case you will have to check if the screw will go into the base plate after the construction or do it "in-place". See the base plate pictures below.
  • Chop off the head
    Join for required length

    Step 2: Base plate with screw holes

    The size of the base plate will depend on the battery size, I used a 5cmx3cm old pcb. You can also see the screw tucked with some glue.
    Base plate with 4 holes drilled

    Step 3: Perpendicular plate with screws

    The perpendicular piece is to support the battery from sliding over. It need to be only of a small size when compared to the height of the battery.

    In the image I have shown an in-place screw joined with glue and supported with a heat shrink.
    Base with perpendicular plate
    Base and perpendicular with 2 screws diagonally placed

    Step4: Prototype PCB for contacts

    The prototype board is used  for placing the battery contacts. Having a general purpose board helps to adjust the placement of contacts especially as the contacts vary widely with batteries.
    Drill 4 holes and align with the base.
    Prototype board

    Step 5: Spring contacts

    • You need metal spring contacts to touch the battery terminals. 
    • We can use the pogo pin contacts - spring contacts which are usually used for test beds. 
    • I didn't have any so I decided to make my own version. 
      • I got a metal  contact from an old aerial cable. 
      • You also need a small spring and I salvaged it from an old mains plug which had automatic shutters. 
      • Some other sources for springs are old toys, or pens.
      • Just remember to get small diameter, so that it don't touch between the battery contacts.
    A metal contact from old copper cable and a salvaged spring
    Make both contacts

    Step 6: Putting it together

    • Get your battery and make sure to mark the polarity of the contacts. 
    • Find the vero board through holes which aligns with the battery contacts and insert the sprint contacts.
    • Again make sure about the polarity and let both leads of the contact go into the through hole so as to not rotate the contact.
    Inserted contacts
    Finished construction
    Both contacts touching the battery, mind the polarity 

    Conclusion

    We went through the construction of a simple phone battery holder for charging. The exposed leads can be connected with a female header to a charging circuitry. The female header helps to readjust for the contacts for a different size battery. The contacts can be fed into a charging circuitry either custom constructed or ready made ebay charging circuits based on the TP4056. This is also extensively reviewed here, http://lygte-info.dk/review/Review%20Charger%20TP4056%20UK.html

    Any constructive comments/suggestions are welcome.Thanks for reading through and hope this was helpful. 

    Wednesday, January 6, 2016

    DIY India to UK plug for free


    A simple method to convert an Indian style power plug (Type D) to plug into UK power socket (Type G/BS1363) plug.

    A word of caution: You are playing with mains electric power supply here and any shocks or carelessness can be fatal. The steps that are followed here is dangerous and the information can be incorrect or completely wrong. I am not responsible for any damage, injuries, loss or anything caused directly or indirectly as a result of following these steps. Also the new standard followed in the UK uses a fused plug and it is highly recommended to get a fused multi-plug adapter for the modification.


    Why

    Admittedly a consumer I was looking to buy a converter of this sort and I was thinking it should be cheap (plastic and metal) as both countries operate in same AC specification. But to my surprise I found that the plug costs anywhere from 2-5£. There are cheaper alternatives which are mostly from China and you will have to wait for 2-4 weeks and pray that it doesn't get lost in the customs.


    Gather your supplies

    UK socket (not powered)
    Multi plug adapter
    Filing tool



    1. The Indian power plug. This can be done on the appliance plug itself, but then you are risking the original plug. So I strongly recommend the modification to be made on a fused multi plug adapter, thus not permanently modifying/damaging the device plug. Also this allows to use the adapter for many other appliances. Even though the modification is backward compatible (modified plug should still fit an Indian socket), it makes a loose fit and has the danger of sparks and shock, so beware.
    2. A filing tool. A manual cheap metal filing tool will be sufficient for our purpose. More sophisticated tools like a Dremel will also work.
    3. A UK socket to try on. A adapter socket or a power plug with switch turned off. Please make sure the AC power is turned off before you try the socket.

    Procedure

    The idea is to file the Indian plug to suit the UK one. This is possible, because both these plugs are based on similar dimensions and the newer UK standard is a upgraded version of original type D.

    Step 1: File the ground pin

    File the Ground lead (longer and thicker round pin) evenly and slowly to look like the flat UK one. With the multi plug, tighten the screws that supports the pins of the plug. Rest the plug side wise on a flat surface, holding the plug with one hand, slowly file one side. Keep comparing this with a UK plug, not to over do the filing. Once the surface is flat, repeat on the opposite side. Make sure that the second filing is exactly opposite to the first side. If it helps, you can tighten the screws of the pin so that it does not rotate while filing.

    Ground pin filed

    Step 2: File line and neutral pins

    Similarly file the smaller pins. This should be much more easier than the ground pin. The easiest way here is to keep trying the plug on the UK socket and judge on what filing  is required. Usually both the pins need a L shape filing with the vertical side of L facing the vertical side of the other L, like this   _|   |_



    Final plug






    Final step:  Keep checking for fit

    Keep checking the plug with the UK socket again with AC power switched off and use the visual feedback to do necessary filing in step 1 and 2.


    This is a quick and simple hack and hoping that it will be useful for someone. Thanks for reading. Please share your thoughts and comments below.


    Monday, November 23, 2015

    Supercharge your DLink DSL-2750u with OpenWrt

    This articles talks about how to install OpenWrt on D-Link DSL-2750U-C1 router. if both terms are new for you, then probably you are in the wrong place. But for the curious, DSL-2750U is a wireless ADSL2 router with 4 LAN ports and the product page is here and OpenWrt is a Linux distribution for embedded devices with a special emphasis on routers.

    Note: This article talks only about DSL-2750U-C1 which is based on Broadcom chipset and the steps will not help for DSL-2750U-U1, which is based on RTL chipset. Please contact openwrt if you happen to have the latter.

    Disclaimer

    Please pay attention as there is a fair chance that you might brick the router and make it completely useless. You are completely on your own and I am not responsible for any direct or indirect consequences for following these steps or any recommendations that I a make. So turn back now if you are unsure.

    Why OpenWrt

    So you don't go by warnings, lets risk it then.
    The benefits of installing OpenWrt is that you can do almost anything with your router respecting the hardware capabilities. OpenWrt is still limited by the fact that there is no ADSL support, so if you are looking to use as an ADSL router then you are better off with the default firmware.

    There is already a long list of OpenWrt supported routers but unfortunately as of this writing this particular model is not supported. This router is completely supported by the OpenWRT firmware, see below for details. My main interest was that I have this router and want to use this to extend  my wireless connection to an old wired desktop computer.

    How to flash

    The trick is that  you can use the firmware for A4001N for flashing this device, like this one here,  https://downloads.openwrt.org/chaos_calmer/15.05/brcm63xx/generic/openwrt-15.05-brcm63xx-generic-A4001N-squashfs-cfe.bin.
    I have picked the latest firmware linked on this page here and the wireless and ethernet work fine with this firmware. My primary use of the unit is as a router, connecting wirelessly to my home wifi and providing connectivity to my desktop (no wireless capability) and printer in my garage.

    For flashing, the OEM firmware-update through the web interface will not work. You have to use the CFE (bootloader) web interface (hold on reset button while powering on the router), refer https://wiki.openwrt.org/doc/techref/bootloader/cfe#using_cfe_web_http_server.

    Just to summarize the flashing steps,
    - Assigning static ip to your machine which is connected to router using a LAN cable
    - power off the router, press the reset, power on the router *keeping* the reset pressed, release after few seconds when the light goes red
    - Point your browser to 192.168.1.1 (remember to clear caches of the browser if you have problems in accessing the page)
    - upload the firmware through the web interface, it will take few minutes and restart
    - Remove the static IP for your machine and configure to do DHCP and Enjoy

    How did I find it

     Initially, I tried with the closest-matching name logic and picked up the DSL-2740 firmware.  I was able to flash it and the ethernet port worked but the WiFi didn't as it had an Atheros chip. I slipstreamed various bcm drivers but it all failed in the step "get the SPROM". Later I worked through the code and found out that the board files have static  SPROM entries for each of the bcm43xxx chips. So I had to pair with a OpenWrt firmware configuration which matched the SoC 63xx and wifi chip BCM43225, so that it can use the static SPROM definition.  This is how when I found A4001N as one such configuration. I had to use the serial port to examine the boot loader and kernel output. I used an Arduino Uno for connecting to the serial port of the device  and minicom terminal (115200bps, no-hw/no-sw flow control, 8bit, 1-stop bit) in Linux to capture the output/input.


    Every now and then I see questions in OpenWrt forums about this device, so thought of sharing this info hoping to be useful for someone. Thanks for reading.