Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions docs/en/advanced-topics/flashing-from-python-code.rst
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:
Copy link
Collaborator

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.


```
#############################################################
# 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep all the comments formatted uniformly.

FLASH_BEGIN = 0x10000
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest renaming to FLASH_ADDRESS or FLASH_OFFSET


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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is called FLASH_WRITE_SIZE in esptool and changes depending on if that's the ROM bootloader or the stub.
So in this case block_size = stub.FLASH_WRITE_SIZE seems better.

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()
```
1 change: 1 addition & 0 deletions docs/en/advanced-topics/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ This sections contains advanced topics and technical documentation useful if you
Serial Protocol <serial-protocol>
SPI Flash Modes <spi-flash-modes>
Boot Mode Selection <boot-mode-selection>
Flashing from your python code <flashing-from-python-code>