cc1101-workaround.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #include "flipper.h"
  2. #include "cc1101-workaround/cc1101.h"
  3. #define RSSI_DELAY 5000 //rssi delay in micro second
  4. #define NUM_OF_SUB_BANDS 7
  5. #define CHAN_SPA 0.05 // channel spacing
  6. int16_t rssi_to_dbm(uint8_t rssi_dec, uint8_t rssiOffset) {
  7. int16_t rssi;
  8. if(rssi_dec >= 128) {
  9. rssi = (int16_t)((int16_t)(rssi_dec - 256) / 2) - rssiOffset;
  10. } else {
  11. rssi = (rssi_dec / 2) - rssiOffset;
  12. }
  13. return rssi;
  14. }
  15. typedef struct {
  16. float base_freq;
  17. uint8_t reg[3]; // FREQ2, FREQ1, FREQ0
  18. uint8_t first_channel;
  19. uint8_t last_channel;
  20. uint8_t rssi_offset;
  21. } Band;
  22. typedef struct {
  23. Band* band;
  24. uint16_t channel;
  25. } FreqConfig;
  26. void setup_freq(CC1101* cc1101, FreqConfig* config) {
  27. cc1101->SpiWriteReg(CC1101_MCSM0, 0x08); // disalbe FS_AUTOCAL
  28. cc1101->SpiWriteReg(CC1101_AGCCTRL2, 0x43 | 0x0C); // MAX_DVGA_GAIN to 11 for fast rssi
  29. cc1101->SpiWriteReg(CC1101_AGCCTRL0, 0xB0); // max AGC WAIT_TIME; 0 filter_length
  30. cc1101->SetMod(GFSK); // set to GFSK for fast rssi measurement | +8 is dcfilter off
  31. cc1101->SetFreq(config->band->reg[0], config->band->reg[1], config->band->reg[2]);
  32. cc1101->SetChannel(config->channel);
  33. //set test0 to 0x09
  34. cc1101->SpiWriteReg(CC1101_TEST0, 0x09);
  35. //set FSCAL2 to 0x2A to force VCO HIGH
  36. cc1101->SpiWriteReg(CC1101_FSCAL2, 0x2A);
  37. // perform a manual calibration by issuing SCAL command
  38. cc1101->SpiStrobe(CC1101_SCAL);
  39. /*
  40. // Cleanup:
  41. cc1101->SpiWriteReg(CC1101_MCSM0, 0x18); //enable FS_AUTOCAL
  42. cc1101->SpiWriteReg(CC1101_AGCCTRL2, 0x43); //back to recommended config
  43. cc1101->SpiWriteReg(CC1101_AGCCTRL0, 0x91); //back to recommended config
  44. */
  45. }
  46. int16_t rx_rssi(CC1101* cc1101, FreqConfig* config) {
  47. cc1101->SetReceive();
  48. delayMicroseconds(RSSI_DELAY);
  49. // 1.4.8) read PKTSTATUS register while the radio is in RX state
  50. uint8_t _pkt_status = cc1101->SpiReadStatus(CC1101_PKTSTATUS);
  51. // 1.4.9) enter IDLE state by issuing a SIDLE command
  52. cc1101->SpiStrobe(CC1101_SIDLE);
  53. // //read rssi value and converto to dBm form
  54. uint8_t rssi_dec = (uint8_t)cc1101->SpiReadStatus(CC1101_RSSI);
  55. int16_t rssi_dBm = rssi_to_dbm(rssi_dec, config->band->rssi_offset);
  56. return rssi_dBm;
  57. }
  58. void tx(CC1101* cc1101) {
  59. cc1101->SetTransmit();
  60. }
  61. void idle(CC1101* cc1101) {
  62. cc1101->SpiStrobe(CC1101_SIDLE);
  63. }
  64. Band bands[NUM_OF_SUB_BANDS] = {
  65. {387, {0x0E, 0xE2, 0x76}, 0, 255, 74},
  66. {399.8, {0x0F, 0x60, 0x76}, 0, 255, 74},
  67. {412.6, {0x0F, 0xDE, 0x76}, 0, 255, 74},
  68. {425.4, {0x10, 0x5C, 0x76}, 160, 180, 74},
  69. {438.2, {0x10, 0xDA, 0x76}, 0, 255, 74},
  70. {451, {0x11, 0x58, 0x8F}, 0, 255, 74},
  71. {463.8, {0x11, 0xD6, 0x8F}, 0, 4, 74},
  72. };
  73. FreqConfig FREQ_LIST[] = {
  74. {&bands[0], 0},
  75. {&bands[0], 50},
  76. {&bands[0], 100},
  77. {&bands[0], 150},
  78. {&bands[0], 200},
  79. {&bands[1], 0},
  80. {&bands[1], 50},
  81. {&bands[1], 100},
  82. {&bands[1], 150},
  83. {&bands[1], 200},
  84. {&bands[2], 0},
  85. {&bands[2], 50},
  86. {&bands[2], 100},
  87. {&bands[2], 150},
  88. {&bands[2], 200},
  89. {&bands[3], 160},
  90. {&bands[3], 170},
  91. {&bands[4], 0},
  92. {&bands[4], 50},
  93. {&bands[4], 100},
  94. {&bands[4], 150},
  95. {&bands[4], 200},
  96. {&bands[5], 0},
  97. {&bands[5], 50},
  98. {&bands[5], 100},
  99. {&bands[5], 150},
  100. {&bands[5], 200},
  101. {&bands[6], 0},
  102. };
  103. typedef enum {
  104. EventTypeTick,
  105. EventTypeKey,
  106. } EventType;
  107. typedef struct {
  108. union {
  109. InputEvent input;
  110. } value;
  111. EventType type;
  112. } Event;
  113. typedef enum {
  114. ModeRx,
  115. ModeTx
  116. } Mode;
  117. typedef struct {
  118. Mode mode;
  119. size_t active_freq;
  120. int16_t last_rssi;
  121. bool need_cc1101_conf;
  122. } State;
  123. static void render_callback(CanvasApi* canvas, void* ctx) {
  124. State* state = (State*)acquire_mutex((ValueMutex*)ctx, 25);
  125. canvas->clear(canvas);
  126. canvas->set_color(canvas, ColorBlack);
  127. canvas->set_font(canvas, FontPrimary);
  128. canvas->draw_str(canvas, 2, 12, "cc1101 workaround");
  129. {
  130. char buf[24];
  131. FreqConfig conf = FREQ_LIST[state->active_freq];
  132. float freq = conf.band->base_freq + CHAN_SPA * conf.channel;
  133. sprintf(buf, "freq: %ld.%02ld MHz", (uint32_t)freq, (uint32_t)(freq * 100.) % 100);
  134. canvas->set_font(canvas, FontSecondary);
  135. canvas->draw_str(canvas, 2, 25, buf);
  136. }
  137. {
  138. canvas->set_font(canvas, FontSecondary);
  139. if(state->need_cc1101_conf) {
  140. canvas->draw_str(canvas, 2, 36, "mode: configuring...");
  141. } else if(state->mode == ModeRx) {
  142. canvas->draw_str(canvas, 2, 36, "mode: RX");
  143. } else if(state->mode == ModeTx) {
  144. canvas->draw_str(canvas, 2, 36, "mode: TX");
  145. } else {
  146. canvas->draw_str(canvas, 2, 36, "mode: unknown");
  147. }
  148. }
  149. {
  150. if(!state->need_cc1101_conf && state->mode == ModeRx) {
  151. char buf[24];
  152. sprintf(buf, "RSSI: %d dBm", state->last_rssi);
  153. canvas->set_font(canvas, FontSecondary);
  154. canvas->draw_str(canvas, 2, 48, buf);
  155. }
  156. }
  157. release_mutex((ValueMutex*)ctx, state);
  158. }
  159. static void input_callback(InputEvent* input_event, void* ctx) {
  160. osMessageQueueId_t event_queue = (QueueHandle_t)ctx;
  161. Event event;
  162. event.type = EventTypeKey;
  163. event.value.input = *input_event;
  164. osMessageQueuePut(event_queue, &event, 0, 0);
  165. }
  166. extern "C" void cc1101_workaround(void* p) {
  167. osMessageQueueId_t event_queue =
  168. osMessageQueueNew(1, sizeof(Event), NULL);
  169. assert(event_queue);
  170. State _state;
  171. _state.mode = ModeRx;
  172. _state.active_freq = 0;
  173. _state.need_cc1101_conf = true;
  174. _state.last_rssi = 0;
  175. ValueMutex state_mutex;
  176. if(!init_mutex(&state_mutex, &_state, sizeof(State))) {
  177. printf("[cc1101] cannot create mutex\n");
  178. furiac_exit(NULL);
  179. }
  180. Widget* widget = widget_alloc();
  181. widget_draw_callback_set(widget, render_callback, &state_mutex);
  182. widget_input_callback_set(widget, input_callback, event_queue);
  183. // Open GUI and register widget
  184. GuiApi* gui = (GuiApi*)furi_open("gui");
  185. if(gui == NULL) {
  186. printf("[cc1101] gui is not available\n");
  187. furiac_exit(NULL);
  188. }
  189. gui->add_widget(gui, widget, WidgetLayerFullscreen);
  190. printf("[cc1101] creating device\n");
  191. CC1101 cc1101(GpioPin{CC1101_CS_GPIO_Port, CC1101_CS_Pin});
  192. printf("[cc1101] init device\n");
  193. uint8_t address = cc1101.Init();
  194. if(address > 0) {
  195. printf("[cc1101] init done: %d\n", address);
  196. } else {
  197. printf("[cc1101] init fail\n");
  198. furiac_exit(NULL);
  199. }
  200. // RX filter bandwidth 58.035714(0xFD) 100k(0xCD) 200k(0x8D)
  201. cc1101.SpiWriteReg(CC1101_MDMCFG4, 0xCD);
  202. // datarate config 250kBaud for the purpose of fast rssi measurement
  203. cc1101.SpiWriteReg(CC1101_MDMCFG3, 0x3B);
  204. // FEC preamble etc. last 2 bits for channel spacing
  205. cc1101.SpiWriteReg(CC1101_MDMCFG1, 0x20);
  206. // 50khz channel spacing
  207. cc1101.SpiWriteReg(CC1101_MDMCFG0, 0xF8);
  208. // create pin
  209. GpioPin led = {GPIOA, GPIO_PIN_8};
  210. // configure pin
  211. pinMode(led, GpioModeOpenDrain);
  212. const int16_t RSSI_THRESHOLD = -89;
  213. Event event;
  214. while(1) {
  215. if(osMessageQueueGet(event_queue, &event, NULL, 150) == osOK) {
  216. State* state = (State*)acquire_mutex_block(&state_mutex);
  217. if(event.type == EventTypeKey) {
  218. if(event.value.input.state && event.value.input.input == InputBack) {
  219. printf("[cc1101] bye!\n");
  220. // TODO remove all widgets create by app
  221. widget_enabled_set(widget, false);
  222. furiac_exit(NULL);
  223. }
  224. if(event.value.input.state && event.value.input.input == InputUp) {
  225. if(state->active_freq > 0) {
  226. state->active_freq--;
  227. state->need_cc1101_conf = true;
  228. }
  229. }
  230. if(event.value.input.state && event.value.input.input == InputDown) {
  231. if(state->active_freq < (sizeof(FREQ_LIST)/sizeof(FREQ_LIST[0]) - 1)) {
  232. state->active_freq++;
  233. state->need_cc1101_conf = true;
  234. }
  235. }
  236. }
  237. if(state->need_cc1101_conf) {
  238. setup_freq(&cc1101, &FREQ_LIST[state->active_freq]);
  239. if(state->mode == ModeRx) {
  240. state->last_rssi = rx_rssi(&cc1101, &FREQ_LIST[state->active_freq]);
  241. } else if(state->mode == ModeTx) {
  242. tx(&cc1101);
  243. }
  244. state->need_cc1101_conf = false;
  245. }
  246. digitalWrite(led, state->last_rssi > RSSI_THRESHOLD ? LOW : HIGH);
  247. release_mutex(&state_mutex, state);
  248. widget_update(widget);
  249. } else {
  250. State* state = (State*)acquire_mutex_block(&state_mutex);
  251. if(!state->need_cc1101_conf && state->mode == ModeRx) {
  252. state->last_rssi = rx_rssi(&cc1101, &FREQ_LIST[state->active_freq]);
  253. }
  254. digitalWrite(led, state->last_rssi > RSSI_THRESHOLD ? LOW : HIGH);
  255. release_mutex(&state_mutex, state);
  256. widget_update(widget);
  257. }
  258. }
  259. /*
  260. while(1) {
  261. for(uint8_t i = 0; i <= NUM_OF_SUB_BANDS; i++) {
  262. highRSSI[i] = MIN_DBM;
  263. }
  264. activeChannel = 300;
  265. tx(&cc1101, activeBand, activeChannel, 500);
  266. scanFreq(&cc1101);
  267. if(activeChannel < 256 && highRSSI[activeBand] > RSSI_THRESHOLD) {
  268. float freq = base_freq[activeBand] + CHAN_SPA * activeChannel;
  269. printf(
  270. "channel: %d, freq: %d, RSSI: %d\n",
  271. activeChannel,
  272. (uint32_t)(freq * 1000),
  273. highRSSI[activeBand]
  274. );
  275. *
  276. if(tx_on) {
  277. tx(&cc1101, activeBand, activeChannel, 500);
  278. } else {
  279. osDelay(1000);
  280. }
  281. *
  282. } else {
  283. // printf("0 carrier sensed\n");
  284. }
  285. *
  286. uint8_t band = 4; // 438.2 MHz
  287. *
  288. cc1101.SetFreq(freqSettings[band][0], freqSettings[band][1], freqSettings[band][2]);
  289. cc1101.SetChannel(0);
  290. cc1101.SetTransmit();
  291. delay(5000);
  292. cc1101.SpiStrobe(CC1101_SIDLE);
  293. *
  294. delay(1000);
  295. }
  296. */
  297. }