STM Programming Prerequisites
STMicroelectronics is one of the worlds largest semiconductor companies and as such many of their products are used throughout industry. This coupled with the greater capabilities of STM uCs as compared to Arduino is what led me to take interest in the STM lineup of microcontrollers. Unfortunately, as a company which usually sells directly to industry, ST microcontrollers have less support than other more popular controllers like Arduino.
Getting Started
To begin we need to download all the prerequisites for programming the ST microcontrollers. As with all my tutorials, I will be working on a Macbook but these tutorials are still applicable for other platforms
​
The easiest way to download the prerequisites in this tutorial is to use the Homebrew. Homebrew is a package manager for Mac. In a nutshell, it provides users with an easy way to locate, install, and configure packages (libraries). To install homebrew visit their website and follow the instructions on the front webpage. Once Homebrew has installed the rest of the process is much more straight forward.
​
Before downloading packages using homebrew, it is necessary to download and install Microsoft's Visual Studio Code for Mac users. The link to this download can be found here. After downloading the software the installer should guide you through the rest of the install. Visual studio code is where we will be doing the majority of the programming for the ST microcontrollers.
​
Finally, the last piece of software we need to download is STM32CubeMX. This software is used to initialize the ST microcontroller. The program essentially allows us to configure the clock and peripherals of the ST microcontroller without having to write all the code for this ourselves. To download this software follow the link here. To be able to download the software from ST you will be required to make a free ST account.
​
Now that we have STM32CubeMX, Visual Studio Code, and Homebrew installed we can begin installing the additional packages to begin programming the ST microcontroller.
Software Packages
The first software package we need to install using brew is the GNU ARM enabled toolchain. Essentially a toolchain is a set of software tools which are chained together. These software tools are often used for embedded system development. In our case, we are downloading a cross-system toolchain. What this means is that the compiler is made to run on one system architecture (i.e x86) and produce binaries for another architecture (i.e ARM). To find the package we are interested in start by typing <brew search gcc> into a terminal window. This should result in the following...

What this command does is it finds packages with a specified name within the homebrew database. From the result of this command, it is clear that there are multiple versions of the GCC ARM compiler available for download through Homebrew. To download the most recent version type <brew install gcc>
​
Following this command brew will likely run an update and then install the package.
​
The second software package that is required is stlink. This package is used for the flashing and reading of programs into and out of the ST microcontroller. To download and install this package type
<brew install stlink>
​
If you ever need to check what packages you have installed using brew you can use the command
<brew list>
​
To discover new brew commands type
<brew help>
​
This will print a list of basic brew commands which can be used to control homebrew.
STCubeMX
Before we can use the STMCubeMX to generate our initialization code we need to create a workspace for all of our ST related work. To do this open up a terminal window and type.
<cd ~>
press enter
​
This command will take you to the home directory for your user. Now to create a new directory for your ST projects type the command
<mkdir STMProjects>
press enter
​
Note the name doesn't have to be STMProjects that is just the name I chose for my workspace. Now open up STM32CubeMX. You will be greeted with the following startup menu

Select the file dropdown menu. From this menu select the "New Project ..." option. This will bring up a new project window which will ask you to select the board you will be programming. This is all shown in the video below where I type in my microcontrollers number and use this to find the microcontroller I will be programming in subsequent tutorials. Note that for the moment I am programming discovery boards, which are boards created by ST for the purpose of gaining experience and experimenting with their microcontrollers. In the future, I will create a tutorial describing how to create PCBs for the ST microcontrollers. Whenever this happens I will be able to break away from the discovery board and begin using my own custom boards. That being said the ST boards are sold for a modest price and are great for experimenting as will be shown in later tutorials. The specific board that I am using is the STM32F334C8 discovery board, which can be found for sale here. Feel free to order a different board, as it will not impact the commands which can be used when programming the microcontroller.
From the menu shown at the end of the screen, we can configure all of the peripherals exactly how we want for whatever project we may be doing. I will skip this part though as it will vary from tutorial to tutorial. Instead, I will focus on the code generation for STMCubeMX software. Below is a tutorial showing the steps to create your initialization code.
​
To generate the code we need to go to the Project Manager tab. From this tab, we can name our project in the <Project Name> entry and we can also specify our path in the <Project Location> entry. In the <toolchain IDE> dropdown menu select the Makefile option. Finally, go to the "Code Generator" side menu. Under the category "STM32 Firmware Library Package" select the option "Copy only necessary library files". All of this can be seen in action in the video below.
For the remainder of this tutorial, we have finished using the STM32CubeMX software so it can now be closed.
Visual Studio Code
We are now on the last step before we can begin programming the ST microcontroller. What we need to do now is set up Visual Studio Code to be able to compile and upload our scripts to the microcontroller. To begin this process start by opening up Visual Studio Code. From here underneath the label "START" select the option "add workspace folder...". From this menu select your STM workspace which you created earlier in this tutorial. See the video below for details
Once the workspace is added you will notice that all of the projects in the workspace and their files can be opened using Visual Studio. We just have one more step before we can compile and upload our code to the microcontroller. In this step we just need to add a couple of files to our visual studio project to allow the code to compile correctly. These files can be found on my GitHub page here. The video below shows how the files should be placed to allow the files to be found in visual studio code. Be sure that the .vscode folder is in the top level of your project (see below).