cc1101-workaround.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. // no change in TEST0 WHERE (>430.5MHz) one should change from TEST0=0x0B to 0x09
  17. uint16_t limitTest0Reg[NUM_OF_SUB_BANDS] = {256, 256, 256, 103, 0, 0, 0};
  18. /* setting to use 50khz channel spacing whole band*****************************************/
  19. int16_t rssiTable[256];
  20. uint16_t channelNumber[256];
  21. // counter used to keep track on how many CS has been asserted
  22. uint8_t carrierSenseCounter = 0;
  23. int16_t calRSSI(uint8_t rssi_dec, uint8_t rssiOffset) {
  24. int16_t rssi;
  25. if(rssi_dec >= 128) {
  26. rssi = (int16_t)((int16_t)(rssi_dec - 256) / 2) - rssiOffset;
  27. } else {
  28. rssi = (rssi_dec / 2) - rssiOffset;
  29. }
  30. return rssi;
  31. }
  32. /*
  33. void scanFreq(CC1101* cc1101) {
  34. uint8_t calCounter; // to determine when to calibrate
  35. uint8_t subBand;
  36. uint16_t channel;
  37. uint16_t i;
  38. float freq;
  39. cc1101->SpiWriteReg(CC1101_MCSM0, 0x08); // disalbe FS_AUTOCAL
  40. cc1101->SpiWriteReg(CC1101_AGCCTRL2, 0x43 | 0x0C); // MAX_DVGA_GAIN to 11 for fast rssi
  41. cc1101->SpiWriteReg(CC1101_AGCCTRL0, 0xB0); // max AGC WAIT_TIME; 0 filter_length
  42. cc1101->SetMod(GFSK); // set to GFSK for fast rssi measurement | +8 is dcfilter off
  43. // 1) loop through all sub bands
  44. for(subBand = START_SUB_BAND; subBand < STOP_SUB_BAND + 1; subBand++) {
  45. // 1.1) set subBands freq by FREQ2, FREQ1, FREQ0
  46. cc1101->SetFreq(
  47. freqSettings[subBand][0], freqSettings[subBand][1], freqSettings[subBand][2]);
  48. // 1.2) set TEST0--maybe!
  49. // 1.3) reset calibration counter
  50. calCounter = 0;
  51. // 1.4) loop throuhg all channels
  52. for(channel = firstChannel[subBand]; channel <= lastChannel[subBand]; channel++) {
  53. uint8_t pktStatus;
  54. // 1.4.1) set channel register
  55. cc1101->SetChannel(channel);
  56. // 1.4.2) set TEST0
  57. if(channel == limitTest0Reg[subBand]) {
  58. //set test0 to 0x09
  59. cc1101->SpiWriteReg(CC1101_TEST0, 0x09);
  60. //set FSCAL2 to 0x2A to force VCO HIGH
  61. cc1101->SpiWriteReg(CC1101_FSCAL2, 0x2A);
  62. //clear calCounter to invoke mannual calibration
  63. calCounter = 0;
  64. }
  65. // 1.4.3) calibrate every 1MHz
  66. if(calCounter++ == 0) {
  67. // perform a manual calibration by issuing SCAL command
  68. cc1101->SpiStrobe(CC1101_SCAL);
  69. }
  70. // 1.4.4) reset calCounter when 1MHz reached
  71. if(calCounter == CAL_INT) {
  72. calCounter = 0;
  73. }
  74. // 1.4.5-6 enter rx mode
  75. cc1101->SetReceive();
  76. // 1.4.7 wait for RSSI to be valid: less than 1.5ms
  77. delayMicroseconds(RSSI_DELAY);
  78. // 1.4.8) read PKTSTATUS register while the radio is in RX state
  79. pktStatus = cc1101->SpiReadStatus(CC1101_PKTSTATUS);
  80. // 1.4.9) enter IDLE state by issuing a SIDLE command
  81. cc1101->SpiStrobe(CC1101_SIDLE);
  82. // 1.4.10) check if CS is assearted
  83. // //read rssi value and converto to dBm form
  84. rssi_dec = (uint8_t)cc1101->SpiReadStatus(CC1101_RSSI);
  85. rssi_dBm = calRSSI(rssi_dec, rssi_offset[subBand]);
  86. // rssiData[subBand][channel]=rssi_dBm;
  87. if(pktStatus & 0x40) { //CS assearted
  88. // store rssi value and corresponding channel number
  89. rssiTable[carrierSenseCounter] = rssi_dBm;
  90. channelNumber[carrierSenseCounter] = channel;
  91. carrierSenseCounter++;
  92. }
  93. #ifdef CC1101_DEBUG
  94. printf("rssi_dBm: %d\n", rssi_dBm);
  95. #endif
  96. } // end channel lop
  97. // 1.5)before moving to next sub band,
  98. // scan through rssiTable to find highest rssi value
  99. for(i = 0; i < carrierSenseCounter; i++) {
  100. if(rssiTable[i] > highRSSI[subBand]) {
  101. highRSSI[subBand] = rssiTable[i];
  102. selectedChannel[subBand] = channelNumber[i];
  103. }
  104. }
  105. // printf("subBand:------------------>");
  106. // Serial.println(subBand);
  107. // Serial.print("selectedChannel:");
  108. // Serial.println(selectedChannel[subBand]);
  109. // Serial.print("highRSSI:");
  110. // Serial.println(highRSSI[subBand]);
  111. // 1.6) reset carrierSenseCounter
  112. carrierSenseCounter = 0;
  113. } // end band loop
  114. // 2) when all sub bands has been scanned , find best subband and channel
  115. int16_t tempRssi = MIN_DBM;
  116. for(subBand = 0; subBand < NUM_OF_SUB_BANDS; subBand++) {
  117. if(highRSSI[subBand] > tempRssi) {
  118. tempRssi = highRSSI[subBand];
  119. activeChannel = selectedChannel[subBand];
  120. activeBand = subBand;
  121. }
  122. }
  123. // printf("activeBand:**********> %d, activeChannel %d,\n", activeBand, activeChannel);
  124. cc1101->SpiWriteReg(CC1101_MCSM0, 0x18); //enable FS_AUTOCAL
  125. cc1101->SpiWriteReg(CC1101_AGCCTRL2, 0x43); //back to recommended config
  126. cc1101->SpiWriteReg(CC1101_AGCCTRL0, 0x91); //back to recommended config
  127. }
  128. void tx(CC1101* cc1101, uint8_t band, uint16_t channel, uint16_t miniSec) {
  129. cc1101->SetFreq(freqSettings[band][0], freqSettings[band][1], freqSettings[band][2]);
  130. cc1101->SetChannel(channel);
  131. // digitalWrite(19,0);
  132. cc1101->SetTransmit();
  133. delay(miniSec);
  134. cc1101->SpiStrobe(CC1101_SIDLE);
  135. }
  136. */
  137. typedef struct {
  138. float base_freq;
  139. uint8_t settings[3]; // FREQ2, FREQ1, FREQ0
  140. uint8_t first_channel;
  141. uint8_t last_channel;
  142. } Band;
  143. typedef struct {
  144. Band* band;
  145. uint16_t channel;
  146. } FreqConfig;
  147. Band bands[NUM_OF_SUB_BANDS] = {
  148. {387, {0x0E, 0xE2, 0x76}, 0, 255},
  149. {399.8, {0x0F, 0x60, 0x76}, 0, 255},
  150. {412.6, {0x0F, 0xDE, 0x76}, 0, 255},
  151. {425.4, {0x10, 0x5C, 0x76}, 160, 180},
  152. {438.2, {0x10, 0xDA, 0x76}, 0, 255},
  153. {451, {0x11, 0x58, 0x8F}, 0, 255},
  154. {463.8, {0x11, 0xD6, 0x8F}, 0, 4},
  155. };
  156. FreqConfig FREQ_LIST[] = {
  157. {&bands[0], 0},
  158. {&bands[0], 50},
  159. {&bands[0], 100},
  160. {&bands[0], 150},
  161. {&bands[0], 200},
  162. {&bands[1], 0},
  163. {&bands[1], 50},
  164. {&bands[1], 100},
  165. {&bands[1], 150},
  166. {&bands[1], 200},
  167. {&bands[2], 0},
  168. {&bands[2], 50},
  169. {&bands[2], 100},
  170. {&bands[2], 150},
  171. {&bands[2], 200},
  172. {&bands[3], 160},
  173. {&bands[3], 170},
  174. {&bands[4], 0},
  175. {&bands[4], 50},
  176. {&bands[4], 100},
  177. {&bands[4], 150},
  178. {&bands[4], 200},
  179. {&bands[5], 0},
  180. {&bands[5], 50},
  181. {&bands[5], 100},
  182. {&bands[5], 150},
  183. {&bands[5], 200},
  184. {&bands[6], 0},
  185. };
  186. typedef enum {
  187. EventTypeTick,
  188. EventTypeKey,
  189. } EventType;
  190. typedef struct {
  191. union {
  192. InputEvent input;
  193. } value;
  194. EventType type;
  195. } Event;
  196. typedef enum {
  197. ModeRx,
  198. ModeTx
  199. } Mode;
  200. typedef struct {
  201. Mode mode;
  202. size_t active_freq;
  203. } State;
  204. static void render_callback(CanvasApi* canvas, void* ctx) {
  205. State* state = (State*)acquire_mutex((ValueMutex*)ctx, 25);
  206. canvas->clear(canvas);
  207. canvas->set_color(canvas, ColorBlack);
  208. canvas->set_font(canvas, FontPrimary);
  209. canvas->draw_str(canvas, 2, 12, "cc1101 workaround");
  210. {
  211. char buf[24];
  212. FreqConfig conf = FREQ_LIST[state->active_freq];
  213. float freq = conf.band->base_freq + CHAN_SPA * conf.channel;
  214. sprintf(buf, "freq: %ld.%02ld MHz", (uint32_t)freq, (uint32_t)(freq * 100.) % 100);
  215. canvas->set_font(canvas, FontSecondary);
  216. canvas->draw_str(canvas, 2, 25, buf);
  217. }
  218. release_mutex((ValueMutex*)ctx, state);
  219. }
  220. static void input_callback(InputEvent* input_event, void* ctx) {
  221. osMessageQueueId_t event_queue = (QueueHandle_t)ctx;
  222. Event event;
  223. event.type = EventTypeKey;
  224. event.value.input = *input_event;
  225. osMessageQueuePut(event_queue, &event, 0, 0);
  226. }
  227. extern "C" void cc1101_workaround(void* p) {
  228. osMessageQueueId_t event_queue =
  229. osMessageQueueNew(1, sizeof(Event), NULL);
  230. assert(event_queue);
  231. State _state;
  232. _state.mode = ModeRx;
  233. _state.active_freq = 0;
  234. ValueMutex state_mutex;
  235. if(!init_mutex(&state_mutex, &_state, sizeof(State))) {
  236. printf("[cc1101] cannot create mutex\n");
  237. furiac_exit(NULL);
  238. }
  239. Widget* widget = widget_alloc();
  240. widget_draw_callback_set(widget, render_callback, &state_mutex);
  241. widget_input_callback_set(widget, input_callback, event_queue);
  242. // Open GUI and register widget
  243. GuiApi* gui = (GuiApi*)furi_open("gui");
  244. if(gui == NULL) {
  245. printf("[cc1101] gui is not available\n");
  246. furiac_exit(NULL);
  247. }
  248. gui->add_widget(gui, widget, WidgetLayerFullscreen);
  249. printf("[cc1101] creating device\n");
  250. CC1101 cc1101(GpioPin{CC1101_CS_GPIO_Port, CC1101_CS_Pin});
  251. printf("[cc1101] init device\n");
  252. uint8_t address = cc1101.Init();
  253. if(address > 0) {
  254. printf("[cc1101] init done: %d\n", address);
  255. } else {
  256. printf("[cc1101] init fail\n");
  257. furiac_exit(NULL);
  258. }
  259. // RX filter bandwidth 58.035714(0xFD) 100k(0xCD) 200k(0x8D)
  260. cc1101.SpiWriteReg(CC1101_MDMCFG4, 0xCD);
  261. // datarate config 250kBaud for the purpose of fast rssi measurement
  262. cc1101.SpiWriteReg(CC1101_MDMCFG3, 0x3B);
  263. // FEC preamble etc. last 2 bits for channel spacing
  264. cc1101.SpiWriteReg(CC1101_MDMCFG1, 0x20);
  265. // 50khz channel spacing
  266. cc1101.SpiWriteReg(CC1101_MDMCFG0, 0xF8);
  267. Event event;
  268. while(1) {
  269. if(osMessageQueueGet(event_queue, &event, NULL, osWaitForever) == osOK) {
  270. State* state = (State*)acquire_mutex_block(&state_mutex);
  271. if(event.type == EventTypeKey) {
  272. if(event.value.input.state && event.value.input.input == InputBack) {
  273. printf("[cc1101] bye!\n");
  274. // TODO remove all widgets create by app
  275. widget_enabled_set(widget, false);
  276. furiac_exit(NULL);
  277. }
  278. if(event.value.input.state && event.value.input.input == InputUp) {
  279. if(state->active_freq > 0) {
  280. state->active_freq--;
  281. }
  282. }
  283. if(event.value.input.state && event.value.input.input == InputDown) {
  284. if(state->active_freq < (sizeof(FREQ_LIST)/sizeof(FREQ_LIST[0]) - 1)) {
  285. state->active_freq++;
  286. }
  287. }
  288. }
  289. release_mutex(&state_mutex, state);
  290. widget_update(widget);
  291. }
  292. }
  293. /*
  294. while(1) {
  295. for(uint8_t i = 0; i <= NUM_OF_SUB_BANDS; i++) {
  296. highRSSI[i] = MIN_DBM;
  297. }
  298. activeChannel = 300;
  299. tx(&cc1101, activeBand, activeChannel, 500);
  300. scanFreq(&cc1101);
  301. if(activeChannel < 256 && highRSSI[activeBand] > RSSI_THRESHOLD) {
  302. float freq = base_freq[activeBand] + CHAN_SPA * activeChannel;
  303. printf(
  304. "channel: %d, freq: %d, RSSI: %d\n",
  305. activeChannel,
  306. (uint32_t)(freq * 1000),
  307. highRSSI[activeBand]
  308. );
  309. *
  310. if(tx_on) {
  311. tx(&cc1101, activeBand, activeChannel, 500);
  312. } else {
  313. osDelay(1000);
  314. }
  315. *
  316. } else {
  317. // printf("0 carrier sensed\n");
  318. }
  319. *
  320. uint8_t band = 4; // 438.2 MHz
  321. *
  322. cc1101.SetFreq(freqSettings[band][0], freqSettings[band][1], freqSettings[band][2]);
  323. cc1101.SetChannel(0);
  324. cc1101.SetTransmit();
  325. delay(5000);
  326. cc1101.SpiStrobe(CC1101_SIDLE);
  327. *
  328. delay(1000);
  329. }
  330. */
  331. }