Cannot Upload Firmware to Arduino 2560 Mega Stack Exchange

This sketch was inspired by the Optiloader sketch written for the Arduino. Nevertheless information technology is a total rewrite, in order to accomodate the Mega2560 lath, which the original one did not handle, due to the larger address space.

Supported bootloaders

The code for the following bootloaders is incorporated in the sketch, and will be downloaded depending on which signature is detected:

  • Atmega8 (1024 bytes)
  • Atmega168 Optiboot (512 bytes)
  • Atmega328 Optiboot (for Uno etc. at sixteen MHz) (512 bytes)
  • Atmega328 (8 MHz) for Lilypad etc. (2048 bytes)
  • Atmega32U4 for Leonardo (4096 bytes)
  • Atmega1280 Optiboot (1024 bytes)
  • Atmega1284 Optiboot (1024 bytes)
  • Atmega2560 with fixes for watchdog timer problem (8192 bytes)
  • Atmega16U2 - the bootloader on the USB interface flake of the Uno
  • Atmega256RFR2 - the bootloader on the Pinoccio Scout board

You accept the selection of programming (writing) the bootloader or verifying (checking) the existing bootloader.

[EDIT] Changed on 26 November 2014 to let you to optionally omit some of the higher up bootloaders to save space in the programming board (eg. for the Arduino Micro).

Code

Source on GitHub

The latest version will be available on GitHub:

https://github.com/nickgammon/arduino_sketches

You can see from that what recent changes were.

This particular sketch is in the "Atmega_Board_Programmer" subdirectory.

Decoupling capacitors

Tip

If you are programming your own board, or using a breadboard, and so decoupling capacitors are required! Decoupling capacitors are typically ceramic capacitors, 0.1 µF (100 nF) between Vcc and Gnd (and besides AVcc and Gnd if applicative), close to the chip.

If yous don't utilise them you may find that the bit is recognized, but cannot be programmed. They are not an optional extra.

If y'all are only programming 1 Arduino board from another, and then they will already have decoupling capacitors on the board, so you don't need to worry nigh them.

Programming cable

For boards which have an ICSP header, it helps to make up a programming cable (although not essential). You lot can do this past getting a 6-pivot IDC cable, and cut the fifth wire out of circuit and soldering a pin onto it, like this:

This wire (the reset signal) gets plugged into D10 on the Arduino with the programming sketch on it, equally in the images below.

If yous don't accept such a cable, just connect together the pins as described below.

Programming a lath

Upload higher up sketch to the "programming" Uno. Connect programming cable as shown.

Wiring for Uno and like

(Ruggeduino depicted with programming cable)

(Uno depicted with "manual" programming cable)

                                  Arduino Uno      Target Uno                  D10 (SS)            Reset D11 (MOSI)          D11 D12 (MISO)          D12 D13 (SCK)           D13  Gnd                 Gnd +5V                 +5V                              

Example output for Uno

                                  Atmega bit programmer. Written by Nick Gammon. Entered programming mode OK. Signature = 0x1E 0x95 0x0F  Processor = ATmega328P Flash retentiveness size = 32768 bytes. LFuse = 0xFF  HFuse = 0xDE  EFuse = 0xFD  Lock byte = 0xCF  Bootloader accost = 0x7E00 Bootloader length = 512 bytes. Type 'G' to program the chip with the bootloader ...                              

Once you see the above, blazon "G" into the serial monitor to commence programming ...

                                  Erasing chip ... Writing bootloader ... Committing page starting at 0x7E00 Committing page starting at 0x7E80 Committing page starting at 0x7F00 Committing page starting at 0x7F80 Written. Verifying ... No errors found. Writing fuses ... LFuse = 0xFF  HFuse = 0xDE  EFuse = 0xFD  Lock byte = 0xCF  Done. Type 'C' when ready to continue with some other chip ...                              

This takes one second.

[EDIT] The more recent versions of the sketch ask this question:

                                  Blazon 'Fifty' to utilize Lilypad (8 MHz) loader, or 'U' for Uno (16 MHz) loader ...                              

Type "U" if you are using a Uno (or any board running at 16 MHz) or "50" if yous are using an eight MHz lath.

[EDIT] (23 December 2013) Sketch version ane.21 onwards automatically clears the "divide clock past 8" fuse bit before programming.

Wiring for Mega2560 and similar

                                  Arduino Uno      Target Mega2560                  D10 (SS)            Reset D11 (MOSI)          D51 D12 (MISO)          D50 D13 (SCK)           D52  Gnd                 Gnd +5V                 +5V                              

