In this tutorial, I will show you how to create a project in MDK-Keil. The textbook focuses on two different STM32 Discovery Kits:
STM32L1 and STM32L4. STM32L1 has a Cortex-M3 microprocessor while STM32L4 has a
Cortex-M4 with FPU and DSP. In this tutorial, I will create a project for the STM32L4
discovery kit. Let’s open MDK-Keil. The first step is to make sure that
required packages have already been installed.
We need at least three packages: STM32L4_xxDFP, ARM::CMSIS, and
ARM-Middleware. aIf a package has not been installed, you can click the install button.
It will automatically download and install the package.
Now let’s create a new project.
Click “project”, “New Project”, select a project folder. Let me create a new folder.
called "example project", select the folder, give a project name, just called "project".
click "save". Now we need to select the device. We have to select STM32, L4, 476, 476VG.
And select this device, this microprocessor. Click "OK". In the runtime environment dialogue,
we need to select one software component called "CMSIS::Core". Make sure do not
select
the start-up package. Startup assembly file here. Because we are going to use a modified
version that can be downloaded from the book website. Do not select this one. Click "ok".
Let's go to the textbook website. The textbook website. Click "Example Labs".
The textbook website provides the project templates for different
development boards. So our focus is STM32L4 in this tutorial, and we create a C
project template. Let's download the startup. Right click, save link as, save in
the project folder, and
save the head file.
Let's go back to the project. Let's highlight the projects "source group"
right click, add existing file in the project, select assembly, select startup.
Now we have the startup here. Let's create a new file, a new file, select C
Let's call main. Let's write the C code. In the C code, we're not doing anything but just
have a dead loop here. Include the header file we have downloaded. It's called
"stm32l476xx.h". Let's write the main function.
Now we need to change the project properties. Let's go to the "options" for the target, and
we can verify that we select the right device.
This is STM32L4 processor used on the discover kit. Let's go to the output.
You can highlight and create HEX. This is useful if you want to download the HEX file by
using ST utility. But this is optional. In the linker page, make sure
this is selected, and this is selected by default. And in the debugger
environments, there two different debug options.
One is to use simulator. The other one uses hardware debug.
The simulator has some limitations. For example, two major limitations are, you cannot access all the
peripherals, and the second one is, all simulations can only assume ideal
conditions for memory access, so it does not a similar memory delay. And the other one
is hardware debug. The default is ULINK. And we are going to use ST-Link debugger.
Click setting, so this has already been selected, make sure
the port, there are two options here: JTAG and Serial Wire. Make sure we
select Serial Wire. Another important thing if you go to flash, make
sure the flash memory has been selected. If the flash memory is not there, you
can click "add", you can select the flash memory.
Since it is already there, we are going to do cancel. I am going to click OK. Now I can click "Build project".
You can build a project here, we can "build target" or simply click this one, the
icon here. Build. Now I have no errors. I can set a breakpoint and debug the code. This one inserts a break
point, and this is the debug icon. Debug
Now you can see the program runs and stops at the break point. And this is the register value.
Register r0 to register 15, and program status register, including
NZCV flags, and FPU registers here. In the next tutorial, I will show you how to debug
a project