There are no shortage of resources, only unoptimized code

This article is shared from the WeChat public account of China Mobile Internet of Things " OneOS Technology Practice - There are no insufficient resources, only unoptimized code ".

The game of cost and allocation

For MCUs, resources are costs. For the same series of chips, manufacturers usually introduce various gradient RAM and ROM configurations to meet different application requirements. For the same package, the larger the RAM and ROM capacity, the higher the price. Therefore, when selecting a chip, it is impossible to simply choose the highest configuration, but to choose the configuration that best meets the requirements under the premise of evaluating application requirements and ensuring a certain margin. So how to take up the least amount of resources when realizing the given product function; or give the product richer functions under the limited resources?

With OneOS development, you have many options.

First, it is developed based on OneOS-Lite. OneOS-Lite is specially tailored for resource-constrained MCUs, and is streamlined and easy to use.

OneOS-Lite版

Second, custom tailoring. If the Lite version cannot meet the needs, then based on the OneOS mainline version, through customized tailoring, you can also create an exclusive version that suits you.

Let's take the mainline version as an example to see what effective resource optimization methods are available! The optimization idea can also be generalized to other versions~

Resource optimization ideas

(1) Based on the idea of ​​function tailoring

Resource optimization, the first and easiest thing to think of is cropping, that is, only the useful ones are kept and the useless ones are discarded.

1. Do not open the principle

OneOS provides a wealth of peripheral drivers and functional components. For a specific project, components that will not be used from beginning to end can be directly closed. For example, in a metering application, Graphic, Touch, USB and other drivers, Micropython , GUI, Jecrryscript and other components will not be used, then directly remove the corresponding function module in menuconfig.

2. The principle of instant opening

OneOS provides rich development and debugging tools, such as assert, Shell, Atest, Dlog, Diagonse, etc. These functional components are powerful tools in the development process, which can help developers discover potential risks, analyze and locate problems, etc., but these functions do not directly affect the function of the product, so we can dynamically turn it on or off during the development process. , it is turned on when it is used, and it is turned off when it is used up. For example, the Diagonse component, when a hardfault problem occurs that is difficult to locate, open it, and use its stack backtracking function to quickly locate the problem. After fixing the problem, turn it off again.

3. Use it well

If there are some functions or components, we can't avoid using them, but we don't need full functions. At this point, we can turn on some of the functions we need and turn off irrelevant sub-functions. For example, a project will use the AT parser of the molink component, and it is not necessary to print the original data information of the AT interaction, this sub-function can be turned off. Similarly, the default stack space allocated by the AT parser is 2KB. When we actually use it, we find that 1KB is enough, so we can directly modify it to 1KB.

The above idea based on functional module tailoring is to eliminate the resource consumption of irrelevant code. When a functional module is closed, its related code will not participate in the compilation, and the optimization effect of RAM or ROM will be immediate.

(2) Based on the idea of ​​reasonable distribution

When the functional module has been determined, then we can finely adjust the allocation of resources according to the specific application, and realize resource optimization from the perspective of improving the utilization rate of resources.

1. Thread stack resource allocation

When creating a user thread, the stack space size of the thread needs to be allocated in advance. If the thread stack is set too large, it will cause a waste of memory, but if the thread stack is set too small, a stack overflow error will occur. Then you can determine the size of the final stack according to the maximum percentage of thread stack usage. Generally, it is more appropriate to control the percentage at 80%.

2. Interrupt stack resource allocation

The interrupt stack is the stack space used in the interrupt process, and the stack space is the static memory allocated during the compilation phase. The stack size is usually specified in the startup file or linker script. You can find the address of the top of the stack through the map file, and search the unused space of the stack space to get the usage percentage of the interrupt stack, and then control the usage percentage to an appropriate level.

3. Reduce static memory usage

Reducing static memory usage can effectively improve memory usage efficiency. Define static buffers and large structures, the memory required by them has been fixed at compile time and cannot be used for other purposes. If a functional module has only a specific scenario or periodic work in the entire life cycle, the memory used by the functional module should be dynamically applied for and released after it is used up. For example, a 4G module is used in the project, and data interaction with the platform is carried out every 30 minutes, then the thread of the module should be dynamically created and recycled after the data interaction is completed.

4. RAM and ROM space replacement

In general, RAM resources are more tense than ROM resources, and the problem of insufficient memory can be alleviated by swapping RAM space and ROM space. Once a binary file is generated, its ROM space occupation is fixed, and developers can easily assess whether the ROM space is sufficient; however, the RAM space occupation changes dynamically during code execution, and sufficient margin can reduce the risk of memory overflow. Therefore, in the development process, consciously putting data into ROM as much as possible can replace more RAM space.

The on-demand allocation of resources can make limited resources have a higher utilization rate and reduce unnecessary waste. This kind of optimization idea can run through the entire development process, adding up to a lot.

(3) Based on compilation and optimization ideas

Based on compilation optimization, resource usage can be optimized as a whole, and the optimization effect is obvious.

Using different compilers, the generated object files are also different. Common ones are gcc, armcc, etc. Selecting a suitable compiler can achieve the purpose of resource optimization, but in view of the actual situation of general chip support, the optional compiler is limited. But for chips with arm architecture, keil provides two different versions of the compiler, AC5 and AC6. Compared with the former, both RAM and ROM have obvious optimizations under the same optimization level.

In addition to choosing a compiler, the same compiler can also choose different optimization levels. Selecting an optimization option that is beneficial to resource optimization can significantly reduce the size of the target file.

development example

Let's take a look at the results of resource optimization through the performance of OneOS in practical applications!

In a metering ecological project, the MCU model is FM33G048, the total memory is 24kb, and the total Flash is 256kb. The supported drivers and components are as follows:

Resource optimization method:

Resource comparison before and after optimization:

It can be seen that through 5 steps of tailoring and adjustment, the optimization ratio of RAM and ROM has reached more than 25% , which saves so many resources. Let's say, if the boss wants to add some fashionable functions, it is not impossible!

The final resource usage is as follows:

Ok, this is the end of this resource optimization introduction. If you want to know other aspects of OneOS, you can leave a message in the comment area~

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324138455&siteId=291194637