Go Down

Topic: What resides in top of Progam Memory? (Read 9752 times) previous topic - next topic

ninja2

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?




gardner

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).

AWOL

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.
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

CrossRoads

Or, bump up to a 1284 based board for 4x the memory!
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

liuzengqiang

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 :)
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

nickgammon


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.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

ninja2

#6
Feb 10, 2012, 11:25 am Last Edit: Feb 10, 2012, 11:26 am by ninja2 Reason: 1
32,768 bytes of program memory, more like


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 ?

ninja2

thanks all for the quality advice

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

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!

MarkT



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.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

ninja2

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

nickgammon


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.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

jwatte

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.

liuzengqiang


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.


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.
Serial LCD keypad panel,phi_prompt user interface library,SDI-12 USB Adapter

ninja2

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?

nickgammon


Quite plausible. Does arduino check fuse settings before uploading?


I doubt it because using the serial programming, it can't.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

Go Up