How it all started…
One of the classes I’m currently taking requires the setup of the AVR Toolchain on my personal rig. The instruction given by the professor claims that it will only work on “Intel Macs”, which got me wondering if it really is the case. Is it possible to install it on an M1 Mac? Thus, the journey down the rabbit hole started… with me sinking hours into researching for solutions that only a few people encountered online…
Thanks to the great work of many GitHub community members, the installation of the AVR Toolchain and GDB has nearly been flawless. The bug that I spent the majority of my time on was patched by the time I am writing this article. Although there are still some tweakings needed.
The majority of the steps mentioned in this article come from the instruction manual distributed by Professor Philip Brisk and his TAs, with some tweaks added.
Steps
1. Installing Xcode developer tools
Use this command to make sure you have the newest version of Xcode developer tools installed:
xcode-select --install
2. Installing Homebrew
Install Homebrew if you don’t have one installed.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Note: Homebrew and MacPorts CANNOT coexist! It will cause errors later on!! Uninstall MacPorts if you have it!
3. Setting up avr-gcc, avr-gdb, simavr, avrdude
Add the repository containing AVR Toolchain for MacOS to Homebrew.brew tap osx-cross/avr
Note: Please install these packages in the order listed below, as there will be dependency problems if you don’t.
Install avr-gcc.brew install avr-gcc
Install avr-gdb.brew install avr-gdb
Install simavr.brew install simavr
Note: if you encounter error like this: “osx-cross/avr/simavr is a head-only formula”, use the following command instead: brew install --HEAD osx-cross/avr/simavr
Install avrdude.brew install avrdude
Note: Running AVRDUDE on macOS High Sierra or higher will require some extra steps due to extra layers of security in most recent macOS updates. Follow the steps of this Medium post to be able to program your microcontroller.
4. Installing GTKWave
Install GTKWave.brew tap homebrew/cask
brew cask install gtkwave
You’ll also need Perl’s Switch module to run GTKWave, install them using:sudo cpan install Switch
After installation, use this command to check where the Switch module is installed:perl -V:'installsitelib'
Note: If the output looks something like this: /usr/local/Cellar/perl/...
, copy it to /Library/Perl/5.*/
using this command:sudo cp /usr/local/Cellar/perl/5.*/lib/perl5/site_perl/5.*/Switch.pm /Library/Perl/5.*/
(5.* indicates your Perl version number, it may vary from machine to machine. Replace it as needed.)
You should now be able to run GTKWave using the following commands:/Applications/gtkwave.app/Contents/Resources/bin/gtkwave
If it does run, you can add the following line to ~/.bash_profile
file:export PATH="/Applications/gtkwave.app/Contents/Resources/bin/":"$PATH"
or you can add this line too:alias gtkwave=/Applications/gtkwave.app/Contents/Resources/bin/gtkwave
To apply the updated settings to your ~/.bash_profile
, run the following command:source ~/.bash_profile
5. Installing GNU Debugger (GDB)
Now, this is where the tweaking begins…
There are currently 2 versions of GDB that has been proven to work with M1 Mac, download them using the link below:
GNU Debugger 10.1
GNU Debugger 9.2
First, make sure you have Python3 installed using the following command:python3 --version
If it is not installed, use the following command to install it:brew install python
Make a folder to install GDB (Replace <gdb-installation-folder> with a name of your folder):mkdir <gdb-installation-folder>
Unzip the downloaded file into <gdb-installation-folder>.
Change directory into <gdb-installation-folder> and run the following commands:
(Replace <version number> with whatever version of GDB you downloaded)sudo ./gdb-<version number>/configure --target=avr --with-python=python3
sudo make
Now you will run into a compiler error that looks something like this:
Follow the warning and add
../.././gdb-9.2/sim/avr/../common/sim-core.c:75:2: error: implicitly declaring library function 'free' with type 'void (void *)' [-Werror,-Wimplicit-function-declaration]
free (tbd->free_buffer);
^
../.././gdb-9.2/sim/avr/../common/sim-core.c:75:2: note: include the header or explicitly provide a declaration for 'free'
2 warnings and 1 error generated.#include <stdlib.h>
to the beginning of the files mentioned in the error message and recompile.
Note: Similar errors will show up multiple times as there are multiple files missing #include <stdlib.h>
. Correct all of them until it successfully compiles.
6. DONE!
You’ve successfully set up the environment you needed! Enjoy! Leave a comment below if you encountered any problems!