0:10
everyone welcome to controllers tech
0:14
this video is the start of a new series
0:16
and in this series i will cover the
0:18
stm32 register level programming
0:22
this is the first video and today we
0:24
will see how to set up the clock
0:27
and that's why this video is going to be
0:31
there are no shortcuts here some
0:33
registers and values might be different
0:36
and that's why you need to see the
0:40
i will use kyle for these tutorials
0:44
let's open the pack installer first
0:47
type your mcu name and let it refresh
0:58
now you need to install the dfp pack
1:02
that will be all you need
1:07
also make sure the compiler is updated
1:11
i already have them installed and
1:24
go to project and create a new project
1:33
give the name to this project and click
1:47
search for your device here
1:58
we only need to include the core file
2:08
here is our project let's add the main
2:19
first of all include the device specific
2:27
this file have all the registers irq
2:36
i am going to create a main function and
2:38
a while loop inside it
2:44
let's create a function where we will
2:50
and here are the steps that i am going
2:54
first we need to enable the external
2:57
and wait for it to become ready then
3:01
and configure the voltage regulator
3:04
next we need to configure the flash
3:09
then configure the prescalers for
3:13
then configure the pll then enable the
3:18
and finally select the pll as the clock
3:25
we will cover all these steps one by one
3:29
let's also open the cube mx for the
3:59
i am selecting external crystal for the
4:03
i will configure for the maximum clock
4:05
here because i always prefer it
4:07
and this way i can keep the same
4:09
settings for the other programs too
4:23
we will come back to this once we start
4:33
let's start with the first step
4:36
so we need to turn the hse on
4:40
and wait for it to become ready
4:44
this bit is in the rcc cr register
4:53
as you can see here the 16th bit is for
4:57
and the 17th bit is the hse ready bit
5:07
in the rcc cr register set the hse
5:18
this is already defined in the header
5:20
file and it's basically writing a 1
5:23
at 16th position now we will wait for
5:28
to set in the cr register
5:35
this will indicate that the hse crystal
5:39
let's move to the second step enable the
5:43
power interface clock
5:44
and configure the voltage regulator
5:55
power enable is 28th bit of the rcc apb1
6:12
we will set this bit in order to enable
6:18
it now the setting for the voltage
6:31
this bit is located in power control
6:34
and it's the bit number 14 and 15.
6:43
just keep this to its reset value in
6:45
this case it will be 1
6:57
the vos will always correspond to the
7:06
now the step 3 we need to configure the
7:12
to do so we need to modify the flash
7:20
let's see the cube mx here we have all
7:25
prefetch buffer data cache all are
7:29
and the flash latency is 5ws
7:40
all these bits are available in access
7:51
let's set the flash acr register
8:12
now the fourth step is to configure the
8:14
prescalers for the main clock
8:16
and the peripheral clocks
8:26
as you can see here apb1 prescaler is 4
8:30
apb2 prescaler is 2 and ahb prescaler is
8:43
all these configuration bits are in our
8:45
cc clock configuration register
8:59
let's first set the ahb prescaler
9:03
ahb prescaler is one in our setup
9:06
so i am using division 1.
9:14
next set the abp1 prescaler which have
9:29
and at last set the apd2 prescaler
9:32
whose value is 2 and that's why division
9:36
let's move to the fifth step and
9:46
we will do that by modifying the rccpll
9:50
configuration register here we need to
9:53
set the values of pllm
10:06
you can see the values here pllm is for
10:29
let's define these values first
10:49
in the pllm we can directly input the
10:54
similarly in plln we can input the value
11:05
but in pllp we can't directly use the
11:09
as we need to select from these
11:12
i have the pllp as 2 so i need to write
11:16
0 to this 16th position
11:24
so i will simply shift this 0 to the
11:35
let's modify the pll configuration
11:40
p l l m starts at bit zero
11:44
p l l n starts at bit six
11:54
p pllp starts at bit 16.
11:58
here we also need to select the source
12:02
which in this case is hse oscillator and
12:05
i need to write a 1 a 22nd bit
12:20
in step 6 we need to enable the pll
12:32
in our cc control register bit 24
12:37
and bit 25 confirms the ready status
12:49
let's enable the pll first and now we
12:52
will wait for the pll to become ready
13:03
the final step is to set the clock
13:10
this can be done in the rcc
13:12
configuration register
13:15
the zeroth bit is to set the clock
13:17
source and the second bit is to confirm
13:21
here i will use the pllp as the clock
13:26
set the pll as the clock source
13:30
and wait for the source to become ready
13:42
this completes the clock setup now from
13:47
in most of these videos i will use these
13:52
so this was basically just a one-time
13:58
now as we want to blink the led we need
14:01
to configure the gpio
14:05
here the things are easy just enable the
14:10
set the pin as output and configure any
14:12
settings you need for the output mode
14:16
let's start with the first one to enable
14:21
we need to look into a cc peripheral
14:24
clock enable register
14:26
as you can see here the zeroth bit of
14:30
enables the gpio clock
14:37
by the way i am using pin pa5 for the
14:43
write a one to the zeroth position to
14:45
enable the clock for gpioa
14:49
in the second step we'll set the pin as
14:58
this can be done using gp io mode
15:06
here we need to write a 0 and 1 to set
15:12
every two bits of this register controls
15:16
for example bits 0 and 1 controls the
15:21
bits 2 and 3 controls the pin 1 bits 4
15:24
controls the pin 2 and similarly bits 30
15:29
controls the pin 15. as i need to set
15:33
5 as the output i need to modify bits 10
15:36
and 11 so 11th bit will be 0 and 10th
15:49
in the gpioa mode register
15:52
i will write a 1 in the 10th position
15:55
and 11th position is 0 by default
16:02
now the last step is to configure the
16:06
like what type of output we want
16:09
default setup is for push-pull and this
16:11
is exactly what we need
16:19
then we have the type of speed we want
16:23
by default its low speed i am okay with
16:27
since i am only blinking led for now
16:30
the there is pull up or pull down
16:34
we will leave this to none since we are
16:37
for blinking so all the remaining
16:40
registers should be zero
16:42
even if you don't write values to them
16:44
they will be zero by default
16:47
i will write them anyway
16:51
this finishes up the gpio configuration
16:56
let's write the main function now
16:59
first of all set the system clock and
17:05
and now on the while loop i need to
17:10
to set or reset a pin we need to use the
17:13
gpio bit set reset register
17:17
as you can see here to set a pin we need
17:20
to set it in the lower half of the
17:24
and in order to reset the pin we need to
17:32
as i am setting pin p-a-5 i will write
17:35
the fifth bit of this register to 1.
17:38
let's give some delay which i will cover
17:42
now to reset the pin 5 i need to write a
17:45
1 to the upper half of this register
17:48
as you see here the upper half starts at
17:52
so the pin 5 will be the 21st bit and to
17:56
i need to write a 1 in 21st position
18:00
instead of doing that i will simply
18:02
write a 1 to the 5th position
18:04
and shift the entire value by 16 places
18:08
and again give the delay for toggling
18:11
let's create that delay function
18:15
this is just some random wait time
18:23
we don't have any errors here go to
18:27
and update the frequency here mind will
18:30
be running at 180 megahertz
18:34
select the st link debugger
18:39
select connect under reset
18:49
click download to flash the code
18:56
you can see the led is blinking but it's
18:59
too fast to be noticed
19:04
let's increase the delay
19:18
now the blinking is perfect
19:26
let's modify it one last time
19:42
and you can see now it's even more
19:49
so that's all for this video i will
19:53
continue this series
19:54
and we will cover all sort of
19:58
also a cc was just one time setup
20:01
and next video onward i will simply
20:04
include it in the main file directly
20:06
i hope you learned something today
20:09
things can be a bit hard in the
20:11
beginning especially if you have never
20:13
worked with registers before
20:16
but you have to start somewhere in the
20:20
i will cover the basic feature of the
20:22
timer and we will use it to generate the
20:26
goodbye till then keep watching and have