Kineties-L是32位的微處理器家族,基于ARM Cortex M0+。我手上有一個FRDM-KL25Z的開發板,官方有一個基于eclipse的IDE——CodeWarrior,可以很方便地進行編譯、下載、調試。但是不足的是免費版本的CodeWarrior不能很方便地使用C++,如圖,當我選擇C++時就不能選擇Device Initialization或者Processor Expert了。
我們使用eclipse來配置一個純凈的KL25Z的開發環境:
1、 安裝交叉編譯工具:
a) 首先安裝交叉編譯工具,推薦使用Codesourcery。
http://www.mentor.com/embedded-software/sourcery-tools/sourcery-codebench/editions/lite-edition/
b) 選擇ARM processors,點擊Download the EABI Release,在隨后的頁面中注冊賬戶(有賬戶的話直接登錄),下載鏈接會發送到你注冊的郵箱中。
c) 然后下載,安裝。
d) 安裝完畢后就能在命令行下編譯ARM程序了。
2、 安裝eclipse:
a) 訪問http://www.eclipse.org/downloads/
b) 下載Eclipse IDE for C/C++Developers。
c) 解壓即可使用。(前提是已經安裝JDK)
3、 安裝GNU ARM Eclipse Plug-in
這是一個eclipse的插件,裝上這個插件之后,eclipse就能夠編譯ARM程序了。
a) 在eclipse中點擊Help->Install New Software,輸入
http://gnuarmeclipse.sourceforge.net/updates/
b) 安裝成功后就能使用GNU ARM Eclipse插件了。
4、 新建C++工程
a) 輸入工程名
b) 工程類型選擇ARM Cross Target Application->Empty Project
c) 工具鏈選擇ARM Windows GCC (Sourcery G++ Lite)
a) 首先加入在CodeWarrior中新建C++工程時生成的代碼,包括三個目錄:Project_Headers、Project_Setting、Sources

b)
6、 配置工程
因為CodeWarrior也是使用eclipse來配置工程的,所以可以參照CodeWarrior來配置工程。
a) 在工程文件夾上單擊右鍵,選擇Properties。在彈出的對話框中選擇C/C++ Build->Setting。
b) Target Processor:
Processor: cortex-m0
Endiannes: Little Endian (-mlittle-endian)
Float ABI: Library (-mfloat-abi=soft)
c) Debugging
Debug level: Maximum (-g3)
d) Additional Tools
e) ARM Sourcery Windows GCC Assembler
Command line pattern:
${COMMAND} ${INPUTS} ${FLAGS} ${OUTPUT_FLAG}${OUTPUT_PREFIX}${OUTPUT}
f) ARM Sourcery Windows GCC C Compiler
因為eclipse會根據源文件的后綴名來判斷使用gcc還是g++,為了統一使用g++來編譯程序,將Command修改為arm-none-eabi-g++。
Command line pattern:
${COMMAND} ${INPUTS} ${FLAGS} ${OUTPUT_FLAG}${OUTPUT_PREFIX}${OUTPUT}
g) Preprocessor
將Do not search system directories (-nostdinc)打鉤
h) Directories
加入Include路徑:
"${workspace_loc:/${ProjName}/Project_Headers}"
"${workspace_loc:/${ProjName}/Sources}"
"${workspace_loc:/${ProjName}/Generated_Code}"
"${workspace_loc:/${ProjName}/Project_Settings/Startup_Code}"
"D:\Freescale\CW MCU v10.3\MCU\ARM_GCC_Support\ewl\EWL_C\include"
"D:\Freescale\CW MCU v10.3\MCU\ARM_GCC_Support\ewl\EWL_C++\include"
"D:\Freescale\CW MCU v10.3\MCU\ARM_GCC_Support\ewl\EWL_Runtime\include"
其中后面三個在CodeWarrior的安裝目錄下。
i) Optimization
在Function sections (-ffunction-sections)和Data sections (-fdata-sections)前打鉤
j) Miscellaneous
在Other flags寫入:-c -fmessage-length=0 -Iinclude -include lib_ewl_c++.prefix
k) ARM Sourcery Windows GCC C++ Compiler
和前面的ARM Sourcery Windows GCC C Compiler配置相同。
l) ARM Sourcery Windows GCC C++ Linker
Command line pattern:
${COMMAND} ${INPUTS} ${FLAGS} ${OUTPUT_FLAG}${OUTPUT_PREFIX}${OUTPUT}
m) ARM Sourcery Windows GCC C++ Linker
Script file (-T)中寫入工程中的鏈接文件
C:\Users\wangqi\workspace-cpp\cpp\Project_Settings\Linker_Files\MKL25Z128_flash.ld
n) Libraries
Library search path中寫入庫文件的路徑:
"D:\Freescale\CW MCU v10.3\MCU\ARM_GCC_Support\ewl\lib\armv6-m"
o) Miscellaneous
Linker flags (-Xlinker [option])中寫入:
--undefined=__pformatter_
--defsym=__pformatter=__pformatter_
--start-group
-lc++ -lrt -lsupc++ -lc -lgcc -luart
--end-group
Other flags中寫入:
-n
7、 修改程序
此時如果運行程序會發生這樣的錯誤:
問題出在Vector.c中,打開Vector.c文件尋找__thumb_startup(void)聲明。
將extern void __thumb_startup( void );改成extern "C" void __thumb_startup( void );
8、 將elf文件轉化成bin文件
點擊工程目錄右鍵,選擇Properties->C/C++ Build->Setting->Build Steps
在Post-build steps一項的Command中輸入
arm-none-eabi-objcopy -O binary -S cpp.elf cpp.bin