diff --git a/esphome/components/sx126x/__init__.py b/esphome/components/sx126x/__init__.py index a4ba5c34f3..29e3ad5359 100644 --- a/esphome/components/sx126x/__init__.py +++ b/esphome/components/sx126x/__init__.py @@ -41,6 +41,8 @@ CONF_SPREADING_FACTOR = "spreading_factor" CONF_SYNC_VALUE = "sync_value" CONF_TCXO_VOLTAGE = "tcxo_voltage" CONF_TCXO_DELAY = "tcxo_delay" +CONF_WHITENING_ENABLE = "whitening_enable" +CONF_WHITENING_INITIAL = "whitening_initial" sx126x_ns = cg.esphome_ns.namespace("sx126x") SX126x = sx126x_ns.class_("SX126x", cg.Component, spi.SPIDevice) @@ -232,6 +234,10 @@ CONFIG_SCHEMA = ( cv.positive_time_period_microseconds, cv.Range(max=TimePeriod(microseconds=262144000)), ), + cv.Optional(CONF_WHITENING_ENABLE, default=False): cv.boolean, + cv.Optional(CONF_WHITENING_INITIAL, default=0x0100): cv.All( + cv.hex_int, cv.Range(min=0, max=0x1FF) + ), }, ) .extend(cv.COMPONENT_SCHEMA) @@ -285,6 +291,8 @@ async def to_code(config: ConfigType) -> None: cg.add(var.set_rf_switch(config[CONF_RF_SWITCH])) cg.add(var.set_tcxo_voltage(config[CONF_TCXO_VOLTAGE])) cg.add(var.set_tcxo_delay(config[CONF_TCXO_DELAY])) + cg.add(var.set_whitening_enable(config[CONF_WHITENING_ENABLE])) + cg.add(var.set_whitening_initial(config[CONF_WHITENING_INITIAL])) NO_ARGS_ACTION_SCHEMA = automation.maybe_simple_id( diff --git a/esphome/components/sx126x/sx126x.cpp b/esphome/components/sx126x/sx126x.cpp index aed0105e1f..af42c63bf4 100644 --- a/esphome/components/sx126x/sx126x.cpp +++ b/esphome/components/sx126x/sx126x.cpp @@ -251,6 +251,16 @@ void SX126x::configure() { this->write_register_(REG_CRC_POLYNOMIAL, buf, 2); } + // set whitening params + if (this->whitening_enable_) { + // according to the datasheet, section 12 table 12-1 "The user should not + // change the value of the 7 MSB of this register" + this->read_register_(REG_WHITENING_INITIAL, buf, 1); + buf[0] = (buf[0] & 0xFE) | ((this->whitening_initial_ >> 8) & 0x01); + buf[1] = this->whitening_initial_ & 0xFF; + this->write_register_(REG_WHITENING_INITIAL, buf, 2); + } + // set packet params and sync word this->set_packet_params_(this->get_max_packet_size()); if (!this->sync_value_.empty()) { @@ -297,7 +307,7 @@ void SX126x::set_packet_params_(uint8_t payload_length) { } else { buf[7] = 0x01; } - buf[8] = 0x00; + buf[8] = (this->whitening_enable_) ? 0x01 : 0x00; this->write_opcode_(RADIO_SET_PACKETPARAMS, buf, 9); } } @@ -541,6 +551,10 @@ void SX126x::dump_config() { ESP_LOGCONFIG(TAG, " Sync Value: 0x%s", format_hex_to(hex_buf, this->sync_value_.data(), this->sync_value_.size())); } + ESP_LOGCONFIG(TAG, " Whitening Enable: %s", TRUEFALSE(this->whitening_enable_)); + if (this->whitening_enable_) { + ESP_LOGCONFIG(TAG, " Whitening Initial: 0x%03x", this->whitening_initial_); + } if (this->is_failed()) { ESP_LOGE(TAG, "Configuring SX126x failed"); } diff --git a/esphome/components/sx126x/sx126x.h b/esphome/components/sx126x/sx126x.h index 8298beb36e..6816084df0 100644 --- a/esphome/components/sx126x/sx126x.h +++ b/esphome/components/sx126x/sx126x.h @@ -71,6 +71,8 @@ class SX126x : public Component, void set_crc_size(uint8_t crc_size) { this->crc_size_ = crc_size; } void set_crc_polynomial(uint16_t crc_polynomial) { this->crc_polynomial_ = crc_polynomial; } void set_crc_initial(uint16_t crc_initial) { this->crc_initial_ = crc_initial; } + void set_whitening_enable(bool whitening_enable) { this->whitening_enable_ = whitening_enable; } + void set_whitening_initial(uint16_t whitening_initial) { this->whitening_initial_ = whitening_initial; } 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; } @@ -128,6 +130,8 @@ class SX126x : public Component, 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}; diff --git a/esphome/components/sx126x/sx126x_reg.h b/esphome/components/sx126x/sx126x_reg.h index c70817364f..197a2aaadb 100644 --- a/esphome/components/sx126x/sx126x_reg.h +++ b/esphome/components/sx126x/sx126x_reg.h @@ -52,6 +52,7 @@ enum SX126xOpCode : uint8_t { enum SX126xRegister : uint16_t { REG_VERSION_STRING = 0x0320, + REG_WHITENING_INITIAL = 0x06B8, REG_CRC_INITIAL = 0x06BC, REG_CRC_POLYNOMIAL = 0x06BE, REG_GFSK_SYNCWORD = 0x06C0, diff --git a/tests/components/sx126x/common.yaml b/tests/components/sx126x/common.yaml index a4a24d8da7..05794ad1a8 100644 --- a/tests/components/sx126x/common.yaml +++ b/tests/components/sx126x/common.yaml @@ -21,6 +21,8 @@ sx126x: coding_rate: CR_4_6 tcxo_voltage: 1_8V tcxo_delay: 5ms + whitening_enable: false + whitening_initial: 0x1FF on_packet: then: - lambda: |-