DATA_SEG struct PLC_MACHINE plc_state;
struct PLC_STATE
{
u8 cur_state;
u8 next_state;
u8(*action) (u8 init, void *args);
};
struct PLC_MACHINE
{
u8 init;
u8 trycnt;
u8 wait_t;
struct PLC_STATE *pstate;
};
memset(&plc_state, 0, sizeof(plc_state));置0为PLC_MACHINE下的所有参数为零,如init为0;trycnt 为0 ;wait_t为0; u8(*action) (u8 init, void *args);这是这个函数里面的init 也为0;
chg_state(RST_PLC);在初始化的时候写这个函数就是从状态机的这个位置开始运行。const struct PLC_STATE plc_state_slot[] =
{
{RST_PLC, R_EID, reset_plc},
{R_EID, S_AID, rd_plc_eid},
{S_AID, UNLINK2, wr_plc_aid},
{UNLINK2, WAIT, set_unlink},
{WAIT, S_PWDREG, wait_sec},
{S_PWDREG, S_PANID, set_register},
{S_PANID, _END, wr_plc_panid},
#if KEY_REG
{UNLINK1, S_REG, set_unlink},
#endif
{G_GWID, S_REG, rd_gw_aid},
{S_REG, G_SID, set_register},
{G_SID, _END, rd_plc_sid},
{_END, _END, frame_handle},
};
static u8 reset_plc(u8 init, void *args)
{
if (init)
{//上电init为零就不用复位载波模块了。
SET_PIN_L(PLC_RESET_PORT, PLC_RESET_PIN);
return 0;
}
if (plc_state.wait_t >= MIN_PLC_RUN)
{
next_state();
}
else if (plc_state.wait_t >= 1)
{
SET_PIN_H(PLC_RESET_PORT, PLC_RESET_PIN);
}
return 0;
}