cc1101-workaround.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. #include "flipper.h"
  2. #include "cc1101-workaround/cc1101.h"
  3. extern "C" void cli_print(const char* str);
  4. #define RSSI_DELAY 5000 //rssi delay in micro second
  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. uint32_t freq_reg = config->band->base_freq * 1e6 / (F_OSC / 65536);
  32. cc1101->SetFreq((freq_reg >> 16) & 0xFF, (freq_reg >> 8) & 0xFF, (freq_reg)&0xFF);
  33. cc1101->SetChannel(config->channel);
  34. /*
  35. //set test0 to 0x09
  36. cc1101->SpiWriteReg(CC1101_TEST0, 0x09);
  37. //set FSCAL2 to 0x2A to force VCO HIGH
  38. cc1101->SpiWriteReg(CC1101_FSCAL2, 0x2A);
  39. // perform a manual calibration by issuing SCAL command
  40. cc1101->SpiStrobe(CC1101_SCAL);
  41. */
  42. }
  43. static GpioPin debug_0 = {GPIOB, GPIO_PIN_2};
  44. int16_t rx_rssi(CC1101* cc1101, const FreqConfig* config) {
  45. // cc1101->SpiStrobe(CC1101_SFRX);
  46. // cc1101->SetReceive();
  47. // uint8_t begin_size = cc1101->SpiReadStatus(CC1101_RXBYTES);
  48. // uint8_t rx_status = cc1101->SpiReadStatus(CC1101_MARCSTATE);
  49. // delay_us(RSSI_DELAY);
  50. // osDelay(15);
  51. // uint8_t end_size = cc1101->SpiReadStatus(CC1101_RXBYTES);
  52. // 1.4.8) read PKTSTATUS register while the radio is in RX state
  53. /*uint8_t _pkt_status = */ // cc1101->SpiReadStatus(CC1101_PKTSTATUS);
  54. // 1.4.9) enter IDLE state by issuing a SIDLE command
  55. // cc1101->SpiStrobe(CC1101_SIDLE);
  56. // //read rssi value and converto to dBm form
  57. uint8_t rssi_dec = (uint8_t)cc1101->SpiReadStatus(CC1101_RSSI);
  58. int16_t rssi_dBm = rssi_to_dbm(rssi_dec, config->band->rssi_offset);
  59. /*
  60. char buf[256];
  61. sprintf(buf, "status: %d -> %d, rssi: %d\n", rx_status, cc1101->SpiReadStatus(CC1101_MARCSTATE), rssi_dBm);
  62. cli_print(buf);
  63. sprintf(buf, "begin: %d, end: %d\n", begin_size, end_size);
  64. cli_print(buf);
  65. */
  66. // uint8_t rx_data[64];
  67. // uint8_t fifo_length = end_size - begin_size;
  68. /*
  69. if(fifo_length < 64) {
  70. // cc1101->SpiReadBurstReg(CC1101_RXFIFO, rx_data, fifo_length);
  71. *
  72. printf("FIFO:");
  73. for(uint8_t i = 0; i < fifo_length; i++) {
  74. for(uint8_t bit = 0; bit < 8; bit++) {
  75. printf("%s", (rx_data[i] & (1 << bit)) > 0 ? "1" : "0");
  76. }
  77. printf(" ");
  78. }
  79. printf("\n");
  80. *
  81. for(uint8_t i = 0; i < fifo_length; i++) {
  82. for(uint8_t bit = 0; bit < 8; bit++) {
  83. gpio_write((GpioPin*)&debug_0, (rx_data[i] & (1 << bit)) > 0);
  84. delay_us(5);
  85. }
  86. }
  87. } else {
  88. cli_print("fifo size over\n");
  89. }
  90. */
  91. return rssi_dBm;
  92. }
  93. void flp_config(CC1101* cc1101) {
  94. // cc1101->SpiWriteReg(CC1101_FSCTRL1, 0x06); //IF frequency
  95. // cc1101->SpiWriteReg(CC1101_FSCTRL0, 0x00); //frequency offset before synthesizer
  96. // cc1101->SpiWriteReg(CC1101_MDMCFG4, 0xCC); // RX filter bandwidth 100k(0xcc)
  97. // cc1101->SpiWriteReg(CC1101_MDMCFG3, 0x43); //datarate config 512kBaud for the purpose of fast rssi measurement
  98. // cc1101->SpiWriteReg(CC1101_MDMCFG1, 0x21); //FEC preamble etc. last 2 bits for channel spacing
  99. // cc1101->SpiWriteReg(CC1101_MDMCFG0, 0xF8); //100khz channel spacing
  100. // CC1101_CHANNR moved to SetChannel func
  101. cc1101->SpiWriteReg(
  102. CC1101_MCSM0, 0x18); // calibrate when going from IDLE to RX or TX ; 149 - 155 μs timeout
  103. // MCSM0.FS_AUTOCAL[1:0] = 1
  104. // cc1101->SpiSetRegValue(CC1101_MCSM0, 1, 5, 4); // this not work
  105. // cc1101->SpiWriteReg(CC1101_FOCCFG, 0x16); //frequency compensation
  106. cc1101->SpiWriteReg(CC1101_AGCCTRL2, 0x43);
  107. cc1101->SpiWriteReg(CC1101_AGCCTRL1, 0x49);
  108. cc1101->SpiWriteReg(CC1101_AGCCTRL0, 0x91);
  109. //freq synthesizer calibration
  110. cc1101->SpiWriteReg(CC1101_FSCAL3, 0xEA);
  111. cc1101->SpiWriteReg(CC1101_FSCAL2, 0x2A);
  112. cc1101->SpiWriteReg(CC1101_FSCAL1, 0x00);
  113. cc1101->SpiWriteReg(CC1101_FSCAL0, 0x1F);
  114. // cc1101->SpiWriteReg(CC1101_TEST2, 0x81);
  115. // cc1101->SpiWriteReg(CC1101_TEST1, 0x35);
  116. // cc1101->SpiWriteReg(CC1101_TEST0, 0x0B); //should be 0x0B for lower than 430.6MHz and 0x09 for higher
  117. // cc1101->SpiWriteReg(CC1101_IOCFG2, 0x0D); //data output pin for asynchronous mode
  118. // cc1101->SpiWriteReg(CC1101_IOCFG0, 0x2E); //High impedance (3-state), GDO0 configed as data input for asynchronous mode
  119. // cc1101->SpiWriteReg(CC1101_PKTCTRL0, 0x33); //whitening off; asynchronous serial mode; CRC diable;reserved
  120. // cc1101->SpiWriteReg(CC1101_FIFOTHR, 0x47); //Adc_retention enabled for RX filter bandwidth less than 325KHz; defalut fifo threthold.
  121. // === Transparent mode ===
  122. // async data out
  123. cc1101->SpiSetRegValue(CC1101_IOCFG0, 13, 5, 0);
  124. // FIFOTHR.ADC_RETENTION = 1
  125. cc1101->SpiSetRegValue(CC1101_FIFOTHR, 1, 6, 6);
  126. // PKTCTRL1.APPEND_STATUS = 0
  127. cc1101->SpiSetRegValue(CC1101_PKTCTRL1, 0, 2, 2);
  128. // PKTCTRL0.WHITE_DATA = 0
  129. cc1101->SpiSetRegValue(CC1101_PKTCTRL0, 0, 6, 6);
  130. // PKTCTRL0.LENGTH_CONFIG = 2 // Infinite packet length mode
  131. cc1101->SpiSetRegValue(CC1101_PKTCTRL0, 2, 1, 0);
  132. // PKTCTRL0.CRC_EN = 0
  133. cc1101->SpiSetRegValue(CC1101_PKTCTRL0, 0, 2, 2);
  134. // PKTCTRL0.PKT_FORMAT = 3
  135. cc1101->SpiSetRegValue(CC1101_PKTCTRL0, 3, 5, 4);
  136. // bandwidth 50-100 kHz
  137. if(!cc1101->setRxBandwidth(75.0)) {
  138. printf("wrong rx bw\n");
  139. }
  140. // datarate ~30 kbps
  141. if(!cc1101->setBitRate(100.)) {
  142. printf("wrong bitrate\n");
  143. }
  144. cc1101->SetReceive();
  145. // mod
  146. // MDMCFG2.MOD_FORMAT = 3 (3: OOK, 0: 2-FSK)
  147. cc1101->SpiSetRegValue(CC1101_MDMCFG2, 3, 6, 4);
  148. // MDMCFG2.SYNC_MODE = 0
  149. cc1101->SpiSetRegValue(CC1101_MDMCFG2, 0, 2, 0);
  150. }
  151. void async_config(CC1101* cc1101) {
  152. cc1101->SpiSetRegValue(CC1101_IOCFG0, 13, 5, 0); // GDO0 Output Pin Configuration
  153. // FIFOTHR.ADC_RETENTION = 1
  154. cc1101->SpiSetRegValue(CC1101_FIFOTHR, 1, 6, 6);
  155. // PKTCTRL1.APPEND_STATUS = 0
  156. cc1101->SpiSetRegValue(CC1101_PKTCTRL1, 0, 2, 2);
  157. cc1101->SpiWriteReg(CC1101_PKTCTRL0, 0x32); // Packet Automation Control
  158. /*
  159. FIXME: this sequence not work
  160. // PKTCTRL0.PKT_FORMAT = 3
  161. cc1101->SpiSetRegValue(CC1101_PKTCTRL0, 3, 5, 4);
  162. // PKTCTRL0.LENGTH_CONFIG = 2 // Infinite packet length mode
  163. cc1101->SpiSetRegValue(CC1101_PKTCTRL0, 2, 1, 0);
  164. // PKTCTRL0.CRC_EN = 0
  165. cc1101->SpiSetRegValue(CC1101_PKTCTRL0, 0, 2, 2);
  166. // PKTCTRL0.WHITE_DATA = 0
  167. cc1101->SpiSetRegValue(CC1101_PKTCTRL0, 0, 6, 6);
  168. */
  169. cc1101->SpiWriteReg(CC1101_MDMCFG4, 0xD6); //Modem Configuration
  170. cc1101->SpiWriteReg(CC1101_MDMCFG3, 0xE4); //Modem Configuration
  171. /*
  172. FIXME: not work
  173. // bandwidth 50-100 kHz
  174. if(!cc1101->setRxBandwidth(75.0)) {
  175. printf("wrong rx bw\n");
  176. }
  177. // datarate ~30 kbps
  178. if(!cc1101->setBitRate(100.)) {
  179. printf("wrong bitrate\n");
  180. }
  181. */
  182. cc1101->SpiWriteReg(CC1101_MDMCFG2, 0x30); //Modem Configuration
  183. /*
  184. FIXME: not work
  185. // MDMCFG2.MOD_FORMAT = 3 (3: OOK, 0: 2-FSK)
  186. cc1101->SpiSetRegValue(CC1101_MDMCFG2, 3, 6, 4);
  187. // MDMCFG2.SYNC_MODE = 0
  188. cc1101->SpiSetRegValue(CC1101_MDMCFG2, 0, 2, 0);
  189. */
  190. cc1101->SpiWriteReg(CC1101_MCSM0, 0x18); //Main Radio Control State Machine Configuration
  191. cc1101->SpiWriteReg(CC1101_FSCAL3, 0xE9); //Frequency Synthesizer Calibration
  192. cc1101->SpiWriteReg(CC1101_FSCAL2, 0x2A); //Frequency Synthesizer Calibration
  193. cc1101->SpiWriteReg(CC1101_FSCAL1, 0x00); //Frequency Synthesizer Calibration
  194. cc1101->SpiWriteReg(CC1101_FSCAL0, 0x1F); //Frequency Synthesizer Calibration
  195. }
  196. void tx_config(CC1101* cc1101) {
  197. // cc1101->SpiWriteReg(CC1101_IOCFG2,0x0B); //GDO2 Output Pin Configuration
  198. // cc1101->SpiWriteReg(CC1101_IOCFG0,0x0C); //GDO0 Output Pin Configuration
  199. cc1101->SpiSetRegValue(CC1101_IOCFG0, 13, 5, 0); // GDO0 Output Pin Configuration
  200. cc1101->SpiWriteReg(CC1101_FIFOTHR, 0x47); //RX FIFO and TX FIFO Thresholds
  201. cc1101->SpiWriteReg(CC1101_PKTCTRL0, 0x32); //Packet Automation Control
  202. cc1101->SpiWriteReg(CC1101_FSCTRL1, 0x06); //Frequency Synthesizer Control
  203. cc1101->SpiWriteReg(CC1101_FREQ2, 0x10); //Frequency Control Word, High Byte
  204. cc1101->SpiWriteReg(CC1101_FREQ1, 0xB0); //Frequency Control Word, Middle Byte
  205. cc1101->SpiWriteReg(CC1101_FREQ0, 0x71); //Frequency Control Word, Low Byte
  206. cc1101->SpiWriteReg(CC1101_MDMCFG4, 0x6A); //Modem Configuration
  207. cc1101->SpiWriteReg(CC1101_MDMCFG3, 0x2E); //Modem Configuration
  208. cc1101->SpiWriteReg(CC1101_MDMCFG2, 0x30); //Modem Configuration
  209. cc1101->SpiWriteReg(CC1101_DEVIATN, 0x15); //Modem Deviation Setting
  210. cc1101->SpiWriteReg(CC1101_MCSM0, 0x18); //Main Radio Control State Machine Configuration
  211. cc1101->SpiWriteReg(CC1101_FOCCFG, 0x16); //Frequency Offset Compensation Configuration
  212. cc1101->SpiWriteReg(CC1101_WORCTRL, 0xFB); //Wake On Radio Control
  213. cc1101->SpiWriteReg(CC1101_FREND0, 0x11); //Front End TX Configuration
  214. cc1101->SpiWriteReg(CC1101_FSCAL3, 0xE9); //Frequency Synthesizer Calibration
  215. cc1101->SpiWriteReg(CC1101_FSCAL2, 0x2A); //Frequency Synthesizer Calibration
  216. cc1101->SpiWriteReg(CC1101_FSCAL1, 0x00); //Frequency Synthesizer Calibration
  217. cc1101->SpiWriteReg(CC1101_FSCAL0, 0x1F); //Frequency Synthesizer Calibration
  218. cc1101->SpiWriteReg(CC1101_TEST2, 0x81); //Various Test Settings
  219. cc1101->SpiWriteReg(CC1101_TEST1, 0x35); //Various Test Settings
  220. cc1101->SpiWriteReg(CC1101_TEST0, 0x09); //Various Test Settings
  221. }
  222. // f = (f_osc/65536) * (FREQ + CHAN * (256 + CH_SP_M) * 2^(CH_SP_E - 2))
  223. // FREQ = f / (f_osc/65536)
  224. // CHAN = 0
  225. // TODO: CHAN number not implemented!
  226. // TODO: reg values not affetcts
  227. const Band bands[] = {
  228. {300., {0x00, 0x00, 0x00}, 0, 255, 74},
  229. {315., {0x00, 0x00, 0x00}, 0, 255, 74},
  230. {348., {0x00, 0x00, 0x00}, 0, 255, 74},
  231. {387., {0x00, 0x00, 0x00}, 0, 255, 74},
  232. {433.92, {0x00, 0x00, 0x00}, 0, 255, 74},
  233. {438.9, {0x00, 0x00, 0x00}, 0, 255, 74},
  234. {464., {0x00, 0x00, 0x00}, 0, 255, 74},
  235. {779., {0x00, 0x00, 0x00}, 0, 255, 74},
  236. {868., {0x00, 0x00, 0x00}, 0, 255, 74},
  237. {915., {0x00, 0x00, 0x00}, 0, 255, 74},
  238. {928., {0x00, 0x00, 0x00}, 0, 255, 74},
  239. };
  240. const FreqConfig FREQ_LIST[] = {
  241. {&bands[0], 0},
  242. {&bands[1], 0},
  243. {&bands[2], 0},
  244. {&bands[3], 0},
  245. {&bands[4], 0},
  246. {&bands[5], 0},
  247. {&bands[6], 0},
  248. {&bands[7], 0},
  249. {&bands[8], 0},
  250. {&bands[9], 0},
  251. {&bands[10], 0},
  252. };
  253. extern "C" void cc1101_isr() {
  254. gpio_write((GpioPin*)&debug_0, gpio_read(&cc1101_g0_gpio));
  255. }
  256. typedef enum {
  257. EventTypeTick,
  258. EventTypeKey,
  259. } EventType;
  260. typedef struct {
  261. union {
  262. InputEvent input;
  263. } value;
  264. EventType type;
  265. } AppEvent;
  266. typedef enum { ModeRx, ModeTx } Mode;
  267. typedef struct {
  268. int16_t dbm;
  269. uint8_t reg;
  270. } TxLevel;
  271. const TxLevel TX_LEVELS[] = {
  272. {-10, 0},
  273. {-5, 0},
  274. {0, 0},
  275. {5, 0},
  276. };
  277. typedef struct {
  278. Mode mode;
  279. size_t active_freq;
  280. int16_t last_rssi;
  281. size_t tx_level;
  282. bool need_cc1101_conf;
  283. } State;
  284. static void render_callback(Canvas* canvas, void* ctx) {
  285. State* state = (State*)acquire_mutex((ValueMutex*)ctx, 25);
  286. if(!state) return;
  287. canvas_clear(canvas);
  288. canvas_set_color(canvas, ColorBlack);
  289. canvas_set_font(canvas, FontPrimary);
  290. canvas_draw_str(canvas, 2, 12, "cc1101 workaround");
  291. {
  292. char buf[24];
  293. FreqConfig conf = FREQ_LIST[state->active_freq];
  294. float freq = conf.band->base_freq + CHAN_SPA * conf.channel;
  295. sprintf(buf, "freq: %ld.%02ld MHz", (uint32_t)freq, (uint32_t)(freq * 100.) % 100);
  296. canvas_set_font(canvas, FontSecondary);
  297. canvas_draw_str(canvas, 2, 25, buf);
  298. }
  299. {
  300. canvas_set_font(canvas, FontSecondary);
  301. if(state->need_cc1101_conf) {
  302. canvas_draw_str(canvas, 2, 36, "mode: configuring...");
  303. } else if(state->mode == ModeRx) {
  304. canvas_draw_str(canvas, 2, 36, "mode: RX");
  305. } else if(state->mode == ModeTx) {
  306. canvas_draw_str(canvas, 2, 36, "mode: TX");
  307. } else {
  308. canvas_draw_str(canvas, 2, 36, "mode: unknown");
  309. }
  310. }
  311. {
  312. if(!state->need_cc1101_conf && state->mode == ModeRx) {
  313. char buf[24];
  314. sprintf(buf, "RSSI: %d dBm", state->last_rssi);
  315. canvas_set_font(canvas, FontSecondary);
  316. canvas_draw_str(canvas, 2, 48, buf);
  317. }
  318. }
  319. {
  320. char buf[24];
  321. sprintf(buf, "tx level: %d dBm", TX_LEVELS[state->tx_level].dbm);
  322. canvas_set_font(canvas, FontSecondary);
  323. canvas_draw_str(canvas, 2, 63, buf);
  324. }
  325. release_mutex((ValueMutex*)ctx, state);
  326. }
  327. static void input_callback(InputEvent* input_event, void* ctx) {
  328. osMessageQueueId_t event_queue = (QueueHandle_t)ctx;
  329. AppEvent event;
  330. event.type = EventTypeKey;
  331. event.value.input = *input_event;
  332. osMessageQueuePut(event_queue, &event, 0, 0);
  333. }
  334. extern "C" void cc1101_workaround(void* p) {
  335. osMessageQueueId_t event_queue = osMessageQueueNew(1, sizeof(AppEvent), NULL);
  336. furi_check(event_queue);
  337. State _state;
  338. _state.mode = ModeRx;
  339. _state.active_freq = 4;
  340. _state.need_cc1101_conf = true;
  341. _state.last_rssi = 0;
  342. _state.tx_level = 0;
  343. ValueMutex state_mutex;
  344. if(!init_mutex(&state_mutex, &_state, sizeof(State))) {
  345. printf("[cc1101] cannot create mutex\n");
  346. furiac_exit(NULL);
  347. }
  348. Widget* widget = widget_alloc();
  349. widget_draw_callback_set(widget, render_callback, &state_mutex);
  350. widget_input_callback_set(widget, input_callback, event_queue);
  351. // Open GUI and register widget
  352. Gui* gui = (Gui*)furi_open("gui");
  353. if(gui == NULL) {
  354. printf("[cc1101] gui is not available\n");
  355. furiac_exit(NULL);
  356. }
  357. gui_add_widget(gui, widget, GuiLayerFullscreen);
  358. gpio_init(&debug_0, GpioModeOutputPushPull);
  359. gpio_write((GpioPin*)&debug_0, false);
  360. printf("[cc1101] creating device\n");
  361. GpioPin cs_pin = {CC1101_CS_GPIO_Port, CC1101_CS_Pin};
  362. gpio_init(&cc1101_g0_gpio, GpioModeInput);
  363. // TODO open record
  364. GpioPin* cs_pin_record = &cs_pin;
  365. CC1101 cc1101(cs_pin_record);
  366. printf("[cc1101] init device\n");
  367. uint8_t address = cc1101.Init();
  368. if(address > 0) {
  369. printf("[cc1101] init done: %d\n", address);
  370. } else {
  371. printf("[cc1101] init fail\n");
  372. furiac_exit(NULL);
  373. }
  374. cc1101.SpiStrobe(CC1101_SIDLE);
  375. // flp_config(&cc1101);
  376. // async_config(&cc1101);
  377. tx_config(&cc1101);
  378. // setup_freq(&cc1101, &FREQ_LIST[4]);
  379. // enable_cc1101_irq();
  380. printf("init ok\n");
  381. // TODO open record
  382. GpioPin* led_record = (GpioPin*)&led_gpio[1];
  383. // configure pin
  384. gpio_init(led_record, GpioModeOutputOpenDrain);
  385. const int16_t RSSI_THRESHOLD = -60;
  386. // setup_freq(&cc1101, &FREQ_LIST[1]);
  387. cc1101.SetReceive();
  388. AppEvent event;
  389. while(1) {
  390. osStatus_t event_status = osMessageQueueGet(event_queue, &event, NULL, 100);
  391. State* state = (State*)acquire_mutex_block(&state_mutex);
  392. if(event_status == osOK) {
  393. if(event.type == EventTypeKey) {
  394. if(event.value.input.state && event.value.input.input == InputBack) {
  395. printf("[cc1101] bye!\n");
  396. // TODO remove all widgets create by app
  397. widget_enabled_set(widget, false);
  398. furiac_exit(NULL);
  399. }
  400. if(event.value.input.state && event.value.input.input == InputUp) {
  401. if(state->active_freq > 0) {
  402. state->active_freq--;
  403. state->need_cc1101_conf = true;
  404. }
  405. }
  406. if(event.value.input.state && event.value.input.input == InputDown) {
  407. if(state->active_freq < (sizeof(FREQ_LIST) / sizeof(FREQ_LIST[0]) - 1)) {
  408. state->active_freq++;
  409. state->need_cc1101_conf = true;
  410. }
  411. }
  412. if(event.value.input.state && event.value.input.input == InputLeft) {
  413. if(state->tx_level < (sizeof(TX_LEVELS) / sizeof(TX_LEVELS[0]) - 1)) {
  414. state->tx_level++;
  415. } else {
  416. state->tx_level = 0;
  417. }
  418. state->need_cc1101_conf = true;
  419. }
  420. if(event.value.input.input == InputOk) {
  421. state->mode = event.value.input.state ? ModeTx : ModeRx;
  422. state->need_cc1101_conf = true;
  423. }
  424. }
  425. } else {
  426. }
  427. if(state->need_cc1101_conf) {
  428. if(state->mode == ModeRx) {
  429. cc1101.SpiStrobe(CC1101_SIDLE);
  430. gpio_init(&cc1101_g0_gpio, GpioModeInput);
  431. setup_freq(&cc1101, &FREQ_LIST[state->active_freq]);
  432. cc1101.SetReceive();
  433. state->last_rssi = rx_rssi(&cc1101, &FREQ_LIST[state->active_freq]);
  434. } else if(state->mode == ModeTx) {
  435. cc1101.SpiStrobe(CC1101_SIDLE);
  436. setup_freq(&cc1101, &FREQ_LIST[state->active_freq]);
  437. cc1101.SetTransmit();
  438. gpio_init(&cc1101_g0_gpio, GpioModeOutputPushPull);
  439. gpio_write(&cc1101_g0_gpio, false);
  440. }
  441. state->need_cc1101_conf = false;
  442. }
  443. if(!state->need_cc1101_conf && state->mode == ModeRx) {
  444. state->last_rssi = rx_rssi(&cc1101, &FREQ_LIST[state->active_freq]);
  445. gpio_write(led_record, state->last_rssi < RSSI_THRESHOLD);
  446. } else if(!state->need_cc1101_conf && state->mode == ModeTx) {
  447. /*
  448. const uint8_t data = 0xA5;
  449. for(uint8_t i = 0; i < 8; i++) {
  450. gpio_write(&cc1101_g0_gpio, (data & (1 << i)) > 0);
  451. osDelay(1);
  452. }
  453. gpio_write(&cc1101_g0_gpio, false);
  454. */
  455. /*
  456. // BELL UDB-Q022-0000
  457. const uint16_t HALF_PERIOD = 500;
  458. for(uint8_t n = 0; n < 4; n++) {
  459. for(uint8_t i = 0; i < 4; i++) {
  460. gpio_write(&cc1101_g0_gpio, true);
  461. delay_us(3 * HALF_PERIOD);
  462. gpio_write(&cc1101_g0_gpio, false);
  463. delay_us(HALF_PERIOD);
  464. }
  465. for(uint8_t i = 0; i < 40; i++) {
  466. gpio_write(&cc1101_g0_gpio, true);
  467. delay_us(HALF_PERIOD);
  468. gpio_write(&cc1101_g0_gpio, false);
  469. delay_us(HALF_PERIOD);
  470. }
  471. }
  472. */
  473. // BELL ERA C61, static code
  474. const uint16_t ONE_ON = 150;
  475. const uint16_t ONE_OFF = 400;
  476. const uint16_t ZERO_ON = 420;
  477. const uint16_t ZERO_OFF = 130;
  478. const bool SEQ[] = {true, true, false, false, true, false, true, false, true,
  479. false, true, true, true, false, true, false, true, true,
  480. true, true, true, false, true, false, true};
  481. for(uint8_t n = 0; n < 10; n++) {
  482. for(uint8_t i = 0; i < sizeof(SEQ) / sizeof(SEQ[0]); i++) {
  483. if(SEQ[i]) {
  484. gpio_write(&cc1101_g0_gpio, false);
  485. delay_us(ONE_ON);
  486. gpio_write(&cc1101_g0_gpio, true);
  487. delay_us(ONE_OFF);
  488. } else {
  489. gpio_write(&cc1101_g0_gpio, false);
  490. delay_us(ZERO_ON);
  491. gpio_write(&cc1101_g0_gpio, true);
  492. delay_us(ZERO_OFF);
  493. }
  494. }
  495. osDelay(4);
  496. }
  497. gpio_write(&cc1101_g0_gpio, false);
  498. }
  499. release_mutex(&state_mutex, state);
  500. widget_update(widget);
  501. }
  502. }