Skip to content

Commit 83613c8

Browse files
committed
refactor(stub_class): Make into a mixin to avoid code repetition
1 parent a32988e commit 83613c8

18 files changed

+103
-277
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ repos:
2525
additional_dependencies:
2626
- tomli
2727
- repo: https://github.com/espressif/conventional-precommit-linter
28-
rev: v1.9.0
28+
rev: v1.10.0
2929
hooks:
3030
- id: conventional-precommit-linter
3131
stages: [commit-msg]
32+
args:
33+
- --allow-breaking
3234
default_stages: [commit]
3335
default_install_hook_types: [pre-commit, commit-msg]

esptool/loader.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,26 @@ def watchdog_reset(self):
16141614
self.hard_reset()
16151615

16161616

1617+
class StubMixin:
1618+
"""
1619+
Mixin class bundling the stub loader-specific properties.
1620+
Not intended for direct instantiation.
1621+
A child class (e.g. ESP32StubLoader) uses multiple inheritance
1622+
to combine this mixin class (StubMixin) with a parent class (e.g. ESP32ROM).
1623+
"""
1624+
1625+
FLASH_WRITE_SIZE = 0x4000 # Default value, can be overridden
1626+
STATUS_BYTES_LENGTH = 2
1627+
IS_STUB = True
1628+
1629+
def __init__(self, rom_loader):
1630+
self.secure_download_mode = rom_loader.secure_download_mode
1631+
self._port = rom_loader._port
1632+
self._trace_enabled = rom_loader._trace_enabled
1633+
self.cache = rom_loader.cache
1634+
self.flush_input() # resets _slip_reader
1635+
1636+
16171637
def slip_reader(port, trace_function):
16181638
"""Generator to read SLIP packets from a serial port.
16191639
Yields one full SLIP packet at a time, raises exception on timeout or invalid data.

esptool/targets/esp32.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: 2014-2022 Fredrik Ahlberg, Angus Gratton,
1+
# SPDX-FileCopyrightText: 2025 Fredrik Ahlberg, Angus Gratton,
22
# Espressif Systems (Shanghai) CO LTD, other contributors as noted.
33
#
44
# SPDX-License-Identifier: GPL-2.0-or-later
@@ -7,7 +7,7 @@
77
import time
88
from typing import Dict, Optional
99

10-
from ..loader import ESPLoader
10+
from ..loader import ESPLoader, StubMixin
1111
from ..util import FatalError, NotSupportedError
1212

1313

@@ -452,19 +452,8 @@ def check_spi_connection(self, spi_connection):
452452
raise FatalError("SPI Pin numbers must be in the range 0-29, 32, or 33.")
453453

454454

455-
class ESP32StubLoader(ESP32ROM):
456-
"""Access class for ESP32 stub loader, runs on top of ROM."""
457-
458-
FLASH_WRITE_SIZE = 0x4000 # matches MAX_WRITE_BLOCK in stub_loader.c
459-
STATUS_BYTES_LENGTH = 2 # same as ESP8266, different to ESP32 ROM
460-
IS_STUB = True
461-
462-
def __init__(self, rom_loader):
463-
self.secure_download_mode = rom_loader.secure_download_mode
464-
self._port = rom_loader._port
465-
self._trace_enabled = rom_loader._trace_enabled
466-
self.cache = rom_loader.cache
467-
self.flush_input() # resets _slip_reader
455+
class ESP32StubLoader(StubMixin, ESP32ROM):
456+
"""Stub loader for ESP32, runs on top of ROM."""
468457

469458
def change_baud(self, baud):
470459
ESPLoader.change_baud(self, baud)

esptool/targets/esp32c2.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: 2014-2024 Fredrik Ahlberg, Angus Gratton,
1+
# SPDX-FileCopyrightText: 2014-2025 Fredrik Ahlberg, Angus Gratton,
22
# Espressif Systems (Shanghai) CO LTD, other contributors as noted.
33
#
44
# SPDX-License-Identifier: GPL-2.0-or-later
@@ -8,7 +8,7 @@
88
from typing import Dict
99

1010
from .esp32c3 import ESP32C3ROM
11-
from ..loader import ESPLoader
11+
from ..loader import ESPLoader, StubMixin
1212
from ..util import FatalError
1313

1414

@@ -166,23 +166,10 @@ def check_spi_connection(self, spi_connection):
166166
raise FatalError("SPI Pin numbers must be in the range 0-20.")
167167

168168

169-
class ESP32C2StubLoader(ESP32C2ROM):
170-
"""Access class for ESP32C2 stub loader, runs on top of ROM.
169+
class ESP32C2StubLoader(StubMixin, ESP32C2ROM):
170+
"""Stub loader for ESP32-C2, runs on top of ROM."""
171171

