aanper před 5 roky
rodič
revize
b007b9cb60

+ 249 - 1
applications/cc1101-workaround/cc1101-workaround.c

@@ -1,8 +1,256 @@
 #include "flipper.h"
 #include "flipper.h"
 #include "u8g2.h"
 #include "u8g2.h"
+#include <stdio.h>
+
+#include "cc1101/cc1101.h"
+
+#define MIN_DBM -120
+#define STEP_DBM 10
+#define RSSI_DELAY 600 //rssi delay in micro second
+
+#define RSSI_THRESHOLD -60
+
+#define START_SUB_BAND 3
+#define STOP_SUB_BAND 3
+#define NUM_OF_SUB_BANDS 7
+#define CAL_INT 20 //cal every 10 channels(every 1MHz)
+
+//variables used to calculate rssi
+uint8_t rssi_dec;
+int16_t rssi_dBm;
+uint8_t rssi_offset[NUM_OF_SUB_BANDS] = {74, 74, 74, 74, 74, 74, 74};
+
+#define CHAN_SPA 0.05 //channel spacing
+
+float base_freq[NUM_OF_SUB_BANDS] = {387, 399.8, 412.6, 425.4, 438.2, 451, 463.8};
+//FREQ2,FREQ1,FREQ0
+uint8_t freqSettings[NUM_OF_SUB_BANDS][3] = {
+    {0x0E, 0xE2, 0x76}, //band0
+    {0x0F, 0x60, 0x76},
+    {0x0F, 0xDE, 0x76}, //band1
+    {0x10, 0x5C, 0x76},
+    {0x10, 0xDA, 0x76},
+    {0x11, 0x58, 0x8F},
+    {0x11, 0xD6, 0x8F}}; //band2
+//no change in TEST0 WHERE (>430.5MHz) one should change from TEST0=0x0B to 0x09
+uint16_t limitTest0Reg[NUM_OF_SUB_BANDS] = {256, 256, 256, 103, 0, 0, 0};
+/* setting to use 50khz channel spacing whole band*****************************************/
+
+//int16_t rssiData[NUM_OF_SUB_BANDS][256];
+int16_t rssiTable[256];
+uint16_t channelNumber[256];
+uint8_t carrierSenseCounter = 0; //counter used to keep track on how many CS has been asserted
+
+uint8_t firstChannel[NUM_OF_SUB_BANDS] = {0, 0, 0, 160, 0, 0, 0};
+//stop channel in each subband
+uint8_t lastChannel[NUM_OF_SUB_BANDS] = {255, 255, 255, 180, 255, 255, 4};
+//initialized to a value lower than the rssi threshold/ higher than channel number
+int16_t highRSSI[NUM_OF_SUB_BANDS] =
+    {MIN_DBM, MIN_DBM, MIN_DBM, MIN_DBM, MIN_DBM, MIN_DBM, MIN_DBM};
+uint16_t selectedChannel[NUM_OF_SUB_BANDS] = {300, 300, 300, 300, 300, 300, 300};
+
+int8_t activeBand; //store subband that contains strongest signal
+uint16_t activeChannel; //
+
+int16_t calRSSI(uint8_t rssi_dec, uint8_t rssiOffset) {
+    int16_t rssi;
+
+    if(rssi_dec >= 128) {
+        rssi = (int16_t)((int16_t)(rssi_dec - 256) / 2) - rssiOffset;
+    } else {
+        rssi = (rssi_dec / 2) - rssiOffset;
+    }
+
+    return rssi;
+}
+
+void scanFreq(CC1101* cc1101) {
+    uint8_t calCounter; //to deterin when to calibrate
+    uint8_t subBand;
+    uint16_t channel;
+    uint16_t i;
+
+    float freq;
+
+    cc1101->SpiWriteReg(CC1101_MCSM0, 0x08); //disalbe FS_AUTOCAL
+    cc1101->SpiWriteReg(CC1101_AGCCTRL2, 0x43 | 0x0C); //MAX_DVGA_GAIN to 11 for fast rssi
+    cc1101->SpiWriteReg(CC1101_AGCCTRL0, 0xB0); //max AGC WAIT_TIME; 0 filter_length
+    cc1101->SetMod(GFSK); //set to GFSK for fast rssi measurement | +8 is dcfilter off
+
+    // 1) loop through all sub bands
+    for(subBand = START_SUB_BAND; subBand < STOP_SUB_BAND + 1; subBand++) {
+        //1.1) set subBands freq by FREQ2, FREQ1, FREQ0
+        cc1101->SetFreq(
+            freqSettings[subBand][0], freqSettings[subBand][1], freqSettings[subBand][2]);
+        //1.2) set TEST0--maybe!
+        //1.3) reset calibration counter
+        calCounter = 0;
+        // 1.4) loop throuhg all channels
+        for(channel = firstChannel[subBand]; channel <= lastChannel[subBand]; channel++) {
+            uint8_t pktStatus;
+            //1.4.1) set channel register
+            cc1101->SetChannel(channel);
+
+            //1.4.2)  set TEST0
+            if(channel == limitTest0Reg[subBand]) {
+                //set test0 to 0x09
+                cc1101->SpiWriteReg(CC1101_TEST0, 0x09);
+                //set FSCAL2 to 0x2A to force VCO HIGH
+                cc1101->SpiWriteReg(CC1101_FSCAL2, 0x2A);
+                //clear calCounter to invoke mannual calibration
+                calCounter = 0;
+            }
+            //1.4.3) calibrate every 1MHz
+            if(calCounter++ == 0) {
+                //perform a manual calibration by issuing SCAL command
+                cc1101->SpiStrobe(CC1101_SCAL);
+            }
+            //1.4.4) reset calCounter when 1MHz reached
+            if(calCounter == CAL_INT) {
+                calCounter = 0;
+            }
+            // 1.4.5-6 enter rx mode
+            cc1101->SetReceive();
+            //1.4.7 wait for RSSI to be valid: less than 1.5ms
+            delayMicroseconds(RSSI_DELAY);
+            // 1.4.8) read PKTSTATUS register while the radio is in RX state
+            pktStatus = cc1101->SpiReadStatus(CC1101_PKTSTATUS);
+            // 1.4.9) enter IDLE state by issuing a SIDLE command
+            cc1101->SpiStrobe(CC1101_SIDLE);
+            // 1.4.10) check if CS is assearted
+            // //read rssi value and converto to dBm form
+            rssi_dec = (uint8_t)cc1101->SpiReadStatus(CC1101_RSSI);
+            rssi_dBm = calRSSI(rssi_dec, rssi_offset[subBand]);
+
+            //rssiData[subBand][channel]=rssi_dBm;
+            if(pktStatus & 0x40) { //CS assearted
+                //store rssi value and corresponding channel number
+                rssiTable[carrierSenseCounter] = rssi_dBm;
+                channelNumber[carrierSenseCounter] = channel;
+                carrierSenseCounter++;
+            }
+
+#ifdef CC1101_DEBUG
+            printf("rssi_dBm: %d\n", rssi_dBm);
+#endif
+        } //end channel lop
+
+        //1.5)before moving to next sub band, scan through rssiTable to find highest rssi value
+        for(i = 0; i < carrierSenseCounter; i++) {
+            if(rssiTable[i] > highRSSI[subBand]) {
+                highRSSI[subBand] = rssiTable[i];
+                selectedChannel[subBand] = channelNumber[i];
+            }
+        }
+        // Serial.print("subBand:------------------>");
+        // Serial.println(subBand);
+        // Serial.print("selectedChannel:");
+        // Serial.println(selectedChannel[subBand]);
+        // Serial.print("highRSSI:");
+        // Serial.println(highRSSI[subBand]);
+
+        //1.6) reset carrierSenseCounter
+        carrierSenseCounter = 0;
+    } // end band loop
+
+    //2) when all sub bands has been scanned , find best subband and channel
+    int16_t tempRssi = MIN_DBM;
+    for(subBand = 0; subBand < NUM_OF_SUB_BANDS; subBand++) {
+        if(highRSSI[subBand] > tempRssi) {
+            tempRssi = highRSSI[subBand];
+            activeChannel = selectedChannel[subBand];
+            activeBand = subBand;
+        }
+    }
+
+    // printf("activeBand:**********> %d, activeChannel %d,\n", activeBand, activeChannel);
+
+    cc1101->SpiWriteReg(CC1101_MCSM0, 0x18); //enable FS_AUTOCAL
+    cc1101->SpiWriteReg(CC1101_AGCCTRL2, 0x43); //back to recommended config
+    cc1101->SpiWriteReg(CC1101_AGCCTRL0, 0x91); //back to recommended config
+}
+
+void jamming(CC1101* cc1101, uint8_t band, uint16_t channel, uint16_t miniSec) {
+    cc1101->SetFreq(freqSettings[band][0], freqSettings[band][1], freqSettings[band][2]);
+    cc1101->SetChannel(channel);
+    // digitalWrite(19,0);
+    cc1101->SetTransmit();
+    delay(miniSec);
+
+    cc1101->SpiStrobe(CC1101_SIDLE);
+}
+
+extern QueueHandle_t event_queue;
+
+bool jamm_on = false;
+
+void set_jam(bool state) {
+    jamm_on = state;
+}
+
+extern "C" void radio() {
+    CC1101 cc1101(SS_PIN, GDO0, GDO2);
+
+    uint8_t address = cc1101.Init();
+
+    if(address > 0) {
+        printf("CC1101 init done: %d\n", address);
+    } else {
+        printf("CC1101 init fail\n");
+    }
+
+    /* setting to use 50khz channel spacing**************************************/
+    cc1101.SpiWriteReg(
+        CC1101_MDMCFG4, 0xCD); // RX filter bandwidth 58.035714(0xFD) 100k(0xCD) 200k(0x8D)
+    cc1101.SpiWriteReg(
+        CC1101_MDMCFG3, 0x3B); //datarate config 250kBaud  for the purpose of fast rssi measurement
+    cc1101.SpiWriteReg(CC1101_MDMCFG1, 0x20); //FEC preamble etc. last 2 bits for channel spacing
+    cc1101.SpiWriteReg(CC1101_MDMCFG0, 0xF8); //50khz channel spacing
+
+    Event event;
+    event.type = EventTypeRadio;
+
+    while(1) {
+        for(uint8_t i = 0; i <= NUM_OF_SUB_BANDS; i++) {
+            highRSSI[i] = MIN_DBM;
+        }
+
+        activeChannel = 300;
+
+        scanFreq(&cc1101);
+
+        if(activeChannel < 256 && highRSSI[activeBand] > RSSI_THRESHOLD) {
+            float freq = base_freq[activeBand] + CHAN_SPA * activeChannel;
+
+            /*
+            printf(
+                "channel: %d, freq: %d, RSSI: %d\n",
+                activeChannel,
+                (uint32_t)(freq * 1000),
+                highRSSI[activeBand]
+            );
+            */
+
+            event.value.radio.freq = freq;
+            event.value.radio.rssi_db = highRSSI[activeBand];
+            xQueueSend(event_queue, (void*)&event, 0);
+
+            if(jamm_on) {
+                jamming(&cc1101, activeBand, activeChannel, 500);
+            } else {
+                osDelay(1000);
+            }
+        } else {
+            // printf("0 carrier sensed\n");
+        }
+
+        osDelay(1);
+    }
+}
 
 
 void cc1101_workaround(void* p) {
 void cc1101_workaround(void* p) {
-    FuriRecordSubscriber* fb_record = furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL);
+    FuriRecordSubscriber* fb_record =
+        furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL);
 
 
     if(fb_record == NULL) {
     if(fb_record == NULL) {
         printf("[cc1101] cannot create fb record\n");
         printf("[cc1101] cannot create fb record\n");

+ 2 - 1
applications/examples/fatfs_list.c

@@ -49,7 +49,8 @@ void fatfs_list(void* p) {
 
 
     furi_log = get_default_log();
     furi_log = get_default_log();
 
 
-    FuriRecordSubscriber* fb_record = furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL);
+    FuriRecordSubscriber* fb_record =
+        furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL);
     if(fb_record == NULL) {
     if(fb_record == NULL) {
         fuprintf(furi_log, "[widget][fatfs_list] cannot create fb record\n");
         fuprintf(furi_log, "[widget][fatfs_list] cannot create fb record\n");
         furiac_exit(NULL);
         furiac_exit(NULL);

+ 2 - 1
applications/examples/u8g2_example.c

@@ -5,7 +5,8 @@ void u8g2_example(void* p) {
     FuriRecordSubscriber* log = get_default_log();
     FuriRecordSubscriber* log = get_default_log();
 
 
     // open record
     // open record
-    FuriRecordSubscriber* fb_record = furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL);
+    FuriRecordSubscriber* fb_record =
+        furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL);
 
 
     if(fb_record == NULL) {
     if(fb_record == NULL) {
         fuprintf(log, "[widget] cannot create fb record\n");
         fuprintf(log, "[widget] cannot create fb record\n");

+ 2 - 1
applications/examples/u8g2_qrcode.c

@@ -14,7 +14,8 @@ void u8g2_qrcode(void* p) {
     FuriRecordSubscriber* log = get_default_log();
     FuriRecordSubscriber* log = get_default_log();
 
 
     // open record
     // open record
-    FuriRecordSubscriber* fb_record = furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL);
+    FuriRecordSubscriber* fb_record =
+        furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL);
 
 
     // Allocate a chunk of memory to store the QR code
     // Allocate a chunk of memory to store the QR code
     // https://github.com/ricmoo/QRCode
     // https://github.com/ricmoo/QRCode

+ 3 - 1
applications/startup.h

@@ -72,7 +72,9 @@ const FlipperStartupApp FLIPPER_STARTUP[] = {
 #endif
 #endif
 
 
 #ifdef APP_CC1101
 #ifdef APP_CC1101
-    {.app = cc1101_workaround, .name = "cc1101_workaround", .libs = {2, FURI_LIB{"display_u8g2", "input_task"}}},
+    {.app = cc1101_workaround,
+     .name = "cc1101_workaround",
+     .libs = {2, FURI_LIB{"display_u8g2", "input_task"}}},
 #endif
 #endif
 
 
 };
 };

+ 0 - 1
applications/tests/furi_valuemutex_test.c

@@ -39,7 +39,6 @@ void test_furi_valuemutex() {
     //read mutex blocking case
     //read mutex blocking case
 }
 }
 
 
-
 /*
 /*
 TEST: concurrent access
 TEST: concurrent access
 
 

+ 0 - 1
applications/tests/minunit_test.c

@@ -67,7 +67,6 @@ MU_TEST_SUITE(test_suite) {
     MU_RUN_TEST(mu_test_furi_create_open);
     MU_RUN_TEST(mu_test_furi_create_open);
     MU_RUN_TEST(mu_test_furi_valuemutex);
     MU_RUN_TEST(mu_test_furi_valuemutex);
     MU_RUN_TEST(mu_test_furi_concurrent_access);
     MU_RUN_TEST(mu_test_furi_concurrent_access);
-
 }
 }
 
 
 int run_minunit() {
 int run_minunit() {

+ 3 - 9
core/api-basic/value-expanders.h

@@ -12,15 +12,10 @@ void COPY_COMPOSE(void* ctx, void* state) {
     read_mutex((ValueMutex*)ctx, state, 0);
     read_mutex((ValueMutex*)ctx, state, 0);
 }
 }
 
 
-typedef enum {
-    UiLayerBelowNotify
-    UiLayerNotify,
-    UiLayerAboveNotify
-} UiLayer;
+typedef enum { UiLayerBelowNotify UiLayerNotify, UiLayerAboveNotify } UiLayer;
 
 
-ValueComposerHandle* add_compose_layer(
-    ValueComposer* composer, ValueComposerCallback cb, void* ctx, uint32_t layer
-);
+ValueComposerHandle*
+add_compose_layer(ValueComposer* composer, ValueComposerCallback cb, void* ctx, uint32_t layer);
 
 
 bool remove_compose_layer(ValueComposerHandle* handle);
 bool remove_compose_layer(ValueComposerHandle* handle);
 
 
@@ -44,7 +39,6 @@ typedef struct {
     PubSub pubsub;
     PubSub pubsub;
 } ValueManager;
 } ValueManager;
 
 
-
 /*
 /*
 acquire value, changes it and send notify with current value.
 acquire value, changes it and send notify with current value.
 */
 */

+ 10 - 7
core/api-hal/api-spi.h

@@ -17,13 +17,17 @@ For transmit/receive data use `spi_xfer` function.
 */
 */
 bool spi_xfer(
 bool spi_xfer(
     SPI_HandleTypeDef* spi,
     SPI_HandleTypeDef* spi,
-    uint8_t* tx_data, uint8_t* rx_data, size_t len,
-    PubSubCallback cb, void* ctx);
+    uint8_t* tx_data,
+    uint8_t* rx_data,
+    size_t len,
+    PubSubCallback cb,
+    void* ctx);
 
 
 /*
 /*
 Blocking verison:
 Blocking verison:
 */
 */
-static inline bool spi_xfer_block(SPI_HandleTypeDef* spi, uint8_t* tx_data, uint8_t* rx_data, size_t len) {
+static inline bool
+spi_xfer_block(SPI_HandleTypeDef* spi, uint8_t* tx_data, uint8_t* rx_data, size_t len) {
     semaphoreInfo s;
     semaphoreInfo s;
     osSemaphore block = createSemaphoreStatic(s);
     osSemaphore block = createSemaphoreStatic(s);
     if(!spi_xfer(spi, tx_data, rx_data, len, RELEASE_SEMAPHORE, (void*)block)) {
     if(!spi_xfer(spi, tx_data, rx_data, len, RELEASE_SEMAPHORE, (void*)block)) {
@@ -52,15 +56,15 @@ typedef struct {
     ValueMutex* bus; ///< <SpiBus*>
     ValueMutex* bus; ///< <SpiBus*>
 } SpiDevice;
 } SpiDevice;
 
 
-## SPI IRQ device
+##SPI IRQ device
 
 
-/*
+    /*
 Many devices (like CC1101 and NFC) present as SPI bus and IRQ line.
 Many devices (like CC1101 and NFC) present as SPI bus and IRQ line.
 For work with it there is special entity `SpiIrqDevice`.
 For work with it there is special entity `SpiIrqDevice`.
 Use `subscribe_pubsub` for subscribinq to irq events.
 Use `subscribe_pubsub` for subscribinq to irq events.
 */
 */
 
 
-typedef struct {
+    typedef struct {
     ValueMutex* bus; ///< <SpiBus*>
     ValueMutex* bus; ///< <SpiBus*>
     PubSub* irq;
     PubSub* irq;
 } SpiIrqDevice;
 } SpiIrqDevice;
@@ -75,7 +79,6 @@ typedef struct {
     ValueMutex* spi; ///< <SPI_HandleTypeDef*>
     ValueMutex* spi; ///< <SPI_HandleTypeDef*>
 } DisplayBus;
 } DisplayBus;
 
 
-
 typedef struct {
 typedef struct {
     ValueMutex* bus; ///< <DisplayBus*>
     ValueMutex* bus; ///< <DisplayBus*>
 } DisplayDevice;
 } DisplayDevice;