Changeset 68 for webserver/example/EnergyMeters/Source
- Timestamp:
- 09/12/10 19:33:03 (14 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
webserver/example/EnergyMeters/Source/EnergyMeters/SolarCountUART.c
r67 r68 23 23 #define SOLAR_FSM_STUCK_TIMEOUT 2000 24 24 25 /* how often (in ms) do we query the timer? */ 26 #define SOLAR_TIME_CHECK_INTERVAL (60 * 1000) 27 28 25 29 /* means "unknown value", converted to "U" on rrdtool script side */ 26 30 #define SOLAR_FSM_INVALID_VALUE 0x4000 /* 16384 */ … … 34 38 unsigned long correctVoltWattRx = 0; /* counter of correctly received wattage/voltage packets */ 35 39 unsigned long failedVoltWattRx = 0; /* counter of incorrectly received wattage/voltage packets */ 36 40 unsigned long queryTimeCounter = 0; /* counter attempted queries for the time */ 37 41 38 42 static unsigned short staticCRC; … … 81 85 unsigned char currentChannel; /* the SolarCount channel we currently query (here: 0 to 3) */ 82 86 83 static int solarState = SOL_ NO_INIT; /* state of the SolarCount communication state machine */87 static int solarState = SOL_QUERY_TIME; /* state of the SolarCount communication state machine */ 84 88 85 89 … … 140 144 portTickType xLastSolarStateChangeTime; 141 145 146 portTickType xLastTimeCheckTime; 147 148 142 149 /* time when the elements of the history array were last moved on one step */ 143 150 portTickType xLastSolarHistoryAdvance; … … 148 155 149 156 int solarReadCounter = 0; 157 158 159 int solarStateWhenFail = 0xFF; 160 int currentChannelWhenFail = 0xFF; 150 161 151 162 /* pototype section */ … … 181 192 int i; 182 193 xLastSolarStateChangeTime = xTaskGetTickCount(); 194 xLastTimeCheckTime = xTaskGetTickCount(); 183 195 xLastReadTime = xTaskGetTickCount(); 184 solarState = SOL_ NO_INIT;196 solarState = SOL_QUERY_TIME; 185 197 uart3TxRunning = 0; /* reset flag to tell if we are currently sending a datagram */ 186 198 rxBuf3NextFreeRxPos = 0; … … 374 386 375 387 /* 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 */ 379 391 xLastSolarStateChangeTime = xTaskGetTickCount(); 392 393 /* save error info */ 394 solarStateWhenFail = solarState; 395 currentChannelWhenFail = currentChannel; 396 380 397 initSolarFSM(); 381 398 solarReadErrors++; 399 382 400 } 383 401 else if (xTaskGetTickCount() < xLastSolarStateChangeTime) … … 429 447 /* now ready to query channel 0 data in next state */ 430 448 xLastSolarStateChangeTime = xTaskGetTickCount(); 431 solarState = SOL_ QUERY_TIME;449 solarState = SOL_CHAN; 432 450 } 433 451 else … … 441 459 442 460 resetUart3RxBuf(); /* init */ 443 currentChannel = 0; /* the channel we query first */444 461 445 462 /* send query_sc_time */ 446 463 send_uart3((unsigned char *)query_sc_time, sizeof(query_sc_time)); 447 464 xLastSolarStateChangeTime = xTaskGetTickCount(); 465 queryTimeCounter++; 448 466 solarState = SOL_WAIT_FOR_TIME_RSP; /* query sent wait for response */ 449 467 break; … … 456 474 if (cmpResult == 0) 457 475 { 458 /* we have got a match! */476 /* we have got a match! */ 459 477 460 478 if (checkRxTwoByteCheckSum(1, 10, 11) == 1) /* checksum in bytes 11 and 12, check bytes 1 to 10 */ … … 472 490 else 473 491 { 474 ;492 /* time rx checksum error */ ; 475 493 } 476 494 … … 478 496 resetUart3RxBuf(); 479 497 480 481 solarState = SOL_ CHAN; /* change state: time response received, nowquery the channels */498 xLastSolarStateChangeTime = xTaskGetTickCount(); 499 solarState = SOL_NO_INIT; /* change state: time response received, now init and query the channels */ 482 500 } 483 501 else … … 517 535 xLastSolarStateChangeTime = xTaskGetTickCount(); 518 536 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 519 563 break; 520 564 default: 521 ; /* do nothing */ 565 currentChannel = 0; /* current channel not 0 to 6, reset it. */ 566 /* nothing else to do */ 567 522 568 } /* switch */ 523 if ( solarState != SOL_FINISHED)569 if ((solarState != SOL_FINISHED) && (solarState != SOL_QUERY_TIME)) 524 570 { 525 xLastSolarStateChangeTime = xTaskGetTickCount();571 xLastSolarStateChangeTime = xTaskGetTickCount(); 526 572 solarState = SOL_CHAN_WAIT_REPLY; /* wait for reply if not finished */ 527 573 } /* if */ … … 552 598 case 6: 553 599 /* 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); 555 601 break; 556 602 default: … … 601 647 /* switch to next channel */ 602 648 currentChannel++; 649 650 /* finally, discard data from buffer */ 651 resetUart3RxBuf(); 652 603 653 /* now ready to query channel data for next channel */ 604 654 xLastSolarStateChangeTime = xTaskGetTickCount(); … … 607 657 else 608 658 { 659 660 /* I think we never go here ... */ 661 609 662 /* we received the finishing sequence */ 610 663 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 */ 615 686 616 687 }