ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
cc1101defs.h
Go to the documentation of this file.
1#pragma once
2
3#include <cinttypes>
4
5namespace esphome::cc1101 {
6
7static constexpr float XTAL_FREQUENCY = 26000000;
8
9static constexpr float RSSI_OFFSET = 74.0f;
10static constexpr float RSSI_STEP = 0.5f;
11
12static constexpr uint8_t FSCAL1_PLL_NOT_LOCKED = 0x3F;
13static constexpr uint8_t PLL_LOCK_RETRIES = 3;
14
15static constexpr uint8_t STATUS_CRC_OK_MASK = 0x80;
16static constexpr uint8_t STATUS_LQI_MASK = 0x7F;
17
18static constexpr uint16_t MAX_PACKET_LENGTH = 1024;
19static constexpr uint16_t PACKET_IDLE_TIMEOUT_BYTES = 64;
20static constexpr uint32_t PACKET_IDLE_TIMEOUT_MIN_MS = 5;
21static constexpr uint8_t RXBYTES_STABLE_RETRIES = 4;
22
23static constexpr uint8_t BUS_BURST = 0x40;
24static constexpr uint8_t BUS_READ = 0x80;
25static constexpr uint8_t BUS_WRITE = 0x00;
26static constexpr uint8_t BYTES_IN_RXFIFO = 0x7F; // byte number in RXfifo
27static constexpr size_t PA_TABLE_SIZE = 8;
28
29enum class Register : uint8_t {
30 IOCFG2 = 0x00, // GDO2 output pin configuration
31 IOCFG1 = 0x01, // GDO1 output pin configuration
32 IOCFG0 = 0x02, // GDO0 output pin configuration
33 FIFOTHR = 0x03, // RX FIFO and TX FIFO thresholds
34 SYNC1 = 0x04, // Sync word, high INT8U
35 SYNC0 = 0x05, // Sync word, low INT8U
36 PKTLEN = 0x06, // Packet length
37 PKTCTRL1 = 0x07, // Packet automation control
38 PKTCTRL0 = 0x08, // Packet automation control
39 ADDR = 0x09, // Device address
40 CHANNR = 0x0A, // Channel number
41 FSCTRL1 = 0x0B, // Frequency synthesizer control
42 FSCTRL0 = 0x0C, // Frequency synthesizer control
43 FREQ2 = 0x0D, // Frequency control word, high INT8U
44 FREQ1 = 0x0E, // Frequency control word, middle INT8U
45 FREQ0 = 0x0F, // Frequency control word, low INT8U
46 MDMCFG4 = 0x10, // Modem configuration
47 MDMCFG3 = 0x11, // Modem configuration
48 MDMCFG2 = 0x12, // Modem configuration
49 MDMCFG1 = 0x13, // Modem configuration
50 MDMCFG0 = 0x14, // Modem configuration
51 DEVIATN = 0x15, // Modem deviation setting
52 MCSM2 = 0x16, // Main Radio Control State Machine configuration
53 MCSM1 = 0x17, // Main Radio Control State Machine configuration
54 MCSM0 = 0x18, // Main Radio Control State Machine configuration
55 FOCCFG = 0x19, // Frequency Offset Compensation configuration
56 BSCFG = 0x1A, // Bit Synchronization configuration
57 AGCCTRL2 = 0x1B, // AGC control
58 AGCCTRL1 = 0x1C, // AGC control
59 AGCCTRL0 = 0x1D, // AGC control
60 WOREVT1 = 0x1E, // High INT8U Event 0 timeout
61 WOREVT0 = 0x1F, // Low INT8U Event 0 timeout
62 WORCTRL = 0x20, // Wake On Radio control
63 FREND1 = 0x21, // Front end RX configuration
64 FREND0 = 0x22, // Front end TX configuration
65 FSCAL3 = 0x23, // Frequency synthesizer calibration
66 FSCAL2 = 0x24, // Frequency synthesizer calibration
67 FSCAL1 = 0x25, // Frequency synthesizer calibration
68 FSCAL0 = 0x26, // Frequency synthesizer calibration
69 RCCTRL1 = 0x27, // RC oscillator configuration
70 RCCTRL0 = 0x28, // RC oscillator configuration
71 FSTEST = 0x29, // Frequency synthesizer calibration control
72 PTEST = 0x2A, // Production test
73 AGCTEST = 0x2B, // AGC test
74 TEST2 = 0x2C, // Various test settings
75 TEST1 = 0x2D, // Various test settings
76 TEST0 = 0x2E, // Various test settings
77 UNUSED = 0x2F,
78 PARTNUM = 0x30,
79 VERSION = 0x31,
80 FREQEST = 0x32,
81 LQI = 0x33,
82 RSSI = 0x34,
83 MARCSTATE = 0x35,
84 WORTIME1 = 0x36,
85 WORTIME0 = 0x37,
86 PKTSTATUS = 0x38,
87 VCO_VC_DAC = 0x39,
88 TXBYTES = 0x3A,
89 RXBYTES = 0x3B,
90 RCCTRL1_STATUS = 0x3C,
91 RCCTRL0_STATUS = 0x3D,
92 PATABLE = 0x3E,
93 FIFO = 0x3F,
94};
95
96enum class Command : uint8_t {
97 RES = 0x30, // Reset chip.
98 FSTXON = 0x31, // Enable and calibrate frequency synthesizer
99 XOFF = 0x32, // Turn off crystal oscillator.
100 CAL = 0x33, // Calibrate frequency synthesizer and turn it off
101 RX = 0x34, // Enable RX.
102 TX = 0x35, // Enable TX.
103 IDLE = 0x36, // Exit RX / TX
104 // 0x37 is RESERVED / UNDEFINED in CC1101 Datasheet
105 WOR = 0x38, // Start automatic RX polling sequence (Wake-on-Radio)
106 PWD = 0x39, // Enter power down mode when CSn goes high.
107 FRX = 0x3A, // Flush the RX FIFO buffer.
108 FTX = 0x3B, // Flush the TX FIFO buffer.
109 WORRST = 0x3C, // Reset real time clock.
110 NOP = 0x3D, // No operation.
111};
112
113enum class State : uint8_t {
114 SLEEP,
115 IDLE,
116 XOFF,
117 VCOON_MC,
118 REGON_MC,
119 MANCAL,
120 VCOON,
121 REGON,
122 STARTCAL,
123 BWBOOST,
124 FS_LOCK,
125 IFADCON,
126 ENDCAL,
127 RX,
128 RX_END,
129 RX_RST,
132 FSTXON,
133 TX,
134 TX_END,
137};
138
145
156
167
178
189
196
203
210
217
218enum class Freeze : uint8_t {
223};
224
231
232enum class HystLevel : uint8_t {
237};
238
239enum class FocLimit : uint8_t {
244};
245
246enum class FocPreK : uint8_t {
251};
252
253enum class FocPostK : uint8_t {
256};
257
264
265enum class BsPreKi : uint8_t {
270};
271
272enum class BsPreKp : uint8_t {
277};
278
279enum class BsPostKi : uint8_t {
282};
283
284enum class BsPostKp : uint8_t {
287};
288
295
301
302struct __attribute__((packed)) CC1101State {
303 // Byte array accessors for bulk SPI transfers
304 uint8_t *regs() { return reinterpret_cast<uint8_t *>(this); }
305 const uint8_t *regs() const { return reinterpret_cast<const uint8_t *>(this); }
306
307 // 0x00
308 union {
309 uint8_t IOCFG2;
310 struct {
311 uint8_t GDO2_CFG : 6;
312 uint8_t GDO2_INV : 1;
313 uint8_t : 1;
314 };
315 };
316 // 0x01
317 union {
318 uint8_t IOCFG1;
319 struct {
320 uint8_t GDO1_CFG : 6;
321 uint8_t GDO1_INV : 1;
322 uint8_t GDO_DS : 1; // GDO, not GD0
323 };
324 };
325 // 0x02
326 union {
327 uint8_t IOCFG0;
328 struct {
329 uint8_t GDO0_CFG : 6;
330 uint8_t GDO0_INV : 1;
331 uint8_t TEMP_SENSOR_ENABLE : 1;
332 };
333 };
334 // 0x03
335 union {
336 uint8_t FIFOTHR;
337 struct {
338 uint8_t FIFO_THR : 4;
339 uint8_t CLOSE_IN_RX : 2; // RxAttenuation
340 uint8_t ADC_RETENTION : 1;
341 uint8_t : 1;
342 };
343 };
344 // 0x04
345 uint8_t SYNC1;
346 // 0x05
347 uint8_t SYNC0;
348 // 0x06
349 uint8_t PKTLEN;
350 // 0x07
351 union {
352 uint8_t PKTCTRL1;
353 struct {
354 uint8_t ADR_CHK : 2;
355 uint8_t APPEND_STATUS : 1;
356 uint8_t CRC_AUTOFLUSH : 1;
357 uint8_t : 1;
358 uint8_t PQT : 3;
359 };
360 };
361 // 0x08
362 union {
363 uint8_t PKTCTRL0;
364 struct {
365 uint8_t LENGTH_CONFIG : 2;
366 uint8_t CRC_EN : 1;
367 uint8_t : 1;
368 uint8_t PKT_FORMAT : 2;
369 uint8_t WHITE_DATA : 1;
370 uint8_t : 1;
371 };
372 };
373 // 0x09
374 uint8_t ADDR;
375 // 0x0A
376 uint8_t CHANNR;
377 // 0x0B
378 union {
379 uint8_t FSCTRL1;
380 struct {
381 uint8_t FREQ_IF : 5;
382 uint8_t RESERVED : 1; // hm?
383 uint8_t : 2;
384 };
385 };
386 // 0x0C
387 uint8_t FSCTRL0;
388 // 0x0D
389 uint8_t FREQ2; // [7:6] always zero
390 // 0x0E
391 uint8_t FREQ1;
392 // 0x0F
393 uint8_t FREQ0;
394 // 0x10
395 union {
396 uint8_t MDMCFG4;
397 struct {
398 uint8_t DRATE_E : 4;
399 uint8_t CHANBW_M : 2;
400 uint8_t CHANBW_E : 2;
401 };
402 };
403 // 0x11
404 union {
405 uint8_t MDMCFG3;
406 struct {
407 uint8_t DRATE_M : 8;
408 };
409 };
410 // 0x12
411 union {
412 uint8_t MDMCFG2;
413 struct {
414 uint8_t SYNC_MODE : 2;
415 uint8_t CARRIER_SENSE_ABOVE_THRESHOLD : 1;
416 uint8_t MANCHESTER_EN : 1;
417 uint8_t MOD_FORMAT : 3; // Modulation
418 uint8_t DEM_DCFILT_OFF : 1;
419 };
420 };
421 // 0x13
422 union {
423 uint8_t MDMCFG1;
424 struct {
425 uint8_t CHANSPC_E : 2;
426 uint8_t : 2;
427 uint8_t NUM_PREAMBLE : 3;
428 uint8_t FEC_EN : 1;
429 };
430 };
431 // 0x14
432 union {
433 uint8_t MDMCFG0;
434 struct {
435 uint8_t CHANSPC_M : 8;
436 };
437 };
438 // 0x15
439 union {
440 uint8_t DEVIATN;
441 struct {
442 uint8_t DEVIATION_M : 3;
443 uint8_t : 1;
444 uint8_t DEVIATION_E : 3;
445 uint8_t : 1;
446 };
447 };
448 // 0x16
449 union {
450 uint8_t MCSM2;
451 struct {
452 uint8_t RX_TIME : 3;
453 uint8_t RX_TIME_QUAL : 1;
454 uint8_t RX_TIME_RSSI : 1;
455 uint8_t : 3;
456 };
457 };
458 // 0x17
459 union {
460 uint8_t MCSM1;
461 struct {
462 uint8_t TXOFF_MODE : 2;
463 uint8_t RXOFF_MODE : 2;
464 uint8_t CCA_MODE : 2;
465 uint8_t : 2;
466 };
467 };
468 // 0x18
469 union {
470 uint8_t MCSM0;
471 struct {
472 uint8_t XOSC_FORCE_ON : 1;
473 uint8_t PIN_CTRL_EN : 1;
474 uint8_t PO_TIMEOUT : 2;
475 uint8_t FS_AUTOCAL : 2;
476 uint8_t : 2;
477 };
478 };
479 // 0x19
480 union {
481 uint8_t FOCCFG;
482 struct {
483 uint8_t FOC_LIMIT : 2;
484 uint8_t FOC_POST_K : 1;
485 uint8_t FOC_PRE_K : 2;
486 uint8_t FOC_BS_CS_GATE : 1;
487 uint8_t : 2;
488 };
489 };
490 // 0x1A
491 union {
492 uint8_t BSCFG;
493 struct {
494 uint8_t BS_LIMIT : 2;
495 uint8_t BS_POST_KP : 1;
496 uint8_t BS_POST_KI : 1;
497 uint8_t BS_PRE_KP : 2;
498 uint8_t BS_PRE_KI : 2;
499 };
500 };
501 // 0x1B
502 union {
503 uint8_t AGCCTRL2;
504 struct {
505 uint8_t MAGN_TARGET : 3; // MagnTarget
506 uint8_t MAX_LNA_GAIN : 3; // MaxLnaGain
507 uint8_t MAX_DVGA_GAIN : 2; // MaxDvgaGain
508 };
509 };
510 // 0x1C
511 union {
512 uint8_t AGCCTRL1;
513 struct {
514 uint8_t CARRIER_SENSE_ABS_THR : 4;
515 uint8_t CARRIER_SENSE_REL_THR : 2; // CarrierSenseRelThr
516 uint8_t AGC_LNA_PRIORITY : 1;
517 uint8_t : 1;
518 };
519 };
520 // 0x1D
521 union {
522 uint8_t AGCCTRL0;
523 struct {
524 uint8_t FILTER_LENGTH : 2; // FilterLengthFskMsk or FilterLengthAskOok
525 uint8_t AGC_FREEZE : 2; // Freeze
526 uint8_t WAIT_TIME : 2; // WaitTime
527 uint8_t HYST_LEVEL : 2; // HystLevel
528 };
529 };
530 // 0x1E
531 uint8_t WOREVT1;
532 // 0x1F
533 uint8_t WOREVT0;
534 // 0x20
535 union {
536 uint8_t WORCTRL;
537 struct {
538 uint8_t WOR_RES : 2;
539 uint8_t : 1;
540 uint8_t RC_CAL : 1;
541 uint8_t EVENT1 : 3;
542 uint8_t RC_PD : 1;
543 };
544 };
545 // 0x21
546 union {
547 uint8_t FREND1;
548 struct {
549 uint8_t MIX_CURRENT : 2;
550 uint8_t LODIV_BUF_CURRENT_RX : 2;
551 uint8_t LNA2MIX_CURRENT : 2;
552 uint8_t LNA_CURRENT : 2;
553 };
554 };
555 // 0x22
556 union {
557 uint8_t FREND0;
558 struct {
559 uint8_t PA_POWER : 3;
560 uint8_t : 1;
561 uint8_t LODIV_BUF_CURRENT_TX : 2;
562 uint8_t : 2;
563 };
564 };
565 // 0x23
566 union {
567 uint8_t FSCAL3;
568 struct {
569 uint8_t FSCAL3_LO : 4;
570 uint8_t CHP_CURR_CAL_EN : 2; // Disable charge pump calibration stage when 0.
571 uint8_t FSCAL3_HI : 2;
572 };
573 };
574 // 0x24
575 union {
576 // uint8_t FSCAL2;
577 struct {
578 uint8_t FSCAL2 : 5;
579 uint8_t VCO_CORE_H_EN : 1;
580 uint8_t : 2;
581 };
582 };
583 // 0x25
584 union {
585 // uint8_t FSCAL1;
586 struct {
587 uint8_t FSCAL1 : 6;
588 uint8_t : 2;
589 };
590 };
591 // 0x26
592 union {
593 // uint8_t FSCAL0;
594 struct {
595 uint8_t FSCAL0 : 7;
596 uint8_t : 1;
597 };
598 };
599 // 0x27
600 union {
601 // uint8_t RCCTRL1;
602 struct {
603 uint8_t RCCTRL1 : 7;
604 uint8_t : 1;
605 };
606 };
607 // 0x28
608 union {
609 // uint8_t RCCTRL0;
610 struct {
611 uint8_t RCCTRL0 : 7;
612 uint8_t : 1;
613 };
614 };
615 // 0x29
616 uint8_t FSTEST;
617 // 0x2A
618 uint8_t PTEST;
619 // 0x2B
620 uint8_t AGCTEST;
621 // 0x2C
622 uint8_t TEST2;
623 // 0x2D
624 uint8_t TEST1;
625 // 0x2E
626 union {
627 uint8_t TEST0;
628 struct {
629 uint8_t TEST0_LO : 1;
630 uint8_t VCO_SEL_CAL_EN : 1; // Enable VCO selection calibration stage when 1
631 uint8_t TEST0_HI : 6;
632 };
633 };
634 // 0x2F
635 uint8_t REG_2F;
636 // 0x30
637 uint8_t PARTNUM;
638 // 0x31
639 uint8_t VERSION;
640 // 0x32
641 union {
642 uint8_t FREQEST;
643 struct {
644 int8_t FREQOFF_EST : 8;
645 };
646 };
647 // 0x33
648 union {
649 uint8_t LQI;
650 struct {
651 uint8_t LQI_EST : 7;
652 uint8_t LQI_CRC_OK : 1;
653 };
654 };
655 // 0x34
656 int8_t RSSI;
657 // 0x35
658 union {
659 // uint8_t MARCSTATE;
660 struct {
661 uint8_t MARC_STATE : 5; // State
662 uint8_t : 3;
663 };
664 };
665 // 0x36
666 uint8_t WORTIME1;
667 // 0x37
668 uint8_t WORTIME0;
669 // 0x38
670 union {
671 uint8_t PKTSTATUS;
672 struct {
673 uint8_t GDO0 : 1;
674 uint8_t : 1;
675 uint8_t GDO2 : 1;
676 uint8_t SFD : 1;
677 uint8_t CCA : 1;
678 uint8_t PQT_REACHED : 1;
679 uint8_t CS : 1;
680 uint8_t CRC_OK : 1; // same as LQI_CRC_OK?
681 };
682 };
683 // 0x39
684 uint8_t VCO_VC_DAC;
685 // 0x3A
686 union {
687 uint8_t TXBYTES;
688 struct {
689 uint8_t NUM_TXBYTES : 7;
690 uint8_t TXFIFO_UNDERFLOW : 1;
691 };
692 };
693 // 0x3B
694 union {
695 uint8_t RXBYTES;
696 struct {
697 uint8_t NUM_RXBYTES : 7;
698 uint8_t RXFIFO_OVERFLOW : 1;
699 };
700 };
701 // 0x3C
702 union {
703 // uint8_t RCCTRL1_STATUS;
704 struct {
705 uint8_t RCCTRL1_STATUS : 7;
706 uint8_t : 1;
707 };
708 };
709 // 0x3D
710 union {
711 // uint8_t RCCTRL0_STATUS;
712 struct {
713 uint8_t RCCTRL0_STATUS : 7;
714 uint8_t : 1;
715 };
716 };
717 // 0x3E
718 uint8_t REG_3E;
719 // 0x3F
720 uint8_t REG_3F;
721};
722
723static_assert(sizeof(CC1101State) == 0x40, "CC1101State size mismatch");
724
725} // namespace esphome::cc1101
static void uint32_t