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
- Visual Studio Code
- Arch Linux BIN or Open Source Version
- Other Linux Distros – search your package repository
- VSC C/C++ extension
- VSC Cortex-Debug extension
- GNU ARM Embedded Toolchain
- Linux – search package repository for
arm-none-eabi
- I installed to following packages for Arch Linux
- Linux – search package repository for
- 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
- Go to Atmel Start
- Create a new project.
- Select your development board or microcontroller.
- Optional – configure your peripherals and driver software
- Export Project and be sure to select the option to generate a gcc Makefile
- 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. - Replace the files in the GitHub repository with the new files.
- Start VSC and open the repository folder. File -> Open Folder -> Select Folder
- Open
c_cpp_properties.json
and change__SAMC21J18A__
in thedefines
section, line 30, to the microcontroller you are using. - 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. - 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.