學習elua(二)--編譯和燒寫
上文中介紹了什麼是elua,本文將動手實驗,編譯elua,並燒寫於stm32f4discovery平臺之上
必要的前提:
軟體:
ubuntu作業系統,用於作為交叉編譯的環境(我用的是14.04,不過其他版本應該沒問題)
git(沒有的話apt-get installgit)
硬體
stm32f4discovery開發板
mini usb線
micro usb線
安裝依賴:
apt-get install 5.1(最好裝5.1,5.2沒試過)
apt-get install luarocks
luarocks install luafilesystem
luarocks install lpack
luarocks install md5
安裝工具鏈:
原因詳見最後一節
首先根據連結GNU Tools forARM Embedded Processors toolchain下載gcc-arm-none-eabi-4_8-2014q2-20140609-linux.tar.bz2,假設下載檔案儲存位置為使用者根目錄~
解壓縮:tar -xvjfgcc-arm-none-eabi-4_8-2014q2-20131204-linux.tar.bz2
新增PATH
在sh的配置檔案中新增
exportPATH=$PATH:~/gcc-arm-none-eabi-4_8-2014q2/bin
設定好後可以做一個測試:
$ arm-none-eabi-gcc–print-multi-lib
下載elua原始碼
git clonehttps://github.com/elua/elua.git
編譯elua原始碼
進入elua目錄
luabuild_elua.lua board=stm32f4discovery
*********************************
CompilingeLua ...
CPU: STM32F407VG
Board: stm32f4discovery
Platform: stm32f4
Allocator: multiple
BootMode: standard
Target: lua
Toolchain: codesourcery
ROMFSmode: verbatim
Version: v0.9-215-gc216dd2
luabuild_elua.lua board=stm32f4discovery–c相當於clean
prog 是生成hex和bin檔案,用於燒寫,如果沒有prog選項,只生成elf和map檔案
燒寫elua映象
先安裝依賴
whichautoconf
沒有sudo apt-get install autoconf
同理sudo apt-getinstall libusb-1.0
sudo apt-get install pkg-config
下載stlink的程式碼
git clone https://github.com/texane/stlink stlink.git
編譯stlink的程式碼
cd stlink.git
./autogen.sh
./configure
make
安裝
cd flash
sudo cp st-flash /usr/bin
安裝udev,好識別stlink裝置
cd ..
sudo cp *.rules /etc/udev/rules.d
sudo restart udev
用miniusb連線pc和stm32f4discovery
命令列輸入(elua 目錄下):
st-flash write elua_lua_stm32f4discovery.bin 0x8000000
連線stm32f4discovery
用micro usb連線pc和stm32f4discovery,linux不需要驅動,在/dev目錄下有ttyACM0
使用cutecom作為串列埠終端(sudo apt-get install cutecom)
開啟cutecom sudo cutecom
ttyACM0 115200 n 8 1
注意回車和換行
輸入help
將返回elua shell的幫助,關於elua shell 的資訊將在第四篇文章中涉及
官方推薦的工具鏈codesourcery的問題
最開始我參考的了官方文獻:
根據官方推薦安裝sourcery的lite 版本(免費)注意為for ARM EABI
Obtainingand installing the toolchain is very easy:
1. go to the CodeSourcery download location for the toolchain.
2. select from the table the current version in the "EABI"line (the link to the current version is just above the "Allversions..." link).
3. download and run the installer.
下載arm-2014.05-28-arm-none-eabi.bin
安裝/bin/sh arm-2014.05-28-arm-none-eabi.bin
配置PATH
export PATH=/home/XXX/MentorGraphics/Sourcery_CodeBench_Lite_for_ARM_EABI/bin//bin:$PATH
這時需要驗證一下
$ arm-none-eabi-gcc–print-multi-lib
對比GNU的版本我們可以發現,少了很多庫,其中就沒有對硬浮點的支援(這個時候來出現連結錯誤後,查了很多檔案才發現)
編譯 luabuild_elua.lua board=stm32f4discovery
連結時報錯:
好長的一個錯誤,字面上看ld錯誤,libm.a不支援VFP
上網搜尋 stm32f4discovery uses VFPregister arguments 等關鍵字,發現:
There are a variety of arm-toolchains outthere. These include arm DevKit pro, Yagarto,MentorGraphics CodeSourcery Lite, and the gcc-arm toolchain.Of these toolchains, the two most popular ones are the codesourcery Lite (ownedby mentor Graphics) and the gcc-arm toolchain.Traditionally I would use the Codesourcery Litetoolchain. However that toolchain does not provide “hardfp” (hardware floatingpoint) support.So If I choose to use it with say the stm32F4Discovery board in the future, Iwill not be able to take full advantage of the floating point unit (FPU) on thestm32F4 microcontroller on board. The gcc-arm toolchain on the other hand hashardfp support and can be used with all Cortex-M0,M3 and M4 parts. Thisuniversality is why I will choose it even though I will most likely onlyfocus on Cortex-M0 cores. And so for the purpose of this tutorial, I will beusing the gcc-arm toolchain.
I have startedworking on STM32F4 Discovery board and have compiled and run a few basicprograms using the latest Yagarto toolchain containing the GCC 4.6.2. Latelythough on several forums I have read that many toolchains including the latestYagarto have problems when it comes to using the on-board hardware FPU.Ihave also read that the latest CodeSourcery toolchain does support hardwarefloating point, but not in the lite edition!!
While digging deepinto the topic I found this toolchain which is specifically for ARM Cortex M/Rcontrollers, and claims to have no problems when it comes to hardware FPU.
兩種解決方案:
第一種,換工具鏈,如上文中GNU的工具鏈
第二種,將硬浮點改為軟浮點:
FP32with fpu enabled
其中:
- Software floating point emulation, which is the default. In this case, the compiler implements floating-point arithmetic by means of library calls.
- VFP hardware floating-point support using the soft-float ABI. This is selected by the-mfloat-abi=softfp option. When you select this variant, the compiler generates VFP floating-point instructions, but the resulting code uses the same call and return conventions as code compiled with software floating point.
- VFP hardware floating-point support using the VFP ABI, which is the VFP variant of the Procedure Call Standard for the ARM® Architecture (AAPCS). This ABI uses VFP registers to pass function arguments and return values, resulting in faster floating-point code. To use this variant, compile with -mfloat-abi=hard.
判斷是浮點編譯選項的問題,庫檔案和編譯選項不相容
查詢elua原始碼,搜尋“mfloat-abi”,發現在/src/platform/stm32f4/conf.lua
localtarget_flags = { '-mcpu=cortex-m4', '-mthumb','-mfloat-abi=hard','-mfpu=fpv4-sp-d16' }
該為
local target_flags = {'-mcpu=cortex-m4', '-mthumb','-mfloat-abi=softfp}
不過需要clean一下
luabuild_elua.lua board=stm32f4discovery –c
再編譯
luabuild_elua.lua board=stm32f4discovery target=lualong
--和-mfloat-abi=softfp有關,如果預設target=lua為float 型別,有console,但是輸入lua命令後沒反映
*********************************
CompilingeLua ...
CPU: STM32F407VG
Board: stm32f4discovery
Platform: stm32f4
Allocator: multiple
BootMode: standard
Target: lualong
Toolchain: codesourcery
ROMFSmode: verbatim
Version: v0.9-215-gc216dd2
ok通過編譯,不過此時的lua不能支援浮點運算
參考文獻:
下一篇文章,將定elua的編譯選項,新增和刪除一些功能模組(例如wofs檔案系統等)