0:09
hello and welcome to controllers Tech
0:12
today we will start yet another video
0:14
series covering w25q series nor flash
0:20
I could make a single video showing a
0:22
library and it's working but I prefer
0:25
doing it this way where I explain how to
0:27
write functions using the data from the
0:31
we will read the info from the datasheet
0:33
and write different functions to read
0:36
write update and erase the memory
0:39
in today's video we will see the
0:42
connections the cube MX setup and we
0:45
will write a simple function to read the
0:49
for this video series I am going to use
0:54
sdm-32f407ze T6 MCU board as it has the
0:57
flash already soldered into it
1:01
w25q16 which is 16 megabits in size
1:06
here is how the connection is made to
1:10
the Cs pin is connected to the FCS
1:14
the data out is connected to the Miso
1:16
data in is connected to the mossy and
1:19
the clock is connected to the clock pin
1:22
actually I have one more module which is
1:24
32 megabits in size and I am going to
1:28
connect it to the same pin
1:30
we know how we can connect multiple
1:32
devices to the same SBI lines and each
1:35
can be selected by using the different
1:39
here I have used the f103 just for the
1:43
reference but if you have the f103 this
1:46
is how the connection would be
1:48
the chip select pin of the first module
1:51
is connected to the pb14 and the second
1:53
module to the PIN pb6
1:56
other than that both modules are
1:59
connected to the same SBI lines
2:02
here is the data sheet for the winbond
2:05
w25q16 the one which is soldered on the
2:10
a convenient thing about these modules
2:12
is that at the base level they all work
2:16
they just differ in the size and that
2:18
doesn't change how the memory is
2:20
distributed at the lower level
2:23
you will understand this in a while
2:26
here you can see the module can support
2:28
the Dual or quad SBI also
2:31
as I mentioned we will be using the
2:34
standard SBI so we have four pins clock
2:38
chip select data in and data out
2:42
here is a little detail about the memory
2:45
this 16 megabits variant has the memory
2:48
distributed among the
2:50
8192 pages with each page being 256
2:57
we can program 256 bytes at once
3:01
this is something which remains common
3:03
throughout the variants
3:05
whatever module you choose the page size
3:08
will be 256 bytes but the number of
3:12
pages will vary depending on how much
3:14
memory the module has
3:16
we can't erase a single page but a group
3:19
of 16 Pages which is called a sector and
3:22
is four kilobytes in size
3:25
we can also erase a block which is a
3:27
group of 128 Pages or 256 pages
3:32
or an entire check can be erased at once
3:36
we will cover everything in this series
3:39
as we go along with it
3:41
let's look at the block diagram of the
3:44
here we have one block which has 16
3:49
each sector is four kilobytes in size
3:52
and contains 16 pages
3:55
this block distribution Remains the Same
3:58
across the different variants of The
4:00
winbond Flash memories
4:02
but the number of blocks will change
4:04
depending on the size of your flash
4:08
w25q16 we have 32 blocks in total
4:12
I also have the datasheet for another
4:14
module I am using for today's
4:16
demonstration that is w25q32
4:20
it's 32 megabits in size but as you can
4:24
see the only change here is the number
4:26
of pages which is twice the number of 16
4:33
the memory distribution is also similar
4:37
we have the 16 sectors in one block and
4:40
each sector is 4 kilobytes in size
4:43
the changes in the number of blocks
4:45
which is 64 twice the number of blocks
4:51
so we can write common functions for all
4:54
these devices just setting the higher
4:56
limits for different modules
4:58
you can get more info about the module
5:00
from the winbond website
5:05
here you can see the different modules
5:10
the number of blocks just gets doubled
5:12
for the higher variant
5:15
there are different products available
5:17
for each memory size but the most common
5:20
ones are the JV series
5:23
for the rest of the changes you can
5:25
check the model number on your IC and
5:28
then download the respective data sheet
5:31
as I mentioned we will be reading the
5:34
device ID in today's tutorial
5:36
the ID is unique to each product series
5:40
you can check the ID of the device under
5:42
the instructions section
5:45
for the 16 megabits here is the ID is
5:51
we can check the same for the 32
6:08
the manufacturer ID EFX Remains the Same
6:12
across all the windbond flash memories
6:15
we will understand the ID in detail
6:17
while we read it later in the video
6:20
let's start the queue ID and create a
6:25
I am using stm32f407zet6mcu
6:32
give some name to the project and click
6:39
let's set the clock first
6:41
I am enabling the external Crystal for
6:45
the board has 8 megahertz Crystal and we
6:48
will run the system at the maximum 168
6:54
enable the serial wire debug and let's
6:57
set the systick for the time base
7:00
all right now we will enable the SBI one
7:03
in the full duplex Master mode
7:06
the data size must be 8 Bits as we read
7:09
or write the data in bytes
7:11
the data should be transferred as MSB
7:15
let's set the prescaler such that the
7:18
board rate is around 2.5 megabits per
7:22
let's see the clock conditions in the
7:31
here it is mentioned that the SBI mode
7:34
must be either mode 0 or mode 3. in both
7:38
of these modes the data is sampled on
7:41
the rising Edge and shifted out on the
7:42
falling edge of the clock
7:45
the clock polarity is the only
7:47
difference between mode 0 and mode 3 but
7:50
since both are acceptable the clock
7:52
polarity does not matter
7:55
so we will keep the clock polarity low
7:58
the clock phase is set to one Edge which
8:00
means the data will be sampled on the
8:02
first clock Edge that is the rising Edge
8:06
this is what we need so we will keep it
8:10
the SPI pins configured by default are
8:13
the pins pa5 6 and 7. but as per the
8:17
schematics of the board The Flash is
8:19
connected to the pins pb3 4 and 5. also
8:24
the chip select is connected to the PIN
8:28
so let's reassign the SPI pins to pb3 4
8:39
and now we will set the pin pv14 as
8:42
output so as to use the chipped select
8:46
I am using another flash on the same SPI
8:49
lines so let's configure one more pin as
8:51
output so to be used as the chip select
8:54
for the second module
8:56
go to the gpio configuration and set the
9:00
initial state for these pins High
9:03
we pull the pin low to enable the
9:05
respective module that is why we are
9:07
keeping the high initially
9:09
let's set this speed to highest as we
9:12
might need to switch the states at high
9:15
that is all the configuration we need
9:18
generate the code now
9:20
we will create new library files for
9:23
this project and we will keep adding
9:25
things to it in the future videos
9:34
create a header file also
9:42
let's start with the source file
9:45
include the main file and the header
9:50
copy the SBI definition from the main
9:52
file and Define it here as an extern
9:57
to keep things simple I am defining the
10:00
SBI 1 as the w25q SBI
10:04
we need to frequently pull the chip
10:06
select pin high and low so let's define
10:13
the pv14 is the chip select for the
10:16
module on this board
10:25
let's define the number of blocks
10:27
available for the 16 megabits memory I
10:34
let's check the datasheet to understand
10:36
about the IDS and how to read them
10:39
here is the instruction to read the ID
10:44
we will also reset the memory so let's
10:47
check the reset command first
10:50
the reset command terminates any ongoing
10:55
the device returns to its default power
10:57
on State and any volatile content on the
11:02
the reset command is made up of two
11:06
this is to make sure that the reset is
11:08
not issued accidentally
11:10
we have to send the instruction to
11:12
enable the reset and then send the reset
11:17
each instruction is eight bits in size
11:20
so we have to send two bytes for reset
11:23
the reset time is 30 microseconds
11:28
let's write the function to reset the
11:35
Define a buffer to store the
11:38
first we will store the enable reset
11:41
command and then store the reset command
11:46
now select the device by pulling the
11:51
send the data using the function SBI
11:55
the SPI Handler is w25q SBI which we
12:00
defined above data HD data buffer 2
12:03
bytes of data and let's set the timeout
12:07
once the data is transferred unselect
12:10
the device by pulling the chip select
12:13
that is it the function to reset the
12:17
now we will write another function to
12:24
here we have the instructions to read ID
12:27
there are different IDs available like
12:30
manufacturer ID the device ID and the je
12:35
the je deck ID is more unique to a
12:39
particular device so we will read it
12:42
it can be read by issuing the
12:46
the je deck ID is made of manufacturer
12:50
ID and the two device IDs the memory
12:53
type and the capacity
12:55
so the je deck ID is three bytes long
12:58
the higher byte is manufacturer ID then
13:01
the next byte is memory type and the
13:04
least significant byte is the capacity
13:07
while reading the je deck ID for this
13:10
device we are expecting the manufacturer
13:12
ID EFX memory type 40 hex and capacity
13:19
all right let's write the function to
13:28
Define a variable to send the
13:35
the device will output three bytes of
13:38
data so Define an array to store them
13:41
now pull the Cs low to select the device
13:45
send the instruction
13:47
this is a single variable so we need to
13:52
now receive the three bytes of data and
13:55
store in the array we defined
13:58
pull the Cs High to unselect the device
14:02
we will combine the three data bytes and
14:04
make a single ID out of them
14:06
the manufacturer ID is the MSB then we
14:10
have the memory type in the middle and
14:12
the capacity in the LSB
14:15
so we will shift the data received first
14:17
by 16 places the second byte by eight
14:20
places and add them to the third byte
14:25
we have made a 24-bit ID now
14:31
we need to Define these functions in the
14:33
header file so that we can call them in
14:37
now go to the main file and include the
14:43
Define a 32-bit variable to store the ID
14:49
Now call the w25q reset function to
14:54
let's add some delay in the reset
14:56
function so the device settles down
14:58
properly after the reset
15:01
call the function to read the ID and
15:04
store it in the ID variable we just
15:08
that is it let's build and debug the
15:13
I am adding the variable ID to the live
15:17
let's run the code now
15:19
we have received some data in the ID
15:23
let me change the number format to
15:25
hexadecimal you can see the ID received
15:32
this is exactly the same ID we were
15:36
the manufacturer ID EF hex and the
15:40
memory type and capacity
15:44
so we received the ID for the 16 megabit
15:47
device successfully as I mentioned in
15:50
the beginning I have connected one more
15:52
device on the same bus but the chip
15:54
select for that device is connected to
15:58
so let's update the chip select pin here
16:02
build and debug the code again
16:23
this is as per the information in the 32
16:25
megabits memory device
16:28
so the code is working fine so far
16:32
we were able to reset the device
16:33
hopefully and we read the ID of both the
16:38
we will continue in the next video where
16:41
we will see how to read the data from
16:44
this is it for the video
16:46
I hope you understood the basic
16:48
functions and the connection shown in
16:52
you can download the code from the link
16:57
leave comments in case of any doubt
17:00
keep watching and have a nice day ahead