STM32F10X series clock configuration

1. What is a clock? Why do you need a clock?

        If someone suddenly asked you this question one day, how would you answer it? Someone made a definition of a clock: A clock is a device that counts periodic events from a common starting point . In my understanding, the clock is like the pulse of living things, maintaining the most basic survival of all things. Comparing the single chip microcomputer with human, the diastole and contraction of the human heart correspond to the logic 0 and 1 of the TTL level signal. It is vaguely felt that the clock is crucial to the Single Chip Microcomputer.

        In 1971, the microprocessor (MPU) represented by Intel 4004 was born, and the 4004 used a crystal oscillator as a system clock generator. Then the microcomputer developed vigorously in the two directions of operation and control, and the later microcontroller (MCU, the full name of Microcontroller Unit, which is similar to the single-chip microcomputer), can be said to be the product of the digital circuit era of the single-chip microcomputer. The core part of the microcontroller is the CPU. Figure 1-1 shows the internal structure of the CPU consisting of registers, controllers, arithmetic units and clocks. It can be seen from this that the CPU is inseparable from the clock signal, and naturally the single-chip microcomputer is also inseparable from the clock signal.

        But so far, it seems that it is still only known but not sure why. If you want to know the answer to this question, you can only start from its real internal circuit. Figure 1-2 shows the internal structure of the 4004 core microprocessor, which consists of 2250 transistors.

Figure 1-2 4004 internal structure

                The picture is a bit huge, let's zoom in on a random place to take a look. The enlarged picture is shown in Figure 1-3. It can be seen that its internal structure is composed of various transistors, without any operational amplifier circuit, a proper digital circuit structure.

Figure 1-3 4004 partial amplifier circuit

         We who have studied digital electronic technology know that various gate circuits such as AND, OR, NOT, AND NOT, OR NOT, AND OR NOT, EXCLUSIVE OR are the basic units of digital circuits. Although their structures are diverse and complex, they can usually be divided into There are two types of combinational logic circuits and sequential logic circuits. At the same time, we also know that a single-chip microcomputer is composed of an arithmetic unit, a controller, a memory, and an input and output device, and its internal specific circuit is simplified by a logic function, which is not easy to analyze directly. The curve saves the country, then let's take a look at the GPIO internal structure of the PIC16F877 microcontroller designed by MicroChip, as shown in Figure 1-4.

Figure 1-4 GPIO internal structure of PIC16F877 microcontroller

         The GPIO of the PIC16F877 microcontroller includes two edge-triggered synchronous D flip-flops (edge ​​D flip-flops), the output latch D1 and the direction latch D2. The output of the flip-flop is not only related to the D port, but also controlled by the clock edge CK. , it can be seen from this that the microcontroller needs a clock edge to control the most basic GPIO. So how is the most basic timing of the microcontroller realized? Let's take a look at the basic structure of its peripheral Timer1, as shown in Figure 1-5.

Figure 1-5 Basic structure of Timer1

         When Timer1 of PIC16F877 microcontroller works in timing mode, TMRON is set to 1 to enable Timer1, TMR1CS (T1CON<1>) bit is set to 0, and the data selector ( MUX , full name Multiplexer) selects the crystal oscillator clock signal divided by 4 (Focs/4) is passed to the prescaler Prescaler for further frequency division. The timing clock source (Fosc/4) is derived from the frequency division of the system clock system clock Fsys (Fsys=Fosc), and the system clock also rises uniformly when each rising edge clock arrives. T1SYNC is 0 or 1. The same effect ( original text ) : The synchronize control bit T1SYNC (T1CON<2>) has no effect, since the internal clock is always in sync. ). Finally, the register TMR1 is advanced under the action of the timing clock pulse to complete the timing function. Other peripherals such as UART and CCP require precise clock signals to work stably. The importance of clocks to microcontrollers is self-evident.

2. How is the clock generated?

        The clock is actually a circuit that continuously oscillates to generate periodic high and low levels, but the single-chip microcomputer has high requirements for the stability of the frequency of this oscillator circuit. Starting from the principle, there are many types of oscillation circuits, the common ones are RC sine wave oscillation circuit, LC sine wave oscillation circuit, quartz crystal oscillation circuit, ceramic oscillation circuit and so on.

        The STM32F103C8T6 microcontroller integrates an 8MHz RC oscillator circuit (HSI) and a 40KHz RC oscillator circuit (LSI), but the clock frequency of the RC oscillator circuit, the accuracy of the resistors and capacitors, the manufacturing process, the ambient temperature and humidity, and the power supply voltage all have The relationship is usually only used at low frequencies . The STM32 official also bluntly stated in the technical manual that the actual frequency of the actual LSI is between 30-60KHz . The RC oscillator circuit is composed of four parts: the amplifier circuit, the frequency selection network, the positive feedback network, and the amplitude stabilization link .

