[sx126x] Pass the sx1261 hardware version as a flag (#21202)

This commit is contained in:
J. Nick Koston
2026-10-20 11:36:38 -04:00
committed by GitHub
parent c0cbf9cdfd
commit ebc9619e3b
5 changed files with 45 additions and 13 deletions
+2 -1
View File
@@ -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]))
+1 -1
View File
@@ -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) {
+11 -11
View File
@@ -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};
};
@@ -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
@@ -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