cc1101-workaround.cpp 11 KB

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