0:07
hello everyone welcome to controllers
0:11
Tech in my previous video about free RT
0:15
OS I covered how to send or receive data
0:18
using q AK you can only hold the data of
0:22
same type if we want to transfer the
0:26
data of different types we have to use
0:28
structure q and in this video we are
0:31
going to discuss about it we will start
0:34
by creating the project in cube ide
0:42
select the controller you are using give
0:50
some name to the project and click
0:59
in the cube MX first of all I am
1:03
selecting external crystal for the clock
1:14
go to cysts and select the time-base
1:18
anything other than cystic I am
1:21
selecting timer six you are to is to
1:24
communicate with the computer
1:40
select the free Artie OS version one and
1:43
leave everything as it is
1:55
this default task will be created and we
1:59
will deal with it later now go to the
2:02
clock setup the crystal is 8 megahertz
2:04
and I want the controller to run at 180
2:14
click Save to generate the project
2:24
I am NOT going to use the CMC's API so
2:31
go to see MCH file and copy all these we
2:36
will include them directly in our
2:38
project comment out the CM sis file we
2:46
will remove the default task related
2:47
functions which were generated by
3:02
let's start with the coding now first it
3:11
find the Q handler next we need to
3:20
define the task handlers I will be using
3:24
to sender tasks and one receiver task
3:34
like I said in the beginning that we
3:36
will be sending different data types to
3:39
the same queue to do that we need to
3:42
create a structure of all those data
3:44
types I am creating this structure which
3:48
have a pointer to the character an
3:49
integer and an unsigned 16-bit integer
3:53
let's call it my struct these will be
4:01
required to inside the main function we
4:09
first need to create a queue that can
4:12
hold the structure type this queue can
4:15
store two elements which are of the type
4:35
these string will be printed based on if
4:38
the queue was created or not
4:46
after creating queue we will create the
4:58
looks like I forgot to define the task
5:01
functions let's define them now
5:13
these functions are where the
5:15
task-related code will be written
5:27
X tasks create takes the following
5:30
parameters task function some name of
5:34
the task stack depth the parameter to
5:37
the task the priority of this task and
5:40
the handler of this task note that I am
5:47
using same priority of two for both the
5:50
sender tasks receive a task have lower
5:54
priority after creating the tasks we
5:58
will start the scheduler now let's write
6:06
the codes for these tasks first the
6:10
sender one task I am declaring a pointer
6:14
to my struct this is to convert the
6:19
delay to ticks when the control enters
6:23
the task this string will be printed on
6:25
the console now before assigning the
6:29
data we must allocate the memory for
6:31
this pointer to the structure
6:38
to do so we will use PV port malloc
6:42
function it's defined in the portable
6:48
dot H file it's takes size as the
6:52
parameter there is also V port free to
6:55
free the allocated memory I am guessing
6:59
you are already familiar with the malloc
7:01
function PV port malloc is also the same
7:05
but you can consider it as safer when
7:07
using the free RTOS the size of memory
7:11
we need is same as the size of my struct
7:22
now we will load the data to the
7:43
and then send this data to the queue
7:54
XQ cent takes the following parameters
7:58
the handler of the queue the address of
8:01
the data and the amount of time to wait
8:05
if the data is successfully sent to
8:08
queue it will return PD pass
8:18
and this string will be printed to the
8:21
console let's make it a bit more
8:24
interesting index 1 will be incremented
8:27
by 1 each time the control enters this
8:30
task these values will be modified based
8:33
on this index 1 value and at last this
8:42
task will go into suspension for 2
8:46
similarly send a task 2 will also send
8:49
the data to the queue every 2 seconds
9:00
now let's write the receiver task
9:09
our PTR to struct is another pointer to
9:12
my struct this is where the received
9:15
data will be stored converting three
9:19
seconds to tix this string will be
9:27
printed when the control enters the
9:29
receiver tasks now we will receive the
9:37
data from the queue xq receive takes the
9:41
following parameters the handler of the
9:44
queue address of the variable where the
9:47
data should be stored and the wake time
9:49
in case the queue is empty it will
9:52
return pd pass if the data is received
9:55
from the queue here we will allocate new
9:58
memory location to store the string I am
10:02
expecting the size of string could be
10:04
100 bytes whenever you are using s print
10:08
F in the free rtos make sure you
10:11
allocate the memory like this otherwise
10:14
s printf will cause hard fall to error
10:17
this string will be stored in the PTR
10:51
and we will send this string to the UART
11:12
after sending free the location of the
11:15
PTR that we just created we must also
11:19
free the memory that we allocated in
11:21
both the sender tasks so we will use V
11:25
port free and the argument will be the
11:27
our PTR to struct this way the memory
11:30
will be free for the pointer which was
11:33
received by the queue and the other
11:35
pointer will be safe I will demonstrate
11:38
this in a while we also need to include
11:46
the SD d i/o for the s printf and string
11:49
dot H for the string related functions
11:52
let's build this code now there are no
11:56
errors so we will debug the code I am
12:01
using Hercules for the serial console
12:10
let's run this now I am going to let it
12:35
so the queue was created successfully it
12:39
entered the sender to task sent the data
12:42
to the queue and task dot suspended
12:45
somehow the data is not printing
12:48
properly here so we will just look at
12:51
the received data data from sender one
12:56
is printed data from sender two again
13:04
from sender one with new counter value
13:07
of two and large value increased same
13:11
result from the sender to task let's run
13:18
it freely as you can see the values are
13:22
printing from both the tasks also the
13:24
counter is increasing and so is the
13:26
large value send a one sender two now
13:32
let's see how the memory allocation
13:52
I will put a breakpoint here and here
14:12
note that I am watching this in the
14:14
variables tab and not in the live
14:17
expression we hit the first breakpoint
14:21
keep an eye at the PT artis struct it's
14:25
showing zero right now when I stepped
14:34
over this function memory gets allocated
14:36
for the PT artis struct the location for
14:40
this memory is heat plus 1964 we will
14:45
just consider it as 1964 let's run the
14:53
code again we hit another breakpoint in
14:57
send a to task step it over the memory
15:01
PT artis struct is now 1988
15:20
now we hit the breakpoint in the
15:23
receiver task note the PTR variable
15:31
the memory allocated for it is 2012 also
15:36
our PTR to struct have the data from
15:38
memory 1964 which is associated with
15:41
sender 1 PTR to struct right now
16:06
let's add another breakpoint here we
16:10
have already freed the memory of PTR so
16:13
2012 location is free now if we step
16:16
over this sender one PTR to struct is
16:19
also freed now if we run the code now we
16:23
hit the sender 1 breakpoint again as the
16:27
memory 1964 was free it will be
16:29
allocated to sender one again let's run
16:38
it we hid the sender to breakpoint the
16:43
memory 1988 is already occupied and next
16:47
free slot is 2012 here 2012 will be
16:51
allocated for the sender to PTR 2 struct
16:54
guys this is just the memory allocation
16:57
as the queue already have two waiting
17:00
elements so send a to can't send the
17:02
data to the queue right now we are in
17:05
the receiver task right now note that
17:09
our PTR 2 struct have the data from the
17:11
memory location of 1988 which was
17:14
allocated to sender to task before PTR
17:21
was allocated the memory of 2036 and was
17:25
freed in the previous step after running
17:28
this statement memory location of 1988
17:31
which was associated with the sender to
17:33
task is also freed again we are in the
17:38
sender 1 task the lowest slot available
17:41
right now is 1988 and this will be
17:44
allocated to the sender 1 PTR 2 struct
17:47
this is how the memory allocation works
17:50
as I mentioned we free the memory after
17:54
receiving the data and this way only the
17:57
memory allocated to the pointer that we
17:59
received gets free and other data
18:01
remains safe this is it guys I hope you
18:06
understood the concept of structured
18:08
queue and how to use the memory
18:10
allocation in free RT OS
18:13
you can download the code from the link
18:16
in the description leave comments if you
18:19
have doubts be safe and don't step out
18:23
of your home have a nice day