How to create a variable at an absolute address in DRAM ?

Serdar
Posts: 18
Joined: Fri Sep 02, 2022 7:33 pm

How to create a variable at an absolute address in DRAM ?

Postby Serdar » Sat Jan 28, 2023 12:08 pm

Hi, I need to create variable in absolute addresses in DRAM.
I used a dirty method to do this.

sections.ld

Code: Select all

config :
   {
     . = ALIGN(4);
     KEEP (*(.config))
   } > CONFIG
   tracks :
   {
    . = ALIGN(4);
    KEEP (*(.tracks))
   } > TRACKS
memory.ld

Code: Select all

CONFIG (RW) : org = 0x3FCC0000, len = 0x4000
TRACKS (RW) : org = 0x3FCC4000, len = 0x4000
In C code

Code: Select all

static const volatile LoggerConfig g_savedLoggerConfig  __attribute__((section(".config\n\t#")));
Image

By the way, my project is a custom Cmake project.(https://docs.espressif.com/projects/esp ... -%EF%83%81)
When I change the CMakeLists file, the memory.ld and section.ld file changes.

And I'm not sure I've literally allocated these spaces.
Any task should not automatically generate data in these fields.
I said this because when I changed the data in the area that I thought I had allocate, the wifi task was broken.
Issue fixed when i allocate data in another area of DRAM.
And it was a weird situation for me :)

I'm asking my question now:
How can I cleanly create variable in a specific field in DRAM ?

Serdar
Posts: 18
Joined: Fri Sep 02, 2022 7:33 pm

Re: How to create a variable at an absolute address in DRAM ?

Postby Serdar » Tue Feb 14, 2023 6:15 pm

Hi again, i really need this answer.

banjoluck
Posts: 25
Joined: Fri Jul 29, 2022 5:11 pm

Re: How to create a variable at an absolute address in DRAM ?

Postby banjoluck » Tue Feb 14, 2023 7:50 pm

I don't really think there is a good way to do what you want without a linker script trick. The folks at espressif did this for the base address pointers into the register files. Some of the MCU compilers have a directive to do this at the source code level. There is not an equivalent construct in ISO C or C++.

banjoluck
Posts: 25
Joined: Fri Jul 29, 2022 5:11 pm

Re: How to create a variable at an absolute address in DRAM ?

Postby banjoluck » Tue Feb 14, 2023 7:55 pm

...aside from an explicit cast:

template <typename T_, uint32_t address> constexpr T_& xref () {
return *reinterpret_cast<T_*>(address); }

uint32_t& v = *xref<uint32_t*>(0xXXXXXXXX);

MicroController
Posts: 1734
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: How to create a variable at an absolute address in DRAM ?

Postby MicroController » Thu Feb 16, 2023 6:07 pm

If your compiler has no language extension for that, there's no way to do it without support from the linker: The linker needs to know that that bit of memory is allocated/used by something, else it may just allocate other stuff right there.

You can have custom pieces added to the generated linker instructions via "linker fragment files", see https://docs.espressif.com/projects/esp ... ation.html

Serdar
Posts: 18
Joined: Fri Sep 02, 2022 7:33 pm

Re: How to create a variable at an absolute address in DRAM ?

Postby Serdar » Mon Feb 20, 2023 10:31 pm

Hi, thanks for your replies.

I created linker_fragment_file like this.

Code: Select all

[sections:CONFIG_ASL ]
entries:
    .CONFIG_ASL+

[sections:TRACKS ]
entries:
    .TRACKS+

[scheme:auto_default]
entries:
    CONFIG_ASL  -> dram0_data
    TRACKS -> dram0_data

[mapping:CONFIG_ASL ]
archive: *
entries:
    * (auto_default);
    CONFIG_ASL  -> dram0_data KEEP() ALIGN(4, pre, post) SURROUND(CONFIG_ASL )

[mapping:TRACKS]
archive: *
entries:
    * (auto_default);
    TRACKS -> dram0_data KEEP() ALIGN(4, pre, post) SURROUND(TRACKS)
Image

I am able to create the variable from .c file like this

Code: Select all

int g_tracks[24] __attribute__((section("TRACKS")));
What I'm wondering is, can I create a variable with specific size and address as i wish by linker_fragment_file?

ESP_Sprite
Posts: 9764
Joined: Thu Nov 26, 2015 4:08 am

Re: How to create a variable at an absolute address in DRAM ?

Postby ESP_Sprite » Tue Feb 21, 2023 12:30 am

Just wondering: is it actually a variable at an absolute address you want, or do you simply want a variable that does not get cleared after a reset so your firmware can simply 'pick up' the value again after a reboot? Because if it's the second, you're probably better off declaring it static with a __NOINIT_ATTR flag (ref) rather than messing around with ld scripts.

Serdar
Posts: 18
Joined: Fri Sep 02, 2022 7:33 pm

Re: How to create a variable at an absolute address in DRAM ?

Postby Serdar » Tue Feb 21, 2023 9:31 pm

I need an absolute address.
If that's not possible, I'll have to access the variable addresses from other .c pages after the code runs.

MicroController
Posts: 1734
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: How to create a variable at an absolute address in DRAM ?

Postby MicroController » Tue Feb 21, 2023 11:56 pm

Can you elaborate why you'd want the variable to reside at a specific address in RAM?
- Can you live with the variable residing somewhere in RAM and sharing the single variable across your code via "extern" declarations? (https://stackoverflow.com/questions/143 ... urce-files)

Serdar
Posts: 18
Joined: Fri Sep 02, 2022 7:33 pm

Re: How to create a variable at an absolute address in DRAM ?

Postby Serdar » Wed Feb 22, 2023 3:11 am

I have an architecture like this.
/////////////////////////////
any_page.c
static const volatile Tracks g_tracks __attribute__((section(".tracks\n\t#")));
..... there are other variables in different pages defined like this ...

memory.c

Code: Select all

/* Base @ of Sector 0, 16 Kbytes */
#define ADDR_FLASH_SECTOR_0 ((uint32_t)0x3FCC0000) 
/* Base @ of Sector 1, 16 Kbytes */
#define ADDR_FLASH_SECTOR_1 ((uint32_t)0x3FCC4000)
/* Base @ of Sector 2, 16 Kbytes */
#define ADDR_FLASH_SECTOR_2 ((uint32_t)0x3FCC8000)
/* Base @ of Sector 3, 16 Kbytes */
#define ADDR_FLASH_SECTOR_3 ((uint32_t)0x3FCCC000)
/* Base @ of Sector 4, 64 Kbytes */
#define ADDR_FLASH_SECTOR_4 ((uint32_t)0x3FCE0000)


static uint32_t selectFlashSector(const void *address)
{
        uint32_t addr = (uint32_t) address;

        if(addr == ADDR_FLASH_SECTOR_0)
                return 0;
        else if(addr == ADDR_FLASH_SECTOR_1)
                return 1;
        else if(addr == ADDR_FLASH_SECTOR_2)
                return 2;
        else if(addr == ADDR_FLASH_SECTOR_3)
                return 3;
        else if(addr == ADDR_FLASH_SECTOR_4)
                return 4;
        else
                return 444;
}
/////////////////////////////
The select Flash Sector function decides where to place the directory in flash memory based on the incoming pointer.

So I am trying to generate data at absolute address.
If not possible to do this, i will have to use extern.
Last edited by Serdar on Wed Feb 22, 2023 3:16 am, edited 1 time in total.

Who is online

Users browsing this forum: Gaston1980, Google [Bot] and 149 guests