Using CLion for AVR (ATmega, Arduino) development

Andreas Haufler
1 min readJun 1, 2016

--

Jetbrains builds the greatest IDEs. Period. Therefore I was very pleased to learn, that there is a new C / C++ IDE named CLion. I already used it for a small tool running on the Raspi (raspicsp).

Now since AVRStudio only runs on Windows not OSX and the Arduino GUI is great but somewhat limited for larger projects, I tried to come up with a CMAKE configuration (this is the build system used by CLion) to support the AVR toolchain.

And what can I say. That was quite easy to do. Essentially you only tell CMAKE to use another compiler and maybe some additional CFLAGS and it pretty much works out of the box. Once CMAKE ran, CLion will happily support you with auto completion and all the hints and inspections it has.

Here’s an example snippet for an ATMega328 (an Arduino actually):

SET(MCU "atmega328p")
SET(F_CPU "16000000")
SET(CMAKE_SYSTEM_NAME Generic)
# For some reason, these paths have to be absolute, otherwise
# CLion won't be able to find headers etc.
SET(CMAKE_C_COMPILER /usr/local/Cellar/avr-gcc/4.9.3/bin/avr-gcc)
SET(CMAKE_CXX_COMPILER /usr/local/Cellar/avr-gcc/4.9.3/bin/avr-g++)

SET(CMAKE_C_FLAGS "-mmcu=${MCU} -DF_CPU=${F_CPU} -Os")
SET(CMAKE_C_LINK_FLAGS "-mmcu=${MCU}")

SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

Once the .elf file is build, you can convert that into a hex file using:

avr-objcopy -j .text -j .data -O ihex my_project.elf my_project.hex

And upload it to an Arduino using (simply copy the command from the Arduino console):

avrdude -C/../avrdude.conf -v -patmega328p -carduino -P/dev/my_com_port -b57600 -D -Uflash:w:/...my_project.hex:i

--

--

Andreas Haufler

I'm a passionate software developer interested in language design, database and web technologies