Developing in C for the ATmega328P: Setup Introduction

6 minute read

Updated: Where I describe the optional steps to setup the Standard C toolchain for the ATmega328P on Windows, Linux, or macOS using a command line-based approach.

Update

I’ve determined there is a method of using the Arduino legacy IDE (1.8.x) tool chain instead of installing the GNU tool chain (which is described on the operating system pages). If you are serious about working with the AVR family of microcontrollers, I continue to believe installing the GNU tool chain appropriate to your operating system. This will provide the latest tools and will allow you to control your tools as compared to Arduino.

That said, using the Arduino tool chain is an easy introduction to how to use the command line to create applications for an AVR microcontroller. To do so perform the following steps:

  1. Use the command line as stated below
  2. Install the Arduino IDE
  3. Use git clone the AVR_C rep
  4. Add env.make per the README file in the repository
  5. Change the TOOLCHAIN = per the repository

Command Line Required

Executing the tool chain and performing this work, will require you to work in your operating system’s command line interface. If you aren’t familar with the command line, it need not be a hindrance, there are only a few commands you will need to execute. I recommend reviewing these command line tutorials before you start. Practice a little bit prior to performing the commands below.

As a guide, here are the commands that I believe you will need to know and understand:

# present working directory - find out where you are 
$ pwd
/Users/lkoepsel/Sites/wellys.com
# list - find out what is in the current directory
$ ls
data        deploy.sh   layouts     public      resources   static      themes
# make directory - create a new directory
$ mkdir temp
# change directory - in this case go "up" one level
$ cd ..
$ pwd
/Users/lkoepsel/Sites

Introduction

Setting up the tool chain for C for the AVR family of processors isn’t difficult, if you are using a Linux-based operating system or one that is quite similar to Linux, i.e; Unix-based, macOS or Windows Subsystem for Linux (WSL). With this in mind, this tutorial will use the following approach for installing the tool chain required for developing in Standard C for the Arduino Uno (AVR ATmega328P):

  • Linux - use apt as the package manager and use the command line interface (Console)
  • macOS - install homebrew, a Linux-like package manager and use the command line interface (Terminal)
  • Windows 10 using WSL - install Windows Subsystem for Linux (WSL1) and use WSL as the command line interface. This approach is almost identical to Linux, above.
  • Windows 10/11 using a third party download - install a special tool chain packaged designed for Windows

To test the tool chain is working, I’ll use a simple program from Freedom Embedded. The program is basic and doesn’t require any other additional libraries (outside of the standard C library, avr-libc) The goal is exactly the same as that of the Arduino, demonstrate one can get the LED to blink.

Once, its confirmed the tool chain is working, I’ll use a Makefile from Elliot William’s book (GitHub site) (the book is recommended if this topic is interesting to you.) which works extremely well. You might have to make slight changes for your specific setup, however, the Makefile offers an easy and complete automation tool.

In summary the steps will be:

  1. Install the tool chain.
  2. Test the tool chain using specific commands for each step.
  3. Automate using a Makefile, so that the compile/link/load step is one command.

Before You Start, Install the Arduino IDE

If it isn’t already installed, install the Arduino legacy (1.8.x) IDE, as we’ll want to use it to confirm or ensure the serial ports are working properly with the Arduino Uno. We can use it to develop code and not use the C++ Arduino Software Framework.

  1. Go to Arduino Software
  2. Scroll halfway down the page to Legacy IDE (1.8.X), and download the appropriate version for you operating system. For Windows, I recommend “Windows Win 7 and newer”, NOT the “Windows app version”! Additional instructions for installing for Windows, macOS or Linux.

CHECK AND CONFIRM

Let’s use the Arduino Uno and the Arduino IDE to confirm the specific port we will be using as well as ensure we have a good physical connection between our Windows system and the Arduino.

Connect your Arduino Uno to your computer and open the Arduino IDE. Go to Tools -> Port -> ???(Arduino Uno) to confirm the port for the Uno. In Windows, it was COM3, in macOS, it was /dev/cu.usbmodem3101 and in Linux, it was /dev/ttyACM0. Write down this port name as you will need it for the step later using the averdude command.

Go to File -> Examples -> 01. Basics -> Blink to open the Blink sketch. Click on Upload and ensure the file is uploaded to your Uno and the built-in LED is blinking. If everything is working, close the Arduino IDE. Note: The IDE must be closed for the command line application avrdude to access the port. If you have connections issues with avrdude connecting, confirm the IDE is not running.

Changes Required to Use Arduino Tool Chain

If you are beginning programming, follow these steps to using the Arduino IDE toolchain on the AVR_C repository or Labs 10C repository.

The Makefile has a variable TOOLCHAIN = , which allows you to use either, a system-installed toolchain (default) or the toolchain installed by the legacy Arduino (1.8.x) IDE.

In order to use the latter, perform the following steps:

  1. Add arduino to the TOOLCHAIN variable as in TOOLCHAIN = arduino
  2. Uncomment, by removing the leading # (this is pound sign AND the following space) from the lines BIN = ... and AVRDUDECONF = ... lines appropriate to your operating system
# Change the line below to TOOLCHAIN = arduino, if you want to use the Arduino IDE tools
# And uncomment the appropriate block of code based on your OS
TOOLCHAIN = 
ifeq ($(TOOLCHAIN), arduino)
	# macOS lines, remove both the # and the following space
	# BIN = /Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/
	# AVRDUDECONF = -C /Applications/Arduino.app/Contents/Java/hardware/arduino/avr/bootloaders/gemma/avrdude.conf

	# Windows lines, remove both the # and the following space
	# BIN = 'C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\'
	# AVRDUDECONF = '-CC:\Program Files (x86)\Arduino\hardware\arduino\avr\bootloaders\gemma\avrdude.conf'	
else
	BIN =
	AVRDUDECONF = 
endif

The BIN = ... and AVRDUDECONF = ... lines must be indented only with a tab at the beginning of the line for make to accept the lines.

(OPTIONAL) Next Steps: Specific Operating System Installations

This set of steps are for installing the newer GNU version of the avr tools. If you are serious about development, I recommend following the step for your operating system. If you are beginning programming, follow the steps to using the Arduino IDE toolchain on the AVR_C repository.

Comments powered by Talkyard.