[Serial] After more than ten years of electronic development, I will look back and see through the MCU with everyone!

1f4dd5b556310bcf8c4550191d65f45a.jpeg

"After more than ten years of development of single-chip microcomputers, I look at the single-chip microcomputers back and forth from my current height, which can be said to be hopeless."

What Zhennan wants to introduce next is the "architecture model of single-chip microcomputer", which is detached from any specific type of single-chip microcomputer chip ( 我感觉我要成仙), and it has strong universality. Almost all microcontrollers, or ARM, DSP and higher-end processors follow this model. In other words, several major elements in this model are required.

See through MCU

CPU model

CPU, that is, the central processing unit, "it is the core of the computer system, occupies the supreme position, and has absolute management and control rights," as shown in Figure 1.6.

208ac1544ba620a943931aafb0326bf2.png

Figure 1.6 The CPU occupies the core position in the computer system

The core tasks of the CPU are 执行指令, for example, calculating the sum of two numbers, reading and writing registers, operating the bus to read and write memory, and so on. 每一个CPU都有自己事先设计好的一套指令集, or instruction system, each instruction completes a specific operation and function. But instruction sets don't exist in a vacuum, 每条指令必然都对应着一套电路. When the CPU executes an instruction, the corresponding circuit is actually working. Therefore, whether the performance of a CPU is excellent depends partly on whether the instruction set is rich, whether the instruction function is powerful, and whether the instruction circuit is powerful and efficient.

In terms of complexity, CPU instruction sets are mainly divided into two types: 复杂指令集(CISC)与精简指令集(RISC). Most embedded CPUs are RISC, which is reflected in the number of instructions: fewer instructions, fewer corresponding circuits, which can greatly reduce the difficulty of CPU design and reduce power consumption; On the one hand, it is manifested in the function level of the instruction: the instruction itself is generally not suitable for implementing overly complex functions, which makes the execution efficiency of the instruction relatively high. CISC is different (x86 is the most classic CISC instruction set), it has a huge number of instructions (there are about 300 at least, and even more than 500, while RISC usually does not exceed 100), and the functions of the instructions are relatively powerful . This means that the CPU using the CISC instruction set is very difficult in circuit design, and the development cycle is relatively long. But it is beyond the reach of RISC in terms of function and performance (the work done by one CISC instruction may require several RISC instructions to complete). "That's why CISC instruction CPUs are mostly used in computer systems such as large servers and workstations."

b5c372458d65bbeb9c01053c5e99704a.jpeg

In fact, CISC and RISC are only produced to meet different needs. They are not opposed to each other, but to promote each other and learn from each other. Some RISC instructions have begun to be added to CISC, and some CPUs with CISC instructions have also appeared in the embedded field. A new type of CPU that combines CISC and RISC dual instruction sets will be the future development trend.

The above is a brief introduction of Zhennan to the CPU instruction set. In fact, there are some key technologies closely related to the instruction set, such as pipeline, instruction prefetching, out-of-order execution, etc., which make the performance of the CPU more improved ( Zhennan worked at Intel China Research Institute in the early days, mainly researching this technology, so he was deeply touched). However, Zhennan will not explain it here, and interested readers can study it by themselves.

Until now, many people still ask me about the basic principles of computer, system architecture, hardware composition, etc. I am not only answering, but also asking them: "You are so interested in the basics of computer, why didn't you learn it at first?" What about computer majors?" I actually understand that many people are a little blind when they apply for the college entrance examination.

What is the essence of the order? Is a=0 in C language? is MOV in assembly language? No, the statements you see are just an expression of instructions. "The instruction is essentially a binary sequence with a certain length (such as 0101111010101010 or 1011010111011011, etc.)." After the CPU gets the instruction, "first, the instruction decoding circuit separates the opcode and operand from it," as shown in Figure 1.7 (Take the MOV instruction of 51 as an example for illustration).

0669efc247808253143ed8643e65a307.png

Figure 1.7 Decoding of instruction codes

