0:08
hello and welcome to controllers Tech
0:12
this is the second video in The w25q
0:15
Flash series and today we will see how
0:18
to read the data from the flash memory
0:20
in the previous video we saw how to
0:23
reset the device and how to read the
0:27
today we will read the data from the
0:29
different locations in the flash memory
0:32
I have already stored the data at some
0:35
specific locations in the memory
0:36
although we will cover the writing part
0:40
let's see the datasheet of the device
0:44
here is the instruction to read the data
0:47
let's quickly take a look at what we
0:51
here is the previous project
0:54
in the main file we wrote the code to
0:57
reset the chip and read the device ID
1:00
we Define these functions in the w25q
1:03
source file and today we will add more
1:06
functions to this file
1:07
I have modified the code from the
1:10
previous project and defined these SBI
1:12
read and write functions
1:15
this will generalize this Library so
1:18
that it can be used with any
1:19
microcontroller that supports SBI
1:22
all you need to do is modify the SBI
1:25
read and write functions according to
1:27
the microcontroller you use
1:29
I will show this later with the AVR
1:32
controller after I cover the SBI
1:34
peripheral in it anyway let's get back
1:38
the command to read the data is 3 hex
1:42
to send the command we drive the pin low
1:45
send the read enable instruction
1:47
followed by the 24-bit memory address
1:50
after receiving the address the device
1:53
will start Shifting the data out on the
1:55
respective pin starting from the
1:57
mentioned memory address
1:59
the data is sent in most significant
2:03
after transmitting a data byte the
2:06
memory address is automatically
2:07
incremented allowing a continuous stream
2:09
of data to be transmitted
2:11
the transmission continues as long as
2:16
basically we can read an entire chip
2:18
with very minimal effort just by
2:20
providing a single command
2:22
here the timing diagram shows the same
2:26
we send the instruction to enable the
2:28
read followed by the 24-bit address
2:31
after receiving the address the device
2:34
starts pushing data out in byte format
2:38
so let's write a function to read the
2:43
the start page where we want to start
2:47
this is a 32-bit variable because the
2:50
number of pages is 256 times the number
2:53
of blocks so this number can be very
2:57
the offset for the start page
3:00
the offset can range from 0 to 255.
3:05
the size of the data we want to read
3:08
the size should be in bytes
3:10
and the pointer to the array to store
3:17
the process to send the command Remains
3:21
let's define the T data array with 4
3:25
the first byte will be the read-enable
3:27
instruction that is 3 hex
3:33
now we need to send the 24-bit memory
3:37
let's calculate the address first
3:40
here we will multiply the page address
3:42
with 256 and add the offset with the
3:47
this is because a page contains 256
3:51
so let's assume that I want to read from
3:54
page 1 with an offset of four
3:57
by using the calculation we will get the
3:59
memory address equal to 260.
4:02
page 0 contains the addresses from 0 to
4:06
255 so the address for page 1 will start
4:12
since the offset is 4 the memory address
4:17
this is the same that we got by using
4:20
the formula now we have the 24-bit
4:23
memory address and we need to send it to
4:27
we will split the 24-bit address into
4:29
three bytes and store the most
4:31
significant byte first
4:34
since this is a general code that can be
4:36
used throughout the winbourne flash
4:38
memories we also need to take care of
4:41
the memories with larger sizes
4:43
I am using the 16 megabits memory which
4:48
each block contains 16 sectors and each
4:52
sector contains 16 pages
4:55
there are 256 bytes in a single page and
4:59
that makes the device contain a total of
5:03
this number is still within the limits
5:06
of 24 bits we use for the addressing
5:09
but as we go higher in the memory size
5:12
the number of bytes exceeds the 24-bit
5:16
for such devices the memory address must
5:21
let's define the T data size as 5 bytes
5:24
so that it can be used in both
5:28
the memory address exceeds 24 bits for
5:31
the memories with 512 blacks or more
5:35
so if the number of blocks is less than
5:37
512 we can use a 24-bit address
5:42
but if the number of blocks are 512 and
5:45
more we have to use the 32-bit address
5:47
and hence the address will be split into
5:52
now we will send the instruction along
5:56
for the addresses within 24 bits we send
5:59
4 bytes and for the rest we send 5 bytes
6:04
once the command is sent we will read
6:07
the data from the device
6:09
the received data will be stored in the
6:11
idata array that we will pass to the
6:15
the size is also passed from The Reef
6:20
after reading the data pull the Cs pin
6:24
we will test it in a while
6:27
one important thing to note here is that
6:29
the read instruction will be ignored if
6:31
it is passed while the write or arrays
6:33
operation is going on
6:36
but we will take care of it when we will
6:38
write the function to write or erase the
6:41
we don't need to worry about it here
6:44
just like read instruction there is a
6:46
fast read instruction
6:48
this is similar to read instruction
6:50
except that it can operate at the
6:53
highest possible frequency
6:55
to send the fast read command we send
6:57
the instruction followed by the 24-bit
7:00
address followed by the dummy clock
7:03
basically we don't send any data during
7:09
the process is similar to the recommand
7:12
so we will just modify the read command
7:15
we need to send one extra byte here
7:31
the last command byte will be zero so
7:34
that the device can receive the dummy
7:37
the instruction to send the fast read is
7:48
just increase the size by 1 in both
7:52
let's define both the functions in the
7:54
header file so that we can call them in
7:58
let's write the main function now
8:03
we will check the ID before reading the
8:05
data from it I have stored the data to
8:09
different locations in the memory
8:11
the first one is located on page 1 at an
8:16
and the second one is on page 17 at an
8:20
offset of 10. I intentionally stored
8:23
them in different sectors so as to
8:25
demonstrate that the read command can
8:27
read the entire sector without any
8:30
first we will read page one with the
8:35
this is where the data is stored so we
8:38
should get the data in the beginning of
8:42
Define the RX buffer array
8:45
now let's read the 512 bytes from the
8:52
we will read the data from the second
8:54
location which is stored on page 1 at
8:59
let's also read 512 bytes starting from
9:05
we will Fast read the 512 bytes data
9:08
just to test the command
9:11
let's build and debug the code
9:18
I have added the RX data to the live
9:22
let's add a breakpoint before reading
9:26
all right we have hit the break point so
9:29
step over the function to read the data
9:33
the data is stored at the beginning of
9:37
here you can see the data hello world
9:41
we read 20 bytes so the rest 9 bytes are
9:49
the next statement reads the chip from
9:53
since the data is stored one page 1 at
9:56
an offset of 85 the address from the
10:01
so we should get the data at 340 first
10:06
here you can see the data hello world
10:14
now we will read the data at the second
10:16
location which is on page 17 at an
10:24
here we have received the data
10:30
the position of this data from the start
10:32
of the 16th page will be 266.
10:40
here we have got the data the
10:45
we can also see the data in the memory
10:48
hover on the RX data and copy its memory
10:53
now in the memory tab paste the address
10:56
or just enter it manually
10:58
let me reset the debugger and run again
11:06
here you can see the data at the
11:08
beginning of the memory
11:10
and now the data is at an offset
11:13
note that this is the memory map of RX
11:16
data and not the device so the data in
11:19
here is how it is stored in the RX data
11:23
so we are able to read the data stored
11:26
in the device memory
11:27
now let's do a final read where we will
11:30
read more than one sector
11:34
since the second set of data is stored
11:36
on page 17 we need to read a total of 17
11:40
pages that means a total of
11:45
let me modify the RX data to store these
11:50
4608 bytes of data from the start of the
12:09
the first set of data should be
12:12
available at the position 341.
12:21
and the second set of data should be
12:23
available at the address
12:25
4360 second byte from the beginning
12:38
here you can see the second set
12:41
so we were able to read the data
12:43
starting from a particular location or
12:46
starting from a particular page or even
12:48
starting from the beginning of the
12:51
this is all we need to know for the read
12:55
the right operation is going to be
12:57
complicated as we can't write more than
12:59
256 bytes in a single operation
13:03
we will cover it in the next video
13:06
this is it for today
13:08
you can download the code from the link
13:12
leave comments in case of any doubt
13:16
keep watching and have a nice day ahead