[Diao Ye Learns Programming] MicroPython Manual ESP32 Specific Port Library esp32.Partition.BOOT

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.BOOT is a constant in MicroPython used to represent the ESP32 boot partition. It provides access to and identification of the boot partition for appropriate operations during development. The following is a detailed explanation of its main features, application scenarios and matters needing attention, and several practical application cases are given:

main feature:

esp32.Partition.BOOT is a constant used to represent the boot partition of ESP32.
It provides access and identification to the boot partition, allowing developers to understand and operate the current boot partition.

Application scenarios:

The esp32.Partition.BOOT constant is suitable for scenarios where you need to understand and operate the ESP32 boot partition.
You can use this constant to get the current boot partition for corresponding operations and debugging.
During a firmware update, this constant can be used to determine which firmware version to load and execute.

Things to note:

Before using the esp32.Partition.BOOT constant, you need to import the esp32 module first.
Make sure you understand the ESP32 partition layout and boot partition concepts before using this constant.
Note that different boot partitions may have different functions and uses. Please choose the correct boot partition according to the actual situation.

Here are a few practical application examples:

Case 1: Get the current boot partition:

import esp32

# 获取当前的启动分区
boot_partition = esp32.Partition.BOOT

print("当前的启动分区:", boot_partition)

Case 2: Load firmware based on boot partition:

import esp32

# 根据启动分区加载相应的固件
if esp32.Partition.BOOT == esp32.Partition(1, 0):
    # 加载固件 A
    # ...
elif esp32.Partition.BOOT == esp32.Partition(2, 0):
    # 加载固件 B
    # ...
else:
    # 其他处理逻辑
    # ...

Case 3: Output boot partition information:

import esp32

# 输出当前的启动分区信息
boot_partition = esp32.Partition.BOOT

print("启动分区 ID:", boot_partition.id())
print("启动分区子类型:", boot_partition.subtype())
print("启动分区标签:", boot_partition.label())
print("启动分区大小:", boot_partition.size())

Please note that in actual application, you can adopt different operations and logic based on the current boot partition according to specific needs and situations. Make sure that when using the esp32.Partition.BOOT constant, you understand the boot partition it represents and handle it accordingly according to actual needs.

Case 4: Check whether the current partition is a boot partition:

import machine

# 获取当前分区
partition = machine.Partition()

# 检查当前分区是否为启动分区
is_boot_partition = partition == machine.Partition.BOOT

# 输出检查结果
if is_boot_partition:
    print("当前分区为启动分区")
else:
    print("当前分区不是启动分区")

In this example, we use the esp32.Partition.BOOT constant to check whether the current partition is a boot partition. We first use the machine.Partition() method to get the current partition. We then compare the current partition with esp32.Partition.BOOT to check if they are equal. If they are equal, we set the is_boot_partition variable to True, indicating that the current partition is the boot partition. Finally, we use the print() function to output the corresponding results based on the value of the is_boot_partition variable.

Case 5: Mark boot partition applications as invalid::

import machine

# 打开启动分区
boot_partition = machine.Partition(machine.Partition.BOOT)

# 将启动分区的应用标记为无效
boot_partition.mark_app_invalid()

# 重启设备
machine.reset()

In this example, we use the esp32.Partition.BOOT constant to open the boot partition and mark its application as invalid. We first use the machine.Partition() method and pass in machine.Partition.BOOT to open the boot partition. Then, we use the mark_app_invalid() method to mark the application of the boot partition as invalid. Finally, we restart the device using the machine.reset() method for the changes to take effect.

Case 6: Get the size of the boot partition::

import machine

# 获取启动分区的大小
boot_partition_size = machine.Partition.BOOT.size()

# 输出启动分区的大小
print("启动分区的大小:", boot_partition_size, "字节")

In this example, we use the esp32.Partition.BOOT constant to get the size of the boot partition. We use the size() method to get the size of the boot partition and store it in the boot_partition_size variable. Finally, we use the print() function to output the size of the boot partition.

Case 7: Print boot partition information:

from esp32 import Partition
boot = Partition.BOOT
print("Boot size: %d" % boot.size)

Case 8: Reading data from the boot partition:

from esp32 import Partition
boot = Partition.BOOT
data = boot.read(0, 100)

Case 9: Wipe boot partition:

from esp32 import Partition
boot = Partition.BOOT
boot.erase_region(0, boot.size)

The first example prints the size information of the boot partition (BOOT). The second example reads partial data from the boot partition. The third example wipes the entire boot partition. esp32.Partition.BOOT is used to obtain the boot partition object of ESP32. Based on it, you can obtain partition information and perform read and write operations on the boot partition. These examples demonstrate common uses for accessing the boot partition in scenarios such as firmware updates and troubleshooting.

Insert image description here

Guess you like

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