This tutorial will show you how to program and debug STM32 Discovery Kit.
The discovery kit has an on-board ST-Link debugger. This is the ST-Link debugger.
and
this is a STM32L4 processor, which we try to program and debug. We don't need any
extra hardware.
except a USB cable to program and debug this chip, by using this debugger. Let's look at a demo project.
In this project, we have a very simple program. The main function, which called the find_sum function
to calculate the sum of integers. To debug the program, we need to make
sure we set the debugger correctly. So go to "options for project", click it, click "debug", and make sure we select the ST-Link debugger,
and also select "Run to main". When we start the debugger, it automatically stops at first line of the main function,
and it's selected by default
Go to setting, make sure the serial wire (SW) is selected, instead of JTAG. We use Serial Wire.
In the flash download, make sure we have flash there. If it is not there, you can simply click "add", and select the 1MB flash.
To start the debug, you can click the debug button here. Click it. When the debug starts,
it has three windows. And the left shows register values. Here is a disassembly window. This is C program source code window.
The debug buttons are here. This is a reset, this is run, step in, step over, step out, run to,
go to the current position. And we can set up a break point.
For example, within the for (loop) in the subroutine. We can click run
and it will stop at the current position. And we can click it to remove the break break
point to do step over, or step in.
Here is the call stack that will show you the variable names and values. If we keep stepping
or running the subroutine, the values here will change in real time.
We can also set a break here and click "run". It will go and run to this position. Or we can simply select "run out" to return from
that subroutine. And we can look at the memory values. The memory address of data memory, by default, starts
hex value, 20000000.
See here, we have defined a global variable a, which is FFFFFFFF, and then a string
These (are) two global variables. We can show the memory value in different formats. Right click the window, and
we can, for example, select as decimal. It will show you the decimal values, instead of the default HEX values.
And we can deselect ... And we can also select ASCII. This will show you the second (variable) myString as ASCII values.
ASCII string. These are dots because they are not displayable ASCII characters.
Let me go back. Show you unsigned long, de-select decimal, Now we are showing the ASCII values.
If we execute program, we can also see variable changes in real time. For example, we just exit from the subroutine,
pSum is stored as .... This is the memory address of pSum.
20000038. So we can look at that memory value. Right now, it is
0000 because the assign has not completed yet. We have not assign the return value back to the sum variable. If we step
over, the value has changed to 2D in HEX or 45 in decimal
this is the debug. Another important reason we use Keil is we can see the peripheral registers in real time.
And here I am going to show you an example. Let me close this project. If you go to the textbook website, download the example
project here. Save the project, and
go to the download directory. And let me unzip it, and open the project
This program just simply light up a LED. So I'm trying to open another window and show my web camera to see the board here. This is the board. And I can set up
a break point here. This program just lights up a LED.
Specifically, it sets the GPIO pin B 2 to high. If you look at the schematic here,
If PB2 is high, LED 4, which is LED red, will be on.
So I can set up a break point here, and start the debug.
I made one mistake because I have not compile my project yet. Now, debug it. And I click "run", it will stop at the break point. Again, a very nice
feature of this program is, in the MDK (Keil) debug environment, I can see peripheral register values in real time. For example, I click
"peripheral", "system viewer", "GPIO", and "GPIOB". This is GPIO B. This is the mode register. I am
setting GPIO B 2 the mode as 01, which is digital output. Output type, by default, is push-pull. If not selected, it is 0; if selected, it is 1. Output speed of GPIO 2 is 00. And also,
most importantly, because we have set it as digital output, we look at ODR,
output data register, here.
This statement tries to set ODR 2, which is here, as 1.
See here, the LED is on, and the output data register (bit) 2 is selected because it is high. For some registers, we can also
change the value in the debug environment. For example, output data register, if I click it, it will set it to zero, and the LED is off.
If I click it again, the LED is on. You can see many other peripheral registers, such as RCC
other GPIO ports, and also
core registers, including sysTick timer, and NVIC. You can see whether interrupts are
enabled or disabled.