[Diao Ye Learns Programming] MicroPython Manual ESP32 Specific Port Library esp32.Partition.set_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.set_boot() is the method in MicroPython used to set the ESP32 partition to boot. It allows you to specify a partition as the boot partition, thereby changing the boot behavior of the device. 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:

The esp32.Partition.set_boot() method is used to set the boot partition of the ESP32 device.
You can change the device's boot behavior by marking a specific partition as a boot partition.
The index value of the partition (starting from 0) is used to identify the partition to be set as the boot partition.

Application scenarios:

The esp32.Partition.set_boot() method is suitable for scenarios where the startup behavior of the ESP32 device needs to be changed.
You can use this method to select one of multiple partitions as the boot partition to switch boot code or firmware in different application scenarios.
This method can also be used during the firmware update process to write the new firmware to a partition and set it as the partition for the next boot.

Things to note:

Before using the esp32.Partition.set_boot() method, you need to import the esp32 module first.
When setting up a boot partition, make sure to provide the correct partition index to avoid selecting the wrong partition as the boot partition.
Modifying the boot partition may cause changes in device behavior. Please back up important data before doing this and make sure you understand the impact.

Here are a few practical application examples:

Case 1: Set partition 0 as the boot partition:

import esp32

# 将分区0设置为启动分区
esp32.Partition.set_boot(0)

print("分区0已设置为启动分区.")

Case 2: Switch boot partition:

import esp32

# 将分区1设置为启动分区
esp32.Partition.set_boot(1)

print("分区1已设置为启动分区.")

Case 3: Switch boot partition after firmware update:

import esp32

# 写入新固件到分区1
# ...

# 将分区1设置为启动分区
esp32.Partition.set_boot(1)

print("新固件已写入分区1,并设置为启动分区.")

Note that the partitioned index in the above example may need to be adjusted based on the actual partition configuration. You can select the appropriate partition index to operate based on the partition layout and needs on the ESP32. Also, ensure that when using the esp32.Partition.set_boot() method, you provide the correct partition index and do so with an understanding of its impact.

Case 4: Switch boot partition:

import machine

# 设置要作为启动分区的分区号
boot_partition = 1

# 设置分区为启动分区
machine.Partition.set_boot(boot_partition)

# 重启设备
machine.reset()

In this example, we use the esp32.Partition.set_boot() method to switch the boot partition. We first set a variable boot_partition to specify the partition number to be used as the boot partition. Then, we use the set_boot() method to set the specified partition as the boot partition. Finally, we restart the device using the machine.reset() method for the changes to take effect.

Case 5: Backup boot partition::

import machine

# 设置要备份的分区号
source_partition = 0

# 设置备份的目标分区号
target_partition = 1

# 备份启动分区
machine.Partition.set_boot(source_partition, target_partition)

# 重启设备
machine.reset()

In this example, we use the esp32.Partition.set_boot() method to back up the boot partition. We first set two variables source_partition and target_partition to specify the partition number to be backed up and the target partition number to be backed up respectively. Then, we use the set_boot() method to copy the contents of the source partition to the target partition to complete the backup. Finally, we restart the device using the machine.reset() method for the changes to take effect.

Case 6: Restore default boot partition::

import machine

# 恢复为默认启动分区
machine.Partition.set_boot()

# 重启设备
machine.reset()

In this example, we use the esp32.Partition.set_boot() method to restore the default boot partition. We do not pass any parameters to the set_boot() method, which will revert the device to the default boot partition settings. We then use the machine.reset() method to reboot the device for the changes to take effect.

Case 7: Set the default boot partition:

import esp32

partition = esp32.Partition(esp32.Partition.RUNNING)
partition.set_boot()

In this example, we use the esp32.Partition() class to get the currently running partition object. Then, by calling the set_boot() method of the partition object, we set the current partition as the default boot partition. This means the ESP32 will load code from this partition the next time it boots.

Case 8: Switch boot partition::

import esp32

partition = esp32.Partition(esp32.Partition.SUBTYPE_APP_1)
partition.set_boot()

In this example, we use the esp32.Partition() class to get a partition object of a specific subtype. In the example, we use esp32.Partition.SUBTYPE_APP_1 to get the object of application partition 1. We set application partition 1 as the boot partition by calling the set_boot() method of the partition object. This can be used to implement OTA (over-the-air firmware upgrade) or dual-system functionality by switching boot partitions to load different versions of firmware.

Case 9: Set up a protected boot partition::

import esp32

partition = esp32.Partition(esp32.Partition.RUNNING)
partition.set_boot(protect=True)

In this example, we use the esp32.Partition() class to get the currently running partition object. By calling the set_boot() method of the partition object and passing protect=True as the parameter, we set the current partition as a protected boot partition. This means that the partition will be protected from accidental writes to it. This can be used to protect boot code or critical system partitions from unauthorized changes.

These examples show the esp32.Partition.set_boot() method in action. It can help you set the default boot partition, switch the boot partition to achieve OTA or dual-system functionality, and set up a protected boot partition to prevent unauthorized changes. When using the set_boot() method, please note the following:

Please choose your boot partition carefully, make sure you select the correct partition and back up important data.
When switching boot partitions, make sure there is valid firmware in the target partition to load.
When setting up a protected boot partition, make sure you understand its implications and carefully consider write access restrictions on the partition.

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/132921274