cc1101-workaround.cpp 9.1 KB

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