AHBSRAM1 and __attribute__

25 May 2011

Given the following code:-

#include "mbed.h"

Serial pc(USBTX, USBRX);

DigitalOut myled(LED1);

char bufA[6000] __attribute__((section("AHBSRAM1")));
char bufB[6000] __attribute__((section("AHBSRAM1")));

int main() {
    
    pc.baud(115200);
    
    pc.printf("BufA @ %p\n", bufA);
    pc.printf("BufB @ %p\n", bufB);
    
    while(1) {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
    }
}

Output fropm above wrote:

BufA @ 20080000

BufB @ 20081770

Now, all well and good. This is what I expected. However, if I adjust the size of the arrays so that more memory is requested in the given section than is available I noticed the following:-

#include "mbed.h"

Serial pc(USBTX, USBRX);

DigitalOut myled(LED1);

char bufA[10000] __attribute__((section("AHBSRAM1")));
char bufB[10000] __attribute__((section("AHBSRAM1")));

int main() {
    
    pc.baud(115200);
    
    pc.printf("BufA @ %p\n", bufA);
    pc.printf("BufB @ %p\n", bufB);
    
    while(1) {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
    }
}

Output fropm above wrote:

BufA @ 100000f0

BufB @ 10002800

The first thing I notice is the compiler neither produces and error on compile or at least a warning that the requested memory allocation has failed to meet the users expectation.

The second thing I notice is that, having determined the request failed, it just sticks in in main memory (again, no warning from the compiler that it did this).

The third thing I notice is that the two buffers together are too big to go into the ram bank. However, it could have got one in and placed the other in main memory. But having overflowed the ram bank it gives up and places everything into main memory leaving ram bank1 totally unused.

It would be great if the compiler

  • Produced an error or at a minimum a warning
  • Having the linker's map file available to look at after a build would be very useful

Is there some method to get what I want now with say an #pragma to force the compiler/linker to respect my section attribute requests and tell me if I failed to get it right rather than stuff the extra into main ram?

25 May 2011

Hi Andy

Assuming you don't actually want to run your program, just debug the memory allocation, the only way I found to generate a compiler warning in these circumstances was to "reserve" the main RAM first by filling with a buffer to its limit.

That is, compile your program and use the compiler build statistics to get the main RAM used (eg. 2.6K). Then create a buffer in main RAM of 32K less 2.6K.

Any other allocations into RAM banks cannot then silently be placed into main RAM (because it is already full).

Considering how critical memory is, we definitely need more information and control over how it is allocated by the compiler.

Regards
Daniel