static void initialize(u8 bind)
{
CLOCK_StopTimer(); //停止计时器
CYRF_Reset();
CYRF_GetMfgData(cyrfmfg_id);
if (Model.fixed_id)
{
cyrfmfg_id[0] ^= (Model.fixed_id >> 0) & 0xff;
cyrfmfg_id[1] ^= (Model.fixed_id >> 8) & 0xff;
cyrfmfg_id[2] ^= (Model.fixed_id >> 16) & 0xff;
cyrfmfg_id[3] ^= (Model.fixed_id >> 24) & 0xff;
}
cyrf_config();
if (Model.protocol == PROTOCOL_DSMX)
{
calc_dsmx_channel(); //计算跳频图案
}
else
{
if (RANDOM_CHANNELS)//这里总是1
{
u8 tmpch[10];
CYRF_FindBestChannels(tmpch, 10, 5, 3, 75); //选出10个信号最好的通道
u8 idx = rand() % 10;
channels[0] = tmpch[idx]; //选用第一个
while (1) //随机选用另外一个
{
idx = rand() % 10;
if (tmpch[idx] != channels[0])
break;
}
channels[1] = tmpch[idx];
//一共选用两个通道
}
else
{
channels[0] = (cyrfmfg_id[0] + cyrfmfg_id[2] + cyrfmfg_id[4]
+ ((Model.fixed_id >> 0) & 0xff)
+ ((Model.fixed_id >> 16) & 0xff)) % 39 + 1;
channels[1] = (cyrfmfg_id[1] + cyrfmfg_id[3] + cyrfmfg_id[5]
+ ((Model.fixed_id >> 8) & 0xff)
+ ((Model.fixed_id >> 8) & 0xff)) % 40 + 40;
}
}
//The crc for channel 'a' is NOT(mfgid[1] << 8 + mfgid[0])
//The crc for channel 'b' is (mfgid[1] << 8 + mfgid[0])
crc = ~((cyrfmfg_id[0] << 8) + cyrfmfg_id[1]);
crcidx = 0;
//sop-col = (mfgid[0] + mfgid[1] + mfgid[2] + 2) & 0x07
//data-col0 = 7 - sop-col
//data-col1 = data-col0 + 1
sop_col = (cyrfmfg_id[0] + cyrfmfg_id[1] + cyrfmfg_id[2] + 2) & 0x07;
data_col = 7 - sop_col;
model = MODEL; //#define MODEL 0
//修剪通道数
num_channels = Model.num_channels;
if (num_channels < 6)
num_channels = 6;
else if (num_channels > 12)
num_channels = 12;
//切换到TX模式
CYRF_ConfigRxTx(1);
if (bind)
{
//需要额外的绑定操作
state = DSM2_BIND;
PROTOCOL_SetBindState((BIND_COUNT > 200 ? BIND_COUNT / 2 : 200) * 10); //msecs
initialize_bind_state();
}
else
{
state = DSM2_CHANSEL;
}
CLOCK_StartTimer(10000, dsm2_cb);
}
#define BIND_CHANNEL 0x0d //This can be any odd channel
void initialize_bind_state()
{
u8 data_code[32];
CYRF_ConfigRFChannel(BIND_CHANNEL); //This seems to be random?
u8 pn_row = get_pn_row(BIND_CHANNEL);
CYRF_ConfigCRCSeed(crc);
CYRF_ConfigSOPCode(pncodes[pn_row][sop_col]);
memcpy(data_code, pncodes[pn_row][data_col], 16);
memcpy(data_code + 16, pncodes[0][8], 8);
memcpy(data_code + 24, pn_bind, 8);
CYRF_ConfigDataCode(data_code, 32);
build_bind_packet();
}