What is an RC Phase Shift Oscillator? What is a Wien Bridge Oscillator? https://zhuanlan.zhihu.com/p/213245623Since         the RC oscillator circuit is generally integrated on-chip, the quartz crystal oscillator circuit may be the most commonly seen oscillator circuit. Anyone who has soldered the STC89C52RC microcontroller should have the experience of soldering a crystal oscillator and a 22pF capacitor on the XTAL1 and XTAL2 pins of the microcontroller. The soldered crystal oscillator may have appeared in Figure 2-1. In fact, this is the outermost part of the quartz crystal oscillator circuit. part.

Image result of crystal oscillator
Figure 2-1 Quartz crystal

        Quartz crystal oscillators are characterized by high precision and high stability, and their oscillation frequency is related to the slicing direction of the crystal. The conventional circuit connection is shown in Figure 2-2. In general, the amplitude of the mechanical vibration of the wafer and the amplitude of the alternating electric field are very small, but when the frequency of the applied alternating voltage is a certain value, the amplitude increases significantly, which is much larger than the amplitude at other frequencies. This phenomenon is called piezoelectric resonance. Both ends of the crystal oscillator are generally grounded through two load capacitors, and the capacitance of the load capacitor is related to the frequency of the crystal oscillator. This value is generally given by the manufacturer, and MicroChip gives suggestions in the datasheet of PIC16F877, where LP refers to low-power quartz crystal, XT refers to crystal/resonator, HS refers to high-speed crystal/resonator, and the detailed data is shown in Figure 2-2. Show.

Figure 2-2 Quartz crystal load capacitance matching value

        In addition, a 1MΩ feedback resistor is often connected in parallel with the pins at both ends of the crystal oscillator, and a 1KΩ current limiting resistor is connected in series to make the oscillation circuit work more stably. The wiring is shown in Figure 2-3.

Figure 2-3 Quartz oscillator circuit

Why should a capacitor be added next to the crystal oscillator of the microcontroller? https://zhuanlan.zhihu.com/p/216722171?utm_source=wechat_session&utm_medium=social&utm_oi=824614012732993536&utm_campaign=shareopn

3. STM32 clock structure framework

        The microcontrollers of the STM32F10X series can be divided into an internal integrated RC oscillator circuit and an external clock circuit according to whether the on- chip clock generator is integrated . The external clock can be generated by the quartz/ceramic oscillator connected to the OSC32_OUT and OSC32_IN , OSC_OUT and OSC_IN pins of the microcontroller, or input from the OSC32_IN and OSC_IN by an external clock generator.

        According to the clock speed , it can be divided into high-speed clock circuit and low-speed clock circuit . The high-speed clock circuit includes a high-speed internal clock (HSI) and a high-speed external clock (HSE). HSE is usually generated by an 8MHz crystal oscillator connected to both ends of the OSC_OUT and OSC_IN pins, and LSE is usually generated by a 32.768KHz crystal oscillator connected to both ends of the OSC32_OUT and OSC32_IN pins. Please refer to Figure 3-1 for the clock pin location.

Explanation of proper nouns:

HSE (High Speed ​​External Clock signal): high-speed external clock signal, 4-16MHz external crystal/ceramic resonator or active clock

HSI (High Speed ​​Internal Clock signal): high-speed internal clock signal, 8MHz RC oscillator

LSE (Low Speed ​​External Clock signal): low-speed external clock signal, 32.768KHz external crystal oscillator

LSI (Low Speed ​​Internal Clock signal): low-speed internal clock signal, 40KHz internal RC oscillator

PLL (Phase-Locked Loop): Phase-locked loop, it can be known from the clock tree that it can multiply HSI and HSE

Figure 3-1 STM32F103C8T6 pinout

        With the foundation of the above knowledge, let's look at the internal clock circuit of the STM32F10X series microcontroller (commonly known as the clock tree , it looks too complicated for beginners, but this is also the magic weapon for STM32 to achieve low power consumption), as shown in Figure 3-2 shown.

