0:11
welcome to controllers tech
0:14
today in the video we will see how to
0:16
use quad spi in stm32
0:20
quan spi is mainly used to interface the
0:23
external flash memory and it's very fast
0:26
compared to the regular spi chips
0:29
for this reason the quad spi flash
0:32
memories can also be used as the
0:34
internal flash memory of the
0:37
our focus will be on this part how to
0:40
use the flash memory as the internal
0:43
in this video i will focus on the setup
0:46
process and how to read and write data
0:49
to the external flash
0:51
the next video will cover the mapping of
0:53
the external flash as the internal
0:55
memory and also how to load the
0:59
stm32 microcontrollers mainly have three
1:02
different types of flash memories
1:09
mx-25 out of these three memories i have
1:13
access to n25q and mt25
1:18
so i have written the codes for these
1:20
two memories but i will show you how to
1:22
make it work for the mx25 also
1:25
as i don't have the access to mx25
1:28
series i can't say if it will work for
1:31
so let's start the video now
1:34
sd microelectronics already have a
1:37
github tutorial on these memories i will
1:40
leave the link to this in the
1:43
here you can see there are three qspi
1:47
i have also uploaded the drivers for two
1:50
of them on my github page
1:52
these files are slightly different but i
1:55
have kept the formatting same as that of
1:59
let's see how to make these work
2:02
create a new project in cube id
2:08
discovery board for this tutorial
2:10
give some name to this project and click
2:17
first thing we are going to do is set up
2:23
select the external high speed crystal
2:28
if you can figure anything wrong here
2:30
the project won't work
2:33
so make sure you configure the right
2:34
crystal frequency as per your board
2:46
as you can see the n 25 q can reach up
2:49
to 108 megahertz we will take advantage
2:57
i am running the system at 216 megahertz
3:01
even if you run at a bit lower frequency
3:04
it's all right since 108 is the maximum
3:07
it can also perform well at slightly
3:11
so the clock has been configured to run
3:16
as you can see here in the data sheet
3:19
the quad spi is connected to the ahb1
3:22
which runs at 216 megahertz also
3:26
so right now the qual spi clock is also
3:29
216 megahertz which we will reduce later
3:33
by using the prescaler
3:35
all right the clock is configured now
3:38
if you are using cortex m7 enable the
3:42
now we will go to the quad spi and
3:45
enable the quad spi lines for
3:56
this here is the schematics for the f75z
4:00
board and here you can see it only have
4:06
this is why i can only use bank 1 while
4:08
enabling the quad spi
4:14
next we need to make sure that the pin
4:16
connection is correct as per the
4:20
i have said it a lot of times during the
4:22
ethernet videos and some people still
4:24
configured the wrong pins
4:27
cube mx sometimes configures the wrong
4:29
pins for these modules so you need to
4:32
cross check the pins with the schematics
4:35
then we need to make sure that the pins
4:37
are configured to run at highest
4:40
press ctrl a to select all the pins and
4:43
go below and change the output speed to
4:54
now let's configure the parameters
4:56
the qspi clock is 216 megahertz right
5:00
now and this n25q can run at 108
5:06
so we need to use the prescaler of 2 to
5:09
bring the q-s-p-i clock to 108 megahertz
5:13
here we will enter 2-1 that is 1.
5:17
this configuration we are doing is for
5:19
the single transfer rate mode
5:22
just set the fifo threshold to 4.
5:25
next is the sample shifting
5:28
it also depends on the mode we are using
5:31
for the dual transfer rate there will be
5:34
no shifting and for single transfer rate
5:36
there will be shifting by half cycle
5:40
flash size depends on the memory size
5:44
for example i have the 128 megabits
5:48
which is 16 megabytes of flash storage
5:51
2 to the power 24 gives us the 16
5:56
so the flash size will be 24 minus 1
6:02
chip select high time let's keep it to 6
6:10
clock mode should be low
6:12
flash id is 1 since there is only one
6:17
dual flash is disabled as there is no
6:20
second flash available
6:22
that's it for the configuration
6:25
one last thing we need to do is create
6:27
separate files for the peripherals
6:30
like i mentioned in the beginning i am
6:32
trying to keep it as per the sd's qspi
6:35
drivers and that's how they have put it
6:40
now click save to generate the project
6:51
if you check the q s p i drivers by the
6:53
s t they have separate files for q s p i
6:59
so if you want to use these drivers you
7:02
need to put this code in the user code
7:06
and the rest of them in the user code
7:16
and the same goes for the header file
7:20
you need to put these functions wherever
7:22
they are defined in these files
7:25
of course you need to change this memory
7:33
now if you take a look at my github the
7:35
functions are almost the same
7:38
the only change i have made is i have
7:40
added the read function so that we can
7:42
read the data from a particular address
7:50
now to make it work you need to download
8:02
now we will copy the n25q header file in
8:14
the rest we will modify
8:16
let's start with the q s p i dot c file
8:37
all right first we will put these
8:39
functions in the user code zero
9:02
and then we will copy all these in the
9:35
now let's see the header file
9:42
the write function seems to be defined
9:46
let me delete one and i will update the
9:56
we need to copy these private defines
10:07
i forgot to include the n 25 q header
10:18
in the n25q header file you can see the
10:21
memory size is defined here
10:24
you can change it as per the memory size
10:26
for your qspi device
10:29
let's build it once to check for any
10:42
all right we are good to go
10:44
we will initialize the qspi first
11:01
then erase the entire chip
11:04
then we will write data to the memory
11:07
we need to define the data first
11:13
let's create another variable to store
11:15
the data read from the memory
11:22
so we will write this data into the q s
11:25
p i at the address zero
11:27
then we will read the data from the same
11:31
the write and read address is zero which
11:33
means that it will be the start of the
11:35
qspi address which in most cases is 90
11:40
i am saying most cases because there are
11:42
very few microcontrollers where the qspi
11:45
address starts at a 0 million hexa
11:49
few warnings but that's all right let's
11:54
i am putting a break point at while and
11:57
one in the error handler
11:59
just in case if any of the functions
12:01
fail it will hit the error handler
12:08
we hit the while loop which means the
12:11
functions were executed or right
12:14
read buff do contain the data that we
12:16
stored in the memory
12:21
we can also check the data in the cube
12:32
just connect the controller
12:34
now click the external loader
12:37
search for your controller here
12:44
you can see that the qspi address starts
12:50
i didn't uncheck the others let me do
12:58
all right now it's fine
13:03
now we will enter the qspi address here
13:11
and here you can see the data that we
13:13
stored in the flash memory
13:29
some of you guys always have problems
13:31
while storing the numerical values
13:34
i will show you the simplest approach to
13:37
let's say the number is 1 2 3 4.
13:41
create one buffer also
13:45
now we will use the s print f to convert
13:48
the number to the string
13:50
and we will store this string into the
13:55
include the stdio for the s print f
13:59
just ignore these warnings and let's
14:07
here we hit the break point
14:10
and you can see the string of the number
14:14
you can later convert it back to the
14:17
i know this is not the effective way of
14:19
storing the numbers but the purpose of
14:21
these qspi tutorials is not to store the
14:24
data into the flash anyway
14:27
we will use this flash memory as the
14:29
internal flash memory so as to
14:31
compensate for the low flash in some
14:36
for storing data into the external flash
14:39
i will make another video using the
14:40
wideband ics as they are widely
14:43
available and we can simply use spi for
14:48
still if you want to store the numbers
14:50
into the flash you need to use the union
14:54
you can check out the eep rom code on my
14:57
github and see how i have used union to
15:00
store the number and floats
15:02
now similar to this n25q there is the
15:09
you need to follow the same steps here
15:22
if you check the initialization function
15:24
i have kept everything similar to what i
15:26
did in the n25 queue
15:37
and in the header file 2 the similar
15:40
function are being used
15:42
i have h745 discovery board which have
15:46
the two flash memories each 512 megabits
15:51
so i have modified this particular code
15:54
to work with dual flash also
15:57
the steps are similar to what you saw in
16:01
just in case if you want to see the
16:03
working for mt 25 also let me know in
16:08
i will make another video for it
16:11
or else in the next video we will see
16:14
the memory mapped mode where these
16:16
external flash memories will be treated
16:18
as the internal flash
16:21
we will also see how to load the
16:23
application from these external flash
16:29
the code is on the github so get it from
16:33
leave comments in case of any doubt keep
16:36
watching and have a nice day ahead