123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- #include "plt.h"
- #include "cw1310_comm.h"
- #include "cw1310.h"
- #include "cw1310_sm.h"
- static struct state_t cw1310_states[] = {
- { SMST_LAUNCH, "launch" },
- { SMST_READY, "ready" },
- { SMST_ERR, "err" },
- };
- static struct err_t cw1310_errs[] = {
- { CW1310ERR_NONE, "none" },
- // launch
- { CW1310ERR_LAUNCH_COMMERR, "launch, comm err" },
- // err
- { CW1310ERR_ERR_COMMERR, "err comm err" },
- // ready
- { CW1310ERR_READY_COMMERR, "ready comm err" },
- };
- int cw1310_sm_init(int idx)
- {
- struct statemachine_t* sm = &cw1310[idx].sm;
- sm_reset_timing(sm, 1, 1);
- sm->states = cw1310_states;
- sm->state_nbr = sizeof(cw1310_states)/sizeof(struct state_t);
- sm->errs = cw1310_errs;
- sm->err_nbr = sizeof(cw1310_errs)/sizeof(struct err_t);
- sm_set_state( sm, SMST_LAUNCH, CW1310ERR_NONE );
-
- return 0;
- }
- static void cw1310_sm_launch( int idx )
- {
- struct cw1310_t* dev = &cw1310[idx];
- struct comm_t* comm = &dev->comm;
- struct statemachine_t* sm = &dev->sm;
- if( sm_get_step(sm) == 0 ){ // entry
- log_dbg("%s, idx:%d, state:%s, step:%d, entry", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- cw1310_reset_cmd(idx);
- cw1310_comm_reset(idx);
- sm_set_step(sm, 20);
- }
- /*
- if( sm_get_step(sm) == 0 ){ // entry
- log_dbg("%s, idx:%d, state:%s, step:%d, entry", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- cw1310_reset_cmd(idx);
- sm_set_step(sm, 10);
- }else if( sm_get_step(sm) == 10){ // wait cmd
- if( cw1310_get_cmd(idx) == CMD_SM_READY){
- log_dbg("%s, idx:%d, state:%s, step:%d, get ready cmd, try to comm",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- cw1310_reset_cmd(idx);
- cw1310_comm_reset(idx);
- sm_set_step(sm, 20);
- }
- }*/else if( sm_get_step(sm) == 20){ // chk comm state
- if( comm_get_state(comm) == COMMST_NORMAL){
- log_dbg("%s, idx:%d, state:%s, step:%d, comm ok, goto ready",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_READY, CW1310ERR_NONE);
- }else{
- log_dbg("%s, idx:%d, state:%s, step:%d, comm err, goto err",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_ERR, CW1310ERR_LAUNCH_COMMERR);
- }
- }
- }
- static void cw1310_sm_err( int idx )
- {
- struct cw1310_t* dev = &cw1310[idx];
- struct comm_t* comm = &dev->comm;
- struct statemachine_t* sm = &dev->sm;
- static double ts_last_try;
- double ts;
- if( sm_get_step(sm) == 0 ){ // entry
- cw1310_reset_cmd(idx);
- sm_set_step(sm, 10);
- ts_last_try = sm_get_timeofday();
- }else if( sm_get_step(sm) == 10){ // wait cmd
- ts = sm_get_timeofday();
- if( cw1310_get_cmd(idx) == CMD_SM_READY){
- log_dbg("%s, idx:%d, state:%s, step:%d, get ready cmd, try to comm",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- cw1310_reset_cmd(idx);
- cw1310_comm_reset(idx);
- sm_set_step(sm, 20);
- }else if( ts - ts_last_try > 60000 ){ // 60s
- ts_last_try = ts;
- cw1310_comm_reset(idx);
- sm_set_step(sm, 20);
- }
- }else if( sm_get_step(sm) == 20){ // chk comm state
- if( comm_get_state(comm) == COMMST_NORMAL){
- log_dbg("%s, idx:%d, state:%s, step:%d, comm ok, goto ready", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_READY, CW1310ERR_NONE);
- }else{
- log_dbg("%s, idx:%d, state:%s, step:%d, comm err, stay err", __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_ERR, CW1310ERR_ERR_COMMERR);
- }
- }
- }
- static void cw1310_sm_ready( int idx )
- {
- struct cw1310_t* dev = &cw1310[idx];
- struct comm_t* comm = &dev->comm;
- struct statemachine_t* sm = &dev->sm;
- /* chk comm state */
- if( comm_get_state(comm) != COMMST_NORMAL){
- log_dbg("%s, idx:%d, state:%s, step:%d, comm err detected, goto err",
- __func__, idx, sm_get_szstate(sm), sm_get_step(sm));
- sm_set_state(sm, SMST_ERR, CW1310ERR_READY_COMMERR);
- return;
- }
- }
- void* cw1310_sm( int idx )
- {
- struct cw1310_t* dev = &cw1310[idx];
- struct statemachine_t* sm = &dev->sm;
- sm_cal_timing(sm);
- switch( sm_get_state(sm) ){
- case SMST_LAUNCH:
- cw1310_sm_launch( idx );
- break;
- case SMST_READY:
- cw1310_sm_ready( idx );
- break;
- case SMST_ERR:
- cw1310_sm_err( idx );
- break;
- default:
- log_dbg("%s unknown state : %d",__func__, sm_get_state(sm));
- break;
- }
- }
|