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:

    _images/1639281734520-d63c222e-6c15-46f6-9349-2aae5e3a22a1.png

    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.

    _images/1639281716434-51e185a4-bbbc-4fe6-8867-9834b0973da1.png

    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

    _images/1639281843772-333666fe-d348-4616-8f39-5ab63500e3c7.png

    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.

    _images/1639282316919-0450c010-004a-4d84-891a-14a6d0537e11.png

    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

    _images/1639282444794-6ca987a5-05f6-4377-af99-718985fae914.png