Saturday 5 October 2013

Getting JTAGICE MKii ISP(6PIN) and debugWire working with Atmel Studio 6.1 and ATTiny84

The Atmel JTAGICE Mkii can be a pain; but you know this already. I picked up one of these from eBay as an addition to my new-b electronics lab; realizing AFTER I hit the "Buy Now" button, that these can be a real headache to use for the advanced user, let alone an uber-noob such as myself. Still, I was strangely surprised to find the seller had included two of the units in the box, even though the sale was only for one ;)

Well I got lucky, and managed to get it working in days rather than weeks with Atmel Studio 6 (AS6).

Here goes:

1. Download/install Atmel Studio 6.1 from http://www.atmel.com/tools/ATMELSTUDIO.aspx

I had previously tried to install Studio 4, but was unable to get the bundled Jungo USB drivers working with the MKii. Various forums said Studio 5 was unstable, and that AS6 was a massive pig. I didn't try Studio 5, and AS6 IS a pig (as it uses Ms Visual Studio as a base); but it works, and comes with updated Jungo drivers that do work with the MKii. Do the usual check for software updates before proceeding after install.

2. Fire up AS6, connect your MKii via usb, then fire it up.

Apologies to begin, as I don't recall the steps exactly, but I'm pretty certain AS6 auto detected firmware updates for the Mkii the first time I started them up; either way I remember a firmware update being required, and AS6 will do it for you.

The MKii should now be recognized by AS6.

3. Mapping the ISP(6PIN) pin outs for the Mkii

The pin mapping on the JTAG header (attached to the MKii via the ribbon cable) can be confusing as they're marked on the opposite side of the pcb where the header connections are located, and they're (correctly) printed as though you're looking THROUGH the board, rather than it being mirrored. Add to the fact I didn't have a 'squid' cable and you end up with something rather unintuitive.

Fortunately we have the MKii Quick Start Guide, which maps the JTAG pins to their ISP6PIN counterparts.

If you get stuck:

If you have a magnifying glass (or are Superman) you'll notice that both pins 1 and 10 are marked on the header (pin 1 in this case is obscured by a wire (top right @ '1'), but 10 can be faintly seen inside the circle). Also notice the numbering is in REVERSE order starting from the top right.

IMPORTANT!!! While the WIRES you see are for an ISP(6PIN) connection, the NUMBERS correspond to the JTAG pinout! 






Below is a quick reference of the mapping from JTAG to ISP (to ATTiny84), where each number corresponds to the 'pin':

JTAG     ->    ISP(6PIN)    (->    ATTiny84)
1 TCK    ->    3 SCK          (->    9 SCK)
2 GND    ->    6 GND         (->    GND)
3 TDO    ->    1 MISO        (->    8 MISO)
4 VTref   ->    2 VCC          (->    VCC (inline with 460R))
6 nSRST ->    5 RESET       (->    4 RESET)
9 TDI      ->    4 MOSI        (->    7 MOSI)

As I do not have an ISP header/connector on the breadboard, I connected straight from the JTAG header to the relevant pins of my ATTiny84, referencing the ISP column above against the 84's datasheet.


The numbers correspond from the MKii JTAG pinout directly to the ATTiny84 ISP pinout; see the table above for JTAG to ISP pin mappings. I have an LED/resistor combo for checking the final connections on PB0.


The only items of interest/importance are:

  • The 420R resistor between ISP pin 4 and VCC (4 -> VCC): Debug Wire when being disabled/closed causes the MKii to drop the voltage across the circuit to levels low enough to cause the chip to malfunction (~2.2 volts), and the resistor prevents that - see post here for more http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=1104069#1104069 (Note: a 470R resistor was used in the forum post, but I didn't have one so used the closest available @ 420R)

  • Pin 4 (RESET): The MKii needs to have absolute control over the reset to function correctly. I've seen posts where people have put a 10k pull-up resistor, but have also read this wasn't entirely necessary either (as the MKii keeps the pin high). Have also read that the reset capabilities need to be 'cut' when doing real on-board ISP (eg. on an Arduino), and will update the blog when I get to that point ;)


5. Putting it all together

