Developing in C for the ATmega328P: Raspberry Pi and VS Code Setup

6 minute read

Where I setup the Standard C toolchain for the ATmega328P on the Raspberry Pi, however I use VS Code on my Mac (or Windows) for development and connect via SSH to the Raspberry Pi.

Introduction

This is the best setup, period. Why? It provides the most simple tool chain installation, a stable, known platform and enables hardware debugging, should you want to do so.

Linux has always been the preferred platform for open-source embedded development, as the Standard C tools are native to Linux. macOS is second, as its Unix bloodline, enables it to resemble a Linux system quite easily. Windows is a distant third, as the hoops to jump through to build the tool chain are a pain (Note: WSL can’t easily interact with the serial port).

In this entry, I’ll use the one of the least expensive Linux systems, the Raspberry Pi (RPi) and my Mac as the complete development system. While I might continue to develop on the Mac, in future entries, I’ll use this platform setup as its the most consistent and stable platform.

As I stated earlier, I will use VS Code on my Mac for development. Given the RPi is so great, why continue to use the Mac for development? The graphics are better on the Mac, I have other tools I might want to use on my Mac for the development process and the speed of the Mac, outside of AVR development, is significantly faster. The same point can be made of a Windows PC, while the RPi is great for command line-based C development, it is sorely lacking in browser or any GUI application performance.

Overview and Steps

My setup is to use VS Code SSH extension on my Mac to connect to the RPi. I connect my Uno to the RPi and not my Mac. And since I’m on Linux, I can install Bloom and have a phenomenal hardware debug platform.

  1. Setup the Raspberry Pi using the Raspberry Pi OS (Bookworm)
  2. On the RPi add the standard C tool chain and obtain the AVR C repository
  3. On my Mac, open VS Code, connect to the RPi and begin development

1. Raspberry Pi Setup

Create bootable image

For this and subsequent entries, I’ll use a Raspberry Pi 4 with 4GB of memory. (Note: I have tested this setup on a Raspberry Pi 3B with 1GB of memory ($35!) and did not notice appreciable performance issues.) I use the Raspberry Pi Imager software and install the 64-bit Lite version of the Raspberry Pi OS (see image).

Raspberry Pi Imager -> Raspberry Pi OS (other) -> Raspberry Pi OS Lite (64-bit)

Raspberry Pi Imager -> Raspberry Pi OS (other) -> Raspberry Pi OS Lite (64-bit)

Large Version to see detail

A great feature of Pi Imager, is that it provides the ability to add a WiFi password, authentication and a public key. I highly recommend adding a public key in the second setup screen. Key steps below:

  1. Set hostname (and remember it). You will use it to connect to the RPi.

  2. While I strongly recommend using the public key authentication, you will still need a Username and Password, be sure to write these down.

  3. Configure wireless LAN and locale settings according as appropriate.

  4. Next -> Edit Settings -> Services -> Allow public-key authentication only If you don’t already have one, click on the SSH-KEYGEN button to generate one.

  5. After you have set all of your desired settings, be sure to click Save at the bottom.

    • Then “Yes” to “Would you like to apply OS customisation settings?
    • Then “Yes” to “All existing data on the …SDXC Reader Media will be erasedNOTE: BE SURE THIS ISN’T YOUR MAIN DRIVE!
    • Enter your password and Pi Imager will create a bootable SD (or USB drive) for you

Initial Linux setup

Once the SD card has been written, put the card into the RPi, plug in an Ethernet cable (preferable) (or you connect via wireless), connect power to the RPi and wait for about 2 minutes. This will allow the RPi to boot, perform initial housekeeping then wait for you to login.

In your Terminal(macOS) or Command Prompt(Windows) window (from here on, I’ll simply say “in your command line interface (CLI)):

ssh hostname.local
# enter yes to "Are you sure you want to..."
# update the latest, latest software
sudo apt update && sudo apt upgrade -y
# if you have additional desired configuration (optional)
sudo raspi-config
# You no longer need to to A1: Expand Filesystem, this is performed on first boot

Once these steps have been performed, congratulations, you have a fully functioning Linux box! Now let’s make it an AVR C development machine!

Linux Tool Chain Installation

For the Linux installation, it can’t get much easier. In your CLI:

# install C tool chain
sudo apt install gcc-avr binutils-avr gdb-avr avr-libc avrdude make git tio -y
avr-gcc --version && make --version && git --version && tio --version && avrdude
# the second line above, will print out a lot of text, look for lines similar to these
avr-gcc (GCC) 5.4.0
...
GNU Make 4.3
...
git version 2.39.2
tio v2.5
avrdude version 7.1, URL: <https://github.com/avrdudes/avrdude>

If the lines above, appear the following tools are installed:

  • avr-gcc - the Standard C tool chain for compiling and linking code for the AVR family. Version 5.4 is very old, however, its stable and it works.
  • Make - a tool used to automate tasks for developing code
  • git - version control used for AVR_C
  • tio - a serial program which runs on the command line
  • avrdude - a tool used to upload code to the flash memory on an AVR microcontroller board

Public Key (on Raspberry Pi) for GitHub

Create Public Key

In your CLI:

# add public key for Github
ssh-keygen -t ed25519 -C "lkoepsel@wellys.com" && eval "$(ssh-agent -s)" && ssh-add ~/.ssh/id_ed25519 && cat ~/.ssh/id_ed25519.pub

On all of the prompts above, please press enter and accept the defaults. The last two lines will be similar to:

Identity added: /home/username/.ssh/id_ed25519 (username@site.com)
ssh-ed25519 AAAAC3Nza..........................VhMemWs8 username@site.com

Enter Public Key in GitHub

  1. In your browser, go to your GitHub repository
  2. Click on your image in a circle in the upper left-hand corner (you might need to login)
  3. Settings -> SSH and GPG Keys -> New SSH key
  4. Copy the text from your the last line (ssh-ed…) and paste it into the second box. It will end with your email address.
  5. I recommend using the RPi hostname as the title
  6. Click on Add SSH Key (you might need to authenticate again)

Add the AVR C repository

Go to the AVR_C repository, click the green button Code -> copy icon and it will copy “git@github.com:lkoepsel/AVR_C.git”

Go to your CLI:

# you will be in your home directory, you can confirm
pwd
/home/username
# then add "git clone" then paste the repo
git clone git@github.com:lkoepsel/AVR_C.git
# this will copy the repo into AVR_C folder
cd AVR_C

Test the Tool Chain

Its best to quickly test the tool chain locally, before, attempting to do so with VS Code.

  1. In your browser in the GitHub AVR_C repository, scroll down the README until you see the text for env.make:

    Full version of the env.make file I am using:

    # Environmental variables for specific boards
    # See https://wellys.com/posts/avr_c_make_part2/ for more information
    # Uncomment entire block less top line of block
    # After switching boards, Library MUST BE RE-COMPILED
    ...
    

Comments powered by Talkyard.