2020-10-13 13:39:59 +03:00
|
|
|
#include "flipper.h"
|
2020-10-13 20:59:39 +03:00
|
|
|
|
2020-10-14 10:26:23 +03:00
|
|
|
#include "cc1101-workaround/cc1101.h"
|
2020-10-13 20:59:39 +03:00
|
|
|
|
2020-10-18 07:47:49 +03:00
|
|
|
#define RSSI_DELAY 5000 //rssi delay in micro second
|
2020-10-14 10:26:23 +03:00
|
|
|
#define CHAN_SPA 0.05 // channel spacing
|
2020-11-12 21:08:40 +03:00
|
|
|
#define F_OSC 26e6
|
2020-10-13 20:59:39 +03:00
|
|
|
|
2020-10-18 07:47:49 +03:00
|
|
|
int16_t rssi_to_dbm(uint8_t rssi_dec, uint8_t rssiOffset) {
|
2020-10-13 20:59:39 +03:00
|
|
|
int16_t rssi;
|
|
|
|
|
|
|
|
if(rssi_dec >= 128) {
|
|
|
|
rssi = (int16_t)((int16_t)(rssi_dec - 256) / 2) - rssiOffset;
|
|
|
|
} else {
|
|
|
|
rssi = (rssi_dec / 2) - rssiOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rssi;
|
|
|
|
}
|
|
|
|
|
2020-10-18 07:47:49 +03:00
|
|
|
typedef struct {
|
|
|
|
float base_freq;
|
|
|
|
uint8_t reg[3]; // FREQ2, FREQ1, FREQ0
|
|
|
|
uint8_t first_channel;
|
|
|
|
uint8_t last_channel;
|
|
|
|
uint8_t rssi_offset;
|
|
|
|
} Band;
|
2020-10-13 20:59:39 +03:00
|
|
|
|
2020-10-18 07:47:49 +03:00
|
|
|
typedef struct {
|
2020-10-18 10:47:38 +03:00
|
|
|
const Band* band;
|
2020-10-18 07:47:49 +03:00
|
|
|
uint16_t channel;
|
|
|
|
} FreqConfig;
|
2020-10-13 20:59:39 +03:00
|
|
|
|
2020-10-18 10:47:38 +03:00
|
|
|
void setup_freq(CC1101* cc1101, const FreqConfig* config) {
|
2020-10-19 09:22:56 +03:00
|
|
|
// cc1101->SpiWriteReg(CC1101_MCSM0, 0x08); // disalbe FS_AUTOCAL
|
2020-10-14 10:26:23 +03:00
|
|
|
cc1101->SpiWriteReg(CC1101_AGCCTRL2, 0x43 | 0x0C); // MAX_DVGA_GAIN to 11 for fast rssi
|
|
|
|
cc1101->SpiWriteReg(CC1101_AGCCTRL0, 0xB0); // max AGC WAIT_TIME; 0 filter_length
|
|
|
|
cc1101->SetMod(GFSK); // set to GFSK for fast rssi measurement | +8 is dcfilter off
|
2020-10-13 20:59:39 +03:00
|
|
|
|
2020-11-12 21:08:40 +03:00
|
|
|
uint32_t freq_reg = config->band->base_freq * 1e6 / (F_OSC / 65536);
|
|
|
|
cc1101->SetFreq((freq_reg >> 16) & 0xFF, (freq_reg >> 8) & 0xFF, (freq_reg)&0xFF);
|
2020-10-18 07:47:49 +03:00
|
|
|
cc1101->SetChannel(config->channel);
|
2020-10-13 20:59:39 +03:00
|
|
|
|
2020-10-19 09:22:56 +03:00
|
|
|
/*
|
2020-10-18 07:47:49 +03:00
|
|
|
//set test0 to 0x09
|
|
|
|
cc1101->SpiWriteReg(CC1101_TEST0, 0x09);
|
|
|
|
//set FSCAL2 to 0x2A to force VCO HIGH
|
|
|
|
cc1101->SpiWriteReg(CC1101_FSCAL2, 0x2A);
|
2020-10-13 20:59:39 +03:00
|
|
|
|
2020-10-18 07:47:49 +03:00
|
|
|
// perform a manual calibration by issuing SCAL command
|
|
|
|
cc1101->SpiStrobe(CC1101_SCAL);
|
2020-10-19 09:22:56 +03:00
|
|
|
*/
|
2020-10-13 20:59:39 +03:00
|
|
|
}
|
|
|
|
|
2020-10-18 10:47:38 +03:00
|
|
|
int16_t rx_rssi(CC1101* cc1101, const FreqConfig* config) {
|
2020-10-18 07:47:49 +03:00
|
|
|
cc1101->SetReceive();
|
2020-10-13 20:59:39 +03:00
|
|
|
|
2020-10-29 10:58:19 +03:00
|
|
|
delay_us(RSSI_DELAY);
|
2020-10-19 09:23:49 +03:00
|
|
|
|
2020-10-18 07:47:49 +03:00
|
|
|
// 1.4.8) read PKTSTATUS register while the radio is in RX state
|
2020-10-19 09:22:56 +03:00
|
|
|
/*uint8_t _pkt_status = */ cc1101->SpiReadStatus(CC1101_PKTSTATUS);
|
2020-10-19 09:23:49 +03:00
|
|
|
|
2020-10-18 07:47:49 +03:00
|
|
|
// 1.4.9) enter IDLE state by issuing a SIDLE command
|
2020-10-13 20:59:39 +03:00
|
|
|
cc1101->SpiStrobe(CC1101_SIDLE);
|
2020-10-18 07:47:49 +03:00
|
|
|
|
|
|
|
// //read rssi value and converto to dBm form
|
|
|
|
uint8_t rssi_dec = (uint8_t)cc1101->SpiReadStatus(CC1101_RSSI);
|
|
|
|
int16_t rssi_dBm = rssi_to_dbm(rssi_dec, config->band->rssi_offset);
|
|
|
|
|
|
|
|
return rssi_dBm;
|
2020-10-13 20:59:39 +03:00
|
|
|
}
|
2020-10-18 07:10:15 +03:00
|
|
|
|
2020-10-18 10:47:38 +03:00
|
|
|
void tx(CC1101* cc1101, const FreqConfig* config) {
|
2020-10-19 09:22:56 +03:00
|
|
|
/*
|
2020-10-18 10:47:38 +03:00
|
|
|
cc1101->SpiWriteReg(CC1101_MCSM0, 0x18); //enable FS_AUTOCAL
|
|
|
|
cc1101->SpiWriteReg(CC1101_AGCCTRL2, 0x43); //back to recommended config
|
|
|
|
cc1101->SpiWriteReg(CC1101_AGCCTRL0, 0x91); //back to recommended config
|
2020-10-19 09:22:56 +03:00
|
|
|
*/
|
2020-10-18 10:47:38 +03:00
|
|
|
|
2020-11-12 21:08:40 +03:00
|
|
|
uint32_t freq_reg = config->band->base_freq * 1e6 / (F_OSC / 65536);
|
|
|
|
cc1101->SetFreq((freq_reg >> 16) & 0xFF, (freq_reg >> 8) & 0xFF, (freq_reg)&0xFF);
|
2020-10-18 10:47:38 +03:00
|
|
|
cc1101->SetChannel(config->channel);
|
|
|
|
|
2020-10-18 07:47:49 +03:00
|
|
|
cc1101->SetTransmit();
|
|
|
|
}
|
2020-10-18 07:10:15 +03:00
|
|
|
|
2020-10-18 07:47:49 +03:00
|
|
|
void idle(CC1101* cc1101) {
|
|
|
|
cc1101->SpiStrobe(CC1101_SIDLE);
|
|
|
|
}
|
2020-10-18 07:10:15 +03:00
|
|
|
|
2020-11-12 21:08:40 +03:00
|
|
|
// f = (f_osc/65536) * (FREQ + CHAN * (256 + CH_SP_M) * 2^(CH_SP_E - 2))
|
|
|
|
// FREQ = f / (f_osc/65536)
|
|
|
|
// CHAN = 0
|
|
|
|
// TODO: CHAN number not implemented!
|
|
|
|
// TODO: reg values not affetcts
|
|
|
|
|
|
|
|
const Band bands[] = {
|
|
|
|
{300., {0x00, 0x00, 0x00}, 0, 255, 74},
|
|
|
|
{315., {0x00, 0x00, 0x00}, 0, 255, 74},
|
|
|
|
{348., {0x00, 0x00, 0x00}, 0, 255, 74},
|
|
|
|
{387., {0x00, 0x00, 0x00}, 0, 255, 74},
|
|
|
|
{433., {0x00, 0x00, 0x00}, 0, 255, 74},
|
|
|
|
{464., {0x00, 0x00, 0x00}, 0, 255, 74},
|
|
|
|
{779., {0x00, 0x00, 0x00}, 0, 255, 74},
|
|
|
|
{868., {0x00, 0x00, 0x00}, 0, 255, 74},
|
|
|
|
{915., {0x00, 0x00, 0x00}, 0, 255, 74},
|
|
|
|
{928., {0x00, 0x00, 0x00}, 0, 255, 74},
|
2020-10-18 07:10:15 +03:00
|
|
|
};
|
|
|
|
|
2020-10-18 10:47:38 +03:00
|
|
|
const FreqConfig FREQ_LIST[] = {
|
2020-11-12 21:08:40 +03:00
|
|
|
{&bands[0], 0},
|
|
|
|
{&bands[1], 0},
|
|
|
|
{&bands[2], 0},
|
|
|
|
{&bands[3], 0},
|
|
|
|
{&bands[4], 0},
|
|
|
|
{&bands[5], 0},
|
|
|
|
{&bands[6], 0},
|
|
|
|
{&bands[7], 0},
|
|
|
|
{&bands[8], 0},
|
|
|
|
{&bands[9], 0},
|
2020-10-18 07:10:15 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
EventTypeTick,
|
|
|
|
EventTypeKey,
|
|
|
|
} EventType;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
union {
|
|
|
|
InputEvent input;
|
|
|
|
} value;
|
|
|
|
EventType type;
|
2020-10-26 12:26:15 +03:00
|
|
|
} AppEvent;
|
2020-10-18 07:10:15 +03:00
|
|
|
|
2020-10-19 09:23:49 +03:00
|
|
|
typedef enum { ModeRx, ModeTx } Mode;
|
2020-10-18 07:10:15 +03:00
|
|
|
|
2020-10-18 10:47:38 +03:00
|
|
|
typedef struct {
|
|
|
|
int16_t dbm;
|
|
|
|
uint8_t reg;
|
|
|
|
} TxLevel;
|
|
|
|
|
|
|
|
const TxLevel TX_LEVELS[] = {
|
|
|
|
{-10, 0},
|
|
|
|
{-5, 0},
|
|
|
|
{0, 0},
|
|
|
|
{5, 0},
|
|
|
|
};
|
|
|
|
|
2020-10-18 07:10:15 +03:00
|
|
|
typedef struct {
|
|
|
|
Mode mode;
|
|
|
|
size_t active_freq;
|
2020-10-18 07:47:49 +03:00
|
|
|
int16_t last_rssi;
|
2020-10-18 10:47:38 +03:00
|
|
|
size_t tx_level;
|
2020-10-18 07:47:49 +03:00
|
|
|
bool need_cc1101_conf;
|
2020-10-18 07:10:15 +03:00
|
|
|
} State;
|
|
|
|
|
|
|
|
static void render_callback(CanvasApi* canvas, void* ctx) {
|
|
|
|
State* state = (State*)acquire_mutex((ValueMutex*)ctx, 25);
|
2020-10-13 20:59:39 +03:00
|
|
|
|
2020-10-18 05:43:09 +03:00
|
|
|
canvas->clear(canvas);
|
|
|
|
canvas->set_color(canvas, ColorBlack);
|
|
|
|
canvas->set_font(canvas, FontPrimary);
|
|
|
|
canvas->draw_str(canvas, 2, 12, "cc1101 workaround");
|
2020-10-18 07:10:15 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
char buf[24];
|
|
|
|
FreqConfig conf = FREQ_LIST[state->active_freq];
|
2020-10-19 09:23:49 +03:00
|
|
|
float freq = conf.band->base_freq + CHAN_SPA * conf.channel;
|
2020-10-18 07:10:15 +03:00
|
|
|
sprintf(buf, "freq: %ld.%02ld MHz", (uint32_t)freq, (uint32_t)(freq * 100.) % 100);
|
|
|
|
|
|
|
|
canvas->set_font(canvas, FontSecondary);
|
|
|
|
canvas->draw_str(canvas, 2, 25, buf);
|
|
|
|
}
|
|
|
|
|
2020-10-18 07:47:49 +03:00
|
|
|
{
|
|
|
|
canvas->set_font(canvas, FontSecondary);
|
|
|
|
|
|
|
|
if(state->need_cc1101_conf) {
|
|
|
|
canvas->draw_str(canvas, 2, 36, "mode: configuring...");
|
|
|
|
} else if(state->mode == ModeRx) {
|
|
|
|
canvas->draw_str(canvas, 2, 36, "mode: RX");
|
|
|
|
} else if(state->mode == ModeTx) {
|
|
|
|
canvas->draw_str(canvas, 2, 36, "mode: TX");
|
|
|
|
} else {
|
|
|
|
canvas->draw_str(canvas, 2, 36, "mode: unknown");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
if(!state->need_cc1101_conf && state->mode == ModeRx) {
|
|
|
|
char buf[24];
|
|
|
|
sprintf(buf, "RSSI: %d dBm", state->last_rssi);
|
|
|
|
|
|
|
|
canvas->set_font(canvas, FontSecondary);
|
|
|
|
canvas->draw_str(canvas, 2, 48, buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-18 10:47:38 +03:00
|
|
|
{
|
|
|
|
char buf[24];
|
|
|
|
sprintf(buf, "tx level: %d dBm", TX_LEVELS[state->tx_level].dbm);
|
|
|
|
|
|
|
|
canvas->set_font(canvas, FontSecondary);
|
|
|
|
canvas->draw_str(canvas, 2, 63, buf);
|
|
|
|
}
|
|
|
|
|
2020-10-18 07:10:15 +03:00
|
|
|
release_mutex((ValueMutex*)ctx, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void input_callback(InputEvent* input_event, void* ctx) {
|
|
|
|
osMessageQueueId_t event_queue = (QueueHandle_t)ctx;
|
|
|
|
|
2020-10-26 12:26:15 +03:00
|
|
|
AppEvent event;
|
2020-10-18 07:10:15 +03:00
|
|
|
event.type = EventTypeKey;
|
|
|
|
event.value.input = *input_event;
|
|
|
|
osMessageQueuePut(event_queue, &event, 0, 0);
|
2020-10-18 05:43:09 +03:00
|
|
|
}
|
|
|
|
|
2020-10-14 10:26:23 +03:00
|
|
|
extern "C" void cc1101_workaround(void* p) {
|
2020-10-26 12:26:15 +03:00
|
|
|
osMessageQueueId_t event_queue = osMessageQueueNew(1, sizeof(AppEvent), NULL);
|
2020-10-29 09:27:17 +03:00
|
|
|
furi_check(event_queue);
|
2020-10-18 07:10:15 +03:00
|
|
|
|
|
|
|
State _state;
|
|
|
|
_state.mode = ModeRx;
|
|
|
|
_state.active_freq = 0;
|
2020-10-18 07:47:49 +03:00
|
|
|
_state.need_cc1101_conf = true;
|
|
|
|
_state.last_rssi = 0;
|
2020-10-18 10:47:38 +03:00
|
|
|
_state.tx_level = 0;
|
2020-10-18 07:10:15 +03:00
|
|
|
|
|
|
|
ValueMutex state_mutex;
|
|
|
|
if(!init_mutex(&state_mutex, &_state, sizeof(State))) {
|
|
|
|
printf("[cc1101] cannot create mutex\n");
|
|
|
|
furiac_exit(NULL);
|
|
|
|
}
|
|
|
|
|
2020-10-18 05:43:09 +03:00
|
|
|
Widget* widget = widget_alloc();
|
|
|
|
|
2020-10-18 07:10:15 +03:00
|
|
|
widget_draw_callback_set(widget, render_callback, &state_mutex);
|
|
|
|
widget_input_callback_set(widget, input_callback, event_queue);
|
2020-10-13 20:59:39 +03:00
|
|
|
|
2020-10-18 05:43:09 +03:00
|
|
|
// Open GUI and register widget
|
2020-10-18 05:52:11 +03:00
|
|
|
GuiApi* gui = (GuiApi*)furi_open("gui");
|
2020-10-18 05:43:09 +03:00
|
|
|
if(gui == NULL) {
|
2020-10-18 07:10:15 +03:00
|
|
|
printf("[cc1101] gui is not available\n");
|
2020-10-14 10:26:23 +03:00
|
|
|
furiac_exit(NULL);
|
|
|
|
}
|
2020-10-26 20:00:17 +03:00
|
|
|
gui->add_widget(gui, widget, GuiLayerFullscreen);
|
2020-10-13 20:59:39 +03:00
|
|
|
|
2020-10-14 10:26:23 +03:00
|
|
|
printf("[cc1101] creating device\n");
|
2020-10-26 10:16:54 +03:00
|
|
|
GpioPin cs_pin = {CC1101_CS_GPIO_Port, CC1101_CS_Pin};
|
|
|
|
|
|
|
|
// TODO open record
|
|
|
|
GpioPin* cs_pin_record = &cs_pin;
|
|
|
|
CC1101 cc1101(cs_pin_record);
|
2020-10-14 10:26:23 +03:00
|
|
|
printf("[cc1101] init device\n");
|
2020-10-13 20:59:39 +03:00
|
|
|
|
|
|
|
uint8_t address = cc1101.Init();
|
|
|
|
if(address > 0) {
|
2020-10-18 05:43:09 +03:00
|
|
|
printf("[cc1101] init done: %d\n", address);
|
2020-10-13 20:59:39 +03:00
|
|
|
} else {
|
2020-10-18 05:43:09 +03:00
|
|
|
printf("[cc1101] init fail\n");
|
|
|
|
furiac_exit(NULL);
|
2020-10-13 20:59:39 +03:00
|
|
|
}
|
|
|
|
|
2020-10-14 10:26:23 +03:00
|
|
|
// RX filter bandwidth 58.035714(0xFD) 100k(0xCD) 200k(0x8D)
|
|
|
|
cc1101.SpiWriteReg(CC1101_MDMCFG4, 0xCD);
|
|
|
|
// datarate config 250kBaud for the purpose of fast rssi measurement
|
|
|
|
cc1101.SpiWriteReg(CC1101_MDMCFG3, 0x3B);
|
|
|
|
// FEC preamble etc. last 2 bits for channel spacing
|
|
|
|
cc1101.SpiWriteReg(CC1101_MDMCFG1, 0x20);
|
|
|
|
// 50khz channel spacing
|
|
|
|
cc1101.SpiWriteReg(CC1101_MDMCFG0, 0xF8);
|
2020-10-13 20:59:39 +03:00
|
|
|
|
2020-10-26 10:16:54 +03:00
|
|
|
// TODO open record
|
2020-11-12 21:08:40 +03:00
|
|
|
GpioPin* led_record = (GpioPin*)&led_gpio[1];
|
2020-10-18 07:47:49 +03:00
|
|
|
|
|
|
|
// configure pin
|
2020-10-29 10:58:19 +03:00
|
|
|
gpio_init(led_record, GpioModeOutputOpenDrain);
|
2020-10-18 07:47:49 +03:00
|
|
|
|
|
|
|
const int16_t RSSI_THRESHOLD = -89;
|
|
|
|
|
2020-10-26 12:26:15 +03:00
|
|
|
AppEvent event;
|
2020-10-18 07:10:15 +03:00
|
|
|
while(1) {
|
2020-10-19 09:22:56 +03:00
|
|
|
osStatus_t event_status = osMessageQueueGet(event_queue, &event, NULL, 150);
|
|
|
|
State* state = (State*)acquire_mutex_block(&state_mutex);
|
2020-10-18 07:10:15 +03:00
|
|
|
|
2020-10-19 09:22:56 +03:00
|
|
|
if(event_status == osOK) {
|
2020-10-18 07:10:15 +03:00
|
|
|
if(event.type == EventTypeKey) {
|
|
|
|
if(event.value.input.state && event.value.input.input == InputBack) {
|
|
|
|
printf("[cc1101] bye!\n");
|
|
|
|
// TODO remove all widgets create by app
|
|
|
|
widget_enabled_set(widget, false);
|
|
|
|
furiac_exit(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.value.input.state && event.value.input.input == InputUp) {
|
|
|
|
if(state->active_freq > 0) {
|
|
|
|
state->active_freq--;
|
2020-10-18 07:47:49 +03:00
|
|
|
state->need_cc1101_conf = true;
|
2020-10-18 07:10:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.value.input.state && event.value.input.input == InputDown) {
|
2020-10-19 09:23:49 +03:00
|
|
|
if(state->active_freq < (sizeof(FREQ_LIST) / sizeof(FREQ_LIST[0]) - 1)) {
|
2020-10-18 07:10:15 +03:00
|
|
|
state->active_freq++;
|
2020-10-18 07:47:49 +03:00
|
|
|
state->need_cc1101_conf = true;
|
2020-10-18 07:10:15 +03:00
|
|
|
}
|
|
|
|
}
|
2020-10-18 10:47:38 +03:00
|
|
|
|
|
|
|
if(event.value.input.state && event.value.input.input == InputLeft) {
|
2020-10-19 09:23:49 +03:00
|
|
|
if(state->tx_level < (sizeof(TX_LEVELS) / sizeof(TX_LEVELS[0]) - 1)) {
|
2020-10-18 10:47:38 +03:00
|
|
|
state->tx_level++;
|
|
|
|
} else {
|
|
|
|
state->tx_level = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
state->need_cc1101_conf = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event.value.input.input == InputOk) {
|
|
|
|
state->mode = event.value.input.state ? ModeTx : ModeRx;
|
|
|
|
state->need_cc1101_conf = true;
|
|
|
|
}
|
2020-10-18 07:10:15 +03:00
|
|
|
}
|
2020-10-18 07:47:49 +03:00
|
|
|
} else {
|
|
|
|
if(!state->need_cc1101_conf && state->mode == ModeRx) {
|
|
|
|
state->last_rssi = rx_rssi(&cc1101, &FREQ_LIST[state->active_freq]);
|
|
|
|
}
|
2020-10-19 09:22:56 +03:00
|
|
|
}
|
2020-10-18 07:47:49 +03:00
|
|
|
|
2020-10-19 09:22:56 +03:00
|
|
|
if(state->need_cc1101_conf) {
|
|
|
|
if(state->mode == ModeRx) {
|
|
|
|
setup_freq(&cc1101, &FREQ_LIST[state->active_freq]);
|
|
|
|
state->last_rssi = rx_rssi(&cc1101, &FREQ_LIST[state->active_freq]);
|
|
|
|
// idle(&cc1101);
|
|
|
|
} else if(state->mode == ModeTx) {
|
|
|
|
tx(&cc1101, &FREQ_LIST[state->active_freq]);
|
|
|
|
}
|
2020-10-18 07:47:49 +03:00
|
|
|
|
2020-10-19 09:22:56 +03:00
|
|
|
state->need_cc1101_conf = false;
|
2020-10-18 07:10:15 +03:00
|
|
|
}
|
2020-10-19 09:22:56 +03:00
|
|
|
|
2020-10-29 10:58:19 +03:00
|
|
|
gpio_write(
|
2020-10-26 10:16:54 +03:00
|
|
|
led_record,
|
2020-10-29 10:58:19 +03:00
|
|
|
(state->last_rssi > RSSI_THRESHOLD && !state->need_cc1101_conf) ? false : true);
|
2020-10-19 09:22:56 +03:00
|
|
|
|
|
|
|
release_mutex(&state_mutex, state);
|
|
|
|
widget_update(widget);
|
2020-10-18 07:10:15 +03:00
|
|
|
}
|
2020-10-13 13:39:59 +03:00
|
|
|
}
|