Skip to content

Commit

Permalink
Merge branch 'espressif:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason2866 authored Jul 31, 2022
2 parents cd8630f + 94f29a5 commit a059707
Show file tree
Hide file tree
Showing 40 changed files with 453 additions and 687 deletions.
15 changes: 15 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,21 @@ target_esptool_test_esp32s3_usbcdc:
script:
- coverage run --parallel-mode ${CI_PROJECT_DIR}/test/test_esptool.py /dev/serial_ports/ESP32S3_USBCDC esp32s3 115200

# ESP32C2
target_esptool_test_esp32c2_40mhz:
extends: .target_esptool_test
tags:
- esptool_esp32c2_40mhz_target
script:
- coverage run --parallel-mode ${CI_PROJECT_DIR}/test/test_esptool.py /dev/serial_ports/ESP32C2_40MHZ esp32c2 115200

target_esptool_test_esp32c2_26mhz:
extends: .target_esptool_test
tags:
- esptool_esp32c2_26mhz_target
script:
- coverage run --parallel-mode ${CI_PROJECT_DIR}/test/test_esptool.py /dev/serial_ports/ESP32C2_26MHZ esp32c2 115200

combine_reports:
stage: report
image: python:3.7-bullseye
Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
include README.md
include LICENSE
include esptool/targets/stub_flasher/*.json
# sdist includes test/test*.py by default, but esptool.py tests
# are so far only intended to run from the git repo itself
prune test
Expand Down
31 changes: 20 additions & 11 deletions espefuse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,26 @@ def main(custom_commandline=None):
just_print_help = [
True for arg in remaining_args if arg in ["--help", "-h"]
] or remaining_args == []
esp = get_esp(
common_args.port,
common_args.baud,
common_args.before,
common_args.chip,
just_print_help,
common_args.virt,
common_args.debug,
common_args.path_efuse_file,
)

print("espefuse.py v{}".format(esptool.__version__))

try:
esp = get_esp(
common_args.port,
common_args.baud,
common_args.before,
common_args.chip,
just_print_help,
common_args.virt,
common_args.debug,
common_args.path_efuse_file,
)
except esptool.FatalError as e:
raise esptool.FatalError(
f"{e}\nPlease make sure that you have specified "
"the right port with the --port argument"
) # TODO: Require the --port argument in the next major release, ESPTOOL-490

efuses, efuse_operations = get_efuses(
esp, just_print_help, debug_mode, common_args.do_not_confirm
)
Expand All @@ -228,7 +238,6 @@ def main(custom_commandline=None):
efuse_operations.add_commands(subparsers, efuses)

grouped_remaining_args, used_cmds = split_on_groups(remaining_args)
print("espefuse.py v%s" % esptool.__version__)
if len(grouped_remaining_args) == 0:
parser.print_help()
parser.exit(1)
Expand Down
25 changes: 16 additions & 9 deletions espefuse/efuse/base_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
#
# SPDX-License-Identifier: GPL-2.0-or-later

import argparse
import binascii
import re
import sys

from bitstring import BitArray, BitString
from bitstring import BitArray, BitString, CreationError

import esptool

Expand All @@ -28,7 +27,7 @@ def check_arg_value(efuse, new_value):
if efuse.efuse_type.startswith("bool"):
new_value = 1 if new_value is None else int(new_value, 0)
if new_value != 1:
raise argparse.ArgumentTypeError(
raise esptool.FatalError(
"New value is not accepted for efuse '{}' "
"(will always burn 0->1), given value={}".format(
efuse.name, new_value
Expand All @@ -48,32 +47,32 @@ def check_arg_value(efuse, new_value):
new_value = int(new_value, 0)
else:
if new_value is None:
raise argparse.ArgumentTypeError(
raise esptool.FatalError(
"New value required for efuse '{}' (given None)".format(
efuse.name
)
)
new_value = int(new_value, 0)
if new_value == 0:
raise argparse.ArgumentTypeError(
raise esptool.FatalError(
"New value should not be 0 for '{}' "
"(given value= {})".format(efuse.name, new_value)
)
elif efuse.efuse_type.startswith("bytes"):
if new_value is None:
raise argparse.ArgumentTypeError(
raise esptool.FatalError(
"New value required for efuse '{}' "
"(given None)".format(efuse.name)
)
if len(new_value) * 8 != efuse.bitarray.len:
raise argparse.ArgumentTypeError(
raise esptool.FatalError(
"The length of efuse '{}' ({} bits) "
"(given len of the new value= {} bits)".format(
efuse.name, efuse.bitarray.len, len(new_value) * 8
)
)
else:
raise argparse.ArgumentTypeError(
raise esptool.FatalError(
"The '{}' type for the '{}' efuse is not supported yet.".format(
efuse.efuse_type, efuse.name
)
Expand Down Expand Up @@ -598,7 +597,15 @@ def convert_to_bitstring(self, new_value):
# *[x] - means a byte.
return BitArray(bytes=new_value[::-1], length=len(new_value) * 8)
else:
return BitArray(self.efuse_type + "={}".format(new_value))
try:
return BitArray(self.efuse_type + "={}".format(new_value))
except CreationError as err:
print(
"New value '{}' is not suitable for {} ({})".format(
new_value, self.name, self.efuse_type
)
)
raise esptool.FatalError(err)

def check_new_value(self, bitarray_new_value):
bitarray_old_value = self.get_bitstring() | self.get_bitstring(from_read=False)
Expand Down
2 changes: 1 addition & 1 deletion espefuse/efuse/esp32c2/emulate_efuse_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def get_chip_description(self):
return ""

def get_crystal_freq(self):
return 40 # MHz (common for all chips)
return 40 # MHz

def get_security_info(self):
return {
Expand Down
15 changes: 11 additions & 4 deletions espefuse/efuse/esp32c2/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def __init__(self, esp, skip_connect=False, debug=False, do_not_confirm=False):
for efuse in self.Fields.BLOCK2_CALIBRATION_EFUSES
]
else:
if self["BLOCK2_VERSION"].get() == 1:
if self["BLOCK2_VERSION_MINOR"].get() == 1:
self.efuses += [
EfuseField.from_tuple(
self, self.Fields.get(efuse), self.Fields.get(efuse).class_type
Expand Down Expand Up @@ -200,16 +200,23 @@ def efuse_read(self):
def set_efuse_timing(self):
"""Set timing registers for burning efuses"""
# Configure clock
apb_freq = self.get_crystal_freq()
if apb_freq != 40:
xtal_freq = self.get_crystal_freq()
if xtal_freq not in [26, 40]:
raise esptool.FatalError(
"The eFuse supports only xtal=40M (xtal was %d)" % apb_freq
"The eFuse supports only xtal=26M and 40M (xtal was %d)" % xtal_freq
)

self.update_reg(
self.REGS.EFUSE_WR_TIM_CONF2_REG, self.REGS.EFUSE_PWR_OFF_NUM_M, 0x190
)

tpgm_inactive_val = 200 if xtal_freq == 40 else 130
self.update_reg(
self.REGS.EFUSE_WR_TIM_CONF0_REG,
self.REGS.EFUSE_TPGM_INACTIVE_M,
tpgm_inactive_val,
)

def get_coding_scheme_warnings(self, silent=False):
"""Check if the coding scheme has detected any errors."""
old_addr_reg = 0
Expand Down
16 changes: 9 additions & 7 deletions espefuse/efuse/esp32c2/mem_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class EfuseDefineRegisters(EfuseRegistersBase):
EFUSE_PWR_OFF_NUM_S = 0
EFUSE_PWR_OFF_NUM_M = 0xFFFF << EFUSE_PWR_OFF_NUM_S

EFUSE_WR_TIM_CONF0_REG = DR_REG_EFUSE_BASE + 0x110
EFUSE_TPGM_INACTIVE_S = 8
EFUSE_TPGM_INACTIVE_M = 0xFF << EFUSE_TPGM_INACTIVE_S


class EfuseDefineBlocks(EfuseBlocksBase):

Expand Down Expand Up @@ -123,16 +127,14 @@ class EfuseDefineFields(EfuseFieldsBase):
# Parameters in BLOCK2
# Name Category Block Word Pos Type:len WR_DIS RD_DIS Class Description Dictionary
("MAC", "identity", 2, 0, 0, "bytes:6", 6, None, 'mac', "Factory MAC Address", None),
("WAFER_VERSION", "identity", 2, 1, 16, "uint:3", 6, None, None, "WAFER version",
{0: "(revision 0)",
1: "(revision 1)"}),
("PKG_VERSION", "identity", 2, 1, 19, "uint:3", 6, None, None, "Package version",
("WAFER_VERSION_MINOR", "identity", 2, 1, 16, "uint:4", 6, None, None, "Minor WAFER version", None),
("WAFER_VERSION_MAJOR", "identity", 2, 1, 20, "uint:2", 6, None, None, "Major WAFER version", None),
("PKG_VERSION", "identity", 2, 1, 22, "uint:3", 6, None, None, "Package version",
{0: "ESP32-C2"}),
("BLOCK2_VERSION", "identity", 2, 1, 22, "uint:3", 6, None, None, "Version of BLOCK2",
("BLOCK2_VERSION_MINOR", "identity", 2, 1, 25, "uint:3", 6, None, None, "Minor version of BLOCK2",
{0: "No calibration", 1: "With calibration"}),

("RF_REF_I_BIAS_CONFIG", "rf", 2, 1, 25, "uint:3", 6, None, None, "", None),
("LDO_VOL_BIAS_CONFIG_LOW", "ldo", 2, 1, 29, "uint:3", 6, None, None, "", None),
("BLOCK2_VERSION_MAJOR", "identity", 2, 1, 28, "uint:2", 6, None, None, "Major version of BLOCK2", None),
("LDO_VOL_BIAS_CONFIG_HIGH", "ldo", 2, 2, 0, "uint:27", 6, None, None, "", None),
("PVT_LOW", "pvt", 2, 2, 27, "uint:5", 6, None, None, "", None),
("PVT_HIGH", "pvt", 2, 3, 0, "uint:10", 6, None, None, "", None),
Expand Down
4 changes: 2 additions & 2 deletions espefuse/efuse/esp32c2/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def set_flash_voltage(esp, efuses, args):
def adc_info(esp, efuses, args):
print("")
# fmt: off
if efuses["BLOCK2_VERSION"].get() == 1:
if efuses["BLOCK2_VERSION_MINOR"].get() == 1:
print(" RF_REF_I_BIAS_CONFIG: {}".format(efuses["RF_REF_I_BIAS_CONFIG"].get()))

print(" LDO_VOL_BIAS_CONFIG_LOW: {}".format(efuses["LDO_VOL_BIAS_CONFIG_LOW"].get()))
Expand All @@ -165,7 +165,7 @@ def adc_info(esp, efuses, args):
print(" ADC_CALIBRATION_2: {}".format(efuses["ADC_CALIBRATION_2"].get()))

else:
print("BLOCK2_VERSION = {}".format(efuses["BLOCK2_VERSION"].get_meaning()))
print("BLOCK2_VERSION_MINOR = {}".format(efuses["BLOCK2_VERSION_MINOR"].get_meaning()))
# fmt: on


Expand Down
6 changes: 4 additions & 2 deletions esptool/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,10 @@ def add_spi_flash_subparsers(parent, allow_keep, auto_detect):
"--min-rev",
"-r",
help="Minimum chip revision",
choices=["0", "1", "2", "3"],
default="0",
type=int,
choices=range(256),
metavar="{0, ... 255}",
default=0,
)
parser_elf2image.add_argument(
"--secure-pad",
Expand Down
2 changes: 1 addition & 1 deletion esptool/cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ def elf2image(args):
image.flash_mode = FLASH_MODES[args.flash_mode]

if args.chip != "esp8266":
image.min_rev = int(args.min_rev)
image.min_rev = args.min_rev
image.append_digest = args.append_digest

if args.flash_mmu_page_size:
Expand Down
47 changes: 37 additions & 10 deletions esptool/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
#
# SPDX-License-Identifier: GPL-2.0-or-later

import base64
import hashlib
import itertools
import json
import os
import re
import string
Expand Down Expand Up @@ -72,6 +74,14 @@
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"""
Expand Down Expand Up @@ -121,6 +131,23 @@ def esp32s3_or_newer_function_only(func):
)


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.
Expand Down Expand Up @@ -698,12 +725,12 @@ def mem_begin(self, size, blocks, blocksize, offset):
"""Start downloading an application image to RAM"""
# check we're not going to overwrite a running stub with this data
if self.IS_STUB:
stub = self.STUB_CODE
stub = StubFlasher(get_stub_json_path(self.CHIP_NAME))
load_start = offset
load_end = offset + size
for (start, end) in [
(stub["data_start"], stub["data_start"] + len(stub["data"])),
(stub["text_start"], stub["text_start"] + len(stub["text"])),
(stub.data_start, stub.data_start + len(stub.data)),
(stub.text_start, stub.text_start + len(stub.text)),
]:
if load_start < end and load_end > start:
raise FatalError(
Expand Down Expand Up @@ -861,26 +888,26 @@ def parse_flash_freq_arg(cls, arg):

def run_stub(self, stub=None):
if stub is None:
stub = self.STUB_CODE
stub = StubFlasher(get_stub_json_path(self.CHIP_NAME))

if self.sync_stub_detected:
print("Stub is already running. No upload is necessary.")
return self.STUB_CLASS(self)

# Upload
print("Uploading stub...")
for field in ["text", "data"]:
if field in stub:
offs = stub[field + "_start"]
length = len(stub[field])
for field in [stub.text, stub.data]:
if field is not None:
offs = stub.text_start if field == stub.text else stub.data_start
length = len(field)
blocks = (length + self.ESP_RAM_BLOCK - 1) // self.ESP_RAM_BLOCK
self.mem_begin(length, blocks, self.ESP_RAM_BLOCK, offs)
for seq in range(blocks):
from_offs = seq * self.ESP_RAM_BLOCK
to_offs = from_offs + self.ESP_RAM_BLOCK
self.mem_block(stub[field][from_offs:to_offs], seq)
self.mem_block(field[from_offs:to_offs], seq)
print("Running stub...")
self.mem_finish(stub["entry"])
self.mem_finish(stub.entry)

p = self.read()
if p != b"OHAI":
Expand Down
Loading

0 comments on commit a059707

Please sign in to comment.