Programming the Mega 2560

I've read that the Mega 2560 can be programmed directly with a USB cable and IDE, but I haven't been able to load more than about 40 KBs or so.

I used my USBTinyISP to load sketches and to replace the bootloader before realizing its memory access is limited. It seems my 2560 has survived, but I don't know whether I should be able to access the full 254 KB with just the IDE software and a USB cable.

I've read Nick Gammon's procedures and I've read about the USB AVRISP mkII. I'm just looking to know what people are doing these days.

I'm using Windows 7.

Thank you for the help.

Do you mean you are using an Arduino Mega 2560 board, or are you just using an Atmega 2560 chip on your own circuit board.

If the latter I suspect a circuit diagram would help you to get advice.

...R

frankpc:
I used my USBTinyISP to load sketches and to replace the bootloader before realizing its memory access is limited. It seems my 2560 has survived, but I don’t know whether I should be able to access the full 254 KB with just the IDE software and a USB cable.

It should be no problem to upload 250K sketches from the Arduino IDE to the MEGA2560 by using the USB cable.

But to do so, the MEGA2560 needs to have the MEGA2560 bootloader installed.

If you destroyed the bootloader on your MEGA and now want it back, you can do either way:

  • use an ISP to install the bootloader on your board
  • use an Arduino “UNO as ISP” to install the bootloader on your board

Ater the bootloader is back on the board, you can use the USB cable for uploading sketches.

Robin2:
Do you mean you are using an Arduino Mega 2560 board, or are you just using an Atmega 2560 chip on your own circuit board.

If the latter I suspect a circuit diagram would help you to get advice.

...R

I have an Arduino Mega 2560 board.

jurs:
It should be no problem to upload 250K sketches from the Arduino IDE to the MEGA2560 by using the USB cable.

But to do so, the MEGA2560 needs to have the MEGA2560 bootloader installed.

If you destroyed the bootloader on your MEGA and now want it back, you can do either way:

  • use an ISP to install the bootloader on your board
  • use an Arduino "UNO as ISP" to install the bootloader on your board

Ater the bootloader is back on the board, you can use the USB cable for uploading sketches.

That is good news! So I need to purchase either an "UNO" or a dedicated programmer to install a new bootloader. It seems I might as well buy a new UNO, which is less expensive than a programmer.

Does it seem that when I replaced the bootloader on my MEGA 2560 with my USBTinyISP and IDE, that either the bootloader was put at the wrong address or perhaps the wrong bootloader was installed? I ask because my MEGA 2560 works fine except for its apparent memory limitation.

Thank you for your expertise and time.

Frank

frankpc:
Does it seem that when I replaced the bootloader on my MEGA 2560 with my USBTinyISP and IDE, that either the bootloader was put at the wrong address or perhaps the wrong bootloader was installed? I ask because my MEGA 2560 works fine except for its apparent memory limitation.

If you already have one ISP programmer that is supported by the Arduino-IDE, you normally just have to:

  • connect the ISP programmer with the Atmega ISP connector on the Arduino board
    (be careful to use the right one, many Arduino boards have two ISP connectors and only one of them is the right one)
  • select your programmer from the "Tools - Programmer" menu
  • select your board from the "Tools - Board" menu
  • select "Tools - Burn bootloader" to start burning a new bootloader to the board

frankpc:
That is good news! So I need to purchase either an "UNO" or a dedicated programmer to install a new bootloader

I'd recommend a dedicated programmer. You'll get more use out of it down the road. They are less hassle and more reliable than using an Arduino as ISP. That's a great hack, but a lousy tool.

I recommend the USBtiny.
Edit: Didn't know the 2560 wasn't compatible.

I believe you can't use a USBtiny with ATmega2560, as frankpc discovered. The best programmer in my opinion is an Arduino of some sort running ArduinoISP sketch. An ATtiny, or an ATmega8 using V-USB like a USBasp, would not be superior to an Arduino with proper USB interface and easily-customizable sketch. Something like a Nano is extremely inexpensive and can be dedicated for the purpose.

