Converting C code into machine readable .hex

Method 1 : Arduino CLI (unverified)

** Arduino CLI pulling the Arduino core and libraries into proj path**

  1. Install the Arduino CLI

  2. Create a directory and place authored c files. Ex) ~/MyProj/src/

  3. Ensure that the arduino-cli core libraries are up to date w/ command and installed (use only once)

    arduino-cli core update-index && arduino-cli core install arduino:avr

  4. Compile and export hex

    arduino-cli compile --fqbn arduino:avr:uno --export-binaries MyProj

Method 2 : Manual Method using avr-gcc

  1. Locate the Arduino core includes and compiled core archive ( or compile yourself)

    /.arduino15/packages/arduino/hardware/avr/1.8.6/cores/arduino

  2. Copmpile your code (Ex file.c)

    avr-gcc -c -mmcu=atmega328p -DF_CPU=16000000UL -I/path/to/cores/arduino -I/path/to/variants/standard file.c -o file.o

  3. Link core libs and objects

    avr-gcc -mmcu=atmega328p file.o /path/to/core.a -o file.elf

  4. Produce hex file

    avr-objcopy -O ihex -R .eeprom file.elf file.hex

Flashing Hardware using AVR Programmer

  1. Upload hex file to the arduino using AVRDUDE. The hex will likely be in MyProj/build/ with the name like MyProj.ino.hex or MyProj.hex.

    avrdude -c usbtiny -p atmega328p -U flash:w:MyProj.hex

    • -c usbtiny : selecting programmer
    • -p atmega328p : selecting device
    • -U flash:w:PyProj.hex : all memory options allowed, do flash write