0:18
everyone welcome to controllers tech
0:22
i have already covered a video on usb
0:25
where i interfaced the usb flash drive
0:30
well this is another video on usb host
0:33
and today i will show you
0:35
how to interface hid with sdm32
0:39
i will interface both the mouse and the
0:43
let's start by creating a project in
0:47
first i am using stm32 discovery board
0:52
give some name to the project and click
1:14
let's clear the default pin outs
1:21
first of all i am selecting the external
1:24
crystal for the clock
1:26
now go to usb otg fs and enable the host
1:32
also enable the v bus so that we can
1:34
provide power to the usb device
1:41
go to usb host and enable the hid class
1:55
if you notice here there is no solution
1:58
bus this is because we haven't enabled
2:04
open the user manual for your board
2:12
as you can see here the supply for the v
2:14
bus can be enabled by the pin
2:16
pc 0. it is connected to enable pin
2:20
which is active low pin so to turn on
2:25
we need to pull the pin pc 0 to low
2:28
and then this 3 volt supply will be
2:41
select the pin pc 0 as output
2:45
as you can see now there is a solution
2:48
bus let's enable the uart
2:52
so that we can see the output on the
3:00
now the clock setup i have eight
3:03
megahertz external crystal
3:05
and i want the system to run at maximum
3:09
this is it for the setup click save to
3:27
here is our main file
3:33
first of all let's include the usb h id
3:37
file we will be using the functions from
3:55
here we are interested in hid event
4:00
this will be called whenever we receive
4:04
this is a common function for both the
4:06
mouse and the keyboard
4:09
so we need to check if the data is
4:11
received from the mouse
4:13
or from the keyboard hid get device type
4:17
will be used for this purpose
4:29
if the data is received from the mouse
4:32
we will perform the operations here
4:38
and we will write a similar function for
4:46
first define a pointer to the mouse info
4:53
basically this structure will contains
4:55
all the data from the mouse
4:57
that we will receive the data contains
5:00
the movement in x-axis
5:02
in y-axis and the indication of which
5:05
button is pressed on the mouse
5:15
here we will receive and store the data
5:25
the movement in the x and the y axis
5:30
and that's why it is hard to understand
5:34
to make it look simpler i am making some
5:46
this way the movement in one direction
5:50
while in the other direction it will be
6:15
now we will store all these values in a
6:18
and finally send them to you art
6:31
we will do the same steps for the
6:38
first define the keyboard type def
6:45
then get the keyboard data and store it
6:56
in keyboard we have a function to get
6:58
the ascii code of the key
7:07
we will get the ascii code from the
7:09
structure where we stored the data
7:11
and store it in a variable
7:18
now store all these values in a buffer
7:21
and finally send them to you
7:40
let's build this code now
7:55
some error in the variables here
7:59
let me just quickly fix that
8:10
also let's add the stdio.h
8:14
for the s printf to work
8:22
now everything is good let's flash this
8:27
i am connecting the mouse via the otg
8:31
open the serial monitor to view the data
8:35
move the mouse to the left as you can
8:38
see the x values are negative
8:41
you can observe how the values are
8:45
they decreased from 0 to minus 4 minus
8:48
minus 22 minus 30 basically the faster
8:53
the higher this value will be
8:56
and as the mouse slows down the values
8:59
started increasing and finally become
9:03
when the mouse stops this is the
9:05
movement in the right direction
9:08
the x values are positive now
9:12
they follow the same pattern as they did
9:14
in the left direction
9:27
now as i am moving the mouse up the y
9:30
value is changing in the negative
9:49
the mouse is moving down this time
9:52
y values are positive and almost no
10:05
now let's test the buttons this is the
10:10
you can see the button one value was one
10:12
when i press the button
10:14
and becomes zero when i release it for
10:19
button 2 value becomes 1 when i press
10:22
and becomes 0 when i release it
10:26
and for the middle click the button 3
10:30
let's see one more time
10:35
left click right click
10:39
middle click this is it for the mouse
10:42
now let's remove the mouse and connect
10:47
we will keep our program running as we
10:49
have included both the mouse and
10:51
keyboard in our callback function
10:57
so when we press the key the character
11:00
and when we release the key a null gets
11:04
we can print the capital characters
11:25
during testing i found that the
11:27
bluetooth mouse and keyboards don't work
11:30
so try using the wired ones
11:33
i hope you guys understood this video
11:37
i will try to make use of this in my
11:41
where i will try to display the movement
11:45
and the keyboard characters on the tft
11:49
this is it for this video you can
11:52
download the code from the link in the
11:56
leave comments if you have any doubts
11:59
keep watching and have a nice day