Position: Index > Unclassified >

Adding external memory to Atmega128

2017-12-01 03:09  
Declaration:We aim to transmit more information by carrying articles . We will delete it soon, if we are involved in the problems of article content ,copyright or other problems.

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 toLinker 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 range0×800000 – 0x8010FFand external0×801100 – 0x8030FFas we are using 8Kbytes of external memory chip so we add 0×2000 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 usemalloc()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("X ",mem[index]);
	if((index&0x0F) == 0x0F)
	{
		PRINTF("\n");
	}
}

in the terminal screen we get:

In this case heap memory is start is at 0×1221 location. Meaning that .data and .bss sections are between address space 0×1100 to 0x121E. You can check these locations in .map files. In my case .data section starts at 0×1100 and.bss section-starts at 0×1114. 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 (0×0100), 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.


Reprinted Url Of This Article:
http://www.scienceprog.com/adding-external-memory-to-atmega128/