0:04
hello everyone I have already made few
0:08
videos on data transfer using new art in
0:11
stm32 the latest one of them was
0:15
circular buffer using DMA interrupt and
0:18
idle line detection circular buffer was
0:21
quite good but it is very complex and
0:24
sometimes we might miss the data today
0:27
in this video I will use yet another but
0:31
more effective method of data transfer
0:33
using new art I have implemented the
0:36
arduino like head and tail methods to
0:39
receive and transmit data I am using
0:42
stm32 cube IDE and I will recommend that
0:46
you use it too so let's start by
0:49
creating our project in the IDE
1:13
I am keeping the board rate at eleven
1:16
five two zero zero and make sure that
1:19
you enable the interrupt I am keeping
1:22
the clock at max frequency that's it for
1:25
the setup now let's build our code
1:42
before starting we need to copy some
1:45
library files into the code
2:03
include the header file and let's check
2:06
if there is any error let me just change
2:15
it for f1 series and done let's take a
2:24
look at some functions now you can
2:27
change the size of the buffer here if
2:29
you are going to receive large data at
2:31
once make it big for small continuous
2:34
data transfer keep it low
2:37
make sure you define your UART handle
2:40
type here and we need to put this one in
2:43
the interrupt file this is the function
2:48
definition for the ISR that we are going
2:51
to use instead of the default one and
2:53
next we need to route the ISR to the one
2:57
written by us and comment out the
3:02
default one note here that every time
3:06
you regenerate the code you have to
3:21
okay guys let's talk about the functions
3:24
now UART Reid is going to read the data
3:27
that is in the rx buffer it reads one
3:31
character at a time after successfully
3:33
reading it will increment the tail by
3:36
one and returns the character in the
3:38
decimal format the head counts in the RX
3:42
buffer will increment each time there is
3:45
a new character received in the buffer
3:47
this process is interrupt driven to make
3:50
sure that the tail is always catching up
3:53
with the head we have to read data as
3:55
soon as it arrives or else there will be
3:58
no more space for the new data and any
4:02
incoming data will be lost if you just
4:05
Google the ring buffer you can check out
4:07
the Wikipedia page as you can see here
4:17
pet increments when we type in some data
4:20
the tail increments when we read the
4:22
data if the first two bytes are already
4:32
read the head can overwrite the data but
4:35
think of a situation that we haven't
4:38
read these first two bytes then the
4:40
process will stop there and we will lose
4:43
the last two bytes of the data that's
4:46
why I said in the beginning that if you
4:48
are expecting large data at once
4:50
make sure the buffer size is big or else
4:54
the head will reach the end and any
4:56
further data will be lost
4:58
UART write function will write a
5:00
character to the TX buffer and increment
5:03
the head in the TX buffer sending data
5:06
to the UART is again an interrupt driven
5:08
process and the tail will be incremented
5:11
after the day two is sent UART send
5:14
string will spend the entire string
5:17
instead of single character UART print
5:20
base prints the number in different
5:22
formats such as hex decimal etc
5:27
his data available checks whether there
5:30
is any data available to be read in the
5:32
RX buffer it returns one on success wait
5:36
for checks for the provided string
5:38
within the incoming data it returns the
5:41
position of the string within the buffer
5:56
initialize the ring buffer first
6:04
inside the while loop first we should
6:07
check the data and if it is available we
6:10
will read it and then write it to the
6:12
UART again let's build the code and run
7:15
as you can see that the board rate is
7:20
set to eleven five to double zero
7:22
sending single character and received it
7:25
to pink color is what we are spending
7:28
and black color is the one that we
7:31
received sending two characters let's
7:34
send the string hello send even bigger
7:37
string make sure this string size is
7:40
less than the size of the rx buffer yup
7:48
received it too so the UART is working
7:51
irrespective of the length of the
8:13
I will come back to this in a while
8:18
let's check the weight for function now
8:39
here I want to know the position of that
8:42
hollow string in the buffer and if the
8:45
position is valid it will send it to the
9:38
if I send some random string nothing get
9:50
now I will send hello and see that some
9:54
number is printed on the terminal if you
9:57
take a look at the rx buffer you will
9:59
notice that this is the position where
10:02
the string hello ended
10:19
let's try with some other string this
10:22
time I will send hell along with other
10:25
characters nothing on the terminal as
10:35
now same string but complete Hollow
10:38
and you see the position is printed
10:41
let's take a look at the buffer again
10:44
and yes it is the position where hello
10:51
ended next I want to demonstrate that
10:54
this is not a blocking mode function so
10:57
for that I have to use LED blinking
12:14
so I will receive and print data and
12:17
also the LED will blink every 100
13:00
let's send some data now as you can see
13:04
that LED is blinking as it should also
13:08
the date who is being written every 100
13:42
let's change this code a little and try
13:45
to print all the day two at once
13:52
now the data is being printed all at
13:56
once this is it guys this was the
13:59
working of a ring buffer if you can
14:02
contribute to some changes I am open to
14:05
suggestions make sure you read the data
14:08
before the next batch of data arrives or
14:10
else the data will be lost you can
14:14
download the code from the link in the
14:16
description have a nice day