01110100 is the instruction 74H, and its function is to transfer the following operand (00010000, namely 10H) to the A register (accumulator in 51 CPU). If this instruction is expressed in assembly language, it is MOV A, #10H. After it is translated by the assembler, it is the 16-bit instruction code in the above figure. "The proposal of assembly language is only to encapsulate the most primitive CPU binary instructions. Use some easy-to-remember tags, such as MOV, ADD, INC, etc. to represent instructions, and after being translated by the assembler, it is a sequence of instruction codes that can directly enter the CPU for execution.”

Zhennan often imagines how people input instructions to the CPU in the early days of the CPU - "punched paper tape", as shown in Figure 1.8.

07831e7984c626a5478639822a8b0912.png

Figure 1.8 People use paper tape to punch holes to input instructions to the CPU

Before the generation of assembly language, the preparation of program instructions was completely done manually. 人们将编好的若干条指令通过纸带打孔方式输入到CPU中,让它可以依次执行,最终完成整个计算任务(The 'holes' and 'solids' on the tape represent 1s and 0s). In a sense, "paper tape" is the first generation programming language, and "assembly language" is the second generation programming language. They are all languages ​​closest to CPU instructions, so we call them "low-level languages". Finally, the C language was produced, which is very close to the natural language (English) we humans use every day, which means that it is far away from CPU instructions. It requires a special compiler for preprocessing, semantic analysis, compilation, etc. to generate intermediate code (assembly), and then further assembly, connection, etc. to obtain the instruction code that can be executed by the CPU. so,C语言被称为“高级语言”。

To sum up, 我们可以认为CPU就是一个取指令执行的机器this is the main function and work of the CPU. But the architecture of the CPU is not just that simple. 如何协调取指令的过程,防止出错?指令存储在哪里?CPU如何从存储器中取出指令?We must have a deep understanding of these issues, otherwise C language and single-chip microcomputers cannot be truly proficient.

memory model

Memory is crucial to the entire computer system: "Program instructions for CPU execution, variables and data during program running..." , they all use memory as a carrier. Therefore, in actual application and development, people always hope that the RAM and ROM capacity of the single-chip microcomputer chip can be as large as possible. In this way, more code instructions can be stored and larger and more complex programs can be run. In addition, the read and write speed of the memory itself has become one of the biggest bottlenecks of CPU performance. A more vivid description is shown in Figure 1.9.

664e439d9dbf194d87f842f3af6e848d.png

Figure 1.9 The CPU fetches instructions from the memory and stores variables and data

ROM, that is, read-only memory, "that is to say, the content stored on it cannot be directly modified by the CPU . " (Usually only a dedicated programmer can be used to modify the data in it, but now some new single-chip microcomputer chips can already modify ROM data during CPU operation, this technology is called "IAP") Therefore, ROM is usually It is used to solidify and store program instruction codes and some data that does not need to be modified, such as fonts, fonts, constants, etc.

Unlike ROM, RAM is readable and writable, so it is called random read-write memory. During the running process of CPU, any data in RAM can be read, written and modified. This is the physical basis for the underlying implementation of assignment statements in C language, such as "int a; a=0", which means writing a value of 0 to a certain storage unit in RAM. But if it is "code int a; a=0", an error will be reported when compiling. (The code keyword is used in the C language of the 51 single-chip microcomputer to indicate that the location of the "variable" is in the ROM. The same definition uses const on the ARM.) Moreover, the read and write speed of RAM is much faster than that of ROM, (this 所以CPU在运行程序的时候,通常都会把一些代码指令拷贝到RAM中来,尤其是那些会被频繁执行的部分is The .text segment in C language, that is, the code segment). But RAM is usually much more expensive than ROM (you should have a macro perception of this point, a 16G U disk is only 10 yuan, but computer memory sticks cost hundreds of dollars), which is why manufacturers use single-chip microcomputer chips The reason why the ratio of RAM is very stingy, and the ratio of ROM is slightly generous.

In order to better explain the following content, everyone must be clear about the memory, which is also a common sense: it is composed of many storage units with consecutive addresses, as shown in Figure 1.10.

c2a7c0c249724a80ff00eef3598c5fc8.png

Figure 1.10 The memory is composed of storage units with consecutive addresses

Generally speaking, "the memory is a container of instructions and data, and it is interdependent with the CPU" , which enables the entire computer system to operate normally. At this time, an extremely important question arises: "How does the CPU accurately fetch instructions and data from the memory, and how does it write data into the memory?" This question is simple to say, but it is Extended a key technology - "bus"!