172-
(Basically the same as ESP32StubLoader, but different base class.
173-
Can possibly be made into a mixin.)
174-
"""
175-
176-
FLASH_WRITE_SIZE = 0x4000 # matches MAX_WRITE_BLOCK in stub_loader.c
177-
STATUS_BYTES_LENGTH = 2 # same as ESP8266, different to ESP32 ROM
178-
IS_STUB = True
179-
180-
def __init__(self, rom_loader):
181-
self.secure_download_mode = rom_loader.secure_download_mode
182-
self._port = rom_loader._port
183-
self._trace_enabled = rom_loader._trace_enabled
184-
self.cache = rom_loader.cache
185-
self.flush_input() # resets _slip_reader
172+
pass
186173

187174

188175
ESP32C2ROM.STUB_CLASS = ESP32C2StubLoader

esptool/targets/esp32c3.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: 2014-2024 Fredrik Ahlberg, Angus Gratton,
1+
# SPDX-FileCopyrightText: 2014-2025 Fredrik Ahlberg, Angus Gratton,
22
# Espressif Systems (Shanghai) CO LTD, other contributors as noted.
33
#
44
# SPDX-License-Identifier: GPL-2.0-or-later
@@ -8,7 +8,7 @@
88
from typing import Dict
99

1010
from .esp32 import ESP32ROM
11-
from ..loader import ESPLoader
11+
from ..loader import ESPLoader, StubMixin
1212
from ..util import FatalError, NotImplementedInROMError
1313

1414

@@ -273,23 +273,10 @@ def check_spi_connection(self, spi_connection):
273273
)
274274

275275

276-
class ESP32C3StubLoader(ESP32C3ROM):
277-
"""Access class for ESP32C3 stub loader, runs on top of ROM.
276+
class ESP32C3StubLoader(StubMixin, ESP32C3ROM):
277+
"""Stub loader for ESP32-C3, runs on top of ROM."""
278278

279-
(Basically the same as ESP32StubLoader, but different base class.
280-
Can possibly be made into a mixin.)
281-
"""
282-
283-
FLASH_WRITE_SIZE = 0x4000 # matches MAX_WRITE_BLOCK in stub_loader.c
284-
STATUS_BYTES_LENGTH = 2 # same as ESP8266, different to ESP32 ROM
285-
IS_STUB = True
286-
287-
def __init__(self, rom_loader):
288-
self.secure_download_mode = rom_loader.secure_download_mode
289-
self._port = rom_loader._port
290-
self._trace_enabled = rom_loader._trace_enabled
291-
self.cache = rom_loader.cache
292-
self.flush_input() # resets _slip_reader
279+
pass
293280

294281

295282
ESP32C3ROM.STUB_CLASS = ESP32C3StubLoader

esptool/targets/esp32c5.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
1+
# SPDX-FileCopyrightText: 2024-2025 Fredrik Ahlberg, Angus Gratton,
2+
# Espressif Systems (Shanghai) CO LTD, other contributors as noted.
23
#
34
# SPDX-License-Identifier: GPL-2.0-or-later
45

@@ -8,7 +9,7 @@
89

910
from .esp32c3 import ESP32C3ROM
1011
from .esp32c6 import ESP32C6ROM
11-
from ..loader import ESPLoader
12+
from ..loader import ESPLoader, StubMixin
1213
from ..util import FatalError
1314

1415

@@ -168,23 +169,10 @@ def watchdog_reset(self):
168169
ESP32C3ROM.watchdog_reset(self)
169170

170171

171-
class ESP32C5StubLoader(ESP32C5ROM):
172-
"""Access class for ESP32C5 stub loader, runs on top of ROM.
172+
class ESP32C5StubLoader(StubMixin, ESP32C5ROM):
173+
"""Stub loader for ESP32-C5, runs on top of ROM."""
173174

