123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- #include "plt.h"
- static struct state_t mc90hdnc1a_states[] = {
- { SMST_LAUNCH, "launch" },
- { SMST_READY, "ready" },
- { SMST_ERR, "err" },
- };
- static struct err_t mc90hdnc1a_errs[] = {
- { MC90HDNC1AERR_NONE, "none" },
- // launch
- { MC90HDNC1AERR_LAUNCH_COMMERR, "launch, comm err" },
- // err
- { MC90HDNC1AERR_ERR_COMMERR, "err comm err" },
- // ready
- { MC90HDNC1AERR_READY_COMMERR, "ready comm err" },
- };
- int mc90hdnc1a_sm_init(int idx)
- {
- struct statemachine_t* sm = &mc90hdnc1a[idx].sm;
- sm_reset_timing(sm, 1, 1);
- sm->states = mc90hdnc1a_states;
- sm->state_nbr = sizeof(mc90hdnc1a_states)/sizeof(struct state_t);
- sm->errs = mc90hdnc1a_errs;
- sm->err_nbr = sizeof(mc90hdnc1a_errs)/sizeof(struct err_t);
- sm_set_state( sm, SMST_LAUNCH, MC90HDNC1AERR_NONE );
-
- return 0;
- }
- static void mc90hdnc1a_sm_launch( int idx )
- {
- struct mc90hdnc1a_t* dev = &mc90hdnc1a[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));
- mc90hdnc1a_reset_cmd(idx);
- mc90hdnc1a_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));
- mc90hdnc1a_reset_cmd(idx);
- sm_set_step(sm, 10);
- }else if( sm_get_step(sm) == 10){ // wait cmd
- if( mc90hdnc1a_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));
- mc90hdnc1a_reset_cmd(idx);
- mc90hdnc1a_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, MC90HDNC1AERR_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, MC90HDNC1AERR_LAUNCH_COMMERR);
- }
- }
- }
- static void mc90hdnc1a_sm_err( int idx )
- {
- struct mc90hdnc1a_t* dev = &mc90hdnc1a[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
- mc90hdnc1a_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( mc90hdnc1a_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));
- mc90hdnc1a_reset_cmd(idx);
- mc90hdnc1a_comm_reset(idx);
- sm_set_step(sm, 20);
- }else if( ts - ts_last_try > 60000 ){ // 60s
- ts_last_try = ts;
- mc90hdnc1a_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, MC90HDNC1AERR_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, MC90HDNC1AERR_ERR_COMMERR);
- }
- }
- }
- static void mc90hdnc1a_sm_ready( int idx )
- {
- struct mc90hdnc1a_t* dev = &mc90hdnc1a[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, MC90HDNC1AERR_READY_COMMERR);
- return;
- }
- }
- void* mc90hdnc1a_sm( int idx )
- {
- struct mc90hdnc1a_t* dev = &mc90hdnc1a[idx];
- struct statemachine_t* sm = &dev->sm;
- sm_cal_timing(sm);
- switch( sm_get_state(sm) ){
- case SMST_LAUNCH:
- mc90hdnc1a_sm_launch( idx );
- break;
- case SMST_READY:
- mc90hdnc1a_sm_ready( idx );
- break;
- case SMST_ERR:
- mc90hdnc1a_sm_err( idx );
- break;
- default:
- log_dbg("%s unknown state : %d",__func__, sm_get_state(sm));
- break;
- }
- }
|