Case output for Mega2560

                                  Atmega flake developer. Written by Nick Gammon. Entered programming manner OK. Signature = 0x1E 0x98 0x01  Processor = ATmega2560 Flash memory size = 262144 bytes. LFuse = 0xFF  HFuse = 0xD8  EFuse = 0xFD  Lock byte = 0xCF  Bootloader accost = 0x3E000 Bootloader length = 8192 bytes. Type 'G' to program the flake with the bootloader ...                              

In one case you see the higher up, type "G" into the serial monitor to commence programming ...

                                  Erasing chip ... Writing bootloader ... Committing page starting at 0x3E000 Committing page starting at 0x3E100 Committing page starting at 0x3E200 Committing page starting at 0x3E300 Committing page starting at 0x3E400 Committing folio starting at 0x3E500 Committing page starting at 0x3E600 Committing page starting at 0x3E700 Committing folio starting at 0x3E800 Committing page starting at 0x3E900 Committing page starting at 0x3EA00 Committing page starting at 0x3EB00 Committing page starting at 0x3EC00 Committing page starting at 0x3ED00 Committing page starting at 0x3EE00 Committing page starting at 0x3EF00 Committing folio starting at 0x3F000 Committing page starting at 0x3F100 Committing page starting at 0x3F200 Committing page starting at 0x3F300 Committing folio starting at 0x3F400 Committing page starting at 0x3F500 Committing page starting at 0x3F600 Committing page starting at 0x3F700 Committing page starting at 0x3F800 Committing folio starting at 0x3F900 Committing page starting at 0x3FA00 Committing page starting at 0x3FB00 Committing page starting at 0x3FC00 Committing page starting at 0x3FD00 Committing page starting at 0x3FE00 Committing page starting at 0x3FF00 Written. Verifying ... No errors plant. Writing fuses ... LFuse = 0xFF  HFuse = 0xD8  EFuse = 0xFD  Lock byte = 0xCF  Done. Blazon 'C' when ready to continue with another chip ...                              

This takes three seconds.

Programming a "blank bones" board

                                  Atmega chip programmer. Written past Nick Gammon. Entered programming style OK. Signature = 0x1E 0x95 0x0F  Processor = ATmega328P Flash memory size = 32768 bytes. LFuse = 0xFF  HFuse = 0xDA  EFuse = 0xFC  Lock byte = 0xFF  Bootloader accost = 0x7E00 Bootloader length = 512 bytes. Type 'Grand' to plan the scrap with the bootloader ... Erasing flake ... Writing bootloader ... Committing page starting at 0x7E00 Committing page starting at 0x7E80 Committing page starting at 0x7F00 Committing page starting at 0x7F80 Written. Verifying ... No errors plant. Writing fuses ... LFuse = 0xFF  HFuse = 0xDE  EFuse = 0xFD  Lock byte = 0xCF  Done. Type 'C' when set up to continue with another chip ...                              

Note that the fuses were changed after the board was programmed.

Programming a RBB (really blank bones lath)

                                  Atmega scrap programmer. Written past Nick Gammon. Entered programming mode OK. Signature = 0x1E 0x95 0x0F  Processor = ATmega328P Flash memory size = 32768 bytes. LFuse = 0xFF  HFuse = 0xDA  EFuse = 0xFD  Lock byte = 0xCF  Bootloader address = 0x7E00 Bootloader length = 512 bytes. Type 'Thou' to plan the fleck with the bootloader ... Erasing chip ... Writing bootloader ... Committing page starting at 0x7E00 Committing folio starting at 0x7E80 Committing folio starting at 0x7F00 Committing page starting at 0x7F80 Written. Verifying ... No errors found. Writing fuses ... LFuse = 0xFF  HFuse = 0xDE  EFuse = 0xFD  Lock byte = 0xCF  Done. Type 'C' when fix to go along with another fleck ...                              

Wiring for Leonardo

The Leonardo does non expose the ICSP pins on the board "header" pins and so you either take to use the IDC programming cable described earlier, or connect the ICSP pins of both boards together using female-to-female hookup wire, similar this:

Yous connect pin 1 of the ICSP header (the one with the dot) on one lath to pivot 1 on the other board. And connect pin 2 of each board together, and so on, omitting pin v which is Reset.

Reset on the Leonardo is connected to Pivot 10 of the programming lath, equally shown.

Close-upwards of the wiring:

