From ebc9619e3be6b6b96f3fb65859271ed915a0a85b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 20 Oct 2026 05:36:38 -1000 Subject: [PATCH] [sx126x] Pass the sx1261 hardware version as a flag (#21202) --- esphome/components/sx126x/__init__.py | 3 ++- esphome/components/sx126x/sx126x.cpp | 2 +- esphome/components/sx126x/sx126x.h | 22 +++++++++---------- .../sx126x/config/hw_version_sx1261.yaml | 21 ++++++++++++++++++ .../sx126x/test_packet_tables.py | 10 +++++++++ 5 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 tests/component_tests/sx126x/config/hw_version_sx1261.yaml diff --git a/esphome/components/sx126x/__init__.py b/esphome/components/sx126x/__init__.py index 79798a73cce..3a028b8dfb9 100644 --- a/esphome/components/sx126x/__init__.py +++ b/esphome/components/sx126x/__init__.py @@ -253,7 +253,8 @@ async def to_code(config: ConfigType) -> None: await cg.add_pin(var.set_busy_pin, config[CONF_BUSY_PIN]) cg.add(var.set_bandwidth(config[CONF_BANDWIDTH])) cg.add(var.set_frequency(config[CONF_FREQUENCY])) - cg.add(var.set_hw_version(config[CONF_HW_VERSION])) + if config[CONF_HW_VERSION] == "sx1261": + cg.add(var.set_sx1261(True)) cg.add(var.set_deviation(config[CONF_DEVIATION])) cg.add(var.set_modulation(config[CONF_MODULATION])) cg.add(var.set_pa_ramp(config[CONF_PA_RAMP])) diff --git a/esphome/components/sx126x/sx126x.cpp b/esphome/components/sx126x/sx126x.cpp index 6c95fe5f7a6..c770ec7697e 100644 --- a/esphome/components/sx126x/sx126x.cpp +++ b/esphome/components/sx126x/sx126x.cpp @@ -185,7 +185,7 @@ void SX126x::configure() { // configure pa int8_t pa_power = this->pa_power_; - if (this->hw_version_ == "sx1261") { + if (this->sx1261_) { // the following values were taken from section 13.1.14.1 table 13-21 // in rev 2.1 of the datasheet if (pa_power == 15) { diff --git a/esphome/components/sx126x/sx126x.h b/esphome/components/sx126x/sx126x.h index aa275c9e972..c853d7c9ed0 100644 --- a/esphome/components/sx126x/sx126x.h +++ b/esphome/components/sx126x/sx126x.h @@ -78,7 +78,7 @@ class SX126x final : public Component, void set_deviation(uint32_t deviation) { this->deviation_ = deviation; } void set_dio1_pin(GPIOPin *dio1_pin) { this->dio1_pin_ = dio1_pin; } void set_frequency(uint32_t frequency) { this->frequency_ = frequency; } - void set_hw_version(const std::string &hw_version) { this->hw_version_ = hw_version; } + void set_sx1261(bool sx1261) { this->sx1261_ = sx1261; } void set_mode_rx(); void set_mode_tx(); void set_mode_standby(SX126xStandbyMode mode); @@ -126,23 +126,19 @@ class SX126x final : public Component, GPIOPin *busy_pin_{nullptr}; GPIOPin *dio1_pin_{nullptr}; GPIOPin *rst_pin_{nullptr}; - std::string hw_version_; - char version_[16]; - SX126xBw bandwidth_{SX126X_BW_125000}; uint32_t bitrate_{0}; - bool crc_enable_{false}; - bool crc_inverted_{false}; - uint8_t crc_size_{0}; - uint16_t crc_polynomial_{0}; - uint16_t crc_initial_{0}; - bool whitening_enable_{false}; - uint16_t whitening_initial_{0}; uint32_t deviation_{0}; uint32_t frequency_{0}; uint32_t payload_length_{0}; uint32_t tcxo_delay_{0}; + uint16_t crc_polynomial_{0}; + uint16_t crc_initial_{0}; + uint16_t whitening_initial_{0}; uint16_t preamble_detect_{0}; uint16_t preamble_size_{0}; + char version_[16]; + SX126xBw bandwidth_{SX126X_BW_125000}; + uint8_t crc_size_{0}; uint8_t tcxo_voltage_{0}; uint8_t coding_rate_{0}; uint8_t modulation_{PACKET_TYPE_LORA}; @@ -150,6 +146,10 @@ class SX126x final : public Component, uint8_t shaping_{0}; uint8_t spreading_factor_{0}; int8_t pa_power_{0}; + bool sx1261_{false}; + bool crc_enable_{false}; + bool crc_inverted_{false}; + bool whitening_enable_{false}; bool rx_start_{false}; bool rf_switch_{false}; }; diff --git a/tests/component_tests/sx126x/config/hw_version_sx1261.yaml b/tests/component_tests/sx126x/config/hw_version_sx1261.yaml new file mode 100644 index 00000000000..2d94535383d --- /dev/null +++ b/tests/component_tests/sx126x/config/hw_version_sx1261.yaml @@ -0,0 +1,21 @@ +esphome: + name: test + +esp32: + board: esp32dev + +spi: + clk_pin: 18 + mosi_pin: 23 + miso_pin: 19 + +sx126x: + cs_pin: 12 + rst_pin: 13 + busy_pin: 25 + dio1_pin: 26 + frequency: 433920000 + hw_version: sx1261 + modulation: LORA + bandwidth: 125_0kHz + rf_switch: true diff --git a/tests/component_tests/sx126x/test_packet_tables.py b/tests/component_tests/sx126x/test_packet_tables.py index 9ee3379f085..aebbb9efd21 100644 --- a/tests/component_tests/sx126x/test_packet_tables.py +++ b/tests/component_tests/sx126x/test_packet_tables.py @@ -28,6 +28,7 @@ def test_constant_packets_share_progmem_tables( shared = next(k for k, v in tables.items() if v == "{0xC5, 0x51, 0x78, 0x82}") assert main_cpp.count(f"set_data_static({shared}, 4);") == 2 assert "set_data_template(" in main_cpp + assert "set_sx1261" not in main_cpp def test_packet_data_length_limit() -> None: @@ -37,3 +38,12 @@ def test_packet_data_length_limit() -> None: validate_packet_data([0x01] * 256) with pytest.raises(cv.Invalid): validate_packet_data([]) + + +def test_hw_version_sx1261_sets_flag( + generate_main: Callable[[str | Path], str], + component_config_path: Callable[[str], Path], +) -> None: + """An sx1261 sets the flag.""" + main_cpp = generate_main(component_config_path("hw_version_sx1261.yaml")) + assert "set_sx1261(true);" in main_cpp