174-
(Basically the same as ESP32StubLoader, but different base class.
175-
Can possibly be made into a mixin.)
176-
"""
177-
178-
FLASH_WRITE_SIZE = 0x4000 # matches MAX_WRITE_BLOCK in stub_loader.c
179-
STATUS_BYTES_LENGTH = 2 # same as ESP8266, different to ESP32 ROM
180-
IS_STUB = True
181-
182-
def __init__(self, rom_loader):
183-
self.secure_download_mode = rom_loader.secure_download_mode
184-
self._port = rom_loader._port
185-
self._trace_enabled = rom_loader._trace_enabled
186-
self.cache = rom_loader.cache
187-
self.flush_input() # resets _slip_reader
175+
pass
188176

189177

190178
ESP32C5ROM.STUB_CLASS = ESP32C5StubLoader

esptool/targets/esp32c5beta3.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
1+
# SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
22
#
33
# SPDX-License-Identifier: GPL-2.0-or-later
44

@@ -7,7 +7,7 @@
77
from typing import Dict
88

99
from .esp32c6 import ESP32C6ROM
10-
from ..loader import ESPLoader
10+
from ..loader import ESPLoader, StubMixin
1111

1212

1313
class ESP32C5BETA3ROM(ESP32C6ROM):
@@ -104,23 +104,10 @@ def change_baud(self, baud):
104104
ESPLoader.change_baud(self, baud)
105105

106106

107-
class ESP32C5BETA3StubLoader(ESP32C5BETA3ROM):
108-
"""Access class for ESP32C5BETA3 stub loader, runs on top of ROM.
107+
class ESP32C5BETA3StubLoader(StubMixin, ESP32C5BETA3ROM):
108+
"""Stub loader for ESP32-C5(beta3), runs on top of ROM."""
109109

110-
(Basically the same as ESP32StubLoader, but different base class.
111-
Can possibly be made into a mixin.)
112-
"""
113-
114-
FLASH_WRITE_SIZE = 0x4000 # matches MAX_WRITE_BLOCK in stub_loader.c
115-
STATUS_BYTES_LENGTH = 2 # same as ESP8266, different to ESP32 ROM
116-
IS_STUB = True
117-
118-
def __init__(self, rom_loader):
119-
self.secure_download_mode = rom_loader.secure_download_mode
120-
self._port = rom_loader._port
121-
self._trace_enabled = rom_loader._trace_enabled
122-
self.cache = rom_loader.cache
123-
self.flush_input() # resets _slip_reader
110+
pass
124111

125112

126113
ESP32C5BETA3ROM.STUB_CLASS = ESP32C5BETA3StubLoader

esptool/targets/esp32c6.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# SPDX-FileCopyrightText: 2024 Fredrik Ahlberg, Angus Gratton,
1+
# SPDX-FileCopyrightText: 2024-2025 Fredrik Ahlberg, Angus Gratton,
22
# Espressif Systems (Shanghai) CO LTD, other contributors as noted.
33
#
44
# SPDX-License-Identifier: GPL-2.0-or-later
55

66
import struct
77

88
from .esp32c3 import ESP32C3ROM
9-
from ..loader import ESPLoader
9+
from ..loader import ESPLoader, StubMixin
1010
from ..util import FatalError, NotImplementedInROMError
1111

1212

@@ -198,23 +198,10 @@ def watchdog_reset(self):
198198
ESPLoader.watchdog_reset(self)
199199

200200

201-
class ESP32C6StubLoader(ESP32C6ROM):
202-
"""Access class for ESP32C6 stub loader, runs on top of ROM.
201+
class ESP32C6StubLoader(StubMixin, ESP32C6ROM):
202+
"""Stub loader for ESP32-C6, runs on top of ROM."""
203203

