Xilinx SDK中指定变量的物理位置

问题点:使用Xilinx SDK开发 zynq,如果不注意修改堆栈大小,运行会直接卡死崩掉。因为SDK开发属于裸机程序开发了,内存需要自己管理,而带系统的由系统管理。

首先在声明变量的时候在后面增加attribute
int matrix[16][16384] __attribute__((section(".matrix")));

然后在Linker Script里面做如下修改:
1. 双击打开lscript.ld,在GUI的Available Memory Region里面增加需要的内存区域。当然这一步也可以直接用Text Editor编辑lscript.ld完成。

2. 右键单击lscript.ld,单击Open With->Text Editor。在其中增加.matrix的定义。

编辑完成后的lscript.ld 如下所示:
MEMORY
{
   ps7_ddr_0_S_AXI_BASEADDR : ORIGIN = 0x00100000, LENGTH = 0x3FF00000
   ps7_ram_0_S_AXI_BASEADDR : ORIGIN = 0x00000000, LENGTH = 0x00030000
   ps7_ram_1_S_AXI_BASEADDR : ORIGIN = 0xFFFF0000, LENGTH = 0x0000FE00
   ps7_ddr_0_A_AXI_MATRIX  : ORIGIN = 0x20000000, LENGTH = 0x100000
}

.matirx : {
   __matrix_start = .;
   *(.matrix)
   *(.matrix.*)
   __matrix_end = .;
} > ps7_ddr_0_A_AXI_MATRIX

_end = .;
}

注意:如果Linker Script配置不对,编译不会报错,而是直接把变量放到.data section里面。所以源码里面最好检查一下。

其他常用的Attribute

1.

aligned (alignment)
This attribute specifies a minimum alignment for the variable or structure field, measured in bytes.
例子:
int x __attribute__ ((aligned (16))) = 0;
struct foo { int x[2] __attribute__ ((aligned (8))); };
short array[3] __attribute__ ((aligned (__BIGGEST_ALIGNMENT__)));

2.
packed
The packed attribute specifies that a variable or structure fieldshould have the smallest possible alignment—one byte for a variable,and one bit for a field, unless you specify a larger value with thealigned attribute.
例子:
struct foo
{
char a;
int x[2] __attribute__ ((packed));
};

猜你喜欢

转载自blog.csdn.net/nick123chao/article/details/77653400