device_bind_process是怎么实现绑定的?
enum
{
NOSTATE,
UBIND, //等待接收 握手一次1
WAIT_FOR_TOUCH, //
WAIT_FOR_CONFIRM, //点击按键后 回复一次2
BIND_SUCCESS, //等待接收 握手3次
};
此段代码是在void touch_scan(void)
//绑定状态的切换 按2、3按键进入网状态 怎么表现的2、3键
if (device_bind_process==WAIT_FOR_TOUCH)
{
if (i==3)
{
device_bind_process = WAIT_FOR_CONFIRM; //点击按键后 回复一次2
enter_vlps_xms(50);//进入第功率状态
return;
}
}
//检测到有新设备的时候,状态为WAIT_FOR_TOUCH,按了3键紧接着进入了WAIT_FOR_CONFIRM,进入WAIT_FOR_CONFIRM以后干吗用呢????
void task_25ms(void *args)
{
if (!is_task_set(EV_REPS))//如果没有启动wireless_response就启动wireless_check_irq
{
notify(EV_WIRQ); //75MS Per.time 启动wirless_check_irq
}
// if ((silicon_status&SILLICON_ALL_SET)||is_task_set(EV_REPS)||is_task_set(EV_WIRQ)||cap_pad[0].led_on||cap_pad[1].led_on||cap_pad[2].led_on||cap_pad[3].led_on) return; //sleep or not
// enter_vlps_xms(15); //wake up for EV_WIRQ 75ms
if (bind_state_cnt!=0)
{
if(0==--bind_state_cnt)
{
device_bind_process=UBIND;//入网的时候会把bind_state_cnt赋值0x28,,在282内没有完成就是没有绑定成功。
在规定的时间内没有完成绑定的工作
}
}
}
device_bind_process初始化为NOSTATE
uint8 device_bind_process=NOSTATE; //绑定状态切换
void bind_listen_send()
{
test_buff[8]={0x8E,0x24,0x8B,0x00,0x65,0x3,0xF0,0x95};
if (si4438_statue == SI4438_SLEEP)
{
vRadio_StartRX(SI4438_CHANL, SI4438_DATALETH); //4MS 所有READCTS带自动复位功能
si4438_statue = SI4438_RX;
clr_detc();
}
//注意 清旧设备和这里会出现连续保持RX
if (si4438_statue == SI4438_RX)
{
clr_detc();
if(0==read_4438_irq_port())
// if (1)
{
if(SI446X_CMD_GET_INT_STATUS_REP_PACKET_RX_PEND_BIT!=bRadio_Check_Tx_RX(g_frame_buffer))
return; // test by lgt
//如果是新设备会进入第一次入网的申请 木
if(BIND_MESSGE == frame_ctl(&g_frame_buffer[0],Si446xCmd.FIFO_INFO.RX_FIFO_COUNT)) // test by lgt
//if(BIND_MESSGE == frame_ctl(test_buff,8))
{
//adapter_infor_add(); //置旧设备标志位,写FLASH
device_bind_process=WAIT_FOR_TOUCH;
这里就就解释了WAIT_FOR_TOUCH的由来
}
else //非绑定报文
{
if (!(silicon_status&0x58))
{
si446x_change_state(1);
return; //保证关灯时电够用
}
//notify(EV_BIND);
}
si446x_change_state(1); //先休眠无线在切低功耗 记得和上面的changestate合一起
// enter_vlps_100ms();
}
set_detc();
}
else
{
//code finished ,no test
//clr_selc();
if(cap_pad[0].led_on||cap_pad[1].led_on||cap_pad[2].led_on||cap_pad[3].led_on)
return;
si446x_get_int_status(0u, 0u, 0u); //一定清标志位
// enter_vlps_100ms(); //先电容充电再发送报文
// enter_vlps_100ms();
if (device_bind_process==WAIT_FOR_CONFIRM)//如果处于设置状态的话,就会填充握手报文。8e 24 8b 00 65 aa bb cc 04 77 00
{
Device_broadcast_mess.ctrl=4;
Device_broadcast_mess.handle=1;
Device_broadcast_mess.data=RELAY_4_CHAN;// enter_vlps_xms(200);
Device_broadcast_mess.sum=sum((unsigned char *)&Device_broadcast_mess,10);
}
if (device_bind_process == NOSTATE)
{
memset_my(Device_broadcast_mess.dev_id, 0XFF, ADAPTER_ADDRESS_LEN);
memset_my(Device_broadcast_mess.my_id, 0, SLAVE_ADDRESS_LEN);
Device_broadcast_mess.ctrl=3;
Device_broadcast_mess.data=RELAY_4_CHAN;
}
Device_broadcast_mess.sum=sum((unsigned char *)&Device_broadcast_mess,10);
vRadio_StartTx_Variable_Packet(0, &Device_broadcast_mess.head, 11); // 重传/100ms+20ms/次
while(read_4438_irq_port()!=0);
si4438_delay--;
//set_selc();
}
}
u8 wirless_state_analyse()
{
if (device_bind_process==WAIT_FOR_TOUCH) //TOUCH状态在触摸按键检测中切换
{
change_led_select();//等待触摸,不断闪灯
notify(EV_BIND);
return 0xff;
}
if (device_bind_process==BIND_SUCCESS) //TOUCH状态在触摸按键检测中切换
{
si446x_change_state(1); //先关闭无线 写FLASH需要时间
adapter_infor_add();//把新设备置成老设备 木
memset_my(g_frame_buffer, 0, 20);
// enter_vlps_xms(50); //防止绑定结束后复位
ctrl_set_all();
led_clr_all();
bind_state_cnt=0;//绑定成功了,就会把bind_state_cnt置0.。
si4438_statue = SI4438_SLEEP;//SI4438_SLEEP状态出现在3中情况下,BIND_SUCCESS,UBIND,初始化的时候三种状态
si4438_delay = WIRELESS_DELAY;
device_bind_process=NOSTATE;//无状态的三种情况是绑定成功,绑定失败,初始化为无状态
enter_my_sleep(5); //此处有重大BUG 成功后复位
return 0xff;
}
if(device_bind_process==UBIND)
{
si446x_change_state(1);
ctrl_set_all();
led_clr_all();
si4438_statue = SI4438_SLEEP;
si4438_delay = WIRELESS_DELAY;
device_bind_process=NOSTATE;
enter_vlps_xms(500);
return 0xff;
}
return 0;
}
uint8 SI4438_bind_message_handle(struct SI4438_Frame * pframe)
{
switch (pframe->ctrl)
{
case ADAPTER_BIND_FLAG:
mymemcpy(device_id, pframe->dev_id, ADAPTER_ADDRESS_LEN);
mymemcpy(slave_device_id,pframe->my_id,SLAVE_ADDRESS_LEN);
mymemcpy(Device_broadcast_mess.dev_id,pframe->dev_id,ADAPTER_ADDRESS_LEN); //填入广播报文
mymemcpy(Device_broadcast_mess.my_id,pframe->my_id,SLAVE_ADDRESS_LEN);//填入广播报文
Device_broadcast_mess.handle=pframe->handle;
bind_state_cnt = BIND_WAIT_TIME;//等待成功的时间
return BIND_MESSGE;
case BIND_ACK_FLAG:
if(device_bind_process != WAIT_FOR_CONFIRM)return 0xFF;
device_bind_process=BIND_SUCCESS;
return BIND_ACK_FLAG;
default:return 0xff;
}
}
uint8 SI4438_bind_message_handle(struct SI4438_Frame * pframe)问题来了谁来调用SI4438_bind_message_handle
uint8 frame_ctl(uint8 rxframe[], uint8 rxlen)
{
uint8 i = 0,framelenth;
struct SI4438_Frame *pframe;
if (rxlen<10) return 0xFF;//做的容错,因为协议都是11位的 木
while(i < rxlen)
{
if(HW_HEAD == rxframe||STC == rxframe)//判断起始符,0X8E 木
break;
i++;
}
if(i >= rxlen)
{
return(0xFF);
}
framelenth=rxlen-i;//除起始符外的长度
if (framelenth<10) return 0XFF;
//if (STC==rxframe)return(frame_handle(0,rxframe + i)); //选择兼容载波协议
pframe = (struct SI4438_Frame *)(rxframe + i);
if (pframe->sum!=sum(rxframe+i,10)) return 0xFF;//判断校验码
if (DEVICE_IS_NEW==new_device_flag) //新设备发送入网申请命令 木
{
return(SI4438_bind_message_handle(pframe));
}
return (SI4438_remote_handle(pframe)); //选择支持自定义短协议
//return(remote_handle(pframe)); //选择支持遥控器
}
void wirless_check_irq(void *args)
{
clr_wdog();
// uint8_t lenth=0;
if (DEVICE_IS_NEW==new_device_flag) //新设备将不执行正常检测中断 added in 2015-3-4 no test
{
return;//如果是新设备就不用检测无线的中断了。
}
if (si4438_statue == SI4438_SLEEP) //绑定成功就读,那么不成功也是读呀????因为绑定成功和不成功设备都要是SI4438_SLEEP
{
vRadio_StartRX(SI4438_CHANL, SI4438_DATALETH); //4MS 所有READCTS带自动复位功能, 读的东西放在哪里了????
si4438_statue = SI4438_RX;//为什么要把它置于读的状态????木 表示已经读过4438了吗???
//GPIOA_PCOR |= GPIO_PCOR_PTCO(CTRL2_PIN); //无线监听动作开始计量
}
if(0==read_4438_irq_port())//用来检测中断木 为0表示中断发生
{
if (si4438_statue==SI4438_TX)goto finish_rcv; //finish the tx//vRadio_StartRX只是开始读,把数据读出来只是bRadio_Check_Tx_RX这个函数
if(SI446X_CMD_GET_INT_STATUS_REP_PACKET_RX_PEND_BIT!=bRadio_Check_Tx_RX(g_frame_buffer))return;//从433里面读出了数据 木
si446x_change_state(1);//猜测是清除的状态
// clr_detc();
// GPIOA_PCOR |= GPIO_PCOR_PTCO(CTRL1_PIN); //无线接收发送动作开始计量
frame_ctl(&g_frame_buffer[0],Si446xCmd.FIFO_INFO.RX_FIFO_COUNT);//用读出来的数据改变灯的状态,把读出来的数据付诸于动作
// set_detc();
enter_vlps_xms(100); //进入低功率状态
si4438_delay = WIRELESS_DELAY;
goto finish_rcv;
}
if (si4438_delay) //如果不为0就不断的启动 无线检测 //CAL IN 1ms_TASK
{
notify(EV_WIRQ);//启动自己的无线检测
}
else
{
finish_rcv:;
si4438_delay=WIRELESS_DELAY; // WIRELESS_DELAY 0x28
si446x_get_int_status(0u, 0u, 0u);//猜测清零操作
si446x_change_state(1); //猜测是清除的状态
si4438_statue=SI4438_SLEEP; //sleep
// GPIOA_PSOR |= GPIO_PSOR_PTSO(CTRL2_PIN); //无线监听动作开始计量
if ((silicon_status&SILLICON_ALL_SET)||cap_pad[0].led_on
||cap_pad[1].led_on||cap_pad[2].led_on
||cap_pad[3].led_on) return; //sleep or not
enter_vlps_xms(100); //低功耗的作用
}
}
const struct task tasks[] =
{
{EV_1MS, 0, NULL, task_1ms},//主要用来操作si4438_delay
{EV_25MS, 0, NULL, task_25ms},//启动无线中断
{EV_TICK, ALWAYS_ALIVE, NULL, sys_tick},//系统时间驱动EV_1MS、EV_25MS、EV_5S
{EV_TOUCH_SCAN, ALWAYS_ALIVE, NULL, task_touch_scan},//LED等的亮度变化,和按键的扫描
{EV_BIND, 0, NULL, adapter_bind},//
{EV_WIRQ, 0, NULL, wirless_check_irq},//无线的监测是通过中断吗????,接收无线
{EV_CLRDOG, ALWAYS_ALIVE, NULL, clr_watchdog},
{EV_REPS, 0, NULL, wirless_reponse},//对无线进行回应
{EV_5S, 0, NULL, touch_seq_check},//主要用来启动绑定,首先查询开关是否触发。检查触摸屏的变化并且,如有变化与适配器通信告知
是谁启动的EV_WIRQ
};
启动EV_WIRQ在两种函数里进行启动,其中有一个是自启动
自启动的条件是
if (si4438_delay) //如果不为0就不断的启动 无线检测 {
notify(EV_WIRQ);//启动自己的无线检测
}
void task_25ms(void *args)
{
if (!is_task_set(EV_REPS))//如果没有启动wireless_response就启动wireless_check_irq
{
notify(EV_WIRQ); //75MS Per.time 启动wirless_check_irq
}
// if ((silicon_status&SILLICON_ALL_SET)||is_task_set(EV_REPS)||is_task_set(EV_WIRQ)||cap_pad[0].led_on||cap_pad[1].led_on||cap_pad[2].led_on||cap_pad[3].led_on) return; //sleep or not
// enter_vlps_xms(15); //wake up for EV_WIRQ 75ms
if (bind_state_cnt!=0)
{
if(0==--bind_state_cnt)
{
device_bind_process=UBIND;//入网的时候会把bind_state_cnt赋值0x28,,在282内没有完成就是没有绑定成功。
}
}
}
void wirless_check_irq(void *args)
{
clr_wdog();
// uint8_t lenth=0;
if (DEVICE_IS_NEW==new_device_flag) //新设备将不执行正常检测中断 added in 2015-3-4 no test
{
return;//如果是新设备就不用检测无线的中断了。
}
if (si4438_statue == SI4438_SLEEP) //绑定成功就读,那么不成功也是读呀????因为绑定成功和不成功设备都要是SI4438_SLEEP
{
vRadio_StartRX(SI4438_CHANL, SI4438_DATALETH); //4MS 所有READCTS带自动复位功能, 读的东西放在哪里了????
si4438_statue = SI4438_RX;//为什么要把它置于读的状态????木 表示已经读过4438了吗???
//GPIOA_PCOR |= GPIO_PCOR_PTCO(CTRL2_PIN); //无线监听动作开始计量
}
if(0==read_4438_irq_port())//用来检测中断木 为0表示中断发生
{
if (si4438_statue==SI4438_TX)goto finish_rcv; //finish the tx//vRadio_StartRX只是开始读,把数据读出来只是bRadio_Check_Tx_RX这个函数
if(SI446X_CMD_GET_INT_STATUS_REP_PACKET_RX_PEND_BIT!=bRadio_Check_Tx_RX(g_frame_buffer))return;//从433里面读出了数据 木
si446x_change_state(1);//猜测是清除的状态
// clr_detc();
// GPIOA_PCOR |= GPIO_PCOR_PTCO(CTRL1_PIN); //无线接收发送动作开始计量
frame_ctl(&g_frame_buffer[0],Si446xCmd.FIFO_INFO.RX_FIFO_COUNT);//用读出来的数据改变灯的状态,把读出来的数据付诸于动作
// set_detc();
enter_vlps_xms(100); //进入低功率状态
si4438_delay = WIRELESS_DELAY;
goto finish_rcv;
}
if (si4438_delay) //如果不为0就不断的启动 无线检测 //CAL IN 1ms_TASK
{
notify(EV_WIRQ);//启动自己的无线检测
}
else
{
finish_rcv:;
si4438_delay=WIRELESS_DELAY; // WIRELESS_DELAY 0x28
si446x_get_int_status(0u, 0u, 0u);//猜测清零操作
si446x_change_state(1); //猜测是清除的状态
si4438_statue=SI4438_SLEEP; //sleep
// GPIOA_PSOR |= GPIO_PSOR_PTSO(CTRL2_PIN); //无线监听动作开始计量
if ((silicon_status&SILLICON_ALL_SET)||cap_pad[0].led_on
||cap_pad[1].led_on||cap_pad[2].led_on
||cap_pad[3].led_on) return; //sleep or not
enter_vlps_xms(100); //低功耗的作用
}
}