Compare commits

..
36 changed files with 1968 additions and 1221 deletions

No files matched your search

+2
View File
@@ -146,6 +146,7 @@ esphome/components/cover/* @esphome/core
esphome/components/cs5460a/* @balrog-kun
esphome/components/cse7761/* @berfenger
esphome/components/cst226/* @clydebarrow
esphome/components/cst3240/* @tpruvot
esphome/components/cst328/* @latonita
esphome/components/cst816/* @clydebarrow
esphome/components/cst9220/* @clydebarrow
@@ -467,6 +468,7 @@ esphome/components/nfc/* @jesserockz @kbx81
esphome/components/noblex/* @AGalfra
esphome/components/noise/* @esphome/core
esphome/components/npi19/* @bakerkj
esphome/components/npm1300/* @tomaszduda23
esphome/components/nrf52/* @tomaszduda23
esphome/components/ns2009/* @tchilov
esphome/components/ns2009/touchscreen/* @tchilov
+19 -427
View File
@@ -2,7 +2,6 @@
from collections import Counter, defaultdict
from dataclasses import dataclass, field
from functools import cached_property
import logging
from pathlib import Path
import re
@@ -56,7 +55,6 @@ _COMPONENT_PREFIX_EXTERNAL = "[external]"
_COMPONENT_PREFIX_LIB = "[lib]"
_COMPONENT_CORE = f"{_COMPONENT_PREFIX_ESPHOME}core"
_COMPONENT_API = f"{_COMPONENT_PREFIX_ESPHOME}api"
_COMPONENT_ARDUINO_CORE = "arduino_core"
# Placement new storage suffix (generated by codegen Pvariable)
_PSTORAGE_SUFFIX = "__pstorage"
@@ -85,24 +83,6 @@ _NAMESPACE_STD = "std::"
# Type alias for symbol information: (symbol_name, size, component)
SymbolInfoType = tuple[str, int, str]
# Only ESP8266 has this section; its .rodata is copied to RAM at boot like .data
_ESP8266_FLASH_CODE_SECTION = ".irom0.text"
# readelf -SW row: [Nr] Name Type Address Off Size ES Flg
_OBJ_SECTION_PATTERN = re.compile(
r"^\s*\[\s*(\d+)\]\s+(\S+)\s+(\S+)\s+[\da-f]+\s+[\da-f]+\s+([\da-f]+)\s+[\da-f]+\s+(\S*)"
)
_RELOC_SECTION_PATTERN = re.compile(r"^Relocation section '\.rela(\S+)'")
# Code section; group 1 is the function for -ffunction-sections names, after any
# placement prefix (.text.startup.<fn>, .iram.text.<fn> for IRAM_ATTR)
_CODE_SECTION_PATTERN = re.compile(
r"^\.(?:literal|text|irom0\.text|iram\.text|iram0\.text)"
r"(?:\.(?:startup|unlikely|hot|exit)(?=\.|$))?(?:\.(.+))?$"
)
# readelf -x row: " 0x00000000 0000803f 0000803f 0000803f 0000803f ...?...?...?...?"
_HEX_DUMP_PREFIX = " 0x"
_HEX_DUMP_COLUMNS = slice(13, 48)
# RAM sections - symbols in these sections consume RAM
RAM_SECTIONS = frozenset([".data", ".bss"])
@@ -139,21 +119,6 @@ class SDKSymbol:
demangled: str = "" # Demangled name (populated after analysis)
@dataclass
class UnnamedConstant:
"""Constant without a symbol in an object's .rodata (RAM on ESP8266)."""
size: int
source_file: str
component: str
functions: list[str] # Functions that reference it, mangled
jump_table: bool # Holds code addresses (a switch table), not a value
data: bytes # Leading bytes, to recognize the value
strings: list[str] = field(default_factory=list) # String literals it points to
# Every function that uses the section, which decides whether the link kept it
users: list[str] = field(default_factory=list)
@dataclass
class ComponentMemory:
"""Tracks memory usage for a component."""
@@ -187,157 +152,6 @@ class ComponentMemory:
return self.data_size + self.bss_size
@dataclass
class _RodataSection:
"""An object's .rodata section and the byte ranges its symbols cover."""
name: str
size: int
covered: list[tuple[int, int]] = field(default_factory=list) # (offset, size)
@cached_property
def _named(self) -> frozenset[int]:
return frozenset(
offset
for start, size in self.covered
for offset in range(start, min(start + size, self.size))
)
def is_unnamed(self, offset: int) -> bool:
return 0 <= offset < self.size and offset not in self._named
@property
def unnamed_size(self) -> int:
return self.size - len(self._named)
def _parse_hex_dumps(output: str) -> dict[str, bytearray]:
"""Collect the bytes of each ``readelf -x`` section dump by section name."""
contents: dict[str, bytearray] = defaultdict(bytearray)
section: str | None = None
for line in output.splitlines():
if line.startswith("Hex dump of section"):
section = line.split("'")[1]
elif section is not None and line.startswith(_HEX_DUMP_PREFIX):
contents[section] += bytes.fromhex(line[_HEX_DUMP_COLUMNS].replace(" ", ""))
elif line and not line.startswith(" "):
section = None
return contents
def _parse_relocations(output: str) -> list[tuple[str, int, str, int]]:
"""List (relocated section, offset, target, addend) from ``readelf -rW`` output."""
relocations: list[tuple[str, int, str, int]] = []
section: str | None = None
for line in output.splitlines():
if line.startswith("Relocation section"):
match = _RELOC_SECTION_PATTERN.match(line)
section = match.group(1) if match else None
elif line[:1].isdigit() and section is not None:
# Offset Info Type Sym.Value Sym.Name + Addend
fields = line.split()
if len(fields) < 5:
continue
addend = int(fields[6], 16) if len(fields) > 6 else 0
if len(fields) > 6 and fields[5] == "-":
addend = -addend
relocations.append((section, int(fields[0], 16), fields[4], addend))
elif line and line[0] != " ":
section = None
return relocations
def _pointer_target(referrer: bytes, offset: int, addend: int) -> int | None:
"""Offset a relocated pointer at ``offset`` points to, or None if not dumped."""
if offset < 0 or offset + 4 > len(referrer):
return None
# Xtensa keeps part of the addend in the relocated word
return addend + int.from_bytes(referrer[offset : offset + 4], "little")
def _pointed_literal(
referrer: bytes, offset: int, addend: int, strings: bytes
) -> bytes | None:
"""Read the string literal a relocated pointer refers to, or None if out of range."""
start = _pointer_target(referrer, offset, addend)
if start is None or not 0 <= start < len(strings):
return None
end = strings.find(b"\0", start)
return bytes(strings[start : end if end != -1 else len(strings)])
@dataclass
class _FunctionDefinitions:
"""Where each function is defined across the scanned objects."""
weak: dict[str, set[str]] = field(default_factory=lambda: defaultdict(set))
strong: set[str] = field(default_factory=set) # Global, non-weak
count: Counter[str] = field(default_factory=Counter) # Objects defining it
def linked_copy_unknown(self, file_path: str, functions: list[str]) -> bool:
"""True if another object's copy of these functions may be the linked one."""
if not functions:
return False
if self.weak[file_path].issuperset(functions):
# Inline copies: dropped only once a strong definition replaces every user
return self.strong.issuperset(functions)
# Same-named local functions in several objects; the ELF can't tell them apart
return all(f not in self.strong and self.count[f] > 1 for f in functions)
def _parse_unnamed_rodata(
output: str,
) -> tuple[dict[str, dict[str, _RodataSection]], _FunctionDefinitions]:
"""Find .rodata sections with bytes no symbol covers in ``readelf -SW -sW`` output.
Returns:
Object path -> section index -> section, and where functions are defined
"""
sections: dict[str, dict[str, _RodataSection]] = defaultdict(dict)
definitions = _FunctionDefinitions()
file_path = ""
for line in output.splitlines():
if line.startswith("File: "):
file_path = line[6:]
elif match := _OBJ_SECTION_PATTERN.match(line):
index, name, section_type, size_hex, flags = match.groups()
# Merged sections (string literals) are shared between objects
if (
section_type == "PROGBITS"
and name.startswith(".rodata")
and "A" in flags
and "M" not in flags
):
sections[file_path][index] = _RodataSection(name, int(size_hex, 16))
else:
# Num: Value Size Type Bind Vis Ndx Name
fields = line.split()
if len(fields) < 8:
continue
if fields[3] == "OBJECT" and (
section := sections[file_path].get(fields[6])
):
section.covered.append((int(fields[1], 16), int(fields[2], 0)))
elif fields[3] == "FUNC" and fields[6] != "UND":
definitions.count[fields[7]] += 1
if fields[4] == "WEAK":
definitions.weak[file_path].add(fields[7])
elif fields[4] == "GLOBAL":
definitions.strong.add(fields[7])
unnamed = {
path: kept
for path, file_sections in sections.items()
if (
kept := {
index: section
for index, section in file_sections.items()
if section.unnamed_size
}
)
}
return unnamed, definitions
class MemoryAnalyzer:
"""Analyzes memory usage from ELF files."""
@@ -417,9 +231,6 @@ class MemoryAnalyzer:
self._heuristic_to_lib: dict[str, str] = {}
# Function call counts: mangled_name -> call_count
self._function_call_counts: Counter[str] = Counter()
# True on ESP8266, where .rodata is RAM; it is then counted as .data
self.rodata_in_ram = False
self._unnamed_constants: list[UnnamedConstant] = []
def analyze(self) -> dict[str, ComponentMemory]:
"""Analyze the ELF file and return component memory usage."""
@@ -429,7 +240,6 @@ class MemoryAnalyzer:
self._scan_source_symbols()
self._categorize_symbols()
self._analyze_cswtch_symbols()
self._analyze_object_rodata()
self._analyze_sdk_libraries()
self._analyze_function_calls()
return dict(self.components)
@@ -443,17 +253,18 @@ class MemoryAnalyzer:
check=True,
)
raw_sections = [
(match.group(1), int(match.group(2), 16))
for line in result.stdout.splitlines()
if (match := _READELF_SECTION_PATTERN.match(line))
]
self.rodata_in_ram = any(
name == _ESP8266_FLASH_CODE_SECTION for name, _ in raw_sections
)
# Parse section headers
for line in result.stdout.splitlines():
# Look for section entries
if not (match := _READELF_SECTION_PATTERN.match(line)):
continue
for section_name, size in raw_sections:
mapped_section = self._standard_section(map_section_name(section_name))
section_name = match.group(1)
size_hex = match.group(2)
size = int(size_hex, 16)
# Map to standard section name
mapped_section = map_section_name(section_name)
if not mapped_section:
continue
@@ -461,12 +272,6 @@ class MemoryAnalyzer:
self.sections[mapped_section] = MemorySection(mapped_section)
self.sections[mapped_section].total_size += size
def _standard_section(self, section: str | None) -> str | None:
"""Count .rodata as .data where it is RAM (ESP8266)."""
if section == ".rodata" and self.rodata_in_ram:
return ".data"
return section
def _parse_symbols(self) -> None:
"""Parse symbols from ELF file."""
result = subprocess.run(
@@ -484,18 +289,14 @@ class MemoryAnalyzer:
continue
section, name, size, address = symbol_info
section = self._standard_section(section)
if section not in self.sections:
continue
# Every alias is in the ELF, even when its size is counted once
self._elf_symbol_names.add(name)
# Skip duplicate symbols at the same address (e.g., C1/C2 constructors)
if address in seen_addresses:
if address in seen_addresses or section not in self.sections:
continue
self.sections[section].symbols.append((name, size, ""))
self.sections[section].symbol_size += size
self._elf_symbol_names.add(name)
seen_addresses.add(address)
def _categorize_symbols(self) -> None:
@@ -1124,12 +925,10 @@ class MemoryAnalyzer:
Path to the directory containing .o files, or None if not found.
"""
# The ELF is typically at .pioenvs/<env>/firmware.elf
# PlatformIO puts object files in .pioenvs/<env>/src/ and .pioenvs/<env>/lib*/,
# the native ESP8266 build in .pioenvs/<env>/obj/src/ and obj/lib/
# Object files are in .pioenvs/<env>/src/ and .pioenvs/<env>/lib*/
pioenvs_dir = self.elf_path.parent
for obj_dir in (pioenvs_dir, pioenvs_dir / "obj"):
if any(obj_dir.glob("src/*.o")):
return obj_dir
if pioenvs_dir.exists() and any(pioenvs_dir.glob("src/*.o")):
return pioenvs_dir
return None
@staticmethod
@@ -1311,12 +1110,6 @@ class MemoryAnalyzer:
):
return f"{_COMPONENT_PREFIX_LIB}{self._lib_hash_to_name[parts[1]]}"
# Native ESP8266 build: lib/<owner>__<Name>/... and the Arduino core in core/
if len(parts) >= 3 and parts[0] == "lib":
return f"{_COMPONENT_PREFIX_LIB}{parts[1].rsplit('__', 1)[-1].lower()}"
if len(parts) >= 2 and parts[0] == "core":
return _COMPONENT_ARDUINO_CORE
# Other framework/library files - return the first path component
# e.g., FrameworkArduino/... -> FrameworkArduino
return parts[0] if parts else source_file
@@ -1398,7 +1191,9 @@ class MemoryAnalyzer:
and other_mem is not None
):
other_mem.add_section_size(section_name, -size)
self._add_component_size(component, section_name, size)
if component not in self.components:
self.components[component] = ComponentMemory(component)
self.components[component].add_section_size(section_name, size)
# Sort by size descending
self._cswtch_symbols.sort(key=lambda x: x[1], reverse=True)
@@ -1410,209 +1205,6 @@ class MemoryAnalyzer:
total_size,
)
def _analyze_object_rodata(self) -> None:
"""Attribute .rodata without a symbol, which is RAM on ESP8266, to components."""
if not self.rodata_in_ram:
return
if (obj_dir := self._find_object_files_dir()) is None:
_LOGGER.warning(
"No object files next to the ELF, so constants in RAM are not listed"
)
return
objects = [str(f) for f in sorted(obj_dir.rglob("*.o"))]
result = run_tool([self.readelf_path, "-SW", "-sW", *objects])
if result is None:
_LOGGER.warning("readelf failed, so constants in RAM are not listed")
return
if result.returncode != 0:
# readelf still prints every object it could read
_LOGGER.warning(
"readelf could not read every object: %s", result.stderr.strip()
)
output = result.stdout
# readelf names each file only when given several
if len(objects) == 1:
output = f"File: {objects[0]}\n{output}"
self._attribute_unnamed_constants(output, obj_dir)
names = {n for c in self._unnamed_constants for n in c.functions}
self._batch_demangle_symbols(sorted(names - self._demangle_cache.keys()))
def _attribute_unnamed_constants(self, output: str, obj_dir: Path) -> None:
"""Attribute constants GCC copies into objects built from constant initializers."""
unnamed, definitions = _parse_unnamed_rodata(output)
# Inline (weak) functions are emitted, with their constants, in every
# object that uses them; the linker keeps one copy
inline_copies: dict[tuple[tuple[str, ...], int], str] = {}
for file_path, sections in unnamed.items():
rel_path = Path(file_path).relative_to(obj_dir).as_posix()
component = self._source_file_to_component(rel_path)
for constant in self._scan_object_constants(
file_path, sections, rel_path, component
):
# Only linked users can show which object's copy survived
live = [u for u in constant.users if u in self._elf_symbol_names]
if definitions.linked_copy_unknown(file_path, live):
continue
if definitions.weak[file_path].issuperset(live):
key = (tuple(live), constant.size)
if inline_copies.setdefault(key, file_path) != file_path:
continue
self._unnamed_constants.append(constant)
self._charge_ram(component, constant.size)
self._unnamed_constants.sort(key=lambda c: c.size, reverse=True)
def _scan_object_constants(
self,
file_path: str,
sections: dict[str, _RodataSection],
rel_path: str,
component: str,
) -> list[UnnamedConstant]:
"""Return the unnamed constants of one object that the link kept.
Args:
file_path: Object file to read
sections: Candidate sections by index
rel_path: Object path shown in the report
component: Component the object belongs to
"""
result = run_tool(
[self.readelf_path, "-rW"]
+ [arg for index in sections for arg in ("-x", index)]
+ [file_path]
)
if result is None or result.returncode != 0:
_LOGGER.warning(
"readelf failed reading constants of %s: %s",
file_path,
result.stderr.strip() if result else "not run",
)
return []
by_name = {section.name: section for section in sections.values()}
contents = _parse_hex_dumps(result.stdout)
# Every function using the section decides whether the link kept it
users: dict[str, set[str]] = defaultdict(set)
# Functions shown as using the unnamed bytes
functions: dict[str, set[str]] = defaultdict(set)
# (code section, offset, addend, target, function) into sections with named bytes
mixed: list[tuple[str, int, int, str, str]] = []
jump_tables: set[str] = set()
# Section -> (offset, string section, addend) of each pointer to a literal
pointers: dict[str, list[tuple[int, str, int]]] = defaultdict(list)
for section, offset, target, addend in _parse_relocations(result.stdout):
if target in by_name:
code = _CODE_SECTION_PATTERN.match(section)
if code and code.group(1):
users[target].add(code.group(1))
if by_name[target].covered:
mixed.append((section, offset, addend, target, code.group(1)))
else:
functions[target].add(code.group(1))
elif section in by_name and by_name[section].is_unnamed(offset):
if _CODE_SECTION_PATTERN.match(target):
jump_tables.add(section)
elif ".str" in target:
pointers[section].append((offset, target, addend))
self._add_mixed_section_users(file_path, mixed, by_name, functions)
strings = self._read_pointed_strings(file_path, pointers, contents)
return [
UnnamedConstant(
size=section.unnamed_size,
source_file=rel_path,
component=component,
functions=sorted(functions[name]),
jump_table=name in jump_tables,
data=bytes(
byte
for offset, byte in enumerate(contents[name])
if section.is_unnamed(offset)
)[:16],
strings=strings.get(name, []),
users=sorted(users[name]),
)
for name, section in by_name.items()
# Dropped by --gc-sections unless a function that uses it was kept
if not self._elf_symbol_names.isdisjoint(users[name])
]
def _add_mixed_section_users(
self,
file_path: str,
mixed: list[tuple[str, int, int, str, str]],
by_name: dict[str, _RodataSection],
functions: dict[str, set[str]],
) -> None:
"""Name only the functions that point at the unnamed bytes of a mixed section."""
if not mixed:
return
result = run_tool(
[self.readelf_path]
+ [arg for name in sorted({m[0] for m in mixed}) for arg in ("-x", name)]
+ [file_path]
)
code = _parse_hex_dumps(result.stdout) if result else {}
for section, offset, addend, target, function in mixed:
start = _pointer_target(code.get(section, b""), offset, addend)
# A pointer that can't be read, or lands outside the section, keeps the user
section_size = by_name[target].size
if (
start is None
or not 0 <= start < section_size
or by_name[target].is_unnamed(start)
):
functions[target].add(function)
def _read_pointed_strings(
self,
file_path: str,
pointers: dict[str, list[tuple[int, str, int]]],
contents: dict[str, bytearray],
) -> dict[str, list[str]]:
"""Resolve each constant's pointers to string literals, in offset order."""
if not pointers:
return {}
targets = sorted({t for refs in pointers.values() for _, t, _ in refs})
result = run_tool(
[self.readelf_path]
+ [arg for target in targets for arg in ("-x", target)]
+ [file_path]
)
if result is None or result.returncode != 0:
_LOGGER.warning(
"readelf failed reading strings of %s: %s",
file_path,
result.stderr.strip() if result else "not run",
)
return {}
literals = _parse_hex_dumps(result.stdout)
resolved: dict[str, list[str]] = {}
for name, refs in pointers.items():
texts = [
_pointed_literal(
contents[name], offset, addend, literals.get(target, b"")
)
for offset, target, addend in sorted(refs)
]
# One pointer that can't be read makes the list misleading; show bytes instead
if all(text is not None for text in texts):
resolved[name] = [t.decode("utf-8", "backslashreplace") for t in texts]
return resolved
def _charge_ram(self, component: str, size: int) -> None:
"""Charge RAM bytes that have no ELF symbol to a component."""
self._add_component_size(component, ".data", size)
if (data_section := self.sections.get(".data")) is not None:
data_section.symbol_size += size
def _add_component_size(self, component: str, section: str, size: int) -> None:
"""Add size to a component, creating it under its own name."""
if component not in self.components:
self.components[component] = ComponentMemory(component)
self.components[component].add_section_size(section, size)
def _analyze_function_calls(self) -> None:
"""Count function call sites by parsing disassembly output.
+1 -43
View File
@@ -5,7 +5,6 @@ from __future__ import annotations
from collections import defaultdict
from collections.abc import Callable
import heapq
import json
from operator import itemgetter
from pathlib import Path
import sys
@@ -205,39 +204,6 @@ class MemoryAnalyzerCLI(MemoryAnalyzer):
f"{i + 1:>2}. {size:>7,} B {section_label:<8} {demangled_display:<{self.COL_TOP_SYMBOL_NAME}} {component}"
)
def _add_unnamed_constants_analysis(self, lines: list[str]) -> None:
"""Add the unnamed constants (ESP8266 .rodata in RAM) section."""
self._add_section_header(lines, "Unnamed Constants in RAM (.rodata)")
total_size = sum(c.size for c in self._unnamed_constants)
lines.append(
f"Total: {len(self._unnamed_constants)} constant(s), {total_size:,} B. "
"GCC copies these into objects built from constant initializers;"
)
lines.append(
"PROGMEM tables, assigned fields or a user-provided constructor avoid the copy."
)
lines.append("")
for constant in self._unnamed_constants:
kind = "switch table" if constant.jump_table else "constant"
lines.append(
f"{constant.size:>6,} B {kind:<12} {constant.component} ({constant.source_file})"
)
lines.extend(
f"{'':>9} used by {self._demangle_symbol(function)}"
for function in constant.functions
)
if constant.strings:
# Escaped, so control characters in a literal can't disturb the terminal
shown = ", ".join(
json.dumps(text[:40]) for text in constant.strings[:4]
)
more = ", ..." if len(constant.strings) > 4 else ""
lines.append(f"{'':>9} points to {shown}{more}")
elif constant.data and not constant.jump_table:
more = " ..." if constant.size > len(constant.data) else ""
lines.append(f"{'':>9} bytes: {constant.data.hex(' ')}{more}")
lines.append("")
def _add_cswtch_analysis(self, lines: list[str]) -> None:
"""Add CSWTCH (GCC switch table lookup) analysis section."""
self._add_section_header(lines, "CSWTCH Analysis (GCC Switch Table Lookups)")
@@ -428,12 +394,6 @@ class MemoryAnalyzerCLI(MemoryAnalyzer):
f"{total_flash:>{self.COL_TOTAL_FLASH - 2},} B | {total_ram:>{self.COL_TOTAL_RAM - 2},} B"
)
if self.rodata_in_ram:
lines.append("")
lines.append(
"ESP8266 keeps .rodata in RAM, so constants are counted in RAM (data)."
)
# Show unattributed RAM (SDK/framework overhead)
unattributed_bss, unattributed_data, unattributed_total = (
self.get_unattributed_ram()
@@ -697,9 +657,6 @@ class MemoryAnalyzerCLI(MemoryAnalyzer):
lines.append(f" ... and {len(large_ram_syms) - 10} more")
lines.append("")
if self._unnamed_constants:
self._add_unnamed_constants_analysis(lines)
# CSWTCH (GCC switch table) analysis
if self._cswtch_symbols:
self._add_cswtch_analysis(lines)
@@ -781,6 +738,7 @@ def main():
build_dir = sys.argv[1]
# Load build directory
import json
from esphome.platformio.toolchain import IDEData
+7
View File
@@ -0,0 +1,7 @@
import esphome.codegen as cg
CODEOWNERS = ["@tpruvot"]
DEPENDENCIES = ["i2c"]
DOMAIN = "cst3240"
cst3240_ns = cg.esphome_ns.namespace("cst3240")
@@ -0,0 +1,14 @@
from esphome.components import cstxxx
from esphome.types import ConfigType
from .. import cst3240_ns
AUTO_LOAD = ["cstxxx"]
CST3240Touchscreen = cst3240_ns.class_("CST3240Touchscreen", cstxxx.CSTTouchscreen)
CONFIG_SCHEMA = cstxxx.cst_touchscreen_schema(CST3240Touchscreen, 0x5A)
async def to_code(config: ConfigType) -> None:
await cstxxx.new_cst_touchscreen(config)
@@ -0,0 +1,112 @@
#include "cst3240_touchscreen.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include <utility>
namespace esphome::cst3240 {
ESPHOME_LOG_TAG(TAG, "cst3240.touchscreen");
void CST3240Touchscreen::setup() {
if (this->reset_pin_ == nullptr) {
this->continue_setup_();
return;
}
pulse_reset(this->reset_pin_, {.high_before_ms = 5, .low_ms = 5});
this->set_timeout(30, [this] { this->continue_setup_(); });
}
void CST3240Touchscreen::continue_setup_() {
if (!this->enter_command_mode_())
return;
if (!this->setup_check(this->read_words_(cstxxx::REG_CHIP_INFO, this->project_id_, this->chip_id_),
LOG_STR("Failed to read chip ID")))
return;
// Only warn: the chip ID has not been confirmed on enough boards to fail setup on it
if (this->chip_id_ != CST3240_CHIP_ID) {
ESP_LOGW(TAG, "Unexpected chip ID 0x%04X", this->chip_id_);
}
this->read_words_(cstxxx::REG_FW_VERSION, this->fw_build_, this->fw_version_);
if (this->x_raw_max_ == this->x_raw_min_ || this->y_raw_max_ == this->y_raw_min_) {
uint16_t x_max, y_max;
if (this->read_words_(cstxxx::REG_RESOLUTION, x_max, y_max)) {
this->x_raw_max_ = x_max;
this->y_raw_max_ = y_max;
if (this->swap_x_y_)
std::swap(this->x_raw_max_, this->y_raw_max_);
} else {
this->x_raw_max_ = this->display_->get_native_width();
this->y_raw_max_ = this->display_->get_native_height();
}
}
this->status_comm_check(this->write_register16(cstxxx::REG_NORMAL_MODE, nullptr, 0) == i2c::ERROR_OK);
this->setup_interrupt_pin_(gpio::INTERRUPT_RISING_EDGE);
this->setup_complete_ = true;
}
void CST3240Touchscreen::update_touches() {
if (!this->setup_complete_) {
this->skip_update_ = true;
return;
}
uint8_t data[CST3240_REPORT_SIZE];
if (!this->status_comm_result(this->read_register16(cstxxx::REG_TOUCH_DATA, data, sizeof(data)) == i2c::ERROR_OK)) {
this->skip_update_ = true;
return;
}
const uint8_t sync = data[cstxxx::SYNC_IDX];
if (sync != cstxxx::SYNC_BYTE) {
this->skip_update_ = true;
if (sync != 0) {
ESP_LOGV(TAG, "Skipping report, sync byte 0x%02X", sync);
this->clear_report_();
}
return;
}
const uint8_t count = data[cstxxx::FINGER_COUNT_IDX] & 0x0F;
if (count <= CST3240_MAX_TOUCHES) {
for (uint8_t i = 0; i < count; i++) {
auto touch = cstxxx::decode_touch(data, i);
// A released finger is still listed once, with both press bits clear
if ((touch.event & TOUCH_EVENT_PRESSED) == 0)
continue;
this->add_raw_touch_position_(touch.id, touch.x, touch.y, touch.z);
}
}
if (this->interrupt_pin_ == nullptr) {
this->clear_report_();
} else if (!this->write_byte_reg16(cstxxx::REG_FINGER_COUNT, 0) ||
!this->write_byte_reg16(cstxxx::REG_TOUCH_DATA, cstxxx::SYNC_BYTE)) {
ESP_LOGW(TAG, "Failed to acknowledge the report");
}
}
void CST3240Touchscreen::clear_report_() {
// Built on the stack: write_register16() heap allocates a frame this long
uint8_t frame[2 + CST3240_REPORT_SIZE]{};
put_be16(frame, cstxxx::REG_TOUCH_DATA);
if (this->write(frame, sizeof(frame)) != i2c::ERROR_OK) {
ESP_LOGW(TAG, "Failed to clear the report");
}
}
void CST3240Touchscreen::dump_config() {
ESP_LOGCONFIG(TAG,
"CST3240 Touchscreen:\n"
" Chip ID: 0x%04X, Project ID: 0x%04X\n"
" FW version: %d.%d.%d\n"
" X/Y resolution: %d/%d",
this->chip_id_, this->project_id_, this->fw_version_ >> 8, this->fw_version_ & 0xFF, this->fw_build_,
this->x_raw_max_, this->y_raw_max_);
LOG_I2C_DEVICE(this);
LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
LOG_PIN(" Reset Pin: ", this->reset_pin_);
}
} // namespace esphome::cst3240
@@ -0,0 +1,31 @@
#pragma once
#include "esphome/components/cstxxx/cstxxx.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
namespace esphome::cst3240 {
static constexpr uint16_t CST3240_CHIP_ID = 0x3240;
static constexpr uint8_t CST3240_MAX_TOUCHES = 5;
static constexpr uint8_t CST3240_REPORT_SIZE = cstxxx::report_size(CST3240_MAX_TOUCHES);
static constexpr uint8_t TOUCH_EVENT_PRESSED = 0x06;
class CST3240Touchscreen : public cstxxx::CSTTouchscreen {
public:
void setup() override;
void dump_config() override;
protected:
void update_touches() override;
void continue_setup_();
/// Zeroes the touch report block, which polling relies on to tell new reports from old.
void clear_report_();
uint16_t chip_id_{};
uint16_t project_id_{};
uint16_t fw_version_{};
uint16_t fw_build_{};
};
} // namespace esphome::cst3240
+3 -1
View File
@@ -1,6 +1,5 @@
import esphome.codegen as cg
from esphome.components import logger
from esphome.components.nrf52.framework import include_west_project
from esphome.components.zephyr import zephyr_add_prj_conf
from esphome.config_helpers import filter_source_files_from_platform
import esphome.config_validation as cv
@@ -53,6 +52,9 @@ CONFIG_SCHEMA = cv.All(
async def to_code(config: ConfigType) -> None:
if CORE.using_zephyr:
# Imported here so configs for other platforms do not load the nrf52 package
from esphome.components.nrf52.framework import include_west_project
zephyr_add_prj_conf("HWINFO", True)
# gdb thread support
zephyr_add_prj_conf("DEBUG_THREAD_INFO", True)
+3 -1
View File
@@ -56,7 +56,6 @@ from esphome.const import (
from esphome.core import CORE, EsphomeError, HexInt
from esphome.core.config import BOARD_MAX_LENGTH
from esphome.coroutine import CoroPriority, coroutine_with_priority
from esphome.espidf import sbom
from esphome.espidf.component import IDFComponent, generate_idf_components
import esphome.final_validate as fv
from esphome.helpers import copy_file_if_changed, rmtree, write_file_if_changed
@@ -4033,6 +4032,9 @@ def copy_files():
CORE.relative_build_path("version.txt"),
__version__,
)
# Imported here: sbom loads git and urllib helpers that config validation does not need
from esphome.espidf import sbom
sbom.write_manifests()
# Remote extra build files are fetched into the shared download cache in
+15 -18
View File
@@ -347,26 +347,23 @@ bool FUSB302B::read_message_(PdMsg &msg, bool &is_sop) {
bool FUSB302B::send_message(const PdMsg &msg) {
// Register address, then the TX tokens; written in one transfer without the copy write_register() makes
uint8_t buf[1 + 4 + 1 + 2 + PD_MAX_NUM_DATA_OBJECTS * 4 + 4];
size_t len = 0;
uint8_t *p = buf;
buf[len++] = FUSB_FIFOS;
buf[len++] = FUSB_TX_TOKEN_SYNC1;
buf[len++] = FUSB_TX_TOKEN_SYNC1;
buf[len++] = FUSB_TX_TOKEN_SYNC1;
buf[len++] = FUSB_TX_TOKEN_SYNC2;
buf[len++] = FUSB_TX_TOKEN_PACKSYM | (msg.num_of_obj * 4 + 2);
put_le16(buf + len, msg.get_coded_header());
len += 2;
for (uint8_t i = 0; i < msg.num_of_obj; i++) {
put_le32(buf + len, msg.data_objects[i]);
len += 4;
}
buf[len++] = FUSB_TX_TOKEN_JAM_CRC;
buf[len++] = FUSB_TX_TOKEN_EOP;
buf[len++] = FUSB_TX_TOKEN_TXOFF;
buf[len++] = FUSB_TX_TOKEN_TXON;
*p++ = FUSB_FIFOS;
*p++ = FUSB_TX_TOKEN_SYNC1;
*p++ = FUSB_TX_TOKEN_SYNC1;
*p++ = FUSB_TX_TOKEN_SYNC1;
*p++ = FUSB_TX_TOKEN_SYNC2;
*p++ = FUSB_TX_TOKEN_PACKSYM | (msg.num_of_obj * 4 + 2);
p = put_le16(p, msg.get_coded_header());
for (uint8_t i = 0; i < msg.num_of_obj; i++)
p = put_le32(p, msg.data_objects[i]);
*p++ = FUSB_TX_TOKEN_JAM_CRC;
*p++ = FUSB_TX_TOKEN_EOP;
*p++ = FUSB_TX_TOKEN_TXOFF;
*p++ = FUSB_TX_TOKEN_TXON;
i2c::ErrorCode err = this->write(buf, len);
i2c::ErrorCode err = this->write(buf, p - buf);
if (err != i2c::ERROR_OK) {
ESP_LOGW(TAG, "Sending message type %u failed (error %d)", msg.type, static_cast<int>(err));
return false;
+303
View File
@@ -0,0 +1,303 @@
from collections.abc import Callable
from esphome import automation
import esphome.codegen as cg
from esphome.components import i2c
from esphome.components.const import CONF_CHARGE_CURRENT, CONF_ENABLED
import esphome.config_validation as cv
from esphome.const import CONF_DURATION, CONF_ID, CONF_MODE, CONF_VOLTAGE
from esphome.types import ConfigType
CODEOWNERS = ["@tomaszduda23"]
DEPENDENCIES = ["i2c"]
DOMAIN = "npm1300"
MULTI_CONF = True
CONF_NPM1300_ID = "npm1300_id"
CONF_VBUS_CURRENT_LIMIT = "vbus_current_limit"
CONF_THERMISTOR_RESISTANCE = "thermistor_resistance"
CONF_THERMISTOR_BETA = "thermistor_beta"
CONF_CHARGER = "charger"
CONF_TERMINATION_VOLTAGE = "termination_voltage"
CONF_WARM_TERMINATION_VOLTAGE = "warm_termination_voltage"
CONF_TERMINATION_CURRENT = "termination_current"
CONF_TRICKLE_VOLTAGE = "trickle_voltage"
CONF_DISCHARGE_CURRENT_LIMIT = "discharge_current_limit"
CONF_RECHARGE = "recharge"
CONF_BUCK1 = "buck1"
CONF_BUCK2 = "buck2"
CONF_LDO1 = "ldo1"
CONF_LDO2 = "ldo2"
npm1300_ns = cg.esphome_ns.namespace("npm1300")
NPM1300Component = npm1300_ns.class_(
"NPM1300Component", cg.PollingComponent, i2c.I2CDevice
)
NPM1300Output = npm1300_ns.enum("NPM1300Output", is_class=True)
NPM1300OutputConfig = npm1300_ns.struct("NPM1300OutputConfig")
NPM1300ChargerConfig = npm1300_ns.struct("NPM1300ChargerConfig")
NPM1300Config = npm1300_ns.struct("NPM1300Config")
NPM1300_CHILD_SCHEMA = cv.Schema(
{cv.GenerateID(CONF_NPM1300_ID): cv.use_id(NPM1300Component)}
)
# Status registers read on each update: VBUSINSTATUS, BCHGCHARGESTATUS and BCHGERRREASON
STATUS_PUBLISHER = cg.HubFunction(
"status",
cg.void,
[
(cg.uint8, "vbus_status"),
(cg.uint8, "charge_status"),
(cg.uint8, "error_reason"),
],
define="USE_NPM1300_STATUS_PUBLISHER",
)
VBUS_STATUS = STATUS_PUBLISHER.param("vbus_status")
CHARGE_STATUS = STATUS_PUBLISHER.param("charge_status")
ERROR_REASON = STATUS_PUBLISHER.param("error_reason")
# The ADC results and the amperes per code of the battery current
DATA_PUBLISHER = cg.HubFunction(
"data",
cg.void,
[(cg.uint8.operator("const").operator("ptr"), "adc"), (cg.float_, "ibat_scale")],
define="USE_NPM1300_DATA_PUBLISHER",
)
ADC = DATA_PUBLISHER.param("adc")
IBAT_SCALE = DATA_PUBLISHER.param("ibat_scale")
# The dump lines of the children decoded by the two functions above
DUMP = cg.HubFunction("dump", cg.void, [(cg.const_char_ptr, "tag")])
DUMP_TAG = DUMP.param("tag")
# Config key of each regulator and its NPM1300Output enumerator
OUTPUTS = NPM1300Output.options(
CONF_BUCK1, CONF_BUCK2, CONF_LDO1, CONF_LDO2, prefix="NPM1300_OUTPUT_"
)
# Written for a field the YAML leaves out
NPM1300_UNSET = 0xFF
# ADCNTCRSEL codes, by kiloohms
THERMISTOR_RESISTANCES = {10: 1, 47: 2, 100: 3}
LDO_MODES = {"load_switch": 0, "ldo": 1}
TERMINATION_CURRENTS = {"10%": 0, "20%": 1}
# BCHGVTRICKLESEL, by millivolts
TRICKLE_VOLTAGES = {2900: 0, 2500: 1}
# BCHGISETDISCHARGE codes, by milliamperes
DISCHARGE_CURRENT_LIMITS = {200: 84, 1000: 415}
# BCHGVTERM: 3.50 V to 3.65 V, then 4.00 V to 4.45 V, both in 50 mV steps
TERMINATION_VOLTAGES = [3500 + 50 * i for i in range(4)] + [
4000 + 50 * i for i in range(10)
]
# The wake-up timer counts 16 ms ticks in a 24-bit register
HIBERNATE_TICK_MS = 16
HIBERNATE_MAX_TICKS = 0xFFFFFF
def _millis(value: float) -> int:
return round(value * 1000)
def _one_of_units(
validator: Callable[[object], float],
allowed: list[int],
unit: str,
scale: float = 1000,
) -> Callable[[object], float]:
"""Validate a quantity with `validator` and accept only the listed values, given in units * `scale`."""
def validate(value: object) -> float:
value = validator(value)
if round(value * scale) not in allowed:
options = ", ".join(f"{v / scale:g}{unit}" for v in allowed)
raise cv.Invalid(f"Must be one of {options}")
return value
return validate
def _stepped(
validator: Callable[[object], float],
minimum: int,
maximum: int,
step: int,
unit: str,
) -> Callable[[object], float]:
"""Validate a quantity in a range of milli-units, in steps of `step`."""
allowed = list(range(minimum, maximum + 1, step))
in_range = cv.All(validator, cv.Range(min=minimum / 1000, max=maximum / 1000))
def validate(value: object) -> float:
value = in_range(value)
if _millis(value) not in allowed:
raise cv.Invalid(f"Must be a multiple of {step}m{unit}")
return value
return validate
OUTPUT_VOLTAGE = _stepped(cv.voltage, 1000, 3300, 100, "V")
VBUS_CURRENT_LIMIT = _stepped(cv.current, 100, 1500, 100, "A")
BUCK_SCHEMA = cv.Schema(
{
cv.Optional(CONF_VOLTAGE): OUTPUT_VOLTAGE,
cv.Optional(CONF_ENABLED): cv.boolean,
}
)
LDO_SCHEMA = BUCK_SCHEMA.extend(
{
cv.Optional(CONF_MODE): cv.enum(LDO_MODES, lower=True),
}
)
CHARGER_SCHEMA = cv.Schema(
{
cv.Required(CONF_CHARGE_CURRENT): _stepped(cv.current, 32, 800, 2, "A"),
cv.Optional(CONF_TERMINATION_VOLTAGE, default="4.2V"): _one_of_units(
cv.voltage, TERMINATION_VOLTAGES, "V"
),
cv.Optional(CONF_WARM_TERMINATION_VOLTAGE): _one_of_units(
cv.voltage, TERMINATION_VOLTAGES, "V"
),
cv.Optional(CONF_TERMINATION_CURRENT, default="10%"): cv.enum(
TERMINATION_CURRENTS
),
cv.Optional(CONF_TRICKLE_VOLTAGE, default="2.9V"): _one_of_units(
cv.voltage, list(TRICKLE_VOLTAGES), "V"
),
cv.Optional(CONF_DISCHARGE_CURRENT_LIMIT, default="1000mA"): _one_of_units(
cv.current, list(DISCHARGE_CURRENT_LIMITS), "A"
),
cv.Optional(CONF_RECHARGE, default=True): cv.boolean,
cv.Optional(CONF_ENABLED, default=True): cv.boolean,
}
)
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(NPM1300Component),
cv.Optional(CONF_VBUS_CURRENT_LIMIT): VBUS_CURRENT_LIMIT,
cv.Optional(CONF_THERMISTOR_RESISTANCE): _one_of_units(
cv.resistance, list(THERMISTOR_RESISTANCES), "kOhm", scale=0.001
),
cv.Optional(CONF_THERMISTOR_BETA, default=3380): cv.int_range(
min=1, max=65535
),
cv.Optional(CONF_CHARGER): CHARGER_SCHEMA,
cv.Optional(CONF_BUCK1): BUCK_SCHEMA,
cv.Optional(CONF_BUCK2): BUCK_SCHEMA,
cv.Optional(CONF_LDO1): LDO_SCHEMA,
cv.Optional(CONF_LDO2): LDO_SCHEMA,
}
)
.extend(cv.polling_component_schema("60s"))
.extend(i2c.i2c_device_schema(0x6B))
)
def _termination_voltage_code(volts: float) -> int:
return TERMINATION_VOLTAGES.index(_millis(volts))
def _vbus_current_limit_code(amps: float) -> int:
"""VBUSINILIM0: 100 mA per step of the code."""
return _millis(amps) // 100
def _charger_config(config: ConfigType) -> cg.StructInitializer:
termination = _termination_voltage_code(config[CONF_TERMINATION_VOLTAGE])
warm = config.get(CONF_WARM_TERMINATION_VOLTAGE)
return cg.StructInitializer(
NPM1300ChargerConfig,
("current", _millis(config[CONF_CHARGE_CURRENT]) // 2),
(
"discharge_limit",
DISCHARGE_CURRENT_LIMITS[_millis(config[CONF_DISCHARGE_CURRENT_LIMIT])],
),
("termination_voltage", termination),
(
"warm_termination_voltage",
termination if warm is None else _termination_voltage_code(warm),
),
("trickle_voltage", TRICKLE_VOLTAGES[_millis(config[CONF_TRICKLE_VOLTAGE])]),
("termination_current", TERMINATION_CURRENTS[config[CONF_TERMINATION_CURRENT]]),
("recharge", config[CONF_RECHARGE]),
("enabled", config[CONF_ENABLED]),
)
def _output_config(config: ConfigType | None) -> cg.StructInitializer:
config = config or {}
voltage = config.get(CONF_VOLTAGE)
mode = config.get(CONF_MODE)
enabled = config.get(CONF_ENABLED)
return cg.StructInitializer(
NPM1300OutputConfig,
(
"voltage",
NPM1300_UNSET if voltage is None else (_millis(voltage) - 1000) // 100,
),
("ldo_mode", NPM1300_UNSET if mode is None else LDO_MODES[mode]),
("enabled", NPM1300_UNSET if enabled is None else int(enabled)),
)
def _config(config: ConfigType) -> cg.StructInitializer:
charger = config.get(CONF_CHARGER)
resistance = config.get(CONF_THERMISTOR_RESISTANCE)
return cg.StructInitializer(
NPM1300Config,
(
"outputs",
cg.ArrayInitializer(*(_output_config(config.get(key)) for key in OUTPUTS)),
),
("charger", None if charger is None else _charger_config(charger)),
("thermistor_beta", config[CONF_THERMISTOR_BETA]),
(
"thermistor",
NPM1300_UNSET
if resistance is None
else THERMISTOR_RESISTANCES[round(resistance / 1000)],
),
("charger_configured", charger is not None),
)
async def to_code(config: ConfigType) -> None:
# In flash: only setup() and dump_config() read it
table = cg.shared_progmem_array(
"npm1300_config", NPM1300Config, cg.ArrayInitializer(_config(config))
)
var = await i2c.new_i2c_component(config, table)
if (limit := config.get(CONF_VBUS_CURRENT_LIMIT)) is not None:
cg.add(var.set_vbus_current_limit(_vbus_current_limit_code(limit)))
automation.register_apply_action(
"npm1300.enter_ship_mode",
automation.maybe_simple_id({cv.Required(CONF_ID): cv.use_id(NPM1300Component)}),
automation.ApplyCall("enter_ship_mode()"),
)
automation.register_apply_action(
"npm1300.hibernate",
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(NPM1300Component),
cv.Required(CONF_DURATION): cv.templatable(
cv.All(
cv.positive_time_period_milliseconds,
cv.Range(
min=cv.TimePeriod(milliseconds=HIBERNATE_TICK_MS),
max=cv.TimePeriod(
milliseconds=HIBERNATE_TICK_MS * HIBERNATE_MAX_TICKS
),
),
)
),
}
),
automation.ApplyField(CONF_DURATION, "hibernate", cg.uint32),
)
+106
View File
@@ -0,0 +1,106 @@
from dataclasses import dataclass
from esphome.automation import parent_ref
import esphome.codegen as cg
from esphome.components import binary_sensor
import esphome.config_validation as cv
from esphome.const import (
DEVICE_CLASS_BATTERY_CHARGING,
DEVICE_CLASS_POWER,
DEVICE_CLASS_PROBLEM,
ENTITY_CATEGORY_DIAGNOSTIC,
)
from esphome.cpp_generator import HexIntLiteral
from esphome.types import ConfigType
from . import (
CHARGE_STATUS,
CONF_NPM1300_ID,
DUMP,
DUMP_TAG,
ERROR_REASON,
NPM1300_CHILD_SCHEMA,
STATUS_PUBLISHER,
VBUS_STATUS,
)
DEPENDENCIES = ["npm1300"]
@dataclass(frozen=True, kw_only=True)
class _StatusBits:
"""On when any bit of mask is set in the status register."""
key: str
register: cg.MockObj
mask: int
name: str
device_class: str = cv.UNDEFINED
entity_category: str = cv.UNDEFINED
_FIELDS = (
_StatusBits(
key="vbus_present",
register=VBUS_STATUS,
mask=0x01,
name="VBUS Present",
device_class=DEVICE_CLASS_POWER,
),
_StatusBits(
key="vbus_current_limit_active",
register=VBUS_STATUS,
mask=0x02,
name="VBUS Current Limit Active",
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
# Trickle, constant current or constant voltage charging
_StatusBits(
key="charging",
register=CHARGE_STATUS,
mask=0x1C,
name="Charging",
device_class=DEVICE_CLASS_BATTERY_CHARGING,
),
_StatusBits(
key="charge_complete",
register=CHARGE_STATUS,
mask=0x02,
name="Charge Complete",
),
_StatusBits(
key="charger_error",
register=ERROR_REASON,
mask=0xFF,
name="Charger Error",
device_class=DEVICE_CLASS_PROBLEM,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
)
CONFIG_SCHEMA = NPM1300_CHILD_SCHEMA.extend(
{
cv.Optional(field.key): binary_sensor.binary_sensor_schema(
device_class=field.device_class, entity_category=field.entity_category
)
for field in _FIELDS
}
)
async def to_code(config: ConfigType) -> None:
hub = await cg.get_variable(config[CONF_NPM1300_ID])
sensors = binary_sensor.sub_binary_sensors(config)
for field in _FIELDS:
if (var := await sensors(field.key, None)) is not None:
state = (field.register & HexIntLiteral(field.mask)) != 0
STATUS_PUBLISHER.add(hub, parent_ref(var).publish_state(state))
DUMP.add(
hub,
binary_sensor.binary_sensor_ns.log_binary_sensor(
DUMP_TAG,
" ",
cg.RawExpression(f'LOG_STR_LITERAL("{field.name}")'),
parent_ref(var),
),
)
+483
View File
@@ -0,0 +1,483 @@
#include "npm1300.h"
#include <algorithm>
#include <cinttypes>
#include <cmath>
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
#ifdef USE_TEXT_SENSOR
#include "esphome/core/progmem.h"
#endif
namespace esphome::npm1300 {
ESPHOME_LOG_TAG(TAG, "npm1300");
// Registers: the base address of the peripheral in the high byte, the offset in the low byte
static constexpr uint16_t REG_VBUSIN_TASK_UPDATE_ILIM = 0x0200;
static constexpr uint16_t REG_VBUSIN_ILIM0 = 0x0201;
static constexpr uint16_t REG_VBUSIN_ILIM_STARTUP = 0x0202;
static constexpr uint16_t REG_VBUSIN_STATUS = 0x0207;
static constexpr uint16_t REG_BCHG_TASK_RELEASE_ERR = 0x0300;
static constexpr uint16_t REG_BCHG_TASK_CLEAR_CHG_ERR = 0x0301;
static constexpr uint16_t REG_BCHG_ENABLE_SET = 0x0304;
static constexpr uint16_t REG_BCHG_ENABLE_CLR = 0x0305;
static constexpr uint16_t REG_BCHG_DISABLE_SET = 0x0306;
static constexpr uint16_t REG_BCHG_DISABLE_CLR = 0x0307;
static constexpr uint16_t REG_BCHG_ISET_MSB = 0x0308;
static constexpr uint16_t REG_BCHG_ISET_DISCHARGE_MSB = 0x030A;
static constexpr uint16_t REG_BCHG_VTERM = 0x030C;
static constexpr uint16_t REG_BCHG_VTERM_R = 0x030D;
static constexpr uint16_t REG_BCHG_VTRICKLE_SEL = 0x030E;
static constexpr uint16_t REG_BCHG_ITERM_SEL = 0x030F;
static constexpr uint16_t REG_BCHG_CHARGE_STATUS = 0x0334;
static constexpr uint16_t REG_BCHG_ERR_REASON = 0x0336;
static constexpr uint16_t REG_BUCK1_ENA_SET = 0x0400;
static constexpr uint16_t REG_BUCK1_NORM_VOUT = 0x0408;
static constexpr uint16_t REG_BUCK_SW_CTRL_SEL = 0x040F;
static constexpr uint16_t REG_BUCK_STATUS = 0x0434;
static constexpr uint16_t REG_ADC_TASK_VBAT = 0x0500;
static constexpr uint16_t REG_ADC_TASK_VBUS = 0x0507;
static constexpr uint16_t REG_ADC_NTC_R_SEL = 0x050A;
static constexpr uint16_t REG_ADC_TASK_AUTO_TIM_UPDATE = 0x050C;
static constexpr uint16_t REG_ADC_RESULTS = 0x0510;
static constexpr uint16_t REG_ADC_IBAT_MEAS_EN = 0x0524;
static constexpr uint16_t REG_TIMER_TARGET_STROBE = 0x0703;
static constexpr uint16_t REG_TIMER_CONFIG = 0x0705;
static constexpr uint16_t REG_TIMER_HI_BYTE = 0x0708;
static constexpr uint16_t REG_LDSW1_TASK_SET = 0x0800;
static constexpr uint16_t REG_LDSW_STATUS = 0x0804;
static constexpr uint16_t REG_LDSW1_LDO_SEL = 0x0808;
static constexpr uint16_t REG_LDSW1_VOUT_SEL = 0x080C;
static constexpr uint16_t REG_SHIP_TASK_HIBERNATE = 0x0B00;
static constexpr uint16_t REG_SHIP_TASK_SHIP_MODE = 0x0B02;
// VBUSINSTATUS
static constexpr uint8_t VBUS_PRESENT = 0x01;
// BCHGCHARGESTATUS; bit 0 (battery detected) is reserved since C00 silicon
static constexpr uint8_t CHARGE_STATUS_COMPLETED = 0x02;
static constexpr uint8_t CHARGE_STATUS_TRICKLE = 0x04;
static constexpr uint8_t CHARGE_STATUS_CONSTANT_CURRENT = 0x08;
static constexpr uint8_t CHARGE_STATUS_CONSTANT_VOLTAGE = 0x10;
static constexpr uint8_t CHARGE_STATUS_DIE_TEMP_HIGH = 0x40;
// BCHGDISABLESET and BCHGDISABLECLR
static constexpr uint8_t CHARGER_DISABLE_RECHARGE = 0x01;
static constexpr uint8_t CHARGER_DISABLE_NTC = 0x02;
// BUCKSTATUS power good, LDSWSTATUS powered up as load switch or as LDO
static constexpr uint8_t BUCK_POWER_GOOD[2] = {0x04, 0x40};
static constexpr uint8_t LDSW_POWERED[2] = {0x03, 0x0C};
// TIMERCONFIG: wake-up timer with the 16 ms prescaler
static constexpr uint8_t TIMER_CONFIG_WAKEUP = 0x04;
static constexpr uint32_t TIMER_TICK_MS = 16;
static constexpr uint32_t TIMER_MAX_TICKS = 0xFFFFFF;
// BCHGISETDISCHARGE codes up to this one select the 200 mA discharge limit, the others 1000 mA
static constexpr uint16_t DISCHARGE_LIMIT_200MA_CODE = 84;
static uint16_t discharge_limit_ma(uint16_t code) { return code <= DISCHARGE_LIMIT_200MA_CODE ? 200 : 1000; }
// BCHGISET and BCHGISETDISCHARGE hold a 9-bit code: the high 8 bits, then bit 0 in the next register
static void split_9bit(uint16_t code, uint8_t *out) {
out[0] = code >> 1;
out[1] = code & 1;
}
#ifdef USE_NPM1300_DATA_PUBLISHER
static uint16_t join_9bit(const uint8_t *in) { return (in[0] << 1) | (in[1] & 1); }
// ADCIBATMEASSTATUS
static constexpr uint8_t IBAT_MEAS_INVALID = 0x10;
static constexpr uint8_t IBAT_MODE_SHIFT = 2;
static constexpr uint8_t IBAT_MODE_DISCHARGE = 0x01;
static constexpr uint8_t IBAT_MODE_CHARGE = 0x03;
// Full scale of the battery current measurement per milliampere of the charge and discharge limits
static constexpr float CHARGE_FULL_SCALE_FACTOR = 1.25f;
static constexpr float DISCHARGE_FULL_SCALE_FACTOR = 1.12f;
static constexpr float ADC_MAX_CODE = 1023.0f;
// Five conversions of 250 us each; the loop runs the timeout later than this anyway
static constexpr uint32_t CONVERSION_TIME_MS = 2;
static constexpr uint32_t READ_TIMEOUT_ID = 0;
float ntc_temperature(uint16_t code, float inv_beta) {
if (code == 0)
return NAN;
// The NTC divider gives code = 1024 * R / (R + R_nominal)
return 1.0f / (1.0f / 298.15f - logf(1024.0f / code - 1.0f) * inv_beta) - 273.15f;
}
#endif
#ifdef USE_TEXT_SENSOR
PROGMEM_STRING_TABLE(ChargerStatusStrings, "Error", "Paused, Die Too Hot", "Charge Complete", "Trickle Charge",
"Constant Current", "Constant Voltage", "Discharging", "Not Charging");
// BCHGCHARGESTATUS bits in order of priority; bit i is reported as string i + 1
static const uint8_t CHARGER_STATUS_BITS[] PROGMEM = {CHARGE_STATUS_DIE_TEMP_HIGH, CHARGE_STATUS_COMPLETED,
CHARGE_STATUS_TRICKLE, CHARGE_STATUS_CONSTANT_CURRENT,
CHARGE_STATUS_CONSTANT_VOLTAGE};
static uint8_t charger_status_index(uint8_t vbus_status, uint8_t charge_status, uint8_t error_reason) {
if (error_reason != 0)
return 0;
for (size_t i = 0; i < sizeof(CHARGER_STATUS_BITS); i++) {
if ((charge_status & progmem_read_byte(&CHARGER_STATUS_BITS[i])) != 0)
return i + 1;
}
return (vbus_status & VBUS_PRESENT) == 0 ? 6 : 7;
}
void publish_charger_status(text_sensor::TextSensor *sensor, uint8_t &last, uint8_t vbus_status, uint8_t charge_status,
uint8_t error_reason) {
const uint8_t index = charger_status_index(vbus_status, charge_status, error_reason);
if (index == last)
return;
last = index;
sensor->publish_state(ChargerStatusStrings::get_progmem_str(index, ChargerStatusStrings::LAST_INDEX));
}
#endif
NPM1300Config NPM1300Component::load_config_() const {
NPM1300Config config;
progmem_memcpy(&config, this->config_, sizeof(config));
return config;
}
void NPM1300Component::setup() {
uint8_t status;
if (!this->setup_check(this->read_byte_reg16(REG_BCHG_CHARGE_STATUS, &status)))
return;
// Raise the VBUS current limit before anything turns on a regulator: at the power-on limit of 100 mA the
// inrush current of a regulator can trip the over-current protection and brown out the board
if (this->vbus_current_limit_ != NPM1300_UNSET &&
!this->setup_check(this->write_byte_reg16(REG_VBUSIN_ILIM_STARTUP, this->vbus_current_limit_) &&
this->apply_vbus_current_limit_()))
return;
const NPM1300Config config = this->load_config_();
if (config.thermistor != NPM1300_UNSET &&
!this->setup_check(this->write_byte_reg16(REG_ADC_NTC_R_SEL, config.thermistor)))
return;
if (config.charger_configured && !this->setup_check(this->configure_charger_(config)))
return;
for (uint8_t i = 0; i < NPM1300_OUTPUT_COUNT; i++) {
if (!this->setup_check(this->configure_output_(i, config.outputs[i])))
return;
}
#ifdef USE_NPM1300_DATA_PUBLISHER
if (this->data_publisher_ != nullptr) {
// The battery current is measured after every battery voltage measurement
if (!this->setup_check(this->write_byte_reg16(REG_ADC_IBAT_MEAS_EN, 1)))
return;
if (config.charger_configured) {
this->charge_code_ = config.charger.current;
this->discharge_limit_high_ = discharge_limit_ma(config.charger.discharge_limit) == 1000;
} else if (!this->setup_check(this->read_charge_currents_())) {
return;
}
}
#endif
this->update();
}
bool NPM1300Component::apply_vbus_current_limit_() {
return this->write_byte_reg16(REG_VBUSIN_ILIM0, this->vbus_current_limit_) &&
this->write_byte_reg16(REG_VBUSIN_TASK_UPDATE_ILIM, 1);
}
bool NPM1300Component::configure_charger_(const NPM1300Config &config) {
const NPM1300ChargerConfig &charger = config.charger;
// The charge settings are only taken while the charger is off
if (!this->write_byte_reg16(REG_BCHG_ENABLE_CLR, 1))
return false;
uint8_t current[2];
uint8_t discharge[2];
split_9bit(charger.current, current);
split_9bit(charger.discharge_limit, discharge);
uint8_t disable = charger.recharge ? 0 : CHARGER_DISABLE_RECHARGE;
// Without a thermistor the charger reports an NTC error and does not charge
if (config.thermistor == NPM1300_UNSET)
disable |= CHARGER_DISABLE_NTC;
const uint8_t enable = (CHARGER_DISABLE_RECHARGE | CHARGER_DISABLE_NTC) & ~disable;
if (this->write_register16(REG_BCHG_ISET_MSB, current, sizeof(current)) != i2c::ERROR_OK ||
this->write_register16(REG_BCHG_ISET_DISCHARGE_MSB, discharge, sizeof(discharge)) != i2c::ERROR_OK ||
!this->write_byte_reg16(REG_BCHG_VTERM, charger.termination_voltage) ||
!this->write_byte_reg16(REG_BCHG_VTERM_R, charger.warm_termination_voltage) ||
!this->write_byte_reg16(REG_BCHG_VTRICKLE_SEL, charger.trickle_voltage) ||
!this->write_byte_reg16(REG_BCHG_ITERM_SEL, charger.termination_current) ||
!this->write_byte_reg16(REG_BCHG_DISABLE_SET, disable) || !this->write_byte_reg16(REG_BCHG_DISABLE_CLR, enable) ||
// Measure the battery temperature automatically while charging
!this->write_byte_reg16(REG_ADC_TASK_AUTO_TIM_UPDATE, 1))
return false;
return !charger.enabled || this->set_charger_enabled(true);
}
bool NPM1300Component::configure_output_(uint8_t index, const NPM1300OutputConfig &config) {
const uint8_t channel = index & 1;
if (index < 2) {
// The software voltage replaces the one set by the VSET pin only once its SWCTRLSEL bit is set
if (config.voltage != NPM1300_UNSET &&
(!this->write_byte_reg16(REG_BUCK1_NORM_VOUT + channel * 2, config.voltage) ||
!this->update_register16(REG_BUCK_SW_CTRL_SEL, 1 << channel, 1 << channel)))
return false;
} else if ((config.voltage != NPM1300_UNSET &&
!this->write_byte_reg16(REG_LDSW1_VOUT_SEL + channel, config.voltage)) ||
(config.ldo_mode != NPM1300_UNSET &&
!this->write_byte_reg16(REG_LDSW1_LDO_SEL + channel, config.ldo_mode))) {
return false;
}
return config.enabled == NPM1300_UNSET ||
this->set_output_enabled(static_cast<NPM1300Output>(index), config.enabled != 0);
}
#ifdef USE_NPM1300_DATA_PUBLISHER
bool NPM1300Component::read_charge_currents_() {
uint8_t charge[2];
uint8_t discharge[2];
if (this->read_register16(REG_BCHG_ISET_MSB, charge, sizeof(charge)) != i2c::ERROR_OK ||
this->read_register16(REG_BCHG_ISET_DISCHARGE_MSB, discharge, sizeof(discharge)) != i2c::ERROR_OK)
return false;
this->charge_code_ = join_9bit(charge);
this->discharge_limit_high_ = discharge_limit_ma(join_9bit(discharge)) == 1000;
return true;
}
#endif
bool NPM1300Component::set_charger_enabled(bool enabled) {
if (this->is_failed())
return false;
if (!enabled)
return this->write_byte_reg16(REG_BCHG_ENABLE_CLR, 1);
// A charger error stops the charger until the error is cleared and the charger released
return this->write_byte_reg16(REG_BCHG_TASK_CLEAR_CHG_ERR, 1) &&
this->write_byte_reg16(REG_BCHG_TASK_RELEASE_ERR, 1) && this->write_byte_reg16(REG_BCHG_ENABLE_SET, 1);
}
bool NPM1300Component::set_output_enabled(NPM1300Output output, bool enabled) {
if (this->is_failed())
return false;
const uint8_t index = static_cast<uint8_t>(output);
// A pair of SET and CLR task registers per output
const uint16_t base = index < 2 ? REG_BUCK1_ENA_SET : REG_LDSW1_TASK_SET;
const uint16_t reg = base + (index & 1) * 2 + (enabled ? 0 : 1);
if (!this->write_byte_reg16(reg, 1)) {
ESP_LOGW(TAG, "Turning output %u %s failed", index, enabled ? LOG_STR_LITERAL("on") : LOG_STR_LITERAL("off"));
return false;
}
return true;
}
void NPM1300Component::enter_ship_mode() {
ESP_LOGI(TAG, "Entering ship mode");
this->write_byte_reg16(REG_SHIP_TASK_SHIP_MODE, 1);
}
void NPM1300Component::hibernate(uint32_t duration_ms) {
// A zero period would never wake the chip
const uint32_t ticks = std::clamp<uint32_t>(duration_ms / TIMER_TICK_MS, 1, TIMER_MAX_TICKS);
uint8_t timer[3];
put_be24(timer, ticks);
ESP_LOGI(TAG, "Hibernating for %" PRIu32 " ms", ticks * TIMER_TICK_MS);
if (!this->write_byte_reg16(REG_TIMER_CONFIG, TIMER_CONFIG_WAKEUP) ||
this->write_register16(REG_TIMER_HI_BYTE, timer, sizeof(timer)) != i2c::ERROR_OK ||
!this->write_byte_reg16(REG_TIMER_TARGET_STROBE, 1)) {
ESP_LOGW(TAG, "Setting the wake-up timer failed");
return;
}
// The timer needs a moment to load the new period
delay(1);
this->write_byte_reg16(REG_SHIP_TASK_HIBERNATE, 1);
}
void NPM1300Component::update() {
uint8_t vbus_status;
uint8_t charge_status;
uint8_t error_reason;
if (!this->status_comm_check(this->read_byte_reg16(REG_VBUSIN_STATUS, &vbus_status) &&
this->read_byte_reg16(REG_BCHG_CHARGE_STATUS, &charge_status) &&
this->read_byte_reg16(REG_BCHG_ERR_REASON, &error_reason)))
return;
// The chip goes back to its default VBUS current limit whenever VBUS is removed
const bool vbus_present = (vbus_status & VBUS_PRESENT) != 0;
if (vbus_present && !this->vbus_present_ && this->vbus_current_limit_ != NPM1300_UNSET &&
!this->status_comm_check(this->apply_vbus_current_limit_()))
return;
this->vbus_present_ = vbus_present;
if (error_reason != this->last_error_reason_) {
this->last_error_reason_ = error_reason;
if (error_reason != 0) {
ESP_LOGW(TAG, "Charger error, BCHGERRREASON 0x%02X", error_reason);
}
}
#ifdef USE_NPM1300_STATUS_PUBLISHER
if (this->status_publisher_ != nullptr)
this->status_publisher_(vbus_status, charge_status, error_reason);
#endif
#ifdef USE_SWITCH
if (!this->status_comm_check(this->publish_switches_()))
return;
#endif
#ifdef USE_SENSOR
if (this->vbus_current_limit_sensor_ != nullptr) {
uint8_t code;
if (!this->status_comm_check(this->read_byte_reg16(REG_VBUSIN_ILIM0, &code)))
return;
// 100 mA per step of the code, but code 0 is 500 mA
const uint16_t milliamps = code == 0 ? 500 : code * 100;
this->vbus_current_limit_sensor_->publish_state(milliamps / 1000.0f);
}
#endif
#ifdef USE_NPM1300_DATA_PUBLISHER
if (this->data_publisher_ != nullptr) {
// Start the battery voltage (followed by the battery current), NTC, die temperature and system voltage
// conversions, then the VBUS one
static constexpr uint8_t START[4] = {1, 1, 1, 1};
if (!this->status_comm_check(this->write_register16(REG_ADC_TASK_VBAT, START, sizeof(START)) == i2c::ERROR_OK &&
this->write_byte_reg16(REG_ADC_TASK_VBUS, 1)))
return;
this->set_timeout(READ_TIMEOUT_ID, CONVERSION_TIME_MS, [this]() { this->read_measurements_(); });
return;
}
#endif
this->status_comm_result(true);
}
#ifdef USE_SWITCH
bool NPM1300Component::publish_switches_() {
uint8_t value;
bool ok = true;
if (this->charger_switch_ != nullptr) {
if (this->read_byte_reg16(REG_BCHG_ENABLE_SET, &value)) {
this->charger_switch_->publish_state((value & 1) != 0);
} else {
ok = false;
}
}
switch_::Switch *const bucks[2] = {this->buck1_switch_, this->buck2_switch_};
if (bucks[0] != nullptr || bucks[1] != nullptr) {
if (this->read_byte_reg16(REG_BUCK_STATUS, &value)) {
for (uint8_t i = 0; i < 2; i++)
PUBLISH_STATE(bucks[i], (value & BUCK_POWER_GOOD[i]) != 0);
} else {
ok = false;
}
}
switch_::Switch *const ldos[2] = {this->ldo1_switch_, this->ldo2_switch_};
if (ldos[0] != nullptr || ldos[1] != nullptr) {
if (this->read_byte_reg16(REG_LDSW_STATUS, &value)) {
for (uint8_t i = 0; i < 2; i++)
PUBLISH_STATE(ldos[i], (value & LDSW_POWERED[i]) != 0);
} else {
ok = false;
}
}
return ok;
}
#endif
#ifdef USE_NPM1300_DATA_PUBLISHER
void NPM1300Component::read_measurements_() {
// ADCIBATMEASSTATUS, then the VBAT, NTC, TEMP and VSYS results, their low bits, VBAT0 to VBAT3 (VBAT2 holds the
// battery current and VBAT3 the VBUS voltage) and their low bits
uint8_t adc[11];
if (!this->status_comm_result(this->read_register16(REG_ADC_RESULTS, adc, sizeof(adc)) == i2c::ERROR_OK))
return;
// Amperes per code of the battery current, positive while charging and negative while discharging
float ibat_scale = NAN;
// Any other mode means the system runs from VBUS and the battery current is not measured
const uint8_t mode = (adc[0] >> IBAT_MODE_SHIFT) & 0x03;
if ((adc[0] & IBAT_MEAS_INVALID) == 0) {
if (mode == IBAT_MODE_CHARGE) {
ibat_scale = this->charge_code_ * (2 * CHARGE_FULL_SCALE_FACTOR) / (ADC_MAX_CODE * 1000.0f);
} else if (mode == IBAT_MODE_DISCHARGE) {
ibat_scale = -(this->discharge_limit_high_ ? 1000 : 200) * DISCHARGE_FULL_SCALE_FACTOR / (ADC_MAX_CODE * 1000.0f);
}
}
if (this->data_publisher_ != nullptr)
this->data_publisher_(adc, ibat_scale);
}
#endif
// BCHGVTERM: 3.50 V to 3.65 V, then 4.00 V to 4.45 V, both in 50 mV steps
static uint16_t termination_voltage_mv(uint8_t code) { return code < 4 ? 3500 + 50 * code : 4000 + 50 * (code - 4); }
static const LogString *output_name(uint8_t index) {
switch (static_cast<NPM1300Output>(index)) {
case NPM1300Output::NPM1300_OUTPUT_BUCK1:
return LOG_STR("BUCK1");
case NPM1300Output::NPM1300_OUTPUT_BUCK2:
return LOG_STR("BUCK2");
case NPM1300Output::NPM1300_OUTPUT_LDO1:
return LOG_STR("LDO1");
default:
return LOG_STR("LDO2");
}
}
void NPM1300Component::dump_config() {
ESP_LOGCONFIG(TAG, "nPM1300:");
LOG_I2C_DEVICE(this);
LOG_UPDATE_INTERVAL(this);
if (this->vbus_current_limit_ != NPM1300_UNSET) {
// 100 mA per step of the code
ESP_LOGCONFIG(TAG, " VBUS current limit: %u mA", this->vbus_current_limit_ * 100u);
}
const NPM1300Config config = this->load_config_();
if (config.charger_configured) {
const NPM1300ChargerConfig &charger = config.charger;
ESP_LOGCONFIG(TAG,
" Charger:\n"
" Current: %u mA\n"
" Discharge limit: %u mA\n"
" Termination voltage: %u mV\n"
" Warm termination voltage: %u mV\n"
" Termination current: %u%%\n"
" Trickle voltage: %u mV\n"
" Enabled: %s\n"
" Recharge: %s",
charger.current * 2u, discharge_limit_ma(charger.discharge_limit),
termination_voltage_mv(charger.termination_voltage),
termination_voltage_mv(charger.warm_termination_voltage), charger.termination_current ? 20u : 10u,
charger.trickle_voltage ? 2500u : 2900u, YESNO(charger.enabled), YESNO(charger.recharge));
}
for (uint8_t i = 0; i < NPM1300_OUTPUT_COUNT; i++) {
const NPM1300OutputConfig &output = config.outputs[i];
if (output.voltage == NPM1300_UNSET && output.ldo_mode == NPM1300_UNSET && output.enabled == NPM1300_UNSET)
continue;
ESP_LOGCONFIG(TAG, " %s:", LOG_STR_ARG(output_name(i)));
if (output.voltage != NPM1300_UNSET) {
ESP_LOGCONFIG(TAG, " Voltage: %u mV", 1000u + output.voltage * 100u);
}
if (output.ldo_mode != NPM1300_UNSET) {
ESP_LOGCONFIG(TAG, " Mode: %s",
output.ldo_mode != 0 ? LOG_STR_LITERAL("LDO") : LOG_STR_LITERAL("load switch"));
}
if (output.enabled != NPM1300_UNSET) {
ESP_LOGCONFIG(TAG, " Enabled at boot: %s", YESNO(output.enabled != 0));
}
}
if (config.thermistor != NPM1300_UNSET) {
ESP_LOGCONFIG(TAG, " Thermistor B constant: %u", config.thermistor_beta);
}
#ifdef USE_SENSOR
LOG_SENSOR(" ", "VBUS Current Limit", this->vbus_current_limit_sensor_);
#endif
#ifdef USE_SWITCH
LOG_SWITCH(" ", "Charger", this->charger_switch_);
LOG_SWITCH(" ", "BUCK1", this->buck1_switch_);
LOG_SWITCH(" ", "BUCK2", this->buck2_switch_);
LOG_SWITCH(" ", "LDO1", this->ldo1_switch_);
LOG_SWITCH(" ", "LDO2", this->ldo2_switch_);
#endif
#if defined(USE_NPM1300_STATUS_PUBLISHER) || defined(USE_NPM1300_DATA_PUBLISHER)
if (this->dump_publisher_ != nullptr)
this->dump_publisher_(TAG);
#endif
}
} // namespace esphome::npm1300
+145
View File
@@ -0,0 +1,145 @@
#pragma once
// Product specification: https://docs.nordicsemi.com/bundle/ps_npm1300/page/keyfeatures_html5.html
#include "esphome/components/i2c/i2c.h"
#include "esphome/core/component.h"
#include "esphome/core/defines.h"
#include "esphome/core/helpers.h"
#ifdef USE_SENSOR
#include "esphome/components/sensor/sensor.h"
#endif
#ifdef USE_TEXT_SENSOR
#include "esphome/components/text_sensor/text_sensor.h"
#endif
#ifdef USE_SWITCH
#include "esphome/components/switch/switch.h"
#endif
namespace esphome::npm1300 {
/// The regulators, in the order of their enable task registers
enum class NPM1300Output : uint8_t {
NPM1300_OUTPUT_BUCK1 = 0,
NPM1300_OUTPUT_BUCK2 = 1,
NPM1300_OUTPUT_LDO1 = 2,
NPM1300_OUTPUT_LDO2 = 3,
};
static constexpr uint8_t NPM1300_OUTPUT_COUNT = 4;
/// A configuration field the YAML leaves out, so the chip keeps its own value
static constexpr uint8_t NPM1300_UNSET = 0xFF;
/// Register codes for one regulator, NPM1300_UNSET where the YAML leaves the field out
struct NPM1300OutputConfig {
uint8_t voltage; // VOUT: (millivolts - 1000) / 100
uint8_t ldo_mode; // LDSWxLDOSEL, LDO1 and LDO2 only: 1 LDO, 0 load switch
uint8_t enabled; // turned on (1) or off (0) at boot
};
/// Register codes for the battery charger
struct NPM1300ChargerConfig {
uint16_t current; // BCHGISET: milliamperes / 2
uint16_t discharge_limit; // BCHGISETDISCHARGE: 84 for 200 mA, 415 for 1000 mA
uint8_t termination_voltage; // BCHGVTERM
uint8_t warm_termination_voltage; // BCHGVTERMR
uint8_t trickle_voltage; // BCHGVTRICKLESEL: 0 for 2.9 V, 1 for 2.5 V
uint8_t termination_current; // BCHGITERMSEL: 0 for 10 %, 1 for 20 %
bool recharge;
bool enabled;
};
/// The settings applied in setup(), built by code generation into a PROGMEM table
struct NPM1300Config {
NPM1300OutputConfig outputs[NPM1300_OUTPUT_COUNT];
NPM1300ChargerConfig charger; // only applied when charger_configured
uint16_t thermistor_beta;
uint8_t thermistor; // ADCNTCRSEL code: 1 for 10 kOhm, 2 for 47 kOhm, 3 for 100 kOhm, or NPM1300_UNSET
bool charger_configured;
};
#ifdef USE_NPM1300_DATA_PUBLISHER
/// The 10-bit ADC result whose high 8 bits are adc[msb] and low 2 bits are at `shift` in adc[lsbs]
inline uint16_t adc_code(const uint8_t *adc, uint8_t msb, uint8_t lsbs, uint8_t shift) {
return (adc[msb] << 2) | ((adc[lsbs] >> shift) & 0x03);
}
/// Battery temperature in degrees Celsius from the NTC code and 1 / B constant of the thermistor
float ntc_temperature(uint16_t code, float inv_beta);
#endif
#ifdef USE_TEXT_SENSOR
/// Publish the charger status text, only when it differs from `last`
void publish_charger_status(text_sensor::TextSensor *sensor, uint8_t &last, uint8_t vbus_status, uint8_t charge_status,
uint8_t error_reason);
#endif
class NPM1300Component : public PollingComponent, public i2c::I2CDevice {
#ifdef USE_SENSOR
SUB_SENSOR(vbus_current_limit)
#endif
#ifdef USE_SWITCH
SUB_SWITCH(charger)
SUB_SWITCH(buck1)
SUB_SWITCH(buck2)
SUB_SWITCH(ldo1)
SUB_SWITCH(ldo2)
#endif
public:
/// `config` points into PROGMEM
explicit NPM1300Component(const NPM1300Config *config) : config_(config) {}
void setup() override;
void update() override;
void dump_config() override;
// Right after the I2C bus: the regulators may power other peripherals
float get_setup_priority() const override { return setup_priority::BUS - 1.0f; }
#ifdef USE_NPM1300_STATUS_PUBLISHER
HUB_PUBLISHER(status, uint8_t, uint8_t, uint8_t)
#endif
#ifdef USE_NPM1300_DATA_PUBLISHER
HUB_PUBLISHER(data, const uint8_t *, float)
#endif
#if defined(USE_NPM1300_STATUS_PUBLISHER) || defined(USE_NPM1300_DATA_PUBLISHER)
HUB_PUBLISHER(dump, const char *)
#endif
/// VBUSINILIM0 code: 100 mA per step (code 0, the reset value, is 500 mA)
void set_vbus_current_limit(uint8_t code) { this->vbus_current_limit_ = code; }
/// Turn the battery charger on or off; turning it on also clears a charger error
bool set_charger_enabled(bool enabled);
/// Turn a regulator on or off
bool set_output_enabled(NPM1300Output output, bool enabled);
/// Enter ship mode: every output is turned off until the SHPHLD button is pressed or VBUS is connected
void enter_ship_mode();
/// Enter hibernate mode: every output is turned off for `duration_ms`, or until the SHPHLD button is
/// pressed or VBUS is connected
void hibernate(uint32_t duration_ms);
protected:
NPM1300Config load_config_() const;
bool apply_vbus_current_limit_();
bool configure_charger_(const NPM1300Config &config);
bool configure_output_(uint8_t index, const NPM1300OutputConfig &config);
#ifdef USE_NPM1300_DATA_PUBLISHER
bool read_charge_currents_();
void read_measurements_();
#endif
#ifdef USE_SWITCH
/// Returns false when a status read failed
bool publish_switches_();
#endif
const NPM1300Config *config_;
#ifdef USE_NPM1300_DATA_PUBLISHER
// BCHGISET code and discharge limit, for the full scale of the battery current measurement
uint16_t charge_code_{0};
bool discharge_limit_high_{false};
#endif
uint8_t vbus_current_limit_{NPM1300_UNSET};
uint8_t last_error_reason_{0};
bool vbus_present_{false};
};
} // namespace esphome::npm1300
+151
View File
@@ -0,0 +1,151 @@
from esphome.automation import parent_ref
import esphome.codegen as cg
from esphome.components import sensor
import esphome.config_validation as cv
from esphome.const import (
CONF_BATTERY_VOLTAGE,
CONF_ID,
DEVICE_CLASS_CURRENT,
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_VOLTAGE,
ENTITY_CATEGORY_DIAGNOSTIC,
STATE_CLASS_MEASUREMENT,
UNIT_AMPERE,
UNIT_CELSIUS,
UNIT_VOLT,
)
from esphome.core import CORE
import esphome.final_validate as fv
from esphome.types import ConfigType
from . import (
ADC,
CONF_NPM1300_ID,
CONF_THERMISTOR_BETA,
CONF_THERMISTOR_RESISTANCE,
CONF_VBUS_CURRENT_LIMIT,
DATA_PUBLISHER,
DOMAIN,
DUMP,
DUMP_TAG,
IBAT_SCALE,
NPM1300_CHILD_SCHEMA,
npm1300_ns,
)
DEPENDENCIES = ["npm1300"]
CONF_BATTERY_CURRENT = "battery_current"
CONF_BATTERY_TEMPERATURE = "battery_temperature"
CONF_DIE_TEMPERATURE = "die_temperature"
CONF_SYSTEM_VOLTAGE = "system_voltage"
CONF_VBUS_VOLTAGE = "vbus_voltage"
ADC_MAX_CODE = 1023
def _adc(msb: int, lsbs: int, shift: int) -> cg.MockObj:
"""The 10-bit result in adc[msb] and bits shift..shift+1 of adc[lsbs]."""
return npm1300_ns.adc_code(ADC, msb, lsbs, shift)
# The decoded value and the dump name of each ADC sensor but the battery temperature, which needs the hub config.
# Voltages scale by their full scale (product specification, section ADC).
STATES = {
CONF_BATTERY_VOLTAGE: (_adc(1, 5, 0) * (5.0 / ADC_MAX_CODE), "Battery Voltage"),
CONF_BATTERY_CURRENT: (_adc(8, 10, 4) * IBAT_SCALE, "Battery Current"),
# Product specification: T = 394.67 - 0.7926 * code
CONF_DIE_TEMPERATURE: (394.67 - 0.7926 * _adc(3, 5, 4), "Die Temperature"),
CONF_SYSTEM_VOLTAGE: (_adc(4, 5, 6) * (6.375 / ADC_MAX_CODE), "System Voltage"),
CONF_VBUS_VOLTAGE: (_adc(9, 10, 6) * (7.5 / ADC_MAX_CODE), "VBUS Voltage"),
}
def _voltage_schema() -> cv.Schema:
return sensor.sensor_schema(
unit_of_measurement=UNIT_VOLT,
accuracy_decimals=3,
device_class=DEVICE_CLASS_VOLTAGE,
state_class=STATE_CLASS_MEASUREMENT,
)
def _temperature_schema(**extra: str) -> cv.Schema:
return sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
**extra,
)
CONFIG_SCHEMA = NPM1300_CHILD_SCHEMA.extend(
{
cv.Optional(CONF_BATTERY_VOLTAGE): _voltage_schema(),
cv.Optional(CONF_BATTERY_CURRENT): sensor.sensor_schema(
unit_of_measurement=UNIT_AMPERE,
accuracy_decimals=3,
device_class=DEVICE_CLASS_CURRENT,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_BATTERY_TEMPERATURE): _temperature_schema(),
cv.Optional(CONF_DIE_TEMPERATURE): _temperature_schema(
entity_category=ENTITY_CATEGORY_DIAGNOSTIC
),
cv.Optional(CONF_SYSTEM_VOLTAGE): _voltage_schema(),
cv.Optional(CONF_VBUS_VOLTAGE): _voltage_schema(),
cv.Optional(CONF_VBUS_CURRENT_LIMIT): sensor.sensor_schema(
unit_of_measurement=UNIT_AMPERE,
accuracy_decimals=1,
device_class=DEVICE_CLASS_CURRENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
}
)
def _final_validate(config: ConfigType) -> None:
if CONF_BATTERY_TEMPERATURE not in config:
return
hub = fv.get_declaring_config(config[CONF_NPM1300_ID])
if CONF_THERMISTOR_RESISTANCE not in hub:
raise cv.Invalid(
f"'{CONF_BATTERY_TEMPERATURE}' needs '{CONF_THERMISTOR_RESISTANCE}' "
"set on the npm1300 component",
path=[CONF_BATTERY_TEMPERATURE],
)
FINAL_VALIDATE_SCHEMA = _final_validate
def _add_adc_sensor(
hub: cg.MockObj, var: cg.MockObj, state: cg.MockObj, name: str
) -> None:
DATA_PUBLISHER.add(hub, parent_ref(var).publish_state(state))
DUMP.add(
hub,
sensor.sensor_ns.log_sensor(
DUMP_TAG,
" ",
cg.RawExpression(f'LOG_STR_LITERAL("{name}")'),
parent_ref(var),
),
)
async def to_code(config: ConfigType) -> None:
hub_id = config[CONF_NPM1300_ID]
hub = await cg.get_variable(hub_id)
sensors = sensor.sub_sensors(config)
for key, (state, name) in STATES.items():
if (var := await sensors(key, None)) is not None:
_add_adc_sensor(hub, var, state, name)
if (var := await sensors(CONF_BATTERY_TEMPERATURE, None)) is not None:
hub_config = next(c for c in CORE.config[DOMAIN] if c[CONF_ID] == hub_id)
state = npm1300_ns.ntc_temperature(
_adc(2, 5, 2), 1.0 / hub_config[CONF_THERMISTOR_BETA]
)
_add_adc_sensor(hub, var, state, "Battery Temperature")
await sensors(CONF_VBUS_CURRENT_LIMIT, hub.set_vbus_current_limit_sensor)
+95
View File
@@ -0,0 +1,95 @@
import esphome.codegen as cg
from esphome.components import switch
import esphome.config_validation as cv
from esphome.const import ENTITY_CATEGORY_CONFIG
import esphome.final_validate as fv
from esphome.types import ConfigType
from . import (
CONF_BUCK1,
CONF_BUCK2,
CONF_CHARGER,
CONF_LDO1,
CONF_LDO2,
CONF_NPM1300_ID,
CONF_THERMISTOR_RESISTANCE,
NPM1300_CHILD_SCHEMA,
OUTPUTS,
)
DEPENDENCIES = ["npm1300"]
def _switch_schema() -> cv.Schema:
# The switches show the state of the chip, so nothing is restored at boot
return switch.switch_schema(
switch.ApplySwitch,
default_restore_mode="DISABLED",
entity_category=ENTITY_CATEGORY_CONFIG,
)
CONFIG_SCHEMA = NPM1300_CHILD_SCHEMA.extend(
{
cv.Optional(CONF_CHARGER): _switch_schema(),
cv.Optional(CONF_BUCK1): _switch_schema(),
cv.Optional(CONF_BUCK2): _switch_schema(),
cv.Optional(CONF_LDO1): _switch_schema(),
cv.Optional(CONF_LDO2): _switch_schema(),
}
)
def _final_validate(config: ConfigType) -> None:
if CONF_CHARGER not in config:
return
hub = fv.get_declaring_config(config[CONF_NPM1300_ID])
# Only the charger block turns off the NTC check, without which the charger never starts
if CONF_CHARGER not in hub and CONF_THERMISTOR_RESISTANCE not in hub:
raise cv.Invalid(
f"The '{CONF_CHARGER}' switch needs a '{CONF_CHARGER}' block or "
f"'{CONF_THERMISTOR_RESISTANCE}' on the npm1300 component",
path=[CONF_CHARGER],
)
FINAL_VALIDATE_SCHEMA = _final_validate
def _output_apply(key: str) -> str:
return f"set_output_enabled({OUTPUTS[key]}, state)"
async def to_code(config: ConfigType) -> None:
hub = await cg.get_variable(config[CONF_NPM1300_ID])
switches = switch.sub_switches(config, parent=hub)
await switches(
CONF_CHARGER,
hub.set_charger_switch,
apply="set_charger_enabled(state)",
publish="on_success",
)
await switches(
CONF_BUCK1,
hub.set_buck1_switch,
apply=_output_apply(CONF_BUCK1),
publish="on_success",
)
await switches(
CONF_BUCK2,
hub.set_buck2_switch,
apply=_output_apply(CONF_BUCK2),
publish="on_success",
)
await switches(
CONF_LDO1,
hub.set_ldo1_switch,
apply=_output_apply(CONF_LDO1),
publish="on_success",
)
await switches(
CONF_LDO2,
hub.set_ldo2_switch,
apply=_output_apply(CONF_LDO2),
publish="on_success",
)
+63
View File
@@ -0,0 +1,63 @@
from esphome.automation import parent_ref
import esphome.codegen as cg
from esphome.components import text_sensor
import esphome.config_validation as cv
from esphome.const import ICON_BATTERY
from esphome.cpp_generator import (
AssignmentExpression,
HexIntLiteral,
VariableDeclarationExpression,
)
from esphome.types import ConfigType
from . import (
CHARGE_STATUS,
CONF_NPM1300_ID,
DUMP,
DUMP_TAG,
ERROR_REASON,
NPM1300_CHILD_SCHEMA,
STATUS_PUBLISHER,
VBUS_STATUS,
npm1300_ns,
)
DEPENDENCIES = ["npm1300"]
CONF_CHARGER_STATUS = "charger_status"
CONFIG_SCHEMA = NPM1300_CHILD_SCHEMA.extend(
{
cv.Optional(CONF_CHARGER_STATUS): text_sensor.text_sensor_schema(
icon=ICON_BATTERY,
),
}
)
async def to_code(config: ConfigType) -> None:
hub = await cg.get_variable(config[CONF_NPM1300_ID])
text_sensors = text_sensor.sub_text_sensors(config)
if (var := await text_sensors(CONF_CHARGER_STATUS, None)) is None:
return
last = cg.MockObj(f"esphome__{var}__last")
# 0xFF so the first update publishes
declaration = VariableDeclarationExpression(cg.uint8, "", last, static=True)
STATUS_PUBLISHER.add(
hub, AssignmentExpression(None, None, declaration, HexIntLiteral(0xFF))
)
STATUS_PUBLISHER.add(
hub,
npm1300_ns.publish_charger_status(
parent_ref(var), last, VBUS_STATUS, CHARGE_STATUS, ERROR_REASON
),
)
DUMP.add(
hub,
text_sensor.text_sensor_ns.log_text_sensor(
DUMP_TAG,
" ",
cg.RawExpression('LOG_STR_LITERAL("Charger Status")'),
parent_ref(var),
),
)
@@ -291,25 +291,21 @@ bool ShellyDimmer::send_command_(uint8_t cmd, const uint8_t *const payload, uint
}
size_t ShellyDimmer::frame_command_(uint8_t *data, uint8_t cmd, const uint8_t *const payload, size_t len) {
size_t pos = 0;
// Generate a frame.
data[0] = SHELLY_DIMMER_PROTO_START_BYTE;
data[1] = ++this->seq_;
data[2] = cmd;
data[3] = len;
pos += 4;
uint8_t *p = data;
*p++ = SHELLY_DIMMER_PROTO_START_BYTE;
*p++ = ++this->seq_;
*p++ = cmd;
*p++ = len;
if (payload != nullptr) {
std::memcpy(data + 4, payload, len);
pos += len;
std::memcpy(p, payload, len);
p += len;
}
// Calculate checksum for the payload.
put_be16(data + pos, shelly_dimmer_checksum(data + 1, 3 + len));
pos += 2;
data[pos++] = SHELLY_DIMMER_PROTO_END_BYTE;
return pos;
// Checksum over the sequence, command, length and payload bytes
p = put_be16(p, shelly_dimmer_checksum(data + 1, 3 + len));
*p++ = SHELLY_DIMMER_PROTO_END_BYTE;
return p - data;
}
int ShellyDimmer::handle_byte_(uint8_t c) {
+2
View File
@@ -320,6 +320,8 @@
#define USE_NEXTION_TRIGGER_CUSTOM_SWITCH
#define USE_NEXTION_TRIGGER_CUSTOM_TEXT_SENSOR
#define USE_NEXTION_WAVEFORM
#define USE_NPM1300_DATA_PUBLISHER
#define USE_NPM1300_STATUS_PUBLISHER
#define USE_NUMBER
#define USE_OTA_STATE_LISTENER
#define USE_OUTPUT
+2 -1
View File
@@ -18,7 +18,6 @@ from pathlib import Path
import re
from typing import Any, NamedTuple
from urllib.parse import urlparse
from urllib.request import url2pathname
from esphome import loader, yaml_util
from esphome.components.external_components import ExternalSource, get_sources
@@ -201,6 +200,8 @@ def _esphome_vcs() -> _Source | None:
if commit:
return _Source(strip_url_credentials(url), commit)
if "dir_info" in info and url.startswith("file:"):
from urllib.request import url2pathname
# An editable or local install may be a fork or a patched clone
if (
source := _checkout_vcs(Path(url2pathname(urlparse(url).path)))
+3 -1
View File
@@ -17,7 +17,6 @@ import threading
import time
from typing import IO, TYPE_CHECKING
from urllib.parse import urlparse
from urllib.request import url2pathname
from esphome.helpers import ProgressBar, get_usable_cpu_count, rmtree
from esphome.net_retry import (
@@ -1363,6 +1362,9 @@ def local_path_from_url(url: str) -> Path:
Handles ``file:///home/me/x`` and ``file:///C:/x`` (no host), ``file://localhost/x``
and ``file://server/share/x`` (a UNC path).
"""
# urllib.request loads http.client and the email parser, which config validation does not need
from urllib.request import url2pathname
from esphome.core import EsphomeError
try:
+2 -1
View File
@@ -26,7 +26,6 @@ from pathlib import Path, PurePosixPath
import re
from typing import Any
from urllib.parse import urlsplit, urlunsplit
from urllib.request import url2pathname
from esphome import git
from esphome.core import CORE, EsphomeError, Library
@@ -961,6 +960,8 @@ def _node_key(
f"{scheme}:// library URL '{repository}' must be an absolute "
f"directory path, e.g. {scheme}:///path/to/lib"
)
from urllib.request import url2pathname
path = url2pathname(url_path)
return (name or PurePosixPath(url_path).name), "local", (path, None)
key = str(split_result.path).strip("/").removesuffix(".git")
Whitespace-only changes.
@@ -0,0 +1,114 @@
"""Tests for the npm1300 register encoders and validators."""
import pytest
from esphome import config_validation as cv
from esphome.components.const import CONF_ENABLED
from esphome.components.npm1300 import (
CHARGER_SCHEMA,
NPM1300_UNSET,
OUTPUT_VOLTAGE,
VBUS_CURRENT_LIMIT,
_charger_config,
_output_config,
_termination_voltage_code,
_vbus_current_limit_code,
)
from esphome.const import CONF_MODE, CONF_VOLTAGE
from esphome.cpp_generator import StructInitializer
from esphome.types import ConfigType
def _fields(init: StructInitializer) -> dict[str, str]:
return {key: str(value) for key, value in init.args.items()}
def _charger(config: ConfigType) -> dict[str, str]:
return _fields(_charger_config(CHARGER_SCHEMA(config)))
@pytest.mark.parametrize(
("current", "code"), [("32mA", 16), ("150mA", 75), ("800mA", 400)]
)
def test_charge_current_code(current: str, code: int) -> None:
assert _charger({"charge_current": current})["current"] == str(code)
@pytest.mark.parametrize("current", ["30mA", "802mA", "151mA"])
def test_charge_current_rejected(current: str) -> None:
with pytest.raises(cv.Invalid):
CHARGER_SCHEMA({"charge_current": current})
@pytest.mark.parametrize(
("voltage", "code"),
[(3.5, 0), (3.65, 3), (4.0, 4), (4.2, 8), (4.45, 13)],
)
def test_termination_voltage_code(voltage: float, code: int) -> None:
assert _termination_voltage_code(voltage) == code
@pytest.mark.parametrize("voltage", ["3.7V", "3.95V", "4.5V"])
def test_termination_voltage_gap_rejected(voltage: str) -> None:
with pytest.raises(cv.Invalid):
CHARGER_SCHEMA({"charge_current": "100mA", "termination_voltage": voltage})
def test_warm_termination_defaults_to_termination() -> None:
fields = _charger({"charge_current": "100mA", "termination_voltage": "4.1V"})
assert fields["termination_voltage"] == "6"
assert fields["warm_termination_voltage"] == "6"
def test_warm_termination_code() -> None:
fields = _charger(
{
"charge_current": "100mA",
"termination_voltage": "4.2V",
"warm_termination_voltage": "3.6V",
}
)
assert fields["termination_voltage"] == "8"
assert fields["warm_termination_voltage"] == "2"
@pytest.mark.parametrize(("voltage", "code"), [("1.0V", 0), ("1.8V", 8), ("3.3V", 23)])
def test_output_voltage_code(voltage: str, code: int) -> None:
config = {CONF_VOLTAGE: OUTPUT_VOLTAGE(voltage)}
assert _fields(_output_config(config))["voltage"] == str(code)
@pytest.mark.parametrize("voltage", ["0.9V", "3.4V", "1.85V"])
def test_output_voltage_rejected(voltage: str) -> None:
with pytest.raises(cv.Invalid):
OUTPUT_VOLTAGE(voltage)
@pytest.mark.parametrize("config", [None, {}])
def test_unset_output_fields(config: ConfigType | None) -> None:
unset = str(NPM1300_UNSET)
assert _fields(_output_config(config)) == {
"voltage": unset,
"ldo_mode": unset,
"enabled": unset,
}
def test_output_mode_and_enabled() -> None:
fields = _fields(_output_config({CONF_MODE: "load_switch", CONF_ENABLED: True}))
assert fields["ldo_mode"] == "0"
assert fields["enabled"] == "1"
assert fields["voltage"] == str(NPM1300_UNSET)
@pytest.mark.parametrize(
("limit", "code"), [("100mA", 1), ("300mA", 3), ("500mA", 5), ("1.5A", 15)]
)
def test_vbus_current_limit_code(limit: str, code: int) -> None:
assert _vbus_current_limit_code(VBUS_CURRENT_LIMIT(limit)) == code
@pytest.mark.parametrize("limit", ["50mA", "1600mA", "550mA"])
def test_vbus_current_limit_rejected(limit: str) -> None:
with pytest.raises(cv.Invalid):
VBUS_CURRENT_LIMIT(limit)
+6
View File
@@ -0,0 +1,6 @@
from tests.testing_helpers import ComponentManifestOverride
def override_manifest(manifest: ComponentManifestOverride) -> None:
# touchscreen.h includes display.h
manifest.dependencies = manifest.dependencies + ["display"]
+50
View File
@@ -0,0 +1,50 @@
#pragma once
#include <algorithm>
#include <cstring>
#include <map>
#include <vector>
#include "esphome/components/cst3240/touchscreen/cst3240_touchscreen.h"
#include "esphome/core/helpers.h"
namespace esphome::cst3240::testing {
struct Write {
uint16_t reg;
size_t size;
};
// Answers 16-bit register reads from a map; every write succeeds and is recorded.
class RegisterBus final : public i2c::I2CBus {
public:
std::map<uint16_t, std::vector<uint8_t>> registers;
std::vector<Write> writes;
i2c::ErrorCode write_readv(uint8_t address, const uint8_t *data, size_t size, uint8_t *out,
size_t out_size) override {
if (size < 2)
return i2c::ERROR_NOT_ACKNOWLEDGED;
const uint16_t reg = encode_uint16(data[0], data[1]);
if (out_size == 0) {
this->writes.push_back({reg, size});
return i2c::ERROR_OK;
}
auto &value = this->registers[reg];
value.resize(std::max(value.size(), out_size));
std::memcpy(out, value.data(), out_size);
return i2c::ERROR_OK;
}
// Lists `count` fingers with ids 0..count-1, each with the given event bits.
void set_report(uint8_t count, uint8_t sync = cstxxx::SYNC_BYTE, uint8_t event = TOUCH_EVENT_PRESSED) {
std::vector<uint8_t> data(CST3240_REPORT_SIZE);
for (uint8_t i = 0; i < std::min(count, CST3240_MAX_TOUCHES); i++)
data[i * 5 + (i == 0 ? 0 : 2)] = i << 4 | event;
data[cstxxx::FINGER_COUNT_IDX] = count;
data[cstxxx::SYNC_IDX] = sync;
this->registers[cstxxx::REG_TOUCH_DATA] = data;
}
};
} // namespace esphome::cst3240::testing
+18
View File
@@ -0,0 +1,18 @@
display:
- platform: ssd1306_i2c
i2c_id: i2c_bus
id: cst3240_ssd1306_i2c_display
model: SSD1306_128X64
reset_pin: ${display_reset_pin}
pages:
- id: cst3240_page1
lambda: |-
it.rectangle(0, 0, it.get_width(), it.get_height());
touchscreen:
- platform: cst3240
i2c_id: i2c_bus
id: cst3240_touchscreen
display: cst3240_ssd1306_i2c_display
interrupt_pin: ${interrupt_pin}
reset_pin: ${reset_pin}
@@ -0,0 +1,8 @@
substitutions:
display_reset_pin: "4"
interrupt_pin: "20"
reset_pin: "21"
packages:
i2c: !include ../../test_build_components/common/i2c/esp32-idf.yaml
cst3240: !include common.yaml
@@ -0,0 +1,103 @@
#include <gtest/gtest.h>
#include "esphome/components/cst3240/touchscreen/cst3240_touchscreen.h"
#include "esphome/core/gpio.h"
#include "../common.h"
namespace esphome::cst3240::testing {
class TestableCST3240 : public CST3240Touchscreen {
public:
using CST3240Touchscreen::chip_id_;
using CST3240Touchscreen::is_touched_;
using CST3240Touchscreen::setup_complete_;
using CST3240Touchscreen::skip_update_;
using CST3240Touchscreen::update_touches;
using CST3240Touchscreen::x_raw_max_;
using CST3240Touchscreen::y_raw_max_;
};
class CST3240Test : public ::testing::Test {
protected:
void SetUp() override {
this->bus_.registers[cstxxx::REG_CHIP_INFO] = {0x01, 0x00, 0x40, 0x32};
this->bus_.registers[cstxxx::REG_RESOLUTION] = {0x40, 0x01, 0xF0, 0x00};
this->hub_.set_i2c_bus(&this->bus_);
this->hub_.setup();
this->bus_.writes.clear();
}
void update_() {
this->hub_.skip_update_ = false;
this->hub_.is_touched_ = false;
this->hub_.update_touches();
}
bool cleared_() const {
return this->bus_.writes.size() == 1 && this->bus_.writes[0].reg == cstxxx::REG_TOUCH_DATA &&
this->bus_.writes[0].size == 2u + CST3240_REPORT_SIZE;
}
RegisterBus bus_;
TestableCST3240 hub_;
};
TEST_F(CST3240Test, SetupReadsTheChipAndResolutionWithoutAFirmwareCheck) {
EXPECT_TRUE(this->hub_.setup_complete_);
EXPECT_FALSE(this->hub_.is_failed());
EXPECT_EQ(this->hub_.chip_id_, CST3240_CHIP_ID);
EXPECT_EQ(this->hub_.x_raw_max_, 320);
EXPECT_EQ(this->hub_.y_raw_max_, 240);
}
TEST_F(CST3240Test, ReportWithoutTheSyncByteIsSkippedAndCleared) {
this->bus_.set_report(1, 0x55);
this->update_();
EXPECT_TRUE(this->hub_.skip_update_);
EXPECT_FALSE(this->hub_.is_touched_);
EXPECT_TRUE(this->cleared_());
}
TEST_F(CST3240Test, IdleBlockIsNotRewritten) {
this->bus_.set_report(0, 0);
this->update_();
EXPECT_TRUE(this->hub_.skip_update_);
EXPECT_TRUE(this->bus_.writes.empty());
}
TEST_F(CST3240Test, ReleasedRecordIsNotATouch) {
this->bus_.set_report(1, cstxxx::SYNC_BYTE, 0);
this->update_();
EXPECT_FALSE(this->hub_.skip_update_);
EXPECT_FALSE(this->hub_.is_touched_);
EXPECT_TRUE(this->hub_.get_touches().empty());
}
TEST_F(CST3240Test, PollingClearsTheReport) {
this->bus_.set_report(1);
this->update_();
EXPECT_TRUE(this->hub_.is_touched_);
EXPECT_TRUE(this->cleared_());
}
TEST_F(CST3240Test, InterruptAcknowledgesTheReport) {
InternalGPIOPin pin;
this->hub_.set_interrupt_pin(&pin);
this->bus_.set_report(1);
this->update_();
ASSERT_EQ(this->bus_.writes.size(), 2u);
EXPECT_EQ(this->bus_.writes[0].reg, cstxxx::REG_FINGER_COUNT);
EXPECT_EQ(this->bus_.writes[1].reg, cstxxx::REG_TOUCH_DATA);
EXPECT_EQ(this->bus_.writes[1].size, 3u);
}
TEST_F(CST3240Test, ReportsUpToFiveFingers) {
this->bus_.set_report(5);
this->update_();
EXPECT_EQ(this->hub_.get_touches().size(), 5u);
this->bus_.set_report(6);
this->update_();
EXPECT_FALSE(this->hub_.is_touched_);
}
} // namespace esphome::cst3240::testing
+84
View File
@@ -0,0 +1,84 @@
npm1300:
id: pmic
i2c_id: i2c_bus
vbus_current_limit: 500mA
thermistor_resistance: 10kOhm
thermistor_beta: 3380
charger:
charge_current: 150mA
termination_voltage: 4.2V
warm_termination_voltage: 4.0V
termination_current: 20%
trickle_voltage: 2.5V
discharge_current_limit: 1000mA
recharge: true
buck2:
voltage: 3.3V
ldo1:
voltage: 3.3V
mode: ldo
enabled: true
ldo2:
mode: load_switch
enabled: false
sensor:
- platform: npm1300
battery_voltage:
name: Battery Voltage
battery_current:
name: Battery Current
battery_temperature:
name: Battery Temperature
die_temperature:
name: PMIC Temperature
system_voltage:
name: System Voltage
vbus_voltage:
name: VBUS Voltage
vbus_current_limit:
name: VBUS Current Limit
binary_sensor:
- platform: npm1300
vbus_present:
name: VBUS Present
vbus_current_limit_active:
name: VBUS Current Limit Active
charging:
name: Charging
charge_complete:
name: Charge Complete
charger_error:
name: Charger Error
text_sensor:
- platform: npm1300
charger_status:
name: Charger Status
switch:
- platform: npm1300
charger:
name: Charger
buck1:
name: BUCK1
ldo1:
name: LDO1
ldo2:
name: LDO2
button:
- platform: template
name: Ship Mode
on_press:
- npm1300.enter_ship_mode: pmic
- platform: template
name: Hibernate
on_press:
- npm1300.hibernate:
id: pmic
duration: 1h
- npm1300.hibernate:
id: pmic
duration: !lambda return 60000;
@@ -0,0 +1,3 @@
packages:
i2c: !include ../../test_build_components/common/i2c/esp32-idf.yaml
npm1300: !include common.yaml
+3
View File
@@ -0,0 +1,3 @@
packages:
i2c: !include ../../test_build_components/common/i2c/host.yaml
npm1300: !include common.yaml
@@ -0,0 +1,3 @@
packages:
i2c: !include ../../test_build_components/common/i2c/nrf52.yaml
npm1300: !include common.yaml
+3 -22
View File
@@ -1,34 +1,15 @@
"""Tests for the memory analyzer."""
import subprocess
from unittest.mock import patch
from esphome.analyze_memory import MemoryAnalyzer
def make_analyzer[T: MemoryAnalyzer](
external_components: set[str] | None = None, cls: type[T] = MemoryAnalyzer
) -> T:
"""Create an analyzer through its real constructor, without an ELF or a toolchain."""
def make_analyzer(external_components: set[str] | None = None) -> MemoryAnalyzer:
"""Create a MemoryAnalyzer through its real constructor, without an ELF or a toolchain."""
with (
patch("esphome.analyze_memory.resolve_tool_path", return_value=None),
patch("esphome.analyze_memory.find_tool", return_value="nm"),
):
# Any existing file passes the ELF check; nothing reads it
return cls(__file__, external_components=external_components)
def completed(stdout: str) -> subprocess.CompletedProcess[str]:
"""A successful tool run printing ``stdout``."""
return subprocess.CompletedProcess([], 0, stdout=stdout)
def hex_dump(section: str, data: bytes, address: int = 0) -> str:
"""Format bytes the way ``readelf -x`` does."""
lines = [f"Hex dump of section '{section}':"]
for start in range(0, len(data), 16):
chunk = data[start : start + 16]
words = " ".join(chunk[i : i + 4].hex() for i in range(0, len(chunk), 4))
text = "".join(chr(b) if 32 <= b < 127 else "." for b in chunk)
lines.append(f" 0x{address + start:08x} {words:<35} {text}")
return "\n".join(lines) + "\n\n"
return MemoryAnalyzer(__file__, external_components=external_components)
@@ -1,7 +1,5 @@
"""Tests for source-file-to-component attribution in memory analyzer."""
import pytest
from . import make_analyzer
@@ -32,19 +30,3 @@ def test_source_file_to_component_known_component() -> None:
"src/esphome/components/wifi/wifi_component.cpp.o"
)
assert result == "[esphome]wifi"
@pytest.mark.parametrize(
("rel_path", "component"),
[
(
"lib/esp32async__ESPAsyncWebServer/WebHandlers.cpp.o",
"[lib]espasyncwebserver",
),
("lib/ESP8266WiFi/ESP8266WiFi.cpp.o", "[lib]esp8266wifi"),
("core/core_esp8266_main.cpp.o", "arduino_core"),
],
)
def test_native_library_objects(rel_path: str, component: str) -> None:
"""Objects of the native ESP8266 build's libraries and core get a component."""
assert make_analyzer()._source_file_to_component(rel_path) == component
@@ -1,673 +0,0 @@
"""Tests for ESP8266 .rodata accounting and unnamed constant attribution."""
from collections.abc import Callable
from pathlib import Path
import subprocess
from unittest.mock import patch
import pytest
from esphome.analyze_memory import (
_CODE_SECTION_PATTERN,
MemoryAnalyzer,
MemorySection,
UnnamedConstant,
_FunctionDefinitions,
_parse_unnamed_rodata,
_pointed_literal,
_RodataSection,
)
from esphome.analyze_memory.cli import MemoryAnalyzerCLI
from . import completed, hex_dump, make_analyzer
LIGHT_OBJ = "src/esphome/components/light/light_state.cpp.o"
WEB_OBJ = "src/esphome/components/web_server/web_server.cpp.o"
SETUP = "_ZN7esphome5light10LightState5setupEv"
SAVE = "_ZN7esphome5light10LightState19save_remote_values_Ev"
MATCH_URL = "_ZN7esphome10web_serverL9match_urlEPKcjbb"
# readelf -SW -sW over all objects; the web section index and size vary per test
HEADERS_AND_SYMBOLS = """\
File: {light}
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[124] .rodata._ZN7esphome5light10LightStateC2EPNS0_11LightOutputE.str1.1 PROGBITS 00000000 00067d 000001 01 AMS 0 0 1
[174] .rodata PROGBITS 00000000 00139c 00002c 00 A 0 0 4
[221] .rodata.str1.1 PROGBITS 00000000 001f39 000005 01 AMS 0 0 1
[225] .rodata.BUILTIN_LED PROGBITS 00000000 001f54 000004 00 A 0 0 4
Symbol table '.symtab' contains 348 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 00000000 0 NOTYPE LOCAL DEFAULT UND
300: 00000000 64 FUNC {bind:<6} DEFAULT 12 {setup}
301: 00000000 64 FUNC {bind:<6} DEFAULT 13 {save}
347: 00000000 4 OBJECT WEAK DEFAULT 225 BUILTIN_LED
File: {web}
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[{web_index:>3}] .rodata PROGBITS 00000000 000400 {web_size:06x} 00 A 0 0 4
Symbol table '.symtab' contains 1 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 00000000 0 NOTYPE LOCAL DEFAULT UND
{web_symbols}"""
LIGHT_OBJECT_DUMP = f"""\
Relocation section '.rela.text.{SETUP}' at offset 0x7928 contains 2 entries:
Offset Info Type Sym. Value Symbol's Name + Addend
00000000 00003a01 R_XTENSA_32 00000000 .rodata + 0
00000094 00003b14 R_XTENSA_SLOT0_OP 00000000 .text.{SETUP} + 82
Relocation section '.rela.text.{SAVE}' at offset 0x7d28 contains 1 entry:
Offset Info Type Sym. Value Symbol's Name + Addend
00000000 00003a01 R_XTENSA_32 00000000 .rodata + 0
Relocation section '.rela.xt.prop' at offset 0x8000 contains 1 entry:
Offset Info Type Sym. Value Symbol's Name + Addend
000007d4 00003a01 R_XTENSA_32 00000000 .rodata + 0
Hex dump of section '.rodata':
0x00000000 0000803f 0000803f 0000803f 0000803f ...?...?...?...?
0x00000010 0000803f 0000803f 0000803f 0000803f ...?...?...?...?
0x00000020 0000803f 00000000 00000000 ...?........
"""
WEB_OBJECT_DUMP = f"""\
Relocation section '.rela.text.{MATCH_URL}' at offset 0x100 contains 1 entry:
Offset Info Type Sym. Value Symbol's Name + Addend
00000000 00003a01 R_XTENSA_32 00000000 .rodata + 0
Hex dump of section '.rodata':
0x00000000 00000000 00000000 00000000 00000000 ................
"""
@pytest.fixture
def obj_dir(tmp_path: Path) -> Path:
"""Native ESP8266 build layout: objects under obj/, ELF next to it."""
for rel in (LIGHT_OBJ, WEB_OBJ, "src/main.cpp.o"):
path = tmp_path / "obj" / rel
path.parent.mkdir(parents=True, exist_ok=True)
path.touch()
return tmp_path / "obj"
def _esp8266_analyzer(
obj_dir: Path,
kept: set[str],
web_index: int = 9,
web_size: int = 0x1C,
web_dump: str = WEB_OBJECT_DUMP,
bind: str = "GLOBAL",
web_symbols: str = "",
) -> tuple[MemoryAnalyzer, Callable[..., subprocess.CompletedProcess[str]]]:
analyzer = make_analyzer()
analyzer.elf_path = obj_dir.parent / "firmware.elf"
analyzer.rodata_in_ram = True
analyzer.sections[".data"] = MemorySection(".data", total_size=1000)
analyzer._elf_symbol_names = kept
light = str(obj_dir / LIGHT_OBJ)
web = str(obj_dir / WEB_OBJ)
def run_tool(cmd: list[str]) -> subprocess.CompletedProcess[str]:
if "-SW" in cmd:
return completed(
HEADERS_AND_SYMBOLS.format(
light=light,
web=web,
web_index=web_index,
web_size=web_size,
bind=bind,
setup=SETUP,
save=SAVE,
web_symbols=web_symbols,
)
)
# Only sections with uncovered bytes are dumped
if cmd[-1] == light:
assert cmd[1:-1] == ["-rW", "-x", "174"]
return completed(LIGHT_OBJECT_DUMP)
return completed(web_dump)
return analyzer, run_tool
def test_find_object_files_dir_native_layout(obj_dir: Path) -> None:
"""The native ESP8266 build keeps objects under obj/."""
analyzer = make_analyzer()
analyzer.elf_path = obj_dir.parent / "firmware.elf"
assert analyzer._find_object_files_dir() == obj_dir
def test_find_object_files_dir_platformio_layout(tmp_path: Path) -> None:
"""PlatformIO keeps objects next to the ELF."""
(tmp_path / "src").mkdir()
(tmp_path / "src" / "main.cpp.o").touch()
analyzer = make_analyzer()
analyzer.elf_path = tmp_path / "firmware.elf"
assert analyzer._find_object_files_dir() == tmp_path
def _parse_sections(code_section: str) -> MemoryAnalyzer:
readelf = f"""\
[ 1] .data PROGBITS 3ffe8000 001000 000668 00 WAX 0 0 16
[ 4] {code_section} PROGBITS 40201010 00b010 06612c 00 WAX 0 0 16
[ 6] .rodata PROGBITS 3ffe86b0 0016b0 000b90 00 A 0 0 16
"""
analyzer = make_analyzer()
with patch(
"esphome.analyze_memory.subprocess.run", return_value=completed(readelf)
):
analyzer._parse_sections()
return analyzer
def test_parse_sections_esp8266_counts_rodata_as_data() -> None:
"""ESP8266 (.irom0.text) copies .rodata to RAM, so it is counted as .data."""
analyzer = _parse_sections(".irom0.text")
assert analyzer.rodata_in_ram
assert ".rodata" not in analyzer.sections
assert analyzer.sections[".data"].total_size == 0x668 + 0xB90
def test_parse_sections_other_platforms_keep_rodata() -> None:
"""Other platforms keep .rodata in flash."""
analyzer = _parse_sections(".flash.text")
assert not analyzer.rodata_in_ram
assert analyzer.sections[".rodata"].total_size == 0xB90
assert analyzer.sections[".data"].total_size == 0x668
def test_unnamed_constants_attributed_to_component(obj_dir: Path) -> None:
"""Unnamed .rodata in a kept function is listed and counted as the component's RAM."""
analyzer, run_tool = _esp8266_analyzer(obj_dir, {SETUP, MATCH_URL})
with patch("esphome.analyze_memory.run_tool", side_effect=run_tool):
analyzer._analyze_object_rodata()
light, web = analyzer._unnamed_constants
assert light == UnnamedConstant(
size=44,
source_file=LIGHT_OBJ,
component="[esphome]light",
functions=[SAVE, SETUP],
jump_table=False,
data=bytes.fromhex("0000803f") * 4,
users=[SAVE, SETUP],
)
assert (web.size, web.component, web.functions) == (
28,
"[esphome]web_server",
[MATCH_URL],
)
assert analyzer.components["[esphome]light"].data_size == 44
assert analyzer.components["[esphome]web_server"].ram_total == 28
# Attributed bytes no longer count as unattributed RAM
assert analyzer.sections[".data"].symbol_size == 72
def test_unnamed_constants_skip_garbage_collected(obj_dir: Path) -> None:
"""A constant whose functions the linker dropped is not in the firmware."""
analyzer, run_tool = _esp8266_analyzer(obj_dir, {MATCH_URL})
with patch("esphome.analyze_memory.run_tool", side_effect=run_tool):
analyzer._analyze_object_rodata()
assert [c.component for c in analyzer._unnamed_constants] == ["[esphome]web_server"]
def test_unnamed_constants_skipped_when_rodata_in_flash(obj_dir: Path) -> None:
"""Other platforms keep .rodata in flash, so there is nothing to scan."""
analyzer, run_tool = _esp8266_analyzer(obj_dir, {SETUP, MATCH_URL})
analyzer.rodata_in_ram = False
with patch("esphome.analyze_memory.run_tool", side_effect=run_tool) as mock:
analyzer._analyze_object_rodata()
mock.assert_not_called()
assert analyzer._unnamed_constants == []
@pytest.mark.parametrize(("bind", "expected"), [("WEAK", 1), ("GLOBAL", 2)])
def test_unnamed_constants_inline_copies_counted_once(
obj_dir: Path, bind: str, expected: int
) -> None:
"""Only weak (inline) functions bring the same constant from several objects."""
# The second object emits the same functions, with the same binding
analyzer, run_tool = _esp8266_analyzer(
obj_dir,
{SETUP},
web_index=174,
web_size=0x2C,
web_dump=LIGHT_OBJECT_DUMP,
bind=bind,
web_symbols=(
f" 300: 00000000 64 FUNC {bind:<6} DEFAULT 12 {SETUP}\n"
f" 301: 00000000 64 FUNC {bind:<6} DEFAULT 13 {SAVE}\n"
),
)
with patch("esphome.analyze_memory.run_tool", side_effect=run_tool):
analyzer._analyze_object_rodata()
assert len(analyzer._unnamed_constants) == expected
assert analyzer.sections[".data"].symbol_size == 44 * expected
def test_parse_symbols_keeps_alias_names() -> None:
"""C1/C2 constructor aliases share an address; both names are in the ELF."""
objdump = """4020b558 g F .irom0.text\t0000007e _ZN7esphome3FooC1Ev
4020b558 g F .irom0.text\t0000007e _ZN7esphome3FooC2Ev
"""
analyzer = make_analyzer()
analyzer.sections[".text"] = MemorySection(".text")
with patch(
"esphome.analyze_memory.subprocess.run", return_value=completed(objdump)
):
analyzer._parse_symbols()
assert {"_ZN7esphome3FooC1Ev", "_ZN7esphome3FooC2Ev"} <= analyzer._elf_symbol_names
assert analyzer.sections[".text"].symbol_size == 0x7E
def test_unnamed_constants_skip_named_bytes() -> None:
"""In a section shared with a named object, only the unnamed bytes are shown."""
analyzer = make_analyzer()
analyzer._elf_symbol_names = {"_Z3fooi"}
dump = """Relocation section '.rela.text._Z3fooi' at offset 0x100 contains 1 entry:
Offset Info Type Sym. Value Symbol's Name + Addend
00000000 00003a01 R_XTENSA_32 00000000 .rodata + 4
Hex dump of section '.rodata':
0x00000000 aaaaaaaa 01020304 ........
"""
section = _RodataSection(".rodata", 8, covered=[(0, 4)])
with patch("esphome.analyze_memory.run_tool", return_value=completed(dump)):
(constant,) = analyzer._scan_object_constants(
"foo.o", {"5": section}, "src/foo.o", "[esphome]foo"
)
assert constant.size == 4
assert constant.data == bytes([1, 2, 3, 4])
def test_unnamed_constants_jump_table() -> None:
"""A .rodata section that points into code is a switch jump table."""
analyzer = make_analyzer()
analyzer._elf_symbol_names = {"_Z3fooi"}
dump = """\
Relocation section '.rela.text._Z3fooi' at offset 0x100 contains 1 entry:
Offset Info Type Sym. Value Symbol's Name + Addend
00000000 00003a01 R_XTENSA_32 00000000 .rodata + 0
Relocation section '.rela.rodata' at offset 0x200 contains 2 entries:
Offset Info Type Sym. Value Symbol's Name + Addend
00000000 00003b01 R_XTENSA_32 00000000 .text._Z3fooi + 12
00000004 00003b01 R_XTENSA_32 00000000 .text._Z3fooi + 20
"""
with patch("esphome.analyze_memory.run_tool", return_value=completed(dump)):
(constant,) = analyzer._scan_object_constants(
"foo.o", {"5": _RodataSection(".rodata", 8)}, "src/foo.o", "[esphome]foo"
)
assert constant.jump_table
assert constant.functions == ["_Z3fooi"]
def test_report_lists_unnamed_constants() -> None:
"""The report names the component, the object, the functions and the bytes."""
analyzer = make_analyzer(cls=MemoryAnalyzerCLI)
analyzer.rodata_in_ram = True
analyzer._demangle_cache = {SETUP: "esphome::light::LightState::setup()"}
analyzer._unnamed_constants = [
UnnamedConstant(
size=44,
source_file=LIGHT_OBJ,
component="[esphome]light",
functions=[SETUP],
jump_table=False,
data=bytes.fromhex("0000803f") * 4,
)
]
report = analyzer.generate_report()
assert "ESP8266 keeps .rodata in RAM" in report
assert "Unnamed Constants in RAM (.rodata)" in report
assert f"44 B constant [esphome]light ({LIGHT_OBJ})" in report
assert "used by esphome::light::LightState::setup()" in report
assert "bytes: 00 00 80 3f 00 00 80 3f 00 00 80 3f 00 00 80 3f ..." in report
def test_unnamed_constants_resolve_string_pointers() -> None:
"""A constant of pointers to literals lists the strings, so it can be recognized."""
analyzer = make_analyzer()
analyzer._elf_symbol_names = {"setup"}
relocations = """\
Relocation section '.rela.text.setup' at offset 0x100 contains 1 entry:
Offset Info Type Sym. Value Symbol's Name + Addend
00000000 00003a01 R_XTENSA_32 00000000 .rodata + 0
Relocation section '.rela.rodata' at offset 0xe6fc contains 3 entries:
Offset Info Type Sym. Value Symbol's Name + Addend
00000000 00000501 R_XTENSA_32 00000000 .rodata.str1.1 + 0
00000004 00000501 R_XTENSA_32 00000000 .rodata.str1.1 + 0
00000008 00000501 R_XTENSA_32 00000000 .rodata.str1.1 + 0
Hex dump of section '.rodata':
0x00000000 00000000 0f000000 1e000000 ............
"""
literals = hex_dump(
".rodata.str1.1", b"0.pool.ntp.org\0001.pool.ntp.org\0002.pool.ntp.org\0"
)
def run_tool(cmd: list[str]) -> subprocess.CompletedProcess[str]:
if "-rW" not in cmd:
assert cmd[1:-1] == ["-x", ".rodata.str1.1"]
return completed(literals)
return completed(relocations)
with patch("esphome.analyze_memory.run_tool", side_effect=run_tool):
(constant,) = analyzer._scan_object_constants(
"main.cpp.o",
{"5": _RodataSection(".rodata", 12)},
"src/main.cpp.o",
"[esphome]core",
)
assert constant.strings == ["0.pool.ntp.org", "1.pool.ntp.org", "2.pool.ntp.org"]
cli = make_analyzer(cls=MemoryAnalyzerCLI)
cli._unnamed_constants = [constant]
assert (
'points to "0.pool.ntp.org", "1.pool.ntp.org", "2.pool.ntp.org"'
in cli.generate_report()
)
@pytest.mark.parametrize(
("section", "function"),
[
(".text._Z3foov", "_Z3foov"),
(".literal._Z3foov", "_Z3foov"),
(".irom0.text._Z3foov", "_Z3foov"),
(".text.startup._GLOBAL__sub_I_setup", "_GLOBAL__sub_I_setup"),
(".text.unlikely._Z3foov", "_Z3foov"),
(".iram.text._Z3foov", "_Z3foov"),
(".text.hot_path", "hot_path"),
(".text", None),
],
)
def test_code_section_function(section: str, function: str | None) -> None:
"""Placement prefixes and IRAM code still resolve to the function."""
match = _CODE_SECTION_PATTERN.match(section)
assert match is not None
assert match.group(1) == function
def test_unnamed_constants_same_object_not_merged(obj_dir: Path) -> None:
"""Two equal constants of one inline function in one object are both counted."""
analyzer, run_tool = _esp8266_analyzer(obj_dir, {SETUP}, bind="WEAK")
constant = UnnamedConstant(
size=8,
source_file=LIGHT_OBJ,
component="[esphome]light",
functions=[SAVE, SETUP],
jump_table=False,
data=b"",
)
light = str(obj_dir / LIGHT_OBJ)
with (
patch("esphome.analyze_memory.run_tool", side_effect=run_tool),
patch.object(
analyzer,
"_scan_object_constants",
side_effect=lambda path, *_: [constant, constant] if path == light else [],
),
):
analyzer._analyze_object_rodata()
assert len(analyzer._unnamed_constants) == 2
def test_unnamed_constants_jump_table_only_from_unnamed_bytes() -> None:
"""Code pointers in a named table at the start do not make the rest a jump table."""
analyzer = make_analyzer()
analyzer._elf_symbol_names = {"_Z3fooi"}
dump = """\
Relocation section '.rela.text._Z3fooi' at offset 0x100 contains 1 entry:
Offset Info Type Sym. Value Symbol's Name + Addend
00000000 00003a01 R_XTENSA_32 00000000 .rodata + 4
Relocation section '.rela.rodata' at offset 0x200 contains 1 entry:
Offset Info Type Sym. Value Symbol's Name + Addend
00000000 00003b01 R_XTENSA_32 00000000 .text._Z3fooi + 12
"""
section = _RodataSection(".rodata", 8, covered=[(0, 4)])
with patch("esphome.analyze_memory.run_tool", return_value=completed(dump)):
(constant,) = analyzer._scan_object_constants(
"foo.o", {"5": section}, "src/foo.o", "[esphome]foo"
)
assert not constant.jump_table
def _definitions(rows: dict[str, list[tuple[str, str]]]) -> _FunctionDefinitions:
"""Parse symbol rows (binding, function) per object, as readelf -sW prints them."""
output = "".join(
f"File: {path}\n"
+ "".join(
f" {i}: 00000000 64 FUNC {bind:<6} DEFAULT 12 {name}\n"
for i, (bind, name) in enumerate(symbols)
)
for path, symbols in rows.items()
)
return _parse_unnamed_rodata(output)[1]
def test_linked_copy_unknown_weak_overridden_by_strong() -> None:
"""A strong definition replaces every weak copy, so their constants are not linked."""
definitions = _definitions(
{"a.o": [("WEAK", "_Z3foov")], "b.o": [("GLOBAL", "_Z3foov")]}
)
assert definitions.linked_copy_unknown("a.o", ["_Z3foov"])
assert not definitions.linked_copy_unknown("b.o", ["_Z3foov"])
def test_linked_copy_unknown_weak_partly_overridden() -> None:
"""A constant stays linked while one of its weak users has no strong override."""
definitions = _definitions(
{
"a.o": [("WEAK", "_Z3foov"), ("WEAK", "_Z3barv")],
"b.o": [("GLOBAL", "_Z3foov")],
}
)
assert not definitions.linked_copy_unknown("a.o", ["_Z3barv", "_Z3foov"])
def test_linked_copy_unknown_same_named_locals() -> None:
"""Local functions with one name in two objects can't be told apart in the ELF."""
definitions = _definitions(
{
"a.o": [("LOCAL", "_ZL3barv"), ("LOCAL", "_ZL3bazv")],
"b.o": [("LOCAL", "_ZL3barv")],
}
)
assert definitions.linked_copy_unknown("a.o", ["_ZL3barv"])
# Another, unique user decides it
assert not definitions.linked_copy_unknown("a.o", ["_ZL3barv", "_ZL3bazv"])
def test_unnamed_constants_partial_readelf_output(
obj_dir: Path, caplog: pytest.LogCaptureFixture
) -> None:
"""An unreadable object warns, and the objects readelf did print are still used."""
analyzer, run_tool = _esp8266_analyzer(obj_dir, {SETUP, MATCH_URL})
def failing_run_tool(cmd: list[str]) -> subprocess.CompletedProcess[str]:
result = run_tool(cmd)
if "-SW" in cmd:
return subprocess.CompletedProcess([], 1, result.stdout, "readelf: bad.o")
return result
with patch("esphome.analyze_memory.run_tool", side_effect=failing_run_tool):
analyzer._analyze_object_rodata()
assert len(analyzer._unnamed_constants) == 2
assert "readelf could not read every object: readelf: bad.o" in caplog.text
def test_unnamed_constants_missing_objects_warn(
tmp_path: Path, caplog: pytest.LogCaptureFixture
) -> None:
"""Without object files the report says why constants are missing."""
analyzer = make_analyzer()
analyzer.elf_path = tmp_path / "firmware.elf"
analyzer.rodata_in_ram = True
analyzer._analyze_object_rodata()
assert "No object files next to the ELF" in caplog.text
def test_unnamed_constants_mixed_section_users_by_offset() -> None:
"""Only functions pointing at the unnamed bytes are listed; any user keeps it live."""
analyzer = make_analyzer()
analyzer._elf_symbol_names = {"_Z5namedv"}
dump = """\
Relocation section '.rela.text._Z5namedv' at offset 0x100 contains 1 entry:
Offset Info Type Sym. Value Symbol's Name + Addend
00000000 00003a01 R_XTENSA_32 00000000 .rodata + 0
Relocation section '.rela.text._Z7unnamedv' at offset 0x200 contains 1 entry:
Offset Info Type Sym. Value Symbol's Name + Addend
00000000 00003a01 R_XTENSA_32 00000000 .rodata + 0
""" + hex_dump(".rodata", bytes([0xAA] * 4 + [1, 2, 3, 4]))
# Literal-pool words: _Z5namedv points at offset 0 (named), _Z7unnamedv at 4
code = hex_dump(".text._Z5namedv", (0).to_bytes(4, "little")) + hex_dump(
".text._Z7unnamedv", (4).to_bytes(4, "little")
)
def run_tool(cmd: list[str]) -> subprocess.CompletedProcess[str]:
return completed(dump if "-rW" in cmd else code)
section = _RodataSection(".rodata", 8, covered=[(0, 4)])
with patch("esphome.analyze_memory.run_tool", side_effect=run_tool):
(constant,) = analyzer._scan_object_constants(
"foo.o", {"5": section}, "src/foo.o", "[esphome]foo"
)
# Kept because _Z5namedv is linked, but only _Z7unnamedv uses the unnamed bytes
assert constant.functions == ["_Z7unnamedv"]
@pytest.mark.parametrize(
("referrer", "offset", "addend", "expected"),
[
((3).to_bytes(4, "little"), 0, 0, b"lo"),
((3).to_bytes(4, "little"), 0, -9, None),
((50).to_bytes(4, "little"), 0, 0, None),
(b"\x00\x00", 0, 0, None),
],
)
def test_pointed_literal_bounds(
referrer: bytes, offset: int, addend: int, expected: bytes | None
) -> None:
"""A pointer outside the dumped bytes yields nothing instead of a made-up string."""
assert _pointed_literal(referrer, offset, addend, b"hello\0") == expected
def test_report_escapes_pointed_strings() -> None:
"""Control characters in a literal are escaped in the report."""
analyzer = make_analyzer(cls=MemoryAnalyzerCLI)
analyzer._unnamed_constants = [
UnnamedConstant(
size=8,
source_file="src/main.cpp.o",
component="[esphome]core",
functions=["setup"],
jump_table=False,
data=b"",
strings=["a\nb", "\x1b[2J"],
)
]
report = analyzer.generate_report()
assert 'points to "a\\nb", "\\u001b[2J"' in report
assert "\x1b" not in report
def test_read_pointed_strings_falls_back_on_bad_pointer() -> None:
"""One unreadable pointer drops the string list so the byte preview is shown."""
analyzer = make_analyzer()
table = bytearray((0).to_bytes(4, "little") + (99).to_bytes(4, "little"))
pointers = {".rodata": [(0, ".rodata.str1.1", 0), (4, ".rodata.str1.1", 0)]}
with patch(
"esphome.analyze_memory.run_tool",
return_value=completed(hex_dump(".rodata.str1.1", b"ok\0")),
):
assert analyzer._read_pointed_strings("a.o", pointers, {".rodata": table}) == {}
def test_unnamed_constants_mixed_section_without_unnamed_user(obj_dir: Path) -> None:
"""Unnamed bytes no function points at are still listed and charged."""
analyzer, run_tool = _esp8266_analyzer(obj_dir, {SETUP, MATCH_URL})
constant = UnnamedConstant(
size=4,
source_file=LIGHT_OBJ,
component="[esphome]light",
functions=[],
jump_table=False,
data=b"",
users=[SETUP],
)
light = str(obj_dir / LIGHT_OBJ)
with (
patch("esphome.analyze_memory.run_tool", side_effect=run_tool),
patch.object(
analyzer,
"_scan_object_constants",
side_effect=lambda path, *_: [constant] if path == light else [],
),
):
analyzer._analyze_object_rodata()
assert analyzer._unnamed_constants == [constant]
assert analyzer.components["[esphome]light"].data_size == 4
def test_unnamed_constants_discarded_unique_user_does_not_vouch(obj_dir: Path) -> None:
"""A dropped unique user can't keep a constant whose only linked user is ambiguous."""
analyzer, run_tool = _esp8266_analyzer(obj_dir, {SETUP})
constant = UnnamedConstant(
size=4,
source_file=LIGHT_OBJ,
component="[esphome]light",
functions=[SETUP],
jump_table=False,
data=b"",
# SAVE is unique but was dropped; SETUP is a local defined in two objects
users=[SAVE, SETUP],
)
light = str(obj_dir / LIGHT_OBJ)
with (
patch("esphome.analyze_memory.run_tool", side_effect=run_tool),
patch.object(
analyzer,
"_scan_object_constants",
side_effect=lambda path, *_: [constant] if path == light else [],
),
patch(
"esphome.analyze_memory._FunctionDefinitions.linked_copy_unknown",
side_effect=lambda _path, functions: functions == [SETUP],
),
):
analyzer._analyze_object_rodata()
assert analyzer._unnamed_constants == []
def test_unnamed_constants_single_object(tmp_path: Path) -> None:
"""With one object readelf prints no File: header; its constants are still found."""
single = tmp_path / "obj" / "src" / "only.cpp.o"
single.parent.mkdir(parents=True)
single.touch()
analyzer, _ = _esp8266_analyzer(tmp_path / "obj", {SETUP})
headers = HEADERS_AND_SYMBOLS.split("File: {web}", maxsplit=1)[0].replace(
"File: {light}\n", ""
)
def run_tool(cmd: list[str]) -> subprocess.CompletedProcess[str]:
if "-SW" in cmd:
assert cmd[-1] == str(single)
return completed(headers.format(bind="GLOBAL", setup=SETUP, save=SAVE))
return completed(LIGHT_OBJECT_DUMP)
with patch("esphome.analyze_memory.run_tool", side_effect=run_tool):
analyzer._analyze_object_rodata()
(constant,) = analyzer._unnamed_constants
assert (constant.size, constant.source_file) == (44, "src/only.cpp.o")