cc1101-workaround.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include "flipper.h"
  2. #include "u8g2.h"
  3. #include <stdio.h>
  4. #include "cc1101/cc1101.h"
  5. #define MIN_DBM -120
  6. #define STEP_DBM 10
  7. #define RSSI_DELAY 600 //rssi delay in micro second
  8. #define RSSI_THRESHOLD -60
  9. #define START_SUB_BAND 3
  10. #define STOP_SUB_BAND 3
  11. #define NUM_OF_SUB_BANDS 7
  12. #define CAL_INT 20 //cal every 10 channels(every 1MHz)
  13. //variables used to calculate rssi
  14. uint8_t rssi_dec;
  15. int16_t rssi_dBm;
  16. uint8_t rssi_offset[NUM_OF_SUB_BANDS] = {74, 74, 74, 74, 74, 74, 74};
  17. #define CHAN_SPA 0.05 //channel spacing
  18. float base_freq[NUM_OF_SUB_BANDS] = {387, 399.8, 412.6, 425.4, 438.2, 451, 463.8};
  19. //FREQ2,FREQ1,FREQ0
  20. uint8_t freqSettings[NUM_OF_SUB_BANDS][3] = {
  21. {0x0E, 0xE2, 0x76}, //band0
  22. {0x0F, 0x60, 0x76},
  23. {0x0F, 0xDE, 0x76}, //band1
  24. {0x10, 0x5C, 0x76},
  25. {0x10, 0xDA, 0x76},
  26. {0x11, 0x58, 0x8F},
  27. {0x11, 0xD6, 0x8F}}; //band2
  28. //no change in TEST0 WHERE (>430.5MHz) one should change from TEST0=0x0B to 0x09
  29. uint16_t limitTest0Reg[NUM_OF_SUB_BANDS] = {256, 256, 256, 103, 0, 0, 0};
  30. /* setting to use 50khz channel spacing whole band*****************************************/
  31. //int16_t rssiData[NUM_OF_SUB_BANDS][256];
  32. int16_t rssiTable[256];
  33. uint16_t channelNumber[256];
  34. uint8_t carrierSenseCounter = 0; //counter used to keep track on how many CS has been asserted
  35. uint8_t firstChannel[NUM_OF_SUB_BANDS] = {0, 0, 0, 160, 0, 0, 0};
  36. //stop channel in each subband
  37. uint8_t lastChannel[NUM_OF_SUB_BANDS] = {255, 255, 255, 180, 255, 255, 4};
  38. //initialized to a value lower than the rssi threshold/ higher than channel number
  39. int16_t highRSSI[NUM_OF_SUB_BANDS] =
  40. {MIN_DBM, MIN_DBM, MIN_DBM, MIN_DBM, MIN_DBM, MIN_DBM, MIN_DBM};
  41. uint16_t selectedChannel[NUM_OF_SUB_BANDS] = {300, 300, 300, 300, 300, 300, 300};
  42. int8_t activeBand; //store subband that contains strongest signal
  43. uint16_t activeChannel; //
  44. int16_t calRSSI(uint8_t rssi_dec, uint8_t rssiOffset) {
  45. int16_t rssi;
  46. if(rssi_dec >= 128) {
  47. rssi = (int16_t)((int16_t)(rssi_dec - 256) / 2) - rssiOffset;
  48. } else {
  49. rssi = (rssi_dec / 2) - rssiOffset;
  50. }
  51. return rssi;
  52. }
  53. void scanFreq(CC1101* cc1101) {
  54. uint8_t calCounter; //to deterin when to calibrate
  55. uint8_t subBand;
  56. uint16_t channel;
  57. uint16_t i;
  58. float freq;
  59. cc1101->SpiWriteReg(CC1101_MCSM0, 0x08); //disalbe FS_AUTOCAL
  60. cc1101->SpiWriteReg(CC1101_AGCCTRL2, 0x43 | 0x0C); //MAX_DVGA_GAIN to 11 for fast rssi
  61. cc1101->SpiWriteReg(CC1101_AGCCTRL0, 0xB0); //max AGC WAIT_TIME; 0 filter_length
  62. cc1101->SetMod(GFSK); //set to GFSK for fast rssi measurement | +8 is dcfilter off
  63. // 1) loop through all sub bands
  64. for(subBand = START_SUB_BAND; subBand < STOP_SUB_BAND + 1; subBand++) {
  65. //1.1) set subBands freq by FREQ2, FREQ1, FREQ0
  66. cc1101->SetFreq(
  67. freqSettings[subBand][0], freqSettings[subBand][1], freqSettings[subBand][2]);
  68. //1.2) set TEST0--maybe!
  69. //1.3) reset calibration counter
  70. calCounter = 0;
  71. // 1.4) loop throuhg all channels
  72. for(channel = firstChannel[subBand]; channel <= lastChannel[subBand]; channel++) {
  73. uint8_t pktStatus;
  74. //1.4.1) set channel register
  75. cc1101->SetChannel(channel);
  76. //1.4.2) set TEST0
  77. if(channel == limitTest0Reg[subBand]) {
  78. //set test0 to 0x09
  79. cc1101->SpiWriteReg(CC1101_TEST0, 0x09);
  80. //set FSCAL2 to 0x2A to force VCO HIGH
  81. cc1101->SpiWriteReg(CC1101_FSCAL2, 0x2A);
  82. //clear calCounter to invoke mannual calibration
  83. calCounter = 0;
  84. }
  85. //1.4.3) calibrate every 1MHz
  86. if(calCounter++ == 0) {
  87. //perform a manual calibration by issuing SCAL command
  88. cc1101->SpiStrobe(CC1101_SCAL);
  89. }
  90. //1.4.4) reset calCounter when 1MHz reached
  91. if(calCounter == CAL_INT) {
  92. calCounter = 0;
  93. }
  94. // 1.4.5-6 enter rx mode
  95. cc1101->SetReceive();
  96. //1.4.7 wait for RSSI to be valid: less than 1.5ms
  97. delayMicroseconds(RSSI_DELAY);
  98. // 1.4.8) read PKTSTATUS register while the radio is in RX state
  99. pktStatus = cc1101->SpiReadStatus(CC1101_PKTSTATUS);
  100. // 1.4.9) enter IDLE state by issuing a SIDLE command
  101. cc1101->SpiStrobe(CC1101_SIDLE);
  102. // 1.4.10) check if CS is assearted
  103. // //read rssi value and converto to dBm form
  104. rssi_dec = (uint8_t)cc1101->SpiReadStatus(CC1101_RSSI);
  105. rssi_dBm = calRSSI(rssi_dec, rssi_offset[subBand]);
  106. //rssiData[subBand][channel]=rssi_dBm;
  107. if(pktStatus & 0x40) { //CS assearted
  108. //store rssi value and corresponding channel number
  109. rssiTable[carrierSenseCounter] = rssi_dBm;
  110. channelNumber[carrierSenseCounter] = channel;
  111. carrierSenseCounter++;
  112. }
  113. #ifdef CC1101_DEBUG
  114. printf("rssi_dBm: %d\n", rssi_dBm);
  115. #endif
  116. } //end channel lop
  117. //1.5)before moving to next sub band, scan through rssiTable to find highest rssi value
  118. for(i = 0; i < carrierSenseCounter; i++) {
  119. if(rssiTable[i] > highRSSI[subBand]) {
  120. highRSSI[subBand] = rssiTable[i];
  121. selectedChannel[subBand] = channelNumber[i];
  122. }
  123. }
  124. // Serial.print("subBand:------------------>");
  125. // Serial.println(subBand);
  126. // Serial.print("selectedChannel:");
  127. // Serial.println(selectedChannel[subBand]);
  128. // Serial.print("highRSSI:");
  129. // Serial.println(highRSSI[subBand]);
  130. //1.6) reset carrierSenseCounter
  131. carrierSenseCounter = 0;
  132. } // end band loop
  133. //2) when all sub bands has been scanned , find best subband and channel
  134. int16_t tempRssi = MIN_DBM;
  135. for(subBand = 0; subBand < NUM_OF_SUB_BANDS; subBand++) {
  136. if(highRSSI[subBand] > tempRssi) {
  137. tempRssi = highRSSI[subBand];
  138. activeChannel = selectedChannel[subBand];
  139. activeBand = subBand;
  140. }
  141. }
  142. // printf("activeBand:**********> %d, activeChannel %d,\n", activeBand, activeChannel);
  143. cc1101->SpiWriteReg(CC1101_MCSM0, 0x18); //enable FS_AUTOCAL
  144. cc1101->SpiWriteReg(CC1101_AGCCTRL2, 0x43); //back to recommended config
  145. cc1101->SpiWriteReg(CC1101_AGCCTRL0, 0x91); //back to recommended config
  146. }
  147. void jamming(CC1101* cc1101, uint8_t band, uint16_t channel, uint16_t miniSec) {
  148. cc1101->SetFreq(freqSettings[band][0], freqSettings[band][1], freqSettings[band][2]);
  149. cc1101->SetChannel(channel);
  150. // digitalWrite(19,0);
  151. cc1101->SetTransmit();
  152. delay(miniSec);
  153. cc1101->SpiStrobe(CC1101_SIDLE);
  154. }
  155. extern QueueHandle_t event_queue;
  156. bool jamm_on = false;
  157. void set_jam(bool state) {
  158. jamm_on = state;
  159. }
  160. extern "C" void radio() {
  161. CC1101 cc1101(SS_PIN, GDO0, GDO2);
  162. uint8_t address = cc1101.Init();
  163. if(address > 0) {
  164. printf("CC1101 init done: %d\n", address);
  165. } else {
  166. printf("CC1101 init fail\n");
  167. }
  168. /* setting to use 50khz channel spacing**************************************/
  169. cc1101.SpiWriteReg(
  170. CC1101_MDMCFG4, 0xCD); // RX filter bandwidth 58.035714(0xFD) 100k(0xCD) 200k(0x8D)
  171. cc1101.SpiWriteReg(
  172. CC1101_MDMCFG3, 0x3B); //datarate config 250kBaud for the purpose of fast rssi measurement
  173. cc1101.SpiWriteReg(CC1101_MDMCFG1, 0x20); //FEC preamble etc. last 2 bits for channel spacing
  174. cc1101.SpiWriteReg(CC1101_MDMCFG0, 0xF8); //50khz channel spacing
  175. Event event;
  176. event.type = EventTypeRadio;
  177. while(1) {
  178. for(uint8_t i = 0; i <= NUM_OF_SUB_BANDS; i++) {
  179. highRSSI[i] = MIN_DBM;
  180. }
  181. activeChannel = 300;
  182. scanFreq(&cc1101);
  183. if(activeChannel < 256 && highRSSI[activeBand] > RSSI_THRESHOLD) {
  184. float freq = base_freq[activeBand] + CHAN_SPA * activeChannel;
  185. /*
  186. printf(
  187. "channel: %d, freq: %d, RSSI: %d\n",
  188. activeChannel,
  189. (uint32_t)(freq * 1000),
  190. highRSSI[activeBand]
  191. );
  192. */
  193. event.value.radio.freq = freq;
  194. event.value.radio.rssi_db = highRSSI[activeBand];
  195. xQueueSend(event_queue, (void*)&event, 0);
  196. if(jamm_on) {
  197. jamming(&cc1101, activeBand, activeChannel, 500);
  198. } else {
  199. osDelay(1000);
  200. }
  201. } else {
  202. // printf("0 carrier sensed\n");
  203. }
  204. osDelay(1);
  205. }
  206. }
  207. void cc1101_workaround(void* p) {
  208. FuriRecordSubscriber* fb_record =
  209. furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL);
  210. if(fb_record == NULL) {
  211. printf("[cc1101] cannot create fb record\n");
  212. furiac_exit(NULL);
  213. }
  214. while(1) {
  215. u8g2_t* fb = furi_take(fb_record);
  216. if(fb != NULL) {
  217. u8g2_SetFont(fb, u8g2_font_6x10_mf);
  218. u8g2_SetDrawColor(fb, 1);
  219. u8g2_SetFontMode(fb, 1);
  220. u8g2_DrawStr(fb, 2, 12, "cc1101 workaround");
  221. }
  222. furi_commit(fb_record);
  223. delay(1000);
  224. }
  225. }