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++) {
108 ESP_LOGCONFIG(TAG,
"Running setup");
140 buf[1] = (
delay >> 16) & 0xFF;
141 buf[2] = (
delay >> 8) & 0xFF;
142 buf[3] = (
delay >> 0) & 0xFF;
161 if (strncmp(this->version_,
"SX126", 5) != 0 && strncmp(this->version_,
"LLCC68", 6) != 0) {
174 uint64_t freq = ((uint64_t) this->
frequency_ << 25) / XTAL_FREQ;
175 buf[0] = (uint8_t) ((freq >> 24) & 0xFF);
176 buf[1] = (uint8_t) ((freq >> 16) & 0xFF);
177 buf[2] = (uint8_t) ((freq >> 8) & 0xFF);
178 buf[3] = (uint8_t) (freq & 0xFF);
186 if (pa_power == 15) {
187 uint8_t cfg[4] = {0x06, 0x00, 0x01, 0x01};
190 uint8_t cfg[4] = {0x04, 0x00, 0x01, 0x01};
193 pa_power = std::max(pa_power, (int8_t) -3);
194 pa_power = std::min(pa_power, (int8_t) 14);
200 uint8_t cfg[4] = {0x04, 0x07, 0x00, 0x01};
202 pa_power = std::max(pa_power, (int8_t) -3);
203 pa_power = std::min(pa_power, (int8_t) 22);
218 buf[3] = (
duration > LOW_DATA_RATE_OPTIMIZE_THRESHOLD) ? 0x01 : 0x00;
228 uint32_t bitrate = ((uint64_t) XTAL_FREQ * 32) / this->
bitrate_;
229 uint32_t fdev = ((uint64_t) this->
deviation_ << 25) / XTAL_FREQ;
230 buf[0] = (bitrate >> 16) & 0xFF;
231 buf[1] = (bitrate >> 8) & 0xFF;
232 buf[2] = (bitrate >> 0) & 0xFF;
235 buf[5] = (fdev >> 16) & 0xFF;
236 buf[6] = (fdev >> 8) & 0xFF;
237 buf[7] = (fdev >> 0) & 0xFF;
268 buf[3] = payload_length;
280 buf[6] = payload_length;
289 ESP_LOGE(TAG,
"Packet size does not match config");
292 if (packet.empty() || packet.size() > this->get_max_packet_size()) {
293 ESP_LOGE(TAG,
"Packet size out of range");
306 uint32_t start =
millis();
308 if (
millis() - start > TRANSMIT_TIMEOUT_MS) {
309 ESP_LOGE(TAG,
"Transmit packet failure");
329 listener->on_packet(packet, rssi, snr);
345 status = (buf[0] << 8) | buf[1];
367 uint8_t buf[2] = {0, 0};
384 if (buf[0] > 0 && buf[1] > 0) {
394 buf[0] = (irq >> 8) & 0xFF;
395 buf[1] = (irq >> 0) & 0xFF;
396 buf[2] = (irq >> 8) & 0xFF;
397 buf[3] = (irq >> 0) & 0xFF;
420 buf[0] = (irq >> 8) & 0xFF;
421 buf[1] = (irq >> 0) & 0xFF;
422 buf[2] = (irq >> 8) & 0xFF;
423 buf[3] = (irq >> 0) & 0xFF;
452 uint32_t start =
millis();
454 if (
millis() - start > BUSY_TIMEOUT_MS) {
455 ESP_LOGE(TAG,
"Wait busy timeout");
463 ESP_LOGCONFIG(TAG,
"SX126x:");
464 LOG_PIN(
" CS Pin: ", this->
cs_);
466 LOG_PIN(
" RST Pin: ", this->
rst_pin_);
469 " HW Version: %15s\n"
470 " Frequency: %" PRIu32
" Hz\n"
471 " Bandwidth: %" PRIu32
" Hz\n"
472 " PA Power: %" PRId8
" dBm\n"
473 " PA Ramp: %" PRIu16
" us\n"
474 " Payload Length: %" PRIu32
"\n"
480 const char *shaping =
"NONE";
482 shaping =
"GAUSSIAN_BT_0_3";
484 shaping =
"GAUSSIAN_BT_0_5";
486 shaping =
"GAUSSIAN_BT_0_7";
488 shaping =
"GAUSSIAN_BT_1_0";
492 " Deviation: %" PRIu32
" Hz\n"
494 " Preamble Size: %" PRIu16
"\n"
495 " Preamble Detect: %" PRIu16
"\n"
496 " Bitrate: %" PRIu32
"b/s",
499 const char *cr =
"4/8";
508 " Modulation: LORA\n"
509 " Spreading Factor: %" PRIu8
"\n"
511 " Preamble Size: %" PRIu16,
518 ESP_LOGE(TAG,
"Configuring SX126x failed");
BedjetMode mode
BedJet operating mode.
virtual void mark_failed()
Mark this component as failed.
virtual void digital_write(bool value)=0
virtual bool digital_read()=0
void trigger(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_
InternalGPIOPin * busy_pin_
std::vector< SX126xListener * > listeners_
size_t get_max_packet_size()
InternalGPIOPin * rst_pin_
void set_mode_standby(SX126xStandbyMode mode)
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_
Trigger< std::vector< uint8_t >, float, float > * packet_trigger_
SX126xError transmit_packet(const std::vector< uint8_t > &packet)
InternalGPIOPin * dio1_pin_
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.
std::string format_hex(const uint8_t *data, size_t length)
Format the byte array data of length len in lowercased hex.
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
void IRAM_ATTR HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()