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

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.mark_app_valid_cancel_rollback() is a method in MicroPython used to mark an application as valid and cancel rollback. It allows you to mark an application as valid, preventing rollback to a previous application version. 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.mark_app_valid_cancel_rollback() method is used to mark the application as valid and cancel the rollback.
By calling this method, you mark the current application as valid, preventing the device from rolling back to a previous application version.
Once an app is marked as active, the device will remain on the current app version and will not fall back to a previous version.

Application scenarios:

The esp32.Partition.mark_app_valid_cancel_rollback() method is suitable for scenarios where you need to prevent rollback and maintain the current application version.
After an application update, you can use this method to mark the new version of the application as valid to prevent the device from rolling back to the older version.
This method can also be used to mark a new version of an application as valid after the device has verified it, ensuring that the device continues to run the updated application.

Things to note:

Before using the esp32.Partition.mark_app_valid_cancel_rollback() method, you need to import the esp32 module first.
Make sure to call this method after the application update is complete to avoid marking an unfinished application as valid.
Once you mark an application as valid, you cannot roll back to a previous application version, please ensure you perform appropriate testing and validation before performing marking operations.

Here are a few practical application examples:

Case 1: Mark the current application as valid and cancel the rollback:
:

import esp32

# 标记当前应用程序为有效并取消回滚
esp32.Partition.mark_app_valid_cancel_rollback()

print("当前应用程序已标记为有效,并取消了回滚.")

Case 2: Application marked as valid after update:

import esp32

# 完成应用程序更新
# ...

# 标记新版本的应用程序为有效
esp32.Partition.mark_app_valid_cancel_rollback()

print("新版本的应用程序已标记为有效.")

Case 3: Mark valid after verifying the application:

import esp32

# 验证新版本的应用程序
# ...

# 标记新版本的应用程序为有效
esp32.Partition.mark_app_valid_cancel_rollback()

print("新版本的应用程序已验证并标记为有效.")

Please note that the marking operation in the above example should be called at the appropriate time, and the corresponding application update and verification operations should be performed according to actual needs. Make sure that when using the esp32.Partition.mark_app_valid_cancel_rollback() method, you understand its impact and call it at the appropriate point in time.

Case 4: Verify new firmware:

from machine import Partition
part = Partition.find_partition('/')
part.mark_app_valid_cancel_rollback()

Case 5: Undo rollback:

from machine import Partition
part = Partition.find_partition('/')
part.mark_app_valid_cancel_rollback()
reset()

Case 6: Rollback firmware:

from machine import Partition, reset
part = Partition.find_partition('/')
part.erase_region(0, part.size())
reset()

The first example verifies that the new firmware takes effect immediately. The second example is to undo the rollback after installing new firmware. The third example simulates a rollback to the old firmware. This method is used for rollback management of OTA firmware updates to ensure the consistency of the firmware before and after installation. It can achieve reliable firmware upgrades in conjunction with downloading, erasing and other methods.

Case 7: Cancel the rollback and mark the application as valid:

import machine

# 打开分区
partition = machine.Partition('app')

# 取消回滚并标记应用为有效
partition.mark_app_valid_cancel_rollback()

# 重启设备
machine.reset()

In this example, we use the esp32.Partition.mark_app_valid_cancel_rollback() method to cancel the rollback and mark the application as valid. We first open a partition named 'app' using the machine.Partition() method. We then use the mark_app_valid_cancel_rollback() method to cancel any rollback that may be in progress and mark the app as valid. Finally, we restart the device using the machine.reset() method for the changes to take effect.

Case 8: Cancel the rollback and mark the application as valid, and clear the rollback counter::

import machine

# 打开分区
partition = machine.Partition('app')

# 取消回滚并标记应用为有效,并清除回滚计数器
partition.mark_app_valid_cancel_rollback(clear_rollback_counter=True)

# 重启设备
machine.reset()

In this example, we use the esp32.Partition.mark_app_valid_cancel_rollback() method to cancel the rollback and mark the app as valid, and clear the rollback counter. We first open a partition named 'app' using the machine.Partition() method. We then use the mark_app_valid_cancel_rollback() method to cancel any rollback that may be in progress and mark the app as valid. We also clear the rollback counter by setting the parameter clear_rollback_counter to True. Finally, we restart the device using the machine.reset() method for the changes to take effect.

Case 9: Cancel the rollback and mark the application as valid, and output the rollback counter::

import machine

# 打开分区
partition = machine.Partition('app')

# 取消回滚并标记应用为有效,并获取回滚计数器的值
rollback_counter = partition.mark_app_valid_cancel_rollback()

# 输出回滚计数器的值
print("回滚计数器:", rollback_counter)

# 重启设备
machine.reset()

In this example, we use the esp32.Partition.mark_app_valid_cancel_rollback() method to cancel the rollback and mark the application as valid, and get the value of the rollback counter. We first open a partition named 'app' using the machine.Partition() method. We then use the mark_app_valid_cancel_rollback() method to cancel any rollback that may be in progress and mark the app as valid. At the same time, we store the value of the returned rollback counter in the rollback_counter variable. Finally, we use the print() function to output the value of the rollback counter, and use the machine.reset() method to restart the device for the changes to take effect.

Insert image description here

Guess you like

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