cc1101-workaround.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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}, {&bands[0], 50}, {&bands[0], 100}, {&bands[0], 150}, {&bands[0], 200},
  78. {&bands[1], 0}, {&bands[1], 50}, {&bands[1], 100}, {&bands[1], 150}, {&bands[1], 200},
  79. {&bands[2], 0}, {&bands[2], 50}, {&bands[2], 100}, {&bands[2], 150}, {&bands[2], 200},
  80. {&bands[3], 160}, {&bands[3], 170}, {&bands[4], 0}, {&bands[4], 50}, {&bands[4], 100},
  81. {&bands[4], 150}, {&bands[4], 200}, {&bands[5], 0}, {&bands[5], 50}, {&bands[5], 100},
  82. {&bands[5], 150}, {&bands[5], 200}, {&bands[6], 0},
  83. };
  84. typedef enum {
  85. EventTypeTick,
  86. EventTypeKey,
  87. } EventType;
  88. typedef struct {
  89. union {
  90. InputEvent input;
  91. } value;
  92. EventType type;
  93. } Event;
  94. typedef enum { ModeRx, ModeTx } Mode;
  95. typedef struct {
  96. int16_t dbm;
  97. uint8_t reg;
  98. } TxLevel;
  99. const TxLevel TX_LEVELS[] = {
  100. {-10, 0},
  101. {-5, 0},
  102. {0, 0},
  103. {5, 0},
  104. };
  105. typedef struct {
  106. Mode mode;
  107. size_t active_freq;
  108. int16_t last_rssi;
  109. size_t tx_level;
  110. bool need_cc1101_conf;
  111. } State;
  112. static void render_callback(CanvasApi* canvas, void* ctx) {
  113. State* state = (State*)acquire_mutex((ValueMutex*)ctx, 25);
  114. canvas->clear(canvas);
  115. canvas->set_color(canvas, ColorBlack);
  116. canvas->set_font(canvas, FontPrimary);
  117. canvas->draw_str(canvas, 2, 12, "cc1101 workaround");
  118. {
  119. char buf[24];
  120. FreqConfig conf = FREQ_LIST[state->active_freq];
  121. float freq = conf.band->base_freq + CHAN_SPA * conf.channel;
  122. sprintf(buf, "freq: %ld.%02ld MHz", (uint32_t)freq, (uint32_t)(freq * 100.) % 100);
  123. canvas->set_font(canvas, FontSecondary);
  124. canvas->draw_str(canvas, 2, 25, buf);
  125. }
  126. {
  127. canvas->set_font(canvas, FontSecondary);
  128. if(state->need_cc1101_conf) {
  129. canvas->draw_str(canvas, 2, 36, "mode: configuring...");
  130. } else if(state->mode == ModeRx) {
  131. canvas->draw_str(canvas, 2, 36, "mode: RX");
  132. } else if(state->mode == ModeTx) {
  133. canvas->draw_str(canvas, 2, 36, "mode: TX");
  134. } else {
  135. canvas->draw_str(canvas, 2, 36, "mode: unknown");
  136. }
  137. }
  138. {
  139. if(!state->need_cc1101_conf && state->mode == ModeRx) {
  140. char buf[24];
  141. sprintf(buf, "RSSI: %d dBm", state->last_rssi);
  142. canvas->set_font(canvas, FontSecondary);
  143. canvas->draw_str(canvas, 2, 48, buf);
  144. }
  145. }
  146. {
  147. char buf[24];
  148. sprintf(buf, "tx level: %d dBm", TX_LEVELS[state->tx_level].dbm);
  149. canvas->set_font(canvas, FontSecondary);
  150. canvas->draw_str(canvas, 2, 63, buf);
  151. }
  152. release_mutex((ValueMutex*)ctx, state);
  153. }
  154. static void input_callback(InputEvent* input_event, void* ctx) {
  155. osMessageQueueId_t event_queue = (QueueHandle_t)ctx;
  156. Event event;
  157. event.type = EventTypeKey;
  158. event.value.input = *input_event;
  159. osMessageQueuePut(event_queue, &event, 0, 0);
  160. }
  161. extern "C" void cc1101_workaround(void* p) {
  162. osMessageQueueId_t event_queue = osMessageQueueNew(1, sizeof(Event), NULL);
  163. assert(event_queue);
  164. State _state;
  165. _state.mode = ModeRx;
  166. _state.active_freq = 0;
  167. _state.need_cc1101_conf = true;
  168. _state.last_rssi = 0;
  169. _state.tx_level = 0;
  170. ValueMutex state_mutex;
  171. if(!init_mutex(&state_mutex, &_state, sizeof(State))) {
  172. printf("[cc1101] cannot create mutex\n");
  173. furiac_exit(NULL);
  174. }
  175. Widget* widget = widget_alloc();
  176. widget_draw_callback_set(widget, render_callback, &state_mutex);
  177. widget_input_callback_set(widget, input_callback, event_queue);
  178. // Open GUI and register widget
  179. GuiApi* gui = (GuiApi*)furi_open("gui");
  180. if(gui == NULL) {
  181. printf("[cc1101] gui is not available\n");
  182. furiac_exit(NULL);
  183. }
  184. gui->add_widget(gui, widget, WidgetLayerFullscreen);
  185. printf("[cc1101] creating device\n");
  186. CC1101 cc1101(GpioPin{CC1101_CS_GPIO_Port, CC1101_CS_Pin});
  187. printf("[cc1101] init device\n");
  188. uint8_t address = cc1101.Init();
  189. if(address > 0) {
  190. printf("[cc1101] init done: %d\n", address);
  191. } else {
  192. printf("[cc1101] init fail\n");
  193. furiac_exit(NULL);
  194. }
  195. // RX filter bandwidth 58.035714(0xFD) 100k(0xCD) 200k(0x8D)
  196. cc1101.SpiWriteReg(CC1101_MDMCFG4, 0xCD);
  197. // datarate config 250kBaud for the purpose of fast rssi measurement
  198. cc1101.SpiWriteReg(CC1101_MDMCFG3, 0x3B);
  199. // FEC preamble etc. last 2 bits for channel spacing
  200. cc1101.SpiWriteReg(CC1101_MDMCFG1, 0x20);
  201. // 50khz channel spacing
  202. cc1101.SpiWriteReg(CC1101_MDMCFG0, 0xF8);
  203. // create pin
  204. GpioPin led = {GPIOA, GPIO_PIN_8};
  205. // configure pin
  206. pinMode(led, GpioModeOpenDrain);
  207. const int16_t RSSI_THRESHOLD = -89;
  208. Event event;
  209. while(1) {
  210. osStatus_t event_status = osMessageQueueGet(event_queue, &event, NULL, 150);
  211. State* state = (State*)acquire_mutex_block(&state_mutex);
  212. if(event_status == osOK) {
  213. if(event.type == EventTypeKey) {
  214. if(event.value.input.state && event.value.input.input == InputBack) {
  215. printf("[cc1101] bye!\n");
  216. // TODO remove all widgets create by app
  217. widget_enabled_set(widget, false);
  218. furiac_exit(NULL);
  219. }
  220. if(event.value.input.state && event.value.input.input == InputUp) {
  221. if(state->active_freq > 0) {
  222. state->active_freq--;
  223. state->need_cc1101_conf = true;
  224. }
  225. }
  226. if(event.value.input.state && event.value.input.input == InputDown) {
  227. if(state->active_freq < (sizeof(FREQ_LIST) / sizeof(FREQ_LIST[0]) - 1)) {
  228. state->active_freq++;
  229. state->need_cc1101_conf = true;
  230. }
  231. }
  232. if(event.value.input.state && event.value.input.input == InputLeft) {
  233. if(state->tx_level < (sizeof(TX_LEVELS) / sizeof(TX_LEVELS[0]) - 1)) {
  234. state->tx_level++;
  235. } else {
  236. state->tx_level = 0;
  237. }
  238. state->need_cc1101_conf = true;
  239. }
  240. if(event.value.input.input == InputOk) {
  241. state->mode = event.value.input.state ? ModeTx : ModeRx;
  242. state->need_cc1101_conf = true;
  243. }
  244. }
  245. } else {
  246. if(!state->need_cc1101_conf && state->mode == ModeRx) {
  247. state->last_rssi = rx_rssi(&cc1101, &FREQ_LIST[state->active_freq]);
  248. }
  249. }
  250. if(state->need_cc1101_conf) {
  251. if(state->mode == ModeRx) {
  252. setup_freq(&cc1101, &FREQ_LIST[state->active_freq]);
  253. state->last_rssi = rx_rssi(&cc1101, &FREQ_LIST[state->active_freq]);
  254. // idle(&cc1101);
  255. } else if(state->mode == ModeTx) {
  256. tx(&cc1101, &FREQ_LIST[state->active_freq]);
  257. }
  258. state->need_cc1101_conf = false;
  259. }
  260. digitalWrite(
  261. led, (state->last_rssi > RSSI_THRESHOLD && !state->need_cc1101_conf) ? LOW : HIGH);
  262. release_mutex(&state_mutex, state);
  263. widget_update(widget);
  264. }
  265. /*
  266. while(1) {
  267. for(uint8_t i = 0; i <= NUM_OF_SUB_BANDS; i++) {
  268. highRSSI[i] = MIN_DBM;
  269. }
  270. activeChannel = 300;
  271. tx(&cc1101, activeBand, activeChannel, 500);
  272. scanFreq(&cc1101);
  273. if(activeChannel < 256 && highRSSI[activeBand] > RSSI_THRESHOLD) {
  274. float freq = base_freq[activeBand] + CHAN_SPA * activeChannel;
  275. printf(
  276. "channel: %d, freq: %d, RSSI: %d\n",
  277. activeChannel,
  278. (uint32_t)(freq * 1000),
  279. highRSSI[activeBand]
  280. );
  281. *
  282. if(tx_on) {
  283. tx(&cc1101, activeBand, activeChannel, 500);
  284. } else {
  285. osDelay(1000);
  286. }
  287. *
  288. } else {
  289. // printf("0 carrier sensed\n");
  290. }
  291. *
  292. uint8_t band = 4; // 438.2 MHz
  293. *
  294. cc1101.SetFreq(freqSettings[band][0], freqSettings[band][1], freqSettings[band][2]);
  295. cc1101.SetChannel(0);
  296. cc1101.SetTransmit();
  297. delay(5000);
  298. cc1101.SpiStrobe(CC1101_SIDLE);
  299. *
  300. delay(1000);
  301. }
  302. */
  303. }