Guest User

cc1101.h

a guest
Jun 2nd, 2026
33
0
52 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.03 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <Arduino.h>
  4. #include <SPI.h>
  5.  
  6. // Ramses II / Orcon bypass command bytes (sent in 22F7 payload)
  7. #define BYPASS_AUTO      0xFF
  8. #define BYPASS_OPEN      0xC8
  9. #define BYPASS_CLOSE     0x00
  10. // Out-of-range sentinel for "current bypass state unknown" — never equals a real value (0x00/0xC8/0xFF)
  11. #define BYPASS_UNKNOWN   0xEE
  12.  
  13. #define CC1101_GDO2 11
  14. #define chipSelectPin 4
  15.  
  16. #define PAIR_TIME_OUT   5000   // ms
  17. #define RX_TIME_OUT     300    // ms
  18. #define TX_REQUEST_COUNT     12     // Warm-up polls before requestFanState() drops unresponsive opcodes
  19.  
  20. #define WRITE_BURST              0x40
  21. #define READ_BURST               0xC0
  22. #define CC1101_STATUS_REGISTER   READ_BURST
  23. #define CC1101_UNKNOWNFIFO       0x7E        // ? FIFO address
  24.  
  25. // Command strobes
  26. #define CC1101_SRES              0x30        // Reset CC1101 chip
  27. #define CC1101_SRX               0x34        // Enable RX. Perform calibration first if coming from IDLE and MCSM0.FS_AUTOCAL=1
  28. #define CC1101_STX               0x35        // Enable TX from IDLE (or from RX if CCA enabled and channel clear)
  29. #define CC1101_SIDLE             0x36        // Exit RX / TX, turn off frequency synthesizer
  30. #define CC1101_SPWD              0x39        // Enter power down mode when CSn goes high
  31.  
  32. // Configuration registers
  33. #define CC1101_IOCFG2            0x00        // GDO2 Output Pin Configuration
  34. #define CC1101_IOCFG0            0x02        // GDO0 Output Pin Configuration
  35. #define CC1101_PKTCTRL1          0x07        // Packet Automation Control
  36. #define CC1101_PKTCTRL0          0x08        // Packet Automation Control
  37. #define CC1101_CHANNR            0x0A        // Channel Number
  38. #define CC1101_FSCTRL1           0x0B        // Frequency Synthesizer Control
  39. #define CC1101_FSCTRL0           0x0C        // Frequency Synthesizer Control
  40. #define CC1101_FREQ2             0x0D        // Frequency Control Word, High Byte
  41. #define CC1101_FREQ1             0x0E        // Frequency Control Word, Middle Byte
  42. #define CC1101_FREQ0             0x0F        // Frequency Control Word, Low Byte
  43. #define CC1101_MDMCFG4           0x10        // Modem Configuration
  44. #define CC1101_MDMCFG3           0x11        // Modem Configuration
  45. #define CC1101_MDMCFG2           0x12        // Modem Configuration
  46. #define CC1101_MDMCFG1           0x13        // Modem Configuration
  47. #define CC1101_MDMCFG0           0x14        // Modem Configuration
  48. #define CC1101_DEVIATN           0x15        // Modem Deviation Setting
  49. #define CC1101_MCSM0             0x18        // Main Radio Control State Machine Configuration
  50. #define CC1101_FOCCFG            0x19        // Frequency Offset Compensation Configuration
  51. #define CC1101_BSCFG             0x1A        // Bit Synchronization Configuration
  52. #define CC1101_AGCCTRL2          0x1B        // AGC Control
  53. #define CC1101_AGCCTRL1          0x1C        // AGC Control
  54. #define CC1101_AGCCTRL0          0x1D        // AGC Control
  55. #define CC1101_WORCTRL           0x20        // Wake On Radio Control
  56. #define CC1101_FREND1            0x21        // Front End RX Configuration
  57. #define CC1101_FREND0            0x22        // Front End TX Configuration
  58. #define CC1101_FSCAL3            0x23        // Frequency Synthesizer Calibration
  59. #define CC1101_FSCAL2            0x24        // Frequency Synthesizer Calibration
  60. #define CC1101_FSCAL1            0x25        // Frequency Synthesizer Calibration
  61. #define CC1101_FSCAL0            0x26        // Frequency Synthesizer Calibration
  62. #define CC1101_TEST0             0x2E        // Various Test Settings
  63.  
  64. // Status registers
  65. #define CC1101_MARCSTATE         0x35        // Main Radio Control State Machine State
  66.  
  67. #define CC1101_DEFVAL_IOCFG2     0x0A
  68. #define CC1101_DEFVAL_IOCFG0     0x2E        // High impedance / 3-state
  69. #define CC1101_SERIAL_OUT_IOCFG0 0x0D        // Async serial data out
  70. #define CC1101_DEFVAL_PKTCTRL1   0x04        // Packet Automation Control
  71. #define CC1101_DEFVAL_PKTCTRL0   0x32        // Packet Automation Control
  72. #define CC1101_DEFVAL_CHANNR     0x00        // Channel Number
  73. #define CC1101_DEFVAL_FSCTRL1    0x08        // Frequency Synthesizer Control
  74. #define CC1101_DEFVAL_FSCTRL0    0x00        // Frequency Synthesizer Control
  75. // Carrier frequency = 868 MHz
  76. #define CC1101_DEFVAL_FREQ2_868  0x21        // Frequency Control Word, High Byte
  77. #define CC1101_DEFVAL_FREQ1_868  0x65        // Frequency Control Word, Middle Byte [modified!]
  78. #define CC1101_DEFVAL_FREQ0_868  0x8A        // Frequency Control Word, Low Byte [modified!]
  79.  
  80. #define CC1101_DEFVAL_MDMCFG4    0x5A
  81. #define CC1101_DEFVAL_MDMCFG3    0x83        // Modem Configuration
  82. #define CC1101_DEFVAL_MDMCFG2    0x00        // Modem Configuration
  83. #define CC1101_DEFVAL_MDMCFG1    0x22        // Modem Configuration
  84. #define CC1101_DEFVAL_MDMCFG0    0xF8        // Modem Configuration
  85. #define CC1101_DEFVAL_DEVIATN    0x50        // Modem Deviation Setting
  86. #define CC1101_DEFVAL_MCSM0      0x18        // Main Radio Control State Machine Configuration
  87. #define CC1101_DEFVAL_FOCCFG     0x1D        // Frequency Offset Compensation Configuration
  88. #define CC1101_DEFVAL_BSCFG      0x1C        // Bit Synchronization Configuration
  89. #define CC1101_DEFVAL_AGCCTRL2   0xC7        // AGC Control
  90. #define CC1101_DEFVAL_AGCCTRL1   0x00        // AGC Control
  91. #define CC1101_DEFVAL_AGCCTRL0   0xB2        // AGC Control
  92. #define CC1101_DEFVAL_WORCTRL    0xFB        // Wake On Radio Control
  93. #define CC1101_DEFVAL_FREND1     0xB6        // Front End RX Configuration
  94. #define CC1101_DEFVAL_FREND0     0x17        // Front End TX Configuration
  95. #define CC1101_DEFVAL_FSCAL3     0xE9        // Frequency Synthesizer Calibration
  96. #define CC1101_DEFVAL_FSCAL2     0x2A        // Frequency Synthesizer Calibration
  97. #define CC1101_DEFVAL_FSCAL1     0x00        // Frequency Synthesizer Calibration
  98. #define CC1101_DEFVAL_FSCAL0     0x1F        // Frequency Synthesizer Calibration
  99. #define CC1101_DEFVAL_TEST0      0x09        // Various Test Settings
  100.  
  101. class CC1101
  102. {
  103.   private:
  104.     void writeBurstReg(uint8_t registerAddress, uint8_t* buffer, uint8_t length);
  105.     void manchesterDecode(uint8_t rxBuffer[], uint8_t length, uint8_t *rxPayload);
  106.     void manchesterEncode(uint8_t txBuffer[], uint8_t length, uint8_t *payloadEncoded);
  107.     bool transmitData(uint8_t payload[], uint8_t length);
  108.     uint8_t calcCrc(uint8_t dataframe[], uint8_t length);
  109.     void parseRamsesPayload(uint8_t dataframe[], uint8_t length);
  110.     bool waitForBof(uint16_t timeoutMs);
  111.     void buildRamsesFrame(uint8_t header, uint16_t opcode, const uint8_t *payload, uint8_t payloadLength, uint8_t *out);
  112.     void setRxState(void);
  113.     void setTxState(void);
  114.     void cmdStrobe(uint8_t cmd);
  115.     void writeReg(uint8_t registerAddress, uint8_t value);
  116.     void configRegisters(void);
  117.     void reset(void);
  118.     void setCarrierFreq();
  119.     void setPowerDownState();
  120.  
  121.     enum CodeSlot
  122.     {
  123.         fanSpeed,
  124.         indoorHum,
  125.         fanInfo,
  126.         NUM_CODES
  127.     };
  128.  
  129.     struct CodeEntry
  130.     {
  131.         uint16_t codeId;
  132.         bool rxFlag;
  133.     };
  134.  
  135.     CodeEntry msgId[NUM_CODES];
  136.  
  137.   public:
  138.  
  139.     struct Datapoints
  140.     {
  141.         uint8_t fanSpeed;
  142.         uint8_t indoorHumidity;
  143.         uint8_t outdoorHumidity;
  144.         uint16_t exhaustTemperature;
  145.         uint16_t supplyTemperature;
  146.         uint16_t indoorTemperature;
  147.         uint16_t outdoorTemperature;
  148.         uint16_t comfortTemperature;  // 2411 param 0x75, centi-°C (Orcon bypass target)
  149.         uint8_t bypassPosition;
  150.         uint8_t bypassMode;
  151.         uint8_t exhaustFanspeed;
  152.         uint8_t supplyFanspeed;
  153.         uint16_t exhaustFlow;
  154.         uint16_t supplyFlow;
  155.         uint8_t address[6];
  156.     };
  157.  
  158.   Datapoints newFanState;
  159.     Datapoints currentFanState;
  160.  
  161.     CC1101(void);
  162.     bool txFanspeed(uint8_t fanSpeed);
  163.     bool txDemand(uint8_t demandByte);
  164.     bool setBypass(uint8_t bypassMode);
  165.     bool requestFanState(void);
  166.     bool requestComfortTemperature(void);
  167.     bool setComfortTemperature(uint16_t centiCelsius);
  168.     uint8_t readReg(uint8_t registerAddress, uint8_t registerType);
  169.     void init(void);
  170.     void setRxMode(void);
  171.     bool cloneMode(void);
  172.  
  173. };
  174.  
Advertisement
Add Comment
Please, Sign In to add comment