6.7. Module clipping
As shown in the CH32V103 driver module in the figure below, the modules that are not needed can be directly cut out. If there are several classes in a module that need to be used, can fine-cutting be done by class? This is also possible, which will be introduced later.
image
It is very simple to trim according to the module. Just delete the import statement in main.py, and the modules that are not imported will be automatically trimmed by the precompiler.
Taking the stm32g030c8 project as an example, the default main.py is as follows:
The first line is to import the base object. The base object is provided by the kernel, does not occupy the module space, and does not need to be clipped. The second line is the standard library and cannot be trimmed. The third row of STM32G0 chip modules and the fourth row of the on-board resource modules of the PikaPiZero development board can be cut. Compile and run, and see that the code size is 48k+3k, about 51K.
Then precompile and compile the result: It can be seen that the code size has been reduced to 46K, indicating that the module has been successfully cut.
Then cancel the import of the STM32G0 module
Code size reduced to 36K
6.7.2. Cut by class
In this way, we can create a new module, inherit the required classes from the modules that need to be used, and then only import the newly created module, you can cut out the classes that are not needed in the module. .
For example, there are 7 classes of GPIO, Time, ADC, UART, PWM, IIC, and lowLevel in STM32G0, and I only use the GPIO class.
You can create a new myDevice module, and then inherit only the GPIO class from STM32G0.
Then change import STM32G0 in main.py to import myDevice