From 84995f5e72a48ab92518c9dde797256756d62e10 Mon Sep 17 00:00:00 2001 From: eadmaster <925171+eadmaster@users.noreply.github.com> Date: Wed, 7 Aug 2024 03:11:00 +0200 Subject: [PATCH] added initial support for CC1101 via SPI (#66) --- platformio.ini | 45 ++++++++++++----- src/modules/rf/rf.cpp | 115 +++++++++++++++++++++++++++++++++--------- 2 files changed, 123 insertions(+), 37 deletions(-) diff --git a/platformio.ini b/platformio.ini index 9957bd1d..48033e45 100644 --- a/platformio.ini +++ b/platformio.ini @@ -16,6 +16,11 @@ default_envs = m5stack-core2 m5stack-core16mb m5stack-core4mb +;uncomment to not use global dirs to avoid possible conflicts +;platforms_dir = .pio/platforms +;packages_dir = .pio/packages +;build_cache_dir = .pio/buildcache +;cache_dir = .pio/cache [common] build_flags = @@ -42,6 +47,7 @@ lib_deps = ESP8266SAM TinyGPSPlus tinyu-zhao/FFT@^0.0.1 + lsatan/SmartRC-CC1101-Driver-Lib@^2.5.7 [env:m5stack-cplus2] platform = espressif32 @@ -632,6 +638,11 @@ board = esp32-s3-devkitc-1 framework = arduino ;board_build.partitions = custom_16Mb.csv ;board_upload.flash_size = 16MB +;monitor_speed = 115200 +;board_build.partitions = custom_16Mb.csv +;board_build.f_flash = 40000000L +;board_upload.flash_size = 16MB +;board_upload.maximum_size = 16777216 build_flags = ${common.build_flags} -DESP32S3DEVKITC1 @@ -643,16 +654,18 @@ build_flags = -DLED_ON=HIGH -DLED_OFF=LOW ; sd card pins + ; suggested https://github.com/espressif/esp-idf/tree/master/examples/storage/sd_card/sdmmc -DSDCARD_CS=-1 -DSDCARD_SCK=-1 -DSDCARD_MISO=-1 -DSDCARD_MOSI=-1 - ; grove pins (SDA=default TX pin, SCL=default RX pin) - -DGROVE_SDA=35 - -DGROVE_SCL=36 + ; grove pins (SDA=default TX pin, SCL=default RX pin, for both IR and RF) + ; defaults from https://github.com/espressif/arduino-esp32/blob/master/variants/esp32s3/pins_arduino.h + -DGROVE_SDA=8 ; default RF TX pin + -DGROVE_SCL=9 ; default IR/RF RX pin ; tft vars -DROTATION=1 - -DBACKLIGHT=15 ; tft backlight pin + -DBACKLIGHT=-1 ; tft backlight pin -DWIDTH=240 -DHEIGHT=135 -DMINBRIGHT=160 ; unused? @@ -665,17 +678,25 @@ build_flags = -DFM=2 -DFG=3 ; ui control buttons - -DSEL_BTN=1 - -DUP_BTN=2 ; also work as ESC - -DDW_BTN=3 ; also work as NEXT - -DTOUCH_THRESHOLD=20 + ;-DSEL_BTN=1 + ;-DUP_BTN=2 ; also work as ESC + ;-DDW_BTN=3 ; also work as NEXT -DBTN_ALIAS='"OK"' ;Microphone ;-DMIC_SPM1423=1 ; uncomment to enable Applicable for SPM1423 device - -DPIN_CLK=43 - -DI2S_SCLK_PIN=43 - -DI2S_DATA_PIN=46 - -DPIN_DATA=46 + ;-DPIN_CLK=-1 + ;-DI2S_SCLK_PIN=-1 + ;-DI2S_DATA_PIN=-1 + ;-DPIN_DATA=-1 + ;CC1101 SPI connection pins, can use any on this board https://esp32.com/viewtopic.php?t=37729 + ; best connection pins for higher speed https://docs.espressif.com/projects/esp-idf/en/latest/esp32s3/api-reference/peripherals/spi_master.html#gpio-matrix-and-io-mux + -DUSE_CC1101_VIA_SPI + -DCC1101_GDO0_PIN=9 ; RFSend (SPI2_IOMUX_PIN_NUM_HD / FSPIHD) + -DCC1101_SS_PIN=10 + -DCC1101_MOSI_PIN=11 + -DCC1101_SCK_PIN=12 + -DCC1101_MISO_PIN=13 + -DCC1101_GDO2_PIN=14 ; RFRecv (SPI2_IOMUX_PIN_NUM_CS / FSPIWP) lib_deps = ${common.lib_deps} diff --git a/src/modules/rf/rf.cpp b/src/modules/rf/rf.cpp index f7a0697f..299c5cbf 100644 --- a/src/modules/rf/rf.cpp +++ b/src/modules/rf/rf.cpp @@ -7,6 +7,7 @@ #include "core/mykeyboard.h" #include "core/display.h" #include "core/sd_functions.h" +#include "core/settings.h" #include "rf.h" // Cria um objeto PCA9554 com o endereço I2C do PCA9554PW @@ -186,14 +187,22 @@ void rf_jammerIntermittent() { //@IncursioHack - https://github.com/IncursioHack digitalWrite(RfTx, LOW); // Deactivate pin } - +#ifdef USE_CC1101_VIA_SPI + #include +#endif + void RCSwitch_send(uint64_t data, unsigned int bits, int pulse, int protocol, int repeat) { RCSwitch mySwitch = RCSwitch(); - mySwitch.enableTransmit(RfTx); - mySwitch.setProtocol(protocol); + + #ifdef USE_CC1101_VIA_SPI + mySwitch.enableTransmit(CC1101_GDO0_PIN); + #else + mySwitch.enableTransmit(RfTx); + #endif + + mySwitch.setProtocol(protocol); // override if (pulse) { mySwitch.setPulseLength(pulse); } - mySwitch.setPulseLength(pulse); mySwitch.setRepeatTransmit(repeat); mySwitch.send(data, bits); @@ -523,6 +532,9 @@ void sendRfCommand(struct RfCodes rfcode) { String preset = rfcode.preset; String data = rfcode.data; uint64_t key = rfcode.key; + byte modulation = 2; // possible values for CC1101: 0 = 2-FSK, 1 =GFSK, 2=ASK, 3 = 4-FSK, 4 = MSK + float deviation = 0; + float rxBW = 0; // Receive bandwidth /* Serial.println("sendRawRfCommand"); Serial.println(data); @@ -530,26 +542,50 @@ void sendRfCommand(struct RfCodes rfcode) { Serial.println(preset); Serial.println(protocol); */ - if(frequency != 433920000) { - Serial.print("unsupported frequency: "); - Serial.println(frequency); - return; - } - // MEMO: frequency is fixed with some transmitters https://github.com/sui77/rc-switch/issues/256 - // TODO: add frequency switching via CC1101 https://github.com/LSatan/SmartRC-CC1101-Driver-Lib // Radio preset name (configures modulation, bandwidth, filters, etc.). + /* supported flipper presets: + FuriHalSubGhzPresetIDLE, // < default configuration + FuriHalSubGhzPresetOok270Async, ///< OOK, bandwidth 270kHz, asynchronous + FuriHalSubGhzPresetOok650Async, ///< OOK, bandwidth 650kHz, asynchronous + FuriHalSubGhzPreset2FSKDev238Async, //< FM, deviation 2.380371 kHz, asynchronous + FuriHalSubGhzPreset2FSKDev476Async, //< FM, deviation 47.60742 kHz, asynchronous + FuriHalSubGhzPresetMSK99_97KbAsync, //< MSK, deviation 47.60742 kHz, 99.97Kb/s, asynchronous + FuriHalSubGhzPresetGFSK9_99KbAsync, //< GFSK, deviation 19.042969 kHz, 9.996Kb/s, asynchronous + FuriHalSubGhzPresetCustom, //Custom Preset + */ struct Protocol rcswitch_protocol; int rcswitch_protocol_no = 1; if(preset == "FuriHalSubGhzPresetOok270Async") { rcswitch_protocol_no = 1; // pulseLength , syncFactor , zero , one, invertedSignal rcswitch_protocol = { 350, { 1, 31 }, { 1, 3 }, { 3, 1 }, false }; + modulation = 2; + rxBW = 270; } else if(preset == "FuriHalSubGhzPresetOok650Async") { rcswitch_protocol_no = 2; rcswitch_protocol = { 650, { 1, 10 }, { 1, 2 }, { 2, 1 }, false }; - } else if(preset == "1" || preset == "2" || preset == "3" || preset == "4" || preset == "5" || preset == "6" || preset == "7" || preset == "8" || preset == "9" || preset == "10" || preset == "11" || preset == "12"|| preset == "13" || preset == "14") { + modulation = 2; + rxBW = 650; + } + else if(preset == "FuriHalSubGhzPreset2FSKDev238Async") { + modulation = 0; + deviation = 2.380371; + } + else if(preset == "FuriHalSubGhzPreset2FSKDev476Async") { + modulation = 0; + deviation = 47.60742; + } + else if(preset == "FuriHalSubGhzPresetMSK99_97KbAsync") { + modulation = 4; + deviation = 47.60742; + } + else if(preset == "FuriHalSubGhzPresetGFSK9_99KbAsync") { + modulation = 1; + deviation = 19.042969; + } + else if(preset == "1" || preset == "2" || preset == "3" || preset == "4" || preset == "5" || preset == "6" || preset == "7" || preset == "8" || preset == "9" || preset == "10" || preset == "11" || preset == "12"|| preset == "13" || preset == "14") { rcswitch_protocol_no = preset.toInt(); } else { @@ -557,22 +593,51 @@ void sendRfCommand(struct RfCodes rfcode) { Serial.println(preset); return; } - /* supported flipper presets: - FuriHalSubGhzPresetIDLE, // < default configuration - FuriHalSubGhzPresetOok270Async, ///< OOK, bandwidth 270kHz, asynchronous - FuriHalSubGhzPresetOok650Async, ///< OOK, bandwidth 650kHz, asynchronous - FuriHalSubGhzPreset2FSKDev238Async, //< FM, deviation 2.380371 kHz, asynchronous - FuriHalSubGhzPreset2FSKDev476Async, //< FM, deviation 47.60742 kHz, asynchronous - FuriHalSubGhzPresetMSK99_97KbAsync, //< MSK, deviation 47.60742 kHz, 99.97Kb/s, asynchronous - FuriHalSubGhzPresetGFSK9_99KbAsync, //< GFSK, deviation 19.042969 kHz, 9.996Kb/s, asynchronous - FuriHalSubGhzPresetCustom, //Custom Preset - */ + + #ifndef USE_CC1101_VIA_SPI + // check supported frequency and modulation by the current module + if(frequency != 433920000) { + Serial.print("unsupported frequency: "); + Serial.println(frequency); + return; + } + // MEMO: frequency is fixed with some transmitters https://github.com/sui77/rc-switch/issues/256 + if(modulation != 2) { + Serial.print("unsupported modulation: "); + Serial.println(modulation); + return; + } // init output pin - digitalWrite(RfTx, LED_OFF); - if(RfTx==0) RfTx=GROVE_SDA; // quick fix + gsetRfTxPin(false); + //if(RfTx==0) RfTx=GROVE_SDA; // quick fix pinMode(RfTx, OUTPUT); - + digitalWrite(RfTx, LED_OFF); + + #else // USE_CC1101_VIA_SPI + // init cc1101 module + // derived from https://github.com/LSatan/SmartRC-CC1101-Driver-Lib/blob/master/examples/Rc-Switch%20examples%20cc1101/SendDemo_cc1101/SendDemo_cc1101.ino + + ELECHOUSE_cc1101.setSpiPin(CC1101_SCK_PIN, CC1101_MISO_PIN, CC1101_MOSI_PIN, CC1101_SS_PIN); + ELECHOUSE_cc1101.setGDO(CC1101_GDO0_PIN, CC1101_GDO2_PIN); //Set Gdo0 (tx) and Gdo2 (rx) for serial transmission function. + + if (ELECHOUSE_cc1101.getCC1101()){ // Check the CC1101 Spi connection. + Serial.println("cc1101 Connection OK"); + }else{ + Serial.println("cc1101 Connection Error"); + return; + } + + ELECHOUSE_cc1101.Init(); // must be set to initialize the cc1101! + ELECHOUSE_cc1101.setMHZ(frequency / 1000000.0); // e.g. 433.92. Here you can set your basic frequency. The lib calculates the frequency automatically (default = 433.92).The cc1101 can: 300-348 MHZ, 387-464MHZ and 779-928MHZ. Read More info from datasheet. + //ELECHOUSE_cc1101.setMHZ(433.92); // e.g. 433.92. Here you can set your basic frequency. The lib calculates the frequency automatically (default = 433.92).The cc1101 can: 300-348 MHZ, 387-464MHZ and 779-928MHZ. Read More info from datasheet. + ELECHOUSE_cc1101.setModulation(modulation); + if(deviation) ELECHOUSE_cc1101.setDeviation(deviation); + if(rxBW) ELECHOUSE_cc1101.setRxBW(rxBW); // Set the Receive Bandwidth in kHz. Value from 58.03 to 812.50. Default is 812.50 kHz. + ELECHOUSE_cc1101.setPA(12); // set TxPower. The following settings are possible depending on the frequency band. (-30 -20 -15 -10 -6 0 5 7 10 11 12) Default is max! + ELECHOUSE_cc1101.SetTx(); + #endif + if(protocol == "RAW") { // count the number of elements of RAW_Data int buff_size=0;