将 Arduino 库用于常规 AVR 代码 [英] Using the Arduino libraries for regular AVR code

查看:47
本文介绍了将 Arduino 库用于常规 AVR 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an Arduino Uno and a Linux environment. Though the Arduino IDE is great and all, however it doesn't give me much inputs if something goes wrong. And it also becomes excruciatingly slow and stops communicating with the chip sometimes.

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

The alternative is to code my Arduino Uno using the AVR-GCC/g++ and AVRDude toolchain. However, I would very much like to have access to the same functions as in Arduino (like Serial, digitalWrite, etc.) while programming using a simple text editor and command line tools.

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

So I tried to find the header file with all the functions defined ie "Arduino.h" in /usr/share/arduino/hardware/arduino/cores/arduino.

我用它来编译(使用 makefile 来做到这一点)

I used this for compiling (using a makefile to do this)

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

编译此代码:

#include "Arduino.h"

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

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

But none of the functions seem to be identified when I try to compile (saying undefined reference to 'xxxxxxxxx'). So what is the exact header file that needs to be included to make sure I can use the same functions? Or is there something else I missed?

推荐答案

如何获取未定义引用

的定义

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

How to get the definitions for undefined reference

The message undefined reference to 'xxxxxxxxx' means that the linker can't find the actual definition (not just declaration) for the function in question.

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

When the linker can't find a definition for a function, that definition is typically to be found in a lib file (e.g *.so or *.dll) or in a source file (e.g. *.cpp or *.c). For your Arduino, you need to tell avr-gcc to compile and link the Arduino *.cpp files. You need all the source files from the Arduino core.

ARDCOREDIR = <my arduino folder>/hardware/arduino/cores/arduino

Sudar 提出了很好的建议,提倡对 AVR 使用 Makefile,该文件配置为包含 Arduino 功能定义.(我将链接到我的并在下面解释.)

Sudar gave good advice, advocating the use of a Makefile for AVRs which is configured to include the Arduino function definitions. (I will link to mine and explain it below.)

在我的makefile中,我是这样处理这个操作的:

In my makefile, I handled this operation as follows:

# 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))

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

Observe that I omitted the file main.cpp because the Arduino main.cpp file defines main(), and I wish to define it myself in my own project files.

当然,您还必须向 avr-gcc 传递一个包含 arduino 标头的标志:

Of course, you must also pass avr-gcc an include flag for the arduino headers:

-I$(ARDCOREDIR)

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

And you must include the Ardhuino.h file in your actual project code:

#include <Arduino.h>

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

The Arduino.h header file will include all of the other Arduino header files, including the pinout file, which holds definitions for the Arduino pins. Different Arduinos must have different pinout files, so identify which type you are using, and tell avr-gcc to include the directory containing the appropriate pinout file. If you are using a standard Arduino (UNO), use the following:

-I$(ARDCOREDIR)/../../variants/standard

我的 Makefile

您的构建过程不仅需要执行上述几条指令,还需要知道如何制作应编程到您的 Arduino(或 AVR)上的 .hex 文件.因此,最好使用生成文件.

My Makefile

Not only does your build process need to implement the several instructions described above, it needs to know how to make the .hex file that shall be programmed onto your Arduino (or AVR). Therefore, it is a good idea to use a makefile.

我不喜欢为每个项目重建我的 makefile,所以我的机器上只有一个基本的 makefile,每个项目都有自己的非常短的 makefile,它调用 include 来包含基础生成文件.您可以在此处找到我的基本 makefile.

I don't like to rebuild my makefile for each project, so I just have one base makefile on my machine, and each project has its own, very short makefile which calls include to include the base makefile. You can find my base makefile here.

我机器上单个项目的简短(非基础)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

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

NB: If you're going to use my base makefile, make sure that you set the variable ARDDIR (which appears near the top of the file) to point to the Arduino directory on your machine.

这篇关于将 Arduino 库用于常规 AVR 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