Now that you have your pinouts wired up (and TRIPLE checked, as this is the common place where things go wrong), we're ready to quick test things. Assuming everything is turned off:
  1. Power your board
  2. Power the MKii
  3. Start AS6
  4. In AS6 select 'New Project', 'GCC Executable Project', then 'OK'
  5. Select the chip from the left and hit OK (in my case ATTiny84; you'll notice in the right hand pane once you have selected your chip, it will show which Tools are supported for it, make sure MKii is there!)
  6. Once the default *.c file is opened, select 'Tools -> Device Programming'
  7. At the top left of the the device programming window, you will see a drop down under 'Tool'; if your MKii isn't already selected, it should be in the drop down list - select it.
  8. Under 'Device' your chip should already be selected, if not, find it in the drop down
  9. 'Interface' should be ISP
  10. Click 'Apply'
  11. Here's where things can get tricky! The 'ISP Clock' needs to be lower than 1/4 of the frequency the device is operating on, or it won't work! As I am not using an external crystal, and the default internal clock speed is 1MHz for AVRs, I need to select a value below 2.5kHz and click 'Set' (adjust your settings accordingly, and note you can type in a value inside the box where it shows the value)
  12. You'll notice the lower output window should (hopefully) say it was able to set the clock and interface settings.
  13. THE MOMENT OF TRUTH! Click the 'Read' button underneath 'Device Signature'...what you should see is a hex value for the signature, and a Target Voltage next to it equal to your VCC. If you get an error it is either you have selected an incorrect clock frequency (try setting it smaller) OR you have incorrectly wired the pinouts (I did this twice, confusing the JTAG w/ the ISP w/ the ATTiny84 pinouts; and the reason I elected to wire direct from JTAG to ATTiny pins). Also make sure you have selected the correct chip! There are two options for the ATTiny84 - the '84' and the '84A'...double check the datasheet.
  14. If you got a device signature, congratulations! ISP is working! Click 'Close'

The 'Device Programming' screen - success!!!
Now that we have a connection, we can quickly test whether things are in fact working, do a quick LED blinky test:

#include <avr/io.h>
#include <avr/delay.h>
int main(void)
{
DDRB |= (1 << PB0);
PORTB |= (1 << PB0);
while(1){
_delay_ms(1000);
PORTB ^= (1 << PB0);
}
}

Save and click 'Build -> Build Solution'. If everything builds ok*, click 'Tools -> Device Programming', and click 'Apply' as you did previously. Now from the left hand pane select 'Memories' and click 'Program'.

(* yes there is a warning for F_cpu not being defined; but it will still work. NOTE: A time will come when you go to do a build and the output window states it couldn't do the build because of permissions or something of that nature. When this happens, click 'Build -> Cancel' to cancel the build. Once cancelled, click 'Build -> Clean solution', then 'Build -> Build solution' again...repeat as necessary if build fails again )

Around this time you should be seeing the LED on the MKii flashing, indicating it is programming the chip via ISP...in a couple of seconds you should see the LED on the breadboard flashing.

Give yourself a quick pat on the back for getting this far, but we're only half way.

debugWire


Fortunately, we can leave all the wires we've already hooked up for ISP, as debugWire(dW) uses one of the them (the RESET pin) for debugging purposes. In order to enter dW mode the DWEN fuse on the chip needs to be set, DO NOT attempt to manually set this fuse under 'Tools -> Device Pogramming -> Fuses'!!! I tried this at first and essentially bricked 3 chips (that I was able to 'fix', but more on that later) trying to figure this out. Instead, do the following:
  1. Beside the tab that says the name of the *.c file you are programming, should be another tab which is the same name as the *.c file, just without the extension. If you see this click it. Alternatively, there should be a little hammer icon on the menu at the top with either 'No Tool', or MKii with the device serial printed beside it..click it. (for the life of me, I can't find the regular menu item to bring up this screen!)
  2. There will be a pull down, 'Selected debugger/programmer'; select the MKii. 
  3. Select 'debugWire' from the Interface dropdown

  4. Select 'Debug -> Start Debugging', or 'Debug -> Start debugging and break'

    And they probably don't *$&%ing work!!! Errors!!!

    Ok, no worries. Select 'Debug -> Start without debugging', and you should see this screen:



    Click 'Yes', but make sure your reset line IS taken care of as mentioned before (BTW, notice how AS6 is taking care of setting the DWEN fuse for you?). Next you'll get the popup telling you to cycle the power to your board BEFORE clicking OK.


  5. Now select either of the debugging options which didn't work previously...they should now (you probably know more about AVR debugging than I, so don't ask me any questions ;))
  6. To 'leave' dW you MUST select 'Debug -> Disable debug wire and Close'

    Right about now there's a good chance you're seeing this and yelling WTF!!!???!!!



    If you didn't see this, and dW shut down nicely, don't worry...it WILL happen eventually.

    Ok, so recycle the target power (turn the board off/on) and click OK. At this point you're hoping to go back and click 'Disable debug wire and Close', but it's grayed out (again, WTF). Click 'Start Debugging' to reenter a debugging session, whereby you can now click 'Disable debug....' again.

    Did it work this time? Yes? Great, but it probably won't next time.

    It will most likely fail, numerous times, with varying errors, and the only way I've found to (eventually) get out of dW is to:

    A. attempt to reenter a debugging session (w/ or w/out breaks), and then attempt to disable dW;  though it may fail and say it couldn't enter debug mode...so

    B. select 'Start without debugging', then 'A'; but eventually it may say it couldn't set the fuses properly,  so you'll have to...

    C. try cycling the target power again, then 'B'; but again, there may be some other errors that will pop up, so...

    D. turn off/on the MKii, then 'C'

    The point here is to keep going forward into a debugging session before attempting to disable dW; this is how I was able to 'unbrick' the chips I had screwed up, by putting them in the board with the MKii interface set to debugWire, then following the steps described above. 
Ok, so if you've managed to get out of dW without throwing your MKii against the wall, you'll need to be able to get back into ISP mode again. Go back to Step #1 under 'debugWire' (this section), and set the interface to ISP, from its current debugWire setting, then continue programming as directed previously.


Final thoughts

Fortunately I'm not using this professionally, and am at such a beginning stage of learning that figuring out the warts of this environment are as interesting as what could be made with it. Though it's sad Atmel would create/release such a shoddy product.

If you have any insights/corrections/questions, fire away.

Cheers!
Warren