0:08
hello and welcome to controllers Tech
0:12
this is the fifth video in The w25q
0:15
Flash series and today we will continue
0:18
with writing data into the memory
0:20
in the previous video we saw how to
0:23
write a page or number of pages using
0:25
the page program command
0:28
we were able to successfully write the
0:30
data but in doing so the entire sector
0:35
this will not work for the case where
0:37
you want to just update a small part of
0:39
the memory while keeping the remaining
0:43
so in today's video we will see how to
0:46
write the data without affecting the
0:47
other regions of the sector
0:49
we will achieve this in four different
0:53
first copy the sector's data into the
0:56
then update the Rams data with the new
1:00
then erase the sector
1:02
and finally push the updated data into
1:07
the only drawback with this method is
1:09
that it is going to consume some extra
1:11
time since we need to write the entire
1:13
sector even for a small update
1:16
but this is the only way to go about it
1:19
as we already know the device does not
1:21
let us erase anything smaller than a
1:25
further in the video we will also cover
1:27
how to write and read a single byte into
1:30
the memory without erasing the sector
1:33
but this method of writing only works if
1:36
that particular memory location was
1:40
all right this is the previous project
1:42
that we were working on
1:44
I mentioned in the previous video that
1:47
the code isn't fully tested so there
1:51
I found one issue with the right
1:55
actually whenever I tried to write more
1:57
than 250 bytes at once the function
2:00
wasn't able to write the data
2:03
I don't know if you guys experienced
2:05
this issue or not but I faced it so I am
2:08
going to write a fix for it
2:10
to fix it I am going to put a separate
2:13
condition if the data size is more than
2:15
250 bytes then we will send the data in
2:19
into two separate instructions
2:21
first send the first 100 bytes of data
2:24
and then send the remaining data bytes
2:28
if the data size is less than 250 bytes
2:31
then the data will be sent in the usual
2:35
this will fix that issue and the testing
2:38
will be done in the next function itself
2:41
the next function is going to update the
2:43
data of the entire sector
2:46
the function is going to be similar to
2:48
how we wrote the page write function so
2:50
let's copy some part of it
2:52
the right page function which we covered
2:55
in the previous video actually writes
2:57
the data while clearing the sector also
3:00
so let's rename it to the right clean as
3:03
it cleans the entire sector before
3:05
writing the data into it
3:08
and the new function is just going to
3:10
update the data so we'll just name it
3:14
we will focus more in the sectors here
3:16
so let's remove the page related
3:20
first we will calculate the start sector
3:23
instead of the start page we will use
3:28
to calculate the end sector we need the
3:30
end page value but since we don't have
3:33
it let's copy the code from the previous
3:35
function and directly put it here
3:40
the number of sectors will be calculated
3:44
let's define an array of 4000 and 96
3:48
bytes to store the data stored in the
3:52
we also need to calculate the sector
3:56
this is similar to page offset but it
3:59
will be calculated from the start of the
4:02
for example if I want to write on page
4:05
20 at an offset of 100
4:08
the page 20 lies in sector 1 so this
4:12
section will result in the value 4 which
4:16
each page contains 256 bytes so we
4:20
multiply 4 with 256 and add the page
4:26
this would give us the memory offset of
4:29
1124 from the start of the current
4:33
let's Define a variable to keep track of
4:36
the data index inside the data to be
4:39
all the definitions are over so we will
4:42
call a for loop as many times as the
4:44
number of sectors we need to write
4:47
here we will first determine the start
4:51
start page is where the sector starts so
4:54
it can be either 0 16 32 48 basically a
5:01
next we will read the entire sector
5:04
starting from the start page and store
5:06
the data in the array we defined earlier
5:10
now we have the current data stored in
5:12
the sector so we just need to modify
5:14
certain locations with our new data
5:17
but before doing that we also need to
5:20
calculate how many more bytes we have
5:22
available in the sector
5:24
so let's write another function to find
5:26
the remaining bytes in the current
5:29
we will call this bytes to modify and
5:32
instead of 256 bytes it will be used for
5:39
the bytes remaining variables will keep
5:41
track of how many bytes we can modify in
5:46
this will depend on the sector offset we
5:55
now in the for Loop we will modify the
5:57
array we stored earlier with the new
6:07
the sector offset is the offset in the
6:09
array where the modification will start
6:11
and the data index will be used to keep
6:14
track of how many bytes of the data
6:16
pointer have been already used
6:19
after updating the array we will call
6:22
the right clean function and write the
6:26
this will clear the sector before
6:27
writing data into it but we are writing
6:30
the entire data back into the sector
6:33
the final result is going to be a sector
6:35
update instead of writing fresh data
6:39
after the write is done we will update
6:41
the start sector and set the offset to
6:44
zero so that the new position will be
6:46
the start of the next sector
6:48
we have already copied some data from
6:51
the data pointer so update the data
6:55
also the size will be reduced as we have
6:57
already copied some data into the
7:01
these updated values will be used for
7:04
the next transfer in the new sector
7:07
let's quickly go over it once again
7:10
we find the start sector n sector and
7:13
number of sectors to be modified
7:16
then calculate the sector offset
7:19
then read the data from the sector and
7:23
then calculate how many bytes we can
7:26
update in the current sector and finally
7:28
update the data after updating the data
7:31
write the data into the sector
7:34
then update the variables for the next
7:38
let's define this function in the header
7:50
all right in the main file we already
7:52
have the variable index
7:55
when the button is pressed we will
7:57
increment this index variable
7:59
and for each value of this index
8:02
variable we will perform different
8:05
if the index value is 1 we will write
8:08
the data at the beginning of the memory
8:11
let me type cast these to character
8:13
pointers to avoid any warnings
8:23
similarly if the index value is 2 we
8:26
will write the data to page 2 at an
8:29
offset of 44. page 0 and page 2 both are
8:33
in sector 0 so if the code works we
8:36
should see both the data at the
8:40
we are calling the right function to
8:42
update the data in the sector
8:44
if the index value is 3 we will write
8:47
the data to page 15 at an offset of 250.
8:51
I chose this location on purpose this is
8:54
towards the end of sector 0. this data
8:58
will cross the sector boundary and some
9:00
part of it will be written in sector 1.
9:03
if the index value is 4 we will write
9:06
the data to page 20 which is in sector
9:08
1. so we are writing data into two
9:11
sectors and to verify the data we need
9:16
this is why we are reading
9:19
8192 bytes from the start of the memory
9:24
let's build and debug the code now
9:30
these breakpoints will verify which
9:33
function is being called
9:35
at first we will get the data at the
9:37
beginning of the memory
9:39
right now the data is FF hex
9:42
I am pressing the button now
9:45
we have hit the break point let's resume
9:49
here we have received the first set of
9:51
data in the beginning of the buffer
9:54
I press the button and we hit the second
10:00
we will receive the data from page 2
10:02
comma 44 which is the address
10:05
556 from the beginning
10:08
here we have the new data stored at the
10:12
if we check the previous data at the
10:14
beginning of the memory it is still
10:17
so this means the sector was updated
10:19
rather than how it was getting
10:21
completely erased in the previous video
10:24
now we will see if the data is able to
10:27
cross the sector boundary
10:32
the page 15 comma 250 is at the offset
10:37
4090 from the beginning
10:39
there are only six bytes remaining to be
10:41
written on this sector and our data size
10:48
here you can see the data crossed the
10:50
boundary successfully and we have the
11:03
this time we updated sector 0 and sector
11:06
1 and we still have the previous data in
11:12
I didn't hit the last break point
11:14
because I didn't change it to 4. but I
11:17
hope you understood how this works
11:19
we were able to write two sectors
11:21
continuously and the sectors got updated
11:24
instead of clean writing
11:26
we will also see two more functions
11:28
today one for reading a single data byte
11:31
directly from a memory and another one
11:33
for writing a single byte into the
11:36
let's start with reading the memory
11:41
this function will directly return the
11:43
data and it takes the memory address as
11:47
most of the code will be similar to the
11:49
read function so let's copy it
11:55
let's remove this as we are passing the
11:58
address in the parameter itself
12:01
let's Define a variable which will hold
12:05
after reading the data we will return
12:12
we need to pass the address of the
12:14
variable as this function takes the
12:16
pointer as the parameter
12:18
and we are reading only one byte
12:24
now we will write another function to
12:26
write the one data byte
12:29
the parameter will be the address and
12:31
the data to be written
12:33
let's copy the read function and we will
12:41
the array will have one extra byte the
12:45
let's define the variable index
12:48
we need to pass the instruction for the
13:03
also the last byte of the array will be
13:08
Now update the index value according to
13:11
the bytes occupied in the array
13:13
before writing the data we need to
13:16
confirm that the memory location has
13:20
so we will read the location and only
13:22
proceed if the value returned is FF hex
13:28
if the location is erased we will first
13:33
then pull the Cs pin low write the
13:36
command along with the data and pull the
13:42
now wait for the right operation to
13:44
finish and finally disable the right
13:50
let's define these functions in the
14:01
all right in the main function after the
14:04
button is pressed we will write
14:06
different bytes to different data
14:09
the first few bytes are already occupied
14:12
by this data so let's write to the
14:16
we will write the character C
14:19
at the address 32 let's write another
14:23
now to demonstrate that the overwriting
14:25
will not work let's write another
14:27
character to the address 10 which
14:29
already holds some data from previous
14:33
we can also write in a loop here we will
14:36
send 10 bytes of data starting from the
14:43
all right let's build and debug the code
14:54
we are expecting data in the beginning
14:57
of the buffer itself
14:59
you can see the data from previous code
15:04
I am pressing the button now here you
15:07
can see the characters C and T got
15:09
updated in their respective positions
15:12
also the data we wrote at position 10
15:15
couldn't overwrite the previous data
15:18
now let's check the address 1000.
15:21
here we have the values from 0 to 9.
15:25
writing single byte works fine but make
15:28
sure you write only to the location
15:31
so we were able to update sectors and
15:34
write single byte in today's video
15:37
in the next video we will cover how to
15:39
write numbers floats and 32-bit
15:43
this is it for today
15:45
I hope you understood the concept
15:48
leave comments in case of any doubt the
15:52
link to download the code is in the
15:55
keep watching and have a nice day ahead