12static constexpr uint32_t FXOSC = 32000000u;
13static constexpr uint8_t CHIP_VERSION = 0x24;
14static constexpr uint32_t TRANSMIT_TIMEOUT_MS = 4000;
15static constexpr uint32_t MODE_TIMEOUT_MS = 50;
16static constexpr uint32_t RECONFIGURE_INTERVAL_ID = 0;
17static constexpr uint32_t RECONFIGURE_INTERVAL_MS = 1000;
18static constexpr uint32_t RX_STALL_CHECK_INTERVAL_MS = 1000;
20static constexpr uint8_t RX_STALL_TIMEOUT = 0xFF;
23PROGMEM_STRING_TABLE(SX1231ErrorStrings,
"none",
"timeout",
"invalid parameters",
"radio not ready",
"radio busy");
25static const LogString *set_mode_failure() {
return LOG_STR(
"Set mode failure"); }
26static constexpr uint16_t RAMP[16] PROGMEM = {3400, 2000, 1000, 500, 250, 125, 100, 62, 50, 40, 31, 25, 20, 15, 12, 10};
28static uint32_t bandwidth_hz(uint8_t rx_bw) {
return FXOSC / ((16 + 4 * (rx_bw >> 3)) << ((rx_bw & 0x07) + 2)); }
31PROGMEM_STRING_TABLE(ShapingStrings,
"NONE",
"GAUSSIAN_BT_1_0",
"GAUSSIAN_BT_0_5",
"GAUSSIAN_BT_0_3",
"NONE",
32 "CUTOFF_BR_X_1",
"CUTOFF_BR_X_2");
47 for (
size_t i = 0; i <
len; i++) {
64 if (reason !=
nullptr) {
80 if (reason !=
nullptr) {
98 this->
set_interval(RECONFIGURE_INTERVAL_ID, RECONFIGURE_INTERVAL_MS, [
this]() { this->
configure(); });
109 return LOG_STR(
"Invalid bandwidth");
113 return LOG_STR(
"Invalid bitrate");
117 return LOG_STR(
"Invalid payload length");
120 const int8_t pa_power = clamp<int8_t>(this->
pa_power_, boost ? -2 : -18, boost ? 20 : 13);
122 ESP_LOGW(TAG,
"PA power %d dBm is out of range for the PA pin, using %d dBm", this->
pa_power_, pa_power);
125 const bool high_power = this->
pa_power_ > 17;
135 if (version != CHIP_VERSION) {
136 ESP_LOGE(TAG,
"Chip version 0x%02X, expected 0x%02X", version, CHIP_VERSION);
137 return LOG_STR(
"Unsupported chip version");
141 return set_mode_failure();
155 uint64_t frf = ((uint64_t) this->
frequency_ << 19) / FXOSC;
156 uint8_t frf_bytes[3];
207 return set_mode_failure();
216 return SX1231_MAX_PACKET_SIZE;
225 if (
millis() - start >= TRANSMIT_TIMEOUT_MS) {
232 this->
fail_(set_mode_failure());
236 ESP_LOGW(TAG,
"Packet send failed: %s", LOG_STR_ARG(SX1231ErrorStrings::get_log_str(
result)));
247 (this->applied_.payload_length > 0 && this->applied_.payload_length !=
len)) {
251 ESP_LOGE(TAG,
"DIO0 pin not configured, cannot wait for transmit completion");
260 this->
fail_(set_mode_failure());
271 this->
fail_(set_mode_failure());
301#ifdef SX1231_LISTENER_COUNT
303 if (this->
listeners_.size() == SX1231_LISTENER_COUNT) {
304 ESP_LOGE(TAG,
"Listener list full; call sx1231.request_listener_slot(radio_id) from the component's to_code");
312#ifdef SX1231_LISTENER_COUNT
314 listener->on_packet(packet, rssi, snr);
335 if (payload_length == 0) {
338 if (payload_length == 0 || payload_length > SX1231_MAX_PACKET_SIZE) {
339 ESP_LOGW(TAG,
"Dropping packet with invalid length %u", payload_length);
344 uint8_t packet[SX1231_MAX_PACKET_SIZE];
346 this->
call_listeners_(std::span<const uint8_t>(packet, payload_length), rssi, NAN);
357 ESP_LOGV(TAG,
"Restarting stalled receiver");
378 if (
millis() - start > MODE_TIMEOUT_MS) {
387 ESP_LOGW(TAG,
"Radio unavailable, mode change ignored");
391 this->
fail_(set_mode_failure());
404 ESP_LOGCONFIG(TAG,
"SX1231:");
405 LOG_PIN(
" CS Pin: ", this->
cs_);
406 LOG_PIN(
" RST Pin: ", this->
rst_pin_);
409 " Frequency: %" PRIu32
" Hz\n"
410 " Bandwidth: %" PRIu32
" Hz\n"
412 " PA Power: %" PRId8
" dBm\n"
413 " PA Ramp: %" PRIu16
" us\n"
420 ESP_LOGCONFIG(TAG,
" Deviation: %" PRIu32
" Hz", this->
deviation_);
424 " Bitrate: %" PRIu32
"b/s\n"
427 " Rx Floor: %.1f dBm\n"
433 ESP_LOGCONFIG(TAG,
" CRC Enable: %s", TRUEFALSE(this->
crc_enable_));
439 char hex_buf[SX1231_MAX_SYNC_SIZE * 2 + 1];
440 ESP_LOGCONFIG(TAG,
" Sync Value: 0x%s",
444 ESP_LOGCONFIG(TAG,
" Preamble Size: %" PRIu16, this->
preamble_size_);
BedjetMode mode
BedJet operating mode.
void feed_wdt()
Feed the task watchdog.
Read-only view of bytes in a buffer owned elsewhere, valid only until that buffer is written again.
void mark_failed()
Mark this component as failed.
void status_set_warning()
bool cancel_interval(const char *name)
Cancel an interval function.
void enable_loop()
Enable this component's loop.
void disable_loop()
Disable this component's loop.
void status_clear_warning()
void set_interval(const char *name, uint32_t interval, SchedulerCallback &&f)
Set an interval function with a const char* name.
bool digital_read() override
uint8_t read_register(const uint8_t(&header)[N])
void write_register(const uint8_t(&header)[N], uint8_t value)
void spi_setup() override
void read_registers(const uint8_t(&header)[N], uint8_t *data, size_t length)
void write_registers(const uint8_t(&header)[N], const uint8_t *data, size_t length)
void write_byte(uint8_t data)
void change_mode_(uint8_t mode)
SX1231Error start_transmit_(const uint8_t *data, size_t len)
void fail_(const LogString *reason)
void write_fifo_(const uint8_t *data, size_t len)
SX1231Error transmit_packet(const uint8_t *data, size_t len)
void dump_config() override
void restart_stalled_rx_()
uint8_t read_register_(uint8_t reg)
void call_listeners_(std::span< const uint8_t > packet, float rssi, float snr)
void write_register_(uint8_t reg, uint8_t value)
StaticVector< SX1231Listener *, SX1231_LISTENER_COUNT > listeners_
size_t get_max_packet_size()
const LogString * configure_radio_()
LazyCallbackManager< void(ByteView, float, float)> packet_callback_
StaticVector< uint8_t, SX1231_MAX_SYNC_SIZE > sync_value_
bool reconfigure_pending_
void register_listener(SX1231Listener *listener)
Listeners must request a slot from their to_code: sx1231.request_listener_slot(radio_id).
void write_register16_(uint8_t msb_reg, uint16_t value)
void write_registers_(uint8_t reg, const uint8_t *data, size_t len)
InternalGPIOPin * dio0_pin_
bool set_mode_(uint8_t mode)
@ RADIO_STATE_NEEDS_CONFIGURATION
@ SX1231_ERROR_INVALID_PARAMS
@ TX_START_FIFO_NOT_EMPTY
ESPHOME_LOG_TAG(TAG, "sx1231")
PROGMEM_STRING_TABLE(SX1231ErrorStrings, "none", "timeout", "invalid parameters", "radio not ready", "radio busy")
@ CONTINUOUS_BIT_SYNC_OFF
T clamp_at_most(T value, U max)
void pulse_pin(GPIOPin *pin, bool level, uint32_t width_us)
Drive pin to level for width_us microseconds, then to the opposite level.
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
uint16_t progmem_read_uint16(const uint16_t *addr)
constexpr uint8_t * put_be16(uint8_t *dst, uint16_t value)
Write the low 16, 24 or 32 bits of value to dst, most significant byte first (be) or least significan...
constexpr uint8_t * put_be24(uint8_t *dst, uint32_t value)
void setup_output_pin(GPIOPin *pin, bool level)
Set up pin and drive it to level. Does nothing when pin is null, so an optional pin needs no check.
bool setup_wake_interrupt(GPIOPin *pin, Component *component, gpio::InterruptType type)
Set up pin and attach the wake interrupt when it is a GPIO of the chip itself.
uint32_t IRAM_ATTR HOT millis()
char * format_hex_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length)
Format byte array as lowercase hex to buffer (base implementation).
Application App
Global storage of Application pointer - only one Application can exist.