Figure 3-2 STM32F10X clock tree

        When I look at this picture for the first time, I will be very irritable, I don't know where to start, and I was like that at the time. However, when we focus on the clock sources HSI, HSE, LSI, and LSE , it will be much simpler to move from left to right. Anyway, the clock starts from here.

        Let's look at the low-speed LSI and LSE first, and then provide the clock to the RTC (Real Time Clock, real-time clock). In addition to the clock for the RTC, the LSI also provides the clock for the independent watchdog IWDT.

        Then we pay attention to the system clock SYSCLK, we see how HSI and HSE get to him. It is very important to remember that the system clock SYSCLK is selected from HSI, HSE and PLLCLK by the data selector SW through observation . HSI and HSE will not be discussed. If you choose him as the clock source, what frequency is he, and what frequency is the system clock, and the focus is on PLLCLK. From the perspective of HSI , it can be divided into 2 by HSI (8MHz) (change to 4MHz) into the data selector PLLSCR, and then multiplied by the phase-locked loop PLLMUL (x4, x5, x9, see for yourself) into PLLCLK. It is worth noting that the highest frequency after frequency multiplication by the HSI is 64MHz (8/2*16). From the perspective of HSE , it can be prescaled by the HSE into the data selector PLLDIV1 (select one of /1,2,3,4....,16 to divide), and then go through the phase-locked loop PLLMUL times frequency, up to the upper limit of the system clock of 72MHz. There is a fun place, many sources like to say that the STM32F103C8T6 supports a 72MHz system clock, and then some people foolishly thought that a 72MHz crystal oscillator was connected. Now I know why the board is connected to an 8MHz crystal oscillator, but the main frequency of the system is 72MHz.

         Finally, pay attention to the three clock buses of AHB, APB1, and APB2. Above we are concerned about the system clock. We can see what the clocks of other peripherals look like when we delay it. The reason for this, I guess, is to balance performance and reduce power consumption. This is the reason why we have to open the corresponding clock every time we use a peripheral. What do you think, the operating frequency of peripherals and peripherals is definitely different. They are as fast as the system clock and are prone to problems. It is more accurate than the longer the ADC takes to collect analog signals. Different peripherals are mounted on different clock buses, as shown in Figure 3-3.

Figure 3-3 Peripheral Clock

 Fourth, analyze how the clock is selected from the source code

         Here I will use the standard library to tell you about it. Registers are too difficult [woowoowoo~]. I believe that most of the standard libraries you use are V3.5.0. After all, it was launched in 2011, and then it will not be maintained and updated. What? You don't know which version of the standard library is used? Well, we found and opened stm32f10x.h, by the way, this x means irrelevant item, which means that stm32f100, stm32f103, stm32f107, etc. are applicable. Open it, and then we can see @version when we pull it to the front. The library version in Figure 4-1 is 3.0.0, not 3.5.0 .

Figure 4-1 Standard library version

         The handsome and luminous friend is going to ask, you said that the common one is V3.5.0, here is a picture that is not V3.5.0, does it have a special meaning? Nah, that's right! I don’t know if you have seen the SystemInit() function at the beginning of the main function, as shown in Figure 4-2.

Figure 4-2 SystemInit()

         Could it be that he is used to configure the system clock? Nah, that's right! The library function below V3.5.0 must be called once in the main function, but it is not needed in V3.5.0. It is often seen that some people also call it in V3.5.0, it is possible but not necessary, the reason is shown in Figure 4 -3 shown.

Figure 4-3 Startup file parsing

         In the startup file startup_stm32f10x_md.s provided by ST, line 133 jumps to SystemInit() to execute the clock configuration, and finally jumps to main.c, so there is no need to repeat the call.

        Finally, let's end the article exploring SystemInit(). SystemInit() is defined in the system_stm32f10x.c file, as shown in Figure 4-4.

Figure 4-4 Clock Configuration

                 The official description of this function is as follows: Initialize the Embedded Flash Interface, the PLL and update the SystemCoreClock variable. Well, it turns out that in addition to configuring the clock, it can also initialize the FLASH. We find the code for the clock, as shown in Figure 4-5.

Figure 4-5 Clock Configuration

         Uh, well, SetSysClock() is called again, let's jump in and see, the method is shown in Figure 4-6.

Figure 4-6 Function jump

        After jumping in, we found that we chose HSE as the system clock source, and chose the system clock as 72MHz, and jumped to SetSysClockTo72() for real configuration. The real mountains and rivers are full of doubts and there is no way out, and there is another village in the dark, as shown in Figure 4-7.

Figure 4-7 Clock Configuration

         The question is, there are so many lines, why only 72M is selected in this function (the gray one is ignored by the compilation, the features of keil in the conditional compilation, it is intuitive and easy to use), here we have to turn to the beginning of the file, do it here out the macro definition. So after talking so much, it turns out that to modify the system frequency, you only need to change this macro definition (note 72M, restore the content of the note of the desired frequency), as shown in Figure 4-8.

Figure 4-8 How to modify the clock frequency

         Finally, if you are interested, you can combine the STM32F10X register manual to see how SetSysClockTo72() is configured in detail. The location of SetSysClockTo72() is shown in Figure 4-9. I will not expand it here. I wish you all a happy study!

Figure 4-9 Register configuration

 Don't look at it, I really can't write! ! !

Guess you like

Origin blog.csdn.net/fantastic_sky/article/details/110311790