0:23
welcome to controllers tech before
0:27
i would like to thank those who are
0:29
helping me by donating on the website
0:32
really appreciate the support you guys
0:36
this keeps me motivated i have launched
0:41
plan with this channel if you want to
0:45
you can join the membership by clicking
0:47
the join link below this video
0:51
let's start the video now today in this
0:55
we will make a wave player using stm32
0:58
which should be able to play the wave
1:03
the peripherals that we will be using
1:06
inter ic sound also known as i squared s
1:09
and i2c peripheral fat file system
1:12
usb host and a discovery board of course
1:17
let's take a look at the schematics of
1:21
here we have cs43l 22 ic
1:24
to process the audio in the board
1:27
if you have the same you are good to go
1:34
here you can see the two pins for the
1:38
and four pins for the i2s3 are connected
1:42
and we can enable them in the cube mx
1:47
we will cover them all but first let's
1:49
create a new project in
1:56
i am using stm32f411 discovery board
2:01
give some name to the project and click
2:10
let's clear the default pins
2:15
first things first i am selecting the
2:18
external crystal for the clock
2:23
now we are going to configure all the
2:31
pins pb6 and pb7 are configured as the
2:35
pins but as you can see in the
2:39
pins pb6 and pb9 are being used as the
2:43
pins so we need to change the data pin
2:49
you can do this by pressing the control
2:51
button and then click on the pin pd7
2:54
and it will show you the alternate pin
2:58
i2c is done now enable the i2s
3:08
as you can see here three pins got
3:11
selected and they are the data pin
3:13
the clock and the word select
3:19
according to this schematic we need to
3:21
have the master clock at pc7
3:38
also the clock should be the pc10 but we
3:49
so we will set the pc 10 as the i2s
3:52
and as you can see pb12 is no longer set
3:56
other two pins are configured properly
3:58
so let's leave them as it is
4:05
you can change the audio frequency here
4:09
although it does not matter since the
4:11
code will be capable of changing these
4:13
frequencies based on the frequency of
4:22
circular mode use fifo and data width is
4:26
16 bit so use the half word at least
4:37
so we are done with the i2s setup also
4:43
let's go to the usb setup now
4:51
enable the host mode since we will be
4:53
reading the data from the usb
4:56
and also enable the v-bus
5:01
as you can see in the schematic in order
5:04
to enable the power to the v-bus
5:06
we must activate the enable bit and to
5:15
so i am setting pc 0 as the output and
5:18
by default it will be low
5:24
in the usb host select the class as mass
5:32
i will leave everything to default here
5:35
in the platform setting we must select
5:37
our pin to drive the v
5:46
i am also going to use this button so
5:49
that i can control the wave player
5:51
like pause resume next previous etcetera
6:06
i am selecting it as an external
6:14
don't forget to enable the external
6:16
interrupt in gpio setup
6:33
and now the final setup is for the fat
6:38
enable the usb disk since we are using
6:43
i am going to leave everything as it is
6:45
just enable the use of long
6:52
that's all for the setup part let's
7:01
i have 8 megahertz external crystal and
7:04
the system will run at 96 megahertz
7:11
click save to generate the project
7:18
first of all we need to include some
7:20
libraries that we're going to use in
7:30
copy the c files in the source directory
7:33
and header files in the include
7:36
you can get these files from the link in
7:50
audiolink.c basically links the audio to
8:11
audio.c contains the audio related
8:15
for example initialization of the driver
8:18
audio play pause resume or stop
8:29
then we have cs43l22.c
8:32
file i got this from the examples
8:37
and i haven't modified it at all this is
8:40
the driver for the audio codec
8:44
next is the file handling.c file
8:47
here it have a function to parse the
8:49
audio which basically helps us identify
8:52
files and get the index of the file
8:56
and we also have functions to mount and
9:09
we will be using the audio functions
9:11
from this particular file
9:14
here we have audio player start to start
9:18
then process function processes all the
9:25
and stop function to stop the player
9:32
make sure you set the volume here
9:35
the range is from zero to one hundred
9:53
so let's start by including the wave
9:57
and filehandling.h files
10:04
we need to check the usb state in our
10:07
so make sure that you declare it as an
10:21
we also need to check for the audio
10:26
so declare it as extern 2.
10:48
now we will write our program in the
10:50
while loop and make sure you write after
10:52
the usb host process
10:55
we will first check the usb state
11:03
if the usb is ready for communication we
11:06
have to mount the usb
11:08
i forgot to do that but i will add in a
11:12
now start the audio player i am using
11:16
zero here index basically indicates the
11:20
index of the file that you want to play
11:24
zero means the first file
11:30
now after we have started the player we
11:32
need to process the audio
11:34
and that must be kept in a while loop
11:37
so i will create a variable to check if
11:40
the player has been stopped or not
11:53
if the player is running the audio will
12:06
the parameter here is if you want the
12:10
i am setting it as true so after the
12:14
the first one will start playing
12:17
now we will check the audio state
12:32
there are various audio states like play
12:36
pause resume next previous
12:47
here we are interested in the stop state
12:54
if the audio state is stop the variable
12:58
and the audio will not process anymore
13:25
here i have four audio files in my usb
13:28
and all of them are in the wav format
13:39
i haven't connected the usb yet
13:42
let's debug the program first
13:56
like i said i forgot to mount the usb
14:05
so if the usb is ready we will mount the
14:07
usb first before doing any audio related
14:25
i am going to put a break point at the
14:37
i am going to connect the usb and as
14:40
it hit the break point let's go inside
14:43
this function to see what's happening
14:53
here you can see it have already
14:55
selected a file that it's going to read
15:08
the file has been opened to read
15:12
and now we will read the file
15:16
as you can see here it only read the 44
15:23
basically these first 44 bits contains
15:26
various information about the file
15:29
like the file size sample rate number of
15:45
next the player will initialize at the
15:49
as the audio have let's play it
16:14
you heard the music so the player works
16:20
now i am going to implement the button
16:24
to do so i need the external interrupt
16:41
if the interrupt was triggered from the
16:43
pin 0 we will change the audio state
16:47
here we can select whatever we want the
16:51
like next previous pause resume etc
16:56
i am choosing the next state so the next
16:58
track will play if i press the button
17:01
let's build and run the code
17:37
so you saw whenever i pressed the button
17:39
the next track was playing
17:41
and since there were only four tracks
17:43
and the loop was set true
17:45
the first track play after the fourth
17:48
i am going to change the button to act
17:50
as a pause and a resume button
18:01
here we will check if the audio is in
18:04
and if it is then we will change the
18:17
let's check the audio process function
18:20
to see what happens during different
18:24
you can see here when the audio state is
18:27
it will pause the audio and the state
18:32
and similarly if we resume the audio
18:35
then the audio state will be set to play
18:40
so if we want to check for the pause
18:42
state we should actually check if the
18:44
audio state is weight
18:47
and if it is then the audio will resume
18:55
let's build it and flash
19:27
you saw when i pressed the button the
19:29
music was paused and then played again
19:33
you can add more buttons to this player
19:35
and program them according to the
19:37
options in the audio state
19:40
this is it for this video i hope you
19:45
you can download the code from the link
19:49
all the library files are inside the zip
19:53
keep watching and have a nice day ahead