bus model

If the CPU is regarded as an "imperial capital" and the memory is regarded as an "Acropolis", if they want to communicate with each other, roads must be built, and this road can continue to extend branches and connect many cities in series. In this way, “it is possible to pass between two cities. This “road” is the bus!” as shown in Figure 1.11. (These connected "cities" are like the "CPU peripherals" that Zhennan will talk about later).

ed0c5e3a0cfc32a4bcee47d0ce6f554f.png

Figure 1.11 Structural model of the bus

Well, now there is this path between the CPU and the memory. At this time, if the CPU wants to read a byte at the address addr in the memory, what should it do? This process is mainly divided into three steps: (Did you think of "how many steps are there to put an elephant in a refrigerator?")

  1. The CPU first tells the memory the address to read;

  2. Wait for the memory to fetch the data at the corresponding address;

  3. The CPU fetches the data.

be4c6a1ace305f56d8b8bb294f4b9d81.jpeg

A more vivid illustration is shown in Figure 1.12.

1486aa5750c0449236211648b4283460.png

Figure 1.12 The main process of CPU accessing memory

If you think about it carefully, the realization of this process actually involves several issues: CPU如何将地址给存储器? CPU如何知道存储器已将数据准备好? CPU又如何将数据取走?... To sum up, it is mainly the transmission of addresses and data, as well as the coordination and control between them. To solve this problem, we propose this model, see Figure 1.13.

9ebf30e1321ada1c6e30edbba1294654.png

Figure 1.13 Bus model between CPU and memory (read data)

The wiring seen in the picture is the actual wire used to transmit binary signals (0 or 1). The CPU first outputs the address to the address bus (obviously the number of address lines determines the range of space that the CPU can address), and then sets the RD signal to 0 (RD is usually 1), (the number of data lines determines the 告诉存储器地址已经给出,请准备好数据并将其输出到数据总线上CPU The data throughput, which is also a measure of the number of CPU bits, 51单片机是8位单片机,则它每次只能读到一个字节的数据,ARM是32位的,所以它可以一次性读取一个字). The CPU reads the data bus, and then sets the RD signal to 1, and the whole process is completed.

How does the CPU write data to the memory? In fact, the reason is the same, as shown in Figure 1.14.

fd29fcf38fcb4b644fcdf481fd762f0c.png

Figure 1.14 Bus model between CPU and memory (write data)

It is still the CPU that gives the address first, then gives the data to be written to the data bus, and then sets the WR signal to 0, telling the memory that the address and data are ready, please process it. Finally, set the WR signal to 1. To sum up, there are three major buses in the CPU: address bus, data bus and control bus. This model ends up as shown in Figure 1.15.

e984e54ed1ddc47868ee959577e44f7d.png

Figure 1.15 Bus model between CPU and memory

If we draw a schematic diagram of the level of each bus signal changing with time during the CPU access process, it will look like this, as shown in Figure 1.16.

85c1d1702c9cba78eb2f3613f92444c7.png

Figure 1.16 Timing diagram of CPU bus operation

The above figure is the timing diagram (Timing Digram) of CPU bus operation. It is the most intuitive form for describing interface timing and signal protocols. "Understanding the timing diagram is the fundamental basis for us to learn electronics and single-chip technology, and use C language to correctly write the underlying driver."

dfacd11bb3d7f4716e39c82d7e6bd8da.jpeg

Peripheral model

We already know how the CPU reads and writes the memory through the bus. We also know that the width of the address bus determines the addressing space of the CPU, and the width of the data bus determines the number of bits of the CPU (the amount of data that can be read and written at a time). The control bus affects the memory access speed to a certain extent (the shorter the time when WR and RD are 0, the faster the memory access speed, and of course the memory speed must keep up with it). With the CPU and memory, and the bus connecting them, that's enough to make a complete, functioning computer system. "We can put some algorithms in it to run, but the single-chip microcomputer (embedded processor) is not only used for calculation, but its greater role is to control" (so the English abbreviation of single-chip microcomputer is MCU, that is, Micro Controller Unit, micro control unit). IO is the most direct and commonly used control interface. We can set it to 1 or clear it to output high or low level, so as to realize the control of external circuits or mechanisms.

