Repository navigation
Add esptool backend the ESP32-C3, switch to XIP flash layout
#149
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
Open
lschuermann
wants to merge
2
commits into
master
Choose a base branch
from
dev/esp32-c3-esptool
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,237 @@ | ||
| """ | ||
| Interface for Espressif boards using esptool. | ||
|
|
||
| Espressif chips have a serial bootloader in their mask ROM. Tockloader uses | ||
| the Python API of esptool to access the external SPI flash of the chip through | ||
| this bootloader. The chip stays in its ROM bootloader while Tockloader reads | ||
| and writes flash, and is reset into the Tock kernel afterwards. | ||
| """ | ||
|
|
||
| import logging | ||
|
|
||
| import serial.tools.list_ports | ||
|
|
||
| from .board_interface import BoardInterface | ||
| from .exceptions import TockLoaderException | ||
|
|
||
| # USB VID:PID pairs that indicate an attached Espressif chip. | ||
| # | ||
| # Espressif's own VID is used by the USB Serial/JTAG controller integrated into | ||
| # newer chips. The other entries are USB-UART bridges used on Espressif's | ||
| # development kits. Many unrelated boards use these bridges as well, so we only | ||
| # select such a port after esptool has confirmed that a supported chip is | ||
| # attached to it. | ||
| ESPRESSIF_USB_VID = 0x303A | ||
| USB_UART_BRIDGES = [ | ||
| (0x10C4, 0xEA60), # Silicon Labs CP210x (e.g., ESP32-C3-DevKitM-1) | ||
| (0x1A86, 0x55D3), # WCH CH343 (e.g., ESP32-C3-DevKitC-02 v1.1) | ||
| (0x1A86, 0x7523), # WCH CH340 | ||
| ] | ||
|
|
||
| # Mapping from esptool-reported chip to Tock board name. | ||
| DEFAULT_BOARD_FOR_CHIP = { | ||
| "ESP32-C3": "esp32-c3-devkitm-1", | ||
| } | ||
|
|
||
|
|
||
| class EspTool(BoardInterface): | ||
| def __init__(self, args): | ||
| # Must call the generic init first. | ||
| super().__init__(args) | ||
|
|
||
| self.port = getattr(self.args, "port", None) | ||
|
|
||
| # The `ESPLoader` connection to the chip's ROM bootloader. `None` while | ||
| # the chip is not in its ROM bootloader. | ||
| self.esp = None | ||
|
|
||
| def _import_esptool(self): | ||
| """ | ||
| Import esptool lazily, such that it is only required for users of this | ||
| channel. | ||
| """ | ||
| try: | ||
| import esptool.cmds | ||
| from esptool.logger import log | ||
| except ImportError: | ||
| raise TockLoaderException( | ||
| "Tockloader esptool support requires esptool >= 5.0. " | ||
| "Install it with `pip install esptool`." | ||
| ) | ||
|
|
||
| # esptool prints to stdout by default. Only show its output when | ||
| # tockloader is running in debug mode. | ||
| log.set_verbosity("verbose" if self.args.debug else "silent") | ||
|
|
||
| return esptool.cmds | ||
|
|
||
| def _candidate_ports(self): | ||
| """ | ||
| Return the serial ports that an Espressif chip may be attached to. | ||
| """ | ||
| if self.port is not None: | ||
| return [self.port] | ||
|
|
||
| ports = [] | ||
| for port in serial.tools.list_ports.comports(): | ||
| if ( | ||
| port.vid == ESPRESSIF_USB_VID | ||
| or (port.vid, port.pid) in USB_UART_BRIDGES | ||
| ): | ||
| ports.append(port.device) | ||
| return ports | ||
|
|
||
| def _connect(self, port): | ||
| """ | ||
| Reset the chip attached to `port` into its ROM bootloader, and return an | ||
| `ESPLoader` with the stub flasher running. | ||
| """ | ||
| cmds = self._import_esptool() | ||
| logging.debug('Connecting to Espressif chip on "{}".'.format(port)) | ||
| esp = cmds.detect_chip(port=port, connect_attempts=1) | ||
| logging.debug("Detected {}.".format(esp.CHIP_NAME)) | ||
| esp = cmds.run_stub(esp) | ||
| cmds.attach_flash(esp) | ||
| return esp | ||
|
|
||
| def _disconnect(self): | ||
| """ | ||
| Reset the chip into the Tock kernel and close the serial port. | ||
| """ | ||
| if self.esp is None: | ||
| return | ||
|
|
||
| cmds = self._import_esptool() | ||
| try: | ||
| cmds.reset_chip(self.esp, "hard-reset") | ||
| finally: | ||
| self.esp._port.close() | ||
| self.esp = None | ||
|
|
||
| def _expected_chip(self): | ||
| """ | ||
| Return the chip we expect esptool to report for the selected board, or | ||
| `None` if the board does not specify one. | ||
| """ | ||
| if self.board and self.board in self.KNOWN_BOARDS: | ||
| return self.KNOWN_BOARDS[self.board].get("esptool", {}).get("chip") | ||
| return None | ||
|
|
||
| def attached_board_exists(self): | ||
| # Probe each candidate port with esptool. This resets the attached chip, | ||
| # so we keep the connection open for `open_link_to_board()`. | ||
| try: | ||
| self._import_esptool() | ||
| except TockLoaderException: | ||
| return False | ||
|
|
||
| for port in self._candidate_ports(): | ||
| try: | ||
| esp = self._connect(port) | ||
| except Exception as e: | ||
| logging.debug('No Espressif chip on "{}": {}'.format(port, e)) | ||
| continue | ||
|
|
||
| if esp.CHIP_NAME in DEFAULT_BOARD_FOR_CHIP: | ||
| self.port = port | ||
| self.esp = esp | ||
| return True | ||
|
|
||
| esp._port.close() | ||
|
|
||
| return False | ||
|
|
||
| def open_link_to_board(self): | ||
| if self.port is None: | ||
| ports = self._candidate_ports() | ||
| if len(ports) == 0: | ||
| raise TockLoaderException( | ||
| "No Espressif serial port found. Pass --port to select one." | ||
| ) | ||
| if len(ports) > 1: | ||
| raise TockLoaderException( | ||
| "Multiple candidate serial ports found ({}). Pass --port to " | ||
| "select one.".format(", ".join(ports)) | ||
| ) | ||
| self.port = ports[0] | ||
|
|
||
| self.enter_bootloader_mode() | ||
|
|
||
| # The chip does not tell us which board it is on. If the user did not | ||
| # specify the board, assume the default board for this chip. | ||
| if self.board is None: | ||
| self.board = DEFAULT_BOARD_FOR_CHIP.get(self.esp.CHIP_NAME) | ||
|
|
||
| if self.board and self.board in self.KNOWN_BOARDS: | ||
| logging.info('Using settings from KNOWN_BOARDS["{}"]'.format(self.board)) | ||
| self._configure_from_known_boards() | ||
|
|
||
| expected_chip = self._expected_chip() | ||
| if expected_chip is not None and expected_chip != self.esp.CHIP_NAME: | ||
| self._disconnect() | ||
| raise TockLoaderException( | ||
| 'Board "{}" requires an {}, but esptool detected an {}.'.format( | ||
| self.board, expected_chip, self.esp.CHIP_NAME | ||
| ) | ||
| ) | ||
|
|
||
| def enter_bootloader_mode(self): | ||
| if self.esp is None: | ||
| self.esp = self._connect(self.port) | ||
|
|
||
| def exit_bootloader_mode(self): | ||
| self._disconnect() | ||
|
|
||
| def translate_address(self, address): | ||
| # esptool addresses the external flash by offset. Tock boards link | ||
| # against the address where the chip maps the flash. | ||
| flash_address = self.get_flash_address() or 0 | ||
| if address < flash_address: | ||
| raise TockLoaderException( | ||
| "Address {:#x} is not contained in flash".format(address) | ||
| ) | ||
| return address - flash_address | ||
|
|
||
| def flash_binary(self, address, binary, pad=False): | ||
| # esptool erases whole flash sectors before writing them. Extend the | ||
| # write to sector boundaries with the current flash contents, such that | ||
| # data next to `binary` is preserved. | ||
| address, binary = self._align_and_stretch_to_page(address, binary) | ||
|
|
||
| cmds = self._import_esptool() | ||
| cmds.write_flash( | ||
| self.esp, | ||
| # esptool accepts `bytes`, but not `bytearray`. | ||
| [(self.translate_address(address), bytes(binary))], | ||
| no_progress=not self.args.debug, | ||
| ) | ||
|
|
||
| def read_range(self, address, length): | ||
| logging.debug( | ||
| "DEBUG => Read Range, address: {:#010x}, length: {}".format(address, length) | ||
| ) | ||
| if length == 0: | ||
| return bytes() | ||
|
|
||
| cmds = self._import_esptool() | ||
| return cmds.read_flash( | ||
| self.esp, self.translate_address(address), length, no_progress=True | ||
| ) | ||
|
|
||
| def clear_bytes(self, address): | ||
| logging.debug("Clearing bytes starting at {:#0x}".format(address)) | ||
|
|
||
| binary = bytes([0xFF] * 8) | ||
| self.flash_binary(address, binary) | ||
|
|
||
| def determine_current_board(self): | ||
| # We might need to fill in settings if we only know the board name. | ||
| self._configure_from_known_boards() | ||
|
|
||
| # Check that we learned what we needed to learn. | ||
| if self.board is None: | ||
| raise TockLoaderException("Could not determine the current board") | ||
| if self.arch is None: | ||
| raise TockLoaderException("Could not determine the current arch") | ||
| if self.page_size == 0: | ||
| raise TockLoaderException("Could not determine the current page size") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We should just install it with the package. There is nothing worse than using a tool and then having it tell you you need to install its dependencies for it when it could have just done it.
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.
Yeah, that makes sense! I guess I was defaulting to the way the nRF tools work given they're proprietary.
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.
Hm,
esptoolis GPL-2.0 and Tockloader is MIT, so we couldn't re-distribute a bundle if we add it as a required dependency. 😒