cc1101-workaround.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #include "flipper.h"
  2. #include "u8g2.h"
  3. #include <stdio.h>
  4. #include "cc1101-workaround/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 rssiTable[256];
  32. uint16_t channelNumber[256];
  33. // counter used to keep track on how many CS has been asserted
  34. uint8_t carrierSenseCounter = 0;
  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 determine 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,
  118. // scan through rssiTable to find highest rssi value
  119. for(i = 0; i < carrierSenseCounter; i++) {
  120. if(rssiTable[i] > highRSSI[subBand]) {
  121. highRSSI[subBand] = rssiTable[i];
  122. selectedChannel[subBand] = channelNumber[i];
  123. }
  124. }
  125. // printf("subBand:------------------>");
  126. // Serial.println(subBand);
  127. // Serial.print("selectedChannel:");
  128. // Serial.println(selectedChannel[subBand]);
  129. // Serial.print("highRSSI:");
  130. // Serial.println(highRSSI[subBand]);
  131. // 1.6) reset carrierSenseCounter
  132. carrierSenseCounter = 0;
  133. } // end band loop
  134. // 2) when all sub bands has been scanned , find best subband and channel
  135. int16_t tempRssi = MIN_DBM;
  136. for(subBand = 0; subBand < NUM_OF_SUB_BANDS; subBand++) {
  137. if(highRSSI[subBand] > tempRssi) {
  138. tempRssi = highRSSI[subBand];
  139. activeChannel = selectedChannel[subBand];
  140. activeBand = subBand;
  141. }
  142. }
  143. // printf("activeBand:**********> %d, activeChannel %d,\n", activeBand, activeChannel);
  144. cc1101->SpiWriteReg(CC1101_MCSM0, 0x18); //enable FS_AUTOCAL
  145. cc1101->SpiWriteReg(CC1101_AGCCTRL2, 0x43); //back to recommended config
  146. cc1101->SpiWriteReg(CC1101_AGCCTRL0, 0x91); //back to recommended config
  147. }
  148. void jamming(CC1101* cc1101, uint8_t band, uint16_t channel, uint16_t miniSec) {
  149. cc1101->SetFreq(freqSettings[band][0], freqSettings[band][1], freqSettings[band][2]);
  150. cc1101->SetChannel(channel);
  151. // digitalWrite(19,0);
  152. cc1101->SetTransmit();
  153. delay(miniSec);
  154. cc1101->SpiStrobe(CC1101_SIDLE);
  155. }
  156. extern "C" void cc1101_workaround(void* p) {
  157. FuriRecordSubscriber* fb_record =
  158. furi_open_deprecated("u8g2_fb", false, false, NULL, NULL, NULL);
  159. if(fb_record == NULL) {
  160. printf("[cc1101] cannot create fb record\n");
  161. furiac_exit(NULL);
  162. }
  163. printf("[cc1101] creating device\n");
  164. CC1101 cc1101(GpioPin{CC1101_CS_GPIO_Port, CC1101_CS_Pin});
  165. printf("[cc1101] init device\n");
  166. uint8_t address = cc1101.Init();
  167. if(address > 0) {
  168. printf("CC1101 init done: %d\n", address);
  169. } else {
  170. printf("CC1101 init fail\n");
  171. }
  172. // RX filter bandwidth 58.035714(0xFD) 100k(0xCD) 200k(0x8D)
  173. cc1101.SpiWriteReg(CC1101_MDMCFG4, 0xCD);
  174. // datarate config 250kBaud for the purpose of fast rssi measurement
  175. cc1101.SpiWriteReg(CC1101_MDMCFG3, 0x3B);
  176. // FEC preamble etc. last 2 bits for channel spacing
  177. cc1101.SpiWriteReg(CC1101_MDMCFG1, 0x20);
  178. // 50khz channel spacing
  179. cc1101.SpiWriteReg(CC1101_MDMCFG0, 0xF8);
  180. while(1) {
  181. u8g2_t* fb = (u8g2_t*)furi_take(fb_record);
  182. if(fb != NULL) {
  183. u8g2_SetFont(fb, u8g2_font_6x10_mf);
  184. u8g2_SetDrawColor(fb, 1);
  185. u8g2_SetFontMode(fb, 1);
  186. u8g2_DrawStr(fb, 2, 12, "cc1101 workaround");
  187. }
  188. furi_commit(fb_record);
  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. }