AVR Development on macOS

If you are anything like me you have a, sometimes, faulting love for your Mac. You have many good times together, sharing a common passion for well executed design and software. Other times, you just want to break it over your knee.

For me, the fault of the Mac became blatantly obvious, when I had to do some AVR programming for a course in Embedded Systems. Countless hours of searching for a one-click solution, like Atmel Studio for Windows. This however, simply doesn’t exist.

As my daily workflow is very dependent on macOS, switching between a virtual machine running Atmel Studio, and my Mac, simply doesn’t cut it for me.

There is, however, a rather simple solution to this problem — the AVR toolchain.

This workflow is utilising the incredible CLion IDE and the AVR toolchain to build and compile the executable file from your code and the trusted terminal to convert the executable file to a hex file, and flash to the file to the chips memory. 
I am using an Arduino Uno R3 as the programmer for the ATmega328p chip.

Firstly, you want to install the AVR toolchain:

brew install avr-gcc

This assumes that you have homebrew installed — if you dont, its about time!

Connect the chip to the programmer, and connect to your computer — then search for the connected serial port:

ls /dev/cu.*

You might find that multiple onces show up — if this is the case, run the check below and the different serial ports to see if which one your chip is connected to, and to check the status of the chip, makeing sure everything is running smoothly:

avrdude -c arduino -p atmega328p -P /dev/cu.XXX

A thing i noticed is, that if you are using a arduino with the CH340/CH341 chips, you have to specify the baud rate for the check to succeed — otherwise the programmer won't respond:

avrdude -c arduino -p atmega328p -P /dev/cu.XXX -b baudRateHere

This example uses the arduino as the programmer, the chip is an atmega328p and the serial port is /dev/cu.usbmodemFD121 — change this to fit your case.

The trick is to make CLion CMake configuration that supports the AVR toolchain. 
You now want to open CLion and change the default CMakeList.txt with what you see below (change things like the source files, the MCU, F_CPU and projectname to suit your project):

Now go to the settings (Cmd+,) -> Build, Execution, Deployment -> CMake and set build option output path to /bin — this is where CLion will place your executable file after building.

You now want to navigate to the /bin folder where you placed your executable file, and convert the file to .hex (change the name of the project to fit yours):

cd route_to_my_project_folder/bin
avr-objcopy -j .text -j .data -O ihex my_project.elf my_project.hex

Now that you have a hex file, its time to flash it to the chips memory — this will be done using AVRDude:

avrdude -c arduino -p atmega328p -P /dev/cu.XXX -Uflash:w:my_project.hex:a

Now that you have flashed your project to the chip, you can monitor the chip using screen:

screen /dev/cu.XXX

From Denmark, Michael Nissen did his CS degree at University of Aarhus and is now pursuing his BEng in Interaction Design at the University of Southern Denmark. You can check out some of his work here.

Like what you read? Give Michael Nissen a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.