Show
Ignore:
Timestamp:
09/12/10 19:33:03 (14 years ago)
Author:
phil
Message:

query time once per minute now works.
more detailled output on FSM stuck error.
FSM needs redesign, search for "duplicate".

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • webserver/example/EnergyMeters/Source/EnergyMeters/SolarCountUART.c

    r67 r68  
    2323#define SOLAR_FSM_STUCK_TIMEOUT  2000 
    2424 
     25/* how often (in ms) do we query the timer? */ 
     26#define SOLAR_TIME_CHECK_INTERVAL (60 * 1000) 
     27 
     28 
    2529/* means "unknown value", converted to "U" on rrdtool script side */ 
    2630#define SOLAR_FSM_INVALID_VALUE  0x4000 /* 16384 */ 
     
    3438unsigned long correctVoltWattRx = 0; /* counter of correctly received wattage/voltage packets */ 
    3539unsigned long failedVoltWattRx = 0;  /* counter of incorrectly received wattage/voltage packets */ 
    36  
     40unsigned long queryTimeCounter = 0;  /* counter attempted queries for the time */ 
    3741 
    3842static unsigned short staticCRC; 
     
    8185unsigned char currentChannel; /* the SolarCount channel we currently query (here: 0 to 3) */ 
    8286 
    83 static int solarState = SOL_NO_INIT; /* state of the SolarCount communication state machine */ 
     87static int solarState = SOL_QUERY_TIME; /* state of the SolarCount communication state machine */ 
    8488 
    8589 
     
    140144portTickType xLastSolarStateChangeTime; 
    141145 
     146portTickType xLastTimeCheckTime; 
     147 
     148 
    142149 /* time when the elements of the history array were last moved on one step */ 
    143150portTickType xLastSolarHistoryAdvance; 
     
    148155 
    149156int solarReadCounter = 0; 
     157 
     158 
     159int solarStateWhenFail = 0xFF; 
     160int currentChannelWhenFail = 0xFF; 
    150161 
    151162/* pototype  section */ 
     
    181192  int i; 
    182193  xLastSolarStateChangeTime = xTaskGetTickCount(); 
     194  xLastTimeCheckTime = xTaskGetTickCount(); 
    183195  xLastReadTime = xTaskGetTickCount(); 
    184   solarState = SOL_NO_INIT
     196  solarState = SOL_QUERY_TIME
    185197  uart3TxRunning = 0; /* reset flag to tell if we are currently sending a datagram */ 
    186198  rxBuf3NextFreeRxPos = 0; 
     
    374386 
    375387  /* did the SolarFSM get stuck during reading? */ 
    376   if (xTaskGetTickCount() > xLastSolarStateChangeTime + SOLAR_FSM_STUCK_TIMEOUT
    377   { 
    378     /* something has gone wrong (possible Rx Timeout). restart, state SOL_NO_INIT */ 
     388  if (xTaskGetTickCount() > (xLastSolarStateChangeTime + SOLAR_FSM_STUCK_TIMEOUT)
     389  { 
     390    /* something has gone wrong (possible Rx Timeout). restart, state SOL_QUERY_TIME */ 
    379391    xLastSolarStateChangeTime = xTaskGetTickCount(); 
     392 
     393    /* save error info */ 
     394    solarStateWhenFail = solarState; 
     395    currentChannelWhenFail = currentChannel; 
     396     
    380397    initSolarFSM(); 
    381398    solarReadErrors++; 
     399 
    382400  } 
    383401  else if (xTaskGetTickCount() < xLastSolarStateChangeTime) 
     
    429447      /* now ready to query channel 0 data in next state */ 
    430448      xLastSolarStateChangeTime = xTaskGetTickCount(); 
    431       solarState = SOL_QUERY_TIME
     449      solarState = SOL_CHAN
    432450    } 
    433451    else 
     
    441459 
    442460      resetUart3RxBuf(); /* init */ 
    443       currentChannel = 0; /* the channel we query first */ 
    444461 
    445462       /* send query_sc_time */ 
    446463      send_uart3((unsigned char *)query_sc_time, sizeof(query_sc_time)); 
    447464      xLastSolarStateChangeTime = xTaskGetTickCount(); 
     465      queryTimeCounter++; 
    448466      solarState = SOL_WAIT_FOR_TIME_RSP; /* query sent wait for response */ 
    449467    break; 
     
    456474      if (cmpResult == 0) 
    457475      { 
    458         /* we have got a match! */ 
     476          /* we have got a match! */ 
    459477         
    460478          if (checkRxTwoByteCheckSum(1, 10, 11) == 1) /* checksum in bytes 11 and 12, check bytes 1 to 10 */ 
     
    472490          else 
    473491          { 
    474            
     492            /* time rx checksum error */
    475493          } 
    476494 
     
    478496        resetUart3RxBuf(); 
    479497 
    480  
    481         solarState = SOL_CHAN; /* change state: time response received, now query the channels */ 
     498        xLastSolarStateChangeTime = xTaskGetTickCount(); 
     499        solarState = SOL_NO_INIT; /* change state: time response received, now init and query the channels */ 
    482500      } 
    483501      else 
     
    517535        xLastSolarStateChangeTime = xTaskGetTickCount(); 
    518536        solarState = SOL_FINISHED; 
     537        currentChannel = 0; 
     538 
     539        /* new duplicated code: */ 
     540 
     541        resetUart3RxBuf(); 
     542 
     543 
     544        if (xTaskGetTickCount() > (xLastTimeCheckTime + SOLAR_TIME_CHECK_INTERVAL)) 
     545        { 
     546           /* time to get the time info again! */ 
     547           xLastTimeCheckTime = xTaskGetTickCount(); 
     548           solarState = SOL_QUERY_TIME; /* restart the measurement cycle (with time check at first) */ 
     549        } 
     550        else if (xTaskGetTickCount() < xLastTimeCheckTime) 
     551        { 
     552          /* overflow of timer, just recover. delay will be wrong. */ 
     553          xLastTimeCheckTime = xTaskGetTickCount(); 
     554          solarState = SOL_NO_INIT; /* restart the measurement cycle (without time check at first) */ 
     555        } 
     556        else 
     557        { 
     558          /* not time to get the time info. leave state at SOL_FINISHED. */ 
     559        } 
     560 
     561 
     562 
    519563        break; 
    520564      default: 
    521         ; /* do nothing */ 
     565        currentChannel = 0; /* current channel not 0 to 6, reset it. */ 
     566       /* nothing else to do */ 
     567 
    522568     } /* switch */ 
    523      if (solarState != SOL_FINISHED
     569     if ((solarState != SOL_FINISHED) && (solarState != SOL_QUERY_TIME)
    524570     { 
    525      xLastSolarStateChangeTime = xTaskGetTickCount(); 
     571      xLastSolarStateChangeTime = xTaskGetTickCount(); 
    526572      solarState = SOL_CHAN_WAIT_REPLY; /* wait for reply if not finished */ 
    527573     } /* if */ 
     
    552598         case 6: 
    553599          /* wait for the response to the finishing init */ 
    554           cmpResult = checkUart3Received(init_after_all_chan_rsp, init_after_all_chan_r_m,  sizeof(init_after_all_chan_rsp)-1); 
     600          // cmpResult = checkUart3Received(init_after_all_chan_rsp, init_after_all_chan_r_m,  sizeof(init_after_all_chan_rsp)-1); 
    555601          break; 
    556602        default: 
     
    601647          /* switch to next channel */ 
    602648          currentChannel++; 
     649 
     650          /* finally, discard data from buffer */ 
     651          resetUart3RxBuf(); 
     652 
    603653          /* now ready to query channel data for next channel */ 
    604654          xLastSolarStateChangeTime = xTaskGetTickCount(); 
     
    607657        else 
    608658        { 
     659 
     660          /* I think we never go here ... */ 
     661 
    609662          /* we received the finishing sequence */ 
    610663          xLastSolarStateChangeTime = xTaskGetTickCount(); 
    611           solarState = SOL_NO_INIT; /* restart the measurement cycle */ 
    612         } 
    613  
    614         resetUart3RxBuf(); 
     664 
     665          resetUart3RxBuf(); 
     666 
     667          // may be duplicated 
     668          if (xTaskGetTickCount() > (xLastTimeCheckTime + SOLAR_TIME_CHECK_INTERVAL)) 
     669          { 
     670            /* time to get the time info again! */ 
     671            xLastTimeCheckTime = xTaskGetTickCount(); 
     672            solarState = SOL_QUERY_TIME; /* restart the measurement cycle (with time check at first) */ 
     673          } 
     674          else if (xTaskGetTickCount() < xLastTimeCheckTime) 
     675          { 
     676            /* overflow of timer, just recover. delay will be wrong. */ 
     677            xLastTimeCheckTime = xTaskGetTickCount(); 
     678            solarState = SOL_NO_INIT; /* restart the measurement cycle (without time check at first) */ 
     679          } 
     680          else 
     681          { 
     682            /* not time to get the time info. do a regular reading cycle without time */ 
     683            solarState = SOL_NO_INIT; /* restart the measurement cycle (without time check at first) */ 
     684          } 
     685        } /* else */ 
    615686 
    616687      }