8static const char *
const TAG =
"sx126x";
9static const uint16_t RAMP[8] = {10, 20, 40, 80, 200, 800, 1700, 3400};
10static const uint32_t BW_HZ[31] = {4800, 5800, 7300, 9700, 11700, 14600, 19500, 23400, 29300, 39000, 46900,
11 58600, 78200, 93800, 117300, 156200, 187200, 234300, 312000, 373600, 467000, 7810,
12 10420, 15630, 20830, 31250, 41670, 62500, 125000, 250000, 500000};
15static const uint8_t BW_FSK[21] = {
20static constexpr uint32_t RESET_DELAY_HIGH_US = 5000;
21static constexpr uint32_t RESET_DELAY_LOW_US = 2000;
22static constexpr uint32_t SWITCHING_DELAY_US = 1;
23static constexpr uint32_t TRANSMIT_TIMEOUT_MS = 4000;
24static constexpr uint32_t BUSY_TIMEOUT_MS = 20;
27static constexpr uint8_t OCP_80MA = 0x18;
28static constexpr uint8_t OCP_140MA = 0x38;
31static constexpr float LOW_DATA_RATE_OPTIMIZE_THRESHOLD = 16.38f;
39 for (uint8_t &
byte : packet) {
51 for (
const uint8_t &
byte : packet) {
63 for (int32_t i = 0; i <
size; i++) {
74 for (int32_t i = 0; i <
size; i++) {
88 for (int32_t i = 0; i <
size; i++) {
100 for (int32_t i = 0; i <
size; i++) {
138 buf[1] = (
delay >> 16) & 0xFF;
139 buf[2] = (
delay >> 8) & 0xFF;
140 buf[3] = (
delay >> 0) & 0xFF;
159 this->version_[
sizeof(this->
version_) - 1] =
'\0';
160 if (strncmp(this->version_,
"SX126", 5) != 0 && strncmp(this->version_,
"LLCC68", 6) != 0) {
173 uint64_t freq = ((uint64_t) this->
frequency_ << 25) / XTAL_FREQ;
174 buf[0] = (uint8_t) ((freq >> 24) & 0xFF);
175 buf[1] = (uint8_t) ((freq >> 16) & 0xFF);
176 buf[2] = (uint8_t) ((freq >> 8) & 0xFF);
177 buf[3] = (uint8_t) (freq & 0xFF);
185 if (pa_power == 15) {
186 uint8_t cfg[4] = {0x06, 0x00, 0x01, 0x01};
189 uint8_t cfg[4] = {0x04, 0x00, 0x01, 0x01};
192 pa_power = std::max(pa_power, (int8_t) -3);
193 pa_power = std::min(pa_power, (int8_t) 14);
199 uint8_t cfg[4] = {0x04, 0x07, 0x00, 0x01};
201 pa_power = std::max(pa_power, (int8_t) -3);
202 pa_power = std::min(pa_power, (int8_t) 22);
217 buf[3] = (
duration > LOW_DATA_RATE_OPTIMIZE_THRESHOLD) ? 0x01 : 0x00;
229 buf[0] = (bitrate >> 16) & 0xFF;
230 buf[1] = (bitrate >> 8) & 0xFF;
231 buf[2] = (bitrate >> 0) & 0xFF;
234 buf[5] = (fdev >> 16) & 0xFF;
235 buf[6] = (fdev >> 8) & 0xFF;
236 buf[7] = (fdev >> 0) & 0xFF;
277 buf[3] = payload_length;
289 buf[6] = payload_length;
302 ESP_LOGE(TAG,
"Packet size does not match config");
305 if (packet.empty() || packet.size() > this->get_max_packet_size()) {
306 ESP_LOGE(TAG,
"Packet size out of range");
321 if (
millis() - start > TRANSMIT_TIMEOUT_MS) {
322 ESP_LOGE(TAG,
"Transmit packet failure");
345 listener->on_packet(packet, rssi, snr);
361 status = (buf[0] << 8) | buf[1];
383 uint8_t buf[2] = {0, 0};
400 if (buf[0] > 0 && buf[1] > 0) {
410 buf[0] = (irq >> 8) & 0xFF;
411 buf[1] = (irq >> 0) & 0xFF;
412 buf[2] = (irq >> 8) & 0xFF;
413 buf[3] = (irq >> 0) & 0xFF;
436 buf[0] = (irq >> 8) & 0xFF;
437 buf[1] = (irq >> 0) & 0xFF;
438 buf[2] = (irq >> 8) & 0xFF;
439 buf[3] = (irq >> 0) & 0xFF;
470 if (
millis() - start > BUSY_TIMEOUT_MS) {
471 ESP_LOGE(TAG,
"Wait busy timeout");
479 ESP_LOGCONFIG(TAG,
"SX126x:");
480 LOG_PIN(
" CS Pin: ", this->
cs_);
482 LOG_PIN(
" RST Pin: ", this->
rst_pin_);
485 " HW Version: %15s\n"
486 " Frequency: %" PRIu32
" Hz\n"
487 " Bandwidth: %" PRIu32
" Hz\n"
488 " PA Power: %" PRId8
" dBm\n"
489 " PA Ramp: %" PRIu16
" us\n"
490 " Payload Length: %" PRIu32
"\n"
496 const char *shaping =
"NONE";
498 shaping =
"GAUSSIAN_BT_0_3";
500 shaping =
"GAUSSIAN_BT_0_5";
502 shaping =
"GAUSSIAN_BT_0_7";
504 shaping =
"GAUSSIAN_BT_1_0";
508 " Deviation: %" PRIu32
" Hz\n"
510 " Preamble Size: %" PRIu16
"\n"
511 " Preamble Detect: %" PRIu16
"\n"
512 " Bitrate: %" PRIu32
"b/s",
515 const char *cr =
"4/8";
524 " Modulation: LORA\n"
525 " Spreading Factor: %" PRIu8
"\n"
527 " Preamble Size: %" PRIu16,
532 ESP_LOGCONFIG(TAG,
" Sync Value: 0x%s",
536 ESP_LOGE(TAG,
"Configuring SX126x failed");
BedjetMode mode
BedJet operating mode.
void mark_failed()
Mark this component as failed.
virtual void digital_write(bool value)=0
virtual bool digital_read()=0
void trigger(const Ts &...x)
Inform the parent automation that the event has triggered.
void spi_setup() override
void write_byte(uint8_t data)
uint8_t transfer_byte(uint8_t data)
uint8_t read_fifo_(uint8_t offset, std::vector< uint8_t > &packet)
void write_opcode_(uint8_t opcode, uint8_t *data, uint8_t size)
std::vector< uint8_t > sync_value_
std::vector< SX126xListener * > listeners_
size_t get_max_packet_size()
void set_mode_standby(SX126xStandbyMode mode)
Trigger< std::vector< uint8_t >, float, float > packet_trigger_
uint8_t read_opcode_(uint8_t opcode, uint8_t *data, uint8_t size)
void dump_config() override
void read_register_(uint16_t reg, uint8_t *data, uint8_t size)
void write_register_(uint16_t reg, uint8_t *data, uint8_t size)
void set_packet_params_(uint8_t payload_length)
void call_listeners_(const std::vector< uint8_t > &packet, float rssi, float snr)
std::vector< uint8_t > packet_
uint16_t preamble_detect_
SX126xError transmit_packet(const std::vector< uint8_t > &packet)
uint8_t spreading_factor_
void write_fifo_(uint8_t offset, const std::vector< uint8_t > &packet)
@ RADIO_SET_MODULATIONPARAMS
@ RADIO_SET_LORASYMBTIMEOUT
@ RADIO_GET_RXBUFFERSTATUS
Providing packet encoding functions for exchanging data with a remote host.
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
void HOT delay(uint32_t ms)
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).