Linux 命令行 Arduino 编译和上传?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8189306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Command line Arduino compiling and uploading?
提问by Mark Harrison
采纳答案by user1823664
Compiling and uploading sketches (as apposed to C/C++ sources) on the command line (on Windows, Mac, and Linux) is supported directly via flags to the arduino executable since 1.5.0.
自 1.5.0 起,通过标志直接支持在命令行(在 Windows、Mac 和 Linux 上)上编译和上传草图(与 C/C++ 源代码相关)到 arduino 可执行文件。
An ino can be compiled and uploaded with arduino --upload [sketch.ino]
一个ino可以编译和上传 arduino --upload [sketch.ino]
回答by Cougar
If you can use cmake then there are some links in the same web (thisand thisfor example). GNU makefile is a little bit different from cmake but nothing complicated. Just Google a little bit and you can find a lot of Makefile examples how to compile AVR code.
如果您可以使用 cmake,那么同一个网站中有一些链接(例如this和this)。GNU makefile 与 cmake 有点不同,但并不复杂。只要谷歌一点,你就可以找到很多如何编译 AVR 代码的 Makefile 示例。
回答by wesen
You need to actually create a viable cpp file out of your arduino sketch. The arduino environment does that for you automatically. One trick to get to those files is to open your arduino preferences.txt (it's in ~/Library/Arduino on the mac, I think in your Documents and Settings or Application Data on windows, don't remember exactly), and set build.verbose=true and upload.verbose=true. Start arduino, and compile your sketch (don't upload it). The console at the bottom will show you which files were compiled. You can now go to that directory, which will contain the cpp file, and compiled object files for all the core arduino objects. You can copy those into your project and use the cpp file to do further hacking. Let me know if you need more information about the Makefile, I can provide you with those I have.
您需要从 arduino 草图中实际创建一个可行的 cpp 文件。arduino 环境会自动为您执行此操作。获取这些文件的一个技巧是打开您的 arduino 首选项.txt(它在 mac 上的 ~/Library/Arduino 中,我认为在您的文档和设置或 Windows 上的应用程序数据中,不太记得了),然后设置 build .verbose=true 和 upload.verbose=true。启动arduino,编译你的草图(不要上传)。底部的控制台将显示编译了哪些文件。您现在可以转到该目录,该目录将包含 cpp 文件和所有核心 arduino 对象的编译对象文件。您可以将这些复制到您的项目中并使用 cpp 文件进行进一步的黑客攻击。如果您需要有关 Makefile 的更多信息,请告诉我,我可以为您提供我所拥有的信息。
回答by wesen
You can actually use the arduino GUI to compile and upload, and set the editor to external in the preferences. That way, you can edit the C++ (PDE) files from xcode, and have arduino generate the actual CPP and build the whole shebang.
其实可以使用arduino GUI编译上传,在preferences中设置editor为external。这样,您可以从 xcode 编辑 C++ (PDE) 文件,并让 arduino 生成实际的 CPP 并构建整个 shebang。
You can also use XCode to write plain C++/C for the arduino, using the avr-gcc compiler.
您还可以使用 avr-gcc 编译器,使用 XCode 为 arduino 编写普通的 C++/C。
Have a look at: https://stackoverflow.com/a/8192762/153835
看看:https: //stackoverflow.com/a/8192762/153835
You can then use the plain avrdude upload tool to program the arduino. Have a look at: http://www.ladyada.net/library/arduino/bootloader.html
然后,您可以使用普通的 avrdude 上传工具对 arduino 进行编程。看看:http: //www.ladyada.net/library/arduino/bootloader.html
It used to be that the protocol spoken by Arduino was a modification of the STK500 protocol, and that only the avrdude bundled with arduino could speak it. I don't know if the mainstream avrdude was upgraded, or if you still have to resort to the avrdude inside the Arduino folder.
以前Arduino讲的协议是对STK500协议的修改,只有arduino绑定的avrdude才能讲。不知道是不是主流avrdude升级了,还是还得求助于Arduino文件夹里面的avrdude。
回答by wesen
This is my boilerplate gnu make include for AVR projects, you may need to adapt some of it to fit your environment. It creates dependencies, has a host of standard gcc options I find useful or that optimize for size, as well as a library dir I use. I used this successfully to compile arduino software, I also previously hacked the PdePreprocessor in the arduino editor to be run from the command line to generate all the voodoo:
这是我用于 AVR 项目的样板 gnu make include,您可能需要调整其中的一些内容以适应您的环境。它创建了依赖项,有许多我认为有用或优化大小的标准 gcc 选项,以及我使用的库目录。我成功地使用它来编译 arduino 软件,我之前还破解了 arduino 编辑器中的 PdePreprocessor 以从命令行运行以生成所有伏都教:
https://github.com/wesen/mididuino/blob/master/app/src/processing/app/preproc/PdePreprocessor.java
https://github.com/wesen/mididuino/blob/master/app/src/processing/app/preproc/PdePreprocessor.java
#
# generic AVR makefile
#
# (c) July 2011 - Manuel Odendahl - [email protected]
#
# include this into your main Makefile, after having defined TARGET and TARGET_OBJS
all: $(TARGET).hex
CURDIR := $(dir $(lastword $(MAKEFILE_LIST)))
include $(CURDIR)MidiCtrl.mk
CC = avr-gcc
CXX = avr-g++
OBJCOPY = avr-objcopy
AVR_ARCH ?= atmega64
LDAVR_ARCH ?= avrmega64
FLASH_PROTOCOL = jtag2
CFLAGS += -Os -ffunction-sections -DAVR -I. -mmcu=$(AVR_ARCH) -mcall-prologues -fshort-enums -fpack-struct -Wall -Werror
CFLAGS += -Wall -DLITTLE_ENDIAN -g -flto
CFLAGS += no-tree-loop-optimize -ffreestanding -morder1 -funsigned-char -funsigned-bitfields -fshort-enums -fpack-struct
CFLAGS += -fdata-sections -fno-split-wide-types -fno-inline-small-functions -mcall-prologues
CLDFLAGS += -Wl,--relax,--gc-sections -ffunction-sections
CLDFLAGS += -mmcu=$(AVR_ARCH)
LDFLAGS = -m $(LDAVR_ARCH) -M
# generate list
# CFLAGS += -Wa,[email protected]
%.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
%.o: %.s
$(CC) $(CFLAGS) -c $< -o $@
%.s: %.c
$(CC) -S $(CFLAGS) -fverbose-asm $< -o $@
%.o: %.S
$(CC) $(CFLAGS) -c $< -o $@
%.syx: %.hex
ihex2sysex $< $@
%.srec: %.elf
$(OBJCOPY) -j .text -j .data -O srec $< $@
%.hex: %.elf
$(OBJCOPY) -j .text -j .data -O ihex $< $@
%.ee_srec: %.elf
$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O srec $< $@
AVR_BASE_DIR ?= $(abspath $(CURDIR)..)
AVR_LIB_DIR ?= $(AVR_BASE_DIR)/hardware/libraries
AVR_LIBS += CommonTools Midi
AVR_LIB_DIRS += $(foreach lib,$(AVR_LIBS),$(AVR_LIB_DIR)/$(lib))
AVR_INC_FLAGS += $(foreach dir,$(AVR_LIB_DIRS),-I$(dir))
AVR_OBJS += $(foreach dir,$(AVR_LIB_DIRS),$(foreach file,$(wildcard $(dir)/*.cpp),$(subst .cpp,.o,$(file))))
AVR_OBJS += $(foreach dir,$(AVR_LIB_DIRS),$(foreach file,$(filter-out $(AVR_HOST_EXCLUDE),$(wildcard $(dir)/*.c)),$(subst .c,.o,$(file))))
AVR_DEPS += $(subst .o,.d,$(AVR_OBJS))
# AVR_HOST_EXCLUDE can be used to exclude specific files later on
CXXFLAGS += $(AVR_INC_FLAGS)
CFLAGS += $(AVR_INC_FLAGS)
CXXFlags += -Werror -Wall
CFLAGS += -Werror -Wall
default: all
%.d:%.c
set -e; $(CC) -MM $(CFLAGS) $< \
| sed 's,\($*\)\.o[ :]*,.o $@ : ,g' > $@ ; \
[ -s $@ ] || rm -f $@
%.d:%.cpp
set -e; $(CXX) -MM $(CXXFLAGS) $< \
| sed 's,\($*\)\.o[ :]*,.o $@ : ,g' > $@ ; \
[ -s $@ ] || rm -f $@
%.host.d:%.c
set -e; $(CC) -MM $(CFLAGS) $< \
| sed 's,\($*\)\.o[ :]*,.o $@ : ,g' > $@ ; \
[ -s $@ ] || rm -f $@
%.host.d:%.cpp
set -e; $(CXX) -MM $(CXXFLAGS) $< \
| sed 's,\($*\)\.o[ :]*,.o $@ : ,g' > $@ ; \
[ -s $@ ] || rm -f $@
printlibs:
echo $(AVR_LIBS)
$(TARGET).elf: $(TARGET).o $(TARGET_OBJS) $(AVR_OBJS)
$(CXX) $(CLDFLAGS) -g -o $@ $^
_clean:
- rm *.elf *.hex *.o .midictrl.flags
libclean:
rm -rf $(TARGET_OBJS) $(OBJS)
# concrete settings for development environment
UNAME=$(shell uname)
ISWIN=$(findstring CYGWIN,$(UNAME))
ISMAC=$(findstring Darwin,$(UNAME))
CC = avr-gcc
CXX = avr-g++
OBJCOPY = avr-objcopy
AVR_ARCH = atmega64
F_CPU = 16000000L
CORE = minicommand2
回答by nkrkv
回答by Udo Klein
If you do not insist on make there is also scons/sconstruct scons/sconstruct. Since this in basically written in Python it is much simpler to tweak than make. In addition it can be debugged with any Python debugger.
如果你不坚持 make 还有 scons/sconstruct scons/sconstruct。由于这基本上是用 Python 编写的,因此调整比制作要简单得多。此外,它可以使用任何 Python 调试器进行调试。
回答by Sudar
I have a makefile for Arduinowhich can be used to compile and upload Arduino (or plain AVR C) programs to Arduino.
我有一个Arduino的makefile,可用于编译和上传 Arduino(或普通 AVR C)程序到 Arduino。
Following are some of the important features of this makefile
以下是此 makefile 的一些重要功能
- Supports upload via Arduino as ISP or any programmer
- Supports compiling plain AVR C programs
- Supports user as well as system libraries.
- Generate assembly and symbol files
- Program using alternate Arduino core (like ATtiny or Arduino alternate cores)
- 支持通过 Arduino 作为 ISP 或任何程序员上传
- 支持编译普通的AVR C程序
- 支持用户和系统库。
- 生成程序集和符号文件
- 使用备用 Arduino 内核编程(如 ATtiny 或 Arduino 备用内核)
回答by hithwen
You can use biicode(it's a project I'm working in) which is based on CMake (but you don't actually need to write any cmake file) and is also a tool to manage Arduino libraries
您可以使用基于 CMake 的biicode(这是我正在从事的项目)(但您实际上不需要编写任何 cmake 文件),它也是管理 Arduino 库的工具
It's main features are:
它的主要特点是:
- Dependencies resolution, transitively, as maven does, but without config files: reads dependencies directly from source code.
- Central repository, anyone can upload their libraries. They can be explored, navigated and discovered in the web
- Version control: it checks versions compatibility and allows safe updates of dependencies
- You can use it with any text editor (it has optional eclipse integration)
- It manages project setup and compilations, flashes generated firmware to the board
- 依赖解析,传递性,就像 maven 一样,但没有配置文件:直接从源代码读取依赖。
- 中央存储库,任何人都可以上传他们的库。可以在网络中探索、导航和发现它们
- 版本控制:检查版本兼容性并允许安全更新依赖项
- 您可以将它与任何文本编辑器一起使用(它具有可选的eclipse 集成)
- 它管理项目设置和编译,将生成的固件闪存到板上
You can see a quick demo hereand read more in the documentation.
回答by Madacol
Official CLI tool
官方 CLI 工具
The arduino team is developing a cliclient https://github.com/arduino/arduino-cli
arduino 团队正在开发cli客户端 https://github.com/arduino/arduino-cli
Announcement: https://blog.arduino.cc/2018/08/24/announcing-the-arduino-command-line-interface-cli/
公告:https: //blog.arduino.cc/2018/08/24/annoucing-the-arduino-command-line-interface-cli/
You can do almost everything with this, from downloading boards and libraries, to compile and upload scripts. What's missing is the monitoring part.
你几乎可以用它做任何事情,从下载板和库,到编译和上传脚本。缺少的是监控部分。
To monitor in linux you can still use the commands stty
to configure port and cat
to read it.
要在 linux 中进行监控,您仍然可以使用命令stty
来配置端口并cat
读取它。
stty -F /dev/ttyACM0 38400 # <-- Baud rate. The number in Serial.begin()
cat /dev/ttyACM0 # <-- Port
You can find the port with arduino-cli
您可以使用arduino-cli找到端口
arduino-cli board list
Full instructions in the Github repoand the man page:
Github 存储库和手册页中的完整说明:
$ arduino-cli Arduino Command Line Interface (arduino-cli).
Usage: arduino-cli [command]
Examples: arduino <command> [flags...]
Available Commands:
board Arduino board commands.
compile Compiles Arduino sketches.
config Arduino Configuration Commands.
core Arduino Core operations.
help Help about any command
lib Arduino commands about libraries.
sketch Arduino CLI Sketch Commands.
upload Upload Arduino sketches.
version Shows version number of Arduino CLI.