Pay attention to the pin numbers if you have the boards stop-to-end considering pivot i on i board is not adjacent to pivot 1 on the other board (because they are turned 180°).

You can use one Leonardo to program another Leonardo (or a Uno) providing you use this wiring method - utilise the ICSP headers equally shown and connect them as shown.

Top-down view of the ICSP header. Notice the dot (which is printed on the board next to pin one).

Adding other bootloaders

In order to convert the .hex bootloader files on disk for use with this sketch I ran this Lua script:

                                  --[[   Do MD5 sumcheck of Arduino bootloader .hex file   Nick Gammon   v May 2012   Amended 10 October 2014, to get PROGMEM line right for IDE 1.v.8    To use:    1. Download MUSHclient from: http://www.gammon.com.au/downloads/dlmushclient.htm      MUSHclient is costless and open source.      Source code at: https://github.com/nickgammon/mushclient      You are not required to enter whatsoever personal data to obtain the download.    2. Install MUSHclient (requires Windows, or Wine on Linx and OS/X)    iii. Run MUSHclient (click past all the introductory screens)    4. Make a "new world" in MUSHclient (File menu -> New Globe)    5. Fill in the fields:       World proper name:     Arduino  (doesn't really matter what it is)      TCP/IP address: 0.0.0.0      Port:           4000     (doesn't really thing what it is)    6. A window will open with diverse messages about the version number, etc.    7. Open an Firsthand scripting window (Game carte -> Immediate).      Pressing Ctrl+I is a shortcut for this.    8. Copy this script and paste it into the Immediate window (RH-click to exercise this)    9. Click the Run button.   ten. Navigate to the bootloader .hex file that y'all desire to process.   xi. Click "Open".   12. Bold no errors, close the Firsthand window.   13. In the output window y'all should run across a whole lot of hex codes.   14. Curlicue back to the line of hyphens, and re-create from the line below that, eg.      ------------------------------------------------------------      // File =  Arduino-COMBINED-dfu-usbserial-atmega16u2-Uno-Rev3.hex      // Loader kickoff: 3000, length: 4096      // MD5 sum = D8 8C seventy 6D FE 1F DC 38 82 1E CE AE 23 B2 E6 E7       const byte Arduino_COMBINED_dfu_usbserial_atmega16u2_Uno_Rev3_hex [] PROGMEM = {       0x4B, 0xC0, 0x00, 0x00, 0x64, 0xC0, 0x00, 0x00, 0x62, 0xC0, 0x00, 0x00, 0x60, 0xC0, 0x00, 0x00,       ...      }; // end of <any your file name was>     That stuff is your bootloader every bit an assortment of bytes.  --]]   crave "getlines"  loader = nil adder = 0  -- given a kickoff address, deduce where the bootloader ends end_addresses = {   [0x1000] = 0x2000,   [0x1C00] = 0x2000,   [0x1D00] = 0x2000,   [0x1E00] = 0x2000,   [0x3000] = 0x4000,   [0x3800] = 0x4000,   [0x3E00] = 0x4000,   [0x7000] = 0x8000,   [0x7800] = 0x8000,   [0x7E00] = 0x8000,   [0xF800] = 0x10000,   [0x1F000] = 0x20000,   [0x1FC00] = 0x20000,   [0x3E000] = 0x40000,   }  part process (size, address, rectype, data)    size = tonumber (size, 16)   address = tonumber (accost, 16) + adder   rectype = tonumber (rectype)   local binarydata = utils.fromhex (data)    if rectype == 2 and so  -- Extended Segment Accost Tape     adder = tonumber (data, 16) * 16  -- high order address byte       elseif rectype == 0 then -- data tape     if loader == nil so       start_address = address       end_address = end_addresses [address]       if end_address == zilch then         ColourNote ("blood-red", "", "Don't know stop accost for " .. bit.tostring (address, 16))         ColourNote ("blood-red", "", "Please add to table: end_addresses")         error "Cannot go on"       end -- if terminate address not found        -- work out loader length       length = end_address - address       -- pre-fill up with 0xFF in case not every byte supplied       loader = string.rep ("\255", length)       impress (string.format ("// Loader start: %X, length: %i", accost, length))     finish -- no loader yet      -- insert data over where the 0xFF was     if address >= start_address and (address + size) <= end_address then       loader = loader:sub (1, address - start_address) ..                 binarydata ..                 loader:sub (address - start_address + size + 1, length)     else       ColourNote ("ruby-red", "", "Accost " .. flake.tostring (address, xvi) .. " out of expected range.")     end  -- if in range   end -- if  stop -- function process  print (string.rep ("-", lx))  -- go bootloader file filename = utils.filepicker ("Choose a bootloader", nil, "hex", { hex = "Hex files" })  -- none chosen, give up if not filename and then return end  -- testify file local fn = string.match (filename, "\\([^\\]+)$") impress ("// File = ", fn)  -- process each line for line in io.lines (filename) exercise   size, address, rectype, information = string.match (line, "^:(%x%10)(%x%ten%ten%x)(%ten%x)(%x+)%s*$")   if size and so      process (size, accost, rectype, information:sub (one, -iii))   else     ColourNote ("red", "", "Discarded line: " .. line)   end -- if end -- for loop  --impress (utils.tohex (loader))  -- sumcheck it Tell ("// MD5 sum = ") md5sum = utils.tohex (utils.md5 (loader)) print ((cord.gsub (md5sum, "(%x%x)", "%1 ")))  print ""  --     evidence bootloader in hex  -- convert into C assortment print (string.format ("const byte %south [] PROGMEM = {",        cord.gsub (fn, "[^%a%d]", "_"))) for i = i, #loader do   Tell (cord.format ("0x%02X, ", loader:sub (i, i):byte ()))   if (i - 1) % 16 == 15 then print "" terminate terminate -- of for each byte print (string.format ("}; // end of %southward", string.gsub (fn, "[^%a%d]", "_")))                              

Because it uses a few special features (like a file picker, MD5 sum calculator etc.) you need to run this from within MUSHclient ... a MUD game client bachelor for free download from this site.

Just download MUSHclient (run across the downloads page) and install. Then brand a "dummy" globe file (File menu -> New World) and type in "Arduino" as the world name and "0.0.0.0" equally the TCP/IP address, every bit shown:

Close the world configuration, and then type Ctrl+I to open the Immediate programming window (Game carte -> Immediate) and paste the above script into it, then striking Run, equally shown:

A file-picker will open, yous can navigate to the bootloader .hex file that you want to process, and and so (all beingness well) the appropriate hex codes will be shown in the main window. Close the Immediate window, and then but copy and paste into your bootloader sketch (make a split up xxxx.h tab similar I did for the other bootloaders).

Y'all will then need to fill in the appropriate figures into the signatures table, such every bit the bootloader outset accost, size, programming page size (encounter the datasheet) and the right fuse and lock bits. The fuse bytes will probably be the ones mentioned in the boards.txt file in the Arduino surroundings, for the scrap in question.

The higher up script is now part of the download from GitHub in the file named: bootloader_converter.lua

Checking what bootloader is installed

The thread below describes a sketch that does a MD5 sumcheck of your bootloader, so you lot can see what bootloader you lot have installed:

http://www.gammon.com.au/forum/?id=11633

Alternate clock source

As an alternative to a crystal or resonator, the sketch also outputs an 8 MHz clock on pin D9 of the programming board (using Timer 1). Thus y'all just need to connect D9 on the programming board to XTAL1 of the target lath and this will provide a clock signal, enabling this sketch to run.

Atmega1280

Updated on 9 September 2013 to apply the Optiboot loader for the Atmega1280. This is smaller and avoids certain problems like the disability to upload a sketch with "!!!" in it.

Even so please note that if you burn a bootloader for the 1280 chip yous need to add this to the terminate of the boards.txt in the Arduino distribution folder:

                                  ##############################################################  megao.proper name=Arduino Mega1280                  Optiboot                  megao.upload.protocol=arduino megao.upload.maximum_size=130048                  megao.upload.speed=115200                  megao.bootloader.low_fuses=0xff megao.bootloader.high_fuses=0xdc                  megao.bootloader.extended_fuses=0xf5 megao.bootloader.path=optiboot                  megao.bootloader.file=optiboot_atmega1280.hex                  megao.bootloader.unlock_bits=0x3F megao.bootloader.lock_bits=0x0F megao.build.mcu=atmega1280 megao.build.f_cpu=16000000L megao.build.core=arduino megao.build.variant=mega                              

In my case the file was in:

                                  /Applications/Arduino_1.0.v.app/Contents/Resources/Coffee/hardware/arduino/boards.txt                              

Withal y'all would need to find where it is in your distribution.


- Nick Gammon

www.gammon.com.au, www.mushclient.com

cortinatheausted.blogspot.com

Source: https://www.gammon.com.au/bootloader

0 Response to "Cannot Upload Firmware to Arduino 2560 Mega Stack Exchange"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel