[Diao Ye Learns Programming] MicroPython Manual ESP32 Specific Port Library esp32.Partition.info()

Insert image description here

MicroPython is a lightweight version of the interpreter designed for running the Python 3 programming language in embedded systems. Compared with regular Python, the MicroPython interpreter is small (only about 100KB) and is compiled into a binary Executable file to run, resulting in higher execution efficiency. It uses a lightweight garbage collection mechanism and removes most of the Python standard library to accommodate resource-constrained microcontrollers.

The main features of MicroPython include:
1. The syntax and functions are compatible with standard Python, making it easy to learn and use. Supports most core syntax of Python.
2. Directly access and control hardware, control GPIO, I2C, SPI, etc. like Arduino.
3. Powerful module system, providing file system, network, graphical interface and other functions.
4. Support cross-compilation to generate efficient native code, which is 10-100 times faster than the interpreter.
5. The amount of code is small and the memory usage is small. It is suitable for running on MCU and development boards with small memory.
6. Open source license, free to use. The Shell interactive environment provides convenience for development and testing.
7. The built-in I/O driver supports a large number of microcontroller platforms, such as ESP8266, ESP32, STM32, micro:bit, control board and PyBoard, etc. There is an active community.

MicroPython application scenarios include:
1. Rapidly build prototypes and user interactions for embedded products.
2. Make some small programmable hardware projects.
3. As an educational tool, it helps beginners learn Python and IoT programming.
4. Build smart device firmware to achieve advanced control and cloud connectivity.
5. Various microcontroller applications such as Internet of Things, embedded intelligence, robots, etc.

Things to note when using MicroPython:
1. Memory and Flash space are limited.
2. The explanation and execution efficiency is not as good as C language.
3. Some library functions are different from the standard version.
4. Optimize the syntax for the platform and correct the differences with standard Python.
5. Use memory resources rationally and avoid frequently allocating large memory blocks.
6. Use native code to improve the performance of speed-critical parts.
7. Use abstraction appropriately to encapsulate underlying hardware operations.

Generally speaking, MicroPython brings Python into the field of microcontrollers, which is an important innovation that not only lowers the programming threshold but also provides good hardware control capabilities. It is very suitable for the development of various types of Internet of Things and intelligent hardware.
Insert image description here

MicroPython's esp refers to the MicroPython firmware and related software libraries for ESP8266 and ESP32 chips. ESP8266 and ESP32 are a class of low-cost, low-power Wi-Fi and Bluetooth modules widely used in IoT and embedded systems. MicroPython's esp provides an advanced scripting environment for both chips, allowing developers to use the Python language for rapid prototyping and development.

ESP8266: It is a low-cost, low-power Wi-Fi module/chip developed by Espressif Systems. It has a built-in TCP/IP protocol stack, can be used to connect to the Internet, and has strong processing capabilities. MicroPython's esp provides firmware and related software libraries for ESP8266, allowing developers to use MicroPython language to develop ESP8266 applications.

ESP32: It is a highly integrated Wi-Fi and Bluetooth module/chip launched by Espressif Systems. Compared with ESP8266, it has more powerful processing power, more peripheral interfaces and more memory. MicroPython's esp also provides firmware and related software libraries for ESP32, allowing developers to use MicroPython language to develop ESP32 applications.

MicroPython's esp firmware: It is a MicroPython firmware version specifically for ESP8266 and ESP32 chips. These firmwares have been specifically optimized to run on ESP8266 and ESP32, and provide APIs for hardware interaction, network communication, and peripheral control.

Software libraries: MicroPython's esp also provides a series of software libraries related to ESP8266 and ESP32 hardware to simplify and accelerate the development process. These software libraries provide a rich set of functional interfaces, covering commonly used hardware and communication protocols such as Wi-Fi, Bluetooth, GPIO (General Purpose Input and Output), I2C, SPI, and PWM, allowing developers to easily access and control hardware resources.
Insert image description here
esp32.Partition.info() is a function in MicroPython used to obtain ESP32 partition information. The following explains its main features, application scenarios and matters needing attention in detail, and provides three practical application cases:

main feature:

The esp32.Partition.info() function is used to get detailed information of the ESP32 partition.
The function returns a dictionary containing partition attributes, including the partition's name, type, size, offset, etc.
The corresponding partition attribute value can be obtained by accessing the key of the partition dictionary.

Application scenarios:

Obtaining and displaying partition information: Using the esp32.Partition.info() function, you can easily obtain the detailed information of the partition and display it to developers or users. This is very useful for scenarios such as diagnosis and debugging, system information display, etc.
Dynamic partition attribute query: At runtime, you can use the esp32.Partition.info() function to obtain the attributes of a specific partition, such as the currently running firmware partition, data storage partition, etc. This is useful for dynamic configuration and management of partition-related operations.
Access to custom partition information: Combining the results of esp32.Partition.info() with custom application logic, you can perform different processing based on partition information. For example, dynamically allocate memory or perform specific operations based on partition size.

Precautions:

Before using the esp32.Partition.info() function, the partition table needs to be configured and loaded. The partition table defines the layout and attributes of each partition in the ESP32 memory.
The configuration and loading of the partition table are usually completed during the ESP32 firmware compilation and burning process. Make sure the correct partition table is loaded to use the esp32.Partition.info() function correctly.

