OCFreaks!

Create new STM32 project in Keil uVision 5 tutorial

In this tutorial we see how to create project in KEIL MDK uVision 5 for STM32 ARM Cortex-M based MCUs. Its for beginners who want to get started in programming STM32 with Keil. This tutorial also applies for all supported devices across the STM32 Family viz. STM32F0/F1/F2/F4/F7/etc/. Keil uV 5 is much different than older Keil uV4. uVision 5 has integrated pack installer which is used to install specific MCU family packs and other libraries. To create project for STM32 MCU, you will first need to install MDK5 software packs for your microcontroller family. Either you can download it separately or do it from within the IDE.I recommend adding software packs using IDE.

Basically three(or more?) types of STM32 Keil projects can be created:

  1. One that uses CMSIS(core) only.
  2. One that is based on Standard Peripheral Library.
  3. Finally, one that is based on HAL (Hardware abstraction Layer) Library.

For the sake of this tutorial we will see how to create CMSIS and SPL based STM32F103C8 Keil uv5 project, as an example, but will work exactly the same for STM32F0, STM32F4, and other families. I will cover HAL based projects in another tutorial.

1) Installing prerequisite STM32 Keil software pack

If already installed, you can SKIP this.

Step A. Download latest Keil MDK uVision5 from Keil’s website.

Step B. Install Keil uVision 5 to default path.

STEP C. Open Keil 5 and click on “Pack Installer” icon as shown below:

STEP D. On the left half on the window, under “Devices” type “STM3F103C8”
(or other device name depending on the device present on your development board) in search box and select the MCU in the list below. Now, on the right half of the window click on the “install” button which is towards to the right of “Keil:STM32F1xxx_DFP” and “Keil:STM32NUCLEO_B”. Repeat this step if want to add support for other device family. After this, wait until pack installer finishes downloading the required pack files for selected MCU.

Alternatively, you can manually download the software pack and install it directly from MDK5 Software Packs. It will be present Under “KEIL-> STMicroelectronics STM32F1 Series Device Support, Drivers”. In general for STM32Fx Devices.

STEP E. After installing from Pack Installer you will get a confirmation to reload packs. Click “Yes” as shown below:

2) Step by step Tutorial

Okay, so now we have the necessary packs installed to create our first STM32 project in Keil 5. Just follow the steps mentioned below to create a new project in Keil uV 5 or if your project is not working properly:

Step 1.

Open the Keil IDE, under main menu goto “Project->New uVision Project…” and a window prompt will open asking to save the new project. Type your desired project name and save.

Step 2.

After that, a new window will appear as shown below. Make sure “Software Packs” is selected for the 1st drop down. In the search box below it, type “STM32F103C8” and then select the device from list below. For e.g.: STM32F103C8 for STM32 Blue Pill, STM32F103RB for Nucleo-F103RB, STM32F030R8 for Nucleo-F030R8 and so on.

Finally click “OK”.

Step 3.

A. For CMSIS:
Inside the “Manage Run-Time Environment Window” select the check boxes for “CORE” under “CMSIS” and “Startup” under “Device”. If you want to select any other libraries you can do so by selecting the respective checkboxes. Selecting “Startup” will automatically add all the necessary startup/boot files required for STM32F1xx device, so we don’t have to import them from external sources. The selection of libraries can be changed any time later.

B. For Standard Peripheral Library (SPL):
If you want to use SPL, the select the required peripheral library components as required. Note that some components have dependencies as well, so you will also need to include dependent components. For. E.g. GPIO needs RCC to enable clocks.

Step 4.

Now click on “Options for Target” button as shown below:

Make sure the settings match as shown below.

Step 5.

Now, click on the “Output” tab. If you want to generate hex file then you can check “Create HEX File”. You also enter a suitable name for the executable output file.

Step 6.

Then click on the “Linker” tab and Under that tab check the checkbox option which says “Use Memory Layout from Target Dialog”.

Step 7.

Now, under the “Debug” tab, select ST-LINK as debugger since its the most common for debugging and programming STM32. Finally to click “OK” to apply settings and close window.

Step 8.

Now, in the source navigation pane on the left area, right click on “Source Group 1” and select “Add New Item to Group ‘Source Group 1′”.

Step 9.

A new window will pop-up to add an item as shown below. Select “C File (.c)” or C++ File (.cpp) , then enter the name of the file in the text box to the right of “Name:” and click “Add”.

Step 10.

Now you can write your code in the editor. To compile your program Press “F7” key or in the main menu goto “Project->Build Target”. To check for any compilation errors you can have a look at the build output at the bottom of main window. Two screenshots of the Keil MDK uVision 5 are given below.

A. For CMSIS Core Project:

B. For Standard Peripheral Library based Project: