diff --git a/examples/CC1101_Receive_Address/CC1101_Receive_Address.ino b/examples/CC1101_Receive_Address/CC1101_Receive_Address.ino new file mode 100644 index 00000000..dc067fa7 --- /dev/null +++ b/examples/CC1101_Receive_Address/CC1101_Receive_Address.ino @@ -0,0 +1,106 @@ +/* + RadioLib CC1101 Receive with Address Example + + This example receives packets using CC1101 FSK radio + module. Packets can have 1-byte address of the + destination node. After setting node address, this node + will automatically filter out any packets that do not + contain either node address or broadcast addresses. +*/ + +// include the library +#include + +// CC1101 is in slot A on the shield +CC1101 cc = RadioShield.ModuleA; + +void setup() { + Serial.begin(9600); + + // initialize CC1101 with default settings + Serial.print(F("[CC1101] Initializing ... ")); + // carrier frequency: 868.0 MHz + // bit rate: 4.8 kbps + // Rx bandwidth: 325.0 kHz + // frequency deviation: 48.0 kHz + // sync word: 0xD391 + int state = cc.begin(); + if (state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code ")); + Serial.println(state); + while (true); + } + + // set node address + // NOTE: Calling this method will autmatically enable + // address filtering. CC1101 also allows to set + // number of broadcast address (0/1/2). + // The following sets one brodcast address 0x00. + // When setting two broadcast addresses, 0x00 and + // 0xFF will be used. + Serial.print(F("[CC1101] Setting node address ... ")); + state = cc.setNodeAddress(0x01, 1); + if (state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code ")); + Serial.println(state); + while (true); + } + + // address filtering can also be disabled + // NOTE: Calling this method will also erase previously + // set node address + /* + Serial.print(F("[CC1101] Disabling address filtering ... ")); + state == cc.disableAddressFiltering(); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code ")); + Serial.println(state); + while(true); + } + */ +} + +void loop() { + Serial.print(F("[CC1101] Waiting for incoming transmission ... ")); + + // you can receive data as an Arduino String + String str; + int state = cc.receive(str); + + // you can also receive data as byte array + /* + byte byteArr[8]; + int state = cc.receive(byteArr, 8); + */ + + if (state == ERR_NONE) { + // packet was successfully received + Serial.println(F("success!")); + + // print the data of the packet + Serial.print(F("[CC1101] Data:\t\t")); + Serial.println(str); + + // print RSSI (Received Signal Strength Indicator) + // of the last received packet + Serial.print("[CC1101] RSSI:\t\t"); + Serial.print(cc.getRSSI()); + Serial.println(" dBm"); + + // print LQI (Link Quality Indicator) + // of the last received packet, lower is better + Serial.print("[CC1101] LQI:\t\t"); + Serial.println(cc.getLQI()); + + } else if (state == ERR_CRC_MISMATCH) { + // packet was received, but is malformed + Serial.println(F("CRC error!")); + + } +} diff --git a/examples/CC1101_Transmit_Address/CC1101_Transmit_Address.ino b/examples/CC1101_Transmit_Address/CC1101_Transmit_Address.ino new file mode 100644 index 00000000..2466f8d6 --- /dev/null +++ b/examples/CC1101_Transmit_Address/CC1101_Transmit_Address.ino @@ -0,0 +1,93 @@ +/* + RadioLib CC1101 Transmit to Address Example + + This example transmits packets using CC1101 FSK radio + module. Packets can have 1-byte address of the + destination node. After setting node address, this node + will automatically filter out any packets that do not + contain either node address or broadcast addresses. +*/ + +// include the library +#include + +// CC1101 is in slot A on the shield +CC1101 cc = RadioShield.ModuleA; + +void setup() { + Serial.begin(9600); + + // initialize CC1101 with default settings + Serial.print(F("[CC1101] Initializing ... ")); + // carrier frequency: 868.0 MHz + // bit rate: 4.8 kbps + // Rx bandwidth: 325.0 kHz + // frequency deviation: 48.0 kHz + // sync word: 0xD391 + int state = cc.begin(); + if (state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code ")); + Serial.println(state); + while (true); + } + + // set node address + // NOTE: Calling this method will autmatically enable + // address filtering. CC1101 also allows to set + // number of broadcast address (0/1/2). + // The following sets one brodcast address 0x00. + // When setting two broadcast addresses, 0x00 and + // 0xFF will be used. + Serial.print(F("[CC1101] Setting node address ... ")); + state = cc.setNodeAddress(0x01, 1); + if (state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code ")); + Serial.println(state); + while (true); + } + + // address filtering can also be disabled + // NOTE: Calling this method will also erase previously + // set node address + /* + Serial.print(F("[CC1101] Disabling address filtering ... ")); + state == cc.disableAddressFiltering(); + if(state == ERR_NONE) { + Serial.println(F("success!")); + } else { + Serial.print(F("failed, code ")); + Serial.println(state); + while(true); + } + */ +} + +void loop() { + Serial.print(F("[CC1101] Transmitting packet ... ")); + + // you can transmit C-string or Arduino string up to 63 characters long + int state = cc.transmit("Hello World!"); + + // you can also transmit byte array up to 63 bytes long + /* + byte byteArr[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF}; + int state = cc.transmit(byteArr, 8); + */ + + if (state == ERR_NONE) { + // the packet was successfully transmitted + Serial.println(" success!"); + + } else if (state == ERR_PACKET_TOO_LONG) { + // the supplied packet was longer than 255 bytes + Serial.println(" too long!"); + + } + + // wait for a second before transmitting again + delay(1000); +} diff --git a/keywords.txt b/keywords.txt index ea9c4a2b..cf14ef4a 100644 --- a/keywords.txt +++ b/keywords.txt @@ -188,3 +188,5 @@ ASCII_EXTENDED LITERAL1 ITA2 LITERAL1 ERR_INVALID_RTTY_SHIFT LITERAL1 ERR_UNSUPPORTED_ENCODING LITERAL1 + +ERR_INVALID_NUM_BROAD_ADDRS LITERAL1 diff --git a/src/TypeDef.h b/src/TypeDef.h index 565489eb..c9f2cc8d 100644 --- a/src/TypeDef.h +++ b/src/TypeDef.h @@ -60,7 +60,7 @@ #define ERR_NONE 0 #define ERR_UNKNOWN -1 -// SX127x/RFM9x/RF69 status codes +// SX127x/RFM9x/RF69/CC1101 status codes #define ERR_CHIP_NOT_FOUND -2 #define ERR_EEPROM_NOT_INITIALIZED -3 #define ERR_PACKET_TOO_LONG -4 @@ -117,4 +117,7 @@ #define ERR_INVALID_RTTY_SHIFT -401 #define ERR_UNSUPPORTED_ENCODING -402 +// CC1101-specific status codes +#define ERR_INVALID_NUM_BROAD_ADDRS -601 + #endif diff --git a/src/modules/CC1101.cpp b/src/modules/CC1101.cpp index 3f4284d5..82095294 100644 --- a/src/modules/CC1101.cpp +++ b/src/modules/CC1101.cpp @@ -327,6 +327,32 @@ int16_t CC1101::setSyncWord(uint8_t syncH, uint8_t syncL) { return(state); } +int16_t CC1101::setNodeAddress(uint8_t nodeAddr, uint8_t numBroadcastAddrs) { + if(!(numBroadcastAddrs > 0) && (numBroadcastAddrs <= 2)) { + return(ERR_INVALID_NUM_BROAD_ADDRS); + } + + // enable address filtering + int16_t state = SPIsetRegValue(CC1101_REG_PKTCTRL1, numBroadcastAddrs + 0x01, 1, 0); + if(state != ERR_NONE) { + return(state); + } + + // set node address + return(SPIsetRegValue(CC1101_REG_ADDR, nodeAddr)); +} + +int16_t CC1101::disableAddressFiltering() { + // disable address filtering + int16_t state = _mod->SPIsetRegValue(CC1101_REG_PKTCTRL1, CC1101_ADR_CHK_NONE, 1, 0); + if(state != ERR_NONE) { + return(state); + } + + // set node address to default (0x00) + return(SPIsetRegValue(CC1101_REG_ADDR, 0x00)); +} + float CC1101::getRSSI() { float rssi; if(_rawRSSI >= 128) { @@ -349,7 +375,6 @@ int16_t CC1101::config() { } // set packet mode - // TODO: address filtering state = SPIsetRegValue(CC1101_REG_PKTCTRL1, CC1101_CRC_AUTOFLUSH_OFF | CC1101_APPEND_STATUS_ON | CC1101_ADR_CHK_NONE, 3, 0); state |= SPIsetRegValue(CC1101_REG_PKTCTRL0, CC1101_WHITE_DATA_OFF | CC1101_PKT_FORMAT_NORMAL, 6, 4); state |= SPIsetRegValue(CC1101_REG_PKTCTRL0, CC1101_CRC_ON | CC1101_LENGTH_CONFIG_VARIABLE, 2, 0); diff --git a/src/modules/CC1101.h b/src/modules/CC1101.h index b2526be2..479af514 100644 --- a/src/modules/CC1101.h +++ b/src/modules/CC1101.h @@ -516,6 +516,8 @@ class CC1101: public PhysicalLayer { int16_t setRxBandwidth(float rxBw); int16_t setFrequencyDeviation(float freqDev); int16_t setSyncWord(uint8_t syncH, uint8_t syncL); + int16_t setNodeAddress(uint8_t nodeAddr, uint8_t numBroadcastAddrs = 0); + int16_t disableAddressFiltering(); float getRSSI(); uint8_t getLQI();