This is my first question here, sorry if I miss something.
I’m using an Atmega2560 as main processor for my project but I’m running out of ram so I have connected an external 64 KB (64 x 8) SRAM to it. I searched around and found couple of projects and libraries that use the external sram:
http://andybrown.me.uk/wk/2011/08/28/512kb-sram-expansion-for-the-arduino-mega-design/
https://hackaday.io/project/21561-arduino-mega-2560-32kb-ram-shield
But all of these use the external SRAM as heap memory.
I would like to use the external memory as data, bss, and heap memory leaving the entire SRAM for stack.
I found this in the avr-libc manual: https://www.nongnu.org/avr-libc/user-manual/malloc.html
Using the information from the manual, I changed the makefile for Arduino Mega bootloader here: C:\Program Files (x86)\Arduino\hardware\arduino\avr\bootloaders\stk500v2
Link to original makefile: https://github.com/arduino/ArduinoCore-avr/blob/master/bootloaders/stk500v2/Makefile
I changed “EXTMEMOPTS =“ to this:
EXTMEMOPTS = -Wl,--section-start,.data=0x802200,--defsym=__heap_end=0x80ffff
0x800000 is the offset for Harvard architecture, 0x802200 is the first address for the external memory.
I’m recompiling the bootloader with the new make file but I get the following error:
I believe it’s complaining because the address is outside the internal SRAM, I couldn’t find any way to fix it.
I’m not sure what’s wrong, what am I missing ? Also if this gets solved Is it okay to initialise the external memory hardware (by setting XMCRA and XMCRB registers) in bootloader’s main function? If not where should I do it ?
Update: Here is the linker script: https://codeshare.io/aJom3K
Update 2: The first problem is solved now; I was editing the wrong ld file and after changing the correct file to the following It worked:
DATA_REGION_ORIGIN = 0x802200;
The next problem is where to initialize the hardware for external SRAM ? I'm currently doing this in __jumpMain function of the bootloader which also sets the stack pointer register. Is this fine or should I do it somewhere else ?
bootloader: https://codeshare.io/5zNr1b
Update 3: As mentioned by @Brian Drummond in the partial answer, I changed the linker args to this:
EXTMEMOPTS = -Wl,--section-start,.data=0x802200,--defsym=__heap_end=0x80ffff,--defsym=DATA_REGION_ORIGIN=0x802200
and reverted the changes to the build script to avoid problems in the future.
For anyone doing something similar: You also need to add args for changing the DATA_REGION_LENGTH to the new length otherwise, it will only use the old length which in my case is: 0xfe00.
Update 4: After some testing, apparently the MC is not using the external SRAM; after programming it with the new script I can disconnect the SRAM and it will continue to work fine. Also I checked the data and address lines to the SRAM and nothing is being written or read.
So I'm back at the beginning, Any ideas what's wrong ?