带有 Arduino 的 Avr-GCC [英] Avr-GCC with Arduino

查看:50
本文介绍了带有 Arduino 的 Avr-GCC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Ubuntu 上用 C 编写我的 Arduino.我听说过 avr-gcc,但所有在线教程似乎都非常乏味,而且没有带有 Arduino 引导加载程序的 AVR 芯片的选项.谁能用更简单的方法帮助我在 Ubuntu 上安装 avr-gcc 并开始用 C 语言为 Arduino 编程?

How can I program my Arduino in C on Ubuntu. I've heard of avr-gcc but all online tutorials seem extremely tedious and don't have options for an AVR chip with the Arduino bootloader. Can anyone help me with an easier way to install avr-gcc on Ubuntu and get started programming in C for the Arduino?

推荐答案

我推荐以下编译命令行选项:

I recommend the following set of command line options for compiling:

avr-gcc -c
        -std=gnu99
        -Os
        -Wall
        -ffunction-sections -fdata-sections
        -mmcu=m328p
        -DF_CPU=16000000

用于链接:

avr-gcc -Os
        -mmcu=m328p
        -ffunction-sections -fdata-sections
        -Wl,--gc-sections

哪里……

  • -c 表示仅编译为目标文件,不链接"
  • -std=gnu99 表示我的代码符合 C99 并且我使用 GNU 扩展"
  • -Os 表示针对可执行文件大小而不是代码速度进行优化"
  • -Wall 表示打开(几乎)所有警告"
  • -ffunction-sections -fdata-sections-Wl,--gc-sections 优化所必需的
  • -mmcu=m328p 表示MCU 部件号为 ATmega 328P"
  • -DF_CPU=16000000 表示时钟频率为 16 MHz"(根据您的实际时钟频率调整)
  • -Wl,--gc-sections 表示告诉链接器删除未使用的函数和数据段"(这有助于减少代码大小).
  • -c means "compile to object file only, do not link"
  • -std=gnu99 means "My code conforms to C99 and I use GNU extensions"
  • -Os means "optimize for executable size rather than code speed"
  • -Wall means "turn on (almost) all warnings"
  • -ffunction-sections -fdata-sections is necessary for the -Wl,--gc-sections optimization
  • -mmcu=m328p means "the MCU part number is ATmega 328P"
  • -DF_CPU=16000000 means "the clock frequency is 16 MHz" (adjust for your actual clock frequency)
  • -Wl,--gc-sections means "tell the linker to drop unused function and data sections" (this helps reduce code size).

为了实际编译您的代码,您将首先发出带有仅编译标志"的 avr-gcc 命令,如下所示:

In order to actually compile your code, you would first issue the avr-gcc command with the "compile only flags", like this:

avr-gcc -c -std=gnu99 <etc.> MyProgram.c -o MyProgram.o

然后您将对所有源文件重复此操作.最后,您可以通过在链接模式下调用 AVR-GCC 将生成的目标文件链接在一起:

Then you would repeat this for all of your source files. Finally, you would link the resulting object files together by invoking AVR-GCC in link mode:

avr-gcc -Os <etc.> MyProgram.o SomeUtility.o -o TheExecutable.elf

这会生成一个 ELF 文件,您的 MCU 不能直接执行该文件.因此,您需要以 Intel Hex 格式从中提取有用的部分(原始机器代码):

This generates an ELF file, which isn't directly executable by your MCU. Thus, you'll need to extract the useful part (the raw machine code) from it in the Intel Hex format:

avr-objcopy -O ihex -R .eeprom TheExecutable.elf TheExecutable.ihex

最后,您将需要 AVRdude 将 hex 文件的内容上传到 MCU:

Finally, you will need AVRdude to upload the contents of the hex file to the MCU:

avrdude -C /path/to/avrdude.conf
        -p m328p
        -c PROGRAMMER_NAME
        -b 19600
        -P PORT_NAME
        -U flash:w:TheExecutable.ihex:i

哪里……

  • -C/path/to/avrdude.conf 表示将此文件用作配置文件"
  • -c PROGRAMMER_NAME 表示我正在使用 PROGRAMMER_NAME 类型的程序员"(您需要根据您使用的程序员类型自行填写).
  • -b 19600 是波特率(您可能需要根据您设置的波特率或已预编程到引导加载程序中的波特率进行调整)
  • -P PORT_NAME 表示编程器连接到端口 PORT_NAME".在 Linux 上,它通常类似于 /dev/ttyusbN,其中 N 是某个数字.
  • -U flash:w:TheExecutable.ihex:i 表示将 TheExecutable.ihex 的内容以 Intel Hex 格式写入闪存".
  • -C /path/to/avrdude.conf means "use this file as the configuration file"
  • -c PROGRAMMER_NAME means "I am using a programmer of type PROGRAMMER_NAME" (you will need to fill this in yourself depending on what kind of programmer you use).
  • -b 19600 is the baud rate (you may need to adjust this depending on the baud rate you set or have pre-programmed into the bootloader)
  • -P PORT_NAME means "the programmer is connected to port PORT_NAME". On Linux, it will most often be something like /dev/ttyusbN, where N is some number.
  • -U flash:w:TheExecutable.ihex:i means "write to the Flash memory the contents of TheExecutable.ihex which is in Intel Hex format".

这篇关于带有 Arduino 的 Avr-GCC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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