FreeRTOS Tutorial 8 || Software Timers || STM32 || W/O CMSIS
11 views
Feb 14, 2021
Purchase the Products shown in this video from :: https://controllerstech.store ________________________________________________________________________________________ FreeRTOS PART7 :::: https://youtu.be/4NtB1HU3sbQ Common Setup :: 0:00 CMSIS Based :: 5:56 Pure RTOS Based :: 12:00 FreeRTOS STM32 Playlist :::: https://www.youtube.com/playlist?list=PLfIJKC1ud8gj1t2y36sabPT4YcKzmN_5D STM32 Playlist :::: https://www.youtube.com/playlist?list=PLfIJKC1ud8gga7xeUUJ-bRUbeChfTOOBd DOWNLOAD THIS PROJECT FROM :::: https://controllerstech.com/freertos-tutorials-8-software-timers/ ________________________________________________________________________________________ ******* SUPPORT US BY DONATING****** https://paypal.me/controllertech
View Video Transcript
0:12
[Music]
0:19
hello
0:19
everyone welcome to another video of
0:23
controllers tech
0:25
as the title says today i will cover the
0:27
free rtos again
0:29
and this time it's the timers in free
0:32
rtos
0:34
this video is divided into two parts
0:38
the first part will cover the usage of
0:40
timers using the c
0:41
m sys functions and in the second part i
0:44
will use the purefree rtos
0:46
functions you can check the timing for
0:49
the respective parts
0:51
in the description of the video the
0:54
first half of the video
0:56
where the setup will be done is common
0:58
for both the parts
1:00
let's start by creating the project in
1:03
cube id
1:04
first i am using stm32f446re controller
1:11
give the name to the project and click
1:13
finish
1:19
in the cube mx i am enabling the
1:22
external crystal for the clock
1:26
now before we go into timers i will
1:29
explain about them a little
1:31
so basically there are two types of
1:33
timers in free rtos
1:37
auto reload timer and the one-shot timer
1:41
once started a one-shot timer will
1:44
execute its callback function only once
1:47
it can be manually restarted but will
1:50
not automatically restart itself
1:53
conversely an auto reload timer will
1:56
automatically restart itself after each
1:58
execution of its callback function
2:00
resulting in periodic callback execution
2:07
now let's enable the free r-t-o-s
2:20
in the parameter setup enable the
2:23
timer's usage
2:33
timer priorities can vary between 0 to 6
2:36
keep it at 6 so that we don't have any
2:39
problems regarding something preempting
2:41
it
2:42
leave everything as it is for now if you
2:46
are watching this video for pure
2:47
rtos functions then you don't need to do
2:51
any other setup for the free rtos
2:54
the remaining setup for the free rtos is
2:57
only for those
2:58
who wants to use the cmsis functions
3:02
go to tasks here you can see the default
3:05
task is already created
3:08
let's give it some better name
3:12
i will call it uart task priority normal
3:16
is fine
3:18
task function will also be uart task
3:27
so we can't have same names for both
3:36
its ok now lets create another task
3:41
this will be led task i am keeping the
3:44
priority same as the uart task
3:47
so that they don't interfere within each
3:49
other's operations
3:51
task function will also be the led task
3:55
the tasks part is done here
4:00
let's create the timer now i am naming
4:04
it periodic timer
4:08
the callback function will be pt
4:12
callback
4:18
here you can select the type i
4:21
am keeping this one as periodic
4:27
let's create another timer now
4:37
i couldn't came up with any better name
4:40
for this one anyway this will be a one
4:43
shot timer
4:48
other parameters are kept as it is and
4:50
this completes our free rtos
4:54
setup
4:59
i have p-a-5 connected to the on-board
5:01
led
5:02
and that's why i am setting it as output
5:09
also the user button is connected to the
5:11
pin pc13
5:13
and that's why i am setting it as input
5:19
now enable the uart so that we can send
5:22
some data to the computer
5:28
also since we are using free r-t-o-s
5:31
we need to change time base from systick
5:34
to any other source
5:36
let's see the clock setup now
5:41
i have 8 megahertz clock and i want the
5:44
system to run at 180 megahertz
5:48
this is it for the setup click save to
5:50
generate the project
5:55
so this is our main file here
5:58
are the task handles and the timer
6:04
handles
6:06
then we have the task functions and the
6:09
timer callback defined here
6:18
inside the main function first of all
6:20
the timer is created
6:25
here is the name of the timer and here
6:28
is the type
6:33
the callback is declared in the time as
6:36
definition
6:44
next part is where the tasks are created
6:47
we have already discussed about them in
6:49
the previous videos
7:05
now here is the function that will be
7:07
executed when the uart task will be
7:09
called
7:10
as you must have noticed that we haven't
7:13
started the timers yet
7:14
we have only created them so here
7:18
when the control enters the uart task
7:20
first thing we are going to do is start
7:22
the periodic timer
7:25
i am writing this outside the infinite
7:27
loop so that it can only run once
7:30
os timer start takes 2 parameters
7:34
the first one is the handle to the timer
7:36
that you want to start
7:38
and the second is the time duration for
7:40
the timer
7:42
this timer will expire every one second
7:48
now we will send some data via the uart
7:59
and this task will keep running every 2
8:02
seconds
8:03
the periodic timer will timeout in 1
8:06
second
8:07
and the callback function will be called
8:19
in this callback function we will send
8:21
another string via the uart
8:23
indicating that the string is sent from
8:25
the callback function
8:42
before writing the led task let me
8:45
explain its purpose
8:47
so basically if the button is pressed
8:50
the on-board led should turn on for some
8:52
particular amount of time
8:55
this will be an implementation of
8:56
timeout feature for the led
9:03
we will first check if the button is
9:06
pressed
9:11
if it is then turn on the led by pulling
9:14
the respective pin to high
9:21
and now we will start the one shot timer
9:24
for the period of 4 seconds
9:28
in case you want to change the period of
9:30
the timer during the runtime
9:32
you have to call the timer start
9:34
function with the new time
9:36
period note that the timer start
9:38
function also acts as a timer reset
9:41
function
9:42
so if the timer start is called before
9:45
the timer is expired
9:47
it will reset the timer again in simpler
9:50
terms
9:51
if the button is pressed before the led
9:53
timeout it will extend the timeout by
9:55
another timer period
10:02
once the timer is expired the callback
10:05
function will be called
10:07
and here we will turn off the led
10:13
let's build this now
10:19
we have few warnings and they are
10:21
because of the data types there
10:24
i will leave them for now let's debug it
10:45
i am using hercules to monitor the uart
10:48
data
10:56
let's run it now
11:03
you can see the timer string is printing
11:05
every 1 second
11:06
and on the other hand you art string is
11:08
printing every 2 seconds let's press the
11:14
button the controller
11:16
the led turned on
11:21
and it turned off after 4 seconds just
11:24
as we programmed
11:26
but if we press the button before the
11:28
timeout the led will never turns off
11:32
you can see the uart task and the
11:33
periodic tasks are performing just fine
11:36
and they are not affected by other tasks
11:38
and timers
11:43
this is it for this video i hope you
11:47
understood the usage of timers in free
11:49
rtos
11:50
using the cm sys functions
11:53
you can download the code from the link
11:56
in the description
12:01
next half of this video will cover the
12:03
same but by using the pure
12:05
rtos functions
12:12
i am assuming you have already gone
12:14
through the setup in the first part of
12:16
this video
12:18
first of all we need to copy all the
12:19
header files from cmsis file
12:22
and paste them in our main file
12:32
now delete all the pre-generated cm sys
12:36
functions
12:56
now let's start by creating timer
12:58
handles
13:00
pt handle is for periodic timer and ot
13:03
handle is for one
13:04
shot timer
13:08
now we will create task handles
13:15
you can see the handles for the uart
13:17
task and the led
13:19
task
13:26
now we will write the task functions
13:32
let's start with you art task
13:37
when the control enters the uart task
13:40
first thing we are going to do is
13:42
start the periodic timer i am writing
13:46
this outside the infinite loop
13:48
so that it can only run once since the
13:50
timer is periodic anyway
13:53
x timer start takes 2 parameters
13:57
first is the handle of the timer
14:00
and second is the time to wait before
14:02
starting the timer
14:04
since we want the timer to start
14:06
immediately i am writing a zero here
14:10
now we will send some string via the
14:14
uart
14:19
and this task will keep running every 2
14:22
seconds
14:23
p e m s to tix basically converts the
14:26
milliseconds to ticks
14:28
as per the free r-t rtos setup
14:31
this completes the uart task function
14:34
let's write the led task function now
14:38
before writing the led task let me
14:41
explain its purpose
14:43
so basically if the button is pressed
14:46
the on-board led should turn on for some
14:48
particular amount of time
14:51
this will be an implementation of
14:53
timeout feature for the led
14:58
we will first check if the button is
15:00
pressed
15:06
if it is then turn on the led by pulling
15:09
the respective pin to high
15:17
and now we will start the one shot timer
15:24
immediately
15:26
the period for the timers is set during
15:29
their creation
15:30
the led task will run every 20
15:38
milliseconds
15:40
this completes the code for the task
15:42
related functions
15:44
once the timer is expired the timer
15:47
callback functions will be called
15:50
and that's why we need to write a
15:52
callback function now
15:54
we can write a common callback for all
15:57
the timers
15:58
and that's what i am going to do let's
16:01
call it timer callback
16:02
and its parameter will be the timer
16:06
handle
16:10
if the callback was called by the
16:12
periodic timer
16:14
we will send this string to the uart
16:28
and if it is called by the one shot
16:30
timer then the led
16:32
will turn off note that the timer start
16:36
function also acts as a timer reset
16:38
function
16:40
so if the timer start is called before
16:42
the timer is expired
16:44
it will reset the timer again in simpler
16:48
terms
16:48
if the button is pressed before the led
16:50
timeout it will extend the timeout by
16:53
another timer period
16:55
now let's write our main function we are
16:58
going to start with creating the timers
17:02
x timer create takes the following
17:04
parameters
17:05
the name of the timer here you can give
17:08
some random name
17:09
as it doesn't matter second is the timer
17:12
period
17:13
and i am keeping it one second for the
17:15
periodic timer
17:17
third parameter is to set if the timer
17:20
is periodic
17:21
or the one shot timer true means
17:24
periodic
17:25
and false means one shot fourth
17:28
parameter is the timer id
17:30
and you can assign any id to this timer
17:38
fifth parameter is the callback function
17:41
for the timer
17:45
similarly we will create another timer
17:47
with minor changes in the parameters of
17:49
course
17:51
this will expire in 4 seconds
17:54
it's not a periodic timer so p d false
17:58
and give a different timer id we also
18:02
need to equate them to their respective
18:03
handles
18:04
since the x timer create function
18:06
returns the timer handle on success
18:10
this is all for timers now let's create
18:14
the tasks x task create
18:17
takes the following parameters first
18:20
is the task function
18:25
name of the task again the name does not
18:27
matter
18:28
so use whatever you want next is the
18:32
stack size
18:33
and i am keeping it 128 since it was set
18:36
in the cube mx
18:37
also next is the parameters
18:41
which we will keep as null then the
18:44
priority
18:44
which i will use as 1 for the normal
18:47
priority
18:49
and the last is the handle for the task
18:52
similarly create the led task also
19:08
this completes the task creation
19:12
now in the end start the scheduler
19:17
let's build it now
19:20
we have two warnings here and we can
19:23
remove them by changing the data types
19:38
ok we are all set now let's run this
19:41
program
19:48
i am using hercules to monitor the uart
19:51
data
19:52
you can see the timer string is printing
19:54
every 1 second
19:56
and on the other hand you art string is
19:57
printing every 2 seconds
20:00
let's press the button on the controller
20:03
the led
20:04
turned on and it turned off after 4
20:07
seconds
20:08
just as we programmed
20:17
but if we press the button before the
20:18
timeout the led will never turn off
20:22
you can see the uart task and the
20:24
periodic tasks are performing just fine
20:27
and they are unaffected by other tasks
20:29
and timers
20:31
this led similar to the backlit for the
20:33
display
20:34
or any other device which can be
20:36
controlled by the timeout feature
20:44
we have other functions available also
20:54
x timer change period can change the
20:56
period of the timer at any point
21:04
this is to delete the timer reset is to
21:08
reset the timer
21:09
but x timer start can also be used as
21:11
the reset
21:12
if the timer is running
21:23
just like we are doing here whenever the
21:25
button is pressed
21:26
and the timer is still running this will
21:29
reset the timer
21:30
and it will start from the beginning you
21:33
can check out these functions
21:36
this is it for the video i hope you
21:39
understood it
21:41
you can download the code from the link
21:43
in the description
21:45
keep watching and have a nice day ahead
#Programming