0:09
hello and welcome to controllers tech
0:13
as promised in the dma idle line video
0:16
today we will see how to use the uart
0:18
ring buffer using the dma
0:21
i have already made a video about the
0:23
ring buffer but that one was using some
0:25
registers and hence the things were
0:27
different for different microcontrollers
0:31
so this video will cover a universal
0:33
method for the same and the process will
0:35
be same for all the microcontrollers
0:37
that supports idle line interrupt
0:40
here i have already published the code
0:44
and today i will just demonstrate the
0:48
in the end of the video i will show one
0:50
practical application where i will use
0:54
esp8266 to turn on the led on the
0:58
i have covered it already if you
1:00
remember the web server video few months
1:04
so let's start the cube id and create a
1:15
give some name to the project and click
1:27
let's start with the clock configuration
1:30
i am using external high speed crystal
1:34
type in the crystal frequency here i
1:37
have 8 megahertz crystal on board
1:40
select hse for external crystal
1:45
select pclk and type in the frequency
1:48
you want to run the controller at
1:56
all right now we will configure the uart
1:59
i am using ur2 for the demonstration
2:11
you can check the steps listed in the
2:15
first we need to enable the uart
2:23
then we will configure the dma in the
2:30
enable the receive dma and keep the mode
2:38
also note that the data width is byte as
2:41
that's the standard for the uart
2:45
that's all for the setup now click save
2:48
to generate the project
2:53
i have the three files here that are
2:55
provided in the github
3:01
let's copy the source and the head of
3:03
the files into our project
3:23
the next step is change the uart and dma
3:27
according to your setup
3:29
you need to change them here if you are
3:31
not using the uart too
3:36
you can find the definition in the main
3:45
next we need to change the buffer sizes
3:48
as per the project requirement
3:50
here i am going to perform some small
3:53
tests so these small buffers are okay
3:56
later when i will interface the esp i
3:59
will increase these sizes
4:05
the next step is to define the timeout
4:07
in the interrupt file
4:15
the third downloaded file is for the
4:17
reference and here you can see the
4:19
timeout is defined as the external
4:23
let's do the same in our interrupt file
4:30
we also need to decrease the timeout in
4:49
this is it for the setup
4:51
let's build it once to check for any
4:58
we are good to go now
5:01
let's take a look at the functions
5:04
the first one is to initialize the ring
5:08
this function resets everything and
5:11
other than that it also initializes the
5:13
dma idle line reception
5:16
the next is to reset the ring buffer
5:19
the next function is to check the string
5:23
it checks if the particular string is
5:25
present in the buffer or not
5:28
if the string is found it will return 1
5:33
the next one is a very important
5:36
it waits for a particular string in the
5:40
by default it is set to look for the ok
5:42
and if you want to change it you can
5:44
change it in the rx event handler
5:53
here is the rx event handler and you can
5:56
see the check is being done for the ok
5:59
this particular step is helpful during
6:01
the interfacing of the devices such as
6:03
esp or any gps gsm devices
6:07
these devices send some particular
6:10
string in the end like ok
6:12
so we can quickly check it without any
6:15
additional function needed for it
6:17
this function uses timeout and if the ok
6:20
isn't found in the required time it will
6:23
return zero the next function is wait
6:27
it waits for a particular string to
6:29
arrive in the incoming buffer
6:31
if the string is found it will return 1
6:34
or else if the timeout occurs it will
6:41
the next function is copy upto and it
6:43
copies the incoming data into our buffer
6:46
up to the particular string has arrived
6:49
it will return one if the entered string
6:52
is successfully copied
7:01
the next in line is get after
7:04
it copies the entered number of
7:05
characters after the particular string
7:07
has arrived in the incoming data
7:10
it will also return 1 after the
7:12
characters have been copied or else 0 in
7:23
the next one is get data from buffer
7:26
this is the same function from the
7:28
previous ring buffer code
7:30
it copies the data between the two
7:32
particular strings and save it into the
7:42
that's all the functions we have now
7:44
let's see the main function
7:52
here we will initialize the ring buffer
7:55
now let's test all the functions one by
7:58
we will start with the is confirmed
8:01
if these confirm function does not
8:04
return one within five seconds we will
8:06
call the error handler
8:12
i am giving some delay here so that i
8:15
can set the break point
8:21
we also need to include the header file
8:29
let's test this part first
8:31
i am going to use the hercules for the
8:38
i am sending random data along with ok
8:45
let's set the break points to see which
8:58
we reached here that means the function
9:00
did returned 1 as it detected that ok we
9:09
if i send this string which does not
9:11
have ok it will simply time out and
9:17
we can check the main buffer and here
9:19
you can see the data we sent is present
9:23
but since there was no ok within the
9:25
data it returned zero after the timeout
9:41
let's test another function now
9:57
the next in line is wait for
10:02
so here we will wait for let's say hello
10:06
and if it doesn't arrive within 6
10:07
seconds the timeout will occur
10:24
this time i am going to send this string
10:27
to make this test even better i have
10:30
added this hell right here and the hello
10:34
also this data size is more than what
10:36
the rx buffer can receive in a single go
10:39
so it's perfect for testing the circular
10:46
we hit the delay break point that means
10:48
it did detected the hello and returned
10:55
let's test it again but this time i am
10:58
not going to send the entire hello
11:11
and as expected after six seconds the
11:14
timeout occurred and it returned zero
11:19
you can see the data in the main buffer
11:33
the next one is copy up to
11:35
since this one copies the data into the
11:38
buffer here i need to define a buffer
11:51
we will copy up to the hello we will
11:54
copy it into the buffer and the timeout
11:56
will again be 6 seconds
12:09
i will send the same string and here it
12:13
it means that the string got copied
12:17
if we check the buffer we can see the
12:19
string up to hello got copied into the
12:22
let's test it again and this time i am
12:25
not sending the hello
12:45
this time the timeout occurred
12:48
if we check the buffer it basically
12:50
copied all the data we sent
12:59
i have mentioned this in the description
13:01
that it will copy irrespective of if the
13:03
desired string arrives or not
13:06
so we should use it only if we are sure
13:09
about the string that it will come in
13:18
the next one is get after
13:20
here i want to get four characters after
13:22
hello and i want to save them in the
13:32
the four characters after hello should
13:41
so we hit the break point and in the
13:43
buffer we have the four characters as
13:49
so this is it for the testing part
13:52
i hope you understood how to use these
13:56
the next half of this video will cover
13:58
the practical application where i will
14:02
esp8266 using these functions only
14:21
let me delete the dma setup from the
14:23
uart 2 as i won't be using this one
14:28
esp is connected with the uart 1 so i am
14:32
enabling the dma here
14:37
that's all for the configuration now
14:40
click save to generate the project
14:43
i have these files here they are same as
14:45
the web server video i did few months
14:49
i have modified them a little so as to
14:51
fit with the new ring buffer
14:53
let me copy these in my project
14:56
i will leave the link to these files in
14:58
the description just in case someone
15:05
i think i copied them the other way
15:15
all right let's take a quick look at the
15:19
here we have 2 arts defined just like
15:27
this function sends the data to the esp
15:31
and this one here is another uart used
15:35
in the esp init i am initializing the
15:40
then send the a t command and notice
15:43
here that i am using the is confirmed
15:46
this is because the esp sends ok after
15:49
receiving the at command
15:52
same goes for the cw mode command also
15:56
then here i am using the wait for
15:57
command after connecting to the network
16:01
just go through this you will understand
16:12
i got some errors here
16:14
this is because i haven't modified the
16:18
i am not using uart 2 anymore so i need
16:40
also i am increasing the sizes of the
17:09
all right it's fine now
17:11
let's include the esp header file
17:22
i will write the e s p functions here
17:26
initialize the e s p with the s s id and
17:33
for the connections you can just do a
17:38
the connections remains pretty much the
17:40
same just change the tx and rx pins as
17:50
here we can use a loop and if it fails
17:53
it will try again itself
17:58
inside the esp init i am not using the
18:01
reset command so i am going to put it
18:14
inside the while loop we will call
18:16
server start and it will handle the rest
18:23
all right let's test it now
18:32
this serial console will be used for the
18:50
like here it failed at the at command so
19:03
ok it finally passed the a t command
19:06
it fails a lot of time while connecting
19:08
with the ssid maybe because i am using a
19:11
hotspot from the phone
19:13
but when it finally connects it's pretty
19:21
let me reset this once
19:34
it finally connected
19:36
here we got the ip address now let's
19:45
this is the web page just like the last
19:49
the led is off right now and if i click
19:52
this button it will turn on
19:54
we can turn on the led and the button
19:58
so the web server code works with the
20:01
new ring buffer also
20:03
you can use this ring buffer to
20:05
interface other devices which are based
20:09
i will probably add more functions to
20:11
this library depending on the
20:15
so you guys keep checking the github
20:18
i will leave the link to those esp files
20:21
in the description too
20:23
leave comments in case of any doubt
20:26
keep watching and have a nice day ahead