You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/esptool/basic-commands.rst
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -101,7 +101,7 @@ Use the ``-e/--erase-all`` option to erase all flash sectors (not just the write
101
101
Read Flash Contents: ``read_flash``
102
102
-----------------------------------
103
103
104
-
The read_flash command allows reading back the contents of flash. The arguments to the command are an address, a size, and a file path to output to. For example, to read a full 2MB of attached flash:
104
+
The read_flash command allows reading back the contents of flash. The arguments to the command are an address, a size, and a filename to dump the output to. For example, to read a full 2MB of attached flash:
``esptool.py`` can be easily integrated into Python applications or called from other Python scripts.
6
+
``esptool.py``, ``espefuse.py``, and ``espsecure.py`` can easily be integrated into Python applications or called from other Python scripts.
7
7
8
-
Using Esptool as a Python Module
9
-
--------------------------------
10
-
11
-
The esptool module provides a comprehensive Python API for interacting with ESP chips programmatically. By leveraging the API, developers can automate tasks such as flashing firmware, reading device information, managing flash memory, or preparing and analyzing binary images. The API supports both high-level abstractions and low-level control.
12
-
13
-
Using the Command-Line Interface
14
-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15
-
16
-
The most straightforward and basic integration option is to pass arguments to ``esptool.main()``. This workaround allows you to pass exactly the same arguments as you would on the CLI:
8
+
While it currently does have a poor Python API, something which `#208 <https://github.com/espressif/esptool/issues/208>`_ will address, it allows for passing CLI arguments to ``esptool.main()``. This workaround makes integration very straightforward as you can pass exactly the same arguments as you would on the CLI:
17
9
18
10
.. code-block:: python
19
11
@@ -23,182 +15,16 @@ The most straightforward and basic integration option is to pass arguments to ``
23
15
print("Using command ", "".join(command))
24
16
esptool.main(command)
25
17
26
-
Public API Reference
27
-
^^^^^^^^^^^^^^^^^^^^
28
-
29
-
For more control and custom integration, esptool exposes a public API - a set of high-level functions that encapsulate common operations and simplify the interaction with the ESP chip. These functions are designed to be user-friendly and provide an intuitive way to work with the chip. The public API is the recommended way to interact with the chip programmatically.
30
-
31
-
Basic Workflow:
32
-
33
-
1. **Detect and Connect**: Use ``detect_chip()`` to automatically identify the connected ESP chip and establish a connection, or manually create and instantiate a specific ``ESPLoader`` object (e.g. ``ESP32ROM``) and establish a connection in two steps.
34
-
2. **Run Stub Flasher (Optional)**: Upload and execute the :ref:`stub flasher <stub>` which provides enhanced functionality and speed.
35
-
3. **Perform Operations**: Utilize the chip object's methods or public API command functions to interact with the device.
36
-
4. **Reset and Cleanup**: Ensure proper reset and resource cleanup using context managers.
37
-
38
-
------------
39
-
40
-
This example demonstrates writing two binary files using high-level commands:
41
-
42
-
.. code-block:: python
43
-
44
-
from esptool.cmds import detect_chip, attach_flash, reset_chip, write_flash
45
-
46
-
PORT="/dev/ttyACM0"
47
-
BOOTLOADER="bootloader.bin"
48
-
FIRMWARE="firmware.bin"
49
-
50
-
with detect_chip(PORT) as esp:
51
-
esp = esp.run_stub() # Skip this line to avoid running the stub flasher
52
-
attach_flash(esp) # Attach the flash memory chip, required for flash operations
53
-
withopen(BOOTLOADER, "rb") as bl_file, open(FIRMWARE, "rb") as fw_file:
54
-
write_flash(esp, [(0, bl_file), (0x1000, fw_file)]) # Write the binary files
55
-
reset_chip(esp, "hard_reset") # Reset the chip
56
-
57
-
- The ``esp`` object has to be replaced with the stub flasher object returned by ``esp.run_stub()`` when the stub flasher is activated. This step can be skipped if the stub flasher is not needed.
58
-
- Running ``attach_flash(esp)`` is required for any flash-memory-related operations to work.
59
-
- Using the ``esp`` object in a context manager ensures the port gets closed properly after the block is executed.
60
-
61
-
------------
62
-
63
-
The following example demonstrates running a series of flash memory operations in one go:
64
-
65
-
.. code-block:: python
66
-
67
-
from esptool.cmds import (
68
-
erase_flash,
69
-
attach_flash,
70
-
flash_id,
71
-
read_flash,
72
-
reset_chip,
73
-
verify_flash,
74
-
write_flash,
75
-
)
76
-
from esptool.targets importESP32ROM# Import the target class, e.g. ESP8266ROM, ESP32S3ROM, etc.
77
-
78
-
PORT="/dev/ttyACM0"
79
-
BOOTLOADER="bootloader.bin"
80
-
FIRMWARE="firmware.bin"
81
-
82
-
with ESP32ROM(PORT) as esp:
83
-
esp.connect() # Connect to the ESP chip, needed when ESP32ROM is instantiated directly
84
-
esp = esp.run_stub() # Run the stub loader (optional)
85
-
attach_flash(esp) # Attach the flash memory chip, required for flash operations
86
-
flash_id(esp) # Print information about the flash chip
87
-
erase_flash(esp) # Erase the flash memory first
88
-
withopen(BOOTLOADER, "rb") as bl_file, open(FIRMWARE, "rb") as fw_file:
89
-
write_flash(esp, [(0, bl_file), (0x1000, fw_file)]) # Write the binary files
90
-
verify_flash(esp, [(0, bl_file), (0x1000, fw_file)]) # Verify the written data
91
-
read_flash(esp, 0x0, 0x2400, "output.bin") # Read the flash memory into a file
92
-
reset_chip(esp, "hard_reset") # Reset the chip
93
-
94
-
- This example doesn't use ``detect_chip()``, but instantiates a ``ESP32ROM`` class directly. This is useful when you know the target chip in advance. In this scenario ``esp.connect()`` is required to establish a connection with the device.
95
-
- Multiple operations can be chained together in a single context manager block.
96
-
97
-
------------
98
-
99
-
**The following section provides a detailed reference for the public API functions.**
100
-
101
-
Chip Control Operations
102
-
"""""""""""""""""""""""
103
-
104
-
.. autofunction:: esptool.cmds.detect_chip
105
-
106
-
.. autofunction:: esptool.cmds.load_ram
107
-
108
-
.. autofunction:: esptool.cmds.run
109
-
110
-
.. autofunction:: esptool.cmds.reset_chip
111
-
112
-
------------
113
-
114
-
Chip Information Operations
115
-
"""""""""""""""""""""""""""
116
-
117
-
.. autofunction:: esptool.cmds.chip_id
118
-
119
-
.. autofunction:: esptool.cmds.get_security_info
120
-
121
-
.. autofunction:: esptool.cmds.read_mac
122
18
123
-
------------
124
-
125
-
Flash Memory Manipulation Operations
126
-
""""""""""""""""""""""""""""""""""""
127
-
128
-
.. autofunction:: esptool.cmds.attach_flash
129
-
130
-
.. autofunction:: esptool.cmds.flash_id
131
-
132
-
.. autofunction:: esptool.cmds.read_flash
133
-
134
-
.. autofunction:: esptool.cmds.write_flash
135
-
136
-
.. autofunction:: esptool.cmds.erase_flash
137
-
138
-
.. autofunction:: esptool.cmds.erase_region
139
-
140
-
.. autofunction:: esptool.cmds.verify_flash
141
-
142
-
.. autofunction:: esptool.cmds.read_flash_status
143
-
144
-
.. autofunction:: esptool.cmds.write_flash_status
145
-
146
-
.. autofunction:: esptool.cmds.read_flash_sfdp
147
-
148
-
------------
149
-
150
-
Memory Operations
151
-
"""""""""""""""""
152
-
153
-
.. autofunction:: esptool.cmds.read_mem
154
-
155
-
.. autofunction:: esptool.cmds.write_mem
156
-
157
-
.. autofunction:: esptool.cmds.dump_mem
158
-
159
-
------------
160
-
161
-
Binary Image Manipulation Operations
162
-
""""""""""""""""""""""""""""""""""""
163
-
164
-
The following commands can run without the need for a connected chip:
165
-
166
-
.. autofunction:: esptool.cmds.elf2image
167
-
168
-
.. autofunction:: esptool.cmds.merge_bin
169
-
170
-
.. autofunction:: esptool.cmds.image_info
171
-
172
-
.. autofunction:: esptool.cmds.make_image
173
-
174
-
------------
175
-
176
-
Utility Functions
177
-
"""""""""""""""""
178
-
179
-
.. autofunction:: esptool.cmds.version
180
-
181
-
------------
182
-
183
-
For more information, refer to the command implementations in `esptool/cmds.py <https://github.com/espressif/esptool/blob/master/esptool/cmds.py>`_.
184
-
185
-
186
-
Low-Level API Reference
187
-
^^^^^^^^^^^^^^^^^^^^^^^
188
-
189
-
.. warning::
190
-
191
-
The low-level API provides more control but requires a deeper understanding of the ESP chip, the esptool internals, and the :ref:`serial protocol <serial-protocol>`. It is recommended to use the public API functions for most use cases.
192
-
193
-
Also, the low-level internals are not a part of the public API, so they may change in between releases.
194
-
195
-
Please submit a :ref:`feature request <feature-requests>` if you are missing something from the officially supported API.
19
+
Using Esptool as a Python Module
20
+
--------------------------------
196
21
197
-
For granular control and more configuration freedom, you can directly access the low-level methods and attributes of the ``ESPLoader`` object and create your own routines. The following is an example of a custom routine to flash the {IDF_TARGET_NAME}:
22
+
The following is an example on how to use esptool as a Python module and leverage its Python API to flash the {IDF_TARGET_NAME}:
198
23
199
24
.. note::
200
25
201
-
This example code is a very basic implementation of ``esptool.py -p /dev/ttyACM0 write_flash 0x10000 firmware.bin``
26
+
This example code functionally equivalent to ``esptool.py -p /dev/ttyACM0 write_flash 0x10000 firmware.bin``
27
+
202
28
203
29
.. code-block:: python
204
30
@@ -218,7 +44,7 @@ For granular control and more configuration freedom, you can directly access the
218
44
description = esp.get_chip_description()
219
45
features = esp.get_chip_features()
220
46
print(f"Detected ESP on port {PORT}: {description}")
221
-
print("Features:", ", ".join(features))
47
+
print(f"Features:{", ".join(features)}")
222
48
223
49
esp = esp.run_stub()
224
50
withopen(BIN_FILE, 'rb') as binary:
@@ -234,15 +60,12 @@ For granular control and more configuration freedom, you can directly access the
For more information, refer to the methods of the ``ESPLoader`` class in `esptool/loader.py <https://github.com/espressif/esptool/blob/master/esptool/loader.py>`_.
246
69
247
70
.. _logging:
248
71
@@ -254,7 +77,6 @@ Esptool allows redirecting output by implementing a custom logger class. This ca
254
77
.. code-block:: python
255
78
256
79
from esptool.logger import log, TemplateLogger
257
-
import sys
258
80
259
81
classCustomLogger(TemplateLogger):
260
82
log_to_file =True
@@ -277,7 +99,7 @@ Esptool allows redirecting output by implementing a custom logger class. This ca
@@ -302,8 +124,4 @@ To ensure compatibility with esptool, the custom logger should re-implement (or
302
124
- ``print_overwrite``: Handles message overwriting (can be a simple ``print()`` if overwriting is not needed).
303
125
- ``set_progress``: Handles percentage updates of long-running operations - ``write_flash``, ``read_flash``, and ``dump_mem`` (useful for GUI visualisation, e.g. as a progress bar).
These methods are essential for maintaining proper integration and behavior with esptool. Additionally, all calls to the logger should be made using ``log.print()`` (or the respective method, such as ``log.info()`` or ``log.warning()``) instead of the standard ``print()`` function to ensure the output is routed through the custom logger. This ensures consistency and allows the custom logger to handle all output appropriately. You can further customize this logger to fit your application's needs, such as integrating with GUI components or advanced logging frameworks.
Copy file name to clipboardExpand all lines: docs/en/index.rst
-2Lines changed: 0 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -12,8 +12,6 @@ The flasher stub is a small program included with esptool that replaces the orig
12
12
* Prepare binary executable images ready for flashing.
13
13
* Analyze, assemble, and merge binary images.
14
14
15
-
``esptool.py`` can be used both as a command-line tool and as a Python library. The command-line is the most common way to use the tool, and is the primary focus of this documentation. To use it as a library, see the :ref:`scripting <scripting>` section.
16
-
17
15
This document describes using ``esptool.py`` with the {IDF_TARGET_NAME} SoC. To switch to a different SoC target, choose target from the dropdown in the upper left.
0 commit comments