Adding external memory to Atmega128

Atmega128 is equipped with internal 4Kbytes of SRAM memory. Is it enough? Well it depends on what project it’s gonna hold. If your project must deal with loads of data or run larger RTOS code you will definitely run out of RAM pretty soon. Atmega128 microcontroller has an external memory interface built in, which allows to expand RAM up to 64 Kbytes. With that you could do much more, isn’t it? To test things out I used Piconomic Atmega128 development board. Which has a XMEM interface header brought out. All we need is to make XMEM expansion board with some SRAM memory.

I’ve chosen standard 8Kx8 (8Kbytes) memory chip from Aliance memory Inc. I could use 64Kx8 but this is what I had at the moment. To drive memory chip I’ve used 74HC573 non inverting latch. As you may know latch is used for pins that share same pins for address and data buses. In order to access SRAM contents we need to select 16 bit address that points to 8 bit data cell in chip. As we are using 8Kx8 memory chip we are going to use only 13Address lines. Microcontroller has dedicated pins for that:

AD[0..7] (PORTA pins) – multiplexed data and low order address pins;

A[8..15] (PORTC pins) – high order address pins;

ALE (PG2 pin) – address latch enable;

RD (PG1 pin) – inverted read strobe;

WR(PG0 pin) – inverted write strobe.

Microcontroller board runs at 7.3728MHz so standard 74HC series latch timing characteristics are fine – no special delays are needed. Here is the schematic;

Once board is ready it is time to test it attached to AT128mega board.:

To test if external memory board works we are going to write a simple routine that tests if microcontroller can allocate memory heaps, write to and read from then. First of all we have to set up AVRStudio project. If your are going to use automated make file generation, you need to add external RAM options, so compiler would know what address range to allocate for RAM. To do so open AVRStudio Configuration options and in custom options add line to Linker Options:

-Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x8030ff

This shows linker start and end address of external memory. This also indicates that memory is used for variables (.data/.bss) and heap (malloc()).

This way we end up with having internal SRAM address range 0x800000 – 0x8010FF and external 0x801100 – 0x8030FF as we are using 8Kbytes of external memory chip so we add 0x2000 to end of internal address range. Totally we have 12Kbytes of SRAM where internal now is used only for STACK and external for ,data (static data), ,bss (global or static variables) sections and heap.

There are many ways to test RAM. We are going to use malloc() function which simply allocates some amount of memory and returns its start address to pointer. Once we have configured compiler options we can start writing code. Before we can use external memory we must enable it by setting SRE bit in MCUCR register. This action turns XMEM dedicated microcontroller pins in to XMEM interface. Also we can release PC5, PC6 and PC7 unused address pins – they may be useful for other tasks.

void XMEM_init(void)
{
// External memory interface enable
MCUCR |= (1<<SRE);
XMCRA = 0;
//PC7..PC5 released pins
XMCRB |= (1<<XMM1)|(1<<XMM0);
}

After initialization we can start writing program. We are going to allocate 255 bits in heap memory allocated in external ram by writing simple code:

#define BUFFER_SIZE 255
uint8_t *mem;
mem = malloc(BUFFER_SIZE);

After allocated heap memory we can fill it with some data and read back to see if everything goes right;

uint8_t index;
// Fill memory incrementing values
for(index = 0; index < BUFFER_SIZE; index++)
{
	mem[index] = data++;
}
// Display memory block
for(index = 0; index < BUFFER_SIZE; index++)
{
	PRINTF("%02X ",mem[index]);
	if((index&0x0F) == 0x0F)
	{
		PRINTF("\n");
	}
}

in the terminal screen we get:

In this case heap memory is start is at 0x1221 location. Meaning that .data and .bss sections are between address space 0x1100 to 0x121E. You can check these locations in .map files. In my case .data section starts at 0x1100 and.bss section-starts at 0x1114. And also I find that memory for heaps is allocated at 0x111e. So our *mem pointer points to heap memory starting with 0x121E.

Lets test another situation when external memory is used only for heaps. For this wee need to change linker options like this:

-Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x8030ff

Then we expect out memories to be allocated this way:

After compilation we can see the results in terminal:

We can see that out heap is placed at the start of external memory. First two bytes are used to hold size of allocated area. Lets look at .map file.

.data 0x00800100

.bss 0x00800114

__heap_start = 0x801100

__heap_end = 0x8030ff

We can see that .data sector is allocated at beginning of internal RAM (0x0100), then goes .bss sector, while heap for heap is dedicated whole external RAM.

Which memory allocation model use – it depends on your application. If you are going to use big data buffers like for holding graphical information ready to send to displays – definitely allocate external RAM for heaps only. In other case if you are going to use RTOS with lots of tasks, then probably it is best to leave more internal RAM for STACK and move static data to external RAM.

This is only a surface of what we’ve “scratched”. There are lots of more things you can do with memory allocations and sectors. There are some great resources starting with AVR Libc. Good luck. Eagle and source files.

14 Comments:

  1. Pingback: Electronics-Lab.com Blog » Blog Archive » Adding external memory to Atmega128

  2. Pingback: External RAM for an ATmega128 - Hack a Day

  3. Just when I thought no one used external memory, someone comes up with a post about it.

    Lovely. You could also, show a bit more about connecting other peripherals with external addressing. Ok, it takes a bit more external logic, but not having to mess around with serial comms is bliss. 🙂

    Good post. Keep it coming.

  4. Pingback: External RAM for an ATmega128 « Baudot-Code Blogging

  5. Congratulations! This is exactly the extension PCB that I had in mind when I added the JP4 header for the Piconomic AB111-4 board 🙂

    FYI, I published a short tutorial on exactly this topic:
    http://www.piconomic.co.za/fwlib/group___a_v_r___t_u_t13___x_m_e_m.html

    Please update your links in previous blogs/articles 😉
    http://www.piconomic.co.za/fwlib/index.html

    Also, the development board CAD files are included in the “piconomic_fwlib_0.3.0_20100813.zip” that can be downloaded from:
    http://code.google.com/p/piconomic/downloads/list

    Happy playing!
    Pieter

  6. Pingback: External RAM for an ATmega128 « Hacking and exploit news

  7. Although the article suggests the limit is 64kByte, you can use other spare port pins as the high-order address bits and get even more memory space. You have to manage the paging yourself however.

  8. 64Kbyte limit is only for XMEM interface. With manual paging it can be expanded to much more.

  9. How you you move the #$%& ads off the web page so you can read the text?

  10. Thankyou
    This is very helpful
    Sameer

  11. Hi
    This is very nice Blog i like it very much

    Nice Work

  12. Thankyou
    This is very useful for me.

  13. i have tested the code , but the data displayed on terminal have an error every 64 byte.

    the first 64 byte have a error in the data, then the next 64 byte data didn’t error.

    Any suggestion please regarding my problem ?

    Thank You

Leave a Reply

Your email address will not be published. Required fields are marked *