0:08
hello and welcome to controllers Tech
0:12
this is the fourth video in The w25q
0:15
Flash series and today we will cover how
0:18
to write data into the memory
0:20
in the previous video we saw how to
0:23
erase the sectors and it will be needed
0:25
to write the data we will perform the
0:28
page write operation where we will write
0:30
one or multiple pages together
0:33
the page program instruction allows one
0:36
byte to 256 bytes to be programmed at
0:39
previously erased memory location
0:42
we must issue the right enable
0:44
instruction before programming the page
0:47
the page program instruction is sent in
0:50
the similar manner as page red we send
0:52
the instruction followed by the memory
0:54
address and finally send the data to be
0:58
the issue with page program instruction
1:00
is that we can only write a maximum of
1:06
during page red operation we could read
1:09
as many bytes as we want basically we
1:11
could read the entire page or the entire
1:14
sector or even the entire chip
1:17
but this is not the same in page right
1:19
as we can only write a maximum of 256
1:23
bytes at once so if we want to write two
1:25
pages we have to write them separately
1:28
this is going to be somewhat difficult
1:30
and this is why this video is longer
1:35
let's define a function to write the
1:37
page whose parameters are the page we
1:39
want to write the offset on the page the
1:42
size of the data and finally the pointer
1:47
I will explain the process as we go
1:49
along with the function
1:51
let's define an array of 266 bytes to
1:55
send the instruction address and the
1:59
we can only write 256 bytes of data at
2:03
once 4 bytes will be used by the address
2:05
and one byte for the instruction
2:09
Define a variable start page the page
2:12
where we will start writing the data
2:15
another variable the end page will be
2:18
the last page where the data will be
2:21
this is the formula I am using to
2:24
calculate the end page
2:26
for example if the start page is 1 with
2:29
an offset of 250 and if we want to write
2:35
this would result in the end page being
2:39
next we will calculate the number of
2:41
pages we are writing the data to
2:44
as per the example above we will get the
2:47
number of pages equal to 2.
2:50
before we can write the data to the page
2:54
but since we cannot erase a single page
2:57
we will erase the entire sector
3:00
let's calculate the start sector based
3:05
each sector contains 16 Pages this is
3:08
why I am dividing by 16.
3:11
similarly calculate the N sector based
3:16
and finally calculate the number of
3:20
now we will erase the number of sectors
3:23
we got in the above statement
3:25
this full loop will run as many times as
3:28
we have the sectors to be erased
3:30
we will call the function W 25q arrays
3:34
sector function we defined in the
3:38
the first sector to be erased is the
3:40
start sector and after that the sector
3:42
number will keep incrementing
3:45
now that we have erased the sectors
3:47
let's start preparing the data to be
3:51
the variable data Position will keep
3:53
track of the position of the data inside
3:57
to write the data into the memory we
3:59
will call the for loop as many times as
4:01
the number of pages to be written inside
4:04
the loop we will first calculate the
4:06
memory address based on the page number
4:10
for example if the start page is 4 with
4:13
an offset of 250 the memory address
4:20
next calculate how many bytes we have to
4:23
for example if the offset is 250 we can
4:27
only send 6 bytes so that the data does
4:30
not cross the page boundary
4:32
we will write a separate function for
4:35
the bytes to write function will take
4:38
two parameters the size and the offset
4:41
if size plus offset is less than 256 it
4:46
will return the size itself
4:48
otherwise it will return
4:53
for example if the offset is 250 and the
4:57
size is 10 it will return 6. this is
5:01
because we can only write a maximum of
5:03
six bytes starting from the offset 250.
5:07
similarly if the offset is 250 and the
5:11
size is 4 it will return 4 as we have
5:14
space to write another 4 bytes
5:17
after calculating the memory address we
5:19
will calculate the remaining bytes using
5:22
the function bytes to right
5:24
now before writing the data we need to
5:28
next we will copy the instruction and
5:31
memory address into the TX buffer this
5:34
is similar to what we did in the
5:37
except the instruction code is 2 hex for
5:42
we will not send the buffer here as we
5:45
still need to copy the data into it
5:50
so let's define an index variable and we
5:53
will update its value depending on how
5:55
many bytes have already been occupied in
5:59
the total bytes we have to send includes
6:01
the data bytes plus the index value we
6:06
this includes the instruction byte and
6:10
now we will copy the data into the TX
6:13
array the for Loop will run as many
6:16
times as the bytes remaining
6:18
we will simply copy the data in the
6:20
array starting from the index position
6:24
the data position variable keeps track
6:26
of the data inside the data pointer
6:29
once the data has been copied we will
6:31
send it to the device
6:33
to do that we will pull the Cs pin low
6:36
then send the data and pull the Cs pin
6:40
the size of the data is stored in bytes
6:45
now the data has been sent so it's time
6:48
to update the variables for the next
6:52
here we will increment the start page
6:54
and set the offset to zero so that the
6:57
next transfer can take place from the
6:59
start of the next page
7:01
we have already copied some data so the
7:04
size variable will be reduced
7:07
similarly the data Position will
7:09
increment so that we are an offset
7:11
inside the data pointer for the next
7:13
transfer let's give a delay of 5
7:16
milliseconds and finally disable the
7:19
this 5 milliseconds delay is as per the
7:23
if you take a look at the electrical
7:25
characteristics of the device here you
7:28
can see the page program time is defined
7:31
as three milliseconds
7:33
I just added a little extra time just to
7:36
be on the safer margin
7:38
this entire Loop will run as many times
7:40
as there are the number of pages to be
7:43
for example if we want to write 500
7:46
bytes starting from page 1 with an
7:49
offset of 200. the first Loop will write
7:52
56 bytes to page 1. the next Loop will
7:55
write 256 bytes to page 2 and the third
8:04
we will see this in working but let's
8:06
first Define this function in the header
8:10
before we run this let's make some
8:12
changes in the cube MX configuration
8:16
I am going to add a button to this
8:18
project and we will write the data to
8:20
the device only when this button is
8:23
the board already has a button connected
8:25
to the pin pe3 which is connected to the
8:30
basically pressing the button will pull
8:32
the pin E3 to the ground 2.
8:35
I am assigning the external interrupt to
8:40
now go to the gpio and enable the
8:44
interrupt for this pin
8:46
the interrupt will be triggered for the
8:49
all right let's generate the project
8:54
when the button is pressed the external
8:57
interrupt will trigger and the XD
8:59
callback will be called
9:05
inside the Callback we will simply set
9:08
the variable is pressed to 1.
9:11
the rest of the code will execute in the
9:13
while loop depending on the state of
9:20
let's define an array to store the data
9:25
we will update the data using this index
9:29
when the button is pressed copy the
9:31
updated data to the TX data array
9:34
and write the data to the page
9:37
I am writing to page 0 and an offset of
9:42
I chose the offset 250 just to show that
9:46
the data will cross the page boundary
9:48
and the rest of the data will be written
9:51
we will also read page 0 comma 250 to
9:55
check if the data has been written
10:02
we need to include some headers for the
10:04
S printf and the string length function
10:13
all right let's ignore these warnings
10:19
since we are reading from the offset of
10:22
250 we should see the data at the
10:24
beginning of the RX data buffer
10:27
let's run the debugger now
10:30
right now there is nothing at that
10:32
position so we see the value 255.
10:36
now I am pressing the button and you can
10:38
see the data has been updated
10:41
the index value is 0 right now
10:44
I am pressing the button again and the
10:46
index value has been updated to 1. it's
10:50
2 now it is updating every time I am
10:55
remember that we are seeing the RX data
10:57
buffer in the live expression so this is
11:00
the data being received from the memory
11:02
let's reset the debugger and run it
11:07
we get the same data which was stored
11:11
let me disconnect the device and connect
11:16
let's run the debugger again
11:19
the data is still the same
11:21
basically this data will remain there as
11:24
long as it is not erased
11:26
let me show a step-by-step procedure to
11:30
let's put a breakpoint in the right page
11:33
function all right we have hit the break
11:37
you can see the page is zero offset is
11:40
250 size is 20 bytes and the data is
11:44
what we set in the main function
11:46
the start page is zero the end page is 1
11:50
and the total number of pages is 2.
11:55
both pages are in the same sector so the
11:58
start and end sectors are the same
12:06
the number of sectors to be erased is
12:08
one so next we erase sector zero
12:17
the memory address is
12:19
250. the bytes remaining are six
12:23
this is because we can only write six
12:25
bytes on a page starting from the offset
12:37
bytes to send are 10 which includes six
12:40
data bytes three address bytes and one
12:48
next after the data has been written to
12:51
page 0 the start page increments to 1.
12:55
the offset is zero now
12:58
the size was 20 bytes but now we have
13:01
only 14 bytes remaining
13:03
the data position in the data array has
13:06
been incremented to six
13:08
the loop will run again with the updated
13:15
after writing the remaining data the
13:18
start page gets incremented again
13:20
the size is reduced to zero and the data
13:24
position has incremented to 20. but the
13:27
loop will not run again since we only
13:29
had two pages to program
13:31
so we were able to program the pages
13:35
writing data was tougher than reading
13:38
because of the limitations in the
13:40
you can test the code and let me know if
13:43
there are some issues with specific
13:46
I didn't test it for all the possible
13:49
conditions so it is still open to
13:53
in the next video we will see how to
13:55
store numbers floats 32-bit values Etc
14:00
this is it for today
14:02
I hope you understood the video
14:05
the link to download the code is in the
14:09
leave comments in case of any doubt
14:12
keep watching and have a nice day ahead