ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
sx126x_spi_device.cpp
Go to the documentation of this file.
1#include "sx126x_spi_device.h"
2#include "esphome/core/hal.h"
3
4namespace esphome::sx126x {
5
6static constexpr uint32_t BUSY_TIMEOUT_MS = 20;
7static constexpr uint32_t SWITCHING_DELAY_US = 1;
8
9void SX126xSpiDevice::exchange_(std::span<const uint8_t> header, std::span<const uint8_t> tx, std::span<uint8_t> rx) {
10 if (!this->ok_) {
11 return;
12 }
13 // Selecting first wakes a sleeping radio. The guard releases CS even on timeout.
14 Selection selected(this->device_);
15 const uint32_t start = millis();
16 while (this->busy_pin_->digital_read()) {
17 if (millis() - start > BUSY_TIMEOUT_MS) {
18 this->ok_ = false;
19 return;
20 }
21 }
22 for (auto byte : header) {
23 this->device_.transfer_byte(byte);
24 }
25 for (auto byte : tx) {
26 this->device_.transfer_byte(byte);
27 }
28 for (auto &byte : rx) {
29 byte = this->device_.transfer_byte(0);
30 }
31}
32
33void SX126xSpiDevice::write(std::span<const uint8_t> header, std::span<const uint8_t> data) {
34 this->exchange_(header, data, {});
35 if (this->ok_) {
36 // This delay belongs after CS is released, not in the selected interval.
37 delayMicroseconds(SWITCHING_DELAY_US);
38 }
39}
40
41void SX126xSpiDevice::read(std::span<const uint8_t> header, std::span<uint8_t> data) {
42 this->exchange_(header, {}, data);
43}
44
45} // namespace esphome::sx126x
virtual bool digital_read()=0
uint8_t transfer_byte(uint8_t data)
Definition spi.h:503
void write(std::span< const uint8_t > header, std::span< const uint8_t > data)
void read(std::span< const uint8_t > header, std::span< uint8_t > data)
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition hal.cpp:48
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
static void uint32_t