dislo
dislo

Reputation: 1

Specify absolute address on embedded 32bit

I'm trying to develop a memory area written in C on a 32bit embedded device where data is to be stored on specific addresses.

I currently have the memory area in a section, let say between 0x1000-0x2000 but I need to specify the data in specific addresses.

uint8_t data[0x100]; /* This should be stored at eg. 0x1020 */
uint8_t data2[0x100]; /* This should be stored at eg. 0x1500 */
uint8_t data3[0x100]; /* This should be stored at 0x1F00 */
 

The solution I came up with is to create reserved blocks in between the data blocks.

typedef struct data_t
{
 uint8_t reserved1[0x1020]; /* Used for padding */
 uint8_t data[0x100]; /* This should be stored at eg. 0x1020 */
 uint8_t reserved2[0x3E0]; /* Used for padding */
 uint8_t data2[0x100]; /* This should be stored at eg. 0x1500 */
}

I'm pretty sure there is a nicer solution then that or a smarter solution. I'm using GHS compiler for the ones with GCC tricks.

Upvotes: 0

Views: 94

Answers (1)

0___________
0___________

Reputation: 67476

use linker script for that. Place section at the address, then place variables in this section.

Another way:

typedef struct
{
 uint8_t reserved1[0x1020]; /* Used for padding */
 uint8_t data[0x100]; /* This should be stored at eg. 0x1020 */
 uint8_t reserved2[0x3E0]; /* Used for padding */
 uint8_t data2[0x100]; /* This should be stored at eg. 0x1500 */
}data_t;

#define DATA1 ((volatile data_t *)0x1020)

//usage
void foo()
{
    DATA1 -> data[5] = 4;
}

Upvotes: 2

Related Questions