下面有一小段code,
Code:
int main(void) {
int result;
result=add(1,2);
result=mul(3,4);
return EXIT_SUCCESS;
}
其中add()這個function我要放到flash上,
在執行前要搬到copy到ram裡,
使得add()在ram上執行。
步驟:
(1) 在linker script裡,要定義add的text在特定區域裡執行。
Code:
.text2 : {
PROVIDE (_begin_text2 = . );
add.o (.text)
PROVIDE (_end_text2 = .);
}>SDRAM AT>FLASH
其中>SDRAM AT>FLASH表示放在flash裡,(lma在flash裡)
執行時在SDRAM裡。(vma在SDRAM裡)
我們從elf檔可知,text2的VMA在0x8000,LMA在0x40。
Code:
Sections:
Idx Name Size VMA LMA File off Algn
0 .nds32_init 00000040 00000000 00000000 00001000 2**3
CONTENTS, ALLOC, LOAD, READONLY, CODE
1 .text2 00000034 00008000 00000040 00002000 2**2
CONTENTS, ALLOC, LOAD, READONLY, CODE
2 .text 0000014c 00000074 00000074 00002074 2**2
CONTENTS, ALLOC, LOAD, READONLY, CODE
(2)在執行add()之前,必須要先將code從flash copy到ram裡。
如果覺得執行速度太慢,也有人用硬體實作搬移的工作。
Code:
int copy_text2(void) {
int size;
int text2_vma_begin, text2_vma_end, text2_lma_begin;
text2_vma_begin=(unsigned int)&_begin_text2;
text2_vma_end=(unsigned int)&_end_text2;
text2_lma_begin=(unsigned int)&_lma_text2;
size=text2_vma_end-text2_vma_begin;
/* copy mul from lma to vma */
volatile unsigned int *src = (volatile unsigned int *)text2_lma_begin;
volatile unsigned int *dest = (volatile unsigned int *)text2_vma_begin;
while (size > 0) {
*dest++ = *src++;
size = size - 4;
}
return 0;
}
之後main呼叫時,就和一般的程式相同。
完整的example:
Attachment:
text_n8.zip [89.41 KiB]
Downloaded 1316 times