Explain Zhennan's confusion from a more basic level: "How does the C language have an impact on the physical world?"

In Figure 1.6, with the CPU as the core, besides the memory (RAM and ROM), there are many controllers around it, such as IO controllers, serial port controllers, etc. These are the so-called "CPU peripherals". Peripherals are actually some circuits, which are used to achieve a specific function. These circuits must be controlled by the CPU, so a special interface (register) is reserved in the circuit design. The reading and writing of this interface is in line with the timing of the CPU bus, so it can be directly connected to the CPU bus and coexist with memory and other peripherals (but they belong to different address ranges, and the CPU reads and writes data to these addresses. different functions of the circuit). See Figure 1.17 for a more vivid illustration.

7d4dd61a836c7a2619a0ab55c6c9f704.png

Figure 1.17 Structural model of CPU peripherals

Obviously, "The entire addressing space of the CPU (its size is determined by the width of the address bus) is not exclusively occupied by the memory. The memory only occupies a certain section of it, and other address spaces are allocated to various peripherals, and More may just be reserved for idle use.” Someone may ask: “If this is the case, then we can connect the circuit we made to the CPU bus to expand the peripherals for the CPU.” That’s right, as long as the CPU chip passes the bus When the external pins are opened, we can hook up our own circuit and let the CPU directly access the control, such as hooking up an LCD screen with an 8080 interface. (51's xdata and STM32's FMC are examples of the CPU core opening the bus to the outside world, and you will see some clever application examples of the external bus of the single-chip microcomputer later).

MCU runs

"Well, with the CPU, memory, bus and peripherals, we organically package them together, and then lead out the signals of each peripheral, bus, and power supply and ground through the pins. This is a complete single-chip microcomputer. Chip. Wait, there seems to be something missing to make the microcontroller run? Yes, there is a clock!"

clock system

If the above-mentioned are just the shell of the single-chip microcomputer chip, then 时钟就是在其中流动的血液和跳动的脉搏. The clock is crucial to the microcontroller, it is the incentive of the whole system. 它是否稳定、是否精确、是否高速都直接影响了单片机中所有电路的运作, including CPU, bus, peripherals, etc. A clock is essentially a square wave signal, as shown in Figure 1.18.

5bcf73e8a0a0ba9edfb84f27b7d8ec09.png

Figure 1.18 The clock plays a vital role in the microcontroller

Some people say: "Since the speed of the clock determines the speed of the single-chip microcomputer, as long as the clock frequency is increased as much as possible, the performance of the single-chip microcomputer can be improved!" In principle, this is true, but because of many factors, such as the characteristics of semiconductor materials , chip manufacturing process, etc., so that the clock frequency cannot be increased without limit (an excessively high frequency clock signal will cause the circuit of the single chip microcomputer to work abnormally, and the problem mostly occurs in the integrity of the signal). See Figure 1.19 for an explanation of this.

96e3f65a0b3b2f7eb7c0cc68ee2bfe5a.png

Figure 1.19 Too high a clock frequency will result in compromised signal integrity

To put it more vividly, it is like 人的心跳不能太快, otherwise, the blood would have passed away before it could send oxygen and nutrients to various tissues. At the same time, the blood vessels cannot carry such high blood pressure and blood velocity, which eventually leads to the disorder of the overall function of the human body. On the contrary, the heartbeat cannot be too slow, otherwise the blood will also not be able to complete the work of transporting supplies ( 单片机的时钟过慢可能无法满足我们的应用需求,所以实际应用过程中,选取一个合适的工作时钟是非常重要的).

But then again, many times we really hope that the microcontroller runs as fast as possible, such as some real-time algorithms with a lot of calculations, signal acquisition, audio and video recording and playback, and so on. Therefore, countless engineers and scientists are working to improve hardware performance, increase clock frequency, improve processing technology level, and even try new semiconductor materials or improve circuit structure. But despite this, the clock frequency will still have an insurmountable ceiling. And it is found that the faster the clock is, the greater the power consumption when the circuit is working. These dilemmas and the constant pursuit of high performance and low power consumption have led to the development of CPU chips in the direction of multi-core (the clock frequency is no longer increased, but the overall performance is improved by increasing the number of CPU cores in the chip).

