Atmel XMEGA-A1 Xplained hooked up to a MacBook through a JTAGICE mkII

Developing for AVR with JetBrains CLion on Mac OS

Previously I used Atmel Studio 7 to do AVR development. This integrated development environment is unsurpassed in terms of ease of use and features for AVR development. However, it is Windows only. I grew tired of the hassle with virtual machines and dual boot setups as my working device as a Mac running Mac OS 10.12. I already used some JetBrains products for C/C++ and PHP development so I wanted to try to get the whole AVR toolchain working on Mac OS in combination with JetBrains CLion.

To serve as a suitable alternative to Atmel Studio I need both compilation, uploading and debugging to integrate nicely with CLion. Furthermore, I wanted to be able to use the Atmel Software Framework to have access to an enormous resource of services, drivers and examples.

Prerequisites

To keep things concise I assume that:

  1. Homebrew is installed and working.
  2. A recent version of CLion (2016.3 at the time of writing) is installed and working.
  3. Build tools are available (either through XCode or another way). This because some package will be compiled (for Mac OS) ofrom source by Homebrew.
  4. Working GIT

Installing AVR Tools

Homebrew will install the required tools and take care of building and installing. Let’s check if our environment is ready to compile sources:

This will check whether command line developer tools are installed. Make sure that this is confirmed or otherwise check your XCode installation.

Now the AVR tools are to be installed. This relies on Homebrew formulae provided by the osx-cross repository. So, we will tap into a new repository of Homebrew formulae and install the required packages. It might take some time to compile everything.

Atmel Software Framework

I want to make use of the extensive library of source-code and application examples called Atmel Software Framework. Download the standalone ASF framework to for example ~/Downloads.

The ASF source is now available. For details about its structure, refer to the ASF user guide. Some ASF code will be used in the example project below but without detailed explanation of its purpose or usage.

Configuring CLion

CLion uses CMake as its central build system. CMake enables cross-platform development by generating platform specific build files using a universal CMake configuration. We will use a CMake template that simplifies a lot of our work. Next, CLion need to be configured to use the right compiler and debugger binaries.

First, create a new CLion project.

CMake

To configure CMake, a copy of mkleemann/cmake-avr will be obtained. This repository contains CMake files for AVR development.

It’s mainly about the file generic-gcc-avr.cmake. We could either keep it centrally stored in the local copy we just created or copy it to our CLion project.

Next, configure CMake in CLion by going to CLion > Preferences > Build, Execution, Deployment > CMake

Ensure that at least a ‘Debug’ profile is available. In CMake options, enter the path to the Toolchain file we just obtained. For example:

Configuring CMake in CLion

Now that CMake is aware of the toolchain file, the CMakeLists.txt is to be configured. We will be using CMakeLists.txt.sample (available in the cmake-avr repo) as a starting point. Copy the template right into CLion’s CMakeLists.txt. Let’s go through its relevant sections step by step.

Setting tools, parts and fuses

Somewhere at the beginning of the file some configuration variables are set.

Select the appropriate programmer. Check all available programmers by running  avrdude -c ?  in your terminal. Select the port (for example /dev/ppi0 or usb).

Next, select the MCU. Run avrdude -p ?  to find out supported devices. The setting for the fuses can be found out by either reading the manual or using an online tool like fusecalc.

 

Set the project name. The ASF library contains some Assembly files so adding ASM support is useful in this case.

Set the AVR MCU speed. For example, 32MHz:

Libaries and Source-files

Optionally, set a variable containing the path to the ASF installation. Such a fixed path is not really portable of course but for simplicity it will be used anyhow:

Add a variable containing the list of source files. The actual list really depends on your project. You could start off with a bare project containing only a main.c file. When using ASF libraries, consult the quick starts and examples to see what files are to be included.

If you are compiling ASM files (.s extension) you have to indicate this to CMake:

Add the relevant include-directories to your project. For example:

The gcc/config.mk files supplied with the ASF example projects contains information about what to add to the project to get it up and running. For example, the config.mk file for the XMEGA example application xmega-inertial-demo can be found in:

Adding the executable

Add CMake targets for an AVR executable called for example ‘my_avr_project’. Use the ${SOURCE_FILES} variable to indicate the sources.

Tuning

Tuning the CMakeLists.txt file is really project dependent. You might want to add a ‘define’ containing the BOARD definition for use with ASF  add_definitions("-DBOARD=XMEGA_A3BU_XPLAINED") . You also might want to comment out the -pendantic option to make the compiler a bit less stressful.

Debugging the Project

Debugging only makes sense if you are using a programming device that is capable of debugging is well. Examples include the JTAGICE mkII and Dragon ISP. It is possible however to simulate an AVR device with the simavr tool. This, however, is out of the scope of this guide.

Debugging tools

To support debugging, the debug tool avr-gdb (GNU debugger) is required as well as the AVaRICE program. AVaRICE interfaces the GNU debugger with the physical debugging device (Atmel JTAGICE mkII for example). If you do not have a debugging enabled device, you can skip these steps.

Version 2.13 does not support the Atmel-ICE yet. However, it seems that the development version does. Downloading the source instead of the archive and configure/make should work. See AVaRICE website for sourcecode

Using AVR-GDB binary in CLion

CLion needs to be instructed what application it should use to debug the project. The built-in debugger is not suitable for debugging embedded hardware.

Open CLion > Preferences > Build, Execution, Deployment > Toolchains

Select /usr/local/bin/avr-gdb as Custom GDB debugger. All checks should pass.

Configuring custom GDB debugger in CLion

GDB Remote Debug Configuration

To connect to a specific GDB server (AVaRICE), a GDB Remote Debug configuration has to be added. Go to project configurations (upper right corner or CLion) and click Edit Configurations…. Now use the ‘+’ button to add a new GDB Remote Debug configuration. Select tcp:localhost:4242 as ‘target remote’ argument (or whatever port you are using). Select the Symbol file (.elf) that contains debugging symbols. Make sure you do not select the Release build because it does not contain any debug information.

Adding a GDB Remote Debug configuration that connects to the AVaRICE server

Actual debugging

To start debugging you have to:

  1. Start the AVaRICE server
  2. Connect AVR-GDB in CLion
  3. Set some breakpoints and debug

Start the AVaRICE server with options relevant for your situation. The following example is for an XMEGA using JTAG with a JTAGICE mkII debugger connected through USB and an XMega256A3 micro-controller. The server will be started at port 4242.

Now, select the GDB Avarice configuration in CLion and click the Debug icon.

Finally, add a breakpoint at a suitable point in code by clicking in the left-margin of the code. Let’s start debugging!



Frequently Asked Questions (FAQ)

The Frequently Asked Questions section is to be extended…

1. Error: junk at end of line, first unrecognized character is `/’

Typical errors include:

The ASM files contain directives that have to be processed by the preprocessor before compiling the ASM itself.

Solution: 

The preprocessor directives will be parsed in ASM files when their extension is .S (capital S instead of s). Make sure that in the set_property call the file extension is uppercase. Because of the default case insensitive filesystem the actual casing of the file is not of importance.

2. No source file named /path/to/project/file.c. when debugging

This happens when there are no debug symbols loaded into the executable. (my_avr_project.elf)

Solution:

Make sure that:

  1. You configured to use a Debug build in CLion or CMake.
  2. You have selected the .elf executable that resides in the debug build folder (instead of the release executables).