12#include <esp_memory_utils.h>
18static constexpr size_t TX_FIFO_SIZE = 64;
19static constexpr size_t RX_FIFO_SIZE = 64;
21static constexpr uint8_t RX_FIFO_THRESHOLD = 32;
23static constexpr uint32_t WAKEUP_DELAY_US = 200;
25static float freq_offset_hz(uint8_t freqest) {
return static_cast<int8_t
>(freqest) * (XTAL_FREQUENCY / (1 << 14)); }
26static float rssi_dbm(int8_t rssi) {
return (rssi * RSSI_STEP) - RSSI_OFFSET; }
29PROGMEM_STRING_TABLE(ModulationStrings,
"2-FSK",
"GFSK",
"UNUSED",
"ASK/OOK",
"4-FSK",
"UNUSED",
"UNUSED",
"MSK");
31static void split_float(
float value,
int mbits, uint8_t &e,
uint32_t &
m) {
33 float m_tmp = std::frexp(
value, &e_tmp);
39 e =
static_cast<uint8_t
>(e_tmp - mbits - 1);
40 m =
static_cast<uint32_t>(((m_tmp * 2 - 1) * (1 << (mbits + 1))) + 1) >> 1;
41 if (
m == (1UL << mbits)) {
50 this->
state_.GDO2_CFG = 0x0D;
51 this->
state_.GDO1_CFG = 0x2E;
52 this->
state_.GDO0_CFG = 0x0D;
56 this->
state_.PKTLEN = 0xFF;
57 this->
state_.APPEND_STATUS = 1;
58 this->
state_.LENGTH_CONFIG = 1;
60 this->
state_.WHITE_DATA = 1;
61 this->
state_.FREQ_IF = 0x0F;
65 this->
state_.DRATE_E = 0x0C;
66 this->
state_.CHANBW_E = 0x02;
67 this->
state_.DRATE_M = 0x22;
68 this->
state_.SYNC_MODE = 2;
69 this->
state_.CHANSPC_E = 2;
70 this->
state_.NUM_PREAMBLE = 2;
71 this->
state_.CHANSPC_M = 0xF8;
72 this->
state_.DEVIATION_M = 7;
73 this->
state_.DEVIATION_E = 4;
76 this->
state_.PO_TIMEOUT = 1;
77 this->
state_.FOC_LIMIT = 2;
78 this->
state_.FOC_POST_K = 1;
79 this->
state_.FOC_PRE_K = 2;
80 this->
state_.FOC_BS_CS_GATE = 1;
81 this->
state_.BS_POST_KP = 1;
82 this->
state_.BS_POST_KI = 1;
83 this->
state_.BS_PRE_KP = 2;
84 this->
state_.BS_PRE_KI = 1;
85 this->
state_.MAGN_TARGET = 3;
86 this->
state_.AGC_LNA_PRIORITY = 1;
87 this->
state_.FILTER_LENGTH = 1;
88 this->
state_.WAIT_TIME = 1;
89 this->
state_.HYST_LEVEL = 2;
90 this->
state_.WOREVT1 = 0x87;
91 this->
state_.WOREVT0 = 0x6B;
95 this->
state_.MIX_CURRENT = 2;
96 this->
state_.LODIV_BUF_CURRENT_RX = 1;
97 this->
state_.LNA2MIX_CURRENT = 1;
98 this->
state_.LNA_CURRENT = 1;
99 this->
state_.LODIV_BUF_CURRENT_TX = 1;
100 this->
state_.FSCAL3_LO = 9;
101 this->
state_.CHP_CURR_CAL_EN = 2;
102 this->
state_.FSCAL3_HI = 2;
103 this->
state_.FSCAL2 = 0x0A;
104 this->
state_.FSCAL1 = 0x20;
105 this->
state_.FSCAL0 = 0x0D;
106 this->
state_.RCCTRL1 = 0x41;
107 this->
state_.FSTEST = 0x59;
108 this->
state_.PTEST = 0x7F;
109 this->
state_.AGCTEST = 0x3F;
110 this->
state_.TEST2 = 0x88;
111 this->
state_.TEST1 = 0x31;
112 this->
state_.TEST0_LO = 1;
113 this->
state_.VCO_SEL_CAL_EN = 1;
114 this->
state_.TEST0_HI = 2;
117 this->
state_.PKT_FORMAT = 3;
118 this->
state_.LENGTH_CONFIG = 2;
119 this->
state_.FS_AUTOCAL = 1;
140 this->
defer([
this]() {
166 ESP_LOGD(TAG,
"CC1101 found! Chip ID: 0x%04X", this->
chip_id_);
167 if (this->
state_.VERSION == 0 || this->state_.PARTNUM == 0xFF) {
168 ESP_LOGE(TAG,
"Failed to verify CC1101.");
197 listener->on_packet(packet, freq_offset, rssi, lqi);
203 return (((256.0f + this->
state_.DRATE_M) * (1 << this->state_.DRATE_E)) / (1 << 28)) * XTAL_FREQUENCY;
206#ifdef USE_CC1101_LONG_PACKET
217 this->
state_.PKTLEN = length % 256 == 0 ? 0xFF :
static_cast<uint8_t
>(
length % 256);
236 expected % 256 == 0) {
239 const uint16_t received = this->
packet_.size();
240 if (expected - received >= 256) {
246 ESP_LOGVV(TAG,
"Switched long packet RX to fixed mode: received=%u expected=%u pktlen=%u", received, expected,
254 const float freq_offset = freq_offset_hz(this->
state_.FREQEST);
255 const uint8_t lqi = this->
state_.LQI & STATUS_LQI_MASK;
259 std::vector<uint8_t> packet;
263 if (packet.capacity() > this->packet_.capacity()) {
271 uint16_t to_read = rx_bytes > 0 ? rx_bytes - 1 : 0;
274 if (remaining <= rx_bytes) {
285 std::max(
static_cast<uint32_t>(std::ceil((8.0f * PACKET_IDLE_TIMEOUT_BYTES * 1000.0f) / this->
symbol_rate_())),
286 PACKET_IDLE_TIMEOUT_MIN_MS);
288 ESP_LOGW(TAG,
"Packet receive timeout, discarding packet: received=%u expected=%u fifo=%u",
289 static_cast<uint16_t
>(this->
packet_.size()), this->packet_expected_length_, rx_bytes);
295 const size_t old_size = this->
packet_.size();
304 alignas(4) uint8_t staged[RX_FIFO_SIZE];
306 this->
packet_.resize(old_size + to_read);
307 std::memcpy(this->
packet_.data() + old_size, staged, to_read);
309 ESP_LOGVV(TAG,
"Drained RX FIFO: read=%u received=%u expected=%u fifo=%u", to_read,
310 static_cast<uint16_t
>(this->
packet_.size()), this->packet_expected_length_, rx_bytes);
315 ESP_LOGVV(TAG,
"Packet length lambda discarded packet: received=%u",
static_cast<uint16_t
>(this->
packet_.size()));
319 if (length > MAX_PACKET_LENGTH) {
320 ESP_LOGW(TAG,
"Packet length exceeds maximum, discarding packet: length=%" PRId32
" max=%u",
length,
334 ESP_LOGVV(TAG,
"Resolved packet length: received=%u expected=%u pktlen=%u",
335 static_cast<uint16_t
>(this->
packet_.size()), this->packet_expected_length_, this->state_.PKTLEN);
340 if (this->
packet_.size() > MAX_PACKET_LENGTH) {
341 ESP_LOGW(TAG,
"Packet length exceeds maximum, discarding packet: length=%u max=%u",
342 static_cast<uint16_t
>(this->
packet_.size()), MAX_PACKET_LENGTH);
356 this->gdo0_pin_ ==
nullptr) {
364 for (uint8_t i = 0; i < RX_FIFO_SIZE / RX_FIFO_THRESHOLD + 2 && this->
gdo0_pin_->
digital_read(); i++) {
367 ESP_LOGW(TAG,
"RX FIFO overflow, discarding packet: received=%u expected=%u",
368 static_cast<uint16_t
>(this->
packet_.size()), this->packet_expected_length_);
381 uint8_t rx_bytes = this->
state_.RXBYTES;
383 for (uint8_t retries = RXBYTES_STABLE_RETRIES; retries > 0 && !stable; retries--) {
384 const uint8_t previous = rx_bytes;
386 rx_bytes = this->
state_.RXBYTES;
387 stable = rx_bytes == previous;
392 rx_bytes = this->
state_.NUM_RXBYTES;
394 if (this->
state_.RXFIFO_OVERFLOW || rx_bytes > RX_FIFO_SIZE) {
395 ESP_LOGW(TAG,
"RX FIFO overflow, discarding packet: received=%u expected=%u fifo=%u",
396 static_cast<uint16_t
>(this->
packet_.size()), this->packet_expected_length_, rx_bytes);
400 if (rx_bytes == 0 && this->
packet_.empty() && !this->gdo0_pin_->digital_read()) {
409#ifdef USE_CC1101_LONG_PACKET
418 this->gdo0_pin_ ==
nullptr || !this->gdo0_pin_->digital_read()) {
424 uint8_t rx_bytes = this->
state_.NUM_RXBYTES;
425 bool overflow = this->
state_.RXFIFO_OVERFLOW;
426 if (overflow || rx_bytes == 0) {
427 ESP_LOGW(TAG,
"RX FIFO overflow, flushing");
434 if (rx_bytes == RX_FIFO_SIZE) {
443 uint8_t payload_length, expected_rx;
446 expected_rx = payload_length + 1;
448 payload_length = this->
state_.PKTLEN;
449 expected_rx = payload_length;
451 if (payload_length == 0 || payload_length > RX_FIFO_SIZE || rx_bytes != expected_rx) {
452 ESP_LOGW(TAG,
"Invalid packet: rx_bytes %u, payload_length %u", rx_bytes, payload_length);
458 this->
packet_.resize(payload_length);
465 float freq_offset = freq_offset_hz(this->
state_.FREQEST);
466 float rssi = rssi_dbm(this->
state_.RSSI);
467 bool crc_ok = (this->
state_.LQI & STATUS_CRC_OK_MASK) != 0;
468 uint8_t lqi = this->
state_.LQI & STATUS_LQI_MASK;
469 if (this->
state_.CRC_EN == 0 || crc_ok) {
484 int32_t freq =
static_cast<int32_t
>(this->
state_.FREQ2 << 16 | this->
state_.FREQ1 << 8 | this->
state_.FREQ0) *
485 XTAL_FREQUENCY / (1 << 16);
487 float bw = XTAL_FREQUENCY / (8.0f * (4 + this->
state_.CHANBW_M) * (1 << this->
state_.CHANBW_E));
491 " Frequency: %" PRId32
" Hz\n"
494 " Symbol Rate: %.0f baud\n"
495 " Filter Bandwidth: %.1f Hz\n"
496 " Output Power: %.1f dBm",
498 LOG_STR_ARG(ModulationStrings::get_log_str(this->
state_.MOD_FORMAT & 0x07, 0)), symbol_rate, bw,
499 this->output_power_effective_);
500 LOG_PIN(
" CS Pin: ", this->
cs_);
504#ifdef USE_CC1101_LONG_PACKET
506 ESP_LOGCONFIG(TAG,
" Packet Length: lambda");
510 ESP_LOGCONFIG(TAG,
" Packet Length: variable");
519 ESP_LOGW(TAG,
"begin_tx is for async serial mode; use send_packet in packet mode");
522 ESP_LOGV(TAG,
"Beginning TX sequence");
530 ESP_LOGW(TAG,
"Failed to enter TX state!");
535 ESP_LOGV(TAG,
"Beginning RX sequence");
542 ESP_LOGW(TAG,
"Failed to enter RX state!");
552 ESP_LOGV(TAG,
"Setting IDLE state");
554#ifdef USE_CC1101_LONG_PACKET
569 ESP_LOGV(TAG,
"Entering SLEEP state");
579 ESP_LOGV(TAG,
"Waking up from SLEEP");
598 while (
millis() - start < timeout_ms) {
601 if (s == target_state) {
611 for (uint8_t retries = PLL_LOCK_RETRIES; retries > 0; retries--) {
617 if (this->
state_.FSCAL1 != FSCAL1_PLL_NOT_LOCKED) {
620 ESP_LOGW(TAG,
"PLL lock failed, retrying calibration");
623 ESP_LOGE(TAG,
"PLL lock failed after retries");
635 ESP_LOGW(TAG,
"Failed to enter IDLE state");
640#ifdef USE_CC1101_LONG_PACKET
655 uint8_t index =
static_cast<uint8_t
>(cmd);
670 uint8_t index =
static_cast<uint8_t
>(reg);
675 uint8_t index =
static_cast<uint8_t
>(reg);
685 uint8_t index =
static_cast<uint8_t
>(reg);
690 uint8_t index =
static_cast<uint8_t
>(reg);
695 uint8_t index =
static_cast<uint8_t
>(reg);
701 ESP_LOGW(TAG,
"Cannot transmit packet: packet mode is not FIFO");
704#ifdef USE_CC1101_LONG_PACKET
707 ESP_LOGW(TAG,
"Cannot transmit packet: long packet mode is RX-only");
713 if (
len + (variable ? 1 : 0) > TX_FIFO_SIZE) {
715 "Cannot transmit packet: long packet TX is not implemented, payload length %u exceeds TX FIFO limit %u",
716 static_cast<unsigned>(
len),
static_cast<unsigned>(TX_FIFO_SIZE));
728 alignas(4) uint8_t staged[TX_FIFO_SIZE];
729 if (
len != 0 && (!esp_ptr_dma_capable(data) || (
reinterpret_cast<uintptr_t
>(data) & 3) != 0)) {
730 std::memcpy(staged, data,
len);
738 ESP_LOGW(TAG,
"PLL lock failed during TX");
747 ESP_LOGW(TAG,
"TX timeout");
761 int32_t freq =
static_cast<int32_t
>(this->
state_.FREQ2 << 16 | this->
state_.FREQ1 << 8 | this->
state_.FREQ0) *
762 XTAL_FREQUENCY / (1 << 16);
764 if (freq >= 300000000 && freq <= 348000000) {
766 }
else if (freq >= 378000000 && freq <= 464000000) {
768 }
else if (freq >= 779000000 && freq < 900000000) {
770 }
else if (freq >= 900000000 && freq <= 928000000) {
788 this->
state_.CLOSE_IN_RX =
static_cast<uint8_t
>(
value);
795 this->
state_.DEM_DCFILT_OFF = value ? 0 : 1;
802 int32_t freq =
static_cast<int32_t
>(
value * (1 << 16) / XTAL_FREQUENCY);
803 this->
state_.FREQ2 =
static_cast<uint8_t
>(freq >> 16);
804 this->
state_.FREQ1 =
static_cast<uint8_t
>(freq >> 8);
805 this->
state_.FREQ0 =
static_cast<uint8_t
>(freq);
820 this->
state_.FREQ_IF = value * (1 << 10) / XTAL_FREQUENCY;
829 split_float(XTAL_FREQUENCY / (
value * 8), 2, e,
m);
830 this->
state_.CHANBW_E = e;
831 this->
state_.CHANBW_M =
static_cast<uint8_t
>(
m);
852 split_float(
value * (1 << 18) / XTAL_FREQUENCY, 8, e,
m);
853 this->
state_.CHANSPC_E = e;
854 this->
state_.CHANSPC_M =
static_cast<uint8_t
>(
m);
864 split_float(
value * (1 << 17) / XTAL_FREQUENCY, 3, e,
m);
865 this->
state_.DEVIATION_E = e;
866 this->
state_.DEVIATION_M =
static_cast<uint8_t
>(
m);
873 this->
state_.DEVIATION_E = 0;
874 this->
state_.DEVIATION_M = value - 1;
883 split_float(
value * (1 << 28) / XTAL_FREQUENCY, 8, e,
m);
885 this->
state_.DRATE_M =
static_cast<uint8_t
>(
m);
893 this->
state_.SYNC_MODE =
static_cast<uint8_t
>(
value);
900 this->
state_.CARRIER_SENSE_ABOVE_THRESHOLD = value ? 1 : 0;
907 this->
state_.MOD_FORMAT =
static_cast<uint8_t
>(
value);
922 this->
state_.MANCHESTER_EN = value ? 1 : 0;
950 this->
state_.MAGN_TARGET =
static_cast<uint8_t
>(
value);
957 this->
state_.MAX_LNA_GAIN =
static_cast<uint8_t
>(
value);
964 this->
state_.MAX_DVGA_GAIN =
static_cast<uint8_t
>(
value);
971 this->
state_.CARRIER_SENSE_ABS_THR =
static_cast<uint8_t
>(
value & 0b1111);
978 this->
state_.CARRIER_SENSE_REL_THR =
static_cast<uint8_t
>(
value);
985 this->
state_.AGC_LNA_PRIORITY = value ? 1 : 0;
992 this->
state_.FILTER_LENGTH =
static_cast<uint8_t
>(
value);
999 this->
state_.FILTER_LENGTH =
static_cast<uint8_t
>(
value);
1006 this->
state_.AGC_FREEZE =
static_cast<uint8_t
>(
value);
1013 this->
state_.WAIT_TIME =
static_cast<uint8_t
>(
value);
1020 this->
state_.HYST_LEVEL =
static_cast<uint8_t
>(
value);
1027 this->
state_.FOC_BS_CS_GATE = value ? 1 : 0;
1034 this->
state_.FOC_LIMIT =
static_cast<uint8_t
>(
value);
1041 this->
state_.FOC_PRE_K =
static_cast<uint8_t
>(
value);
1048 this->
state_.FOC_POST_K =
static_cast<uint8_t
>(
value);
1055 this->
state_.BS_LIMIT =
static_cast<uint8_t
>(
value);
1062 this->
state_.BS_PRE_KI =
static_cast<uint8_t
>(
value);
1069 this->
state_.BS_PRE_KP =
static_cast<uint8_t
>(
value);
1076 this->
state_.BS_POST_KI =
static_cast<uint8_t
>(
value);
1083 this->
state_.BS_POST_KP =
static_cast<uint8_t
>(
value);
1090#ifdef USE_CC1101_LONG_PACKET
1097 this->
state_.GDO0_CFG = 0x0D;
1100 this->
state_.APPEND_STATUS = 0;
1101#ifdef USE_CC1101_LONG_PACKET
1104 this->
state_.GDO0_CFG = 0x00;
1105 this->
state_.FIFO_THR = 7;
1113 this->
state_.GDO0_CFG = 0x01;
1114 this->
state_.FIFO_THR = 15;
1135#ifdef USE_CC1101_LONG_PACKET
1156#ifndef USE_CC1101_LONG_PACKET
1157 if (
value > RX_FIFO_SIZE) {
1158 ESP_LOGW(TAG,
"Packet length %u needs a configured packet_length above %u",
value,
1159 static_cast<unsigned>(RX_FIFO_SIZE));
1169#ifdef USE_CC1101_LONG_PACKET
1190 this->
state_.CRC_EN = value ? 1 : 0;
1197 this->
state_.WHITE_DATA = value ? 1 : 0;
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void mark_failed()
Mark this component as failed.
void defer(const char *name, SchedulerCallback &&f)
Defer a callback to the next loop() call with a const char* name.
void enable_loop()
Enable this component's loop.
void disable_loop()
Disable this component's loop.
virtual void digital_write(bool value)=0
void stop()
Stop running the loop continuously.
void start()
Start running the loop continuously.
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Inform the parent automation that the event has triggered.
void set_bs_post_kp(BsPostKp value)
void set_packet_mode(bool value)
float symbol_rate_() const
void set_max_dvga_gain(MaxDvgaGain value)
void set_whitening(bool value)
void set_carrier_sense_above_threshold(bool value)
void write_(Register reg)
void set_sync0(uint8_t value)
void set_freeze(Freeze value)
packet_length_func_t packet_length_func_
void call_listeners_(const std::vector< uint8_t > &packet, float freq_offset, float rssi, uint8_t lqi)
void set_rx_attenuation(RxAttenuation value)
InternalGPIOPin * gdo0_pin_
void set_symbol_rate(float value)
void set_packet_length_lambda(packet_length_func_t func)
void set_bs_pre_ki(BsPreKi value)
uint8_t strobe_(Command cmd)
void set_sync_mode(SyncMode value)
void set_fsk_deviation(float value)
void dump_config() override
void set_bs_pre_kp(BsPreKp value)
float output_power_requested_
void handle_long_packet_(uint8_t rx_bytes)
void set_msk_deviation(uint8_t value)
void set_carrier_sense_rel_thr(CarrierSenseRelThr value)
uint8_t pa_table_[PA_TABLE_SIZE]
void set_lna_priority(bool value)
void set_foc_bs_cs_gate(bool value)
void reconfigure_packet_length_()
void set_foc_pre_k(FocPreK value)
float output_power_effective_
void set_output_power(float value)
void arm_length_(uint16_t length)
void set_modulation_type(Modulation value)
void set_if_frequency(float value)
void set_packet_length(uint16_t value)
void set_frequency(float value)
void set_num_preamble(uint8_t value)
void set_hyst_level(HystLevel value)
void set_filter_bandwidth(float value)
void set_filter_length_fsk_msk(FilterLengthFskMsk value)
void set_crc_enable(bool value)
void set_carrier_sense_abs_thr(int8_t value)
void finish_long_packet_()
void set_bs_post_ki(BsPostKi value)
bool enter_calibrated_(State target_state, Command cmd)
void set_magn_target(MagnTarget value)
void set_channel_spacing(float value)
CC1101Error transmit_packet(const std::vector< uint8_t > &packet)
void set_wait_time(WaitTime value)
void maybe_switch_long_packet_to_fixed_()
std::vector< CC1101Listener * > listeners_
void set_dc_blocking_filter(bool value)
void set_manchester(bool value)
void reset_long_packet_rx_()
uint32_t packet_last_byte_ms_
std::vector< uint8_t > packet_
void set_channel(uint8_t value)
void restart_long_packet_rx_()
void set_foc_limit(FocLimit value)
void set_filter_length_ask_ook(FilterLengthAskOok value)
void set_max_lna_gain(MaxLnaGain value)
void set_sync1(uint8_t value)
bool is_long_packet_mode_() const
bool wait_for_state_(State target_state, uint32_t timeout_ms=100)
HighFrequencyLoopRequester high_freq_
void set_bs_limit(BsLimit value)
void set_foc_post_k(FocPostK value)
Trigger< std::vector< uint8_t >, float, float, uint8_t > packet_trigger_
uint16_t packet_expected_length_
void configure_packet_mode_()
void detach_interrupt() const
bool digital_read() override
void pin_mode(gpio::Flags flags) 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)
uint8_t transfer_byte(uint8_t data)
int32_t(*)(const std::vector< uint8_t > &) packet_length_func_t
PROGMEM_STRING_TABLE(ModulationStrings, "2-FSK", "GFSK", "UNUSED", "ASK/OOK", "4-FSK", "UNUSED", "UNUSED", "MSK")
@ PACKET_FORMAT_ASYNC_SERIAL
ESPHOME_LOG_TAG(TAG, "cc1101")
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
void attach_wake_interrupt(InternalGPIOPin *pin, Component *component, gpio::InterruptType type)
Wake component's loop() from pin's interrupt, through one ISR shared by every caller.
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
void HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.
static uint8_t find(const PowerTableItem *items, size_t count, float &dbm_target)