Zhennan said above that the faster the clock, the greater the power consumption. It is not difficult to understand that the power consumption must be minimal when the circuit is not working; once it starts to work with a clock, the power consumption must increase. This principle is the fundamental basis for realizing low power consumption in many single-chip microcomputer chips. "The designer of the single-chip microcomputer configures a clock switch for each peripheral circuit" (these switches are also some special function registers connected to the CPU bus, so they can be directly controlled by the CPU), so that the peripherals can be controlled to stop or work. This reduces the overall power consumption of the microcontroller to a great extent. This is a new concept in some advanced microcontrollers - clock configuration (CC). It may actually be more powerful, "Not only can you turn off the clock supply of a peripheral, but you can also adjust the frequency of the clock so that the peripheral can work at the most suitable power consumption level."

binary

"Binary" is taken out as a separate section because it is an extremely basic concept. However, many people do not have a visual understanding of binary, and even some people who have already started and have a little development experience still have a vague understanding of it. Therefore, Zhennan believes that it is necessary to focus on elaborating it in a more vivid, popular and profound way, so as to lay a solid foundation for our future study.

We humans have been using a counting method that is considered very natural since ancient times, that is, the decimal system. Its principle is very simple, that is, "full decimal one" (why it is decimal, the root cause is that we have ten fingers). If "XY" is a 2-digit decimal number, then ten numbers from 0 to 9 may appear in each of its digits. A certain bit is currently 9, if it is +1, it will return to 0, and at the same time, it will be 1 to its higher bit. This is how counting works (regardless of base).

Since humans are used to using decimal, why use binary in microcontrollers? Isn't it bad to design it as decimal? In the early days of the advent of computers, or among some technical fanatics, some people did try to create computers of other bases. But no matter what base system is used, the various basic contents of the CPU system introduced by Zhennan earlier must be followed and implemented, such as instruction search and execution, bus operation, and so on. Well, Na Zhennan will take a link in the bus operation - "the CPU gives the address bus the address of the storage unit in the memory to be accessed" as an example, and implement it in decimal.

Suppose the address to be accessed is (3456) 10, see Figure 1.20.

cb4cc21325e9476f50f9a63b65312af3.png

Figure 1.20 The CPU generates addresses to the memory in decimal

Obviously, to transmit the value 3456 in decimal, we need to use 4 address lines, and each address line transmits the four numbers 3, 4, 5, and 6 respectively. Dare to ask everyone, how should this be passed on? How can a line express the ten numbers from 0 to 9? Someone said: "Yes! I divide 5V into 10 parts, 0V ~ 0.5V represents 0, 0.5V ~ 1V represents 1, and so on, 4.5V ~ 5V represents 9." Yes, very smart, this is the legend The "analog computer" approach in Its signal line transmits analog voltage signals instead of digital signals.

While the above approach is possible, there are many factors that prevent people from doing it:

  • The realization of the circuit is relatively difficult, and the design of the analog circuit is much more complicated than the data logic circuit;

  • The transmission speed is not high, and the generation, collection and reception of analog signals are slower than digital signals;

  • The stability and anti-interference ability are relatively weak, and the transmission value is determined only by the voltage difference of 0.5V, which is prone to errors;

  • Power consumption is difficult to reduce, the complexity and scale of the analog circuit and other factors doom its power consumption to be relatively large.

……

Regarding Article 3, someone once raised a question: "I can increase the voltage. I can define 5V as 10V or 20V, so the voltage difference will be widened?" Smart, but you have considered the power consumption question?

Let's implement it in binary mode. (3456)10 converted to binary is (110110000000)2, please see Figure 1.21.

1db6908c94bd1086a5260999ad5aa5b4.png

Figure 1.21 The CPU generates addresses to the memory in binary mode

Binary is full binary, converting a decimal value into binary will increase the number of digits, so we need more address lines. Each bit of a binary number can only represent the two numbers 0 and 1, which corresponds to using two levels on the address line, such as 0V and 5V (actually it may be 0V~2.xV means 0, 2 .xV~5V means 1). The benefits of doing this are obvious, the difficulty of circuit design has dropped a lot, and the anti-interference ability is relatively strong. More importantly, the transmission rate of the signal can be made relatively high, and finally the overall performance of the computer system can be improved. In addition, the binary also makes the power consumption of the chip can be greatly reduced, because we can define the high level as 3.3V, 1.8V, or even 1.2V. (The lower the high-level voltage is defined, the faster the microcontroller signal climbs from low level to high level. 因此,降低电平电压将有利于时钟频率的提高。)

