0:07
hello everyone welcome to controllers
0:11
Tech many of you guys requested this so
0:15
today we will see how to program the
0:17
flash memory in stm32 this video only
0:21
cover those microcontrollers whose
0:24
memory is organized in pages for example
0:27
cortex m3 series or m0 series I will
0:32
make another video about the controllers
0:34
whose memory is distributed in sectors
0:36
like in cortex m4 or m7 devices I am
0:44
and it's a medium density controller as
0:47
mentioned here there is not much to do
0:59
in the setup I am enabling the external
1:03
crystal and setting the clock to run at
1:16
save this to generate the code here is
1:26
our main file let's include the library
1:30
first put the C file in source directory
1:34
and header file in the include
1:48
include the flash page dot H in the main
2:07
let's take a look at the reference
2:12
is a medium density device
2:17
as you can see the main memory is
2:21
divided into 128 pages each page is one
2:27
kilobyte and have a range of addresses
2:29
associated with it now we are going to
2:33
program this page you should always
2:36
start as lower in the memory as possible
2:38
I will tell you the reason for choosing
2:41
this later in the code itself let's open
2:53
the see file that we included flash
2:57
write data will write the data to the
2:59
memory it takes the start page address
3:02
as the parameter and also appointed a
3:05
32-bit variable that you want to store
3:14
first of all it will calculate how many
3:17
32-bit variables you want to write then
3:21
it will calculate the number of pages
3:23
needed to store the data flash page size
3:32
varies according to the controller in my
3:35
case it is 1024 bytes like it said in
3:40
the datasheet after calculating the
3:43
number of pages it will erase that many
3:45
pages starting from the start page
3:47
entered in the parameter now it will
3:51
start writing data each word takes 4
3:55
bytes so we have to increment the
3:57
address by 4 you can also write in half
4:01
word or double word but then the
4:04
increment will also change I have
4:07
commented it out here and finally the
4:11
flash will be locked again
4:16
to read the data we just need to access
4:19
the memory the data will be saved in
4:23
this variable let's start now I will
4:30
write the data first the memory location
4:34
that I copied from the data sheet I need
4:38
to define this variable first Here I am
4:42
defining an array which can hold two
4:44
32-bit variables and we will pass the
4:48
address of this array to the flash right
4:51
let's build this there is some warning
5:02
it's fine let's go ahead and debug the
5:15
here is the memory tab let's enter the
5:18
memory address that we want to observe
5:27
you can see it's all clean here I also
5:37
want to show you another memory address
5:40
this is the start of flash memory for
5:43
f103 controller and you can see it's not
5:48
empty that's why I did not program the
5:52
start of flash memory it is already
5:55
programmed to run this code that we are
5:57
using right now I will explain this in a
6:00
while that what happens if we try to
6:03
overwrite this part of the flash
6:18
you can see it's occupied up to this
6:21
address and therefore we should only
6:23
program after this part that's why I
6:26
chose the lowest part of the flash
6:28
memory let's run this now we hit the
6:34
break point note that the memory
6:37
location is empty right now now let's
6:41
step over this and you can see the data
6:44
being stored in the flash memory other
6:50
than in writing the 32-bit numbers we
6:53
can also write some string to this
6:55
memory we will write hello world in the
7:03
let's build and debug this
7:16
you can see right now the previous data
7:19
is stored in the memory and as we run
7:27
this function the data corresponding to
7:30
hello world is stored in the flash
7:43
I am creating another variable to store
7:46
the data that is read from the memory
7:48
this variable can hold forwards we will
7:57
read from the same memory address
8:13
I am putting our X data in the live
8:30
we can see the data here let's see if it
8:34
is the same data our X data is in the
8:38
integer format so we need to select the
8:41
same here and yes we have the same data
8:45
present in the memory and in our X data
8:56
now I will show you what happens when we
9:00
try to write from the beginning in the
9:01
flash memory I will try to write the
9:04
same data that is hello world
9:17
let's open the disassembly tap here you
9:21
can see the instructions are being
9:23
stored in the same address that we want
9:25
to program with hello world let's enter
9:29
inside this function I will put another
9:40
breakpoint at the arrays function let's
9:43
enter inside the arrays function I am
9:51
stepping over now just to show you guys
9:53
where the problem occurs
10:03
now once we call this function the
10:06
entire page will be erased and all the
10:08
instructions will be arrays DeLonge with
10:11
and that's where the error occurs as the
10:15
instruction that was supposed to execute
10:17
is not present in the memory you can see
10:28
the memory details here this entire page
10:32
is erased the next instruction that was
10:36
supposed to execute was present in that
10:38
page and is not there anymore that's why
10:42
I advised in the beginning that we
10:44
shouldn't write first few pages next I
10:48
will show you how to use st-link utility
10:50
to see the memory details
11:23
see this the beginning of the flash
11:25
memory is again occupied by the
11:27
instructions we will go to the address
11:31
that we have programmed you can see the
11:39
hello world written to the location this
11:47
is it guys in the final code I will
11:51
include a function to convert the
11:53
received data into the string format you
11:57
can download the code from the link in
11:59
the description keep watching be safe