diff --git a/esphome/components/sx127x/sx127x.cpp b/esphome/components/sx127x/sx127x.cpp index e41efe098c..7f62ee2bd3 100644 --- a/esphome/components/sx127x/sx127x.cpp +++ b/esphome/components/sx127x/sx127x.cpp @@ -252,15 +252,17 @@ size_t SX127x::get_max_packet_size() { } } -void SX127x::transmit_packet(const std::vector &packet) { +SX127xError SX127x::transmit_packet(const std::vector &packet) { if (this->payload_length_ > 0 && this->payload_length_ != packet.size()) { ESP_LOGE(TAG, "Packet size does not match config"); - return; + return SX127xError::INVALID_PARAMS; } if (packet.empty() || packet.size() > this->get_max_packet_size()) { ESP_LOGE(TAG, "Packet size out of range"); - return; + return SX127xError::INVALID_PARAMS; } + + SX127xError ret = SX127xError::NONE; if (this->modulation_ == MOD_LORA) { this->set_mode_standby(); if (this->payload_length_ == 0) { @@ -278,11 +280,13 @@ void SX127x::transmit_packet(const std::vector &packet) { this->write_fifo_(packet); this->set_mode_tx(); } + // wait until transmit completes, typically the delay will be less than 100 ms uint32_t start = millis(); while (!this->dio0_pin_->digital_read()) { if (millis() - start > 4000) { ESP_LOGE(TAG, "Transmit packet failure"); + ret = SX127xError::TIMEOUT; break; } } @@ -291,6 +295,7 @@ void SX127x::transmit_packet(const std::vector &packet) { } else { this->set_mode_sleep(); } + return ret; } void SX127x::call_listeners_(const std::vector &packet, float rssi, float snr) { @@ -335,13 +340,7 @@ void SX127x::loop() { } void SX127x::run_image_cal() { - uint32_t start = millis(); - uint8_t mode = this->read_register_(REG_OP_MODE); - if ((mode & MODE_MASK) != MODE_STDBY) { - ESP_LOGE(TAG, "Need to be in standby for image cal"); - return; - } - if (mode & MOD_LORA) { + if (this->modulation_ == MOD_LORA) { this->set_mode_(MOD_FSK, MODE_SLEEP); this->set_mode_(MOD_FSK, MODE_STDBY); } @@ -350,13 +349,15 @@ void SX127x::run_image_cal() { } else { this->write_register_(REG_IMAGE_CAL, IMAGE_CAL_START); } + uint32_t start = millis(); while (this->read_register_(REG_IMAGE_CAL) & IMAGE_CAL_RUNNING) { if (millis() - start > 20) { ESP_LOGE(TAG, "Image cal failure"); + this->mark_failed(); break; } } - if (mode & MOD_LORA) { + if (this->modulation_ == MOD_LORA) { this->set_mode_(this->modulation_, MODE_SLEEP); this->set_mode_(this->modulation_, MODE_STDBY); } @@ -375,6 +376,7 @@ void SX127x::set_mode_(uint8_t modulation, uint8_t mode) { } if (millis() - start > 20) { ESP_LOGE(TAG, "Set mode failure"); + this->mark_failed(); break; } } diff --git a/esphome/components/sx127x/sx127x.h b/esphome/components/sx127x/sx127x.h index fe9f60e860..4cc7c9b6d3 100644 --- a/esphome/components/sx127x/sx127x.h +++ b/esphome/components/sx127x/sx127x.h @@ -34,6 +34,8 @@ enum SX127xBw : uint8_t { SX127X_BW_500_0, }; +enum class SX127xError { NONE = 0, TIMEOUT, INVALID_PARAMS }; + class SX127xListener { public: virtual void on_packet(const std::vector &packet, float rssi, float snr) = 0; @@ -79,7 +81,7 @@ class SX127x : public Component, void set_sync_value(const std::vector &sync_value) { this->sync_value_ = sync_value; } void run_image_cal(); void configure(); - void transmit_packet(const std::vector &packet); + SX127xError transmit_packet(const std::vector &packet); void register_listener(SX127xListener *listener) { this->listeners_.push_back(listener); } Trigger, float, float> *get_packet_trigger() const { return this->packet_trigger_; };