48 #include "CC1101Registers.h"
52 #if !defined(CC1101_SPI)
53 #define CC1101_SPI hwSPI
57 #if defined(ARDUINO_ARCH_AVR)
58 #if defined(__AVR_ATmega32U4__)
59 #define DEFAULT_CC1101_GD0_PIN (3)
61 #define DEFAULT_CC1101_GD0_PIN (2)
63 #elif defined(ARDUINO_ARCH_ESP8266)
64 #define DEFAULT_CC1101_GD0_PIN (5)
65 #elif defined(ARDUINO_ARCH_ESP32)
66 #define DEFAULT_CC1101_GD0_PIN (16)
67 #define DEFAULT_CC1101_GD0_NUM digitalPinToInterrupt(DEFAULT_CC1101_GD0_PIN)
69 #elif defined(ARDUINO_ARCH_SAMD)
70 #define DEFAULT_CC1101_GD0_PIN (2)
71 #elif defined(LINUX_ARCH_RASPBERRYPI)
72 #define DEFAULT_CC1101_GD0_PIN (22)
73 #elif defined(ARDUINO_ARCH_STM32)
74 #define DEFAULT_CC1101_GD0_PIN (PA3)
75 #elif defined(TEENSYDUINO)
76 #define DEFAULT_CC1101_GD0_PIN (8)
78 #define DEFAULT_CC1101_GD0_PIN (2)
81 #ifndef DEFAULT_CC1101_CS_PIN
82 #define DEFAULT_CC1101_CS_PIN (SS)
85 #define CC1101_CRYSTAL_HZ 26000000LL
87 #define CC1101_315MHZ 315000000LL
88 #define CC1101_433MHZ 433100000LL
89 #define CC1101_868MHZ 868100000LL
90 #define CC1101_915MHZ 915100000LL
91 #define CC1101_FREQ_VAL (((MY_CC1101_FREQUENCY)*65536LL) / CC1101_CRYSTAL_HZ)
92 #define CC1101_FREQ_BYTE0 ((CC1101_FREQ_VAL >> 16) & 0xFF)
93 #define CC1101_FREQ_BYTE1 ((CC1101_FREQ_VAL >> 8) & 0xFF)
94 #define CC1101_FREQ_BYTE2 (CC1101_FREQ_VAL & 0xFF)
96 #if (MY_CC1101_FREQUENCY == CC1101_315MHZ)
97 #define CC1101_PA_TABLE PA_TABLE_315
98 #elif (MY_CC1101_FREQUENCY == CC1101_433MHZ)
99 #define CC1101_PA_TABLE PA_TABLE_433
100 #elif (MY_CC1101_FREQUENCY == CC1101_868MHZ)
101 #define CC1101_PA_TABLE PA_TABLE_868
102 #elif (MY_CC1101_FREQUENCY == CC1101_915MHZ)
103 #define CC1101_PA_TABLE PA_TABLE_868
105 #error Unsupported value for MY_CC1101_FREQUENCY
119 } cc1101_powerLevel_t;
121 #ifndef MY_CC1101_POWER_LEVEL
122 #ifdef MY_GATEWAY_FEATURE
123 #define MY_CC1101_POWER_LEVEL CC1101_POWER_6
125 #define MY_CC1101_POWER_LEVEL CC1101_POWER_AUTO
129 #ifndef MY_CC1101_MIN_POWER_LEVEL
130 #define MY_CC1101_MIN_POWER_LEVEL (0)
133 #ifndef MY_CC1101_MAX_POWER_LEVEL
134 #define MY_CC1101_MAX_POWER_LEVEL (7)
137 #define CC1101_ATC_TARGET_RANGE_DBM (15)
138 #define CC1101_RSSI_OFFSET (74u)
139 #define CC1101_TARGET_RSSI (-50)
141 #define CC1101_MODEM_S_BR4_8_FD20 0
142 #define CC1101_MODEM_M_BR38_4_FD20 1
143 #define CC1101_MODEM_F_BR100_FD50 2
144 #define CC1101_MODEM_XF_BR250_FD125 3
146 #ifndef MY_CC1101_MODEM_CONFIGURATION
147 #define MY_CC1101_MODEM_CONFIGURATION CC1101_MODEM_M_BR38_4_FD20
150 #if (MY_CC1101_MODEM_CONFIGURATION) == (CC1101_MODEM_S_BR4_8_FD20)
151 #define MY_CC1101_REG_MDMCFG4_VALUE 0x67 // 135KHz bw, 4800b
152 #define MY_CC1101_REG_MDMCFG3_VALUE 0x83 // 4800b
153 #define MY_CC1101_REG_MDMCFG2_VALUE 0x12
154 #define MY_CC1101_REG_DEVIATN_VALUE 0x34 // 19khz dev.
155 #elif (MY_CC1101_MODEM_CONFIGURATION) == (CC1101_MODEM_M_BR38_4_FD20)
156 #define MY_CC1101_REG_MDMCFG4_VALUE 0x8a // 200KHz bw, 38.4kb
157 #define MY_CC1101_REG_MDMCFG3_VALUE 0x82 // 38.4kb
158 #define MY_CC1101_REG_MDMCFG2_VALUE 0x12
159 #define MY_CC1101_REG_DEVIATN_VALUE 0x47 // 47khz dev.
160 #elif (MY_CC1101_MODEM_CONFIGURATION) == (CC1101_MODEM_F_BR100_FD50)
161 #define MY_CC1101_REG_MDMCFG4_VALUE 0x8B // 200KHz bw, 100kb
162 #define MY_CC1101_REG_MDMCFG3_VALUE 0xF8 // 100kb
163 #define MY_CC1101_REG_MDMCFG2_VALUE 0x12
164 #define MY_CC1101_REG_DEVIATN_VALUE 0x47 // 47khz dev.
165 #elif (MY_CC1101_MODEM_CONFIGURATION) == (CC1101_MODEM_XF_BR250_FD125)
166 #define MY_CC1101_REG_MDMCFG4_VALUE 0x5D // 320KHz bw, 250kb
167 #define MY_CC1101_REG_MDMCFG3_VALUE 0x3B // 250kb
168 #define MY_CC1101_REG_MDMCFG2_VALUE 0x12
169 #define MY_CC1101_REG_DEVIATN_VALUE 0x62 // 125khz dev.
171 #error Invalid setting for MY_CC1101_MODEM_CONFIGURATION.
174 #define CC1101_MAX_PACKET_LEN (0x40u)
175 #define CC1101_PREAMBLE_LENGTH (8u)
176 #define CC1101_BROADCAST_ADDRESS 0xFF
178 #ifndef MY_CC1101_SYNC_WORD
179 #define MY_CC1101_SYNC_WORD 0xF543
182 #define CC1101_TX_RETRIES (5u)
184 #if !defined(MY_CC1101_TX_TIMEOUT_MS)
185 #define MY_CC1101_TX_TIMEOUT_MS \
189 #if !defined(MY_CC1101_TX_RETRY_DELAY_MS)
190 #define MY_CC1101_TX_RETRY_DELAY_MS \
194 #define CC1101_SEND_RETRIES (4u)
196 #if !defined(MY_CC1101_SEND_TIMEOUT_MS)
197 #define MY_CC1101_SEND_TIMEOUT_MS (500ul)
200 #if !defined(MY_CC1101_ACK_SEND_DELAY_MS)
201 #define MY_CC1101_ACK_SEND_DELAY_MS \
206 #if !defined(MY_CC1101_CSN_TIMEOUT_MS)
207 #define MY_CC1101_CSN_TIMEOUT_MS (2ul)
210 #if (MY_CC1101_MAX_POWER_LEVEL) <= (MY_CC1101_MIN_POWER_LEVEL)
211 #error MY_CC1101_MAX_POWER_LEVEL has to be bigger than MY_CC1101_MIN_POWER_LEVEL
214 #if !defined(CC1101_RETRY_TIMEOUT_MS)
217 #define CC1101_RETRY_TIMEOUT_MS \
222 #define CC1101_WRITE_SINGLE 0x00
223 #define CC1101_WRITE_BURST 0x40
224 #define CC1101_READ_SINGLE 0x80
225 #define CC1101_READ_BURST 0xC0
281 #define CC1101_HEADER_LEN \
282 sizeof(cc1101_header_t)
283 #define CC1101_MAX_PAYLOAD_LEN \
284 (CC1101_MAX_PACKET_LEN - CC1101_HEADER_LEN)
286 #define CC1101_PACKET_HEADER_VERSION (1u)
287 #define CC1101_MIN_PACKET_HEADER_VERSION \
345 static void CC1101_sendCommand(cc1101_commands_t command);
351 static uint8_t CC1101_readRegister(cc1101_registers_t reg);
359 static void CC1101_readRegisterBurst(cc1101_registers_t reg, uint8_t *buffer,
368 static void CC1101_sendRegisterBurst(cc1101_registers_t reg,
369 const uint8_t *buffer, uint8_t size);
376 static void CC1101_sendRegister(cc1101_registers_t address, uint8_t value);
382 static bool CC1101_initialise();
387 static void CC1101_configure();
393 static bool CC1101_csnLow();
398 static void CC1101_csnHigh();
403 static void CC1101_wakeUp();
408 static void CC1101_idle();
413 static void CC1101_sleep();
418 static void CC1101_interruptHandler();
423 static void CC1101_handle();
429 static void CC1101_txPower(cc1101_powerLevel_t power);
434 static void CC1101_setFrequency();
440 static bool CC1101_sanityCheck();
446 static void CC1101_setAddress(uint8_t address);
452 static uint8_t CC1101_getAddress(
void);
462 static bool CC1101_sendWithRetry(
const uint8_t recipient,
const void *buffer,
463 const uint8_t bufferSize,
const bool noACK);
473 static bool CC1101_send(
const uint8_t recipient, uint8_t *
data,
480 static bool CC1101_sendPacket(cc1101_packet_t *packet);
487 static void CC1101_sendFifo(
const uint8_t *buffer,
const uint8_t size);
494 static void CC1101_readFifo(uint8_t *buffer,
const uint8_t size);
499 static void CC1101_tx();
504 static void CC1101_rx();
510 static bool CC1101_packetAvailable();
517 static uint8_t CC1101_getData(uint8_t *buffer,
const uint8_t bufferSize);
526 static void CC1101_sendAck(
const uint8_t recipient,
534 static void CC1101_ATC();
541 static void CC1101_setATC(
bool onOff, int8_t targetRSSI);
547 static int16_t CC1101_getSendingRSSI();
553 static int16_t CC1101_getReceivingRSSI();
559 static int8_t CC1101_getTxPowerLevel();
565 static uint8_t CC1101_getTxPowerPercent();
571 static bool CC1101_setTxPowerPercent(
const uint8_t newPowerPercent);