What resides in top of Progam Memory?

My sketch is around 28,900 bytes on an Arduino Uno compatible (USBdroid) which has only 32,256 bytes of program memory space. If my sketch is below about 28,600 it compiles and works fine but if I add more code and it gets above this I get an error message "Problem uploading to board" plus

avrdude: stk500_paged_write(): (a) protocol error, expect=0x14, resp=0x64 avrdude: stk500_cmd(): programmer is out of sync

Seems the top of program memory is used by something else, the bootloader maybe?

Is there a technique/workaround to dodge this issue?

Yes, the bootloader is there. I am uncertain exactly how much flash is blocked off for the bootloader, and it would vary a bit for certain compatibles that don't use the official Arduino one, but on an UNO it is 1/2 K. At ~30K your sketch is really at the limit of what you can run on a 328. I believe your choices are to:

(1) use a different board, like a mega, that has more room, or (2) optimize your program to fit in less space.

I recommend having a bash at (2).

That's correct - the bootloader lives at the top of program memory. If you're really short, you could program the processor directly using an ISP, or you could factor your code further.

Or, bump up to a 1284 based board for 4x the memory!

Some of blog posts about optimizing code:

http://liudr.wordpress.com/2011/02/04/how-to-optimize-your-arduino-memory-usage/

http://liudr.wordpress.com/2011/03/07/optimizing-your-program/

If you are using a lot of strings for serial comm, cut them down, way down :)

ninja2: My sketch is around 28,900 bytes on an Arduino Uno compatible (USBdroid) which has only 32,256 bytes of program memory space.

32,768 bytes of program memory, more like.

You could install the Optiboot loader which only uses 512 bytes.

You could not use a bootloader at all, but program using the SPI interface, and thus get the use of all 32768 bytes.

Probably best to do what others suggested and cut down on what the program is doing.

[quote author=Nick Gammon link=topic=91176.msg684810#msg684810 date=1328828083]32,768 bytes of program memory, more like[/quote]

I know where you got that number Nick (2^16)/2 but for some reason it actually says this (using cut & paste):

"Binary sketch size: 26814 bytes (of a 32256 byte maximum)".

Interesting that 32,768 - 32,256 = 512 bytes. Maybe that's a small boot loader after all?

BTW it's working now because I've whittled about 2.5Kbytes of code out. It seems usable limit is about 28,600. Curious: what's between here and 32,256 ?

thanks all for the quality advice

gardner: I recommend having a bash at (2).

yep, and that's my intention. I have a few Arduinos of which one is a 1280 MEGA, but this has been a great project for learning the limits. First I ran out of SRAM, so I moved all my fixed text strings using PROGMEM. Now I've hit the free memory limit time to prune and refine

liudr: Some of blog posts about optimizing code: http://liudr.wordpress.com/2011/02/04/how-to-optimize-your-arduino-memory-usage/ http://liudr.wordpress.com/2011/03/07/optimizing-your-program/ If you are using a lot of strings for serial comm, cut them down, way down :)

great thanks, looks like a wet weekend ahead so good time to ll go read those!

ninja2: BTW it's working now because I've whittled about 2.5Kbytes of code out. It seems usable limit is about 28,600. Curious: what's between here and 32,256 ?

Which bootloader do you have - optiboot (the new Uno one) is much smaller than previous bootloaders and should just take up 1/2K - it looks you have a chip programmed with a 4k bootloader at a guess.

how do I find out what bootloader I have?

I will go onto the forum at the freetronics site and find out: http://www.freetronics.com/products/usbdroid

ninja2: If my sketch is below about 28,600 it compiles and works fine but if I add more code and it gets above this I get an error message "Problem uploading to board" plus

avrdude: stk500_paged_write(): (a) protocol error, expect=0x14, resp=0x64 avrdude: stk500_cmd(): programmer is out of sync

Sounds like you might have a bootloader bug then. Or maybe the fuses.

Here's a possibility ...

You have "told" the programmer (via the boards.txt files) that you have the new 512 byte bootloader (hence the message about 32,256 of bytes of memory available). However you actually have a 2048 byte bootloader, and the fuses on the board are set appropriately. So when you try to upload, the bootloader tries to overwrite itself, fails, and generates that message.

You can use the IDE to upload a new bootloader, that might help. I think you need an SPI programmer to do that (the bootloader can hardly overwite itself) so you would need a USB programmer (like USBtinyISP) or use a second Arduino as an ISP.

Note that, if you have an SPI programmer / ICSP, then you can do away with the bootloader entirely, and use all of the (2 ^ 15) 32768 bytes if program memory. The trade-off is that you have to program with the programmer each time, instead of a bootloader, which I've found to be slightly slower.

[quote author=Nick Gammon link=topic=91176.msg685592#msg685592 date=1328895229] Here's a possibility ...

You have "told" the programmer (via the boards.txt files) that you have the new 512 byte bootloader (hence the message about 32,256 of bytes of memory available). However you actually have a 2048 byte bootloader, and the fuses on the board are set appropriately. So when you try to upload, the bootloader tries to overwrite itself, fails, and generates that message. [/quote]

Quite plausible. Does arduino check fuse settings before uploading? I would suggest the OP go get an ATMEGA328P with optiboot bootloader. Works great and really fast.

I haven't changed boards.txt myself (in fact I just learnt it existed!) so I assume it is per standard Arduino 1.0 distribution. It includes following entries for Uno:

... ... uno.upload.maximum_size=32256 ... uno.bootloader.low_fuses=0xff uno.bootloader.high_fuses=0xde uno.bootloader.extended_fuses=0x05 ... uno.bootloader.path=optiboot uno.bootloader.file=optiboot_atmega328.hex

Since this is a USBdroid Freetronics may done something different here, and I'm checking that on their forum, although the above suggests 512 byte optiboot is being loaded (unless some other USBdroid file overrides?).

I would appreciate an explanation of what the "fuses" do / and how to interpret the three fuses entries above?

liudr: Quite plausible. Does arduino check fuse settings before uploading?

I doubt it because using the serial programming, it can't.

ninja2: I would appreciate an explanation of what the "fuses" do / and how to interpret the three fuses entries above?

http://www.engbedded.com/fusecalc

Select the Atmega328 from the list and enter your fuse settings in the hex boxes down the page.

uno.upload.maximum_size=32256

As I guessed, it thinks you have 32256 bytes of memory available. Bear in mind these are not necessarily your fuses.

To find your fuses you need to use an SPI programmer.

ninja2: I would appreciate an explanation of what the "fuses" do / and how to interpret the three fuses entries above?

Briefly they are "control flags". The are not in program memory, not in RAM, not in EEPROM. They are 3 bytes of separate "memory" in the processor chip that it consults for things like how to boot, what clock to use, whether to allow programming, etc.

The old version of optiboot shipped on the original UNOs (3.3?) had a bug that made the space between 28k and 32256 unusable. This is fixed on newer versions of optiboot. (4.4).

thanks westfw, that sounds like a good reason for my problem.

so, how to load a later version of optiboot?

That depends on what you have available. Do you have more than one Arduino, or more than one ATMega chip? If so you can use this schematic

http://arduino.cc/en/Tutorial/ArduinoISP

plus this program

https://github.com/WestfW/OptiLoader