使用Arduino库获取常规AVR代码

Adw*_*are 19 avr arduino libraries

我有一个Arduino Uno和一个Linux环境.尽管Arduino IDE非常棒,但是如果出现问题,它并没有给我太多的帮助.它也变得极其缓慢并且有时会停止与芯片通信.

另一种方法是使用AVR-GCC/g ++和AVRDude工具链对我的Arduino Uno进行编码.但是,在使用简单的文本编辑器和命令行工具进行编程时,我非常希望能够访问与Arduino相同的功能(如Serial,digitalWrite等).

所以我试图找到头文件,其中定义了所有函数,即"Arduino.h" /usr/share/arduino/hardware/arduino/cores/arduino.

我用它来编译(使用makefile来做这个)

avr-gcc -mmcu=atmega328p -I. -gdwarf-2 -DF_CPU=16000000UL -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wstrict-prototypes -Wa,-adhlns=testarduino.o -I/usr/share/arduino/hardware/arduino/cores/arduino -I/usr/share/arduino/hardware/arduino/variants/standard -std=gnu99 -MMD -MP -MF .dep/testarduino.elf.d testarduino.o --output testarduino.elf -Wl,-Map=testarduino.map,--cref     -lm
Run Code Online (Sandbox Code Playgroud)

编译这段代码:

#include "Arduino.h"

int main(void)
{
    init();
    pinMode(13,OUTPUT);
    while(1){
    digitalWrite(13,HIGH);
    delay(1000);
    digitalWrite(13,LOW);
    delay(1000);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是当我尝试编译时(似乎未定义引用'xxxxxxxxx')似乎没有识别出任何函数.那么需要包含哪些确切的头文件以确保我可以使用相同的功能?还是我错过了别的什么?

Jel*_*Cat 21

如何获得定义 undefined reference

该消息undefined reference to 'xxxxxxxxx'意味着链接器无法找到相关函数的实际定义(而不仅仅是声明).

当链接器找不到函数的定义时,该定义通常可以在lib文件(例如*.so或*.dll)或源文件(例如*.cpp或*.c)中找到.对于你的Arduino,你需要告诉avr-gcc编译和链接Arduino*.cpp文件.您需要Arduino核心的所有源文件.

ARDCOREDIR = <my arduino folder>/hardware/arduino/cores/arduino
Run Code Online (Sandbox Code Playgroud)

Sudar给出了很好的建议,主张使用AVR的Makefile,它配置为包含Arduino函数定义.(我将链接到我的并在下面解释.)

在我的makefile中,我按如下方式处理了这个操作:

# CPP_SOURCE_FILES is a previously-defined variable, holding my project's source files
CPP_SOURCE_FILES += $(filter-out $(ARDCOREDIR)/main.cpp, $(wildcard $(ARDCOREDIR)/*.cpp))
Run Code Online (Sandbox Code Playgroud)

注意我省略了main.cpp文件main(),因为Arduino main.cpp文件定义了,我希望自己在我自己的项目文件中定义它.

必要的头文件

当然,您还必须为avdu-gcc传递arduino标头的include标志:

-I$(ARDCOREDIR)
Run Code Online (Sandbox Code Playgroud)

并且您必须在实际项目代码中包含Ardhuino.h文件:

#include <Arduino.h>
Run Code Online (Sandbox Code Playgroud)

Arduino.h头文件将包含所有其他Arduino头文件,包括引脚文件,其中包含Arduino引脚的定义.不同的Arduinos必须有不同的引脚输出文件,因此请确定您使用的是哪种类型,并告诉avr-gcc包含包含相应引脚文件的目录.如果您使用的是标准Arduino(UNO),请使用以下命令:

-I$(ARDCOREDIR)/../../variants/standard
Run Code Online (Sandbox Code Playgroud)

我的Makefile

您的构建过程不仅需要实现上述几条指令,还需要知道如何将.hex文件编程到您的Arduino(或AVR)上.因此,使用makefile是个好主意.

我不喜欢为每个项目重建我的makefile,所以我在我的机器上只有一个基本的makefile,每个项目都有自己的,非常短的makefile,它调用include包含基本的makefile.你可以在这里找到我的基本makefile.

我机器上单个项目的简短(非基础)Makefile示例:

# This is the name of the file that shall be created. (It is also the name of my primary source file, without the file extension.)
TARGET = temp

# create a variable that only pertains to this project
MY_OWN_LIBRARY_DIR = /usr/home/MJ/Arduino/libraries/mj_midi

# "EXTRAINCDIRS" is a variable used by the base makefile. The base makefile creates a -I compiler flag for every item in the "EXTRAINCDIRS" list.
EXTRAINCDIRS = $(MY_OWN_LIBRARY_DIR)

# specify *.c source files pertaining to my project
SRC =

# specify *.cpp source files pertaining to my project
PSRC = temp.cpp $(MY_OWN_LIBRARY_DIR)/midi-listener.cpp $(MY_OWN_LIBRARY_DIR)/midi-song.cpp

# specify additional (non-core) Arduino libraries to include
ARDLIBS = SoftwareSerial

# include my base makefile
include /d/dev/avr/Makefile.base
Run Code Online (Sandbox Code Playgroud)

注意:如果您要使用我的基本makefile,请确保将变量ARDDIR(显示在文件顶部附近)设置为指向计算机上的Arduino目录.


Sud*_*dar 5

为Arduino使用了一个makefile,它允许我同时使用用户和系统库.您不必使用Arduino IDE,但同时可以使用它提供的库.