How the program works
Before we start the journey, let's explore how the simplest C program works.
To make the program simple enough, we can get the CPU to take instructions (fetch instruction) directly from Flash and execute it, and the program does not use a global variable, so the data section length in the translated destination (object file) is 0, so The steps to initialize RAM are avoided because the data section is read and if there is a data section in the destination, we have to copy the data section into RAM in the program's activation process to ensure that the program works correctly.
Program's Environmental
Let's define the memory map (memory map) When we execute the program:
The shadow area of Flash represents a saved program image (the program image) in which stack (stack) in the process is only stored in RAM, indicating the black point on the right side of the stack.
The running process of the program after reset
Once the processor is reset, a "get command-decipher-execute" loop, the 3-stage pipeline, is done. As a result, the PC (program counter; The following PCs all refer to this timer, not the personal computer) the value of the timer after the reset is critical. In "The definitive Guide to ARM cortex M3" (Simplified Chinese), we know that the first thing that ARM cortex does after leaving the reset state is to read the values of the following two 32-bit integers:
Remove the initial value of MSP (Main Stack Point) from the address 0x00000000
The initial value of the PC is removed from the address 0x00000004, and the processor is taken from the address of this value
You can use the diagram below to explain:
Note that the initial value of the PC is removed from the address 0x00000004, where the LSB must be 1 (a later experiment is perceived). Why, Why? The address of an exception processor (exception handler) is set to 1 in the LSB, representing the exception processor in the THUMB mode (Thumb mode), which is necessary for arm cortex-m3, Because the processor core supports only the Thumb-2 instruction set and does not support arm mode (also known as arm code or arm state), the exception processing is run in thumb mode, under the "the definitive Guide to ARM cortex M3 Description of ":
With the introduction of the Thumb-2 instruction set, it's now possible to handle all processing requirements in one Oper ation state. There is no need to switch between the two. In fact, the CORTEX-M3 does is not support the ARM code. Even interrupts are now handled with the THUMB state. (Previously, the arm core entered interrupt handlers in the "arm state.") Since There is no need to switch between states, the CORTEX-M3 processor has a number of advantages over traditional ARM p Rocessors, such as:
No state switching overhead, saving both execution time and instruction spaces
No need to separate ARM code and THUMB code source files, making software development and maintenance easier
It ' s easier to get the best efficiency and performance, in turn making it easier to write software, because there is no NE Ed to worry about switching code between ARM and Thumb to try to get the best density/performance
The above is the arm of Cortex M3 core definition of the line, that STMicroelectronics as a chip manufacturer, is how to actually do it. From STMicroelectronics's Reference Manual, the STM32 series processor's lead mode (BOOT) setting, and the behavior of the processor in different modes. We care about the simplest scenario: The system is inspired by the built-in flash, which is the boot0=0 situation.
As mentioned earlier, the built-in flash start address is 0x08000000, does this mean that cortex M3 cannot obtain the MSP and PC initial values required by the system reset from Flash? STM32 to understand this problem, the solution proposed is the address name (alias), and, simply, the STM32 Flash has two sets of addressable spaces, which can be accessed, in addition to the 0x08000000 access, from the address name (that is, the 0x00000000 start).
Based on the above analysis, let's summarize what information our program image should hold.
MSP initial value
PC Initial value (LSB must be 1)
program's text section, data section, etc.
The following code can reach our expectations:
ASM (". Word 0x20001000");
ASM (". Word main");
Main () {...}
In the code above, we specify the MSP as 0x20001000, that is, the stack size is 0x1000 (calculated: 0x20001000-0x20000000 = 0x1000, or 4KB), for the Hello World class program, 4KB of heap overlay should be Dolcevita Dolcevita. The thing to be aware of is that the GNU toolchain can help us with our PC initial values the LSB must be 1, so just make sure that the main symbol is a C function. In fact, compared to developing programs in the Gnu/linux or Microsoft Windows environment, with a certain degree of partition, the main function is not necessarily the first place to be executed in the C program, or even arbitrarily named, which is named in Main, and is also useful for easy understanding. After all, we did not use the launch code provided by the GNU Toolchain, and there was no need to name the entry function according to the requirements of the toolchain. In general, we call this no need for a set of rules. The application of the operating system (mainly referring to the OS ABI [Binary Interface], such as calling convention) is bare-metal.
What the program is doing.
The program, even if it is simple, must not be output, otherwise how can we test the function. In the embedded system, there is often a lack of a wide screen that allows the Hello World program to output strings, so we can only fall into the cliché and play the game.
In order to light the light, the program needs to operate on those weeks ' hard body. When you are working on these hard edges, you need to read the temporary memory.
Since the lighting program is simple, we only need to operate the STM32 Gpio (general purpose I/O) to achieve the goal. For a few of the gpio involved in the operation of the register, you can check St the reference Manual, it is also on the launch of the hard circuit, in order to confirm the value of the timer set. On the Development Board, D Gpio's 9 foot is connected with an LED light, which is the schematic of the lighting, the effect of the program is to let the LED (DS4) constantly blink flashing.
Next, we're going to analyze each of the timer settings and make sure the value of the staging.
Time to activate (enable) d Gpio Port
Configure the Gpio Port's Foot 9 (PD9) as the general push-pull output mode (output Push-pull)
To set the PD9 value of 0 and 1, which is to control the light extinction of LED lights
The setting of the timer naturally has to be traced to STMicroelectronics's reference manual, and the following readers list the relevant descriptions:
APB2 Peripheral Clock Enable register (RCC_APB2ENR)
Address: 0x40021000 + 0x18
Value after Reset: 0x00000000
Set the (IOPD EN) to 1 to activate the Gpiod clock
Port Configuration Register High (GPIOX_CRH) (X=d)
Address: 0x40011400 + 0x4
Value after Reset: 0x44444444
According to our needs:
CNF9[1:0] = (General purpose output Push-pull)
MODE[1:0] = 01 (output mode, maximum speed 10MHz)
Port bit Set/reset Register (GPIOX_BSRR