0:08
hello everyone welcome to controllers
0:12
tech first of all I would like to thank
0:16
Abhishek for testing the code and making
0:18
the final part of this video or else I
0:21
would need to buy another board for this
0:23
purpose alone few weeks ago I made a
0:26
video on SDM 32 USB mass-storage class
0:30
and the SDM 32 was in the device mode
0:34
that means the MCU was acting as a mass
0:37
storage device today in this video I
0:40
will show you how to use stm32 as a host
0:44
for the mass storage class this means
0:47
you can connect your flash drive to it
0:49
and read and write some files or folders
0:52
in the flash drive let's start by
0:55
creating a project in cube ID this time
0:59
I am using stm32f4discovery board give
1:04
some name to this project and click
1:07
finish here we are in our cube MX open
1:16
the user manual for your board it will
1:28
let's clear the pinouts first i am
1:32
enabling the external crystal for the
1:34
clock let's select the UART for the
1:41
debugging purpose 9600 baud rate is just
1:51
a choice you can select any other two
1:59
now click USB OTG FS and select host
2:03
only make sure you enable the V bus
2:07
because flash drives don't have any
2:09
external power supply so we need to
2:12
power them via the bus as you can see
2:15
two data pins and one V bus pin got
2:18
selected now go to USB host and select
2:23
USB mass-storage class click yes
2:35
we will leave everything default here
2:41
now let's enable the fat file system we
2:49
will enable the USB disk as we are
2:51
interfacing USB here we need to make
2:54
some changes enable the long filenames
2:59
select the static buffer on BSS here you
3:08
can change the maximum length of the
3:11
filenames select the maximum sector size
3:15
to Max possible if you want to enable
3:20
the exFAT support enable it here
3:29
there is one very important piece of
3:31
information about the V bus that I
3:33
missed go to USB host platform settings
3:39
as you can see here there is no solution
3:43
for V bus this is because V bus power
3:46
must be turned on and the McCue have a
3:49
pin connection for that you will
3:51
understand it better when we take a look
3:54
at the user manual for this discovery
3:58
here is the diagram that we need this
4:03
right here is our micro USB as you can
4:10
see the V bus is being powered from pc 0
4:14
pin pc 0 is connected to enable pin
4:18
which is an active low pin this means in
4:22
order to turn it on we need to give a
4:24
lowered PC 0 which will turn on the
4:28
power supply for the V bus select the PC
4:35
0 as output in the GPIO settings make
4:48
click Save to generate the code also
5:01
note that the V bus GP IO have a
5:04
solution now and that's pc 0 this is it
5:08
for the setup let's go to our program
5:11
window here is the main dot C file
5:16
let's first include the libraries put
5:20
the file handling dot C file in the
5:23
source folder and file handling dot H
5:25
file in the include folder let's refresh
5:34
the project so that we can see them you
5:40
can change your controller header here
5:43
here you can change the UART file
5:47
handling is exactly the same file that
5:50
we used while interfacing the sdcard I
5:53
just made some changes like brought the
5:56
USB path here other than this there are
6:00
some changes in the names of different
6:02
locations and that's pretty much all all
6:05
the functions are same as they were in
6:14
now let's go to the file where we need
6:18
to make changes that is USB host dot C
6:26
file first let's include the file
6:41
scroll down to us BH user process here
6:46
we have all the cases like what to do
6:48
when the USB connects or disconnects or
6:51
when it is ready for communication we
6:59
are going to write once the USB is ready
7:03
first of all mount the USB check the USB
7:08
details about Topal and free space this
7:15
will list all the directories and files
7:31
you can see we have all these functions
7:34
available for USB let's create a file in
7:42
the root folder and we will call it root
7:44
file dot txt write some data to this
8:00
now I am creating a directory in the
8:02
root folder and its name is directory 1
8:08
create a file inside this directory
8:25
right some data to this file now the
8:28
first parameter of right file is the
8:31
name of the file that you want to write
8:33
and the second parameter is the data
8:40
creating another directory inside the
8:43
root folder I am creating yet another
8:52
directory inside the directory - and
8:57
create a file inside subdirectory now
9:02
write some data to this file we can also
9:12
update the files Here I am updating the
9:16
root file itself when we wrote the data
9:20
to root file in the beginning there was
9:22
a line break so this data must be in the
9:24
second line this is all for files and
9:29
directories know when the USB is
9:32
disconnected we will call the unmount
9:45
let's build this and flash to our board
9:50
first of all I will format the USB so
9:53
that there are no files and folders
9:55
present before writing let's insert the
9:58
USB now I will format it first if you
10:05
remembered I did enable the exFAT
10:07
support so I can format it in exFAT
10:10
filesystem it's completed there is
10:18
nothing inside the USB the serial window
10:25
here is for keeping the trace of what's
10:35
okay let's run the code now connect your
10:39
USB and you can see the output on the
10:42
console you can pause the video and read
10:50
them basically all the operations are
10:54
completed successfully I have removed
10:57
the USB now and you can see the
10:59
unmounting also got executed let's see
11:03
the files on the computer now
11:17
so we have two directories and a root
11:21
file just like we created data is also
11:25
in the correct position subdirectories
11:35
present inside the directory - it also
11:38
have exactly the same text that we wrote
11:41
in it root file have both the initial
11:52
data and the updated data also if you
11:58
insert the USB again you will see
12:00
something like this on the serial
12:02
console here it shows all the files and
12:06
directories present on the flash drive
12:08
it is represented by the first green
12:11
line now as these files are already
12:14
present new ones can't be created and
12:17
that's why there is error and you can
12:19
see them in the red color when we write
12:22
the data to these files the old data
12:25
will be overwritten and the right will
12:27
be successful and you can see the green
12:29
color in between representing this
12:32
scenario I have plans for USB HID for
12:37
keyboard and mouse and I will make the
12:40
video on it very soon also I will make a
12:44
video on controlling the USB directly
12:46
via the controller using some TFT
12:49
display but this is it for today's video
12:52
I hope you enjoyed it you can download
12:57
the code from the link in the
12:59
description keep watching and have a