ARM下Keil轉到Eclipse後的幾個問題
Keil轉戰到Eclipse下,
首先,Eclipse的交叉工具鏈的環境要進行設置;
其次,在Keil中的Scatter file在Eclipse下要重新編寫;
最後,Eclipse的調試環境要進行配置。
Eclipse的環境搭建可參考:
基於Eclipse的嵌入式ARM開發環境建立
問題1,關於中斷
Keil中的中斷函數申請如下:
void __irq FunctionName(void)
就是函數帶一個__irq的前綴。
如果此函數在Eclipse下編譯,error: expected '=', ',', ';', 'asm' or '__attribute__'before 'FunctionName
那麼在Eclispe中怎麼申請一箇中斷(或者中快速中斷)函數呢?
參考網址:
http://velep.com/archives/1014.html三星6410裸機程序開發4:eclipse中實現6410中斷功能
http://blog.csdn.net/jjjzmj1987/article/details/5091525關於 中斷函數申明 static void Dma0Done() __attribute__((interrupt("IRQ")));
示例:
void FunctionName(void) __attribute__ ((interrupt("IRQ")));
void FunctionName (void)
{
//do something
}
Note: 除了可以使用IRQ中斷方式之外,還可以寫FIQ,SWI,ABORT,UNDEF的中斷處理函數。
但在Eclipse內編譯時卻出現了:
error:interrupt Service Routines cannot be coded in Thumb mode
在Eclipse的C/C++ Build->Setting裏將Instruction Set Thumb (-mthumb)改爲ARM (-marm)或者Toolchain default,Thumb interwork (-mthumb-interwork)勾選上,重新編譯。如下圖:
上面選項是什麼意思,可參考:http://blog.csdn.net/armeasy/article/details/6250969
-mthumb
Generate code for the Thumb instructionset. The default is to use the 32-bit ARM instruction set. This optionautomatically enables either 16-bit Thumb-1 or mixed 16/32-bit Thumb-2instructions based on the -mcpu=name and -march=name options. This option is notpassed to the assembler. If you want to force assembler files to be interpretedas Thumb code, either add a `.thumb' directive to the source or pass the-mthumb option directly to the assembler by prefixing it with -Wa.
-mthumb-interwork
Generate code which supports callingbetween the ARM and Thumb instruction sets. Without this option the twoinstruction sets cannot be reliably used inside one program. The default is-mno-thumb-interwork, since slightly larger code is generated when-mthumb-interwork is specified.
問題2,關於程序入口參數
以前用ARM的IDE工具,使用的是ARM標準的彙編語言。現在要使用GNU的工具,當然要了解一點GNU ARM彙編的不同之處。
Keil的程序,在Eclipse重新建立工程後,編譯通過,鏈接時出現一個警告:
ld.exe:warning: cannot find entry symbol _start; defaulting to 00008018
不能找到_start入口。
在Keil下,程序的入口指定爲
__ENTRY
[CODE].section<.section_name> {,”<flags>”}
開始一個新的代碼或數據段。
.text, 代碼段;.data, 初始化數據段;.bss, 未初始化數據段。
這些段都有缺省的標誌(flags)聯接器可以識別這些標誌。(與armasm中的AREA相同)
下面是ELF格式允許的段標誌
標誌
含義
a 允許段
w 可寫段
x 執行段
問題3,文件後綴
.c文件後綴必須是小寫c,如果改成大寫C,將會導致此文件不編譯。
彙編文件的結尾貌似必須是大寫S。
問題4,sbrkr.c:(.text+0x18): undefined reference to ‘_sbrk’
根據網址:https://forum.sparkfun.com/viewtopic.php?t=5390&start=15
運行類似的命令:arm-elf-ar -d libc.asyscalls.o,將syscalls從libc.a中剝離。
在項目中添加syscalls.c文件,還沒有寫DebugWrite原型,編譯就通過了。
可能的原因是因爲我調用了sprintf之類的庫函數,導致在底層調用時沒有找到相應的鏈接函數,這部分鏈接函數可以自己實現。
但是我現在採用的是直接重新實現sprintf函數,因爲目前我還只用到了sprintf函數。可以調試,還要看看有沒有什麼問題。
@2015-03-14
今天Lwip部分移植上。編譯出現了這個。
d:/program/arm_cross/arm-2013.11-24-v4-8-1/bin/../lib/gcc/arm-none-eabi/4.8.1/../../../../arm-none-eabi/lib\libc.a(lib_a-sbrkr.o):In function `_sbrk_r':
sbrkr.c:(.text+0x18):undefined reference to `_sbrk'
d:/program/arm_cross/arm-2013.11-24-v4-8-1/bin/../lib/gcc/arm-none-eabi/4.8.1/../../../../arm-none-eabi/lib\libc.a(lib_a-signalr.o):In function `_kill_r':
signalr.c:(.text+0x1c): undefined reference to `_kill'
d:/program/arm_cross/arm-2013.11-24-v4-8-1/bin/../lib/gcc/arm-none-eabi/4.8.1/../../../../arm-none-eabi/lib\libc.a(lib_a-signalr.o):In function `_getpid_r':
signalr.c:(.text+0x44):undefined reference to `_getpid'
d:/program/arm_cross/arm-2013.11-24-v4-8-1/bin/../lib/gcc/arm-none-eabi/4.8.1/../../../../arm-none-eabi/lib\libc.a(lib_a-writer.o):In function `_write_r':
writer.c:(.text+0x20):undefined reference to `_write'
d:/program/arm_cross/arm-2013.11-24-v4-8-1/bin/../lib/gcc/arm-none-eabi/4.8.1/../../../../arm-none-eabi/lib\libc.a(lib_a-closer.o):In function `_close_r':
closer.c:(.text+0x18):undefined reference to `_close'
d:/program/arm_cross/arm-2013.11-24-v4-8-1/bin/../lib/gcc/arm-none-eabi/4.8.1/../../../../arm-none-eabi/lib\libc.a(lib_a-fstatr.o):In function `_fstat_r':
fstatr.c:(.text+0x1c): undefined reference to `_fstat'
d:/program/arm_cross/arm-2013.11-24-v4-8-1/bin/../lib/gcc/arm-none-eabi/4.8.1/../../../../arm-none-eabi/lib\libc.a(lib_a-isattyr.o):In function `_isatty_r':
isattyr.c:(.text+0x18):undefined reference to `_isatty'
d:/program/arm_cross/arm-2013.11-24-v4-8-1/bin/../lib/gcc/arm-none-eabi/4.8.1/../../../../arm-none-eabi/lib\libc.a(lib_a-lseekr.o):In function `_lseek_r':
lseekr.c:(.text+0x20):undefined reference to `_lseek'
d:/program/arm_cross/arm-2013.11-24-v4-8-1/bin/../lib/gcc/arm-none-eabi/4.8.1/../../../../arm-none-eabi/lib\libc.a(lib_a-readr.o):In function `_read_r':
readr.c:(.text+0x20):undefined reference to `_read'
collect2.exe: error: ld returned 1 exit status
這是由於在用arm-none-eabi工具進行編譯時,沒有newlib庫導致的。之前解決了關於這個庫的一下小問題,當時是採用了printf和sprintf之類的函數,最後解決方法是自己在底層實現了sprintf函數。但這次報的太多了,因此想將newlib移植過來。newlib裏面就是一些常用的系統庫函數。
如果是在linux下編譯,linux本身就帶glibc庫,可能就不會出現這個問題了。
從這個網址上ftp://sourceware.org/pub/newlib/index.html下載了newlib2013-12-23: newlib-2.1.0.tar.gz(16.2MB)。之所以沒有下載最新的是因爲,最新的那幾次版本太頻繁了,怕不穩定。這個應該就夠了。
在configure,make all時,過程中出現了不少問題,configure的參數不一樣有時候也導致編譯不過。make install有時雖然成功了,仍然報這些錯。可能是configure時的參數不對。看到網上說,其實並不是所有的newlib內的函數你都需要,如果移植成功,可能你編譯出來的目標文件比較大。再加上newlib不好移植,於是有轉戰實現syscalls.c文件。參考了:
https://balau82.wordpress.com/2010/12/16/using-newlib-in-arm-bare-metal-programs/
http://contiki.sourceforge.net/docs/2.6/a00523_source.html
在裏面的一些函數實現過程中調用了其他的函數,其他的函數暫時還沒有實現。不過網址http://contiki.sourceforge.net/docs/2.6/a00523_source.html上關於其他的函數也有實現。所以可以參考看看是怎麼實現的。
在網址http://www.embecosm.com/appnotes/ean9/ean9-howto-newlib-1.0.html的5.3. Standard System Call Implementations中也有介紹怎麼實現這些函數,文中說這是一種最簡單的方法,供參考。
問題5調試執行mrc p15,0,r0,c1,c0,0,跑飛
在調試時,運行到執行協處理器命令時程序異常,程序跳轉到0x00000004地址出,即未定義指令異常。最終發現問題在於工程選項中Thumb和ARM指令的設置上。在自己的工程中找到設置Thumb和ARM指令的地方,選擇嘗試。