Note: This is a reference question (but feel free to write answers of your own!)
I want to use the AVR tools directly -- no arduino-builder or arduino-cli. I would also like compilation and uploading to be as fast as reasonably possible
-
How fast? Can you put a metric on that? Uploading necessarily takes time, because it uses serial communications. Compiling, once invoked by the IDE is likely to be as fast as if you typed it on the command line. Of course, we would all like it to be faster than it currently is. But your requirement to be "fast" is rather un-specific.Nick Gammon– Nick Gammon ♦07/04/2022 10:05:37Commented Jul 4, 2022 at 10:05
-
Oh, I see this is a reference question. In that case see: arduino.stackexchange.com/questions/15893/…Nick Gammon– Nick Gammon ♦07/04/2022 10:08:57Commented Jul 4, 2022 at 10:08
1 Answer 1
- Create a new file called
Makefile
in your project directory. Populate it with the following contents:
TEMPDIR := $(shell mktemp -d)
all:
avr-g++ -DF_CPU=<CLK> -mmcu=<PARTNO> -fno-threadsafe-statics -O3 -flto -std=c++23 -isystem/usr/avr/include -lm -fuse-linker-plugin -Wl,--gc-sections *.cpp -o ${TEMPDIR}/a.elf
avr-objcopy -O ihex -R .eeprom ${TEMPDIR}/a.elf ${TEMPDIR}/a.hex
avrdude -V -p<PARTNO> -carduino -P/dev/tty<SERIAL_PORT> -b<BAUD> -D -Uflash:w:${TEMPDIR}/a.hex
rm ${TEMPDIR}/a.elf ${TEMPDIR}/a.hex
rm -d ${TEMPDIR}
- In the Arduino IDE, press Ctrl+, and enable verbose output during compilation and uploading.
- Start uploading with Ctrl+U. Replace the groups of angled brackets in Makefile (e.g.
<CLK>
) with the values in the Arduino IDE's build output. - Run
make
.
-lm
links in the AVR math library. *.cpp
refers to your c++ source files. -V
prevents avrdude from reading back the flash contents and verifying it, which saves time, but you may want to disable it while diagnosing problems.
-
2For a similar but more general solution, you may want to take a look at Sudar Muthu's Makefile. If you are not using the Arduino core library, you can remove a lot of cruft from the
g++
command line.Edgar Bonet– Edgar Bonet06/29/2022 15:30:31Commented Jun 29, 2022 at 15:30 -
1Checking the signature and verifying the flashed application are important safety features, I recommend to keep them to avoid long hours of searching errors.the busybee– the busybee06/30/2022 09:09:51Commented Jun 30, 2022 at 9:09
-
@edgar I just realized "a lot of cruft" refers to my answer, and not Sudar Muthu's Makefile. Fixed, thanks :)glibg10b– glibg10b07/04/2022 07:45:27Commented Jul 4, 2022 at 7:45
-
You could enhance your answer a lot, if you add explanations why each step is necessary, and what it contributes to the process. I'm missing for example that the intermediate step with the Arduino IDE is just for finding some decent values for clock and MCU, which could be entered as well manually, since you chose them in the IDE before. -- A great answer would also reason on each option of each tool. Especially as a beginner you need to learn what is important and what is not. Currently this is just one possible way thrown without additional information to unaware users.the busybee– the busybee07/05/2022 07:39:50Commented Jul 5, 2022 at 7:39