0:17
everyone welcome to controllers tech
0:22
this is yet another video in the usb
0:26
and today we will see how to use
0:28
communication class in usb
0:31
i will show the working of both host and
0:35
for the host i am using discovery f411
0:40
and stm32f103 for the device
0:45
let's start by creating a project for
0:49
like i mentioned i am using discovery
0:55
give some name here and click finish
1:05
i will clear the pin outs first
1:10
i am selecting the external crystal for
1:15
in the usb otg setup set the mode as
1:19
and also enable the v bus host is
1:23
responsible to give the power to the
1:25
and that's why we need the v bus to do
1:29
you can see two pins for the usb and one
1:33
bus got selected here this here is the
1:36
schematics for this board
1:39
as you can see v bus is connected to
1:43
here which can be controlled from the pc
1:46
pin when we pull the pc 0 to low
1:49
the enable pin gets activated and so
1:52
does the voltage to the v
1:58
so i will set the pc 0 to low here
2:04
now go to usb host and select the class
2:07
as the communication host class
2:10
click yes if there is a prompt
2:15
you can see the v bus automatically took
2:17
the pc0 as the solution
2:22
now let's go to the clock setup
2:26
it's already configured here so i don't
2:30
click save to generate the project
2:49
open the usb host.c file
2:53
here we have different states for the
2:55
usb and we need to use them in our
3:04
i will copy this and the usb handle in
3:19
make sure you define them as the
3:35
let's include the usbcdc.h file
3:38
as we need to use some of the functions
3:52
you can browse the file like this
3:58
here we have maximum limit for the
4:02
and then we have all these functions
4:04
that we will be using in our project
4:24
i am going to define a smaller buffer
4:26
size since i am only expecting a small
4:42
we have some more variables here
4:47
these are the transfer and the receive
4:51
i have defined in cdc typedef here to
4:54
switch between different states for the
5:07
actually we won't need the send state i
5:10
when the host is idle
5:22
here is the cdc handler
5:25
it will switch between different cdc
5:32
if the state is idle it will stop the
5:36
then copy the string into the transfer
5:38
buffer and then transmit the data
5:44
here the value will keep changing with
6:09
if the data transmission is successful
6:11
the state will change to receive
6:14
and the variable will increment in the
6:18
host will receive the data from the
6:20
device and store it in the rx buffer
6:25
here the data size is the size of the rx
6:29
but it will only copy the amount the
6:37
now we will wait for a second and switch
6:39
the stake to idle for the next transfer
6:55
in the while loop we will first check if
6:58
the usb is ready for communication
7:00
and if it is then call the cdc handle
7:03
for processing the requests
7:06
that's all for the host part now let's
7:08
create another project for the cdc
7:12
i am using stm32f103c8
7:16
for the device give some name to the
7:20
and click finish first of all
7:23
i am selecting external crystal for the
7:27
select this and serial wire debug
7:31
now select the usb device and you can
7:34
see the two pins got selected here
7:39
and in the settings select communication
7:43
and leave everything to default
7:49
let's configure the clock now
7:58
it has already configured it i will just
8:00
push it to the max anyway
8:05
that's all for the setup click save to
8:21
here we will write our code in the usbd
8:42
first of all we have to deal with the
8:48
as you can see there are seven bytes
9:03
let's create a buffer to store these
9:25
cdc receive will be called whenever the
9:35
and in the receive function we will
9:37
transmit the same data back to the host
9:40
i have defined a new length function
9:43
because this function takes 16-bit
9:46
whereas we have the pointer to a 32-bit
9:56
no errors here so flash it to the board
10:07
the flashing is done now let's go back
10:36
i am going to put a break point here in
10:38
the transmit function
10:42
also i have added the rx buffer to the
10:47
before starting it let's take a look at
10:51
here you can see there is a usb
10:53
connected between the host
10:55
and the device also the device is not
10:58
powered from anywhere
11:00
else as the host will be responsible to
11:04
that's basically all for the connection
11:07
let's run it now it hit the break point
11:10
that means the usb is ready for the
11:19
it hit again and now we have received
11:21
the data from the device
11:36
the value in the data is incrementing as
11:40
let's remove the break point and let it
11:44
seems like it got stuck
11:48
let's reset it and run again
11:57
it's working pretty fine now
12:02
there seems to be some extra characters
12:06
i don't know where they are coming from
12:09
i'll check and update if i make any
12:13
anyway the code works well for the rest
12:17
this is it for this video i hope you
12:20
understood the process for both
12:22
the host and the device you can download
12:26
the code from the link in the
12:29
keep watching and have a nice day ahead