MySensors Library & Examples  2.3.2-95-g0c695e3e
CC1101.h
Go to the documentation of this file.
1 
45 #ifndef _CC1101_h
46 #define _CC1101_h
47 
48 #include "CC1101Registers.h"
49 #include <stdint.h>
50 
51 // CC1101 hardware defaults
52 #if !defined(CC1101_SPI)
53 #define CC1101_SPI hwSPI
54 #endif
55 
56 // default PIN assignments, can be overridden
57 #if defined(ARDUINO_ARCH_AVR)
58 #if defined(__AVR_ATmega32U4__)
59 #define DEFAULT_CC1101_GD0_PIN (3)
60 #else
61 #define DEFAULT_CC1101_GD0_PIN (2)
62 #endif
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)
77 #else
78 #define DEFAULT_CC1101_GD0_PIN (2)
79 #endif
80 
81 #ifndef DEFAULT_CC1101_CS_PIN
82 #define DEFAULT_CC1101_CS_PIN (SS)
83 #endif
84 
85 #define CC1101_CRYSTAL_HZ 26000000LL
86 
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)
95 
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
104 #else
105 #error Unsupported value for MY_CC1101_FREQUENCY
106 #endif
107 
108 // Power levels represent which entry in the PA_TABLE to use.
109 typedef enum cc1101PowerLevel_e {
119 } cc1101_powerLevel_t;
120 
121 #ifndef MY_CC1101_POWER_LEVEL
122 #ifdef MY_GATEWAY_FEATURE
123 #define MY_CC1101_POWER_LEVEL CC1101_POWER_6
124 #else
125 #define MY_CC1101_POWER_LEVEL CC1101_POWER_AUTO
126 #endif
127 #endif
128 
129 #ifndef MY_CC1101_MIN_POWER_LEVEL
130 #define MY_CC1101_MIN_POWER_LEVEL (0)
131 #endif
132 
133 #ifndef MY_CC1101_MAX_POWER_LEVEL
134 #define MY_CC1101_MAX_POWER_LEVEL (7)
135 #endif
136 
137 #define CC1101_ATC_TARGET_RANGE_DBM (15)
138 #define CC1101_RSSI_OFFSET (74u)
139 #define CC1101_TARGET_RSSI (-50)
140 
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
145 
146 #ifndef MY_CC1101_MODEM_CONFIGURATION
147 #define MY_CC1101_MODEM_CONFIGURATION CC1101_MODEM_M_BR38_4_FD20
148 #endif
149 
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.
170 #else
171 #error Invalid setting for MY_CC1101_MODEM_CONFIGURATION.
172 #endif
173 
174 #define CC1101_MAX_PACKET_LEN (0x40u)
175 #define CC1101_PREAMBLE_LENGTH (8u)
176 #define CC1101_BROADCAST_ADDRESS 0xFF
177 
178 #ifndef MY_CC1101_SYNC_WORD
179 #define MY_CC1101_SYNC_WORD 0xF543
180 #endif
181 
182 #define CC1101_TX_RETRIES (5u)
183 
184 #if !defined(MY_CC1101_TX_TIMEOUT_MS)
185 #define MY_CC1101_TX_TIMEOUT_MS \
186  (50ul)
187 #endif
188 
189 #if !defined(MY_CC1101_TX_RETRY_DELAY_MS)
190 #define MY_CC1101_TX_RETRY_DELAY_MS \
191  (50l)
192 #endif
193 
194 #define CC1101_SEND_RETRIES (4u)
195 
196 #if !defined(MY_CC1101_SEND_TIMEOUT_MS)
197 #define MY_CC1101_SEND_TIMEOUT_MS (500ul)
198 #endif
199 
200 #if !defined(MY_CC1101_ACK_SEND_DELAY_MS)
201 #define MY_CC1101_ACK_SEND_DELAY_MS \
202  5
203 #endif
205 
206 #if !defined(MY_CC1101_CSN_TIMEOUT_MS)
207 #define MY_CC1101_CSN_TIMEOUT_MS (2ul)
208 #endif
209 
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
212 #endif
213 
214 #if !defined(CC1101_RETRY_TIMEOUT_MS)
215 // air-time approximation for timeout, 1 hop ~15 bytes payload - adjust if
216 // needed <fill in>
217 #define CC1101_RETRY_TIMEOUT_MS \
218  (1000ul)
219 #endif
221 
222 #define CC1101_WRITE_SINGLE 0x00
223 #define CC1101_WRITE_BURST 0x40
224 #define CC1101_READ_SINGLE 0x80
225 #define CC1101_READ_BURST 0xC0
226 
230 typedef uint16_t
231 cc1101_sequenceNumber_t; // will eventually change to uint8_t in 3.0
235 typedef int8_t cc1101_RSSI_t;
239 typedef uint8_t cc1101_LQI_t;
243 typedef union {
244  struct {
245  uint8_t : 5;
246  bool ackRssiReport : 1;
247  bool ackReceived : 1;
248  bool ackRequested : 1;
249  } fields;
250  uint8_t values;
252 
256 typedef uint8_t cc1101_status_t;
257 
261 typedef struct {
262  // First two bytes must be length/address per the CC1101 packet format.
263  uint8_t length;
264  uint8_t recipient;
265  uint8_t version;
266  uint8_t sender;
270 } __attribute__((packed)) cc1101_header_t;
271 
275 typedef struct {
276  cc1101_sequenceNumber_t sequenceNumber;
279 } __attribute__((packed)) cc1101_ack_t;
280 
281 #define CC1101_HEADER_LEN \
282  sizeof(cc1101_header_t)
283 #define CC1101_MAX_PAYLOAD_LEN \
284  (CC1101_MAX_PACKET_LEN - CC1101_HEADER_LEN)
285 
286 #define CC1101_PACKET_HEADER_VERSION (1u)
287 #define CC1101_MIN_PACKET_HEADER_VERSION \
288  (1u)
289 
290 
293 typedef struct {
294  union {
295  struct {
296  cc1101_header_t header;
297  union {
298  uint8_t payload[CC1101_MAX_PAYLOAD_LEN];
299  cc1101_ack_t ACK;
301  };
302  };
303  uint8_t data[CC1101_MAX_PACKET_LEN];
304  };
305  uint8_t payloadLen;
306  cc1101_RSSI_t RSSI;
307  cc1101_LQI_t LQI;
308 } __attribute__((packed)) cc1101_packet_t;
309 
313 typedef enum {
321 
325 typedef struct {
326  uint8_t address;
327  cc1101_packet_t currentPacket;
328  cc1101_packet_t lastAck;
330  cc1101_powerLevel_t powerLevel;
333  cc1101_status_t chipStatus; //<! CC1101 status byte.
334  bool ATCenabled;
335  volatile bool ackReceived : 1;
336  volatile bool dataReceived : 1;
337  volatile bool txComplete : 1;
338  volatile bool irqFired : 1;
340 
345 static void CC1101_sendCommand(cc1101_commands_t command);
346 
351 static uint8_t CC1101_readRegister(cc1101_registers_t reg);
352 
359 static void CC1101_readRegisterBurst(cc1101_registers_t reg, uint8_t *buffer,
360  uint8_t size);
361 
368 static void CC1101_sendRegisterBurst(cc1101_registers_t reg,
369  const uint8_t *buffer, uint8_t size);
370 
376 static void CC1101_sendRegister(cc1101_registers_t address, uint8_t value);
377 
382 static bool CC1101_initialise();
383 
387 static void CC1101_configure();
388 
393 static bool CC1101_csnLow();
394 
398 static void CC1101_csnHigh();
399 
403 static void CC1101_wakeUp();
404 
408 static void CC1101_idle();
409 
413 static void CC1101_sleep();
414 
418 static void CC1101_interruptHandler();
419 
423 static void CC1101_handle();
424 
429 static void CC1101_txPower(cc1101_powerLevel_t power);
430 
434 static void CC1101_setFrequency();
435 
440 static bool CC1101_sanityCheck();
441 
446 static void CC1101_setAddress(uint8_t address);
447 
452 static uint8_t CC1101_getAddress(void);
453 
462 static bool CC1101_sendWithRetry(const uint8_t recipient, const void *buffer,
463  const uint8_t bufferSize, const bool noACK);
464 
473 static bool CC1101_send(const uint8_t recipient, uint8_t *data,
474  const uint8_t len, const cc1101_controlFlags_t flags);
480 static bool CC1101_sendPacket(cc1101_packet_t *packet);
481 
487 static void CC1101_sendFifo(const uint8_t *buffer, const uint8_t size);
488 
494 static void CC1101_readFifo(uint8_t *buffer, const uint8_t size);
495 
499 static void CC1101_tx();
500 
504 static void CC1101_rx();
505 
510 static bool CC1101_packetAvailable();
511 
517 static uint8_t CC1101_getData(uint8_t *buffer, const uint8_t bufferSize);
518 
526 static void CC1101_sendAck(const uint8_t recipient,
527  const cc1101_sequenceNumber_t sequenceNumber,
528  const cc1101_RSSI_t RSSI, const cc1101_LQI_t LQI);
529 
534 static void CC1101_ATC();
535 
541 static void CC1101_setATC(bool onOff, int8_t targetRSSI);
542 
547 static int16_t CC1101_getSendingRSSI();
548 
553 static int16_t CC1101_getReceivingRSSI();
554 
559 static int8_t CC1101_getTxPowerLevel();
560 
565 static uint8_t CC1101_getTxPowerPercent();
566 
571 static bool CC1101_setTxPowerPercent(const uint8_t newPowerPercent);
572 
574 #endif
cc1101_controlFlags_t::values
uint8_t values
values
Definition: CC1101.h:250
data
char data[MAX_PAYLOAD_SIZE+1]
Buffer for raw payload data.
Definition: MyMessage.h:655
cc1101_status_t
uint8_t cc1101_status_t
Chip status byte.
Definition: CC1101.h:256
cc1101_controlFlags_t
Control flag data type.
Definition: CC1101.h:243
__attribute__::controlFlags
cc1101_controlFlags_t controlFlags
Control flags, used for ACK.
Definition: CC1101.h:267
cc1101_internal_t::lastAck
cc1101_packet_t lastAck
Last ack received.
Definition: CC1101.h:328
CC1101_MAX_PAYLOAD_LEN
#define CC1101_MAX_PAYLOAD_LEN
Max payload length.
Definition: CC1101.h:283
CC1101_MODE_IDLE
@ CC1101_MODE_IDLE
CC1101 is in idle mode.
Definition: CC1101.h:315
CC1101_POWER_AUTO
@ CC1101_POWER_AUTO
Enable ATC.
Definition: CC1101.h:118
cc1101_controlFlags_t::ackRequested
bool ackRequested
ackRequested
Definition: CC1101.h:248
CC1101_POWER_7
@ CC1101_POWER_7
10dBm
Definition: CC1101.h:117
CC1101_POWER_2
@ CC1101_POWER_2
-15dBm
Definition: CC1101.h:112
cc1101_controlFlags_t::ackReceived
bool ackReceived
ackReceived
Definition: CC1101.h:247
CC1101_MAX_PACKET_LEN
#define CC1101_MAX_PACKET_LEN
Maximum packet size that can be carried by the modem.
Definition: CC1101.h:174
__attribute__::sequenceNumber
cc1101_sequenceNumber_t sequenceNumber
Packet sequence number, used for ACK.
Definition: CC1101.h:269
CC1101_POWER_3
@ CC1101_POWER_3
-10dBm
Definition: CC1101.h:113
CC1101_POWER_5
@ CC1101_POWER_5
5dBm
Definition: CC1101.h:115
cc1101_sequenceNumber_t
uint16_t cc1101_sequenceNumber_t
Sequence number data type.
Definition: CC1101.h:231
__attribute__::version
uint8_t version
Header version.
Definition: CC1101.h:265
__attribute__::header
cc1101_header_t header
header
Definition: CC1101.h:296
__attribute__::recipient
uint8_t recipient
Payload recipient.
Definition: CC1101.h:264
cc1101_internal_t::radioMode
cc1101_radioModes_t radioMode
Current radio mode.
Definition: CC1101.h:331
cc1101_internal_t::ackReceived
volatile bool ackReceived
ACK received.
Definition: CC1101.h:335
__attribute__::payloadLen
uint8_t payloadLen
Length of payload (excluding header)
Definition: CC1101.h:305
CC1101_POWER_1
@ CC1101_POWER_1
-20dBm
Definition: CC1101.h:111
__attribute__::LQI
cc1101_LQI_t LQI
LQI.
Definition: CC1101.h:278
cc1101_internal_t
CC1101 internal variables.
Definition: CC1101.h:325
__attribute__
struct @4::@5 __attribute__
Doxygen will complain without this comment.
Definition: DigitalPin.h:65
cc1101_radioModes_t
cc1101_radioModes_t
CC1101 operation Modes.
Definition: CC1101.h:313
__attribute__::RSSI
cc1101_RSSI_t RSSI
RSSI.
Definition: CC1101.h:277
__attribute__::sender
uint8_t sender
Payload sender.
Definition: CC1101.h:266
cc1101_internal_t::irqFired
volatile bool irqFired
irq occured
Definition: CC1101.h:338
cc1101_internal_t::dataReceived
volatile bool dataReceived
Data received.
Definition: CC1101.h:336
CC1101_MODE_FS
@ CC1101_MODE_FS
CC1101 is in frequency synthesis mode.
Definition: CC1101.h:316
CC1101_MODE_SLEEP
@ CC1101_MODE_SLEEP
CC1101 is in sleep mode.
Definition: CC1101.h:314
cc1101PowerLevel_e
cc1101PowerLevel_e
Definition: CC1101.h:109
CC1101_MODE_TX
@ CC1101_MODE_TX
CC1101 is in transmit mode.
Definition: CC1101.h:317
cc1101_LQI_t
uint8_t cc1101_LQI_t
LQI data type.
Definition: CC1101.h:239
cc1101_internal_t::address
uint8_t address
Node address.
Definition: CC1101.h:326
CC1101_POWER_4
@ CC1101_POWER_4
0dBm
Definition: CC1101.h:114
cc1101_internal_t::currentPacket
cc1101_packet_t currentPacket
Buffer for current packet.
Definition: CC1101.h:327
cc1101_internal_t::targetRSSI
cc1101_RSSI_t targetRSSI
ATC target power level.
Definition: CC1101.h:332
__attribute__::length
uint8_t length
Length of data - 1 byte.
Definition: CC1101.h:263
CC1101_MODE_CCA
@ CC1101_MODE_CCA
CC1101 is in clear channel assessment mode.
Definition: CC1101.h:319
CC1101_POWER_0
@ CC1101_POWER_0
-30dBm
Definition: CC1101.h:110
cc1101_controlFlags_t::ackRssiReport
bool ackRssiReport
ackRssiReport
Definition: CC1101.h:246
cc1101_RSSI_t
int8_t cc1101_RSSI_t
RSSI data type.
Definition: CC1101.h:235
CC1101_POWER_6
@ CC1101_POWER_6
7dBm
Definition: CC1101.h:116
cc1101_internal_t::txComplete
volatile bool txComplete
Transmission complete.
Definition: CC1101.h:337
cc1101_internal_t::txSequenceNumber
cc1101_sequenceNumber_t txSequenceNumber
CC1101_txSequenceNumber.
Definition: CC1101.h:329
cc1101_internal_t::powerLevel
cc1101_powerLevel_t powerLevel
TX power level index.
Definition: CC1101.h:330
CC1101_MODE_RX
@ CC1101_MODE_RX
CC1101 is in receive mode.
Definition: CC1101.h:318
cc1101_internal_t::ATCenabled
bool ATCenabled
ATC enabled.
Definition: CC1101.h:334