Here are a few practical application examples:

Case 1: Get the currently running firmware partition information::

import esp32

# 获取当前运行的固件分区信息
fw_partition_info = esp32.Partition.info(esp32.Partition.RUNNING)

# 打印分区属性
print("Firmware Partition Info:")
print("Name:", fw_partition_info['label'])
print("Type:", fw_partition_info['type'])
print("Size:", fw_partition_info['size'])
print("Offset:", fw_partition_info['offset'])

In this example, we use the esp32.Partition.info() function to get information about the currently running firmware partition. By accessing the keys of the partition dictionary, we print out details such as the name, type, size, and offset of the partition.

Case 2: Query the size of the data storage partition::

import esp32

# 获取数据存储分区的大小
data_partition_info = esp32.Partition.info(b'data')
data_partition_size = data_partition_info['size']

print("Data Partition Size:", data_partition_size)

In this example, we use the esp32.Partition.info() function to get information about the datastore partition named "data". Then we get the size of the partition from the partition dictionary and print it out.

Case 3: Processing of custom partition information::

import esp32

# 获取特定分区的信息
partition_info = esp32.Partition.info(esp32.Partition.RUNNING)

# 根据分区大小执行不同的操作
if partition_info['size'] > 1024 * 1024:
    # 大分区处理逻辑
    print("Large Partition Detected!")
    # ...
else:
    # 小分区处理逻辑
    print("Small Partition Detected!")
    # ...

In this example, we use the esp32.Partition.info() function to obtain information about the currently running firmware partition. Then, depending on the size of the partition, we perform different operations. This example shows how to do different processing based on partition information.

Case 4: Find a specific type of partition:

import esp32

partitions = esp32.Partition.find(type='app')
for partition in partitions:
    print("Partition ID: {}".format(partition.id()))
    print("Partition Label: {}".format(partition.label()))
    print("Partition Type: {}".format(partition.type()))
    print("Partition Subtype: {}".format(partition.subtype()))
    print("Partition Size: {} bytes".format(partition.size()))
    print("")

In this example, we use esp32.Partition.find()the method to find partitions of type 'app'. By passing type='app'as parameter, we get all partition objects of type 'app' and iterate over each partition object. Then, we use the methods of the partition object (such as id(), label(), type()etc.) to get the detailed information of the partition and print it out.

Case 5: Find partitions with specific labels:

import esp32

partitions = esp32.Partition.find(label='data')
for partition in partitions:
    print("Partition ID: {}".format(partition.id()))
    print("Partition Label: {}".format(partition.label()))
    print("Partition Type: {}".format(partition.type()))
    print("Partition Subtype: {}".format(partition.subtype()))
    print("Partition Size: {} bytes".format(partition.size()))
    print("")

In this example, we use esp32.Partition.find()the method to find the partition labeled 'data'. By passing label='data'as parameter, we get all partition objects with label 'data' and iterate through each partition object. Then, we use the methods of the partition object (such as id(), label(), type()etc.) to get the detailed information of the partition and print it out.

Case 6: Find partitions of a specific size:

import esp32

partitions = esp32.Partition.find(size=4096)
for partition in partitions:
    print("Partition ID: {}".format(partition.id()))
    print("Partition Label: {}".format(partition.label()))
    print("Partition Type: {}".format(partition.type()))
    print("Partition Subtype: {}".format(partition.subtype()))
    print("Partition Size: {} bytes".format(partition.size()))
    print("")

In this example, we use esp32.Partition.find()the method to find a partition with a size of 4096 bytes. By passing size=4096as parameter, we get all partition objects with size 4096 bytes and iterate through each partition object. Then, we use the methods of the partition object (such as id(), label(), type()etc.) to get the detailed information of the partition and print it out.

Case 7: Find partitions of specified type:

import esp32

# 查找分区类型为数据的分区
partition_type = esp32.Partition.TYPE_DATA
partition = esp32.Partition.find(type=partition_type)

# 打印分区信息
print("分区类型:", partition.type())
print("分区标签:", partition.label())
print("分区大小:", partition.size())
print("分区偏移量:", partition.offset())

Case 8: Find the partition with the specified label::

import esp32

# 查找标签为'my_partition'的分区
partition_label = 'my_partition'
partition = esp32.Partition.find(label=partition_label)

# 打印分区信息
print("分区类型:", partition.type())
print("分区标签:", partition.label())
print("分区大小:", partition.size())
print("分区偏移量:", partition.offset())

Case 9: Find the first partition that meets the conditions::

import esp32

# 查找第一个数据分区
partition = esp32.Partition.find(type=esp32.Partition.TYPE_DATA)

# 打印分区信息
print("分区类型:", partition.type())
print("分区标签:", partition.label())
print("分区大小:", partition.size())
print("分区偏移量:", partition.offset())

These examples show the esp32.Partition.find() method in action. It can help you find specific partitions based on different criteria to meet your application needs. Please note that the specific conditions and matching methods may vary depending on the configuration and firmware version of the ESP32, please adjust according to your actual situation.

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_41659040/article/details/132920124