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

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
The esp32.Partition(id) function in MicroPython is used to access the partition information of ESP32. 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(id) function allows you to get the details of a specific partition on the ESP32, including the partition's name, type, size, offset, etc.
Partitions are logical divisions of ESP32 memory and can be used to store different types of information such as firmware, data, and configuration.
By accessing partition information, you can understand the properties of each partition to better manage and utilize the ESP32's storage resources.

Application scenarios:

Partition information is useful for firmware upgrades and management. You can determine which firmware partition to upgrade by checking the partition information and ensure the correct firmware is loaded and running.
Partition information is also important for data storage and management. You can know the size and offset of each partition so that you can read and write data correctly in your program.
During the development process, partition information can help you understand the allocation and usage of storage resources for resource optimization and adjustment.

Precautions:

Before using the esp32.Partition(id) 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(id) function correctly.

Here are a few practical application examples:

Case 1: Check firmware partition information:

import esp32

# 获取固件分区信息
fw_partition = esp32.Partition(esp32.Partition.RUNNING)

# 打印分区名称、类型、大小等信息
print("Firmware Partition Info:")
print("Name:", fw_partition.get_label())
print("Type:", fw_partition.get_type())
print("Size:", fw_partition.size())
print("Offset:", fw_partition.offset())

In this example, we use the esp32.Partition() function to obtain information about the currently running firmware partition. By calling methods such as get_label(), get_type(), size() and offset(), we print out detailed information such as the name, type, size and offset of the partition.

Case 2: Managing data partitions::

import esp32

# 获取数据分区信息
data_partition = esp32.Partition(b'data')

# 读取数据分区的偏移量和大小
data_offset = data_partition.offset()
data_size = data_partition.size()

# 在数据分区中写入数据
data = b"Hello, World!"
with open("/flash/data.bin", "wb") as f:
    f.seek(data_offset)
    f.write(data)

In this example, we use the esp32.Partition() function to get information about the data partition named "data". Then, we get the offset and size of the data partition. Next, we write the data in the data partition by opening the file and using the offset. This example shows how to leverage partition information to correctly read and write data.

Case 3: Partition table information display::

import esp32

# 获取分区表信息
partition_table = esp32.PartitionTable()

# 打印分区表中的分区信息
print("Partition Table:")
for i in range(partition_table.count()):
    partition = partition_table.get_partition(i)
    print("Partition", i+1)
    print("Name:", partition.get_label())
    print("Type:", partition.get_type())
    print("Size:", partition.size())
    print("Offset:", partition.offset())
    print()

In this example, we use esp32.PartitionTable() to obtain information about the entire partition table. Then, by looping through each partition, we print out details such as the partition's name, type, size, and offset. This example shows how to use the esp32.PartitionTable() and get_partition() methods to obtain and display information about the entire partition table.

Case 4: Read partition information:

import esp32

partition = esp32.Partition(esp32.Partition.RUNNING)
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("Partition Encrypted: {}".format(partition.encrypted()))

In this example, we use esp32.Partition()the class to get the currently running partition information. By passing esp32.Partition.RUNNINGas parameter, we create a 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: Wipe partition data:

import esp32

partition = esp32.Partition(esp32.Partition.RUNNING)
partition.erase_range(0, partition.size())
print("Partition data erased.")

In this example, we use esp32.Partition()the class to get the currently running partition object. Then, we use erase_range()the methods of the partition object to erase the data in the partition. By passing the starting address and the size of the erase range, we can erase the partition data within the specified range. In the example, we erase the data of the entire partition and print out the erasure completion information.

Case 6: Switch partition boot:

import esp32

partition = esp32.Partition(esp32.Partition.RUNNING)
partition.set_boot()
print("Partition boot set.")

In this example, we use esp32.Partition()the class to get the currently running partition object. Then, we use the method of the partition object set_boot()to set the current partition as the boot partition on the next startup. This can be used to switch partition boot to run different code or systems after reboot. In the example, we set the current partition as the boot partition at the next startup and print out the information that the setting is completed.

Case 7: Read partition information:

import esp32

# 获取分区信息
partition = esp32.Partition(0)
info = partition.info()

# 打印分区信息
print("分区类型:", info[0])
print("分区子类型:", info[1])
print("分区大小:", info[2])
print("分区偏移量:", info[3])

Case 8: Reading partition data::

import esp32

# 获取分区数据
partition = esp32.Partition(1)
data = partition.read()

# 打印分区数据
print("分区数据:", data)

Case 9: Wipe partition data::

import esp32

# 获取分区对象
partition = esp32.Partition(2)

# 擦除分区数据
partition.erase()

print("分区已擦除.")

Note that the partition IDs in the above example may need to be adjusted based on the actual partition configuration. You can select the appropriate partition ID to operate based on the partition layout and needs on the ESP32.

These examples show the esp32.Partition(id) class in action. It can help you read partition information, erase partition data and switch partition boot to meet different application needs. Please note that the specific partition ID and operation 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/132919332