Original article. You are welcome to repost it. For reposted content, please indicate the source.
This week's progress is slow. It can only be done after work on weekends and evenings. The events are not continuous, and there are many trivial things. It's annoying. How trivial is it?
1. I am a little obsessive-compulsive. Although RTT classifies folders very well, it still looks uncomfortable to put them in a project and directory, so I'm an obsessive-compulsive disorder, sort it out. According to the previous project habits, we divided the program into four layers: hardware layer, driver layer, system layer, and application layer, the hardware layer and driver layer are placed in the BSP folder. The BSP file is divided into hardware and driver folders and a library folder is added. The system layer is RTT, which contains the components, include, libcpu, and SRC folders, which are closely related to RTT. The last step is the application layer. The file name is applications, applications, and library file folders are all separated from the official RTT. A prj folder is added to the project files of Keil and a CFG folder, which contains some configuration code. This score is based on your preferences.
Refreshing a lot .. Haha .. Si is used to store the source insight project file.
2. Configure the system clock.
When we get a new single-chip microcomputer, I/O port control is often the first requirement, that is, the flow lamp. But there is another key thing that needs to be clearly identified at the very beginning is the various clock configurations in the system. 51 is relatively simple and does not need to be configured. What is the crystal oscillator? The running event of a command is one of 12 points of the crystal oscillator. The I/O port needs to be configured, and the clock needs to be configured. You can select the internal clock and the clock speed level. By the time it reaches 430, the clock configuration is more complicated. There is more than one clock source in the system, and there are several, which can be divided separately. Let's say 407. The clock is more complicated than 430 ..
This figure has enough attention. from the left, I haven't used the Ethernet module of the stm32 enhanced series microcontroller. I won't talk about it here. The entire system clock can have four sources: external high frequency, external low frequency, internal high frequency and internal low frequency. After selecting the clock source, set the PLL to perform frequency doubling and send it to sysclk. This is the speed at which the system runs, and then configure the clock of the external devices on the apb1 and apb2 on the AHB bridge, some low-speed devices are mounted on apb1 and high-speed devices are mounted on apb2. The next figure will be clearer:
After talking about this, how can I configure the code. Haha .. The Code settings have been set in the system startup, and have been set before Main. In startup_stm32f40xx.s, you can see the following Assembly:
1 LDR R0, =SystemInit2 BLX R03 LDR R0, =__main4 BX R0
We can see that the line calls the systeminit function and then enters our common main function. Why do we need to call the systeminit function first? I am not very clear about it, however, it seems that the cmsis (what is the standard set by arm) specification requires the system initialization function name to be systeminit. let's take a look at what this function has done. We first set FPU, then open the external crystal oscillator port, and set the frequency doubling through pll_m, pll_q, pll_n, pll_p, and so on. The figure above shows the settings of the frequency division and frequency doubling. As for how to configure it, I will not talk about it here. The library upgrade below will be clear and I will not repeat it.
3: Standard library upgrade
We have a stable version of rtt1.2.1, but not 2.0.0 (alpha) beta version (alpha): beta version, beta version: beta version gamma (gamma) version: A very mature test version.) This is not a joke. If the plane is flying and running, the loss of fried chicken is not small, and the library in 2.0.0 is also 1.0.0.
Let's talk about why I want to update the standard library to 1.3.0. It is said that IIC has a bug. Many people may have a bug when I use 1.0.0, and it came out when I was 30-September-2011, it has been a long time, and versions 1.0.1 and 1.0.2 have appeared in the middle. It indicates that small updates are quite common and there should still be many defects. The latest version 1.3.0 is 08-November-2013, nine months from now, it's a relatively new one. Using the new stable version can avoid unsolvable errors and minimize the trouble of future development. Next we will talk about how to upgrade.
Note the following when upgrading the database.
(1): The first file involved in the definition of single-chip microcomputer is: stm32f4xx. h, 1.0.0, 407 adopts the macro definition of stm32f40xx. If you do not add the macro definition in the pre-processing of Keil, the compilation of stm32f40xx will certainly fail. if the definition is complete, there will be no problem, because in. the H file contains
1 #ifdef STM32F40XX2 #define STM32F40_41xxx3 #endif /* STM32F40XX */
In a word, it is to be compatible with the previous definition. The correct definition of 1.3.0 407 is to define the macro stm32f40_41xxx. You can add this macro in Keil preprocessing or in stm32f4xx. h. h add (2): the external crystal oscillator frequency macro definition system default definition external crystal oscillator is 25 MHz, in stm32fxx. h contains
1 #if !defined (HSE_VALUE) 2 #define HSE_VALUE ((uint32_t)25000000) /*!< Value of the External oscillator in Hz */ 3 #endif /* HSE_VALUE */
Our board is 8 MHz. We overwrite this macro definition and redefine it in stm32fxx_conf.h as follows:
1 // sky. zhou, defines the external crystal oscillator 8mhz2 # If defined (hse_value) 3/* redefine the HSE value; it's equal to 8 MHz on the STM32F4-DISCOVERY kit */4 # UNDEF hse_value5 # define hse_value (uint32_t) 8000000) 6 # endif/* hse_value */
Although this can be left unspecified, you only need to set the correct pll_m, pll_q, pll_n, and pll_p of the system clock, but when calling the rcc_getclocksfreq () function, we cannot get the correct clock, because this function is calculated based on pll_m, pll_q, pll_n, pll_p, and hse_value and then returned to us, therefore, it is necessary to define the correct hse_value value. (3): Adjust the RCC according to the external crystal oscillator size. This involves the system_stm32f4xx.c file. The PLL is set based on the 25 MHz frequency, that is, pll_m, pll_q, pll_n, pll_p, we can set Jiang pll_m to 8. Why is pll_m 25? Why is the external crystal oscillator 25 MHz set by default? Ha, to prevent overclocking, if the default value is 8 MHz, we connect 16 MHz. The frequency is not 168*2 = 336mhz, it is estimated that the fever will be burned very seriously. There are few computers that exceed the frequency twice. If the fever level is set off, it will be cooled down with liquid nitrogen. Otherwise, it will be burned instantly. The crystal oscillator is 4 ~ 26 MHz. If the default configuration is followed by a maximum crystal oscillator of 26 MHz, the frequency can be PLL to 174.7 MHz. It is estimated that the 407 is affordable, st must have considered this situation, but I didn't try it, so money cannot be wasted .. Haha .. (4): The file involved in the interrupt entry file is stm32f4xx_it.c. Because rt_thread RTOS is used, You Need To comment out several interrupt entries, including hardfault_handler (void) and pendsv_handler (void) void systick_handler (void) and void ppp_irqhandler (void) because they are in the context _ * of RTT _*. S and boarc. I have defined it in C. If I don't comment it here, it will lead to errors. Then I can put other RTT interrupt functions in it. But I don't think I should put my terminal functions here, this avoids the trouble of upgrading the database. The less coupling, the better the independence.