From 58c9c8f51dcec555250195d127a49572c1b9fd9a Mon Sep 17 00:00:00 2001 From: krolyxon Date: Thu, 14 May 2026 23:19:41 +0530 Subject: add .clang-format, and apply formatting --- src/libs/Adafruit_PN532.cpp | 1934 ++++++++++++------------ src/libs/Adafruit_PN532.h | 168 +-- src/libs/BleConnectionStatus.cpp | 23 +- src/libs/BleConnectionStatus.h | 17 +- src/libs/BleMouse.cpp | 250 ++-- src/libs/BleMouse.h | 61 +- src/libs/ELECHOUSE_CC1101_SRC_DRV.cpp | 2619 ++++++++++++++++++--------------- src/libs/ELECHOUSE_CC1101_SRC_DRV.h | 345 ++--- 8 files changed, 2805 insertions(+), 2612 deletions(-) (limited to 'src/libs') diff --git a/src/libs/Adafruit_PN532.cpp b/src/libs/Adafruit_PN532.cpp index 4665656..0a547b7 100644 --- a/src/libs/Adafruit_PN532.cpp +++ b/src/libs/Adafruit_PN532.cpp @@ -91,9 +91,9 @@ byte pn532_packetbuffer[PN532_PACKBUFFSIZ]; ///< Packet buffer used in various /**************************************************************************/ Adafruit_PN532::Adafruit_PN532(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t ss) { - _cs = ss; - spi_dev = new Adafruit_SPIDevice(ss, clk, miso, mosi, 1000000, - SPI_BITORDER_LSBFIRST, SPI_MODE0); + _cs = ss; + spi_dev = new Adafruit_SPIDevice(ss, clk, miso, mosi, 1000000, + SPI_BITORDER_LSBFIRST, SPI_MODE0); } /**************************************************************************/ @@ -107,9 +107,9 @@ Adafruit_PN532::Adafruit_PN532(uint8_t clk, uint8_t miso, uint8_t mosi, /**************************************************************************/ Adafruit_PN532::Adafruit_PN532(uint8_t irq, uint8_t reset, TwoWire *theWire) : _irq(irq), _reset(reset) { - pinMode(_irq, INPUT); - pinMode(_reset, OUTPUT); - i2c_dev = new Adafruit_I2CDevice(PN532_I2C_ADDRESS, theWire); + pinMode(_irq, INPUT); + pinMode(_reset, OUTPUT); + i2c_dev = new Adafruit_I2CDevice(PN532_I2C_ADDRESS, theWire); } /**************************************************************************/ @@ -121,9 +121,9 @@ Adafruit_PN532::Adafruit_PN532(uint8_t irq, uint8_t reset, TwoWire *theWire) */ /**************************************************************************/ Adafruit_PN532::Adafruit_PN532(uint8_t ss, SPIClass *theSPI) { - _cs = ss; - spi_dev = new Adafruit_SPIDevice(ss, 1000000, SPI_BITORDER_LSBFIRST, - SPI_MODE0, theSPI); + _cs = ss; + spi_dev = new Adafruit_SPIDevice(ss, 1000000, SPI_BITORDER_LSBFIRST, + SPI_MODE0, theSPI); } /**************************************************************************/ @@ -136,8 +136,8 @@ Adafruit_PN532::Adafruit_PN532(uint8_t ss, SPIClass *theSPI) { /**************************************************************************/ Adafruit_PN532::Adafruit_PN532(uint8_t reset, HardwareSerial *theSer) : _reset(reset) { - pinMode(_reset, OUTPUT); - ser_dev = theSer; + pinMode(_reset, OUTPUT); + ser_dev = theSer; } /**************************************************************************/ @@ -148,30 +148,30 @@ Adafruit_PN532::Adafruit_PN532(uint8_t reset, HardwareSerial *theSer) */ /**************************************************************************/ bool Adafruit_PN532::begin() { - if (spi_dev) { - // SPI initialization - if (!spi_dev->begin()) { - return false; - } - } else if (i2c_dev) { - // I2C initialization - // PN532 will fail address check since its asleep, so suppress - if (!i2c_dev->begin(false)) { - return false; + if (spi_dev) { + // SPI initialization + if (!spi_dev->begin()) { + return false; + } + } else if (i2c_dev) { + // I2C initialization + // PN532 will fail address check since its asleep, so suppress + if (!i2c_dev->begin(false)) { + return false; + } + } else if (ser_dev) { + ser_dev->begin(115200); + // clear out anything in read buffer + while (ser_dev->available()) + ser_dev->read(); + } else { + // no interface specified + return false; } - } else if (ser_dev) { - ser_dev->begin(115200); - // clear out anything in read buffer - while (ser_dev->available()) - ser_dev->read(); - } else { - // no interface specified - return false; - } - reset(); // HW reset - put in known state - delay(10); - wakeup(); // hey! wakeup! - return true; + reset(); // HW reset - put in known state + delay(10); + wakeup(); // hey! wakeup! + return true; } /**************************************************************************/ @@ -180,13 +180,13 @@ bool Adafruit_PN532::begin() { */ /**************************************************************************/ void Adafruit_PN532::reset(void) { - // see Datasheet p.209, Fig.48 for timings - if (_reset != -1) { - digitalWrite(_reset, LOW); - delay(1); // min 20ns - digitalWrite(_reset, HIGH); - delay(2); // max 2ms - } + // see Datasheet p.209, Fig.48 for timings + if (_reset != -1) { + digitalWrite(_reset, LOW); + delay(1); // min 20ns + digitalWrite(_reset, HIGH); + delay(2); // max 2ms + } } /**************************************************************************/ @@ -195,21 +195,21 @@ void Adafruit_PN532::reset(void) { */ /**************************************************************************/ void Adafruit_PN532::wakeup(void) { - // interface specific wakeups - each one is unique! - if (spi_dev) { - // hold CS low for 2ms - digitalWrite(_cs, LOW); - delay(2); - } else if (ser_dev) { - uint8_t w[3] = {0x55, 0x00, 0x00}; - ser_dev->write(w, 3); - delay(2); - } - - // PN532 will clock stretch I2C during SAMConfig as a "wakeup" - - // need to config SAM to stay in Normal Mode - SAMConfig(); + // interface specific wakeups - each one is unique! + if (spi_dev) { + // hold CS low for 2ms + digitalWrite(_cs, LOW); + delay(2); + } else if (ser_dev) { + uint8_t w[3] = {0x55, 0x00, 0x00}; + ser_dev->write(w, 3); + delay(2); + } + + // PN532 will clock stretch I2C during SAMConfig as a "wakeup" + + // need to config SAM to stay in Normal Mode + SAMConfig(); } /**************************************************************************/ @@ -221,18 +221,18 @@ void Adafruit_PN532::wakeup(void) { */ /**************************************************************************/ void Adafruit_PN532::PrintHex(const byte *data, const uint32_t numBytes) { - uint32_t szPos; - for (szPos = 0; szPos < numBytes; szPos++) { - PN532DEBUGPRINT.print(F("0x")); - // Append leading 0 for small values - if (data[szPos] <= 0xF) - PN532DEBUGPRINT.print(F("0")); - PN532DEBUGPRINT.print(data[szPos] & 0xff, HEX); - if ((numBytes > 1) && (szPos != numBytes - 1)) { - PN532DEBUGPRINT.print(F(" ")); + uint32_t szPos; + for (szPos = 0; szPos < numBytes; szPos++) { + PN532DEBUGPRINT.print(F("0x")); + // Append leading 0 for small values + if (data[szPos] <= 0xF) + PN532DEBUGPRINT.print(F("0")); + PN532DEBUGPRINT.print(data[szPos] & 0xff, HEX); + if ((numBytes > 1) && (szPos != numBytes - 1)) { + PN532DEBUGPRINT.print(F(" ")); + } } - } - PN532DEBUGPRINT.println(); + PN532DEBUGPRINT.println(); } /**************************************************************************/ @@ -247,24 +247,24 @@ void Adafruit_PN532::PrintHex(const byte *data, const uint32_t numBytes) { */ /**************************************************************************/ void Adafruit_PN532::PrintHexChar(const byte *data, const uint32_t numBytes) { - uint32_t szPos; - for (szPos = 0; szPos < numBytes; szPos++) { - // Append leading 0 for small values - if (data[szPos] <= 0xF) - PN532DEBUGPRINT.print(F("0")); - PN532DEBUGPRINT.print(data[szPos], HEX); - if ((numBytes > 1) && (szPos != numBytes - 1)) { - PN532DEBUGPRINT.print(F(" ")); + uint32_t szPos; + for (szPos = 0; szPos < numBytes; szPos++) { + // Append leading 0 for small values + if (data[szPos] <= 0xF) + PN532DEBUGPRINT.print(F("0")); + PN532DEBUGPRINT.print(data[szPos], HEX); + if ((numBytes > 1) && (szPos != numBytes - 1)) { + PN532DEBUGPRINT.print(F(" ")); + } } - } - PN532DEBUGPRINT.print(F(" ")); - for (szPos = 0; szPos < numBytes; szPos++) { - if (data[szPos] <= 0x1F) - PN532DEBUGPRINT.print(F(".")); - else - PN532DEBUGPRINT.print((char)data[szPos]); - } - PN532DEBUGPRINT.println(); + PN532DEBUGPRINT.print(F(" ")); + for (szPos = 0; szPos < numBytes; szPos++) { + if (data[szPos] <= 0x1F) + PN532DEBUGPRINT.print(F(".")); + else + PN532DEBUGPRINT.print((char)data[szPos]); + } + PN532DEBUGPRINT.println(); } /**************************************************************************/ @@ -275,36 +275,36 @@ void Adafruit_PN532::PrintHexChar(const byte *data, const uint32_t numBytes) { */ /**************************************************************************/ uint32_t Adafruit_PN532::getFirmwareVersion(void) { - uint32_t response; + uint32_t response; - pn532_packetbuffer[0] = PN532_COMMAND_GETFIRMWAREVERSION; + pn532_packetbuffer[0] = PN532_COMMAND_GETFIRMWAREVERSION; - if (!sendCommandCheckAck(pn532_packetbuffer, 1)) { - return 0; - } + if (!sendCommandCheckAck(pn532_packetbuffer, 1)) { + return 0; + } - // read data packet - readdata(pn532_packetbuffer, 13); + // read data packet + readdata(pn532_packetbuffer, 13); - // check some basic stuff - if (0 != memcmp((char *)pn532_packetbuffer, - (char *)pn532response_firmwarevers, 6)) { + // check some basic stuff + if (0 != memcmp((char *)pn532_packetbuffer, + (char *)pn532response_firmwarevers, 6)) { #ifdef PN532DEBUG - PN532DEBUGPRINT.println(F("Firmware doesn't match!")); + PN532DEBUGPRINT.println(F("Firmware doesn't match!")); #endif - return 0; - } - - int offset = 7; - response = pn532_packetbuffer[offset++]; - response <<= 8; - response |= pn532_packetbuffer[offset++]; - response <<= 8; - response |= pn532_packetbuffer[offset++]; - response <<= 8; - response |= pn532_packetbuffer[offset++]; - - return response; + return 0; + } + + int offset = 7; + response = pn532_packetbuffer[offset++]; + response <<= 8; + response |= pn532_packetbuffer[offset++]; + response <<= 8; + response |= pn532_packetbuffer[offset++]; + response <<= 8; + response |= pn532_packetbuffer[offset++]; + + return response; } /**************************************************************************/ @@ -323,46 +323,46 @@ uint32_t Adafruit_PN532::getFirmwareVersion(void) { bool Adafruit_PN532::sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, uint16_t timeout) { - // I2C works without using IRQ pin by polling for RDY byte - // seems to work best with some delays between transactions - uint8_t SLOWDOWN = 0; - if (i2c_dev || spi_dev) // SPI and I2C need 1ms slow for page reads - SLOWDOWN = 1; + // I2C works without using IRQ pin by polling for RDY byte + // seems to work best with some delays between transactions + uint8_t SLOWDOWN = 0; + if (i2c_dev || spi_dev) // SPI and I2C need 1ms slow for page reads + SLOWDOWN = 1; - // write the command - writecommand(cmd, cmdlen); + // write the command + writecommand(cmd, cmdlen); - // I2C TUNING - delay(SLOWDOWN); + // I2C TUNING + delay(SLOWDOWN); - // Wait for chip to say its ready! - if (!waitready(timeout)) { - return false; - } + // Wait for chip to say its ready! + if (!waitready(timeout)) { + return false; + } #ifdef PN532DEBUG - if (spi_dev == NULL) { - PN532DEBUGPRINT.println(F("IRQ received")); - } + if (spi_dev == NULL) { + PN532DEBUGPRINT.println(F("IRQ received")); + } #endif - // read acknowledgement - if (!readack()) { + // read acknowledgement + if (!readack()) { #ifdef PN532DEBUG - PN532DEBUGPRINT.println(F("No ACK frame received!")); + PN532DEBUGPRINT.println(F("No ACK frame received!")); #endif - return false; - } + return false; + } - // I2C TUNING - delay(SLOWDOWN); + // I2C TUNING + delay(SLOWDOWN); - // Wait for chip to say its ready! - if (!waitready(timeout)) { - return false; - } + // Wait for chip to say its ready! + if (!waitready(timeout)) { + return false; + } - return true; // ack'd command + return true; // ack'd command } /**************************************************************************/ @@ -389,37 +389,37 @@ bool Adafruit_PN532::sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, */ /**************************************************************************/ bool Adafruit_PN532::writeGPIO(uint8_t pinstate) { - // uint8_t errorbit; + // uint8_t errorbit; - // Make sure pinstate does not try to toggle P32 or P34 - pinstate |= (1 << PN532_GPIO_P32) | (1 << PN532_GPIO_P34); + // Make sure pinstate does not try to toggle P32 or P34 + pinstate |= (1 << PN532_GPIO_P32) | (1 << PN532_GPIO_P34); - // Fill command buffer - pn532_packetbuffer[0] = PN532_COMMAND_WRITEGPIO; - pn532_packetbuffer[1] = PN532_GPIO_VALIDATIONBIT | pinstate; // P3 Pins - pn532_packetbuffer[2] = 0x00; // P7 GPIO Pins (not used ... taken by SPI) + // Fill command buffer + pn532_packetbuffer[0] = PN532_COMMAND_WRITEGPIO; + pn532_packetbuffer[1] = PN532_GPIO_VALIDATIONBIT | pinstate; // P3 Pins + pn532_packetbuffer[2] = 0x00; // P7 GPIO Pins (not used ... taken by SPI) #ifdef PN532DEBUG - PN532DEBUGPRINT.print(F("Writing P3 GPIO: ")); - PN532DEBUGPRINT.println(pn532_packetbuffer[1], HEX); + PN532DEBUGPRINT.print(F("Writing P3 GPIO: ")); + PN532DEBUGPRINT.println(pn532_packetbuffer[1], HEX); #endif - // Send the WRITEGPIO command (0x0E) - if (!sendCommandCheckAck(pn532_packetbuffer, 3)) - return 0x0; + // Send the WRITEGPIO command (0x0E) + if (!sendCommandCheckAck(pn532_packetbuffer, 3)) + return 0x0; - // Read response packet (00 FF PLEN PLENCHECKSUM D5 CMD+1(0x0F) DATACHECKSUM - // 00) - readdata(pn532_packetbuffer, 8); + // Read response packet (00 FF PLEN PLENCHECKSUM D5 CMD+1(0x0F) DATACHECKSUM + // 00) + readdata(pn532_packetbuffer, 8); #ifdef PN532DEBUG - PN532DEBUGPRINT.print(F("Received: ")); - PrintHex(pn532_packetbuffer, 8); - PN532DEBUGPRINT.println(); + PN532DEBUGPRINT.print(F("Received: ")); + PrintHex(pn532_packetbuffer, 8); + PN532DEBUGPRINT.println(); #endif - int offset = 6; - return (pn532_packetbuffer[offset] == 0x0F); + int offset = 6; + return (pn532_packetbuffer[offset] == 0x0F); } /**************************************************************************/ @@ -437,53 +437,52 @@ bool Adafruit_PN532::writeGPIO(uint8_t pinstate) { */ /**************************************************************************/ uint8_t Adafruit_PN532::readGPIO(void) { - pn532_packetbuffer[0] = PN532_COMMAND_READGPIO; + pn532_packetbuffer[0] = PN532_COMMAND_READGPIO; - // Send the READGPIO command (0x0C) - if (!sendCommandCheckAck(pn532_packetbuffer, 1)) - return 0x0; + // Send the READGPIO command (0x0C) + if (!sendCommandCheckAck(pn532_packetbuffer, 1)) + return 0x0; - // Read response packet (00 FF PLEN PLENCHECKSUM D5 CMD+1(0x0D) P3 P7 IO1 - // DATACHECKSUM 00) - readdata(pn532_packetbuffer, 11); + // Read response packet (00 FF PLEN PLENCHECKSUM D5 CMD+1(0x0D) P3 P7 IO1 + // DATACHECKSUM 00) + readdata(pn532_packetbuffer, 11); - /* READGPIO response should be in the following format: + /* READGPIO response should be in the following format: - byte Description - ------------- ------------------------------------------ - b0..5 Frame header and preamble (with I2C there is an extra 0x00) - b6 P3 GPIO Pins - b7 P7 GPIO Pins (not used ... taken by SPI) - b8 Interface Mode Pins (not used ... bus select pins) - b9..10 checksum */ + byte Description + ------------- ------------------------------------------ + b0..5 Frame header and preamble (with I2C there is an extra + 0x00) b6 P3 GPIO Pins b7 P7 GPIO Pins (not used + ... taken by SPI) b8 Interface Mode Pins (not used ... bus + select pins) b9..10 checksum */ - int p3offset = 7; + int p3offset = 7; #ifdef PN532DEBUG - PN532DEBUGPRINT.print(F("Received: ")); - PrintHex(pn532_packetbuffer, 11); - PN532DEBUGPRINT.println(); - PN532DEBUGPRINT.print(F("P3 GPIO: 0x")); - PN532DEBUGPRINT.println(pn532_packetbuffer[p3offset], HEX); - PN532DEBUGPRINT.print(F("P7 GPIO: 0x")); - PN532DEBUGPRINT.println(pn532_packetbuffer[p3offset + 1], HEX); - PN532DEBUGPRINT.print(F("IO GPIO: 0x")); - PN532DEBUGPRINT.println(pn532_packetbuffer[p3offset + 2], HEX); - // Note: You can use the IO GPIO value to detect the serial bus being used - switch (pn532_packetbuffer[p3offset + 2]) { - case 0x00: // Using UART - PN532DEBUGPRINT.println(F("Using UART (IO = 0x00)")); - break; - case 0x01: // Using I2C - PN532DEBUGPRINT.println(F("Using I2C (IO = 0x01)")); - break; - case 0x02: // Using SPI - PN532DEBUGPRINT.println(F("Using SPI (IO = 0x02)")); - break; - } + PN532DEBUGPRINT.print(F("Received: ")); + PrintHex(pn532_packetbuffer, 11); + PN532DEBUGPRINT.println(); + PN532DEBUGPRINT.print(F("P3 GPIO: 0x")); + PN532DEBUGPRINT.println(pn532_packetbuffer[p3offset], HEX); + PN532DEBUGPRINT.print(F("P7 GPIO: 0x")); + PN532DEBUGPRINT.println(pn532_packetbuffer[p3offset + 1], HEX); + PN532DEBUGPRINT.print(F("IO GPIO: 0x")); + PN532DEBUGPRINT.println(pn532_packetbuffer[p3offset + 2], HEX); + // Note: You can use the IO GPIO value to detect the serial bus being used + switch (pn532_packetbuffer[p3offset + 2]) { + case 0x00: // Using UART + PN532DEBUGPRINT.println(F("Using UART (IO = 0x00)")); + break; + case 0x01: // Using I2C + PN532DEBUGPRINT.println(F("Using I2C (IO = 0x01)")); + break; + case 0x02: // Using SPI + PN532DEBUGPRINT.println(F("Using SPI (IO = 0x02)")); + break; + } #endif - return pn532_packetbuffer[p3offset]; + return pn532_packetbuffer[p3offset]; } /**************************************************************************/ @@ -493,19 +492,19 @@ uint8_t Adafruit_PN532::readGPIO(void) { */ /**************************************************************************/ bool Adafruit_PN532::SAMConfig(void) { - pn532_packetbuffer[0] = PN532_COMMAND_SAMCONFIGURATION; - pn532_packetbuffer[1] = 0x01; // normal mode; - pn532_packetbuffer[2] = 0x14; // timeout 50ms * 20 = 1 second - pn532_packetbuffer[3] = 0x01; // use IRQ pin! + pn532_packetbuffer[0] = PN532_COMMAND_SAMCONFIGURATION; + pn532_packetbuffer[1] = 0x01; // normal mode; + pn532_packetbuffer[2] = 0x14; // timeout 50ms * 20 = 1 second + pn532_packetbuffer[3] = 0x01; // use IRQ pin! - if (!sendCommandCheckAck(pn532_packetbuffer, 4)) - return false; + if (!sendCommandCheckAck(pn532_packetbuffer, 4)) + return false; - // read data packet - readdata(pn532_packetbuffer, 9); + // read data packet + readdata(pn532_packetbuffer, 9); - int offset = 6; - return (pn532_packetbuffer[offset] == 0x15); + int offset = 6; + return (pn532_packetbuffer[offset] == 0x15); } /**************************************************************************/ @@ -519,22 +518,22 @@ bool Adafruit_PN532::SAMConfig(void) { */ /**************************************************************************/ bool Adafruit_PN532::setPassiveActivationRetries(uint8_t maxRetries) { - pn532_packetbuffer[0] = PN532_COMMAND_RFCONFIGURATION; - pn532_packetbuffer[1] = 5; // Config item 5 (MaxRetries) - pn532_packetbuffer[2] = 0xFF; // MxRtyATR (default = 0xFF) - pn532_packetbuffer[3] = 0x01; // MxRtyPSL (default = 0x01) - pn532_packetbuffer[4] = maxRetries; + pn532_packetbuffer[0] = PN532_COMMAND_RFCONFIGURATION; + pn532_packetbuffer[1] = 5; // Config item 5 (MaxRetries) + pn532_packetbuffer[2] = 0xFF; // MxRtyATR (default = 0xFF) + pn532_packetbuffer[3] = 0x01; // MxRtyPSL (default = 0x01) + pn532_packetbuffer[4] = maxRetries; #ifdef MIFAREDEBUG - PN532DEBUGPRINT.print(F("Setting MxRtyPassiveActivation to ")); - PN532DEBUGPRINT.print(maxRetries, DEC); - PN532DEBUGPRINT.println(F(" ")); + PN532DEBUGPRINT.print(F("Setting MxRtyPassiveActivation to ")); + PN532DEBUGPRINT.print(maxRetries, DEC); + PN532DEBUGPRINT.println(F(" ")); #endif - if (!sendCommandCheckAck(pn532_packetbuffer, 5)) - return 0x0; // no ACK + if (!sendCommandCheckAck(pn532_packetbuffer, 5)) + return 0x0; // no ACK - return 1; + return 1; } /***** ISO14443A Commands ******/ @@ -556,18 +555,19 @@ bool Adafruit_PN532::setPassiveActivationRetries(uint8_t maxRetries) { /**************************************************************************/ bool Adafruit_PN532::readPassiveTargetID(uint8_t cardbaudrate, uint8_t *uid, uint8_t *uidLength, uint16_t timeout) { - pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET; - pn532_packetbuffer[1] = 1; // max 1 cards at once (we can set this to 2 later) - pn532_packetbuffer[2] = cardbaudrate; + pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET; + pn532_packetbuffer[1] = + 1; // max 1 cards at once (we can set this to 2 later) + pn532_packetbuffer[2] = cardbaudrate; - if (!sendCommandCheckAck(pn532_packetbuffer, 3, timeout)) { + if (!sendCommandCheckAck(pn532_packetbuffer, 3, timeout)) { #ifdef PN532DEBUG - PN532DEBUGPRINT.println(F("No card(s) read")); + PN532DEBUGPRINT.println(F("No card(s) read")); #endif - return 0x0; // no cards read - } + return 0x0; // no cards read + } - return readDetectedPassiveTargetID(uid, uidLength); + return readDetectedPassiveTargetID(uid, uidLength); } /**************************************************************************/ @@ -579,11 +579,12 @@ bool Adafruit_PN532::readPassiveTargetID(uint8_t cardbaudrate, uint8_t *uid, */ /**************************************************************************/ bool Adafruit_PN532::startPassiveTargetIDDetection(uint8_t cardbaudrate) { - pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET; - pn532_packetbuffer[1] = 1; // max 1 cards at once (we can set this to 2 later) - pn532_packetbuffer[2] = cardbaudrate; + pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET; + pn532_packetbuffer[1] = + 1; // max 1 cards at once (we can set this to 2 later) + pn532_packetbuffer[2] = cardbaudrate; - return sendCommandCheckAck(pn532_packetbuffer, 3); + return sendCommandCheckAck(pn532_packetbuffer, 3); } /**************************************************************************/ @@ -600,57 +601,57 @@ bool Adafruit_PN532::startPassiveTargetIDDetection(uint8_t cardbaudrate) { /**************************************************************************/ bool Adafruit_PN532::readDetectedPassiveTargetID(uint8_t *uid, uint8_t *uidLength) { - // read data packet - readdata(pn532_packetbuffer, 20); - // check some basic stuff - - /* ISO14443A card response should be in the following format: - - byte Description - ------------- ------------------------------------------ - b0..6 Frame header and preamble - b7 Tags Found - b8 Tag Number (only one used in this example) - b9..10 SENS_RES - b11 SEL_RES - b12 NFCID Length - b13..NFCIDLen NFCID */ + // read data packet + readdata(pn532_packetbuffer, 20); + // check some basic stuff + + /* ISO14443A card response should be in the following format: + + byte Description + ------------- ------------------------------------------ + b0..6 Frame header and preamble + b7 Tags Found + b8 Tag Number (only one used in this example) + b9..10 SENS_RES + b11 SEL_RES + b12 NFCID Length + b13..NFCIDLen NFCID */ #ifdef MIFAREDEBUG - PN532DEBUGPRINT.print(F("Found ")); - PN532DEBUGPRINT.print(pn532_packetbuffer[7], DEC); - PN532DEBUGPRINT.println(F(" tags")); + PN532DEBUGPRINT.print(F("Found ")); + PN532DEBUGPRINT.print(pn532_packetbuffer[7], DEC); + PN532DEBUGPRINT.println(F(" tags")); #endif - if (pn532_packetbuffer[7] != 1) - return 0; + if (pn532_packetbuffer[7] != 1) + return 0; - uint16_t sens_res = pn532_packetbuffer[9]; - sens_res <<= 8; - sens_res |= pn532_packetbuffer[10]; + uint16_t sens_res = pn532_packetbuffer[9]; + sens_res <<= 8; + sens_res |= pn532_packetbuffer[10]; #ifdef MIFAREDEBUG - PN532DEBUGPRINT.print(F("ATQA: 0x")); - PN532DEBUGPRINT.println(sens_res, HEX); - PN532DEBUGPRINT.print(F("SAK: 0x")); - PN532DEBUGPRINT.println(pn532_packetbuffer[11], HEX); + PN532DEBUGPRINT.print(F("ATQA: 0x")); + PN532DEBUGPRINT.println(sens_res, HEX); + PN532DEBUGPRINT.print(F("SAK: 0x")); + PN532DEBUGPRINT.println(pn532_packetbuffer[11], HEX); #endif - /* Card appears to be Mifare Classic */ - *uidLength = pn532_packetbuffer[12]; + /* Card appears to be Mifare Classic */ + *uidLength = pn532_packetbuffer[12]; #ifdef MIFAREDEBUG - PN532DEBUGPRINT.print(F("UID:")); + PN532DEBUGPRINT.print(F("UID:")); #endif - for (uint8_t i = 0; i < pn532_packetbuffer[12]; i++) { - uid[i] = pn532_packetbuffer[13 + i]; + for (uint8_t i = 0; i < pn532_packetbuffer[12]; i++) { + uid[i] = pn532_packetbuffer[13 + i]; #ifdef MIFAREDEBUG - PN532DEBUGPRINT.print(F(" 0x")); - PN532DEBUGPRINT.print(uid[i], HEX); + PN532DEBUGPRINT.print(F(" 0x")); + PN532DEBUGPRINT.print(uid[i], HEX); #endif - } + } #ifdef MIFAREDEBUG - PN532DEBUGPRINT.println(); + PN532DEBUGPRINT.println(); #endif - return 1; + return 1; } /**************************************************************************/ @@ -667,77 +668,77 @@ bool Adafruit_PN532::readDetectedPassiveTargetID(uint8_t *uid, bool Adafruit_PN532::inDataExchange(uint8_t *send, uint8_t sendLength, uint8_t *response, uint8_t *responseLength) { - if (sendLength > PN532_PACKBUFFSIZ - 2) { + if (sendLength > PN532_PACKBUFFSIZ - 2) { #ifdef PN532DEBUG - PN532DEBUGPRINT.println(F("APDU length too long for packet buffer")); + PN532DEBUGPRINT.println(F("APDU length too long for packet buffer")); #endif - return false; - } - uint8_t i; + return false; + } + uint8_t i; - pn532_packetbuffer[0] = 0x40; // PN532_COMMAND_INDATAEXCHANGE; - pn532_packetbuffer[1] = _inListedTag; - for (i = 0; i < sendLength; ++i) { - pn532_packetbuffer[i + 2] = send[i]; - } + pn532_packetbuffer[0] = 0x40; // PN532_COMMAND_INDATAEXCHANGE; + pn532_packetbuffer[1] = _inListedTag; + for (i = 0; i < sendLength; ++i) { + pn532_packetbuffer[i + 2] = send[i]; + } - if (!sendCommandCheckAck(pn532_packetbuffer, sendLength + 2, 1000)) { + if (!sendCommandCheckAck(pn532_packetbuffer, sendLength + 2, 1000)) { #ifdef PN532DEBUG - PN532DEBUGPRINT.println(F("Could not send APDU")); + PN532DEBUGPRINT.println(F("Could not send APDU")); #endif - return false; - } + return false; + } - if (!waitready(1000)) { + if (!waitready(1000)) { #ifdef PN532DEBUG - PN532DEBUGPRINT.println(F("Response never received for APDU...")); + PN532DEBUGPRINT.println(F("Response never received for APDU...")); #endif - return false; - } + return false; + } - readdata(pn532_packetbuffer, sizeof(pn532_packetbuffer)); + readdata(pn532_packetbuffer, sizeof(pn532_packetbuffer)); - if (pn532_packetbuffer[0] == 0 && pn532_packetbuffer[1] == 0 && - pn532_packetbuffer[2] == 0xff) { - uint8_t length = pn532_packetbuffer[3]; - if (pn532_packetbuffer[4] != (uint8_t)(~length + 1)) { + if (pn532_packetbuffer[0] == 0 && pn532_packetbuffer[1] == 0 && + pn532_packetbuffer[2] == 0xff) { + uint8_t length = pn532_packetbuffer[3]; + if (pn532_packetbuffer[4] != (uint8_t)(~length + 1)) { #ifdef PN532DEBUG - PN532DEBUGPRINT.println(F("Length check invalid")); - PN532DEBUGPRINT.println(length, HEX); - PN532DEBUGPRINT.println((~length) + 1, HEX); + PN532DEBUGPRINT.println(F("Length check invalid")); + PN532DEBUGPRINT.println(length, HEX); + PN532DEBUGPRINT.println((~length) + 1, HEX); #endif - return false; - } - if (pn532_packetbuffer[5] == PN532_PN532TOHOST && - pn532_packetbuffer[6] == PN532_RESPONSE_INDATAEXCHANGE) { - if ((pn532_packetbuffer[7] & 0x3f) != 0) { + return false; + } + if (pn532_packetbuffer[5] == PN532_PN532TOHOST && + pn532_packetbuffer[6] == PN532_RESPONSE_INDATAEXCHANGE) { + if ((pn532_packetbuffer[7] & 0x3f) != 0) { #ifdef PN532DEBUG - PN532DEBUGPRINT.println(F("Status code indicates an error")); + PN532DEBUGPRINT.println(F("Status code indicates an error")); #endif - return false; - } - - length -= 3; - - if (length > *responseLength) { - length = *responseLength; // silent truncation... - } - - for (i = 0; i < length; ++i) { - response[i] = pn532_packetbuffer[8 + i]; - } - *responseLength = length; - - return true; + return false; + } + + length -= 3; + + if (length > *responseLength) { + length = *responseLength; // silent truncation... + } + + for (i = 0; i < length; ++i) { + response[i] = pn532_packetbuffer[8 + i]; + } + *responseLength = length; + + return true; + } else { + PN532DEBUGPRINT.print(F("Don't know how to handle this command: ")); + PN532DEBUGPRINT.println(pn532_packetbuffer[6], HEX); + return false; + } } else { - PN532DEBUGPRINT.print(F("Don't know how to handle this command: ")); - PN532DEBUGPRINT.println(pn532_packetbuffer[6], HEX); - return false; + PN532DEBUGPRINT.println(F("Preamble missing")); + return false; } - } else { - PN532DEBUGPRINT.println(F("Preamble missing")); - return false; - } } /**************************************************************************/ @@ -748,68 +749,70 @@ bool Adafruit_PN532::inDataExchange(uint8_t *send, uint8_t sendLength, */ /**************************************************************************/ bool Adafruit_PN532::inListPassiveTarget() { - pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET; - pn532_packetbuffer[1] = 1; - pn532_packetbuffer[2] = 0; + pn532_packetbuffer[0] = PN532_COMMAND_INLISTPASSIVETARGET; + pn532_packetbuffer[1] = 1; + pn532_packetbuffer[2] = 0; #ifdef PN532DEBUG - PN532DEBUGPRINT.print(F("About to inList passive target")); + PN532DEBUGPRINT.print(F("About to inList passive target")); #endif - if (!sendCommandCheckAck(pn532_packetbuffer, 3, 1000)) { + if (!sendCommandCheckAck(pn532_packetbuffer, 3, 1000)) { #ifdef PN532DEBUG - PN532DEBUGPRINT.println(F("Could not send inlist message")); + PN532DEBUGPRINT.println(F("Could not send inlist message")); #endif - return false; - } + return false; + } - if (!waitready(30000)) { - return false; - } + if (!waitready(30000)) { + return false; + } - readdata(pn532_packetbuffer, sizeof(pn532_packetbuffer)); + readdata(pn532_packetbuffer, sizeof(pn532_packetbuffer)); - if (pn532_packetbuffer[0] == 0 && pn532_packetbuffer[1] == 0 && - pn532_packetbuffer[2] == 0xff) { - uint8_t length = pn532_packetbuffer[3]; - if (pn532_packetbuffer[4] != (uint8_t)(~length + 1)) { + if (pn532_packetbuffer[0] == 0 && pn532_packetbuffer[1] == 0 && + pn532_packetbuffer[2] == 0xff) { + uint8_t length = pn532_packetbuffer[3]; + if (pn532_packetbuffer[4] != (uint8_t)(~length + 1)) { #ifdef PN532DEBUG - PN532DEBUGPRINT.println(F("Length check invalid")); - PN532DEBUGPRINT.println(length, HEX); - PN532DEBUGPRINT.println((~length) + 1, HEX); + PN532DEBUGPRINT.println(F("Length check invalid")); + PN532DEBUGPRINT.println(length, HEX); + PN532DEBUGPRINT.println((~length) + 1, HEX); #endif - return false; - } - if (pn532_packetbuffer[5] == PN532_PN532TOHOST && - pn532_packetbuffer[6] == PN532_RESPONSE_INLISTPASSIVETARGET) { - if (pn532_packetbuffer[7] != 1) { + return false; + } + if (pn532_packetbuffer[5] == PN532_PN532TOHOST && + pn532_packetbuffer[6] == PN532_RESPONSE_INLISTPASSIVETARGET) { + if (pn532_packetbuffer[7] != 1) { #ifdef PN532DEBUG - PN532DEBUGPRINT.println(F("Unhandled number of targets inlisted")); + PN532DEBUGPRINT.println( + F("Unhandled number of targets inlisted")); #endif - PN532DEBUGPRINT.println(F("Number of tags inlisted:")); - PN532DEBUGPRINT.println(pn532_packetbuffer[7]); - return false; - } + PN532DEBUGPRINT.println(F("Number of tags inlisted:")); + PN532DEBUGPRINT.println(pn532_packetbuffer[7]); + return false; + } - _inListedTag = pn532_packetbuffer[8]; - PN532DEBUGPRINT.print(F("Tag number: ")); - PN532DEBUGPRINT.println(_inListedTag); + _inListedTag = pn532_packetbuffer[8]; + PN532DEBUGPRINT.print(F("Tag number: ")); + PN532DEBUGPRINT.println(_inListedTag); - return true; - } else { + return true; + } else { #ifdef PN532DEBUG - PN532DEBUGPRINT.print(F("Unexpected response to inlist passive host")); + PN532DEBUGPRINT.print( + F("Unexpected response to inlist passive host")); #endif - return false; - } - } else { + return false; + } + } else { #ifdef PN532DEBUG - PN532DEBUGPRINT.println(F("Preamble missing")); + PN532DEBUGPRINT.println(F("Preamble missing")); #endif - return false; - } + return false; + } - return true; + return true; } /***** Mifare Classic Functions ******/ @@ -823,11 +826,11 @@ bool Adafruit_PN532::inListPassiveTarget() { */ /**************************************************************************/ bool Adafruit_PN532::mifareclassic_IsFirstBlock(uint32_t uiBlock) { - // Test if we are in the small or big sectors - if (uiBlock < 128) - return ((uiBlock) % 4 == 0); - else - return ((uiBlock) % 16 == 0); + // Test if we are in the small or big sectors + if (uiBlock < 128) + return ((uiBlock) % 4 == 0); + else + return ((uiBlock) % 16 == 0); } /**************************************************************************/ @@ -839,11 +842,11 @@ bool Adafruit_PN532::mifareclassic_IsFirstBlock(uint32_t uiBlock) { */ /**************************************************************************/ bool Adafruit_PN532::mifareclassic_IsTrailerBlock(uint32_t uiBlock) { - // Test if we are in the small or big sectors - if (uiBlock < 128) - return ((uiBlock + 1) % 4 == 0); - else - return ((uiBlock + 1) % 16 == 0); + // Test if we are in the small or big sectors + if (uiBlock < 128) + return ((uiBlock + 1) % 4 == 0); + else + return ((uiBlock + 1) % 16 == 0); } /**************************************************************************/ @@ -870,54 +873,54 @@ uint8_t Adafruit_PN532::mifareclassic_AuthenticateBlock(uint8_t *uid, uint32_t blockNumber, uint8_t keyNumber, uint8_t *keyData) { - // uint8_t len; - uint8_t i; + // uint8_t len; + uint8_t i; - // Hang on to the key and uid data - memcpy(_key, keyData, 6); - memcpy(_uid, uid, uidLen); - _uidLen = uidLen; + // Hang on to the key and uid data + memcpy(_key, keyData, 6); + memcpy(_uid, uid, uidLen); + _uidLen = uidLen; #ifdef MIFAREDEBUG - PN532DEBUGPRINT.print(F("Trying to authenticate card ")); - Adafruit_PN532::PrintHex(_uid, _uidLen); - PN532DEBUGPRINT.print(F("Using authentication KEY ")); - PN532DEBUGPRINT.print(keyNumber ? 'B' : 'A'); - PN532DEBUGPRINT.print(F(": ")); - Adafruit_PN532::PrintHex(_key, 6); + PN532DEBUGPRINT.print(F("Trying to authenticate card ")); + Adafruit_PN532::PrintHex(_uid, _uidLen); + PN532DEBUGPRINT.print(F("Using authentication KEY ")); + PN532DEBUGPRINT.print(keyNumber ? 'B' : 'A'); + PN532DEBUGPRINT.print(F(": ")); + Adafruit_PN532::PrintHex(_key, 6); #endif - // Prepare the authentication command // - pn532_packetbuffer[0] = - PN532_COMMAND_INDATAEXCHANGE; /* Data Exchange Header */ - pn532_packetbuffer[1] = 1; /* Max card numbers */ - pn532_packetbuffer[2] = (keyNumber) ? MIFARE_CMD_AUTH_B : MIFARE_CMD_AUTH_A; - pn532_packetbuffer[3] = - blockNumber; /* Block Number (1K = 0..63, 4K = 0..255 */ - memcpy(pn532_packetbuffer + 4, _key, 6); - for (i = 0; i < _uidLen; i++) { - pn532_packetbuffer[10 + i] = _uid[i]; /* 4 byte card ID */ - } - - if (!sendCommandCheckAck(pn532_packetbuffer, 10 + _uidLen)) - return 0; - - // Read the response packet - readdata(pn532_packetbuffer, 12); - - // check if the response is valid and we are authenticated??? - // for an auth success it should be bytes 5-7: 0xD5 0x41 0x00 - // Mifare auth error is technically byte 7: 0x14 but anything other and 0x00 - // is not good - if (pn532_packetbuffer[7] != 0x00) { + // Prepare the authentication command // + pn532_packetbuffer[0] = + PN532_COMMAND_INDATAEXCHANGE; /* Data Exchange Header */ + pn532_packetbuffer[1] = 1; /* Max card numbers */ + pn532_packetbuffer[2] = (keyNumber) ? MIFARE_CMD_AUTH_B : MIFARE_CMD_AUTH_A; + pn532_packetbuffer[3] = + blockNumber; /* Block Number (1K = 0..63, 4K = 0..255 */ + memcpy(pn532_packetbuffer + 4, _key, 6); + for (i = 0; i < _uidLen; i++) { + pn532_packetbuffer[10 + i] = _uid[i]; /* 4 byte card ID */ + } + + if (!sendCommandCheckAck(pn532_packetbuffer, 10 + _uidLen)) + return 0; + + // Read the response packet + readdata(pn532_packetbuffer, 12); + + // check if the response is valid and we are authenticated??? + // for an auth success it should be bytes 5-7: 0xD5 0x41 0x00 + // Mifare auth error is technically byte 7: 0x14 but anything other and 0x00 + // is not good + if (pn532_packetbuffer[7] != 0x00) { #ifdef PN532DEBUG - PN532DEBUGPRINT.print(F("Authentification failed: ")); - Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 12); + PN532DEBUGPRINT.print(F("Authentification failed: ")); + Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 12); #endif - return 0; - } + return 0; + } - return 1; + return 1; } /**************************************************************************/ @@ -936,49 +939,49 @@ uint8_t Adafruit_PN532::mifareclassic_AuthenticateBlock(uint8_t *uid, uint8_t Adafruit_PN532::mifareclassic_ReadDataBlock(uint8_t blockNumber, uint8_t *data) { #ifdef MIFAREDEBUG - PN532DEBUGPRINT.print(F("Trying to read 16 bytes from block ")); - PN532DEBUGPRINT.println(blockNumber); + PN532DEBUGPRINT.print(F("Trying to read 16 bytes from block ")); + PN532DEBUGPRINT.println(blockNumber); #endif - /* Prepare the command */ - pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; - pn532_packetbuffer[1] = 1; /* Card number */ - pn532_packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */ - pn532_packetbuffer[3] = - blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */ + /* Prepare the command */ + pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; + pn532_packetbuffer[1] = 1; /* Card number */ + pn532_packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */ + pn532_packetbuffer[3] = + blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */ - /* Send the command */ - if (!sendCommandCheckAck(pn532_packetbuffer, 4)) { + /* Send the command */ + if (!sendCommandCheckAck(pn532_packetbuffer, 4)) { #ifdef MIFAREDEBUG - PN532DEBUGPRINT.println(F("Failed to receive ACK for read command")); + PN532DEBUGPRINT.println(F("Failed to receive ACK for read command")); #endif - return 0; - } + return 0; + } - /* Read the response packet */ - readdata(pn532_packetbuffer, 26); + /* Read the response packet */ + readdata(pn532_packetbuffer, 26); - /* If byte 8 isn't 0x00 we probably have an error */ - if (pn532_packetbuffer[7] != 0x00) { + /* If byte 8 isn't 0x00 we probably have an error */ + if (pn532_packetbuffer[7] != 0x00) { #ifdef MIFAREDEBUG - PN532DEBUGPRINT.println(F("Unexpected response")); - Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); + PN532DEBUGPRINT.println(F("Unexpected response")); + Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); #endif - return 0; - } + return 0; + } - /* Copy the 16 data bytes to the output buffer */ - /* Block content starts at byte 9 of a valid response */ - memcpy(data, pn532_packetbuffer + 8, 16); + /* Copy the 16 data bytes to the output buffer */ + /* Block content starts at byte 9 of a valid response */ + memcpy(data, pn532_packetbuffer + 8, 16); /* Display data for debug if requested */ #ifdef MIFAREDEBUG - PN532DEBUGPRINT.print(F("Block ")); - PN532DEBUGPRINT.println(blockNumber); - Adafruit_PN532::PrintHexChar(data, 16); + PN532DEBUGPRINT.print(F("Block ")); + PN532DEBUGPRINT.println(blockNumber); + Adafruit_PN532::PrintHexChar(data, 16); #endif - return 1; + return 1; } /**************************************************************************/ @@ -996,31 +999,31 @@ uint8_t Adafruit_PN532::mifareclassic_ReadDataBlock(uint8_t blockNumber, uint8_t Adafruit_PN532::mifareclassic_WriteDataBlock(uint8_t blockNumber, uint8_t *data) { #ifdef MIFAREDEBUG - PN532DEBUGPRINT.print(F("Trying to write 16 bytes to block ")); - PN532DEBUGPRINT.println(blockNumber); + PN532DEBUGPRINT.print(F("Trying to write 16 bytes to block ")); + PN532DEBUGPRINT.println(blockNumber); #endif - /* Prepare the first command */ - pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; - pn532_packetbuffer[1] = 1; /* Card number */ - pn532_packetbuffer[2] = MIFARE_CMD_WRITE; /* Mifare Write command = 0xA0 */ - pn532_packetbuffer[3] = - blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */ - memcpy(pn532_packetbuffer + 4, data, 16); /* Data Payload */ + /* Prepare the first command */ + pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; + pn532_packetbuffer[1] = 1; /* Card number */ + pn532_packetbuffer[2] = MIFARE_CMD_WRITE; /* Mifare Write command = 0xA0 */ + pn532_packetbuffer[3] = + blockNumber; /* Block Number (0..63 for 1K, 0..255 for 4K) */ + memcpy(pn532_packetbuffer + 4, data, 16); /* Data Payload */ - /* Send the command */ - if (!sendCommandCheckAck(pn532_packetbuffer, 20)) { + /* Send the command */ + if (!sendCommandCheckAck(pn532_packetbuffer, 20)) { #ifdef MIFAREDEBUG - PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); + PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); #endif - return 0; - } - delay(10); + return 0; + } + delay(10); - /* Read the response packet */ - readdata(pn532_packetbuffer, 26); + /* Read the response packet */ + readdata(pn532_packetbuffer, 26); - return 1; + return 1; } /**************************************************************************/ @@ -1031,27 +1034,30 @@ uint8_t Adafruit_PN532::mifareclassic_WriteDataBlock(uint8_t blockNumber, */ /**************************************************************************/ uint8_t Adafruit_PN532::mifareclassic_FormatNDEF(void) { - uint8_t sectorbuffer1[16] = {0x14, 0x01, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, - 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1}; - uint8_t sectorbuffer2[16] = {0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, - 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1}; - uint8_t sectorbuffer3[16] = {0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x78, 0x77, - 0x88, 0xC1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; - - // Note 0xA0 0xA1 0xA2 0xA3 0xA4 0xA5 must be used for key A - // for the MAD sector in NDEF records (sector 0) - - // Write block 1 and 2 to the card - if (!(mifareclassic_WriteDataBlock(1, sectorbuffer1))) - return 0; - if (!(mifareclassic_WriteDataBlock(2, sectorbuffer2))) - return 0; - // Write key A and access rights card - if (!(mifareclassic_WriteDataBlock(3, sectorbuffer3))) - return 0; - - // Seems that everything was OK (?!) - return 1; + uint8_t sectorbuffer1[16] = {0x14, 0x01, 0x03, 0xE1, 0x03, 0xE1, + 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, + 0x03, 0xE1, 0x03, 0xE1}; + uint8_t sectorbuffer2[16] = {0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, + 0x03, 0xE1, 0x03, 0xE1, 0x03, 0xE1, + 0x03, 0xE1, 0x03, 0xE1}; + uint8_t sectorbuffer3[16] = {0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, + 0x78, 0x77, 0x88, 0xC1, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF}; + + // Note 0xA0 0xA1 0xA2 0xA3 0xA4 0xA5 must be used for key A + // for the MAD sector in NDEF records (sector 0) + + // Write block 1 and 2 to the card + if (!(mifareclassic_WriteDataBlock(1, sectorbuffer1))) + return 0; + if (!(mifareclassic_WriteDataBlock(2, sectorbuffer2))) + return 0; + // Write key A and access rights card + if (!(mifareclassic_WriteDataBlock(3, sectorbuffer3))) + return 0; + + // Seems that everything was OK (?!) + return 1; } /**************************************************************************/ @@ -1075,81 +1081,84 @@ uint8_t Adafruit_PN532::mifareclassic_FormatNDEF(void) { uint8_t Adafruit_PN532::mifareclassic_WriteNDEFURI(uint8_t sectorNumber, uint8_t uriIdentifier, const char *url) { - // Figure out how long the string is - uint8_t len = strlen(url); - - // Make sure we're within a 1K limit for the sector number - if ((sectorNumber < 1) || (sectorNumber > 15)) - return 0; - - // Make sure the URI payload is between 1 and 38 chars - if ((len < 1) || (len > 38)) - return 0; - - // Note 0xD3 0xF7 0xD3 0xF7 0xD3 0xF7 must be used for key A - // in NDEF records - - // Setup the sector buffer (w/pre-formatted TLV wrapper and NDEF message) - uint8_t sectorbuffer1[16] = {0x00, - 0x00, - 0x03, - (uint8_t)(len + 5), - 0xD1, - 0x01, - (uint8_t)(len + 1), - 0x55, - uriIdentifier, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00}; - uint8_t sectorbuffer2[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - uint8_t sectorbuffer3[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - uint8_t sectorbuffer4[16] = {0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7, 0x7F, 0x07, - 0x88, 0x40, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; - if (len <= 6) { - // Unlikely we'll get a url this short, but why not ... - memcpy(sectorbuffer1 + 9, url, len); - sectorbuffer1[len + 9] = 0xFE; - } else if (len == 7) { - // 0xFE needs to be wrapped around to next block - memcpy(sectorbuffer1 + 9, url, len); - sectorbuffer2[0] = 0xFE; - } else if ((len > 7) && (len <= 22)) { - // Url fits in two blocks - memcpy(sectorbuffer1 + 9, url, 7); - memcpy(sectorbuffer2, url + 7, len - 7); - sectorbuffer2[len - 7] = 0xFE; - } else if (len == 23) { - // 0xFE needs to be wrapped around to final block - memcpy(sectorbuffer1 + 9, url, 7); - memcpy(sectorbuffer2, url + 7, len - 7); - sectorbuffer3[0] = 0xFE; - } else { - // Url fits in three blocks - memcpy(sectorbuffer1 + 9, url, 7); - memcpy(sectorbuffer2, url + 7, 16); - memcpy(sectorbuffer3, url + 23, len - 24); - sectorbuffer3[len - 22] = 0xFE; - } - - // Now write all three blocks back to the card - if (!(mifareclassic_WriteDataBlock(sectorNumber * 4, sectorbuffer1))) - return 0; - if (!(mifareclassic_WriteDataBlock((sectorNumber * 4) + 1, sectorbuffer2))) - return 0; - if (!(mifareclassic_WriteDataBlock((sectorNumber * 4) + 2, sectorbuffer3))) - return 0; - if (!(mifareclassic_WriteDataBlock((sectorNumber * 4) + 3, sectorbuffer4))) - return 0; - - // Seems that everything was OK (?!) - return 1; + // Figure out how long the string is + uint8_t len = strlen(url); + + // Make sure we're within a 1K limit for the sector number + if ((sectorNumber < 1) || (sectorNumber > 15)) + return 0; + + // Make sure the URI payload is between 1 and 38 chars + if ((len < 1) || (len > 38)) + return 0; + + // Note 0xD3 0xF7 0xD3 0xF7 0xD3 0xF7 must be used for key A + // in NDEF records + + // Setup the sector buffer (w/pre-formatted TLV wrapper and NDEF message) + uint8_t sectorbuffer1[16] = {0x00, + 0x00, + 0x03, + (uint8_t)(len + 5), + 0xD1, + 0x01, + (uint8_t)(len + 1), + 0x55, + uriIdentifier, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00}; + uint8_t sectorbuffer2[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00}; + uint8_t sectorbuffer3[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00}; + uint8_t sectorbuffer4[16] = {0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7, + 0x7F, 0x07, 0x88, 0x40, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF}; + if (len <= 6) { + // Unlikely we'll get a url this short, but why not ... + memcpy(sectorbuffer1 + 9, url, len); + sectorbuffer1[len + 9] = 0xFE; + } else if (len == 7) { + // 0xFE needs to be wrapped around to next block + memcpy(sectorbuffer1 + 9, url, len); + sectorbuffer2[0] = 0xFE; + } else if ((len > 7) && (len <= 22)) { + // Url fits in two blocks + memcpy(sectorbuffer1 + 9, url, 7); + memcpy(sectorbuffer2, url + 7, len - 7); + sectorbuffer2[len - 7] = 0xFE; + } else if (len == 23) { + // 0xFE needs to be wrapped around to final block + memcpy(sectorbuffer1 + 9, url, 7); + memcpy(sectorbuffer2, url + 7, len - 7); + sectorbuffer3[0] = 0xFE; + } else { + // Url fits in three blocks + memcpy(sectorbuffer1 + 9, url, 7); + memcpy(sectorbuffer2, url + 7, 16); + memcpy(sectorbuffer3, url + 23, len - 24); + sectorbuffer3[len - 22] = 0xFE; + } + + // Now write all three blocks back to the card + if (!(mifareclassic_WriteDataBlock(sectorNumber * 4, sectorbuffer1))) + return 0; + if (!(mifareclassic_WriteDataBlock((sectorNumber * 4) + 1, sectorbuffer2))) + return 0; + if (!(mifareclassic_WriteDataBlock((sectorNumber * 4) + 2, sectorbuffer3))) + return 0; + if (!(mifareclassic_WriteDataBlock((sectorNumber * 4) + 3, sectorbuffer4))) + return 0; + + // Seems that everything was OK (?!) + return 1; } /***** Mifare Ultralight Functions ******/ @@ -1166,65 +1175,65 @@ uint8_t Adafruit_PN532::mifareclassic_WriteNDEFURI(uint8_t sectorNumber, /**************************************************************************/ uint8_t Adafruit_PN532::mifareultralight_ReadPage(uint8_t page, uint8_t *buffer) { - if (page >= 64) { + if (page >= 64) { #ifdef MIFAREDEBUG - PN532DEBUGPRINT.println(F("Page value out of range")); + PN532DEBUGPRINT.println(F("Page value out of range")); #endif - return 0; - } + return 0; + } #ifdef MIFAREDEBUG - PN532DEBUGPRINT.print(F("Reading page ")); - PN532DEBUGPRINT.println(page); + PN532DEBUGPRINT.print(F("Reading page ")); + PN532DEBUGPRINT.println(page); #endif - /* Prepare the command */ - pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; - pn532_packetbuffer[1] = 1; /* Card number */ - pn532_packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */ - pn532_packetbuffer[3] = page; /* Page Number (0..63 in most cases) */ + /* Prepare the command */ + pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; + pn532_packetbuffer[1] = 1; /* Card number */ + pn532_packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */ + pn532_packetbuffer[3] = page; /* Page Number (0..63 in most cases) */ - /* Send the command */ - if (!sendCommandCheckAck(pn532_packetbuffer, 4)) { + /* Send the command */ + if (!sendCommandCheckAck(pn532_packetbuffer, 4)) { #ifdef MIFAREDEBUG - PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); + PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); #endif - return 0; - } + return 0; + } - /* Read the response packet */ - readdata(pn532_packetbuffer, 26); + /* Read the response packet */ + readdata(pn532_packetbuffer, 26); #ifdef MIFAREDEBUG - PN532DEBUGPRINT.println(F("Received: ")); - Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); + PN532DEBUGPRINT.println(F("Received: ")); + Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); #endif - /* If byte 8 isn't 0x00 we probably have an error */ - if (pn532_packetbuffer[7] == 0x00) { - /* Copy the 4 data bytes to the output buffer */ - /* Block content starts at byte 9 of a valid response */ - /* Note that the command actually reads 16 byte or 4 */ - /* pages at a time ... we simply discard the last 12 */ - /* bytes */ - memcpy(buffer, pn532_packetbuffer + 8, 4); - } else { + /* If byte 8 isn't 0x00 we probably have an error */ + if (pn532_packetbuffer[7] == 0x00) { + /* Copy the 4 data bytes to the output buffer */ + /* Block content starts at byte 9 of a valid response */ + /* Note that the command actually reads 16 byte or 4 */ + /* pages at a time ... we simply discard the last 12 */ + /* bytes */ + memcpy(buffer, pn532_packetbuffer + 8, 4); + } else { #ifdef MIFAREDEBUG - PN532DEBUGPRINT.println(F("Unexpected response reading block: ")); - Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); + PN532DEBUGPRINT.println(F("Unexpected response reading block: ")); + Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); #endif - return 0; - } + return 0; + } /* Display data for debug if requested */ #ifdef MIFAREDEBUG - PN532DEBUGPRINT.print(F("Page ")); - PN532DEBUGPRINT.print(page); - PN532DEBUGPRINT.println(F(":")); - Adafruit_PN532::PrintHexChar(buffer, 4); + PN532DEBUGPRINT.print(F("Page ")); + PN532DEBUGPRINT.print(page); + PN532DEBUGPRINT.println(F(":")); + Adafruit_PN532::PrintHexChar(buffer, 4); #endif - // Return OK signal - return 1; + // Return OK signal + return 1; } /**************************************************************************/ @@ -1242,43 +1251,44 @@ uint8_t Adafruit_PN532::mifareultralight_ReadPage(uint8_t page, uint8_t Adafruit_PN532::mifareultralight_WritePage(uint8_t page, uint8_t *data) { - if (page >= 64) { + if (page >= 64) { #ifdef MIFAREDEBUG - PN532DEBUGPRINT.println(F("Page value out of range")); + PN532DEBUGPRINT.println(F("Page value out of range")); #endif - // Return Failed Signal - return 0; - } + // Return Failed Signal + return 0; + } #ifdef MIFAREDEBUG - PN532DEBUGPRINT.print(F("Trying to write 4 byte page")); - PN532DEBUGPRINT.println(page); + PN532DEBUGPRINT.print(F("Trying to write 4 byte page")); + PN532DEBUGPRINT.println(page); #endif - /* Prepare the first command */ - pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; - pn532_packetbuffer[1] = 1; /* Card number */ - pn532_packetbuffer[2] = - MIFARE_ULTRALIGHT_CMD_WRITE; /* Mifare Ultralight Write command = 0xA2 */ - pn532_packetbuffer[3] = page; /* Page Number (0..63 for most cases) */ - memcpy(pn532_packetbuffer + 4, data, 4); /* Data Payload */ - - /* Send the command */ - if (!sendCommandCheckAck(pn532_packetbuffer, 8)) { + /* Prepare the first command */ + pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; + pn532_packetbuffer[1] = 1; /* Card number */ + pn532_packetbuffer[2] = + MIFARE_ULTRALIGHT_CMD_WRITE; /* Mifare Ultralight Write command = 0xA2 + */ + pn532_packetbuffer[3] = page; /* Page Number (0..63 for most cases) */ + memcpy(pn532_packetbuffer + 4, data, 4); /* Data Payload */ + + /* Send the command */ + if (!sendCommandCheckAck(pn532_packetbuffer, 8)) { #ifdef MIFAREDEBUG - PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); + PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); #endif - // Return Failed Signal - return 0; - } - delay(10); + // Return Failed Signal + return 0; + } + delay(10); - /* Read the response packet */ - readdata(pn532_packetbuffer, 26); + /* Read the response packet */ + readdata(pn532_packetbuffer, 26); - // Return OK Signal - return 1; + // Return OK Signal + return 1; } /***** NTAG2xx Functions ******/ @@ -1294,72 +1304,72 @@ uint8_t Adafruit_PN532::mifareultralight_WritePage(uint8_t page, */ /**************************************************************************/ uint8_t Adafruit_PN532::ntag2xx_ReadPage(uint8_t page, uint8_t *buffer) { - // TAG Type PAGES USER START USER STOP - // -------- ----- ---------- --------- - // NTAG 203 42 4 39 - // NTAG 213 45 4 39 - // NTAG 215 135 4 129 - // NTAG 216 231 4 225 - - if (page >= 231) { + // TAG Type PAGES USER START USER STOP + // -------- ----- ---------- --------- + // NTAG 203 42 4 39 + // NTAG 213 45 4 39 + // NTAG 215 135 4 129 + // NTAG 216 231 4 225 + + if (page >= 231) { #ifdef MIFAREDEBUG - PN532DEBUGPRINT.println(F("Page value out of range")); + PN532DEBUGPRINT.println(F("Page value out of range")); #endif - return 0; - } + return 0; + } #ifdef MIFAREDEBUG - PN532DEBUGPRINT.print(F("Reading page ")); - PN532DEBUGPRINT.println(page); + PN532DEBUGPRINT.print(F("Reading page ")); + PN532DEBUGPRINT.println(page); #endif - /* Prepare the command */ - pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; - pn532_packetbuffer[1] = 1; /* Card number */ - pn532_packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */ - pn532_packetbuffer[3] = page; /* Page Number (0..63 in most cases) */ + /* Prepare the command */ + pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; + pn532_packetbuffer[1] = 1; /* Card number */ + pn532_packetbuffer[2] = MIFARE_CMD_READ; /* Mifare Read command = 0x30 */ + pn532_packetbuffer[3] = page; /* Page Number (0..63 in most cases) */ - /* Send the command */ - if (!sendCommandCheckAck(pn532_packetbuffer, 4)) { + /* Send the command */ + if (!sendCommandCheckAck(pn532_packetbuffer, 4)) { #ifdef MIFAREDEBUG - PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); + PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); #endif - return 0; - } + return 0; + } - /* Read the response packet */ - readdata(pn532_packetbuffer, 26); + /* Read the response packet */ + readdata(pn532_packetbuffer, 26); #ifdef MIFAREDEBUG - PN532DEBUGPRINT.println(F("Received: ")); - Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); + PN532DEBUGPRINT.println(F("Received: ")); + Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); #endif - /* If byte 8 isn't 0x00 we probably have an error */ - if (pn532_packetbuffer[7] == 0x00) { - /* Copy the 4 data bytes to the output buffer */ - /* Block content starts at byte 9 of a valid response */ - /* Note that the command actually reads 16 byte or 4 */ - /* pages at a time ... we simply discard the last 12 */ - /* bytes */ - memcpy(buffer, pn532_packetbuffer + 8, 4); - } else { + /* If byte 8 isn't 0x00 we probably have an error */ + if (pn532_packetbuffer[7] == 0x00) { + /* Copy the 4 data bytes to the output buffer */ + /* Block content starts at byte 9 of a valid response */ + /* Note that the command actually reads 16 byte or 4 */ + /* pages at a time ... we simply discard the last 12 */ + /* bytes */ + memcpy(buffer, pn532_packetbuffer + 8, 4); + } else { #ifdef MIFAREDEBUG - PN532DEBUGPRINT.println(F("Unexpected response reading block: ")); - Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); + PN532DEBUGPRINT.println(F("Unexpected response reading block: ")); + Adafruit_PN532::PrintHexChar(pn532_packetbuffer, 26); #endif - return 0; - } + return 0; + } /* Display data for debug if requested */ #ifdef MIFAREDEBUG - PN532DEBUGPRINT.print(F("Page ")); - PN532DEBUGPRINT.print(page); - PN532DEBUGPRINT.println(F(":")); - Adafruit_PN532::PrintHexChar(buffer, 4); + PN532DEBUGPRINT.print(F("Page ")); + PN532DEBUGPRINT.print(page); + PN532DEBUGPRINT.println(F(":")); + Adafruit_PN532::PrintHexChar(buffer, 4); #endif - // Return OK signal - return 1; + // Return OK signal + return 1; } /**************************************************************************/ @@ -1375,50 +1385,51 @@ uint8_t Adafruit_PN532::ntag2xx_ReadPage(uint8_t page, uint8_t *buffer) { */ /**************************************************************************/ uint8_t Adafruit_PN532::ntag2xx_WritePage(uint8_t page, uint8_t *data) { - // TAG Type PAGES USER START USER STOP - // -------- ----- ---------- --------- - // NTAG 203 42 4 39 - // NTAG 213 45 4 39 - // NTAG 215 135 4 129 - // NTAG 216 231 4 225 - - if ((page < 4) || (page > 225)) { + // TAG Type PAGES USER START USER STOP + // -------- ----- ---------- --------- + // NTAG 203 42 4 39 + // NTAG 213 45 4 39 + // NTAG 215 135 4 129 + // NTAG 216 231 4 225 + + if ((page < 4) || (page > 225)) { #ifdef MIFAREDEBUG - PN532DEBUGPRINT.println(F("Page value out of range")); + PN532DEBUGPRINT.println(F("Page value out of range")); #endif - // Return Failed Signal - return 0; - } + // Return Failed Signal + return 0; + } #ifdef MIFAREDEBUG - PN532DEBUGPRINT.print(F("Trying to write 4 byte page")); - PN532DEBUGPRINT.println(page); + PN532DEBUGPRINT.print(F("Trying to write 4 byte page")); + PN532DEBUGPRINT.println(page); #endif - /* Prepare the first command */ - pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; - pn532_packetbuffer[1] = 1; /* Card number */ - pn532_packetbuffer[2] = - MIFARE_ULTRALIGHT_CMD_WRITE; /* Mifare Ultralight Write command = 0xA2 */ - pn532_packetbuffer[3] = page; /* Page Number (0..63 for most cases) */ - memcpy(pn532_packetbuffer + 4, data, 4); /* Data Payload */ - - /* Send the command */ - if (!sendCommandCheckAck(pn532_packetbuffer, 8)) { + /* Prepare the first command */ + pn532_packetbuffer[0] = PN532_COMMAND_INDATAEXCHANGE; + pn532_packetbuffer[1] = 1; /* Card number */ + pn532_packetbuffer[2] = + MIFARE_ULTRALIGHT_CMD_WRITE; /* Mifare Ultralight Write command = 0xA2 + */ + pn532_packetbuffer[3] = page; /* Page Number (0..63 for most cases) */ + memcpy(pn532_packetbuffer + 4, data, 4); /* Data Payload */ + + /* Send the command */ + if (!sendCommandCheckAck(pn532_packetbuffer, 8)) { #ifdef MIFAREDEBUG - PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); + PN532DEBUGPRINT.println(F("Failed to receive ACK for write command")); #endif - // Return Failed Signal - return 0; - } - delay(10); + // Return Failed Signal + return 0; + } + delay(10); - /* Read the response packet */ - readdata(pn532_packetbuffer, 26); + /* Read the response packet */ + readdata(pn532_packetbuffer, 26); - // Return OK Signal - return 1; + // Return OK Signal + return 1; } /**************************************************************************/ @@ -1438,86 +1449,87 @@ uint8_t Adafruit_PN532::ntag2xx_WritePage(uint8_t page, uint8_t *data) { /**************************************************************************/ uint8_t Adafruit_PN532::ntag2xx_WriteNDEFURI(uint8_t uriIdentifier, char *url, uint8_t dataLen) { - uint8_t pageBuffer[4] = {0, 0, 0, 0}; - - // Remove NDEF record overhead from the URI data (pageHeader below) - uint8_t wrapperSize = 12; - - // Figure out how long the string is - uint8_t len = strlen(url); - - // Make sure the URI payload will fit in dataLen (include 0xFE trailer) - if ((len < 1) || (len + 1 > (dataLen - wrapperSize))) - return 0; - - // Setup the record header - // See NFCForum-TS-Type-2-Tag_1.1.pdf for details - uint8_t pageHeader[12] = { - /* NDEF Lock Control TLV (must be first and always present) */ - 0x01, /* Tag Field (0x01 = Lock Control TLV) */ - 0x03, /* Payload Length (always 3) */ - 0xA0, /* The position inside the tag of the lock bytes (upper 4 = page - address, lower 4 = byte offset) */ - 0x10, /* Size in bits of the lock area */ - 0x44, /* Size in bytes of a page and the number of bytes each lock bit can - lock (4 bit + 4 bits) */ - /* NDEF Message TLV - URI Record */ - 0x03, /* Tag Field (0x03 = NDEF Message) */ - (uint8_t)(len + 5), /* Payload Length (not including 0xFE trailer) */ - 0xD1, /* NDEF Record Header (TNF=0x1:Well known record + SR + ME + MB) */ - 0x01, /* Type Length for the record type indicator */ - (uint8_t)(len + 1), /* Payload len */ - 0x55, /* Record Type Indicator (0x55 or 'U' = URI Record) */ - uriIdentifier /* URI Prefix (ex. 0x01 = "http://www.") */ - }; - - // Write 12 byte header (three pages of data starting at page 4) - memcpy(pageBuffer, pageHeader, 4); - if (!(ntag2xx_WritePage(4, pageBuffer))) - return 0; - memcpy(pageBuffer, pageHeader + 4, 4); - if (!(ntag2xx_WritePage(5, pageBuffer))) - return 0; - memcpy(pageBuffer, pageHeader + 8, 4); - if (!(ntag2xx_WritePage(6, pageBuffer))) - return 0; - - // Write URI (starting at page 7) - uint8_t currentPage = 7; - char *urlcopy = url; - while (len) { - if (len < 4) { - memset(pageBuffer, 0, 4); - memcpy(pageBuffer, urlcopy, len); - pageBuffer[len] = 0xFE; // NDEF record footer - if (!(ntag2xx_WritePage(currentPage, pageBuffer))) + uint8_t pageBuffer[4] = {0, 0, 0, 0}; + + // Remove NDEF record overhead from the URI data (pageHeader below) + uint8_t wrapperSize = 12; + + // Figure out how long the string is + uint8_t len = strlen(url); + + // Make sure the URI payload will fit in dataLen (include 0xFE trailer) + if ((len < 1) || (len + 1 > (dataLen - wrapperSize))) return 0; - // DONE! - return 1; - } else if (len == 4) { - memcpy(pageBuffer, urlcopy, len); - if (!(ntag2xx_WritePage(currentPage, pageBuffer))) + + // Setup the record header + // See NFCForum-TS-Type-2-Tag_1.1.pdf for details + uint8_t pageHeader[12] = { + /* NDEF Lock Control TLV (must be first and always present) */ + 0x01, /* Tag Field (0x01 = Lock Control TLV) */ + 0x03, /* Payload Length (always 3) */ + 0xA0, /* The position inside the tag of the lock bytes (upper 4 = page + address, lower 4 = byte offset) */ + 0x10, /* Size in bits of the lock area */ + 0x44, /* Size in bytes of a page and the number of bytes each lock bit + can lock (4 bit + 4 bits) */ + /* NDEF Message TLV - URI Record */ + 0x03, /* Tag Field (0x03 = NDEF Message) */ + (uint8_t)(len + 5), /* Payload Length (not including 0xFE trailer) */ + 0xD1, /* NDEF Record Header (TNF=0x1:Well known record + SR + ME + MB) + */ + 0x01, /* Type Length for the record type indicator */ + (uint8_t)(len + 1), /* Payload len */ + 0x55, /* Record Type Indicator (0x55 or 'U' = URI Record) */ + uriIdentifier /* URI Prefix (ex. 0x01 = "http://www.") */ + }; + + // Write 12 byte header (three pages of data starting at page 4) + memcpy(pageBuffer, pageHeader, 4); + if (!(ntag2xx_WritePage(4, pageBuffer))) return 0; - memset(pageBuffer, 0, 4); - pageBuffer[0] = 0xFE; // NDEF record footer - currentPage++; - if (!(ntag2xx_WritePage(currentPage, pageBuffer))) + memcpy(pageBuffer, pageHeader + 4, 4); + if (!(ntag2xx_WritePage(5, pageBuffer))) return 0; - // DONE! - return 1; - } else { - // More than one page of data left - memcpy(pageBuffer, urlcopy, 4); - if (!(ntag2xx_WritePage(currentPage, pageBuffer))) + memcpy(pageBuffer, pageHeader + 8, 4); + if (!(ntag2xx_WritePage(6, pageBuffer))) return 0; - currentPage++; - urlcopy += 4; - len -= 4; + + // Write URI (starting at page 7) + uint8_t currentPage = 7; + char *urlcopy = url; + while (len) { + if (len < 4) { + memset(pageBuffer, 0, 4); + memcpy(pageBuffer, urlcopy, len); + pageBuffer[len] = 0xFE; // NDEF record footer + if (!(ntag2xx_WritePage(currentPage, pageBuffer))) + return 0; + // DONE! + return 1; + } else if (len == 4) { + memcpy(pageBuffer, urlcopy, len); + if (!(ntag2xx_WritePage(currentPage, pageBuffer))) + return 0; + memset(pageBuffer, 0, 4); + pageBuffer[0] = 0xFE; // NDEF record footer + currentPage++; + if (!(ntag2xx_WritePage(currentPage, pageBuffer))) + return 0; + // DONE! + return 1; + } else { + // More than one page of data left + memcpy(pageBuffer, urlcopy, 4); + if (!(ntag2xx_WritePage(currentPage, pageBuffer))) + return 0; + currentPage++; + urlcopy += 4; + len -= 4; + } } - } - // Seems that everything was OK (?!) - return 1; + // Seems that everything was OK (?!) + return 1; } /************** high level communication functions (handles both I2C and SPI) */ @@ -1528,16 +1540,16 @@ uint8_t Adafruit_PN532::ntag2xx_WriteNDEFURI(uint8_t uriIdentifier, char *url, */ /**************************************************************************/ bool Adafruit_PN532::readack() { - uint8_t ackbuff[6]; + uint8_t ackbuff[6]; - if (spi_dev) { - uint8_t cmd = PN532_SPI_DATAREAD; - spi_dev->write_then_read(&cmd, 1, ackbuff, 6); - } else if (i2c_dev || ser_dev) { - readdata(ackbuff, 6); - } + if (spi_dev) { + uint8_t cmd = PN532_SPI_DATAREAD; + spi_dev->write_then_read(&cmd, 1, ackbuff, 6); + } else if (i2c_dev || ser_dev) { + readdata(ackbuff, 6); + } - return (0 == memcmp((char *)ackbuff, (char *)pn532ack, 6)); + return (0 == memcmp((char *)ackbuff, (char *)pn532ack, 6)); } /**************************************************************************/ @@ -1546,25 +1558,25 @@ bool Adafruit_PN532::readack() { */ /**************************************************************************/ bool Adafruit_PN532::isready() { - if (spi_dev) { - // SPI ready check via Status Request - uint8_t cmd = PN532_SPI_STATREAD; - uint8_t reply; - spi_dev->write_then_read(&cmd, 1, &reply, 1); - return reply == PN532_SPI_READY; - } else if (i2c_dev) { - // I2C ready check via reading RDY byte - uint8_t rdy[1]; - i2c_dev->read(rdy, 1); - return rdy[0] == PN532_I2C_READY; - } else if (ser_dev) { - // Serial ready check based on non-zero read buffer - return (ser_dev->available() != 0); - } else if (_irq != -1) { - uint8_t x = digitalRead(_irq); - return x == 0; - } - return false; + if (spi_dev) { + // SPI ready check via Status Request + uint8_t cmd = PN532_SPI_STATREAD; + uint8_t reply; + spi_dev->write_then_read(&cmd, 1, &reply, 1); + return reply == PN532_SPI_READY; + } else if (i2c_dev) { + // I2C ready check via reading RDY byte + uint8_t rdy[1]; + i2c_dev->read(rdy, 1); + return rdy[0] == PN532_I2C_READY; + } else if (ser_dev) { + // Serial ready check based on non-zero read buffer + return (ser_dev->available() != 0); + } else if (_irq != -1) { + uint8_t x = digitalRead(_irq); + return x == 0; + } + return false; } /**************************************************************************/ @@ -1575,20 +1587,20 @@ bool Adafruit_PN532::isready() { */ /**************************************************************************/ bool Adafruit_PN532::waitready(uint16_t timeout) { - uint16_t timer = 0; - while (!isready()) { - if (timeout != 0) { - timer += 10; - if (timer > timeout) { + uint16_t timer = 0; + while (!isready()) { + if (timeout != 0) { + timer += 10; + if (timer > timeout) { #ifdef PN532DEBUG - PN532DEBUGPRINT.println("TIMEOUT!"); + PN532DEBUGPRINT.println("TIMEOUT!"); #endif - return false; - } + return false; + } + } + delay(10); } - delay(10); - } - return true; + return true; } /**************************************************************************/ @@ -1600,28 +1612,28 @@ bool Adafruit_PN532::waitready(uint16_t timeout) { */ /**************************************************************************/ void Adafruit_PN532::readdata(uint8_t *buff, uint8_t n) { - if (spi_dev) { - // SPI read - uint8_t cmd = PN532_SPI_DATAREAD; - spi_dev->write_then_read(&cmd, 1, buff, n); - } else if (i2c_dev) { - // I2C read - uint8_t rbuff[n + 1]; // +1 for leading RDY byte - i2c_dev->read(rbuff, n + 1); - for (uint8_t i = 0; i < n; i++) { - buff[i] = rbuff[i + 1]; + if (spi_dev) { + // SPI read + uint8_t cmd = PN532_SPI_DATAREAD; + spi_dev->write_then_read(&cmd, 1, buff, n); + } else if (i2c_dev) { + // I2C read + uint8_t rbuff[n + 1]; // +1 for leading RDY byte + i2c_dev->read(rbuff, n + 1); + for (uint8_t i = 0; i < n; i++) { + buff[i] = rbuff[i + 1]; + } + } else if (ser_dev) { + // Serial read + ser_dev->readBytes(buff, n); } - } else if (ser_dev) { - // Serial read - ser_dev->readBytes(buff, n); - } #ifdef PN532DEBUG - PN532DEBUGPRINT.print(F("Reading: ")); - for (uint8_t i = 0; i < n; i++) { - PN532DEBUGPRINT.print(F(" 0x")); - PN532DEBUGPRINT.print(buff[i], HEX); - } - PN532DEBUGPRINT.println(); + PN532DEBUGPRINT.print(F("Reading: ")); + for (uint8_t i = 0; i < n; i++) { + PN532DEBUGPRINT.print(F(" 0x")); + PN532DEBUGPRINT.print(buff[i], HEX); + } + PN532DEBUGPRINT.println(); #endif } @@ -1636,30 +1648,30 @@ void Adafruit_PN532::readdata(uint8_t *buff, uint8_t n) { */ /**************************************************************************/ uint8_t Adafruit_PN532::AsTarget() { - pn532_packetbuffer[0] = 0x8C; - uint8_t target[] = { - 0x8C, // INIT AS TARGET - 0x00, // MODE -> BITFIELD - 0x08, 0x00, // SENS_RES - MIFARE PARAMS - 0xdc, 0x44, 0x20, // NFCID1T - 0x60, // SEL_RES - 0x01, 0xfe, // NFCID2T MUST START WITH 01fe - FELICA PARAMS - POL_RES - 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xc0, - 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, // PAD - 0xff, 0xff, // SYSTEM CODE - 0xaa, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, - 0x33, 0x22, 0x11, 0x01, 0x00, // NFCID3t MAX 47 BYTES ATR_RES - 0x0d, 0x52, 0x46, 0x49, 0x44, 0x49, 0x4f, - 0x74, 0x20, 0x50, 0x4e, 0x35, 0x33, 0x32 // HISTORICAL BYTES - }; - if (!sendCommandCheckAck(target, sizeof(target))) - return false; + pn532_packetbuffer[0] = 0x8C; + uint8_t target[] = { + 0x8C, // INIT AS TARGET + 0x00, // MODE -> BITFIELD + 0x08, 0x00, // SENS_RES - MIFARE PARAMS + 0xdc, 0x44, 0x20, // NFCID1T + 0x60, // SEL_RES + 0x01, 0xfe, // NFCID2T MUST START WITH 01fe - FELICA PARAMS - POL_RES + 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xc0, + 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, // PAD + 0xff, 0xff, // SYSTEM CODE + 0xaa, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, + 0x33, 0x22, 0x11, 0x01, 0x00, // NFCID3t MAX 47 BYTES ATR_RES + 0x0d, 0x52, 0x46, 0x49, 0x44, 0x49, 0x4f, + 0x74, 0x20, 0x50, 0x4e, 0x35, 0x33, 0x32 // HISTORICAL BYTES + }; + if (!sendCommandCheckAck(target, sizeof(target))) + return false; - // read data packet - readdata(pn532_packetbuffer, 8); + // read data packet + readdata(pn532_packetbuffer, 8); - int offset = 6; - return (pn532_packetbuffer[offset] == 0x15); + int offset = 6; + return (pn532_packetbuffer[offset] == 0x15); } /**************************************************************************/ /*! @@ -1671,27 +1683,27 @@ uint8_t Adafruit_PN532::AsTarget() { */ /**************************************************************************/ uint8_t Adafruit_PN532::getDataTarget(uint8_t *cmd, uint8_t *cmdlen) { - uint8_t length; - pn532_packetbuffer[0] = 0x86; - if (!sendCommandCheckAck(pn532_packetbuffer, 1, 1000)) { - PN532DEBUGPRINT.println(F("Error en ack")); - return false; - } - - // read data packet - readdata(pn532_packetbuffer, 64); - length = pn532_packetbuffer[3] - 3; - - // if (length > *responseLength) {// Bug, should avoid it in the reading - // target data - // length = *responseLength; // silent truncation... - //} - - for (int i = 0; i < length; ++i) { - cmd[i] = pn532_packetbuffer[8 + i]; - } - *cmdlen = length; - return true; + uint8_t length; + pn532_packetbuffer[0] = 0x86; + if (!sendCommandCheckAck(pn532_packetbuffer, 1, 1000)) { + PN532DEBUGPRINT.println(F("Error en ack")); + return false; + } + + // read data packet + readdata(pn532_packetbuffer, 64); + length = pn532_packetbuffer[3] - 3; + + // if (length > *responseLength) {// Bug, should avoid it in the reading + // target data + // length = *responseLength; // silent truncation... + //} + + for (int i = 0; i < length; ++i) { + cmd[i] = pn532_packetbuffer[8 + i]; + } + *cmdlen = length; + return true; } /**************************************************************************/ @@ -1704,23 +1716,23 @@ uint8_t Adafruit_PN532::getDataTarget(uint8_t *cmd, uint8_t *cmdlen) { */ /**************************************************************************/ uint8_t Adafruit_PN532::setDataTarget(uint8_t *cmd, uint8_t cmdlen) { - uint8_t length; - // cmd1[0] = 0x8E; Must! + uint8_t length; + // cmd1[0] = 0x8E; Must! - if (!sendCommandCheckAck(cmd, cmdlen)) - return false; + if (!sendCommandCheckAck(cmd, cmdlen)) + return false; - // read data packet - readdata(pn532_packetbuffer, 8); - length = pn532_packetbuffer[3] - 3; - for (int i = 0; i < length; ++i) { - cmd[i] = pn532_packetbuffer[8 + i]; - } - // cmdl = 0 - cmdlen = length; - - int offset = 6; - return (pn532_packetbuffer[offset] == 0x15); + // read data packet + readdata(pn532_packetbuffer, 8); + length = pn532_packetbuffer[3] - 3; + for (int i = 0; i < length; ++i) { + cmd[i] = pn532_packetbuffer[8 + i]; + } + // cmdl = 0 + cmdlen = length; + + int offset = 6; + return (pn532_packetbuffer[offset] == 0x15); } /**************************************************************************/ @@ -1733,88 +1745,88 @@ uint8_t Adafruit_PN532::setDataTarget(uint8_t *cmd, uint8_t cmdlen) { */ /**************************************************************************/ void Adafruit_PN532::writecommand(uint8_t *cmd, uint8_t cmdlen) { - if (spi_dev) { - // SPI command write. - uint8_t checksum; - uint8_t packet[9 + cmdlen]; - uint8_t *p = packet; - cmdlen++; - - p[0] = PN532_SPI_DATAWRITE; - p++; - - p[0] = PN532_PREAMBLE; - p++; - p[0] = PN532_STARTCODE1; - p++; - p[0] = PN532_STARTCODE2; - p++; - checksum = PN532_PREAMBLE + PN532_STARTCODE1 + PN532_STARTCODE2; - - p[0] = cmdlen; - p++; - p[0] = ~cmdlen + 1; - p++; - - p[0] = PN532_HOSTTOPN532; - p++; - checksum += PN532_HOSTTOPN532; - - for (uint8_t i = 0; i < cmdlen - 1; i++) { - p[0] = cmd[i]; - p++; - checksum += cmd[i]; - } - - p[0] = ~checksum; - p++; - p[0] = PN532_POSTAMBLE; - p++; + if (spi_dev) { + // SPI command write. + uint8_t checksum; + uint8_t packet[9 + cmdlen]; + uint8_t *p = packet; + cmdlen++; + + p[0] = PN532_SPI_DATAWRITE; + p++; + + p[0] = PN532_PREAMBLE; + p++; + p[0] = PN532_STARTCODE1; + p++; + p[0] = PN532_STARTCODE2; + p++; + checksum = PN532_PREAMBLE + PN532_STARTCODE1 + PN532_STARTCODE2; + + p[0] = cmdlen; + p++; + p[0] = ~cmdlen + 1; + p++; + + p[0] = PN532_HOSTTOPN532; + p++; + checksum += PN532_HOSTTOPN532; + + for (uint8_t i = 0; i < cmdlen - 1; i++) { + p[0] = cmd[i]; + p++; + checksum += cmd[i]; + } + + p[0] = ~checksum; + p++; + p[0] = PN532_POSTAMBLE; + p++; #ifdef PN532DEBUG - Serial.print("Sending : "); - for (int i = 1; i < 8 + cmdlen; i++) { - Serial.print("0x"); - Serial.print(packet[i], HEX); - Serial.print(", "); - } - Serial.println(); + Serial.print("Sending : "); + for (int i = 1; i < 8 + cmdlen; i++) { + Serial.print("0x"); + Serial.print(packet[i], HEX); + Serial.print(", "); + } + Serial.println(); #endif - spi_dev->write(packet, 8 + cmdlen); - } else if (i2c_dev || ser_dev) { - // I2C or Serial command write. - uint8_t packet[8 + cmdlen]; - uint8_t LEN = cmdlen + 1; - - packet[0] = PN532_PREAMBLE; - packet[1] = PN532_STARTCODE1; - packet[2] = PN532_STARTCODE2; - packet[3] = LEN; - packet[4] = ~LEN + 1; - packet[5] = PN532_HOSTTOPN532; - uint8_t sum = 0; - for (uint8_t i = 0; i < cmdlen; i++) { - packet[6 + i] = cmd[i]; - sum += cmd[i]; - } - packet[6 + cmdlen] = ~(PN532_HOSTTOPN532 + sum) + 1; - packet[7 + cmdlen] = PN532_POSTAMBLE; + spi_dev->write(packet, 8 + cmdlen); + } else if (i2c_dev || ser_dev) { + // I2C or Serial command write. + uint8_t packet[8 + cmdlen]; + uint8_t LEN = cmdlen + 1; + + packet[0] = PN532_PREAMBLE; + packet[1] = PN532_STARTCODE1; + packet[2] = PN532_STARTCODE2; + packet[3] = LEN; + packet[4] = ~LEN + 1; + packet[5] = PN532_HOSTTOPN532; + uint8_t sum = 0; + for (uint8_t i = 0; i < cmdlen; i++) { + packet[6 + i] = cmd[i]; + sum += cmd[i]; + } + packet[6 + cmdlen] = ~(PN532_HOSTTOPN532 + sum) + 1; + packet[7 + cmdlen] = PN532_POSTAMBLE; #ifdef PN532DEBUG - Serial.print("Sending : "); - for (int i = 1; i < 8 + cmdlen; i++) { - Serial.print("0x"); - Serial.print(packet[i], HEX); - Serial.print(", "); - } - Serial.println(); + Serial.print("Sending : "); + for (int i = 1; i < 8 + cmdlen; i++) { + Serial.print("0x"); + Serial.print(packet[i], HEX); + Serial.print(", "); + } + Serial.println(); #endif - if (i2c_dev) { - i2c_dev->write(packet, 8 + cmdlen); - } else { - ser_dev->write(packet, 8 + cmdlen); + if (i2c_dev) { + i2c_dev->write(packet, 8 + cmdlen); + } else { + ser_dev->write(packet, 8 + cmdlen); + } } - } } diff --git a/src/libs/Adafruit_PN532.h b/src/libs/Adafruit_PN532.h index f59e576..c33db5c 100644 --- a/src/libs/Adafruit_PN532.h +++ b/src/libs/Adafruit_PN532.h @@ -70,13 +70,13 @@ #define PN532_SPI_DATAREAD (0x03) ///< Data read #define PN532_SPI_READY (0x01) ///< Ready -//#define PN532_I2C_ADDRESS (0x48 >> 1) ///< Default I2C address -// My fucking clone board for some goddamn reason uses the address 0x28 +// #define PN532_I2C_ADDRESS (0x48 >> 1) ///< Default I2C address +// My fucking clone board for some goddamn reason uses the address 0x28 #define PN532_I2C_ADDRESS (0x28) -#define PN532_I2C_READBIT (0x01) ///< Read bit -#define PN532_I2C_BUSY (0x00) ///< Busy -#define PN532_I2C_READY (0x01) ///< Ready -#define PN532_I2C_READYTIMEOUT (20) ///< Ready timeout +#define PN532_I2C_READBIT (0x01) ///< Read bit +#define PN532_I2C_BUSY (0x00) ///< Busy +#define PN532_I2C_READY (0x01) ///< Ready +#define PN532_I2C_READYTIMEOUT (20) ///< Ready timeout #define PN532_MIFARE_ISO14443A (0x00) ///< MiFare @@ -141,83 +141,85 @@ * @brief Class for working with Adafruit PN532 NFC/RFID breakout boards. */ class Adafruit_PN532 { -public: - Adafruit_PN532(uint8_t clk, uint8_t miso, uint8_t mosi, - uint8_t ss); // Software SPI - Adafruit_PN532(uint8_t ss, SPIClass *theSPI = &SPI); // Hardware SPI - Adafruit_PN532(uint8_t irq, uint8_t reset, - TwoWire *theWire = &Wire); // Hardware I2C - Adafruit_PN532(uint8_t reset, HardwareSerial *theSer); // Hardware UART - bool begin(void); - - void reset(void); - void wakeup(void); - - // Generic PN532 functions - bool SAMConfig(void); - uint32_t getFirmwareVersion(void); - bool sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, - uint16_t timeout = 100); - bool writeGPIO(uint8_t pinstate); - uint8_t readGPIO(void); - bool setPassiveActivationRetries(uint8_t maxRetries); - - // ISO14443A functions - bool readPassiveTargetID( - uint8_t cardbaudrate, uint8_t *uid, uint8_t *uidLength, - uint16_t timeout = 0); // timeout 0 means no timeout - will block forever. - bool startPassiveTargetIDDetection(uint8_t cardbaudrate); - bool readDetectedPassiveTargetID(uint8_t *uid, uint8_t *uidLength); - bool inDataExchange(uint8_t *send, uint8_t sendLength, uint8_t *response, - uint8_t *responseLength); - bool inListPassiveTarget(); - uint8_t AsTarget(); - uint8_t getDataTarget(uint8_t *cmd, uint8_t *cmdlen); - uint8_t setDataTarget(uint8_t *cmd, uint8_t cmdlen); - - // Mifare Classic functions - bool mifareclassic_IsFirstBlock(uint32_t uiBlock); - bool mifareclassic_IsTrailerBlock(uint32_t uiBlock); - uint8_t mifareclassic_AuthenticateBlock(uint8_t *uid, uint8_t uidLen, - uint32_t blockNumber, - uint8_t keyNumber, uint8_t *keyData); - uint8_t mifareclassic_ReadDataBlock(uint8_t blockNumber, uint8_t *data); - uint8_t mifareclassic_WriteDataBlock(uint8_t blockNumber, uint8_t *data); - uint8_t mifareclassic_FormatNDEF(void); - uint8_t mifareclassic_WriteNDEFURI(uint8_t sectorNumber, - uint8_t uriIdentifier, const char *url); - - // Mifare Ultralight functions - uint8_t mifareultralight_ReadPage(uint8_t page, uint8_t *buffer); - uint8_t mifareultralight_WritePage(uint8_t page, uint8_t *data); - - // NTAG2xx functions - uint8_t ntag2xx_ReadPage(uint8_t page, uint8_t *buffer); - uint8_t ntag2xx_WritePage(uint8_t page, uint8_t *data); - uint8_t ntag2xx_WriteNDEFURI(uint8_t uriIdentifier, char *url, - uint8_t dataLen); - - // Help functions to display formatted text - static void PrintHex(const byte *data, const uint32_t numBytes); - static void PrintHexChar(const byte *pbtData, const uint32_t numBytes); - -private: - int8_t _irq = -1, _reset = -1, _cs = -1; - int8_t _uid[7]; // ISO14443A uid - int8_t _uidLen; // uid len - int8_t _key[6]; // Mifare Classic key - int8_t _inListedTag; // Tg number of inlisted tag. - - // Low level communication functions that handle both SPI and I2C. - void readdata(uint8_t *buff, uint8_t n); - void writecommand(uint8_t *cmd, uint8_t cmdlen); - bool isready(); - bool waitready(uint16_t timeout); - bool readack(); - - Adafruit_SPIDevice *spi_dev = NULL; - Adafruit_I2CDevice *i2c_dev = NULL; - HardwareSerial *ser_dev = NULL; + public: + Adafruit_PN532(uint8_t clk, uint8_t miso, uint8_t mosi, + uint8_t ss); // Software SPI + Adafruit_PN532(uint8_t ss, SPIClass *theSPI = &SPI); // Hardware SPI + Adafruit_PN532(uint8_t irq, uint8_t reset, + TwoWire *theWire = &Wire); // Hardware I2C + Adafruit_PN532(uint8_t reset, HardwareSerial *theSer); // Hardware UART + bool begin(void); + + void reset(void); + void wakeup(void); + + // Generic PN532 functions + bool SAMConfig(void); + uint32_t getFirmwareVersion(void); + bool sendCommandCheckAck(uint8_t *cmd, uint8_t cmdlen, + uint16_t timeout = 100); + bool writeGPIO(uint8_t pinstate); + uint8_t readGPIO(void); + bool setPassiveActivationRetries(uint8_t maxRetries); + + // ISO14443A functions + bool readPassiveTargetID( + uint8_t cardbaudrate, uint8_t *uid, uint8_t *uidLength, + uint16_t timeout = + 0); // timeout 0 means no timeout - will block forever. + bool startPassiveTargetIDDetection(uint8_t cardbaudrate); + bool readDetectedPassiveTargetID(uint8_t *uid, uint8_t *uidLength); + bool inDataExchange(uint8_t *send, uint8_t sendLength, uint8_t *response, + uint8_t *responseLength); + bool inListPassiveTarget(); + uint8_t AsTarget(); + uint8_t getDataTarget(uint8_t *cmd, uint8_t *cmdlen); + uint8_t setDataTarget(uint8_t *cmd, uint8_t cmdlen); + + // Mifare Classic functions + bool mifareclassic_IsFirstBlock(uint32_t uiBlock); + bool mifareclassic_IsTrailerBlock(uint32_t uiBlock); + uint8_t mifareclassic_AuthenticateBlock(uint8_t *uid, uint8_t uidLen, + uint32_t blockNumber, + uint8_t keyNumber, + uint8_t *keyData); + uint8_t mifareclassic_ReadDataBlock(uint8_t blockNumber, uint8_t *data); + uint8_t mifareclassic_WriteDataBlock(uint8_t blockNumber, uint8_t *data); + uint8_t mifareclassic_FormatNDEF(void); + uint8_t mifareclassic_WriteNDEFURI(uint8_t sectorNumber, + uint8_t uriIdentifier, const char *url); + + // Mifare Ultralight functions + uint8_t mifareultralight_ReadPage(uint8_t page, uint8_t *buffer); + uint8_t mifareultralight_WritePage(uint8_t page, uint8_t *data); + + // NTAG2xx functions + uint8_t ntag2xx_ReadPage(uint8_t page, uint8_t *buffer); + uint8_t ntag2xx_WritePage(uint8_t page, uint8_t *data); + uint8_t ntag2xx_WriteNDEFURI(uint8_t uriIdentifier, char *url, + uint8_t dataLen); + + // Help functions to display formatted text + static void PrintHex(const byte *data, const uint32_t numBytes); + static void PrintHexChar(const byte *pbtData, const uint32_t numBytes); + + private: + int8_t _irq = -1, _reset = -1, _cs = -1; + int8_t _uid[7]; // ISO14443A uid + int8_t _uidLen; // uid len + int8_t _key[6]; // Mifare Classic key + int8_t _inListedTag; // Tg number of inlisted tag. + + // Low level communication functions that handle both SPI and I2C. + void readdata(uint8_t *buff, uint8_t n); + void writecommand(uint8_t *cmd, uint8_t cmdlen); + bool isready(); + bool waitready(uint16_t timeout); + bool readack(); + + Adafruit_SPIDevice *spi_dev = NULL; + Adafruit_I2CDevice *i2c_dev = NULL; + HardwareSerial *ser_dev = NULL; }; #endif diff --git a/src/libs/BleConnectionStatus.cpp b/src/libs/BleConnectionStatus.cpp index f01183b..83d3539 100644 --- a/src/libs/BleConnectionStatus.cpp +++ b/src/libs/BleConnectionStatus.cpp @@ -1,18 +1,17 @@ #include "BleConnectionStatus.h" -BleConnectionStatus::BleConnectionStatus(void) { -} +BleConnectionStatus::BleConnectionStatus(void) {} -void BleConnectionStatus::onConnect(BLEServer* pServer) -{ - this->connected = true; - BLE2902* desc = (BLE2902*)this->inputMouse->getDescriptorByUUID(BLEUUID((uint16_t)0x2902)); - desc->setNotifications(true); +void BleConnectionStatus::onConnect(BLEServer *pServer) { + this->connected = true; + BLE2902 *desc = (BLE2902 *)this->inputMouse->getDescriptorByUUID( + BLEUUID((uint16_t)0x2902)); + desc->setNotifications(true); } -void BleConnectionStatus::onDisconnect(BLEServer* pServer) -{ - this->connected = false; - BLE2902* desc = (BLE2902*)this->inputMouse->getDescriptorByUUID(BLEUUID((uint16_t)0x2902)); - desc->setNotifications(false); +void BleConnectionStatus::onDisconnect(BLEServer *pServer) { + this->connected = false; + BLE2902 *desc = (BLE2902 *)this->inputMouse->getDescriptorByUUID( + BLEUUID((uint16_t)0x2902)); + desc->setNotifications(false); } diff --git a/src/libs/BleConnectionStatus.h b/src/libs/BleConnectionStatus.h index b703150..45b7185 100644 --- a/src/libs/BleConnectionStatus.h +++ b/src/libs/BleConnectionStatus.h @@ -3,18 +3,17 @@ #include "sdkconfig.h" #if defined(CONFIG_BT_ENABLED) -#include #include "BLE2902.h" #include "BLECharacteristic.h" +#include -class BleConnectionStatus : public BLEServerCallbacks -{ -public: - BleConnectionStatus(void); - bool connected = false; - void onConnect(BLEServer* pServer); - void onDisconnect(BLEServer* pServer); - BLECharacteristic* inputMouse; +class BleConnectionStatus : public BLEServerCallbacks { + public: + BleConnectionStatus(void); + bool connected = false; + void onConnect(BLEServer *pServer); + void onDisconnect(BLEServer *pServer); + BLECharacteristic *inputMouse; }; #endif // CONFIG_BT_ENABLED diff --git a/src/libs/BleMouse.cpp b/src/libs/BleMouse.cpp index 665121e..8f5c16e 100644 --- a/src/libs/BleMouse.cpp +++ b/src/libs/BleMouse.cpp @@ -1,173 +1,165 @@ -#include -#include -#include #include "BLE2902.h" #include "BLEHIDDevice.h" -#include "HIDTypes.h" #include "HIDKeyboardTypes.h" -#include +#include "HIDTypes.h" #include "sdkconfig.h" +#include +#include +#include +#include #include "BleConnectionStatus.h" #include "BleMouse.h" #if defined(CONFIG_ARDUHAL_ESP_LOG) - #include "esp32-hal-log.h" - #define LOG_TAG "" +#include "esp32-hal-log.h" +#define LOG_TAG "" #else - #include "esp_log.h" - static const char* LOG_TAG = "BLEDevice"; +#include "esp_log.h" +static const char *LOG_TAG = "BLEDevice"; #endif static const uint8_t _hidReportDescriptor[] = { - USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop) - USAGE(1), 0x02, // USAGE (Mouse) - COLLECTION(1), 0x01, // COLLECTION (Application) - USAGE(1), 0x01, // USAGE (Pointer) - COLLECTION(1), 0x00, // COLLECTION (Physical) - // ------------------------------------------------- Buttons (Left, Right, Middle, Back, Forward) - USAGE_PAGE(1), 0x09, // USAGE_PAGE (Button) - USAGE_MINIMUM(1), 0x01, // USAGE_MINIMUM (Button 1) - USAGE_MAXIMUM(1), 0x05, // USAGE_MAXIMUM (Button 5) - LOGICAL_MINIMUM(1), 0x00, // LOGICAL_MINIMUM (0) - LOGICAL_MAXIMUM(1), 0x01, // LOGICAL_MAXIMUM (1) - REPORT_SIZE(1), 0x01, // REPORT_SIZE (1) - REPORT_COUNT(1), 0x05, // REPORT_COUNT (5) - HIDINPUT(1), 0x02, // INPUT (Data, Variable, Absolute) ;5 button bits - // ------------------------------------------------- Padding - REPORT_SIZE(1), 0x03, // REPORT_SIZE (3) - REPORT_COUNT(1), 0x01, // REPORT_COUNT (1) - HIDINPUT(1), 0x03, // INPUT (Constant, Variable, Absolute) ;3 bit padding - // ------------------------------------------------- X/Y position, Wheel - USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop) - USAGE(1), 0x30, // USAGE (X) - USAGE(1), 0x31, // USAGE (Y) - USAGE(1), 0x38, // USAGE (Wheel) - LOGICAL_MINIMUM(1), 0x81, // LOGICAL_MINIMUM (-127) - LOGICAL_MAXIMUM(1), 0x7f, // LOGICAL_MAXIMUM (127) - REPORT_SIZE(1), 0x08, // REPORT_SIZE (8) - REPORT_COUNT(1), 0x03, // REPORT_COUNT (3) - HIDINPUT(1), 0x06, // INPUT (Data, Variable, Relative) ;3 bytes (X,Y,Wheel) - // ------------------------------------------------- Horizontal wheel - USAGE_PAGE(1), 0x0c, // USAGE PAGE (Consumer Devices) - USAGE(2), 0x38, 0x02, // USAGE (AC Pan) - LOGICAL_MINIMUM(1), 0x81, // LOGICAL_MINIMUM (-127) - LOGICAL_MAXIMUM(1), 0x7f, // LOGICAL_MAXIMUM (127) - REPORT_SIZE(1), 0x08, // REPORT_SIZE (8) - REPORT_COUNT(1), 0x01, // REPORT_COUNT (1) - HIDINPUT(1), 0x06, // INPUT (Data, Var, Rel) - END_COLLECTION(0), // END_COLLECTION - END_COLLECTION(0) // END_COLLECTION + USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop) + USAGE(1), 0x02, // USAGE (Mouse) + COLLECTION(1), 0x01, // COLLECTION (Application) + USAGE(1), 0x01, // USAGE (Pointer) + COLLECTION(1), 0x00, // COLLECTION (Physical) + // ------------------------------------------------- Buttons (Left, Right, + // Middle, Back, Forward) + USAGE_PAGE(1), 0x09, // USAGE_PAGE (Button) + USAGE_MINIMUM(1), 0x01, // USAGE_MINIMUM (Button 1) + USAGE_MAXIMUM(1), 0x05, // USAGE_MAXIMUM (Button 5) + LOGICAL_MINIMUM(1), 0x00, // LOGICAL_MINIMUM (0) + LOGICAL_MAXIMUM(1), 0x01, // LOGICAL_MAXIMUM (1) + REPORT_SIZE(1), 0x01, // REPORT_SIZE (1) + REPORT_COUNT(1), 0x05, // REPORT_COUNT (5) + HIDINPUT(1), 0x02, // INPUT (Data, Variable, Absolute) ;5 button bits + // ------------------------------------------------- Padding + REPORT_SIZE(1), 0x03, // REPORT_SIZE (3) + REPORT_COUNT(1), 0x01, // REPORT_COUNT (1) + HIDINPUT(1), + 0x03, // INPUT (Constant, Variable, Absolute) ;3 bit padding + // ------------------------------------------------- X/Y position, Wheel + USAGE_PAGE(1), 0x01, // USAGE_PAGE (Generic Desktop) + USAGE(1), 0x30, // USAGE (X) + USAGE(1), 0x31, // USAGE (Y) + USAGE(1), 0x38, // USAGE (Wheel) + LOGICAL_MINIMUM(1), 0x81, // LOGICAL_MINIMUM (-127) + LOGICAL_MAXIMUM(1), 0x7f, // LOGICAL_MAXIMUM (127) + REPORT_SIZE(1), 0x08, // REPORT_SIZE (8) + REPORT_COUNT(1), 0x03, // REPORT_COUNT (3) + HIDINPUT(1), + 0x06, // INPUT (Data, Variable, Relative) ;3 bytes (X,Y,Wheel) + // ------------------------------------------------- Horizontal wheel + USAGE_PAGE(1), 0x0c, // USAGE PAGE (Consumer Devices) + USAGE(2), 0x38, 0x02, // USAGE (AC Pan) + LOGICAL_MINIMUM(1), 0x81, // LOGICAL_MINIMUM (-127) + LOGICAL_MAXIMUM(1), 0x7f, // LOGICAL_MAXIMUM (127) + REPORT_SIZE(1), 0x08, // REPORT_SIZE (8) + REPORT_COUNT(1), 0x01, // REPORT_COUNT (1) + HIDINPUT(1), 0x06, // INPUT (Data, Var, Rel) + END_COLLECTION(0), // END_COLLECTION + END_COLLECTION(0) // END_COLLECTION }; -BleMouse::BleMouse(std::string deviceName, std::string deviceManufacturer, uint8_t batteryLevel) : - _buttons(0), - hid(0) -{ - this->deviceName = deviceName; - this->deviceManufacturer = deviceManufacturer; - this->batteryLevel = batteryLevel; - this->connectionStatus = new BleConnectionStatus(); +BleMouse::BleMouse(std::string deviceName, std::string deviceManufacturer, + uint8_t batteryLevel) + : _buttons(0), hid(0) { + this->deviceName = deviceName; + this->deviceManufacturer = deviceManufacturer; + this->batteryLevel = batteryLevel; + this->connectionStatus = new BleConnectionStatus(); } -void BleMouse::begin(void) -{ - xTaskCreate(this->taskServer, "server", 20000, (void *)this, 5, NULL); +void BleMouse::begin(void) { + xTaskCreate(this->taskServer, "server", 20000, (void *)this, 5, NULL); } -void BleMouse::end(void) -{ -} +void BleMouse::end(void) {} -void BleMouse::click(uint8_t b) -{ - _buttons = b; - move(0,0,0,0); - _buttons = 0; - move(0,0,0,0); +void BleMouse::click(uint8_t b) { + _buttons = b; + move(0, 0, 0, 0); + _buttons = 0; + move(0, 0, 0, 0); } -void BleMouse::move(signed char x, signed char y, signed char wheel, signed char hWheel) -{ - if (this->isConnected()) - { - uint8_t m[5]; - m[0] = _buttons; - m[1] = x; - m[2] = y; - m[3] = wheel; - m[4] = hWheel; - this->inputMouse->setValue(m, 5); - this->inputMouse->notify(); - } +void BleMouse::move(signed char x, signed char y, signed char wheel, + signed char hWheel) { + if (this->isConnected()) { + uint8_t m[5]; + m[0] = _buttons; + m[1] = x; + m[2] = y; + m[3] = wheel; + m[4] = hWheel; + this->inputMouse->setValue(m, 5); + this->inputMouse->notify(); + } } -void BleMouse::buttons(uint8_t b) -{ - if (b != _buttons) - { - _buttons = b; - move(0,0,0,0); - } +void BleMouse::buttons(uint8_t b) { + if (b != _buttons) { + _buttons = b; + move(0, 0, 0, 0); + } } -void BleMouse::press(uint8_t b) -{ - buttons(_buttons | b); -} +void BleMouse::press(uint8_t b) { buttons(_buttons | b); } -void BleMouse::release(uint8_t b) -{ - buttons(_buttons & ~b); -} +void BleMouse::release(uint8_t b) { buttons(_buttons & ~b); } -bool BleMouse::isPressed(uint8_t b) -{ - if ((b & _buttons) > 0) - return true; - return false; +bool BleMouse::isPressed(uint8_t b) { + if ((b & _buttons) > 0) + return true; + return false; } -bool BleMouse::isConnected(void) { - return this->connectionStatus->connected; -} +bool BleMouse::isConnected(void) { return this->connectionStatus->connected; } void BleMouse::setBatteryLevel(uint8_t level) { - this->batteryLevel = level; - if (hid != 0) - this->hid->setBatteryLevel(this->batteryLevel); + this->batteryLevel = level; + if (hid != 0) + this->hid->setBatteryLevel(this->batteryLevel); } -void BleMouse::taskServer(void* pvParameter) { - BleMouse* bleMouseInstance = (BleMouse *) pvParameter; //static_cast(pvParameter); - BLEDevice::init(std::string(bleMouseInstance->deviceName.c_str())); - BLEServer *pServer = BLEDevice::createServer(); - pServer->setCallbacks(bleMouseInstance->connectionStatus); +void BleMouse::taskServer(void *pvParameter) { + BleMouse *bleMouseInstance = + (BleMouse *)pvParameter; // static_cast(pvParameter); + BLEDevice::init(std::string(bleMouseInstance->deviceName.c_str())); + BLEServer *pServer = BLEDevice::createServer(); + pServer->setCallbacks(bleMouseInstance->connectionStatus); - bleMouseInstance->hid = new BLEHIDDevice(pServer); - bleMouseInstance->inputMouse = bleMouseInstance->hid->inputReport(0); // <-- input REPORTID from report map - bleMouseInstance->connectionStatus->inputMouse = bleMouseInstance->inputMouse; + bleMouseInstance->hid = new BLEHIDDevice(pServer); + bleMouseInstance->inputMouse = bleMouseInstance->hid->inputReport( + 0); // <-- input REPORTID from report map + bleMouseInstance->connectionStatus->inputMouse = + bleMouseInstance->inputMouse; - bleMouseInstance->hid->manufacturer()->setValue(std::string(bleMouseInstance->deviceManufacturer.c_str())); + bleMouseInstance->hid->manufacturer()->setValue( + std::string(bleMouseInstance->deviceManufacturer.c_str())); - bleMouseInstance->hid->pnp(0x02, 0xe502, 0xa111, 0x0210); - bleMouseInstance->hid->hidInfo(0x00,0x02); + bleMouseInstance->hid->pnp(0x02, 0xe502, 0xa111, 0x0210); + bleMouseInstance->hid->hidInfo(0x00, 0x02); - BLESecurity *pSecurity = new BLESecurity(); + BLESecurity *pSecurity = new BLESecurity(); - pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND); + pSecurity->setAuthenticationMode(ESP_LE_AUTH_BOND); - bleMouseInstance->hid->reportMap((uint8_t*)_hidReportDescriptor, sizeof(_hidReportDescriptor)); - bleMouseInstance->hid->startServices(); + bleMouseInstance->hid->reportMap((uint8_t *)_hidReportDescriptor, + sizeof(_hidReportDescriptor)); + bleMouseInstance->hid->startServices(); - bleMouseInstance->onStarted(pServer); + bleMouseInstance->onStarted(pServer); - BLEAdvertising *pAdvertising = pServer->getAdvertising(); - pAdvertising->setAppearance(HID_MOUSE); - pAdvertising->addServiceUUID(bleMouseInstance->hid->hidService()->getUUID()); - pAdvertising->start(); - bleMouseInstance->hid->setBatteryLevel(bleMouseInstance->batteryLevel); + BLEAdvertising *pAdvertising = pServer->getAdvertising(); + pAdvertising->setAppearance(HID_MOUSE); + pAdvertising->addServiceUUID( + bleMouseInstance->hid->hidService()->getUUID()); + pAdvertising->start(); + bleMouseInstance->hid->setBatteryLevel(bleMouseInstance->batteryLevel); - ESP_LOGD(LOG_TAG, "Advertising started!"); - vTaskDelay(portMAX_DELAY); //delay(portMAX_DELAY); + ESP_LOGD(LOG_TAG, "Advertising started!"); + vTaskDelay(portMAX_DELAY); // delay(portMAX_DELAY); } diff --git a/src/libs/BleMouse.h b/src/libs/BleMouse.h index 8b4ab9b..4468189 100644 --- a/src/libs/BleMouse.h +++ b/src/libs/BleMouse.h @@ -3,42 +3,49 @@ #include "sdkconfig.h" #if defined(CONFIG_BT_ENABLED) -#include "BleConnectionStatus.h" -#include "BLEHIDDevice.h" #include "BLECharacteristic.h" +#include "BLEHIDDevice.h" +#include "BleConnectionStatus.h" #define MOUSE_LEFT 1 #define MOUSE_RIGHT 2 #define MOUSE_MIDDLE 4 #define MOUSE_BACK 8 #define MOUSE_FORWARD 16 -#define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE) # For compatibility with the Mouse library +#define MOUSE_ALL \ + (MOUSE_LEFT | MOUSE_RIGHT | \ + MOUSE_MIDDLE) #For compatibility with the Mouse library class BleMouse { -private: - uint8_t _buttons; - BleConnectionStatus* connectionStatus; - BLEHIDDevice* hid; - BLECharacteristic* inputMouse; - void buttons(uint8_t b); - void rawAction(uint8_t msg[], char msgSize); - static void taskServer(void* pvParameter); -public: - BleMouse(std::string deviceName = "ESP32 Bluetooth Mouse", std::string deviceManufacturer = "Espressif", uint8_t batteryLevel = 100); - void begin(void); - void end(void); - void click(uint8_t b = MOUSE_LEFT); - void move(signed char x, signed char y, signed char wheel = 0, signed char hWheel = 0); - void press(uint8_t b = MOUSE_LEFT); // press LEFT by default - void release(uint8_t b = MOUSE_LEFT); // release LEFT by default - bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default - bool isConnected(void); - void setBatteryLevel(uint8_t level); - uint8_t batteryLevel; - std::string deviceManufacturer; - std::string deviceName; -protected: - virtual void onStarted(BLEServer *pServer) { }; + private: + uint8_t _buttons; + BleConnectionStatus *connectionStatus; + BLEHIDDevice *hid; + BLECharacteristic *inputMouse; + void buttons(uint8_t b); + void rawAction(uint8_t msg[], char msgSize); + static void taskServer(void *pvParameter); + + public: + BleMouse(std::string deviceName = "ESP32 Bluetooth Mouse", + std::string deviceManufacturer = "Espressif", + uint8_t batteryLevel = 100); + void begin(void); + void end(void); + void click(uint8_t b = MOUSE_LEFT); + void move(signed char x, signed char y, signed char wheel = 0, + signed char hWheel = 0); + void press(uint8_t b = MOUSE_LEFT); // press LEFT by default + void release(uint8_t b = MOUSE_LEFT); // release LEFT by default + bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default + bool isConnected(void); + void setBatteryLevel(uint8_t level); + uint8_t batteryLevel; + std::string deviceManufacturer; + std::string deviceName; + + protected: + virtual void onStarted(BLEServer *pServer) {}; }; #endif // CONFIG_BT_ENABLED diff --git a/src/libs/ELECHOUSE_CC1101_SRC_DRV.cpp b/src/libs/ELECHOUSE_CC1101_SRC_DRV.cpp index 5568989..ead012f 100644 --- a/src/libs/ELECHOUSE_CC1101_SRC_DRV.cpp +++ b/src/libs/ELECHOUSE_CC1101_SRC_DRV.cpp @@ -5,24 +5,25 @@ Version: November 12, 2010 This library is designed to use CC1101/CC1100 module on Arduino platform. - CC1101/CC1100 module is an useful wireless module.Using the functions of the - library, you can easily send and receive data by the CC1101/CC1100 module. + CC1101/CC1100 module is an useful wireless module.Using the functions of the + library, you can easily send and receive data by the CC1101/CC1100 module. Just have fun! For the details, please refer to the datasheet of CC1100/CC1101. ---------------------------------------------------------------------------------------------------------------- -cc1101 Driver for RC Switch. Mod by Little Satan. With permission to modify and publish Wilson Shen (ELECHOUSE). +cc1101 Driver for RC Switch. Mod by Little Satan. With permission to modify and +publish Wilson Shen (ELECHOUSE). ---------------------------------------------------------------------------------------------------------------- */ -#include #include "ELECHOUSE_CC1101_SRC_DRV.h" #include +#include /****************************************************************/ -#define WRITE_BURST 0x40 //write burst -#define READ_SINGLE 0x80 //read single -#define READ_BURST 0xC0 //read burst -#define BYTES_IN_RXFIFO 0x7F //byte number in RXfifo -#define max_modul 6 +#define WRITE_BURST 0x40 // write burst +#define READ_SINGLE 0x80 // read single +#define READ_BURST 0xC0 // read burst +#define BYTES_IN_RXFIFO 0x7F // byte number in RXfifo +#define max_modul 6 SPIClass CCSPI(HSPI); @@ -43,7 +44,7 @@ byte MOSI_PIN_M[max_modul]; byte SS_PIN_M[max_modul]; byte GDO0_M[max_modul]; byte GDO2_M[max_modul]; -byte gdo_set=0; +byte gdo_set = 0; bool spi = 0; bool ccmode = 0; float MHz = 433.92; @@ -65,1245 +66,1417 @@ byte pc0PktForm; byte pc0CRC_EN; byte pc0LenConf; byte trxstate = 0; -byte clb1[2]= {24,28}; -byte clb2[2]= {31,38}; -byte clb3[2]= {65,76}; -byte clb4[2]= {77,79}; +byte clb1[2] = {24, 28}; +byte clb2[2] = {31, 38}; +byte clb3[2] = {65, 76}; +byte clb4[2] = {77, 79}; /****************************************************************/ -uint8_t PA_TABLE[8] {0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00}; +uint8_t PA_TABLE[8]{0x00, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; // -30 -20 -15 -10 0 5 7 10 -uint8_t PA_TABLE_315[8] {0x12,0x0D,0x1C,0x34,0x51,0x85,0xCB,0xC2,}; //300 - 348 -uint8_t PA_TABLE_433[8] {0x12,0x0E,0x1D,0x34,0x60,0x84,0xC8,0xC0,}; //387 - 464 +uint8_t PA_TABLE_315[8]{ + 0x12, 0x0D, 0x1C, 0x34, 0x51, 0x85, 0xCB, 0xC2, +}; // 300 - 348 +uint8_t PA_TABLE_433[8]{ + 0x12, 0x0E, 0x1D, 0x34, 0x60, 0x84, 0xC8, 0xC0, +}; // 387 - 464 // -30 -20 -15 -10 -6 0 5 7 10 12 -uint8_t PA_TABLE_868[10] {0x03,0x17,0x1D,0x26,0x37,0x50,0x86,0xCD,0xC5,0xC0,}; //779 - 899.99 +uint8_t PA_TABLE_868[10]{ + 0x03, 0x17, 0x1D, 0x26, 0x37, 0x50, 0x86, 0xCD, 0xC5, 0xC0, +}; // 779 - 899.99 // -30 -20 -15 -10 -6 0 5 7 10 11 -uint8_t PA_TABLE_915[10] {0x03,0x0E,0x1E,0x27,0x38,0x8E,0x84,0xCC,0xC3,0xC0,}; //900 - 928 -/**************************************************************** -*FUNCTION NAME:SpiStart -*FUNCTION :spi communication start -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::SpiStart(void) -{ - // initialize the SPI pins - pinMode(SCK_PIN, OUTPUT); - pinMode(MOSI_PIN, OUTPUT); - pinMode(MISO_PIN, INPUT); - pinMode(SS_PIN, OUTPUT); +uint8_t PA_TABLE_915[10]{ + 0x03, 0x0E, 0x1E, 0x27, 0x38, 0x8E, 0x84, 0xCC, 0xC3, 0xC0, +}; // 900 - 928 +/**************************************************************** + *FUNCTION NAME:SpiStart + *FUNCTION :spi communication start + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::SpiStart(void) { + // initialize the SPI pins + pinMode(SCK_PIN, OUTPUT); + pinMode(MOSI_PIN, OUTPUT); + pinMode(MISO_PIN, INPUT); + pinMode(SS_PIN, OUTPUT); - // enable SPI - #ifdef ESP32 - CCSPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN, SS_PIN); - #else - CCSPI.begin(); - #endif -} -/**************************************************************** -*FUNCTION NAME:SpiEnd -*FUNCTION :spi communication disable -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::SpiEnd(void) -{ - // disable SPI - CCSPI.endTransaction(); - CCSPI.end(); -} -/**************************************************************** -*FUNCTION NAME: GDO_Set() -*FUNCTION : set GDO0,GDO2 pin for serial pinmode. -*INPUT : none -*OUTPUT : none -****************************************************************/ -void ELECHOUSE_CC1101::GDO_Set (void) -{ - pinMode(GDO0, OUTPUT); - pinMode(GDO2, INPUT); -} -/**************************************************************** -*FUNCTION NAME: GDO_Set() -*FUNCTION : set GDO0 for internal transmission mode. -*INPUT : none -*OUTPUT : none -****************************************************************/ -void ELECHOUSE_CC1101::GDO0_Set (void) -{ - pinMode(GDO0, INPUT); -} -/**************************************************************** -*FUNCTION NAME:Reset -*FUNCTION :CC1101 reset //details refer datasheet of CC1101/CC1100// -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::Reset (void) -{ - digitalWrite(SS_PIN, LOW); - delay(1); - digitalWrite(SS_PIN, HIGH); - delay(1); - digitalWrite(SS_PIN, LOW); - while(digitalRead(MISO_PIN)); - CCSPI.transfer(CC1101_SRES); - while(digitalRead(MISO_PIN)); - digitalWrite(SS_PIN, HIGH); -} -/**************************************************************** -*FUNCTION NAME:Init -*FUNCTION :CC1101 initialization -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::Init(void) -{ - setSpi(); - SpiStart(); //spi initialization - digitalWrite(SS_PIN, HIGH); - digitalWrite(SCK_PIN, HIGH); - digitalWrite(MOSI_PIN, LOW); - Reset(); //CC1101 reset - RegConfigSettings(); //CC1101 register config - SpiEnd(); -} -/**************************************************************** -*FUNCTION NAME:SpiWriteReg -*FUNCTION :CC1101 write data to register -*INPUT :addr: register address; value: register value -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::SpiWriteReg(byte addr, byte value) -{ - SpiStart(); - digitalWrite(SS_PIN, LOW); - while(digitalRead(MISO_PIN)); - CCSPI.transfer(addr); - CCSPI.transfer(value); - digitalWrite(SS_PIN, HIGH); - SpiEnd(); -} -/**************************************************************** -*FUNCTION NAME:SpiWriteBurstReg -*FUNCTION :CC1101 write burst data to register -*INPUT :addr: register address; buffer:register value array; num:number to write -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::SpiWriteBurstReg(byte addr, byte *buffer, byte num) -{ - byte i, temp; - SpiStart(); - temp = addr | WRITE_BURST; - digitalWrite(SS_PIN, LOW); - while(digitalRead(MISO_PIN)); - CCSPI.transfer(temp); - for (i = 0; i < num; i++) - { - CCSPI.transfer(buffer[i]); - } - digitalWrite(SS_PIN, HIGH); - SpiEnd(); -} -/**************************************************************** -*FUNCTION NAME:SpiStrobe -*FUNCTION :CC1101 Strobe -*INPUT :strobe: command; //refer define in CC1101.h// -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::SpiStrobe(byte strobe) -{ - SpiStart(); - digitalWrite(SS_PIN, LOW); - while(digitalRead(MISO_PIN)); - CCSPI.transfer(strobe); - digitalWrite(SS_PIN, HIGH); - SpiEnd(); -} -/**************************************************************** -*FUNCTION NAME:SpiReadReg -*FUNCTION :CC1101 read data from register -*INPUT :addr: register address -*OUTPUT :register value -****************************************************************/ -byte ELECHOUSE_CC1101::SpiReadReg(byte addr) -{ - byte temp, value; - SpiStart(); - temp = addr| READ_SINGLE; - digitalWrite(SS_PIN, LOW); - while(digitalRead(MISO_PIN)); - CCSPI.transfer(temp); - value=CCSPI.transfer(0); - digitalWrite(SS_PIN, HIGH); - SpiEnd(); - return value; +// enable SPI +#ifdef ESP32 + CCSPI.begin(SCK_PIN, MISO_PIN, MOSI_PIN, SS_PIN); +#else + CCSPI.begin(); +#endif +} +/**************************************************************** + *FUNCTION NAME:SpiEnd + *FUNCTION :spi communication disable + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::SpiEnd(void) { + // disable SPI + CCSPI.endTransaction(); + CCSPI.end(); +} +/**************************************************************** + *FUNCTION NAME: GDO_Set() + *FUNCTION : set GDO0,GDO2 pin for serial pinmode. + *INPUT : none + *OUTPUT : none + ****************************************************************/ +void ELECHOUSE_CC1101::GDO_Set(void) { + pinMode(GDO0, OUTPUT); + pinMode(GDO2, INPUT); +} +/**************************************************************** + *FUNCTION NAME: GDO_Set() + *FUNCTION : set GDO0 for internal transmission mode. + *INPUT : none + *OUTPUT : none + ****************************************************************/ +void ELECHOUSE_CC1101::GDO0_Set(void) { pinMode(GDO0, INPUT); } +/**************************************************************** + *FUNCTION NAME:Reset + *FUNCTION :CC1101 reset //details refer datasheet of CC1101/CC1100// + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::Reset(void) { + digitalWrite(SS_PIN, LOW); + delay(1); + digitalWrite(SS_PIN, HIGH); + delay(1); + digitalWrite(SS_PIN, LOW); + while (digitalRead(MISO_PIN)) + ; + CCSPI.transfer(CC1101_SRES); + while (digitalRead(MISO_PIN)) + ; + digitalWrite(SS_PIN, HIGH); +} +/**************************************************************** + *FUNCTION NAME:Init + *FUNCTION :CC1101 initialization + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::Init(void) { + setSpi(); + SpiStart(); // spi initialization + digitalWrite(SS_PIN, HIGH); + digitalWrite(SCK_PIN, HIGH); + digitalWrite(MOSI_PIN, LOW); + Reset(); // CC1101 reset + RegConfigSettings(); // CC1101 register config + SpiEnd(); +} +/**************************************************************** + *FUNCTION NAME:SpiWriteReg + *FUNCTION :CC1101 write data to register + *INPUT :addr: register address; value: register value + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::SpiWriteReg(byte addr, byte value) { + SpiStart(); + digitalWrite(SS_PIN, LOW); + while (digitalRead(MISO_PIN)) + ; + CCSPI.transfer(addr); + CCSPI.transfer(value); + digitalWrite(SS_PIN, HIGH); + SpiEnd(); +} +/**************************************************************** + *FUNCTION NAME:SpiWriteBurstReg + *FUNCTION :CC1101 write burst data to register + *INPUT :addr: register address; buffer:register value array; num:number + * to write OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::SpiWriteBurstReg(byte addr, byte *buffer, byte num) { + byte i, temp; + SpiStart(); + temp = addr | WRITE_BURST; + digitalWrite(SS_PIN, LOW); + while (digitalRead(MISO_PIN)) + ; + CCSPI.transfer(temp); + for (i = 0; i < num; i++) { + CCSPI.transfer(buffer[i]); + } + digitalWrite(SS_PIN, HIGH); + SpiEnd(); +} +/**************************************************************** + *FUNCTION NAME:SpiStrobe + *FUNCTION :CC1101 Strobe + *INPUT :strobe: command; //refer define in CC1101.h// + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::SpiStrobe(byte strobe) { + SpiStart(); + digitalWrite(SS_PIN, LOW); + while (digitalRead(MISO_PIN)) + ; + CCSPI.transfer(strobe); + digitalWrite(SS_PIN, HIGH); + SpiEnd(); +} +/**************************************************************** + *FUNCTION NAME:SpiReadReg + *FUNCTION :CC1101 read data from register + *INPUT :addr: register address + *OUTPUT :register value + ****************************************************************/ +byte ELECHOUSE_CC1101::SpiReadReg(byte addr) { + byte temp, value; + SpiStart(); + temp = addr | READ_SINGLE; + digitalWrite(SS_PIN, LOW); + while (digitalRead(MISO_PIN)) + ; + CCSPI.transfer(temp); + value = CCSPI.transfer(0); + digitalWrite(SS_PIN, HIGH); + SpiEnd(); + return value; } /**************************************************************** -*FUNCTION NAME:SpiReadBurstReg -*FUNCTION :CC1101 read burst data from register -*INPUT :addr: register address; buffer:array to store register value; num: number to read -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::SpiReadBurstReg(byte addr, byte *buffer, byte num) -{ - byte i,temp; - SpiStart(); - temp = addr | READ_BURST; - digitalWrite(SS_PIN, LOW); - while(digitalRead(MISO_PIN)); - CCSPI.transfer(temp); - for(i=0;i4){m=4;} -modulation = m; -Split_MDMCFG2(); -switch (m) -{ -case 0: m2MODFM=0x00; frend0=0x10; break; // 2-FSK -case 1: m2MODFM=0x10; frend0=0x10; break; // GFSK -case 2: m2MODFM=0x30; frend0=0x11; break; // ASK -case 3: m2MODFM=0x40; frend0=0x10; break; // 4-FSK -case 4: m2MODFM=0x70; frend0=0x10; break; // MSK -} -SpiWriteReg(CC1101_MDMCFG2, m2DCOFF+m2MODFM+m2MANCH+m2SYNCM); -SpiWriteReg(CC1101_FREND0, frend0); -setPA(pa); -} -/**************************************************************** -*FUNCTION NAME:PA Power -*FUNCTION :set CC1101 PA Power -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setPA(int p) -{ -int a; -pa = p; + *FUNCTION NAME:SpiReadStatus + *FUNCTION :CC1101 read status register + *INPUT :addr: register address + *OUTPUT :status value + ****************************************************************/ +byte ELECHOUSE_CC1101::SpiReadStatus(byte addr) { + byte value, temp; + SpiStart(); + temp = addr | READ_BURST; + digitalWrite(SS_PIN, LOW); + while (digitalRead(MISO_PIN)) + ; + CCSPI.transfer(temp); + value = CCSPI.transfer(0); + digitalWrite(SS_PIN, HIGH); + SpiEnd(); + return value; +} +/**************************************************************** + *FUNCTION NAME:SPI pin Settings + *FUNCTION :Set Spi pins + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setSpi(void) { + if (spi == 0) { +#if defined __AVR_ATmega168__ || defined __AVR_ATmega328P__ + SCK_PIN = 13; + MISO_PIN = 12; + MOSI_PIN = 11; + SS_PIN = 10; +#elif defined __AVR_ATmega1280__ || defined __AVR_ATmega2560__ + SCK_PIN = 52; + MISO_PIN = 50; + MOSI_PIN = 51; + SS_PIN = 53; +#elif ESP8266 + SCK_PIN = 14; + MISO_PIN = 12; + MOSI_PIN = 13; + SS_PIN = 15; +#elif ESP32 + SCK_PIN = 18; + MISO_PIN = 19; + MOSI_PIN = 23; + SS_PIN = 5; +#else + SCK_PIN = 13; + MISO_PIN = 12; + MOSI_PIN = 11; + SS_PIN = 10; +#endif + } +} +/**************************************************************** + *FUNCTION NAME:COSTUM SPI + *FUNCTION :set costum spi pins. + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setSpiPin(byte sck, byte miso, byte mosi, byte ss) { + spi = 1; + SCK_PIN = sck; + MISO_PIN = miso; + MOSI_PIN = mosi; + SS_PIN = ss; +} +/**************************************************************** + *FUNCTION NAME:COSTUM SPI + *FUNCTION :set costum spi pins. + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::addSpiPin(byte sck, byte miso, byte mosi, byte ss, + byte modul) { + spi = 1; + SCK_PIN_M[modul] = sck; + MISO_PIN_M[modul] = miso; + MOSI_PIN_M[modul] = mosi; + SS_PIN_M[modul] = ss; +} +/**************************************************************** + *FUNCTION NAME:GDO Pin settings + *FUNCTION :set GDO Pins + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setGDO(byte gdo0, byte gdo2) { + GDO0 = gdo0; + GDO2 = gdo2; + GDO_Set(); +} +/**************************************************************** + *FUNCTION NAME:GDO0 Pin setting + *FUNCTION :set GDO0 Pin + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setGDO0(byte gdo0) { + GDO0 = gdo0; + GDO0_Set(); +} +/**************************************************************** + *FUNCTION NAME:GDO Pin settings + *FUNCTION :add GDO Pins + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::addGDO(byte gdo0, byte gdo2, byte modul) { + GDO0_M[modul] = gdo0; + GDO2_M[modul] = gdo2; + gdo_set = 2; + GDO_Set(); +} +/**************************************************************** + *FUNCTION NAME:add GDO0 Pin + *FUNCTION :add GDO0 Pin + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::addGDO0(byte gdo0, byte modul) { + GDO0_M[modul] = gdo0; + gdo_set = 1; + GDO0_Set(); +} +/**************************************************************** + *FUNCTION NAME:set Modul + *FUNCTION :change modul + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setModul(byte modul) { + SCK_PIN = SCK_PIN_M[modul]; + MISO_PIN = MISO_PIN_M[modul]; + MOSI_PIN = MOSI_PIN_M[modul]; + SS_PIN = SS_PIN_M[modul]; + if (gdo_set == 1) { + GDO0 = GDO0_M[modul]; + } else if (gdo_set == 2) { + GDO0 = GDO0_M[modul]; + GDO2 = GDO2_M[modul]; + } +} +/**************************************************************** + *FUNCTION NAME:CCMode + *FUNCTION :Format of RX and TX data + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setCCMode(bool s) { + ccmode = s; + if (ccmode == 1) { + SpiWriteReg(CC1101_IOCFG2, 0x0B); + SpiWriteReg(CC1101_IOCFG0, 0x06); + SpiWriteReg(CC1101_PKTCTRL0, 0x05); + SpiWriteReg(CC1101_MDMCFG3, 0xF8); + SpiWriteReg(CC1101_MDMCFG4, 11 + m4RxBw); + } else { + SpiWriteReg(CC1101_IOCFG2, 0x0D); + SpiWriteReg(CC1101_IOCFG0, 0x0D); + SpiWriteReg(CC1101_PKTCTRL0, 0x32); + SpiWriteReg(CC1101_MDMCFG3, 0x93); + SpiWriteReg(CC1101_MDMCFG4, 7 + m4RxBw); + } + setModulation(modulation); +} +/**************************************************************** + *FUNCTION NAME:Modulation + *FUNCTION :set CC1101 Modulation + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setModulation(byte m) { + if (m > 4) { + m = 4; + } + modulation = m; + Split_MDMCFG2(); + switch (m) { + case 0: + m2MODFM = 0x00; + frend0 = 0x10; + break; // 2-FSK + case 1: + m2MODFM = 0x10; + frend0 = 0x10; + break; // GFSK + case 2: + m2MODFM = 0x30; + frend0 = 0x11; + break; // ASK + case 3: + m2MODFM = 0x40; + frend0 = 0x10; + break; // 4-FSK + case 4: + m2MODFM = 0x70; + frend0 = 0x10; + break; // MSK + } + SpiWriteReg(CC1101_MDMCFG2, m2DCOFF + m2MODFM + m2MANCH + m2SYNCM); + SpiWriteReg(CC1101_FREND0, frend0); + setPA(pa); +} +/**************************************************************** + *FUNCTION NAME:PA Power + *FUNCTION :set CC1101 PA Power + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setPA(int p) { + int a; + pa = p; -if (MHz >= 300 && MHz <= 348){ -if (pa <= -30){a = PA_TABLE_315[0];} -else if (pa > -30 && pa <= -20){a = PA_TABLE_315[1];} -else if (pa > -20 && pa <= -15){a = PA_TABLE_315[2];} -else if (pa > -15 && pa <= -10){a = PA_TABLE_315[3];} -else if (pa > -10 && pa <= 0){a = PA_TABLE_315[4];} -else if (pa > 0 && pa <= 5){a = PA_TABLE_315[5];} -else if (pa > 5 && pa <= 7){a = PA_TABLE_315[6];} -else if (pa > 7){a = PA_TABLE_315[7];} -last_pa = 1; -} -else if (MHz >= 378 && MHz <= 464){ -if (pa <= -30){a = PA_TABLE_433[0];} -else if (pa > -30 && pa <= -20){a = PA_TABLE_433[1];} -else if (pa > -20 && pa <= -15){a = PA_TABLE_433[2];} -else if (pa > -15 && pa <= -10){a = PA_TABLE_433[3];} -else if (pa > -10 && pa <= 0){a = PA_TABLE_433[4];} -else if (pa > 0 && pa <= 5){a = PA_TABLE_433[5];} -else if (pa > 5 && pa <= 7){a = PA_TABLE_433[6];} -else if (pa > 7){a = PA_TABLE_433[7];} -last_pa = 2; -} -else if (MHz >= 779 && MHz <= 899.99){ -if (pa <= -30){a = PA_TABLE_868[0];} -else if (pa > -30 && pa <= -20){a = PA_TABLE_868[1];} -else if (pa > -20 && pa <= -15){a = PA_TABLE_868[2];} -else if (pa > -15 && pa <= -10){a = PA_TABLE_868[3];} -else if (pa > -10 && pa <= -6){a = PA_TABLE_868[4];} -else if (pa > -6 && pa <= 0){a = PA_TABLE_868[5];} -else if (pa > 0 && pa <= 5){a = PA_TABLE_868[6];} -else if (pa > 5 && pa <= 7){a = PA_TABLE_868[7];} -else if (pa > 7 && pa <= 10){a = PA_TABLE_868[8];} -else if (pa > 10){a = PA_TABLE_868[9];} -last_pa = 3; -} -else if (MHz >= 900 && MHz <= 928){ -if (pa <= -30){a = PA_TABLE_915[0];} -else if (pa > -30 && pa <= -20){a = PA_TABLE_915[1];} -else if (pa > -20 && pa <= -15){a = PA_TABLE_915[2];} -else if (pa > -15 && pa <= -10){a = PA_TABLE_915[3];} -else if (pa > -10 && pa <= -6){a = PA_TABLE_915[4];} -else if (pa > -6 && pa <= 0){a = PA_TABLE_915[5];} -else if (pa > 0 && pa <= 5){a = PA_TABLE_915[6];} -else if (pa > 5 && pa <= 7){a = PA_TABLE_915[7];} -else if (pa > 7 && pa <= 10){a = PA_TABLE_915[8];} -else if (pa > 10){a = PA_TABLE_915[9];} -last_pa = 4; -} -if (modulation == 2){ -PA_TABLE[0] = 0; -PA_TABLE[1] = a; -}else{ -PA_TABLE[0] = a; -PA_TABLE[1] = 0; -} -SpiWriteBurstReg(CC1101_PATABLE,PA_TABLE,8); -} -/**************************************************************** -*FUNCTION NAME:Frequency Calculator -*FUNCTION :Calculate the basic frequency. -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setMHZ(float mhz){ -byte freq2 = 0; -byte freq1 = 0; -byte freq0 = 0; + if (MHz >= 300 && MHz <= 348) { + if (pa <= -30) { + a = PA_TABLE_315[0]; + } else if (pa > -30 && pa <= -20) { + a = PA_TABLE_315[1]; + } else if (pa > -20 && pa <= -15) { + a = PA_TABLE_315[2]; + } else if (pa > -15 && pa <= -10) { + a = PA_TABLE_315[3]; + } else if (pa > -10 && pa <= 0) { + a = PA_TABLE_315[4]; + } else if (pa > 0 && pa <= 5) { + a = PA_TABLE_315[5]; + } else if (pa > 5 && pa <= 7) { + a = PA_TABLE_315[6]; + } else if (pa > 7) { + a = PA_TABLE_315[7]; + } + last_pa = 1; + } else if (MHz >= 378 && MHz <= 464) { + if (pa <= -30) { + a = PA_TABLE_433[0]; + } else if (pa > -30 && pa <= -20) { + a = PA_TABLE_433[1]; + } else if (pa > -20 && pa <= -15) { + a = PA_TABLE_433[2]; + } else if (pa > -15 && pa <= -10) { + a = PA_TABLE_433[3]; + } else if (pa > -10 && pa <= 0) { + a = PA_TABLE_433[4]; + } else if (pa > 0 && pa <= 5) { + a = PA_TABLE_433[5]; + } else if (pa > 5 && pa <= 7) { + a = PA_TABLE_433[6]; + } else if (pa > 7) { + a = PA_TABLE_433[7]; + } + last_pa = 2; + } else if (MHz >= 779 && MHz <= 899.99) { + if (pa <= -30) { + a = PA_TABLE_868[0]; + } else if (pa > -30 && pa <= -20) { + a = PA_TABLE_868[1]; + } else if (pa > -20 && pa <= -15) { + a = PA_TABLE_868[2]; + } else if (pa > -15 && pa <= -10) { + a = PA_TABLE_868[3]; + } else if (pa > -10 && pa <= -6) { + a = PA_TABLE_868[4]; + } else if (pa > -6 && pa <= 0) { + a = PA_TABLE_868[5]; + } else if (pa > 0 && pa <= 5) { + a = PA_TABLE_868[6]; + } else if (pa > 5 && pa <= 7) { + a = PA_TABLE_868[7]; + } else if (pa > 7 && pa <= 10) { + a = PA_TABLE_868[8]; + } else if (pa > 10) { + a = PA_TABLE_868[9]; + } + last_pa = 3; + } else if (MHz >= 900 && MHz <= 928) { + if (pa <= -30) { + a = PA_TABLE_915[0]; + } else if (pa > -30 && pa <= -20) { + a = PA_TABLE_915[1]; + } else if (pa > -20 && pa <= -15) { + a = PA_TABLE_915[2]; + } else if (pa > -15 && pa <= -10) { + a = PA_TABLE_915[3]; + } else if (pa > -10 && pa <= -6) { + a = PA_TABLE_915[4]; + } else if (pa > -6 && pa <= 0) { + a = PA_TABLE_915[5]; + } else if (pa > 0 && pa <= 5) { + a = PA_TABLE_915[6]; + } else if (pa > 5 && pa <= 7) { + a = PA_TABLE_915[7]; + } else if (pa > 7 && pa <= 10) { + a = PA_TABLE_915[8]; + } else if (pa > 10) { + a = PA_TABLE_915[9]; + } + last_pa = 4; + } + if (modulation == 2) { + PA_TABLE[0] = 0; + PA_TABLE[1] = a; + } else { + PA_TABLE[0] = a; + PA_TABLE[1] = 0; + } + SpiWriteBurstReg(CC1101_PATABLE, PA_TABLE, 8); +} +/**************************************************************** + *FUNCTION NAME:Frequency Calculator + *FUNCTION :Calculate the basic frequency. + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setMHZ(float mhz) { + byte freq2 = 0; + byte freq1 = 0; + byte freq0 = 0; -MHz = mhz; + MHz = mhz; -for (bool i = 0; i==0;){ -if (mhz >= 26){ -mhz-=26; -freq2+=1; -} -else if (mhz >= 0.1015625){ -mhz-=0.1015625; -freq1+=1; -} -else if (mhz >= 0.00039675){ -mhz-=0.00039675; -freq0+=1; -} -else{i=1;} -} -if (freq0 > 255){freq1+=1;freq0-=256;} + for (bool i = 0; i == 0;) { + if (mhz >= 26) { + mhz -= 26; + freq2 += 1; + } else if (mhz >= 0.1015625) { + mhz -= 0.1015625; + freq1 += 1; + } else if (mhz >= 0.00039675) { + mhz -= 0.00039675; + freq0 += 1; + } else { + i = 1; + } + } + if (freq0 > 255) { + freq1 += 1; + freq0 -= 256; + } -SpiWriteReg(CC1101_FREQ2, freq2); -SpiWriteReg(CC1101_FREQ1, freq1); -SpiWriteReg(CC1101_FREQ0, freq0); + SpiWriteReg(CC1101_FREQ2, freq2); + SpiWriteReg(CC1101_FREQ1, freq1); + SpiWriteReg(CC1101_FREQ0, freq0); -Calibrate(); + Calibrate(); } /**************************************************************** -*FUNCTION NAME:Calibrate -*FUNCTION :Calibrate frequency -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::Calibrate(void){ + *FUNCTION NAME:Calibrate + *FUNCTION :Calibrate frequency + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::Calibrate(void) { + + if (MHz >= 300 && MHz <= 348) { + SpiWriteReg(CC1101_FSCTRL0, map(MHz, 300, 348, clb1[0], clb1[1])); + if (MHz < 322.88) { + SpiWriteReg(CC1101_TEST0, 0x0B); + } else { + SpiWriteReg(CC1101_TEST0, 0x09); + int s = ELECHOUSE_cc1101.SpiReadStatus(CC1101_FSCAL2); + if (s < 32) { + SpiWriteReg(CC1101_FSCAL2, s + 32); + } + if (last_pa != 1) { + setPA(pa); + } + } + } else if (MHz >= 378 && MHz <= 464) { + SpiWriteReg(CC1101_FSCTRL0, map(MHz, 378, 464, clb2[0], clb2[1])); + if (MHz < 430.5) { + SpiWriteReg(CC1101_TEST0, 0x0B); + } else { + SpiWriteReg(CC1101_TEST0, 0x09); + int s = ELECHOUSE_cc1101.SpiReadStatus(CC1101_FSCAL2); + if (s < 32) { + SpiWriteReg(CC1101_FSCAL2, s + 32); + } + if (last_pa != 2) { + setPA(pa); + } + } + } else if (MHz >= 779 && MHz <= 899.99) { + SpiWriteReg(CC1101_FSCTRL0, map(MHz, 779, 899, clb3[0], clb3[1])); + if (MHz < 861) { + SpiWriteReg(CC1101_TEST0, 0x0B); + } else { + SpiWriteReg(CC1101_TEST0, 0x09); + int s = ELECHOUSE_cc1101.SpiReadStatus(CC1101_FSCAL2); + if (s < 32) { + SpiWriteReg(CC1101_FSCAL2, s + 32); + } + if (last_pa != 3) { + setPA(pa); + } + } + } else if (MHz >= 900 && MHz <= 928) { + SpiWriteReg(CC1101_FSCTRL0, map(MHz, 900, 928, clb4[0], clb4[1])); + SpiWriteReg(CC1101_TEST0, 0x09); + int s = ELECHOUSE_cc1101.SpiReadStatus(CC1101_FSCAL2); + if (s < 32) { + SpiWriteReg(CC1101_FSCAL2, s + 32); + } + if (last_pa != 4) { + setPA(pa); + } + } +} +/**************************************************************** + *FUNCTION NAME:Calibration offset + *FUNCTION :Set calibration offset + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setClb(byte b, byte s, byte e) { + if (b == 1) { + clb1[0] = s; + clb1[1] = e; + } else if (b == 2) { + clb2[0] = s; + clb2[1] = e; + } else if (b == 3) { + clb3[0] = s; + clb3[1] = e; + } else if (b == 4) { + clb4[0] = s; + clb4[1] = e; + } +} +/**************************************************************** + *FUNCTION NAME:getCC1101 + *FUNCTION :Test Spi connection and return 1 when true. + *INPUT :none + *OUTPUT :none + ****************************************************************/ +bool ELECHOUSE_CC1101::getCC1101(void) { + setSpi(); + if (SpiReadStatus(0x31) > 0) { + return 1; + } else { + return 0; + } +} +/**************************************************************** + *FUNCTION NAME:getMode + *FUNCTION :Return the Mode. Sidle = 0, TX = 1, Rx = 2. + *INPUT :none + *OUTPUT :none + ****************************************************************/ +byte ELECHOUSE_CC1101::getMode(void) { return trxstate; } +/**************************************************************** + *FUNCTION NAME:Set Sync_Word + *FUNCTION :Sync Word + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setSyncWord(byte sh, byte sl) { + SpiWriteReg(CC1101_SYNC1, sh); + SpiWriteReg(CC1101_SYNC0, sl); +} +/**************************************************************** + *FUNCTION NAME:Set ADDR + *FUNCTION :Address used for packet filtration. Optional broadcast addresses + * are 0 (0x00) and 255 (0xFF). INPUT :none OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setAddr(byte v) { SpiWriteReg(CC1101_ADDR, v); } +/**************************************************************** + *FUNCTION NAME:Set PQT + *FUNCTION :Preamble quality estimator threshold + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setPQT(byte v) { + Split_PKTCTRL1(); + pc1PQT = 0; + if (v > 7) { + v = 7; + } + pc1PQT = v * 32; + SpiWriteReg(CC1101_PKTCTRL1, pc1PQT + pc1CRC_AF + pc1APP_ST + pc1ADRCHK); +} +/**************************************************************** + *FUNCTION NAME:Set CRC_AUTOFLUSH + *FUNCTION :Enable automatic flush of RX FIFO when CRC is not OK + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setCRC_AF(bool v) { + Split_PKTCTRL1(); + pc1CRC_AF = 0; + if (v == 1) { + pc1CRC_AF = 8; + } + SpiWriteReg(CC1101_PKTCTRL1, pc1PQT + pc1CRC_AF + pc1APP_ST + pc1ADRCHK); +} +/**************************************************************** + *FUNCTION NAME:Set APPEND_STATUS + *FUNCTION :When enabled, two status bytes will be appended to the payload + * of the packet INPUT :none OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setAppendStatus(bool v) { + Split_PKTCTRL1(); + pc1APP_ST = 0; + if (v == 1) { + pc1APP_ST = 4; + } + SpiWriteReg(CC1101_PKTCTRL1, pc1PQT + pc1CRC_AF + pc1APP_ST + pc1ADRCHK); +} +/**************************************************************** + *FUNCTION NAME:Set ADR_CHK + *FUNCTION :Controls address check configuration of received packages + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setAdrChk(byte v) { + Split_PKTCTRL1(); + pc1ADRCHK = 0; + if (v > 3) { + v = 3; + } + pc1ADRCHK = v; + SpiWriteReg(CC1101_PKTCTRL1, pc1PQT + pc1CRC_AF + pc1APP_ST + pc1ADRCHK); +} +/**************************************************************** + *FUNCTION NAME:Set WHITE_DATA + *FUNCTION :Turn data whitening on / off. + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setWhiteData(bool v) { + Split_PKTCTRL0(); + pc0WDATA = 0; + if (v == 1) { + pc0WDATA = 64; + } + SpiWriteReg(CC1101_PKTCTRL0, + pc0WDATA + pc0PktForm + pc0CRC_EN + pc0LenConf); +} +/**************************************************************** + *FUNCTION NAME:Set PKT_FORMAT + *FUNCTION :Format of RX and TX data + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setPktFormat(byte v) { + Split_PKTCTRL0(); + pc0PktForm = 0; + if (v > 3) { + v = 3; + } + pc0PktForm = v * 16; + SpiWriteReg(CC1101_PKTCTRL0, + pc0WDATA + pc0PktForm + pc0CRC_EN + pc0LenConf); +} +/**************************************************************** + *FUNCTION NAME:Set CRC + *FUNCTION :CRC calculation in TX and CRC check in RX + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setCrc(bool v) { + Split_PKTCTRL0(); + pc0CRC_EN = 0; + if (v == 1) { + pc0CRC_EN = 4; + } + SpiWriteReg(CC1101_PKTCTRL0, + pc0WDATA + pc0PktForm + pc0CRC_EN + pc0LenConf); +} +/**************************************************************** + *FUNCTION NAME:Set LENGTH_CONFIG + *FUNCTION :Configure the packet length + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setLengthConfig(byte v) { + Split_PKTCTRL0(); + pc0LenConf = 0; + if (v > 3) { + v = 3; + } + pc0LenConf = v; + SpiWriteReg(CC1101_PKTCTRL0, + pc0WDATA + pc0PktForm + pc0CRC_EN + pc0LenConf); +} +/**************************************************************** + *FUNCTION NAME:Set PACKET_LENGTH + *FUNCTION :Indicates the packet length + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setPacketLength(byte v) { + SpiWriteReg(CC1101_PKTLEN, v); +} +/**************************************************************** + *FUNCTION NAME:Set DCFILT_OFF + *FUNCTION :Disable digital DC blocking filter before demodulator + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setDcFilterOff(bool v) { + Split_MDMCFG2(); + m2DCOFF = 0; + if (v == 1) { + m2DCOFF = 128; + } + SpiWriteReg(CC1101_MDMCFG2, m2DCOFF + m2MODFM + m2MANCH + m2SYNCM); +} +/**************************************************************** + *FUNCTION NAME:Set MANCHESTER + *FUNCTION :Enables Manchester encoding/decoding + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setManchester(bool v) { + Split_MDMCFG2(); + m2MANCH = 0; + if (v == 1) { + m2MANCH = 8; + } + SpiWriteReg(CC1101_MDMCFG2, m2DCOFF + m2MODFM + m2MANCH + m2SYNCM); +} +/**************************************************************** + *FUNCTION NAME:Set SYNC_MODE + *FUNCTION :Combined sync-word qualifier mode + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setSyncMode(byte v) { + Split_MDMCFG2(); + m2SYNCM = 0; + if (v > 7) { + v = 7; + } + m2SYNCM = v; + SpiWriteReg(CC1101_MDMCFG2, m2DCOFF + m2MODFM + m2MANCH + m2SYNCM); +} +/**************************************************************** + *FUNCTION NAME:Set FEC + *FUNCTION :Enable Forward Error Correction (FEC) + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setFEC(bool v) { + Split_MDMCFG1(); + m1FEC = 0; + if (v == 1) { + m1FEC = 128; + } + SpiWriteReg(CC1101_MDMCFG1, m1FEC + m1PRE + m1CHSP); +} +/**************************************************************** + *FUNCTION NAME:Set PRE + *FUNCTION :Sets the minimum number of preamble bytes to be transmitted. + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setPRE(byte v) { + Split_MDMCFG1(); + m1PRE = 0; + if (v > 7) { + v = 7; + } + m1PRE = v * 16; + SpiWriteReg(CC1101_MDMCFG1, m1FEC + m1PRE + m1CHSP); +} +/**************************************************************** + *FUNCTION NAME:Set Channel + *FUNCTION :none + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setChannel(byte ch) { + chan = ch; + SpiWriteReg(CC1101_CHANNR, chan); +} +/**************************************************************** + *FUNCTION NAME:Set Channel spacing + *FUNCTION :none + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setChsp(float f) { + Split_MDMCFG1(); + byte MDMCFG0 = 0; + m1CHSP = 0; + if (f > 405.456543) { + f = 405.456543; + } + if (f < 25.390625) { + f = 25.390625; + } + for (int i = 0; i < 5; i++) { + if (f <= 50.682068) { + f -= 25.390625; + f /= 0.0991825; + MDMCFG0 = f; + float s1 = (f - MDMCFG0) * 10; + if (s1 >= 5) { + MDMCFG0++; + } + i = 5; + } else { + m1CHSP++; + f /= 2; + } + } + SpiWriteReg(19, m1CHSP + m1FEC + m1PRE); + SpiWriteReg(20, MDMCFG0); +} +/**************************************************************** + *FUNCTION NAME:Set Receive bandwidth + *FUNCTION :none + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setRxBW(float f) { + Split_MDMCFG4(); + int s1 = 3; + int s2 = 3; + for (int i = 0; i < 3; i++) { + if (f > 101.5625) { + f /= 2; + s1--; + } else { + i = 3; + } + } + for (int i = 0; i < 3; i++) { + if (f > 58.1) { + f /= 1.25; + s2--; + } else { + i = 3; + } + } + s1 *= 64; + s2 *= 16; + m4RxBw = s1 + s2; + SpiWriteReg(16, m4RxBw + m4DaRa); +} +/**************************************************************** + *FUNCTION NAME:Set Data Rate + *FUNCTION :none + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setDRate(float d) { + Split_MDMCFG4(); + float c = d; + byte MDMCFG3 = 0; + if (c > 1621.83) { + c = 1621.83; + } + if (c < 0.0247955) { + c = 0.0247955; + } + m4DaRa = 0; + for (int i = 0; i < 20; i++) { + if (c <= 0.0494942) { + c = c - 0.0247955; + c = c / 0.00009685; + MDMCFG3 = c; + float s1 = (c - MDMCFG3) * 10; + if (s1 >= 5) { + MDMCFG3++; + } + i = 20; + } else { + m4DaRa++; + c = c / 2; + } + } + SpiWriteReg(16, m4RxBw + m4DaRa); + SpiWriteReg(17, MDMCFG3); +} +/**************************************************************** + *FUNCTION NAME:Set Devitation + *FUNCTION :none + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setDeviation(float d) { + float f = 1.586914; + float v = 0.19836425; + int c = 0; + if (d > 380.859375) { + d = 380.859375; + } + if (d < 1.586914) { + d = 1.586914; + } + for (int i = 0; i < 255; i++) { + f += v; + if (c == 7) { + v *= 2; + c = -1; + i += 8; + } + if (f >= d) { + c = i; + i = 255; + } + c++; + } + SpiWriteReg(21, c); +} +/**************************************************************** + *FUNCTION NAME:Split PKTCTRL0 + *FUNCTION :none + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::Split_PKTCTRL1(void) { + int calc = SpiReadStatus(7); + pc1PQT = 0; + pc1CRC_AF = 0; + pc1APP_ST = 0; + pc1ADRCHK = 0; + for (bool i = 0; i == 0;) { + if (calc >= 32) { + calc -= 32; + pc1PQT += 32; + } else if (calc >= 8) { + calc -= 8; + pc1CRC_AF += 8; + } else if (calc >= 4) { + calc -= 4; + pc1APP_ST += 4; + } else { + pc1ADRCHK = calc; + i = 1; + } + } +} +/**************************************************************** + *FUNCTION NAME:Split PKTCTRL0 + *FUNCTION :none + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::Split_PKTCTRL0(void) { + int calc = SpiReadStatus(8); + pc0WDATA = 0; + pc0PktForm = 0; + pc0CRC_EN = 0; + pc0LenConf = 0; + for (bool i = 0; i == 0;) { + if (calc >= 64) { + calc -= 64; + pc0WDATA += 64; + } else if (calc >= 16) { + calc -= 16; + pc0PktForm += 16; + } else if (calc >= 4) { + calc -= 4; + pc0CRC_EN += 4; + } else { + pc0LenConf = calc; + i = 1; + } + } +} +/**************************************************************** + *FUNCTION NAME:Split MDMCFG1 + *FUNCTION :none + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::Split_MDMCFG1(void) { + int calc = SpiReadStatus(19); + m1FEC = 0; + m1PRE = 0; + m1CHSP = 0; + int s2 = 0; + for (bool i = 0; i == 0;) { + if (calc >= 128) { + calc -= 128; + m1FEC += 128; + } else if (calc >= 16) { + calc -= 16; + m1PRE += 16; + } else { + m1CHSP = calc; + i = 1; + } + } +} +/**************************************************************** + *FUNCTION NAME:Split MDMCFG2 + *FUNCTION :none + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::Split_MDMCFG2(void) { + int calc = SpiReadStatus(18); + m2DCOFF = 0; + m2MODFM = 0; + m2MANCH = 0; + m2SYNCM = 0; + for (bool i = 0; i == 0;) { + if (calc >= 128) { + calc -= 128; + m2DCOFF += 128; + } else if (calc >= 16) { + calc -= 16; + m2MODFM += 16; + } else if (calc >= 8) { + calc -= 8; + m2MANCH += 8; + } else { + m2SYNCM = calc; + i = 1; + } + } +} +/**************************************************************** + *FUNCTION NAME:Split MDMCFG4 + *FUNCTION :none + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::Split_MDMCFG4(void) { + int calc = SpiReadStatus(16); + m4RxBw = 0; + m4DaRa = 0; + for (bool i = 0; i == 0;) { + if (calc >= 64) { + calc -= 64; + m4RxBw += 64; + } else if (calc >= 16) { + calc -= 16; + m4RxBw += 16; + } else { + m4DaRa = calc; + i = 1; + } + } +} +/**************************************************************** + *FUNCTION NAME:RegConfigSettings + *FUNCTION :CC1101 register config //details refer datasheet of + * CC1101/CC1100// INPUT :none OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::RegConfigSettings(void) { + SpiWriteReg(CC1101_FSCTRL1, 0x06); -if (MHz >= 300 && MHz <= 348){ -SpiWriteReg(CC1101_FSCTRL0, map(MHz, 300, 348, clb1[0], clb1[1])); -if (MHz < 322.88){SpiWriteReg(CC1101_TEST0,0x0B);} -else{ -SpiWriteReg(CC1101_TEST0,0x09); -int s = ELECHOUSE_cc1101.SpiReadStatus(CC1101_FSCAL2); -if (s<32){SpiWriteReg(CC1101_FSCAL2, s+32);} -if (last_pa != 1){setPA(pa);} -} -} -else if (MHz >= 378 && MHz <= 464){ -SpiWriteReg(CC1101_FSCTRL0, map(MHz, 378, 464, clb2[0], clb2[1])); -if (MHz < 430.5){SpiWriteReg(CC1101_TEST0,0x0B);} -else{ -SpiWriteReg(CC1101_TEST0,0x09); -int s = ELECHOUSE_cc1101.SpiReadStatus(CC1101_FSCAL2); -if (s<32){SpiWriteReg(CC1101_FSCAL2, s+32);} -if (last_pa != 2){setPA(pa);} -} -} -else if (MHz >= 779 && MHz <= 899.99){ -SpiWriteReg(CC1101_FSCTRL0, map(MHz, 779, 899, clb3[0], clb3[1])); -if (MHz < 861){SpiWriteReg(CC1101_TEST0,0x0B);} -else{ -SpiWriteReg(CC1101_TEST0,0x09); -int s = ELECHOUSE_cc1101.SpiReadStatus(CC1101_FSCAL2); -if (s<32){SpiWriteReg(CC1101_FSCAL2, s+32);} -if (last_pa != 3){setPA(pa);} -} -} -else if (MHz >= 900 && MHz <= 928){ -SpiWriteReg(CC1101_FSCTRL0, map(MHz, 900, 928, clb4[0], clb4[1])); -SpiWriteReg(CC1101_TEST0,0x09); -int s = ELECHOUSE_cc1101.SpiReadStatus(CC1101_FSCAL2); -if (s<32){SpiWriteReg(CC1101_FSCAL2, s+32);} -if (last_pa != 4){setPA(pa);} -} -} -/**************************************************************** -*FUNCTION NAME:Calibration offset -*FUNCTION :Set calibration offset -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setClb(byte b, byte s, byte e){ -if (b == 1){ -clb1[0]=s; -clb1[1]=e; -} -else if (b == 2){ -clb2[0]=s; -clb2[1]=e; -} -else if (b == 3){ -clb3[0]=s; -clb3[1]=e; -} -else if (b == 4){ -clb4[0]=s; -clb4[1]=e; -} -} -/**************************************************************** -*FUNCTION NAME:getCC1101 -*FUNCTION :Test Spi connection and return 1 when true. -*INPUT :none -*OUTPUT :none -****************************************************************/ -bool ELECHOUSE_CC1101::getCC1101(void){ -setSpi(); -if (SpiReadStatus(0x31)>0){ -return 1; -}else{ -return 0; -} -} -/**************************************************************** -*FUNCTION NAME:getMode -*FUNCTION :Return the Mode. Sidle = 0, TX = 1, Rx = 2. -*INPUT :none -*OUTPUT :none -****************************************************************/ -byte ELECHOUSE_CC1101::getMode(void){ -return trxstate; -} -/**************************************************************** -*FUNCTION NAME:Set Sync_Word -*FUNCTION :Sync Word -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setSyncWord(byte sh, byte sl){ -SpiWriteReg(CC1101_SYNC1, sh); -SpiWriteReg(CC1101_SYNC0, sl); -} -/**************************************************************** -*FUNCTION NAME:Set ADDR -*FUNCTION :Address used for packet filtration. Optional broadcast addresses are 0 (0x00) and 255 (0xFF). -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setAddr(byte v){ -SpiWriteReg(CC1101_ADDR, v); -} -/**************************************************************** -*FUNCTION NAME:Set PQT -*FUNCTION :Preamble quality estimator threshold -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setPQT(byte v){ -Split_PKTCTRL1(); -pc1PQT = 0; -if (v>7){v=7;} -pc1PQT = v*32; -SpiWriteReg(CC1101_PKTCTRL1, pc1PQT+pc1CRC_AF+pc1APP_ST+pc1ADRCHK); -} -/**************************************************************** -*FUNCTION NAME:Set CRC_AUTOFLUSH -*FUNCTION :Enable automatic flush of RX FIFO when CRC is not OK -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setCRC_AF(bool v){ -Split_PKTCTRL1(); -pc1CRC_AF = 0; -if (v==1){pc1CRC_AF=8;} -SpiWriteReg(CC1101_PKTCTRL1, pc1PQT+pc1CRC_AF+pc1APP_ST+pc1ADRCHK); -} -/**************************************************************** -*FUNCTION NAME:Set APPEND_STATUS -*FUNCTION :When enabled, two status bytes will be appended to the payload of the packet -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setAppendStatus(bool v){ -Split_PKTCTRL1(); -pc1APP_ST = 0; -if (v==1){pc1APP_ST=4;} -SpiWriteReg(CC1101_PKTCTRL1, pc1PQT+pc1CRC_AF+pc1APP_ST+pc1ADRCHK); -} -/**************************************************************** -*FUNCTION NAME:Set ADR_CHK -*FUNCTION :Controls address check configuration of received packages -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setAdrChk(byte v){ -Split_PKTCTRL1(); -pc1ADRCHK = 0; -if (v>3){v=3;} -pc1ADRCHK = v; -SpiWriteReg(CC1101_PKTCTRL1, pc1PQT+pc1CRC_AF+pc1APP_ST+pc1ADRCHK); -} -/**************************************************************** -*FUNCTION NAME:Set WHITE_DATA -*FUNCTION :Turn data whitening on / off. -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setWhiteData(bool v){ -Split_PKTCTRL0(); -pc0WDATA = 0; -if (v == 1){pc0WDATA=64;} -SpiWriteReg(CC1101_PKTCTRL0, pc0WDATA+pc0PktForm+pc0CRC_EN+pc0LenConf); -} -/**************************************************************** -*FUNCTION NAME:Set PKT_FORMAT -*FUNCTION :Format of RX and TX data -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setPktFormat(byte v){ -Split_PKTCTRL0(); -pc0PktForm = 0; -if (v>3){v=3;} -pc0PktForm = v*16; -SpiWriteReg(CC1101_PKTCTRL0, pc0WDATA+pc0PktForm+pc0CRC_EN+pc0LenConf); -} -/**************************************************************** -*FUNCTION NAME:Set CRC -*FUNCTION :CRC calculation in TX and CRC check in RX -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setCrc(bool v){ -Split_PKTCTRL0(); -pc0CRC_EN = 0; -if (v==1){pc0CRC_EN=4;} -SpiWriteReg(CC1101_PKTCTRL0, pc0WDATA+pc0PktForm+pc0CRC_EN+pc0LenConf); -} -/**************************************************************** -*FUNCTION NAME:Set LENGTH_CONFIG -*FUNCTION :Configure the packet length -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setLengthConfig(byte v){ -Split_PKTCTRL0(); -pc0LenConf = 0; -if (v>3){v=3;} -pc0LenConf = v; -SpiWriteReg(CC1101_PKTCTRL0, pc0WDATA+pc0PktForm+pc0CRC_EN+pc0LenConf); -} -/**************************************************************** -*FUNCTION NAME:Set PACKET_LENGTH -*FUNCTION :Indicates the packet length -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setPacketLength(byte v){ -SpiWriteReg(CC1101_PKTLEN, v); -} -/**************************************************************** -*FUNCTION NAME:Set DCFILT_OFF -*FUNCTION :Disable digital DC blocking filter before demodulator -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setDcFilterOff(bool v){ -Split_MDMCFG2(); -m2DCOFF = 0; -if (v==1){m2DCOFF=128;} -SpiWriteReg(CC1101_MDMCFG2, m2DCOFF+m2MODFM+m2MANCH+m2SYNCM); -} -/**************************************************************** -*FUNCTION NAME:Set MANCHESTER -*FUNCTION :Enables Manchester encoding/decoding -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setManchester(bool v){ -Split_MDMCFG2(); -m2MANCH = 0; -if (v==1){m2MANCH=8;} -SpiWriteReg(CC1101_MDMCFG2, m2DCOFF+m2MODFM+m2MANCH+m2SYNCM); -} -/**************************************************************** -*FUNCTION NAME:Set SYNC_MODE -*FUNCTION :Combined sync-word qualifier mode -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setSyncMode(byte v){ -Split_MDMCFG2(); -m2SYNCM = 0; -if (v>7){v=7;} -m2SYNCM=v; -SpiWriteReg(CC1101_MDMCFG2, m2DCOFF+m2MODFM+m2MANCH+m2SYNCM); -} -/**************************************************************** -*FUNCTION NAME:Set FEC -*FUNCTION :Enable Forward Error Correction (FEC) -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setFEC(bool v){ -Split_MDMCFG1(); -m1FEC=0; -if (v==1){m1FEC=128;} -SpiWriteReg(CC1101_MDMCFG1, m1FEC+m1PRE+m1CHSP); -} -/**************************************************************** -*FUNCTION NAME:Set PRE -*FUNCTION :Sets the minimum number of preamble bytes to be transmitted. -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setPRE(byte v){ -Split_MDMCFG1(); -m1PRE=0; -if (v>7){v=7;} -m1PRE = v*16; -SpiWriteReg(CC1101_MDMCFG1, m1FEC+m1PRE+m1CHSP); -} -/**************************************************************** -*FUNCTION NAME:Set Channel -*FUNCTION :none -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setChannel(byte ch){ -chan = ch; -SpiWriteReg(CC1101_CHANNR, chan); -} -/**************************************************************** -*FUNCTION NAME:Set Channel spacing -*FUNCTION :none -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setChsp(float f){ -Split_MDMCFG1(); -byte MDMCFG0 = 0; -m1CHSP = 0; -if (f > 405.456543){f = 405.456543;} -if (f < 25.390625){f = 25.390625;} -for (int i = 0; i<5; i++){ -if (f <= 50.682068){ -f -= 25.390625; -f /= 0.0991825; -MDMCFG0 = f; -float s1 = (f - MDMCFG0) *10; -if (s1 >= 5){MDMCFG0++;} -i = 5; -}else{ -m1CHSP++; -f/=2; -} -} -SpiWriteReg(19,m1CHSP+m1FEC+m1PRE); -SpiWriteReg(20,MDMCFG0); -} -/**************************************************************** -*FUNCTION NAME:Set Receive bandwidth -*FUNCTION :none -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setRxBW(float f){ -Split_MDMCFG4(); -int s1 = 3; -int s2 = 3; -for (int i = 0; i<3; i++){ -if (f > 101.5625){f/=2; s1--;} -else{i=3;} -} -for (int i = 0; i<3; i++){ -if (f > 58.1){f/=1.25; s2--;} -else{i=3;} -} -s1 *= 64; -s2 *= 16; -m4RxBw = s1 + s2; -SpiWriteReg(16,m4RxBw+m4DaRa); -} -/**************************************************************** -*FUNCTION NAME:Set Data Rate -*FUNCTION :none -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setDRate(float d){ -Split_MDMCFG4(); -float c = d; -byte MDMCFG3 = 0; -if (c > 1621.83){c = 1621.83;} -if (c < 0.0247955){c = 0.0247955;} -m4DaRa = 0; -for (int i = 0; i<20; i++){ -if (c <= 0.0494942){ -c = c - 0.0247955; -c = c / 0.00009685; -MDMCFG3 = c; -float s1 = (c - MDMCFG3) *10; -if (s1 >= 5){MDMCFG3++;} -i = 20; -}else{ -m4DaRa++; -c = c/2; -} -} -SpiWriteReg(16, m4RxBw+m4DaRa); -SpiWriteReg(17, MDMCFG3); -} -/**************************************************************** -*FUNCTION NAME:Set Devitation -*FUNCTION :none -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setDeviation(float d){ -float f = 1.586914; -float v = 0.19836425; -int c = 0; -if (d > 380.859375){d = 380.859375;} -if (d < 1.586914){d = 1.586914;} -for (int i = 0; i<255; i++){ -f+=v; -if (c==7){v*=2;c=-1;i+=8;} -if (f>=d){c=i;i=255;} -c++; -} -SpiWriteReg(21,c); -} -/**************************************************************** -*FUNCTION NAME:Split PKTCTRL0 -*FUNCTION :none -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::Split_PKTCTRL1(void){ -int calc = SpiReadStatus(7); -pc1PQT = 0; -pc1CRC_AF = 0; -pc1APP_ST = 0; -pc1ADRCHK = 0; -for (bool i = 0; i==0;){ -if (calc >= 32){calc-=32; pc1PQT+=32;} -else if (calc >= 8){calc-=8; pc1CRC_AF+=8;} -else if (calc >= 4){calc-=4; pc1APP_ST+=4;} -else {pc1ADRCHK = calc; i=1;} -} -} -/**************************************************************** -*FUNCTION NAME:Split PKTCTRL0 -*FUNCTION :none -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::Split_PKTCTRL0(void){ -int calc = SpiReadStatus(8); -pc0WDATA = 0; -pc0PktForm = 0; -pc0CRC_EN = 0; -pc0LenConf = 0; -for (bool i = 0; i==0;){ -if (calc >= 64){calc-=64; pc0WDATA+=64;} -else if (calc >= 16){calc-=16; pc0PktForm+=16;} -else if (calc >= 4){calc-=4; pc0CRC_EN+=4;} -else {pc0LenConf = calc; i=1;} -} -} -/**************************************************************** -*FUNCTION NAME:Split MDMCFG1 -*FUNCTION :none -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::Split_MDMCFG1(void){ -int calc = SpiReadStatus(19); -m1FEC = 0; -m1PRE = 0; -m1CHSP = 0; -int s2 = 0; -for (bool i = 0; i==0;){ -if (calc >= 128){calc-=128; m1FEC+=128;} -else if (calc >= 16){calc-=16; m1PRE+=16;} -else {m1CHSP = calc; i=1;} -} -} -/**************************************************************** -*FUNCTION NAME:Split MDMCFG2 -*FUNCTION :none -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::Split_MDMCFG2(void){ -int calc = SpiReadStatus(18); -m2DCOFF = 0; -m2MODFM = 0; -m2MANCH = 0; -m2SYNCM = 0; -for (bool i = 0; i==0;){ -if (calc >= 128){calc-=128; m2DCOFF+=128;} -else if (calc >= 16){calc-=16; m2MODFM+=16;} -else if (calc >= 8){calc-=8; m2MANCH+=8;} -else{m2SYNCM = calc; i=1;} -} -} -/**************************************************************** -*FUNCTION NAME:Split MDMCFG4 -*FUNCTION :none -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::Split_MDMCFG4(void){ -int calc = SpiReadStatus(16); -m4RxBw = 0; -m4DaRa = 0; -for (bool i = 0; i==0;){ -if (calc >= 64){calc-=64; m4RxBw+=64;} -else if (calc >= 16){calc -= 16; m4RxBw+=16;} -else{m4DaRa = calc; i=1;} -} -} -/**************************************************************** -*FUNCTION NAME:RegConfigSettings -*FUNCTION :CC1101 register config //details refer datasheet of CC1101/CC1100// -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::RegConfigSettings(void) -{ - SpiWriteReg(CC1101_FSCTRL1, 0x06); - setCCMode(ccmode); setMHZ(MHz); - - SpiWriteReg(CC1101_MDMCFG1, 0x02); - SpiWriteReg(CC1101_MDMCFG0, 0xF8); - SpiWriteReg(CC1101_CHANNR, chan); - SpiWriteReg(CC1101_DEVIATN, 0x47); - SpiWriteReg(CC1101_FREND1, 0x56); - SpiWriteReg(CC1101_MCSM0 , 0x18); - SpiWriteReg(CC1101_FOCCFG, 0x16); - SpiWriteReg(CC1101_BSCFG, 0x1C); + + SpiWriteReg(CC1101_MDMCFG1, 0x02); + SpiWriteReg(CC1101_MDMCFG0, 0xF8); + SpiWriteReg(CC1101_CHANNR, chan); + SpiWriteReg(CC1101_DEVIATN, 0x47); + SpiWriteReg(CC1101_FREND1, 0x56); + SpiWriteReg(CC1101_MCSM0, 0x18); + SpiWriteReg(CC1101_FOCCFG, 0x16); + SpiWriteReg(CC1101_BSCFG, 0x1C); SpiWriteReg(CC1101_AGCCTRL2, 0xC7); SpiWriteReg(CC1101_AGCCTRL1, 0x00); SpiWriteReg(CC1101_AGCCTRL0, 0xB2); - SpiWriteReg(CC1101_FSCAL3, 0xE9); - SpiWriteReg(CC1101_FSCAL2, 0x2A); - SpiWriteReg(CC1101_FSCAL1, 0x00); - SpiWriteReg(CC1101_FSCAL0, 0x1F); - SpiWriteReg(CC1101_FSTEST, 0x59); - SpiWriteReg(CC1101_TEST2, 0x81); - SpiWriteReg(CC1101_TEST1, 0x35); - SpiWriteReg(CC1101_TEST0, 0x09); + SpiWriteReg(CC1101_FSCAL3, 0xE9); + SpiWriteReg(CC1101_FSCAL2, 0x2A); + SpiWriteReg(CC1101_FSCAL1, 0x00); + SpiWriteReg(CC1101_FSCAL0, 0x1F); + SpiWriteReg(CC1101_FSTEST, 0x59); + SpiWriteReg(CC1101_TEST2, 0x81); + SpiWriteReg(CC1101_TEST1, 0x35); + SpiWriteReg(CC1101_TEST0, 0x09); SpiWriteReg(CC1101_PKTCTRL1, 0x04); - SpiWriteReg(CC1101_ADDR, 0x00); - SpiWriteReg(CC1101_PKTLEN, 0x00); -} -/**************************************************************** -*FUNCTION NAME:SetTx -*FUNCTION :set CC1101 send data -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::SetTx(void) -{ - SpiStrobe(CC1101_SIDLE); - SpiStrobe(CC1101_STX); //start send - trxstate=1; -} -/**************************************************************** -*FUNCTION NAME:SetRx -*FUNCTION :set CC1101 to receive state -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::SetRx(void) -{ - SpiStrobe(CC1101_SIDLE); - SpiStrobe(CC1101_SRX); //start receive - trxstate=2; -} -/**************************************************************** -*FUNCTION NAME:SetTx -*FUNCTION :set CC1101 send data and change frequency -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::SetTx(float mhz) -{ - SpiStrobe(CC1101_SIDLE); - setMHZ(mhz); - SpiStrobe(CC1101_STX); //start send - trxstate=1; -} -/**************************************************************** -*FUNCTION NAME:SetRx -*FUNCTION :set CC1101 to receive state and change frequency -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::SetRx(float mhz) -{ - SpiStrobe(CC1101_SIDLE); - setMHZ(mhz); - SpiStrobe(CC1101_SRX); //start receive - trxstate=2; -} -/**************************************************************** -*FUNCTION NAME:RSSI Level -*FUNCTION :Calculating the RSSI Level -*INPUT :none -*OUTPUT :none -****************************************************************/ -int ELECHOUSE_CC1101::getRssi(void) -{ -int rssi; -rssi=SpiReadStatus(CC1101_RSSI); -if (rssi >= 128){rssi = (rssi-256)/2-74;} -else{rssi = (rssi/2)-74;} -return rssi; -} -/**************************************************************** -*FUNCTION NAME:LQI Level -*FUNCTION :get Lqi state -*INPUT :none -*OUTPUT :none -****************************************************************/ -byte ELECHOUSE_CC1101::getLqi(void) -{ -byte lqi; -lqi=SpiReadStatus(CC1101_LQI); -return lqi; -} -/**************************************************************** -*FUNCTION NAME:SetSres -*FUNCTION :Reset CC1101 -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setSres(void) -{ - SpiStrobe(CC1101_SRES); - trxstate=0; -} -/**************************************************************** -*FUNCTION NAME:setSidle -*FUNCTION :set Rx / TX Off -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::setSidle(void) -{ - SpiStrobe(CC1101_SIDLE); - trxstate=0; -} -/**************************************************************** -*FUNCTION NAME:goSleep -*FUNCTION :set cc1101 Sleep on -*INPUT :none -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::goSleep(void){ - trxstate=0; - SpiStrobe(0x36);//Exit RX / TX, turn off frequency synthesizer and exit - SpiStrobe(0x39);//Enter power down mode when CSn goes high. -} -/**************************************************************** -*FUNCTION NAME:Char direct SendData -*FUNCTION :use CC1101 send data -*INPUT :txBuffer: data array to send; size: number of data to send, no more than 61 -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::SendData(char *txchar) -{ -int len = strlen(txchar); -byte chartobyte[len]; -for (int i = 0; i sync transmitted - while (digitalRead(GDO0)); // Wait for GDO0 to be cleared -> end of packet - SpiStrobe(CC1101_SFTX); //flush TXfifo - trxstate=1; -} -/**************************************************************** -*FUNCTION NAME:Char direct SendData -*FUNCTION :use CC1101 send data without GDO -*INPUT :txBuffer: data array to send; size: number of data to send, no more than 61 -*OUTPUT :none -****************************************************************/ -void ELECHOUSE_CC1101::SendData(char *txchar,int t) -{ -int len = strlen(txchar); -byte chartobyte[len]; -for (int i = 0; i= 128) { + rssi = (rssi - 256) / 2 - 74; + } else { + rssi = (rssi / 2) - 74; + } + return rssi; +} +/**************************************************************** + *FUNCTION NAME:LQI Level + *FUNCTION :get Lqi state + *INPUT :none + *OUTPUT :none + ****************************************************************/ +byte ELECHOUSE_CC1101::getLqi(void) { + byte lqi; + lqi = SpiReadStatus(CC1101_LQI); + return lqi; +} +/**************************************************************** + *FUNCTION NAME:SetSres + *FUNCTION :Reset CC1101 + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setSres(void) { + SpiStrobe(CC1101_SRES); + trxstate = 0; +} +/**************************************************************** + *FUNCTION NAME:setSidle + *FUNCTION :set Rx / TX Off + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::setSidle(void) { + SpiStrobe(CC1101_SIDLE); + trxstate = 0; +} +/**************************************************************** + *FUNCTION NAME:goSleep + *FUNCTION :set cc1101 Sleep on + *INPUT :none + *OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::goSleep(void) { + trxstate = 0; + SpiStrobe(0x36); // Exit RX / TX, turn off frequency synthesizer and exit + SpiStrobe(0x39); // Enter power down mode when CSn goes high. +} +/**************************************************************** + *FUNCTION NAME:Char direct SendData + *FUNCTION :use CC1101 send data + *INPUT :txBuffer: data array to send; size: number of data to send, no + * more than 61 OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::SendData(char *txchar) { + int len = strlen(txchar); + byte chartobyte[len]; + for (int i = 0; i < len; i++) { + chartobyte[i] = txchar[i]; + } + SendData(chartobyte, len); +} +/**************************************************************** + *FUNCTION NAME:SendData + *FUNCTION :use CC1101 send data + *INPUT :txBuffer: data array to send; size: number of data to send, no + * more than 61 OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::SendData(byte *txBuffer, byte size) { + SpiWriteReg(CC1101_TXFIFO, size); + SpiWriteBurstReg(CC1101_TXFIFO, txBuffer, size); // write data to send + SpiStrobe(CC1101_SIDLE); + SpiStrobe(CC1101_STX); // start send + while (!digitalRead(GDO0)) + ; // Wait for GDO0 to be set -> sync transmitted + while (digitalRead(GDO0)) + ; // Wait for GDO0 to be cleared -> end of packet + SpiStrobe(CC1101_SFTX); // flush TXfifo + trxstate = 1; +} +/**************************************************************** + *FUNCTION NAME:Char direct SendData + *FUNCTION :use CC1101 send data without GDO + *INPUT :txBuffer: data array to send; size: number of data to send, no + * more than 61 OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::SendData(char *txchar, int t) { + int len = strlen(txchar); + byte chartobyte[len]; + for (int i = 0; i < len; i++) { + chartobyte[i] = txchar[i]; + } + SendData(chartobyte, len, t); +} +/**************************************************************** + *FUNCTION NAME:SendData + *FUNCTION :use CC1101 send data without GDO + *INPUT :txBuffer: data array to send; size: number of data to send, no + * more than 61 OUTPUT :none + ****************************************************************/ +void ELECHOUSE_CC1101::SendData(byte *txBuffer, byte size, int t) { + SpiWriteReg(CC1101_TXFIFO, size); + SpiWriteBurstReg(CC1101_TXFIFO, txBuffer, size); // write data to send + SpiStrobe(CC1101_SIDLE); + SpiStrobe(CC1101_STX); // start send + delay(t); + SpiStrobe(CC1101_SFTX); // flush TXfifo + trxstate = 1; +} +/**************************************************************** + *FUNCTION NAME:Check CRC + *FUNCTION :none + *INPUT :none + *OUTPUT :none + ****************************************************************/ +bool ELECHOUSE_CC1101::CheckCRC(void) { + byte lqi = SpiReadStatus(CC1101_LQI); + bool crc_ok = bitRead(lqi, 7); + if (crc_ok == 1) { + return 1; + } else { + SpiStrobe(CC1101_SFRX); + SpiStrobe(CC1101_SRX); + return 0; + } +} +/**************************************************************** + *FUNCTION NAME:CheckRxFifo + *FUNCTION :check receive data or not + *INPUT :none + *OUTPUT :flag: 0 no data; 1 receive data + ****************************************************************/ +bool ELECHOUSE_CC1101::CheckRxFifo(int t) { + if (trxstate != 2) { + SetRx(); + } + if (SpiReadStatus(CC1101_RXBYTES) & BYTES_IN_RXFIFO) { + delay(t); + return 1; + } else { + return 0; + } +} +/**************************************************************** + *FUNCTION NAME:CheckReceiveFlag + *FUNCTION :check receive data or not + *INPUT :none + *OUTPUT :flag: 0 no data; 1 receive data + ****************************************************************/ +byte ELECHOUSE_CC1101::CheckReceiveFlag(void) { + if (trxstate != 2) { + SetRx(); + } + if (digitalRead(GDO0)) // receive data + { + while (digitalRead(GDO0)) + ; + return 1; + } else // no data + { + return 0; + } +} +/**************************************************************** + *FUNCTION NAME:ReceiveData + *FUNCTION :read data received from RXfifo + *INPUT :rxBuffer: buffer to store data + *OUTPUT :size of data received + ****************************************************************/ +byte ELECHOUSE_CC1101::ReceiveData(byte *rxBuffer) { + byte size; + byte status[2]; - if(SpiReadStatus(CC1101_RXBYTES) & BYTES_IN_RXFIFO) - { - size=SpiReadReg(CC1101_RXFIFO); - SpiReadBurstReg(CC1101_RXFIFO,rxBuffer,size); - SpiReadBurstReg(CC1101_RXFIFO,status,2); - SpiStrobe(CC1101_SFRX); - SpiStrobe(CC1101_SRX); - return size; - } - else - { - SpiStrobe(CC1101_SFRX); - SpiStrobe(CC1101_SRX); - return 0; - } + if (SpiReadStatus(CC1101_RXBYTES) & BYTES_IN_RXFIFO) { + size = SpiReadReg(CC1101_RXFIFO); + SpiReadBurstReg(CC1101_RXFIFO, rxBuffer, size); + SpiReadBurstReg(CC1101_RXFIFO, status, 2); + SpiStrobe(CC1101_SFRX); + SpiStrobe(CC1101_SRX); + return size; + } else { + SpiStrobe(CC1101_SFRX); + SpiStrobe(CC1101_SRX); + return 0; + } } ELECHOUSE_CC1101 ELECHOUSE_cc1101; \ No newline at end of file diff --git a/src/libs/ELECHOUSE_CC1101_SRC_DRV.h b/src/libs/ELECHOUSE_CC1101_SRC_DRV.h index 136a9b5..fd6df0a 100644 --- a/src/libs/ELECHOUSE_CC1101_SRC_DRV.h +++ b/src/libs/ELECHOUSE_CC1101_SRC_DRV.h @@ -5,12 +5,13 @@ Version: November 12, 2010 This library is designed to use CC1101/CC1100 module on Arduino platform. - CC1101/CC1100 module is an useful wireless module.Using the functions of the - library, you can easily send and receive data by the CC1101/CC1100 module. + CC1101/CC1100 module is an useful wireless module.Using the functions of the + library, you can easily send and receive data by the CC1101/CC1100 module. Just have fun! For the details, please refer to the datasheet of CC1100/CC1101. ---------------------------------------------------------------------------------------------------------------- -cc1101 Driver for RC Switch. Mod by Little Satan. With permission to modify and publish Wilson Shen (ELECHOUSE). +cc1101 Driver for RC Switch. Mod by Little Satan. With permission to modify and +publish Wilson Shen (ELECHOUSE). ---------------------------------------------------------------------------------------------------------------- */ #ifndef ELECHOUSE_CC1101_SRC_DRV_h @@ -18,175 +19,183 @@ cc1101 Driver for RC Switch. Mod by Little Satan. With permission to modify and #include -//***************************************CC1101 define**************************************************// +//***************************************CC1101 +//define**************************************************// // CC1101 CONFIG REGSITER -#define CC1101_IOCFG2 0x00 // GDO2 output pin configuration -#define CC1101_IOCFG1 0x01 // GDO1 output pin configuration -#define CC1101_IOCFG0 0x02 // GDO0 output pin configuration -#define CC1101_FIFOTHR 0x03 // RX FIFO and TX FIFO thresholds -#define CC1101_SYNC1 0x04 // Sync word, high INT8U -#define CC1101_SYNC0 0x05 // Sync word, low INT8U -#define CC1101_PKTLEN 0x06 // Packet length -#define CC1101_PKTCTRL1 0x07 // Packet automation control -#define CC1101_PKTCTRL0 0x08 // Packet automation control -#define CC1101_ADDR 0x09 // Device address -#define CC1101_CHANNR 0x0A // Channel number -#define CC1101_FSCTRL1 0x0B // Frequency synthesizer control -#define CC1101_FSCTRL0 0x0C // Frequency synthesizer control -#define CC1101_FREQ2 0x0D // Frequency control word, high INT8U -#define CC1101_FREQ1 0x0E // Frequency control word, middle INT8U -#define CC1101_FREQ0 0x0F // Frequency control word, low INT8U -#define CC1101_MDMCFG4 0x10 // Modem configuration -#define CC1101_MDMCFG3 0x11 // Modem configuration -#define CC1101_MDMCFG2 0x12 // Modem configuration -#define CC1101_MDMCFG1 0x13 // Modem configuration -#define CC1101_MDMCFG0 0x14 // Modem configuration -#define CC1101_DEVIATN 0x15 // Modem deviation setting -#define CC1101_MCSM2 0x16 // Main Radio Control State Machine configuration -#define CC1101_MCSM1 0x17 // Main Radio Control State Machine configuration -#define CC1101_MCSM0 0x18 // Main Radio Control State Machine configuration -#define CC1101_FOCCFG 0x19 // Frequency Offset Compensation configuration -#define CC1101_BSCFG 0x1A // Bit Synchronization configuration -#define CC1101_AGCCTRL2 0x1B // AGC control -#define CC1101_AGCCTRL1 0x1C // AGC control -#define CC1101_AGCCTRL0 0x1D // AGC control -#define CC1101_WOREVT1 0x1E // High INT8U Event 0 timeout -#define CC1101_WOREVT0 0x1F // Low INT8U Event 0 timeout -#define CC1101_WORCTRL 0x20 // Wake On Radio control -#define CC1101_FREND1 0x21 // Front end RX configuration -#define CC1101_FREND0 0x22 // Front end TX configuration -#define CC1101_FSCAL3 0x23 // Frequency synthesizer calibration -#define CC1101_FSCAL2 0x24 // Frequency synthesizer calibration -#define CC1101_FSCAL1 0x25 // Frequency synthesizer calibration -#define CC1101_FSCAL0 0x26 // Frequency synthesizer calibration -#define CC1101_RCCTRL1 0x27 // RC oscillator configuration -#define CC1101_RCCTRL0 0x28 // RC oscillator configuration -#define CC1101_FSTEST 0x29 // Frequency synthesizer calibration control -#define CC1101_PTEST 0x2A // Production test -#define CC1101_AGCTEST 0x2B // AGC test -#define CC1101_TEST2 0x2C // Various test settings -#define CC1101_TEST1 0x2D // Various test settings -#define CC1101_TEST0 0x2E // Various test settings +#define CC1101_IOCFG2 0x00 // GDO2 output pin configuration +#define CC1101_IOCFG1 0x01 // GDO1 output pin configuration +#define CC1101_IOCFG0 0x02 // GDO0 output pin configuration +#define CC1101_FIFOTHR 0x03 // RX FIFO and TX FIFO thresholds +#define CC1101_SYNC1 0x04 // Sync word, high INT8U +#define CC1101_SYNC0 0x05 // Sync word, low INT8U +#define CC1101_PKTLEN 0x06 // Packet length +#define CC1101_PKTCTRL1 0x07 // Packet automation control +#define CC1101_PKTCTRL0 0x08 // Packet automation control +#define CC1101_ADDR 0x09 // Device address +#define CC1101_CHANNR 0x0A // Channel number +#define CC1101_FSCTRL1 0x0B // Frequency synthesizer control +#define CC1101_FSCTRL0 0x0C // Frequency synthesizer control +#define CC1101_FREQ2 0x0D // Frequency control word, high INT8U +#define CC1101_FREQ1 0x0E // Frequency control word, middle INT8U +#define CC1101_FREQ0 0x0F // Frequency control word, low INT8U +#define CC1101_MDMCFG4 0x10 // Modem configuration +#define CC1101_MDMCFG3 0x11 // Modem configuration +#define CC1101_MDMCFG2 0x12 // Modem configuration +#define CC1101_MDMCFG1 0x13 // Modem configuration +#define CC1101_MDMCFG0 0x14 // Modem configuration +#define CC1101_DEVIATN 0x15 // Modem deviation setting +#define CC1101_MCSM2 0x16 // Main Radio Control State Machine configuration +#define CC1101_MCSM1 0x17 // Main Radio Control State Machine configuration +#define CC1101_MCSM0 0x18 // Main Radio Control State Machine configuration +#define CC1101_FOCCFG 0x19 // Frequency Offset Compensation configuration +#define CC1101_BSCFG 0x1A // Bit Synchronization configuration +#define CC1101_AGCCTRL2 0x1B // AGC control +#define CC1101_AGCCTRL1 0x1C // AGC control +#define CC1101_AGCCTRL0 0x1D // AGC control +#define CC1101_WOREVT1 0x1E // High INT8U Event 0 timeout +#define CC1101_WOREVT0 0x1F // Low INT8U Event 0 timeout +#define CC1101_WORCTRL 0x20 // Wake On Radio control +#define CC1101_FREND1 0x21 // Front end RX configuration +#define CC1101_FREND0 0x22 // Front end TX configuration +#define CC1101_FSCAL3 0x23 // Frequency synthesizer calibration +#define CC1101_FSCAL2 0x24 // Frequency synthesizer calibration +#define CC1101_FSCAL1 0x25 // Frequency synthesizer calibration +#define CC1101_FSCAL0 0x26 // Frequency synthesizer calibration +#define CC1101_RCCTRL1 0x27 // RC oscillator configuration +#define CC1101_RCCTRL0 0x28 // RC oscillator configuration +#define CC1101_FSTEST 0x29 // Frequency synthesizer calibration control +#define CC1101_PTEST 0x2A // Production test +#define CC1101_AGCTEST 0x2B // AGC test +#define CC1101_TEST2 0x2C // Various test settings +#define CC1101_TEST1 0x2D // Various test settings +#define CC1101_TEST0 0x2E // Various test settings -//CC1101 Strobe commands -#define CC1101_SRES 0x30 // Reset chip. -#define CC1101_SFSTXON 0x31 // Enable and calibrate frequency synthesizer (if MCSM0.FS_AUTOCAL=1). - // If in RX/TX: Go to a wait state where only the synthesizer is - // running (for quick RX / TX turnaround). -#define CC1101_SXOFF 0x32 // Turn off crystal oscillator. -#define CC1101_SCAL 0x33 // Calibrate frequency synthesizer and turn it off - // (enables quick start). -#define CC1101_SRX 0x34 // Enable RX. Perform calibration first if coming from IDLE and - // MCSM0.FS_AUTOCAL=1. -#define CC1101_STX 0x35 // In IDLE state: Enable TX. Perform calibration first if - // MCSM0.FS_AUTOCAL=1. If in RX state and CCA is enabled: - // Only go to TX if channel is clear. -#define CC1101_SIDLE 0x36 // Exit RX / TX, turn off frequency synthesizer and exit - // Wake-On-Radio mode if applicable. -#define CC1101_SAFC 0x37 // Perform AFC adjustment of the frequency synthesizer -#define CC1101_SWOR 0x38 // Start automatic RX polling sequence (Wake-on-Radio) -#define CC1101_SPWD 0x39 // Enter power down mode when CSn goes high. -#define CC1101_SFRX 0x3A // Flush the RX FIFO buffer. -#define CC1101_SFTX 0x3B // Flush the TX FIFO buffer. -#define CC1101_SWORRST 0x3C // Reset real time clock. -#define CC1101_SNOP 0x3D // No operation. May be used to pad strobe commands to two - // INT8Us for simpler software. -//CC1101 STATUS REGSITER -#define CC1101_PARTNUM 0x30 -#define CC1101_VERSION 0x31 -#define CC1101_FREQEST 0x32 -#define CC1101_LQI 0x33 -#define CC1101_RSSI 0x34 -#define CC1101_MARCSTATE 0x35 -#define CC1101_WORTIME1 0x36 -#define CC1101_WORTIME0 0x37 -#define CC1101_PKTSTATUS 0x38 -#define CC1101_VCO_VC_DAC 0x39 -#define CC1101_TXBYTES 0x3A -#define CC1101_RXBYTES 0x3B +// CC1101 Strobe commands +#define CC1101_SRES 0x30 // Reset chip. +#define CC1101_SFSTXON \ + 0x31 // Enable and calibrate frequency synthesizer (if MCSM0.FS_AUTOCAL=1). + // If in RX/TX: Go to a wait state where only the synthesizer is + // running (for quick RX / TX turnaround). +#define CC1101_SXOFF 0x32 // Turn off crystal oscillator. +#define CC1101_SCAL \ + 0x33 // Calibrate frequency synthesizer and turn it off + // (enables quick start). +#define CC1101_SRX \ + 0x34 // Enable RX. Perform calibration first if coming from IDLE and + // MCSM0.FS_AUTOCAL=1. +#define CC1101_STX \ + 0x35 // In IDLE state: Enable TX. Perform calibration first if + // MCSM0.FS_AUTOCAL=1. If in RX state and CCA is enabled: + // Only go to TX if channel is clear. +#define CC1101_SIDLE \ + 0x36 // Exit RX / TX, turn off frequency synthesizer and exit + // Wake-On-Radio mode if applicable. +#define CC1101_SAFC 0x37 // Perform AFC adjustment of the frequency synthesizer +#define CC1101_SWOR 0x38 // Start automatic RX polling sequence (Wake-on-Radio) +#define CC1101_SPWD 0x39 // Enter power down mode when CSn goes high. +#define CC1101_SFRX 0x3A // Flush the RX FIFO buffer. +#define CC1101_SFTX 0x3B // Flush the TX FIFO buffer. +#define CC1101_SWORRST 0x3C // Reset real time clock. +#define CC1101_SNOP \ + 0x3D // No operation. May be used to pad strobe commands to two + // INT8Us for simpler software. +// CC1101 STATUS REGSITER +#define CC1101_PARTNUM 0x30 +#define CC1101_VERSION 0x31 +#define CC1101_FREQEST 0x32 +#define CC1101_LQI 0x33 +#define CC1101_RSSI 0x34 +#define CC1101_MARCSTATE 0x35 +#define CC1101_WORTIME1 0x36 +#define CC1101_WORTIME0 0x37 +#define CC1101_PKTSTATUS 0x38 +#define CC1101_VCO_VC_DAC 0x39 +#define CC1101_TXBYTES 0x3A +#define CC1101_RXBYTES 0x3B -//CC1101 PATABLE,TXFIFO,RXFIFO -#define CC1101_PATABLE 0x3E -#define CC1101_TXFIFO 0x3F -#define CC1101_RXFIFO 0x3F +// CC1101 PATABLE,TXFIFO,RXFIFO +#define CC1101_PATABLE 0x3E +#define CC1101_TXFIFO 0x3F +#define CC1101_RXFIFO 0x3F -//************************************* class **************************************************// -class ELECHOUSE_CC1101 -{ -private: - void SpiStart(void); - void SpiEnd(void); - void GDO_Set (void); - void GDO0_Set (void); - void Reset (void); - void setSpi(void); - void RegConfigSettings(void); - void Calibrate(void); - void Split_PKTCTRL0(void); - void Split_PKTCTRL1(void); - void Split_MDMCFG1(void); - void Split_MDMCFG2(void); - void Split_MDMCFG4(void); -public: - void Init(void); - byte SpiReadStatus(byte addr); - void setSpiPin(byte sck, byte miso, byte mosi, byte ss); - void addSpiPin(byte sck, byte miso, byte mosi, byte ss, byte modul); - void setGDO(byte gdo0, byte gdo2); - void setGDO0(byte gdo0); - void addGDO(byte gdo0, byte gdo2, byte modul); - void addGDO0(byte gdo0, byte modul); - void setModul(byte modul); - void setCCMode(bool s); - void setModulation(byte m); - void setPA(int p); - void setMHZ(float mhz); - void setChannel(byte chnl); - void setChsp(float f); - void setRxBW(float f); - void setDRate(float d); - void setDeviation(float d); - void SetTx(void); - void SetRx(void); - void SetTx(float mhz); - void SetRx(float mhz); - int getRssi(void); - byte getLqi(void); - void setSres(void); - void setSidle(void); - void goSleep(void); - void SendData(byte *txBuffer, byte size); - void SendData(char *txchar); - void SendData(byte *txBuffer, byte size, int t); - void SendData(char *txchar, int t); - byte CheckReceiveFlag(void); - byte ReceiveData(byte *rxBuffer); - bool CheckCRC(void); - void SpiStrobe(byte strobe); - void SpiWriteReg(byte addr, byte value); - void SpiWriteBurstReg(byte addr, byte *buffer, byte num); - byte SpiReadReg(byte addr); - void SpiReadBurstReg(byte addr, byte *buffer, byte num); - void setClb(byte b, byte s, byte e); - bool getCC1101(void); - byte getMode(void); - void setSyncWord(byte sh, byte sl); - void setAddr(byte v); - void setWhiteData(bool v); - void setPktFormat(byte v); - void setCrc(bool v); - void setLengthConfig(byte v); - void setPacketLength(byte v); - void setDcFilterOff(bool v); - void setManchester(bool v); - void setSyncMode(byte v); - void setFEC(bool v); - void setPRE(byte v); - void setPQT(byte v); - void setCRC_AF(bool v); - void setAppendStatus(bool v); - void setAdrChk(byte v); - bool CheckRxFifo(int t); +//************************************* class +//**************************************************// +class ELECHOUSE_CC1101 { + private: + void SpiStart(void); + void SpiEnd(void); + void GDO_Set(void); + void GDO0_Set(void); + void Reset(void); + void setSpi(void); + void RegConfigSettings(void); + void Calibrate(void); + void Split_PKTCTRL0(void); + void Split_PKTCTRL1(void); + void Split_MDMCFG1(void); + void Split_MDMCFG2(void); + void Split_MDMCFG4(void); + + public: + void Init(void); + byte SpiReadStatus(byte addr); + void setSpiPin(byte sck, byte miso, byte mosi, byte ss); + void addSpiPin(byte sck, byte miso, byte mosi, byte ss, byte modul); + void setGDO(byte gdo0, byte gdo2); + void setGDO0(byte gdo0); + void addGDO(byte gdo0, byte gdo2, byte modul); + void addGDO0(byte gdo0, byte modul); + void setModul(byte modul); + void setCCMode(bool s); + void setModulation(byte m); + void setPA(int p); + void setMHZ(float mhz); + void setChannel(byte chnl); + void setChsp(float f); + void setRxBW(float f); + void setDRate(float d); + void setDeviation(float d); + void SetTx(void); + void SetRx(void); + void SetTx(float mhz); + void SetRx(float mhz); + int getRssi(void); + byte getLqi(void); + void setSres(void); + void setSidle(void); + void goSleep(void); + void SendData(byte *txBuffer, byte size); + void SendData(char *txchar); + void SendData(byte *txBuffer, byte size, int t); + void SendData(char *txchar, int t); + byte CheckReceiveFlag(void); + byte ReceiveData(byte *rxBuffer); + bool CheckCRC(void); + void SpiStrobe(byte strobe); + void SpiWriteReg(byte addr, byte value); + void SpiWriteBurstReg(byte addr, byte *buffer, byte num); + byte SpiReadReg(byte addr); + void SpiReadBurstReg(byte addr, byte *buffer, byte num); + void setClb(byte b, byte s, byte e); + bool getCC1101(void); + byte getMode(void); + void setSyncWord(byte sh, byte sl); + void setAddr(byte v); + void setWhiteData(bool v); + void setPktFormat(byte v); + void setCrc(bool v); + void setLengthConfig(byte v); + void setPacketLength(byte v); + void setDcFilterOff(bool v); + void setManchester(bool v); + void setSyncMode(byte v); + void setFEC(bool v); + void setPRE(byte v); + void setPQT(byte v); + void setCRC_AF(bool v); + void setAppendStatus(bool v); + void setAdrChk(byte v); + bool CheckRxFifo(int t); }; extern ELECHOUSE_CC1101 ELECHOUSE_cc1101; -- cgit v1.2.3