cc1101-workaround.cpp 12 KB

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