# SPDX-FileCopyrightText: 2014-2022 Fredrik Ahlberg, Angus Gratton,
# Espressif Systems (Shanghai) CO LTD, other contributors as noted.
#
# SPDX-License-Identifier: GPL-2.0-or-later
import base64
import hashlib
import itertools
import json
import os
import re
import string
import struct
import sys
import time
from .util import FatalError, NotImplementedInROMError, UnsupportedCommandError
from .util import byte, hexify, mask_to_shift, pad_to
try:
import serial
except ImportError:
print(
"Pyserial is not installed for %s. "
"Check the README for installation instructions." % (sys.executable)
)
raise
# check 'serial' is 'pyserial' and not 'serial'
# ref. https://github.com/espressif/esptool/issues/269
try:
if "serialization" in serial.__doc__ and "deserialization" in serial.__doc__:
raise ImportError(
"esptool.py depends on pyserial, but there is a conflict with a currently "
"installed package named 'serial'.\n"
"You may work around this by 'pip uninstall serial; pip install pyserial' "
"but this may break other installed Python software "
"that depends on 'serial'.\n"
"There is no good fix for this right now, "
"apart from configuring virtualenvs. "
"See https://github.com/espressif/esptool/issues/269#issuecomment-385298196"
" for discussion of the underlying issue(s)."
)
except TypeError:
pass # __doc__ returns None for pyserial
try:
import serial.tools.list_ports as list_ports
except ImportError:
print(
"The installed version (%s) of pyserial appears to be too old for esptool.py "
"(Python interpreter %s). Check the README for installation instructions."
% (sys.VERSION, sys.executable)
)
raise
except Exception:
if sys.platform == "darwin":
# swallow the exception, this is a known issue in pyserial+macOS Big Sur preview
# ref https://github.com/espressif/esptool/issues/540
list_ports = None
else:
raise
DEFAULT_TIMEOUT = 3 # timeout for most flash operations
START_FLASH_TIMEOUT = 20 # timeout for starting flash (may perform erase)
CHIP_ERASE_TIMEOUT = 120 # timeout for full chip erase
MAX_TIMEOUT = CHIP_ERASE_TIMEOUT * 2 # longest any command can run
SYNC_TIMEOUT = 0.1 # timeout for syncing with bootloader
MD5_TIMEOUT_PER_MB = 8 # timeout (per megabyte) for calculating md5sum
ERASE_REGION_TIMEOUT_PER_MB = 30 # timeout (per megabyte) for erasing a region
ERASE_WRITE_TIMEOUT_PER_MB = 40 # timeout (per megabyte) for erasing and writing data
MEM_END_ROM_TIMEOUT = 0.05 # short timeout for ESP_MEM_END, as it may never respond
DEFAULT_SERIAL_WRITE_TIMEOUT = 10 # timeout for serial port write
DEFAULT_CONNECT_ATTEMPTS = 7 # default number of times to try connection
STUBS_DIR = os.path.join(os.path.dirname(__file__), "./targets/stub_flasher/")
def get_stub_json_path(chip_name):
chip_name = re.sub(r"[-()]", "", chip_name.lower())
chip_name = chip_name.replace("esp", "")
return STUBS_DIR + "stub_flasher_" + chip_name + ".json"
def timeout_per_mb(seconds_per_mb, size_bytes):
"""Scales timeouts which are size-specific"""
result = seconds_per_mb * (size_bytes / 1e6)
if result < DEFAULT_TIMEOUT:
return DEFAULT_TIMEOUT
return result
def check_supported_function(func, check_func):
"""
Decorator implementation that wraps a check around an ESPLoader
bootloader function to check if it's supported.
This is used to capture the multidimensional differences in
functionality between the ESP8266 & ESP32 (and later chips) ROM loaders, and the
software stub that runs on these. Not possible to do this cleanly
via inheritance alone.
"""
def inner(*args, **kwargs):
obj = args[0]
if check_func(obj):
return func(*args, **kwargs)
else:
raise NotImplementedInROMError(obj, func)
return inner
def stub_function_only(func):
"""Attribute for a function only supported in the software stub loader"""
return check_supported_function(func, lambda o: o.IS_STUB)
def stub_and_esp32_function_only(func):
"""Attribute for a function only supported by stubs or ESP32 and later chips ROM"""
return check_supported_function(
func, lambda o: o.IS_STUB or o.CHIP_NAME not in ["ESP8266"]
)
def esp32s3_or_newer_function_only(func):
"""Attribute for a function only supported by ESP32S3 and later chips ROM"""
return check_supported_function(
func, lambda o: o.CHIP_NAME not in ["ESP8266", "ESP32", "ESP32-S2"]
)
class StubFlasher:
def __init__(self, json_path):
with open(json_path) as json_file:
stub = json.load(json_file)
self.text = base64.b64decode(stub["text"])
self.text_start = stub["text_start"]
self.entry = stub["entry"]
try:
self.data = base64.b64decode(stub["data"])
self.data_start = stub["data_start"]
except KeyError:
self.data = None
self.data_start = None
class ESPLoader(object):
"""Base class providing access to ESP ROM & software stub bootloaders.
Subclasses provide ESP8266 & ESP32 Family specific functionality.
Don't instantiate this base class directly, either instantiate a subclass or
call cmds.detect_chip() which will interrogate the chip and return the
appropriate subclass instance.
"""
CHIP_NAME = "Espressif device"
IS_STUB = False
FPGA_SLOW_BOOT = False
DEFAULT_PORT = "/dev/ttyUSB0"
USES_RFC2217 = False
# Commands supported by ESP8266 ROM bootloader
ESP_FLASH_BEGIN = 0x02
ESP_FLASH_DATA = 0x03
ESP_FLASH_END = 0x04
ESP_MEM_BEGIN = 0x05
ESP_MEM_END = 0x06
ESP_MEM_DATA = 0x07
ESP_SYNC = 0x08
ESP_WRITE_REG = 0x09
ESP_READ_REG = 0x0A
# Some comands supported by ESP32 and later chips ROM bootloader (or -8266 w/ stub)
ESP_SPI_SET_PARAMS = 0x0B
ESP_SPI_ATTACH = 0x0D
ESP_READ_FLASH_SLOW = 0x0E # ROM only, much slower than the stub flash read
ESP_CHANGE_BAUDRATE = 0x0F
ESP_FLASH_DEFL_BEGIN = 0x10
ESP_FLASH_DEFL_DATA = 0x11
ESP_FLASH_DEFL_END = 0x12
ESP_SPI_FLASH_MD5 = 0x13
# Commands supported by ESP32-S2 and later chips ROM bootloader only
ESP_GET_SECURITY_INFO = 0x14
# Some commands supported by stub only
ESP_ERASE_FLASH = 0xD0
ESP_ERASE_REGION = 0xD1
ESP_READ_FLASH = 0xD2
ESP_RUN_USER_CODE = 0xD3
# Flash encryption encrypted data command
ESP_FLASH_ENCRYPT_DATA = 0xD4
# Response code(s) sent by ROM
ROM_INVALID_RECV_MSG = 0x05 # response if an invalid message is received
# Maximum block sized for RAM and Flash writes, respectively.
ESP_RAM_BLOCK = 0x1800
FLASH_WRITE_SIZE = 0x400
# Default baudrate. The ROM auto-bauds, so we can use more or less whatever we want.
ESP_ROM_BAUD = 115200
# First byte of the application image
ESP_IMAGE_MAGIC = 0xE9
# Initial state for the checksum routine
ESP_CHECKSUM_MAGIC = 0xEF
# Flash sector size, minimum unit of erase.
FLASH_SECTOR_SIZE = 0x1000
UART_DATE_REG_ADDR = 0x60000078
# This ROM address has a different value on each chip model
CHIP_DETECT_MAGIC_REG_ADDR = 0x40001000
UART_CLKDIV_MASK = 0xFFFFF
# Memory addresses
IROM_MAP_START = 0x40200000
IROM_MAP_END = 0x40300000
# The number of bytes in the UART response that signify command status
STATUS_BYTES_LENGTH = 2
# Bootloader flashing offset
BOOTLOADER_FLASH_OFFSET = 0x0
# ROM supports an encrypted flashing mode
SUPPORTS_ENCRYPTED_FLASH = False
# Response to ESP_SYNC might indicate that flasher stub is running
# instead of the ROM bootloader
sync_stub_detected = False
# Device PIDs
USB_JTAG_SERIAL_PID = 0x1001
# Chip IDs that are no longer supported b
没有合适的资源?快使用搜索试试~ 我知道了~
esptool-4.2.1-linux.tar.gz
需积分: 5 0 下载量 197 浏览量
2023-12-25
12:37:44
上传
评论
收藏 88KB GZ 举报
温馨提示
共61个文件
py:36个
json:20个
ds_store:2个
esptool-4.2.1-linux.tar
资源推荐
资源详情
资源评论
收起资源包目录
esptool-4.2.1-linux.tar.gz (61个子文件)
esptool
esptool.py 1KB
._esptool.py 212B
._esptool 212B
esptool
__init__.py 33KB
.DS_Store 6KB
loader.py 55KB
cmds.py 38KB
util.py 4KB
._util.py 212B
._loader.py 212B
.___main__.py 212B
.___init__.py 212B
targets
__init__.py 748B
esp32c3.py 6KB
esp32h2beta1.py 5KB
stub_flasher
stub_flasher_32c2.json 5KB
._stub_flasher_32s3beta2.json 212B
._stub_flasher_32c2.json 212B
stub_flasher_32h2beta2.json 5KB
stub_flasher_32.json 5KB
stub_flasher_32s3beta2.json 6KB
._stub_flasher_32s3.json 212B
stub_flasher_8266.json 12KB
._stub_flasher_8266.json 212B
._stub_flasher_32h2beta2.json 212B
stub_flasher_32c3.json 5KB
stub_flasher_32c6beta.json 5KB
._stub_flasher_32.json 212B
._stub_flasher_32h2beta1.json 212B
._stub_flasher_32s2.json 212B
._stub_flasher_32c3.json 212B
stub_flasher_32h2beta1.json 5KB
stub_flasher_32s3.json 6KB
._stub_flasher_32c6beta.json 212B
stub_flasher_32s2.json 6KB
esp32s3.py 8KB
._esp32h2beta2.py 212B
esp32c6beta.py 644B
._esp32c3.py 212B
esp32c2.py 6KB
esp32s3beta2.py 1KB
esp32h2beta2.py 1KB
._stub_flasher 212B
._esp32s3.py 212B
.___init__.py 212B
._esp32h2beta1.py 212B
._esp32s3beta2.py 212B
._esp32c2.py 212B
esp8266.py 6KB
._esp32s2.py 212B
._esp32c6beta.py 212B
._esp32.py 212B
esp32.py 12KB
esp32s2.py 10KB
._esp8266.py 212B
._bin_image.py 212B
._cmds.py 212B
._targets 212B
__main__.py 270B
._.DS_Store 120B
bin_image.py 44KB
共 61 条
- 1
资源评论
sixwhen
- 粉丝: 1
- 资源: 207
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- zbar libiconv.so libzbarjni.so 32 64位的配置包
- 基于stm32的家庭湿度控制系统的设计与实现源码+文档说明(高分项目)
- 一个简单的Python程序,它使用socket库来实现一个UDP服务器,该服务器接收UDP消息,然后通过TCP连接将消息转发给T
- OpenVINO CSharp 异步推理接口部署YOLOv8 代码
- 问题解决教程.docx
- Matlab箱线图教程.txt
- 基于树莓派的智能小车:自动避障,实时图像传输,目标检测,网球追踪
- 百度AI-语音合成练习
- 操作系统复习(极速版)
- 一个数字信号处理的例子,包括信号的采样、带噪信号的生成、使用低通滤波器滤波,并绘制结果
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功