0:09
hello and welcome to controllers Tech
0:12
this is the seventh video in the esp32
0:15
series using the espressive IDE and
0:18
today we will continue with the SBI
0:20
peripheral in the previous video we saw
0:23
how to connect the w25 Q flash device
0:26
with the es 32 and how to read the ID of
0:29
the device today we will continue with
0:32
the w25 Q flash memory and we will see
0:36
how to write the data into the memory
0:38
and then read the data back from it
0:41
please note that this video does not
0:43
focus on w25 Q flashmemory we will cover
0:47
the simplest possible way to write and
0:49
read data from it rather we are focusing
0:52
on the SBI peripheral of the
0:54
esp32 therefore more attention will be
0:57
on it I have already explained how the
1:00
w25 Q32 flash memory is connected to the
1:05
32 here I have the project we created in
1:09
the previous video we will continue
1:12
editing the main file from this project
1:14
so let's copy it now create a new
1:19
project I am naming it
1:22
spi3 replace the main file of this
1:25
project with the one we copied from the
1:28
project let's build it at once all right
1:32
there are no errors so we can proceed
1:34
with it I explained the use of address
1:37
bits and address data in the previous
1:41
tutorial basically this parameter is
1:43
used to prevent the es32 from reading
1:46
the data while the address transmission
1:48
is going on the external transaction
1:52
structure has command bits which works
1:54
in the similar way as the address
1:57
bits basically when the command is being
1:59
sent on the mossy line the es32 will not
2:03
read any data on the Miso line it has
2:06
the same use as the address parameter
2:09
but this is a separate parameter so to
2:11
differentiate the command and address
2:13
data say for example if the SBI slave
2:17
device does have the 64-bits addresses
2:20
then we definitely need the command
2:22
parameter as well but in case of w25 Q
2:27
the device has only 24-bits memory
2:29
address so use of command parameter is
2:32
necessity but anyway using it looks
2:35
better as we will be able to separately
2:38
Define the command and address data
2:40
rather than combining them in a single
2:42
variable to use the command parameter we
2:46
need to declare the variable command
2:48
flag in the regular transaction
2:50
structure I have defined both as I am
2:53
going to use both now let's define a
2:57
16-bit command variable and another
2:59
variable to track how many command bytes
3:02
we want to send we will pass the command
3:05
data to the command parameter in the
3:09
structure and in the external
3:11
transaction structure we will pass the
3:13
command size to the command
3:16
bits the size is also in bits so
3:19
multiply the command length by eight the
3:22
rest of the function will remain the
3:25
same 9 FX is actually a command so let
3:30
command we need to change the SBI
3:33
receive function accordingly command is
3:36
the command we Define above it is one
3:39
bite in size there is no address so the
3:42
size will be zero we will store the
3:45
received data in the RX data array and
3:48
we want to read three bytes from the
3:50
slave device then we will combine the
3:53
data received and make a 24-bit ID data
3:56
from it all right let's build and FL the
4:00
project to the board let's see the
4:05
terminal you can see the 24-bit device
4:08
ID is printing every 2 seconds so the
4:12
command parameter of the transaction
4:14
works fine and we can use it in our code
4:18
here is the data sheet of the w25 Q32
4:22
flashmemory let's check some basic
4:25
details about how the memory is arranged
4:27
inside this device as mentioned here for
4:31
the 32 megabits variant there are
4:35
16,384 Pages whereas for the 16 megabits
4:41
8,192 pages each page occupies 256 bytes
4:46
of memory we can program a maximum of
4:50
256 bytes at once so basically a maximum
4:54
of one page at a time to erase the data
4:57
we can erase a sector at the minimum
5:00
which is made up of 16 Pages you can
5:03
also erase a 32 kiloby block a 64
5:06
kilobyte block or the entire chip at
5:09
once it depends on how much data you
5:12
want to erase but erasing memory smaller
5:15
than a sector is not possible as I
5:17
mentioned earlier we will see the
5:19
simplest possible way to write and read
5:22
data from this memory therefore we will
5:25
not dig deep into every command but
5:27
rather focus on the ones we need for now
5:30
we are going to need the right enable
5:32
command page program command sector
5:35
arrays command and finally the read data
5:41
command let's see the right enable
5:43
command first WR enable command must be
5:47
issued prior to every page program any
5:49
type of arrays and WR status register to
5:53
send the right enable instruction we
5:55
simply need to send the command 06 hex
5:59
let's write a new function to enable the
6:02
right inside this function we will first
6:04
store the command in the command
6:07
variable then we will call the SBI
6:09
transmit function to send this command
6:11
over the SBI Bus Let's Wait for 5
6:15
milliseconds just to make sure the
6:17
command is sent that is all for the
6:20
right enable instruction let's go back
6:22
to the data sheet we will read the data
6:25
at the end here is the sector arrays
6:29
instruction ction to erase a sector we
6:31
first need to send the command 20 hex
6:34
followed by the 24-bit address of the
6:37
sector that we want to erase all right
6:40
let's write a new function to erase a
6:42
sector the parameter of this function
6:45
will be the sector number that we want
6:47
erase remember that there are 16 pages
6:50
in a sector so the page number 0 to 15
6:53
are included in sector Z then 16 to 31
6:56
in sector 1 32 to 47 in sector 3 and so
7:01
on inside this function we will first
7:04
Define the command that is 20
7:07
hex we also need to define the memory
7:10
address which depends on the parameter
7:13
function each sector contains 16 pages
7:17
and each page occupies 256 bytes of
7:20
memory therefore the sector addresses
7:23
calculated by multiplying the sector
7:25
number with them we will again use the
7:28
function sp sp I transmit to transmit
7:31
this instruction although here we have
7:33
the command and address as separate
7:35
parameters it doesn't matter in case of
7:38
transmission as long as we are not
7:40
receiving data we can transmit in any
7:43
way we want we now need to send four
7:46
data byes one for the command and three
7:49
for the 24-bit address so let's define
7:53
an array to store four data bytes the
7:56
first element of this array will be the
8:00
then the next three elements will be the
8:02
addressed data as shown in the
8:05
instruction diagram the most significant
8:08
bite of the address is sent first so we
8:11
will shift the address to the right by
8:13
16 places to extract the bits 16 to 23
8:17
from it next we will shift the address
8:19
by eight places to extract the bits 8 to
8:22
15 from it and finally we will extract
8:26
the bits 0 to 7 from it we now have have
8:29
the 24-bit address split into three
8:32
bytes with the most significant bytes
8:34
stored first before sending this
8:37
instruction we must call the right
8:39
enable function finally call the
8:42
function SBI transmit to send these four
8:44
bytes data over the SBI erasing the
8:48
sector takes some time so let's give a
8:50
delay of 1 second just to be on the safe
8:53
side that is all we need to erase a
8:56
sector the next instruction we will see
8:59
is the page program in order to program
9:02
a page we need to send the instruction
9:05
O2 hex followed by the 24-bit address
9:08
followed by the data to be written let's
9:11
write another function to program a page
9:14
the parameters of this function will be
9:16
the pointer to the data array which we
9:19
want to write and the number of bytes to
9:21
be written we will use the same steps
9:24
that we used while erasing the sector we
9:27
also need the page number in the
9:29
parameter here the memory address will
9:32
depend on this page number each page
9:36
occupies 256 bytes therefore the page
9:39
number will be multiplied with this
9:41
value the command to program the page is
9:44
02 hex the steps to store the command
9:48
and address remain the same in addition
9:52
to the command and address we also need
9:54
to send the data so we will store the
9:57
data in the array but starting from the
10:00
fourth element the elements 0 to three
10:03
are already occupied by command and
10:06
addressed data after the data is stored
10:09
the right enable is called to enable the
10:12
right operation then the function SBI
10:15
transmit is used to send the data array
10:19
the number of bytes are already present
10:21
in the parameter but we need to add four
10:23
more bytes for command and address data
10:26
let's give a delay to 200 milliseconds
10:29
to make sure that the data was written
10:32
successfully finally we will see the
10:34
read instruction to read the data the
10:38
master needs to send the command 03 hex
10:41
followed by the 24-bit address the
10:44
master sends the dummy data for every
10:46
bite it wants to read but it is handled
10:49
automatically by the ESB
10:51
32 during the transmission of the
10:53
instruction and address the data outline
10:56
remains High and the master should not
10:58
read the line while the command and
11:00
address transmission is going on let's
11:03
write a new function to read the data
11:07
device the parameters of this function
11:09
will be the page number from where we
11:11
want to start reading pointer to the
11:13
buffer where the data will be stored
11:15
into and the number of bytes we want to
11:18
read we need to transmit the command and
11:20
address so let's copy them from here the
11:24
command is 03 hex here the SPI receive
11:28
function already has all the parameters
11:31
that we need like command address buffer
11:34
to store and bytes to
11:36
read so let's call the SBI receive
11:39
function directly here the command is
11:42
already defined it is one by long the
11:45
address is defined to it is three bytes
11:48
in size buffer to store and number of
11:50
bytes are already present in the
11:53
parameter let's build the project once
11:55
to check for errors all right everything
11:58
seems seems fine let's proceed
12:01
forward let's delete this part from the
12:11
loop here I am defining a string which I
12:14
will store inside the flash
12:17
memory in the main function after
12:19
initializing the SBI call the function
12:22
right data to store the data into the
12:25
memory we will store the data inside
12:28
page zero then data is already defined
12:31
and the number of bytes are 11 we need
12:34
to type cast the data pointer here all
12:38
right after the data has been written to
12:40
the memory we will read the data back
12:43
again we will read page Z the data will
12:46
be stored in the RX data array and we
12:51
bytes now print this data as a string on
12:57
terminal all right let's build and Flash
12:59
the project to the board we will see the
13:04
terminal we did not receive any data
13:10
slave well I forgot to erase the sector
13:17
data all right let's build and Flash the
13:20
project again you can see the received
13:24
terminal it is the same string which we
13:27
stored in the flash memory
13:30
let's see all the transactions on the
13:41
once I will start from the beginning the
13:45
First Data is sent by the master and
13:48
that is 06 hex we call the function to
13:52
erase the sector which in turn calls the
13:56
function this is when the master sends
13:58
the command 06 hex to the
14:01
slave then the master sends 20 hex
14:04
basically the sector eray command
14:07
followed by the three bytes of addressed
14:09
data again the master sends 06 hex to
14:14
operation this is when we call the right
14:17
data function the master enables the
14:19
right by sending this
14:21
command then the master sends 02 hex
14:25
basically the right page command
14:27
followed by the three bytes of the
14:29
followed by 11 datab
14:31
bytes you can see the datab bytes sent
14:34
by the master are the same as what we
14:37
defined then the master sends 03 hex the
14:41
read data command followed by the three
14:44
bytes of address on receiving the read
14:47
instruction the slave starts
14:49
transmitting the data from the address
14:51
mentioned by the master to read each
14:54
bite of data from the slave the master
14:56
needs to transmit some dummy data at it
14:59
so everything works just as we
15:01
programmed it to work let's change the
15:04
string to see if we get the new output
15:07
instead of reading 11 bytes this time I
15:10
will read 15 all right build and Flash
15:14
the project to the board here you can
15:17
see the received data on the terminal we
15:20
have received the same string which we
15:23
stored but along with it there are four
15:26
characters this is because we read 15
15:29
bytes so the remaining four bytes are
15:32
FFX we will see this on the scope
15:42
again I will only focus on the received
15:45
data part here you can see here the last
15:50
FFX well before writing the data we
15:53
erased the entire sector when the flash
15:56
memory is erased the existing data is
15:59
replaced with all ones basically the FF
16:02
hex we only wrote 11 bytes to the flash
16:06
memory starting from the beginning of it
16:09
therefore the rest of the data remained
16:11
the same FF hex this is what we get when
16:14
we continue reading after 11 bytes I
16:17
will do a dedicated series on w25 Q
16:21
flash memory devices in the future these
16:24
videos were focused on the SBI
16:26
transactions rather than the flash
16:29
this is it for the video I hope
16:32
everything was explained properly I will
16:35
make a minseries covering ADC in esp32
16:39
in the near future that's it for today
16:43
you can download the project from the
16:44
link in the description leave comments
16:47
in case of any doubt keep watching and
16:51
have a nice day ahead