204-
(Basically the same as ESP32StubLoader, but different base class.
205-
Can possibly be made into a mixin.)
206-
"""
207-
208-
FLASH_WRITE_SIZE = 0x4000 # matches MAX_WRITE_BLOCK in stub_loader.c
209-
STATUS_BYTES_LENGTH = 2 # same as ESP8266, different to ESP32 ROM
210-
IS_STUB = True
211-
212-
def __init__(self, rom_loader):
213-
self.secure_download_mode = rom_loader.secure_download_mode
214-
self._port = rom_loader._port
215-
self._trace_enabled = rom_loader._trace_enabled
216-
self.cache = rom_loader.cache
217-
self.flush_input() # resets _slip_reader
204+
pass
218205

219206

220207
ESP32C6ROM.STUB_CLASS = ESP32C6StubLoader

esptool/targets/esp32c61.py

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
1+
# SPDX-FileCopyrightText: 2024-2025 Fredrik Ahlberg, Angus Gratton,
2+
# Espressif Systems (Shanghai) CO LTD, other contributors as noted.
23
#
34
# SPDX-License-Identifier: GPL-2.0-or-later
45

@@ -7,6 +8,7 @@
78

89
from .esp32c3 import ESP32C3ROM
910
from .esp32c6 import ESP32C6ROM
11+
from ..loader import StubMixin
1012

1113

1214
class ESP32C61ROM(ESP32C6ROM):
@@ -124,23 +126,10 @@ def watchdog_reset(self):
124126
ESP32C3ROM.watchdog_reset(self)
125127

126128

127-
class ESP32C61StubLoader(ESP32C61ROM):
128-
"""Access class for ESP32C61 stub loader, runs on top of ROM.
129+
class ESP32C61StubLoader(StubMixin, ESP32C61ROM):
130+
"""Stub loader for ESP32-C61, runs on top of ROM."""
129131

130-
(Basically the same as ESP32StubLoader, but different base class.
131-
Can possibly be made into a mixin.)
132-
"""
133-
134-
FLASH_WRITE_SIZE = 0x4000 # matches MAX_WRITE_BLOCK in stub_loader.c
135-
STATUS_BYTES_LENGTH = 2 # same as ESP8266, different to ESP32 ROM
136-
IS_STUB = True
137-
138-
def __init__(self, rom_loader):
139-
self.secure_download_mode = rom_loader.secure_download_mode
140-
self._port = rom_loader._port
141-
self._trace_enabled = rom_loader._trace_enabled
142-
self.cache = rom_loader.cache
143-
self.flush_input() # resets _slip_reader
132+
pass
144133

145134

146135
ESP32C61ROM.STUB_CLASS = ESP32C61StubLoader

esptool/targets/esp32h2.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# SPDX-FileCopyrightText: 2024 Fredrik Ahlberg, Angus Gratton,
1+
# SPDX-FileCopyrightText: 2024-2025 Fredrik Ahlberg, Angus Gratton,
22
# Espressif Systems (Shanghai) CO LTD, other contributors as noted.
33
#
44
# SPDX-License-Identifier: GPL-2.0-or-later
55

66
from typing import Dict
77

88
from .esp32c6 import ESP32C6ROM
9-
from ..loader import ESPLoader
9+
from ..loader import ESPLoader, StubMixin
1010
from ..util import FatalError
1111

1212

@@ -93,23 +93,10 @@ def check_spi_connection(self, spi_connection):
9393
)
9494

9595

96-
class ESP32H2StubLoader(ESP32H2ROM):
97-
"""Access class for ESP32H2 stub loader, runs on top of ROM.
96+
class ESP32H2StubLoader(StubMixin, ESP32H2ROM):
97+
"""Stub loader for ESP32-H2, runs on top of ROM."""
9898

99-
(Basically the same as ESP32StubLoader, but different base class.
100-
Can possibly be made into a mixin.)
101-
"""
102-
103-
FLASH_WRITE_SIZE = 0x4000 # matches MAX_WRITE_BLOCK in stub_loader.c
104-
STATUS_BYTES_LENGTH = 2 # same as ESP8266, different to ESP32 ROM
105-
IS_STUB = True
106-
107-
def __init__(self, rom_loader):
108-
self.secure_download_mode = rom_loader.secure_download_mode
109-
self._port = rom_loader._port
110-
self._trace_enabled = rom_loader._trace_enabled
111-
self.cache = rom_loader.cache
112-
self.flush_input() # resets _slip_reader
99+
pass
113100

114101

115102
ESP32H2ROM.STUB_CLASS = ESP32H2StubLoader

0 commit comments

Comments
 (0)