(note: this post has been completely rewritten from the original version; it started out as a problem description, but I eventually found the answer, decided to post it here for others to find in the future, and rewrote this post to make it less confusing to people finding it via Google. Replies to it posted prior to 6/17/2018 were based on the post's original content.)
- Mega2560 or Mega2560 ADK... probably a few years old... that worked normally for a few hours/days/weeks/months/years, then suddenly began to malfunction.
- The USB serial port appears to be functioning in Windows Device Manager, but Arduino IDE is unable to upload to it, and Serial Monitor launches & shows nothing.
- Atmel Studio 7
- JTAGice mkII
- Arduino SDK (usually, installed automatically when Arduino IDE gets installed)
- You understand the basics of ISP programming & using Atmel Studio, but don't necessarily know how to apply that knowledge to solving the specific problem of, "How do I reflash the bootloader on an Arduino Mega2560 board using Atmel Studio and a JTAGice mk2, as opposed to doing it via the official Arduino IDE and some other programmer?"
1. Connect the JTAGice mkII to the Mega2560. You'll need the JTAGice mkII's adapter to convert its 2x5 IDC to a 2x3 IDC. It connects to the 2x3 header near the board's center marked "ICSP", with the ribbon cable leading into the JTAGice mk2 pointing towards the rear of the Mega2560 board (where the USB port(s) and DC power jack are located). If the ribbon cable is blocking your finger's access to the reset button, you probably connected it backwards.
Some boards might have a second 2x3 connector located near the USB port... that's NOT the one to use for reflashing the bootloader.
2. Launch Atmel Studio 7.
3. Tools -> Device Programming
4. Select "JTAGICE mkII" from the "Tool" dropdown, select "ATmega2560" from the "Device" dropdown, and "ISP" from the "Interface" dropdown, then click "Apply"
5. Continue rightward to "Device Signature". Click "Read". If you get an error, stop. You need to resolve it first. Otherwise, you'll get a hexadecimal number (mine says "0x1E9801"). Target Voltage should read something in the vicinity of 5.0-5.1v.
6. Navigate to "Memories". Click the "..." after the field for "Flash (256kb)", and find the bootloader for your board. As of June 2018, the correct bootloader for a Mega2560 or Mega2560 ADK board is named "stk500boot_v2_mega2560.hex". It's probably located in your "Arduino SDK" directory, under ./hardware/arduino/avr/bootloaders/stk500v2/.
Warning: there's probably ALSO a file in this directory named "Mega2560-prod-firmware-2011-06-29.hex". More likely than not, that's the firmware that got you in trouble to begin with. Apparently, it has a bug involving the watchdog timer that causes it to bootloop when certain things go wrong in an unfortunate sequence. Don't use it.
To flash stk500boot_v2_mega2560.hex after selecting the file, click "Program" (make sure "Erase device before programming" and "Verify flash after programming" are checked. Leave "Set programming address" (under "Advanced") un-checked.
Now, go to "Fuses". Be very, very careful what you click on this pane... one really unfortunate wrong click in the wrong place on this pane could brick your board. Confirm (and select, if necessary) the following values:
- Brown-out detection at VCC=2.7v
- HIGH.SPIEN (checked)
- HIGH.BOOTSZ: Boot Flash size = 4096 words start address $1F000
- HIGH.BOOTRST (checked)
- LOW.CKDIV8 (UN-checked)
- LOW.SUT_CKSEL: Ext. Crystal Osc. 8.0- MHz; Start-up time : 16k CK + 65ms
Other fuses should probably be un-checked:
- LOW.CKDIV8: if this were checked, your board would run at 1/8 the crystal frequency. You probably don't want to do this, because quite a few Arduino libraries make assumptions about the board's clock speed based on the board's defaults.
- LOW.CKOUT: if this were checked, your board would output one pulse per clock cycle on one of the pins (I forgot which one). It's not harmful if you aren't using that pin, but will drive you nuts for hours trying to track down the source of the problem if the fuse is set & you try using that pin for something (been there, done it, bought the T-shirt).
- HIGH.JTAGEN: same situation as LOW.CKOUT, but involves 4 pins instead of 1. Also been there, done it, and bought the T-shirt.
After setting the correct fuse values, click "Program".
That's it! Once you've reflashed the bootloader & set the fuses, your Mega2560 should work just fine, and the problem theoretically shouldn't happen again.