Arduino IDE功能简单,对于不熟悉编程环境的用户十分容易上手;但是对于大型的Arduino项目开发,如果再使用自带的IDE就会多少显得不便。之前我曾经使用过Eclipse + CDT + avr-gcc开发AVR程序,鉴于Arduino也是基于AVR和avr-gcc的,理论上应该也可以使用Eclipse开发。Google之后发现在Arduino官方网站上就有配置方法的介绍[1],此处自己按照步骤实现了一下,并将要点归纳如下。这里仅针对Windows平台,对于其他平台的用户请在原文中找到对应的配置方法。
(作者补充:Eclipse配置Arduino相对比较麻烦,如果想要相对简单的配置方法,可以使用Arduino for Visual Studio插件配合VS2012 – 2015使用,感谢Herry的评论。)
1.下载Eclipse IDE for C/C++
下载地址在Eclipse网站:http://www.eclipse.org/downloads/。该下载页面中有很多Eclipse版本,找到并下载C/C++版本:
2. 在Eclipse环境中安装AVR插件
打开Eclipse软件,点击菜单 > Help > Install New Software…
在弹出的菜单中的 “Work with:” 处输入插件更新的URL地址: http://avr-eclipse.sourceforge.net/updatesite,点击 “Next>” 按照提示安装即可。
3. 配置avr-gcc开发工具链
配置AVR工具链有两种方法:
1) 直接安装WinAVR-20081205
2)配置工具链选项,使其指向Arduino安装目录下的工具链中
打开Window -> Preferences -> AVR -> Paths,选择“Disable search for system paths at startup”,然后将配置选项设置为以下值:
AVR-GCC | “${ARDUINO DIR}\hardware\tools\avr\bin” |
GNU make | “${ARDUINO DIR}\hardware\tools\avr\utils\bin” |
AVR Header files | “${ARDUINO DIR}\hardware\tools\avr\avr\include” |
AVRDude | “${ARDUINO DIR}\hardware\tools\avr\bin” |
配置完成后的结果如下:
4. 配置Avrdude
选择菜单 > Project > Properties > AVRDude,在Program Configuration选择项右边选择 “New..” 新建一个下载配置,配置参数设置如下:
Programmer hardware | 选择 “Arduino” |
Overwrite default port | 输入”\\.\” + 端口号,如”\\.\COM6″ |
Override default baud rate | 在${ARDUINO DIR}\hardware\arduino\avr\boards.txt文件中查询 |
之后还需要设置AVRDude的配置参数。
打开Window -> Preferences -> AVR -> AVRDude,选择“Use custom configuration file for AVRDude”,并将配置文件的路径设置为“${ARDUINO DIR}\hardware\tools\avr\etc\avrdude.conf”。
5. 获得Arduino核心库
为了使用Arduino的API,在链接阶段需要有Arduino的核心库文件。有多种方法获得该文件,其中包括自己编译源代码,不过最简单的方法是直接从Arduino IDE的生成文件中获得。具体方法是使用Arduino IDE建立任意一个工程,Arduino会在目录“C:\Users\<username>\AppData\Local\Temp\buildXXXXXXXXXXXX.tmp”中建立临时文件,从对应文件夹中将core.a拷贝出来。因为这个库文件只能用于对应的处理器,所以最好将它重新命名为所使用的Arduino型号,比如libArduinoUNOCore.a。
6. 创建工程与工程配置
在Eclipse中创建一个新的AVR Cross Target Application工程:
Configuration中只选择release工程,点击Next之后选择CPU类型与频率,我这里使用的是UNO,所用的型号为ATmega328p,频率为16MHz。其他型号如Duemilanove使用的也是ATmega328p,而Arduino Mega使用的为ATmega1280,具体的配置情况也可以在board.txt中查询(xxx.build.mcu)。
之后对工程参数进行配置,在工程上点击右键,选择Properties -> C/C++ Build -> Settings,配置方法如下:
Additional Tools in Toolchain | – 确认勾选Generate Hex – 确认勾选Print Size – 如果想在编译后直接下载,则勾选AVRdude |
AVR Compiler -> Directories | – 添加AVR Core所在的目录 <arduino>\Arduino\hardware\arduino\avr\cores\arduino – 添加”pins_arduino.h”所在目录 “<arduino>\hardware\arduino\avr\variants\standard” |
AVR Compiler -> Optimization | – 设置 Optimization Level 为 Size Optimizations (-Os) – 设置 “Other Optimization Flags” 为 “-ffunction-sections -fdata-sections” – 取消”pack structs” 和 “short enums”. |
AVR Compiler -> Language Standard | – 取消”char is unsigned” 和”bitfields are unsigned”。 |
AVR C/C++ Linker -> Libraries | – 在Library Path中添加之前拷贝的库文件所在目录,必须保证所用的库使用系统的CPU和时钟频率! – 在Libraries中添加Arduino核心库文件,名称为原文件名去除lib头及.a后缀。比如文件名如果是libArduinoCore.a,此处则设置为ArduinoCore。 – 在Libraries中添加库m,可以保证不必要的麻烦 |
AVR C/C++ Linker -> General | – 设置Other Arguments 为 “-Wl,–gc-sections” |
7. 编写程序源文件及编译
在eclipse中新建一个.c源文件,输入以下示例代码:
/* * main.c * * Created on: 5 Aug 2014 * Author: a45 */ /* Blink Turns on an LED on for one second, then off for one second, repeatedly. This example code is in the public domain. */ #include "arduino.h" // Pin 13 has an LED connected on most Arduino boards. // give it a name: int led = 13; // the setup routine runs once when you press reset: void setup() { // initialize the digital pin as an output. pinMode(led, OUTPUT); } // the loop routine runs over and over again forever: void loop() { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second } int main(void) { init(); setup(); while (1) { loop(); } return 0; }
这里的程序结构和Arduino环境下开发略为不同,用户需要自己编写main函数,并将 loop() 以及 init() 包含进来。之后点击编译按钮进行编译,若编译过程中出现错误,请仔细对照之前的配置表格,确认配置是否一致。我的编译过程输出如下:
14:01:47 **** Build of configuration Release for project arduino **** make all Building file: ../main.c Invoking: AVR Compiler avr-gcc -I”C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino” -I”C:\Program Files (x86)\Arduino\hardware\arduino\avr\variants\standard” -Wall -Os -ffunction-sections -fdata-sections -ffunction-sections -fdata-sections -std=gnu99 -mmcu=atmega328p -DF_CPU=16000000UL -MMD -MP -MF”main.d” -MT”main.d” -c -o “main.o” “../main.c” Finished building: ../main.cBuilding target: arduino.elf Invoking: AVR C Linker avr-gcc -Wl,-Map,arduino.map -Wl,–gc-sections -L”F:\EclipseWorksCpp\arduino” -mmcu=atmega328p -o “arduino.elf” ./main.o -lArduinoUNOCore -lm Finished building target: arduino.elf Invoking: AVR Create Extended Listing avr-objdump -h -S arduino.elf >”arduino.lss” Finished building: arduino.lss Create Flash image (ihex format) avr-objcopy -R .eeprom -R .fuse -R .lock -R .signature -O ihex arduino.elf “arduino.hex” Finished building: arduino.hex Create eeprom image (ihex format) avr-objcopy -j .eeprom –no-change-warnings –change-section-lma .eeprom=0 -O ihex arduino.elf “arduino.eep” Finished building: arduino.eep Invoking: Print Size avr-size –format=avr –mcu=atmega328p arduino.elf AVR Memory Usage —————- Device: atmega328p
Program: 1094 bytes (3.3% Full) (.text + .data + .bootloader)
Data: 11 bytes (0.5% Full) (.data + .bss + .noinit) Finished building: sizedummy 14:01:49 Build Finished (took 1s.509ms) |
8. 下载运行
编译成功后,点击工具栏中的AVR图标运行AVRDude进行程序下载。我在下载过程中的命令行输出截取如下:
Launching C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avrdude -pm328p -carduino “-P\\.\COM12” -b115200 -F -Uflash:w:arduino.hex:a “-CC:\Program Files (x86)\Arduino\hardware\tools\avr\etc\avrdude.conf” Output: avrdude: AVR device initialized and ready to accept instructionsReading | ################################################## | 100% 0.00savrdude: Device signature = 0x1e950f avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed To disable this feature, specify the -D option. avrdude: erasing chip avrdude: reading input file “arduino.hex” avrdude: input file arduino.hex auto detected as Intel Hex avrdude: writing flash (1094 bytes):Writing | ################################################## | 100% 0.18savrdude: 1094 bytes of flash written avrdude: verifying flash memory against arduino.hex: avrdude: load data flash data from input file arduino.hex: avrdude: input file arduino.hex auto detected as Intel Hex avrdude: input file arduino.hex contains 1094 bytes avrdude: reading on-chip flash data:Reading | ################################################## | 100% 0.14savrdude: verifying … avrdude: 1094 bytes of flash verifiedavrdude done. Thank you.avrdude finished |
如果下载成功,将看见板载的LED不断闪烁。
至此Eclipse下Arduino开发的完整配置就完成了,虽然配置过程非常复杂,不过也算一劳永逸。Eclipse对于编辑、着色、函数索引、自动完成都有很好的支持,可以大大提高Arduino项目的开发速度。不过目前也有直接将开发配置以插件形式整合到Eclipse中的项目(http://www.baeyens.it/eclipse/),不过看样子配置起来也十分麻烦,相信不久之后随着Arduino的不断应用,相关的IDE支持也会日渐成熟的。
参考资料
[1] arduino.cc, “Arduino Playground – Eclipse”, http://playground.arduino.cc/code/eclipse
[2] avr-eclipse, “Plugin Download”, http://avr-eclipse.sourceforge.net/wiki/index.php/Plugin_Download
[3] Francesco Rigoni , “Arduino development with Eclipse – A step by step tutorial to the basic setup”, http://horrorcoding.altervista.org/arduino-development-with-eclipse-a-step-by-step-tutorial-to-the-basic-setup/
>> 本文章版权归作者所有,如需转载请联系作者授权许可。
>> 原文来自: 云飞机器人实验室
>> 原文地址: Arduino | Windows下使用Eclipse开发Arduino程序
>> 关于我们: 关于云飞实验室
>> 支持我们: 帮助我们可持续发展
我用着VS的arduino插件还不错哈,http://visualmicro.codeplex.com/
这个看上去也不错。
话说回来,在Eclipse使用Arduino虽然一劳永逸,不过需要的配置确实太多了。
搞这么复杂,用SublimeText装个arduino插件,配合arduino IDE就行了。界面比这个好看100倍。
看上去不错,我来试试。