SAMC21 Development using Visual Studio Code

Due to the great silicon shortage of 2020-2021 my go to microcontroller family of choice, the STM32, is having supply issues. Draw to a microcontroller that is in stock and has all the key features I am looking for I got myself a SAMC21 Xplained Pro development board and set to getting it to blink.

I have setup up a GitHub repository as a starting template that can be found here: https://github.com/sdp8483/samc21_vscode

Why Use Visual Studio Code (VSC)?

My suggestion if you want to get going fast and have the best experience is to use Atmel Studio (or whatever Microchip is calling it these days). With one installation you get support for all Atmel microcontrollers you could possibly want.

My issue is that I use Linux and Atmel Studio is Windows only so I needed a different solution. I like Visual Studio Code so that is what I gravitated towards. It has great code completion and looks good too.

Prerequisites

  1. Visual Studio Code
  2. VSC C/C++ extension
  3. VSC Cortex-Debug extension
  4. GNU ARM Embedded Toolchain
    • Linux – search package repository for arm-none-eabi
  5. OpenOCD

How To Use This Repository

The code in the GitHub repository is based on code generated by Atmel Start. I created a new project and selected the SAM C21 Xplained Pro on the device selection page. I then exported the blank project and made sure to enable the generation of a Makefile. For the CMIS-SVD files I downloaded them from the Microchip Packs Repository. These files are used by the Cortex-Debug extension.

Edit main.c with your application code. I have a simple blink sketch with the main clock set to 24MHz in there right now using direct register commands.

Use the keyboard shortcut ctrl-shift-b and then select the build task to compile the code. Select the clean task to remove previously compiled files. This is the same as if you type make or make clean in a terminal.

Use the shortcut key F5 to start the debugger. Your code will be uploaded to the development board and you can run by clicking the play button that appeared in the top center of your screen. Check the VSC documentation to learn how to use the VSC debugger.

How to Use a Different Microcontroller

  1. Go to Atmel Start
    1. Create a new project.
    2. Select your development board or microcontroller.
    3. Optional – configure your peripherals and driver software
    4. Export Project and be sure to select the option to generate a gcc Makefile
    5. Generated code from Atmel Start will have a file extension of .atzip. This file is super easy to open. Just right click or open with your Archive Manager and unzip the file just like it is a .zip file.
    6. Replace the files in the GitHub repository with the new files.
  2. Start VSC and open the repository folder. File -> Open Folder -> Select Folder
  3. Open c_cpp_properties.json and change __SAMC21J18A__ in the defines section, line 30, to the microcontroller you are using.
  4. Open ‘launch.json and change svdFile, line 14, to point to the file for your microcontroller. Should be the part number of the microcontroller you are using.
  5. Open openocd.cfg and change the chipname, line 6.

Troubleshooting

If you run into issues with the Intellisense not working correctly or some other issues be sure to check the settings in c_cpp_properties.json, you may need to add some more directories to your includePath or add a preprocessor definition in the defines setting. Hovering over the settings labels (the blue text) will give a description popup. Reading the Makefile gave me a few clues when setting up this repository for files that should be included and preprocessor definitions. I haven’t tested this setup extensively, I just started with the SAMC21 microcontroller family, so it is possible I missed a setting or configured something incorrectly.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.