jurs:
If you already have one ISP programmer that is supported by the Arduino-IDE, you normally just have to:

  • connect the ISP programmer with the Atmega ISP connector on the Arduino board
    (be careful to use the right one, many Arduino boards have two ISP connectors and only one of them is the right one)
  • select your programmer from the "Tools - Programmer" menu
  • select your board from the "Tools - Board" menu
  • select "Tools - Burn bootloader" to start burning a new bootloader to the board

Thanks. I did use my USBTinyISP to reload the MEGA 2560's bootloader. And it seemed to work well. In fact, I've done it several times. But even though sketches load and work correctly, a sketch larger than about 40 KB causes the IDE compiler to fail with a message that the sketch is too large.

Unfortunately, I never tried to load a 'large' sketch before I used the USBTinyISP to first load a sketch and then to reload the bootloader. So I don't know whether the MEGA 2560 would have ever loaded a sketch > 40 KB. But what I've learned on this thread, if I can get the bootloader normal again, larger sketches will load.

dmjlambert:
Something like a Nano is extremely inexpensive and can be dedicated for the purpose.

I only have the one Arduino now. So I will buy a second one. I'm thinking the UNO, since they seem to be very popular and can be used to properly load the bootloader back to the MEGA 2560.

And from what I've read here, once that is done, I can use the Arduino IDE to load large sketches to the MEGA 2560.

I truly appreciate everybody's help with this.

Thanks.

dmjlambert:
Something like a Nano is extremely inexpensive and can be dedicated for the purpose.

I bought a UNO and successfully re-flashed the bootloader in the ATmega2560.

Main problem now is trying to use PROGMEM to store more than about 64 KB of sound samples.

Is there any way to use PROGMEM to store more than 64 KB ?

Thank you for the help.

Hello,

You can store more than 64kB in PROGMEM, but to read it, you have to use pgm_read_*_far, pgm_get_far_address, and the *_PF functions (like memcpy_PF for example)

See avr-libc: <avr/pgmspace.h>: Program Space Utilities

This can be tricky but if you search on this forum you will find some interesting topics about it.

guix:
Hello,

This can be tricky but if you search on this forum you will find some interesting topics about it.

Thank you!

I am utilizing 96% (245,761 bytes) of the Arduino Mega 2560’s program memory. The bootloader remains in place and I program with the standard USB cable without an external programmer.

My sketch has 10 audio clips, each in a separate ‘data.h’ file with a header similar to the following in each file:

const uint8_t data10[30623] __attribute__((section(".fini7"))) =
{ 0x80,0x80, .... }

The sketch I am using is pretty small. The data files start loading at 5,842. The 10th file starts loading at 223,596. A listing of files, their size, and address follows:

File Addr Size
data1.h 223596 19040
data2.h 199354 24242
data3.h 175112 24242
data4.h 144489 30623
data5.h 122496 21993
data6.h 96347 26149
data7.h 63739 32608
data8.h 35115 28624
data9.h 13784 21331
data10.h 5842 7942

I can get the address of each sound sample using:

#define ADDR(var)({uint32_t a; asm("ldi %A0, lo8(%1)\n ldi %B0, hi8(%1)\n ldi %C0, hlo8(%1)\n ldi %D0, hhi8(%1)": "=d"(a): "p"(&(var))); a;})

The size of each audioclip:
short int wavs[] = {0, sizeof(data1), sizeof(data2), .....}

and I am reading the data a byte at a time using:

s += pgm_read_byte_far(wav[i] + p[i]++);

I take no credit for writing any of that. I found it all online. I’ve been messing with this for a few days and it seems to work well. I am surprised I had such a hard time finding this stuff. It would seem that it could be used quite a bit. I saved all the links that cover each issue above in case anyone is interested in learning more about it.

The purpose of the sketch is for automatic score keeping for a foosball table. Each of two ‘teams’ has two ea. 7 segment LEDs driven by shift registers. The sound clips are for random applause. :slight_smile: The DAC is an R2R ladder network. Photocells used to sense the ball.

Thank you for your support!