How to (cross )compile AVR programs with Raspberry Pi - NicoHood/NicoHood.github.io GitHub Wiki

Extracted from the old Wordpress blog.

Compiling AVR programs with Windows is very tricky. Compiling makefiles with Linux environments is a lot easier. You want to compile AVR programs (like LUFA programs) with a Linux system but still want to stick to your Windows editors? Don't want to install Ubuntu on a 2nd PC/VM and you also got a Raspberry Pi lying around which might not be in use? Perfect!

This tutorial covers how to install the avr-gcc tools and how to exchange the code with your Windows PC. Optionally there are also notes for Ubuntu as well (works nearly the same).

Step 1: Installing avr-gcc

For compiling avr programs we need a compiler of course. It's called avr-gcc (but the package is called gcc-avr). Currently there are two mainly available versions of the compiler: 4.7.2 and 4.8.1

The newer version compiles code with better optimization which might be important for bootloaders which have to fit into a specific size of flash. Since 4.8.1 is not in the current stable repositories you have to do some extra steps in order to install 4.8.1 for raspberry pi. For Ubuntu just use the first line in the instructions to get 4.8.1 instead of 4.7.2. If you are a lazy guy you can start with 4.7.2 and update the packages later again.

# Ubuntu and 4.8.1 only! Add new avr-gcc 4.8.1 package
sudo add-apt-repository ppa:pmjdebruijn/gcc-avr-release

# reload repositories and check for the gcc-avr package
sudo apt-get update
sudo apt-cache search gcc-avr

# install avr-gcc 4.7.2
sudo apt-get install gcc-avr avr-libc

# check installed version of avr-gcc 
avr-gcc -v

Step 2: Testing compilation

Now we need to check if the compiling in general works. In this example I simply download one of my projects: HoodLoader2. You will notice that the flash size shrinks with the newer 4.8.1 version.

# download a project of your choice
wget https://github.com/NicoHood/HoodLoader2/archive/master.zip
unzip -g master.zip
cd HoodLoader2-master
sudo make

Step 3: Connecting your Pi to your Windows share

As said at the beginning, you can use the Raspberry Pi as cross-compiler. This might sound stupid, since it has not the fastest processor but its a fairly simple solution if you want to code with Windows.

For Laptop users it might be useful to connect your Raspberry directly to your PC instead of using WLAN on both devices. See this tutorial.

It is also highly recommended to use Putty for an SSH connection so you don't need a display on your Raspberry Pi.

So first we have to create a Windows share on your Windows PC. Right click the folder you want to share (in my case named 'Arduino') and click on properties->sharing->Advanced Sharing. Select 'Share this folder' and also click on 'permissions' and check all boxes under 'allow'. We have to do this so the Raspberry is allowed to save the new files. Keep in mind that its also allowed to delete files in this share. Make sure to backup the files in the share.

share

Now we have to mount this share with your Raspberry Pi. You can temporary mount the share or on each startup. In any case I recommend you to first test the mounting, then add it to the startup. To find your PC name right click Computer (or this PC) and click on properties. The name should be listed there. You have to enter the letters in upper case for the mount.

# create the folder where its mounted in
cd Desktop
mkdir Arduino
 
#Test mounting:
sudo mount -t cifs -o username=yourusername,password=yourpass,nounix,noserverino //YOUR-PC-NAME/Arduino Arduino
cd Arduino
ls
 
# run it automated at startup. If not connected this can cause a long timeout.
sudo nano /etc/fstab
//YOUR-PC-NAME/Arduino /home/pi/Desktop/Arduino cifs username=yourusername,password=yourpass,nounix,noserverino 0 0
 
# reboot the raspi and check if it works
sudo reboot

You are now able to edit the files with your editor of choice under Windows (for me it's Visual Studio) and compile it with a Raspberry Pi over Ethernet.

Links:

⚠️ **GitHub.com Fallback** ⚠️