-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added example of flashing from python code (ESPTOOL-952) #1029
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
Flashing from your python code | ||
============================== | ||
|
||
The following is an example on how to flash the ESP32 from a custom application: | ||
|
||
``` | ||
############################################################# | ||
# Example code functionally equivalent to | ||
# esptool.py -p /dev/ttyACM0 write_flash 0x10000 firmware.bin | ||
############################################################# | ||
from esptool.cmds import detect_chip | ||
|
||
# the port of the connected ESP32 | ||
port = "/dev/ttyACM0" | ||
|
||
# The firmware file | ||
filename = "./firmware.bin" | ||
|
||
# Typical block size (16 KB) | ||
BLOCK_SIZE = 0x4000 | ||
|
||
#beginning of flash address | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please keep all the comments formatted uniformly. |
||
FLASH_BEGIN = 0x10000 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggest renaming to |
||
|
||
def progress_callback(percent): | ||
print(f"percent {int(percent)}") | ||
|
||
with detect_chip(port) as esp: | ||
chip_desc = esp.get_chip_description() | ||
features = esp.get_chip_features() | ||
print(f"Detected bootloader on port {port} : {chip_desc}") | ||
print(f"Features {features}") | ||
|
||
stub = esp.run_stub() | ||
with open(filename, 'rb') as firmware: | ||
firmware_data = firmware.read() | ||
print(f"firmware length {len(firmware_data)}") | ||
total_size = len(firmware_data) | ||
stub.flash_begin(total_size, FLASH_BEGIN) | ||
|
||
# Flash in blocks using flash_block | ||
block_size = BLOCK_SIZE | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is called |
||
for i in range(0, total_size, block_size): | ||
block = firmware_data[i:i + block_size] | ||
# pad the last block | ||
block = block + bytes([0xFF]) * (BLOCK_SIZE - len(block)) | ||
stub.flash_block(block, i + FLASH_BEGIN) | ||
progress_callback(float(i + len(block)) / total_size * 100) | ||
stub.flash_finish() | ||
|
||
stub.hard_reset() | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing
ESP32
to{IDF_TARGET_NAME}
will make it dynamically change based on the selected target.