To sum up, everyone should have a deeper understanding of the significance of using binary in computer systems. Binary is the foundation of the computer and the basis for the implementation of the underlying CPU hardware and many related circuits. Therefore, in a lot of development and research work related to single-chip microcomputers, a lot of binary concepts and applications will be involved.

interrupt mechanism

The interrupt mechanism is the most important in single-chip and embedded systems, and we must understand it deeply. First of all, we need to understand one thing: CPU执行指令代码,并非一直顺序地逐条执行,而是可能突然跳到某段代码上去的. Because the priority of this code is higher, or it is more urgent, the CPU must temporarily put down the work at hand and execute it immediately, otherwise it may lead to adverse consequences, or even serious accidents. This "sudden jump" is sometimes predictable, or intentional by the designer, but sometimes it is random, and the specific time of its occurrence cannot be determined in advance. This is the most popular expression of "interruption", as shown in Figure 1.22.

6356536e3a29ae65c42e9abd01bcdac4.png

Figure 1.22 Expression of "interruption mechanism"

Some people say: "The interrupt seems to be messing with the CPU. It always interrupts the normal execution of the program." Yes, but it cannot be said to be "troublesome", because the existence of interrupts is reasonable and is to solve real problems. arising from the problem. For example, a single-chip microcomputer is working normally, and it also needs to receive data from the serial port at the same time, but it does not know when the data will arrive. To solve this problem, we can use the CPU polling method, that is, constantly check Whether there is new data coming, if so, receive it. The biggest problem with doing this is to waste the running time of the CPU, which may affect the execution efficiency of other tasks. "If the interrupt method is used, the CPU will be freed, and it can do other work with peace of mind before no data arrives." The serial port controller (CPU peripheral) actively notifies the CPU after receiving new data (the notification process Depending on the special interrupt control circuit and the interrupt source of the CPU), the CPU immediately jumps to the pre-designed processing code (ISR, interrupt service routine) to execute, and completes the data reception and processing. Finally, jump back to the original "breakpoint" and continue to complete the work at hand.

Regarding interrupts, it actually contains a lot of content, such as on-site maintenance, interrupt vectors, interrupt priority, interrupt response speed, interrupt nesting, and so on. To integrate all these, we can be comfortable in the actual development process. However, interrupts are often more complicated than we imagined: if the main program generates an interrupt during sequential execution, the CPU immediately turns to the interrupt service routine, then if an interrupt is generated again during the execution of the interrupt service routine, Where should the CPU go? This is the problem to be solved by the "interrupt nesting" mentioned above (here is just a brief talk, so that everyone can have a preliminary understanding).

f22b19868e1d7728e5562fc3c751a6dd.jpeg

Well, this chapter comes to an end here. Zhennan hopes that through this chapter, everyone can fundamentally and truly understand the essence of CPU and single-chip microcomputer architecture and operating mechanism, and establish a visual model in their minds. With this foundation, everyone's understanding of single-chip microcomputers can truly be three-pointed, and learning and comprehension can get twice the result with half the effort.

"Single-chip microcomputer and C language are actually not difficult. In a sense, it is just a "skilled job", the most important thing is "getting started". With the basics and our ingenuity, everyone can become a master! "

After a year of preparation, Mr. Yu Zhennan combined with years of practical experience to create a new and systematic course " Ten Days to the Top of Embedded C Language (Master C) ", with a total of 100 lectures and a total class time of more than 2,000 minutes. Deciphered many problems that you don't know and often make mistakes in the embedded C language for us.

I hope that this set of courses can enable those who have already started C language and are still wandering [halfway up the mountain] to take it to another level, and finally reach the pinnacle of embedded C language! !

9c8563572f476000735873d71887ba36.jpeg

ffac56eb9b357607da72261d55b80468.jpeg

Click to read the original text to register and learn with your friends!

Guess you like

Origin blog.csdn.net/karaxiaoyu/article/details/131346091