-1
\$\begingroup\$

I recently got interested in arduino again after an fun experience at work. I found an old arduino kit for a class from college and started to tinker with it. I decided to bypass the arduino and work with the microcontroller directly to learn C and about electronics in general.

One tutorial I looked at uses the below makefile code to compile the code and then flash it onto the MCU using the arduino.

Can someone explain what each piece does and if any of the code is unnecessary? Also, I am a bit confused on the flashing part because I have seen that you need a programmer (or use another arduino to flash onto the 2nd arduino) but I only used the one arduino I have and it still worked in making the built in LED blink.

Feel free to recommend learning material and resources.

default: avr-gcc -Os -DF_CPU=16000000UL -mmcu=atmega328p -c -o led.o led.c avr-gcc -o led.bin led.o avr-objcopy -O ihex -R .eeprom led.bin led.hex sudo avrdude -F -V -c arduino -p ATMEGA328P -P /dev/ttyACM0 -b 115200 -U flash:w:led.hex 
\$\endgroup\$
3
  • \$\begingroup\$ gnu.org/software/make/manual/html_node/index.html \$\endgroup\$ Commented Apr 4 at 3:46
  • \$\begingroup\$ The question isn't about electronics per se, but I can't figure out whether you are interested in Makefiles, as that Makefile is just used as a script to run those commands. Or is the question about using avr-gcc toolchain to compile and link the project and export a .hex file? Or using avrdude to program the .hex to your chip? Or why it works with your Mega328P which still has an Arduino bootloader? \$\endgroup\$ Commented Apr 4 at 5:21
  • 1
    \$\begingroup\$ This Makefile actually is not more than a primitive shell script ("batch"). The provided commands have nothing to do with the tool "make". \$\endgroup\$ Commented Apr 4 at 5:50

1 Answer 1

1
\$\begingroup\$

Comments indicate what each line does:

default: # Compile the file but (-c flag) do not link. Output file is led.o. avr-gcc -Os -DF_CPU=16000000UL -mmcu=atmega328p -c -o led.o led.c # Linking step avr-gcc -o led.bin led.o # Copy the relevant information into a file in a format that # the Arduino will be able to run avr-objcopy -O ihex -R .eeprom led.bin led.hex # Flash over USB (typically the Arduino has already been flashed # at the factory with a programmer which allows the use of # USB to flash) sudo avrdude -F -V -c arduino -p ATMEGA328P -P /dev/ttyACM0 -b 115200 -U flash:w:led.hex 

A useful source, though not exactly covering the above commands, might be: A simple project (Stanford University).

\$\endgroup\$
2
  • 1
    \$\begingroup\$ The programmer, in this case, is what is termed a Bootloader. The Bootloader (pre-flashed into the Arduino either by you, using another arduino as a programmer, or at the factory) looks for a code/signal at startup, and if that code is detected, it flashes what is sent to it (via USB) into memory. As seen above, avrdude is the program that is used to do this. \$\endgroup\$ Commented Apr 4 at 5:22
  • \$\begingroup\$ "file in a format that the Arduino will be able to run" - the Arduino knows nothing of file formats. In fact if anything the .bin file GCC produces - a raw binary - is what the AVR will need to load from its flash. The Intel Hex format file is made as it's a cleaner way of storing the binary data in a human readable form with extra meta information which is used by the avrdude program not the Arduni.. \$\endgroup\$ Commented Apr 4 at 7:36

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.