505 | | portENTER_CRITICAL(); |
---|
506 | | chanVolt[currentChannel] = rxUART3[11]; |
---|
507 | | chanWatt[currentChannel] = (int)(rxUART3[7]) | (((int)(rxUART3[8])) << 8) /* index 7: LSB, index 8: MSB */; |
---|
508 | | portEXIT_CRITICAL(); |
---|
| 513 | |
---|
| 514 | if (checkRxTwoByteCheckSum() == 1) |
---|
| 515 | { |
---|
| 516 | /* checksum correct */ |
---|
| 517 | portENTER_CRITICAL(); |
---|
| 518 | chanVolt[currentChannel] = rxUART3[11]; |
---|
| 519 | chanWatt[currentChannel] = (int)(rxUART3[7]) | (((int)(rxUART3[8])) << 8) /* index 7: LSB, index 8: MSB */; |
---|
| 520 | correctVoltWattRx++; |
---|
| 521 | portEXIT_CRITICAL(); |
---|
| 522 | } |
---|
| 523 | else |
---|
| 524 | { |
---|
| 525 | /* checksum incorrect */ |
---|
| 526 | portENTER_CRITICAL(); |
---|
| 527 | /* mark values as invalid */ |
---|
| 528 | //chanVolt[currentChannel] = 1; |
---|
| 529 | chanWatt[currentChannel] = SOLAR_FSM_INVALID_VALUE; |
---|
| 530 | failedVoltWattRx++; |
---|
| 531 | portEXIT_CRITICAL(); |
---|
| 532 | } |
---|
| 533 | |
---|
| 777 | |
---|
| 778 | |
---|
| 779 | |
---|
| 780 | portCHAR checkRxOneByteCheckSum(void) |
---|
| 781 | { |
---|
| 782 | portCHAR retVal; /* return value */ |
---|
| 783 | |
---|
| 784 | // "\xf0\xb0\x0a\x00\xa0\x00\x87\x00\x00\x00\xec\x00\xf2\ |
---|
| 785 | |
---|
| 786 | int x; |
---|
| 787 | |
---|
| 788 | unsigned long crcLONG = 0; |
---|
| 789 | unsigned char crcCHAR; |
---|
| 790 | unsigned char crcCHARinv; |
---|
| 791 | unsigned char carry = 0; |
---|
| 792 | |
---|
| 793 | /* use bytes 1 to 13 for the checksum */ |
---|
| 794 | for (x = 1; x <= 13; x++) |
---|
| 795 | { |
---|
| 796 | |
---|
| 797 | /* overflow? */ |
---|
| 798 | // if (((portSHORT)crc + (portSHORT)(rxUART3[x])) > 0xFF) |
---|
| 799 | // { |
---|
| 800 | // carry++; |
---|
| 801 | // } |
---|
| 802 | |
---|
| 803 | crcLONG = (crcLONG + rxUART3[x]); /* add and truncate to 8 bit */ |
---|
| 804 | } |
---|
| 805 | |
---|
| 806 | carry = (unsigned char)(((crcLONG & 0xFF00) >> 8)); |
---|
| 807 | |
---|
| 808 | crcCHAR = (unsigned char)(crcLONG & 0xFF); |
---|
| 809 | |
---|
| 810 | crcCHAR = (unsigned char)(crcCHAR + carry); |
---|
| 811 | crcCHARinv = (unsigned char)(~crcCHAR); |
---|
| 812 | |
---|
| 813 | |
---|
| 814 | if (crcCHARinv == rxUART3[14]) |
---|
| 815 | { |
---|
| 816 | retVal = 1; /* checksum OK, data is valid */ |
---|
| 817 | } |
---|
| 818 | else |
---|
| 819 | { |
---|
| 820 | retVal = 0; /* checksum error, data is invalid */ |
---|
| 821 | } |
---|
| 822 | return retVal; |
---|
| 823 | } /* checkRxOneByteCheckSum */ |
---|
| 824 | |
---|
| 825 | |
---|
| 826 | portCHAR checkRxTwoByteCheckSum(void) |
---|
| 827 | { |
---|
| 828 | unsigned short cmpValue; |
---|
| 829 | portCHAR retVal; /* return value */ |
---|
| 830 | portCHAR x; |
---|
| 831 | |
---|
| 832 | staticCRC = 0xFFFF; /* init 16 bit checksum CRC start value */ |
---|
| 833 | |
---|
| 834 | /* use bytes 1 to 13 for the checksum */ |
---|
| 835 | for (x = 1; x <= 12; x++) |
---|
| 836 | { |
---|
| 837 | |
---|
| 838 | crc16_ccitt(rxUART3[x]); |
---|
| 839 | } |
---|
| 840 | |
---|
| 841 | |
---|
| 842 | |
---|
| 843 | cmpValue = ((((unsigned short)rxUART3[14])<<8) | rxUART3[13]); |
---|
| 844 | if (cmpValue == (staticCRC & 0xFFFF)) |
---|
| 845 | { |
---|
| 846 | retVal = 1; /* checksum OK, data is valid */ |
---|
| 847 | } |
---|
| 848 | else |
---|
| 849 | { |
---|
| 850 | retVal = 0; /* checksum error, data is invalid */ |
---|
| 851 | } |
---|
| 852 | return retVal; |
---|
| 853 | |
---|
| 854 | } /* portCHAR checkRxTwoByteCheckSum() */ |
---|
| 855 | |
---|
| 856 | |
---|
| 857 | |
---|
| 858 | |
---|
| 859 | void crc16_ccitt(char ser_data) |
---|
| 860 | { |
---|
| 861 | staticCRC = (unsigned char)(staticCRC >> 8) | (staticCRC << 8); |
---|
| 862 | staticCRC ^= ser_data; |
---|
| 863 | staticCRC ^= (unsigned char)(staticCRC & 0xff) >> 4; |
---|
| 864 | staticCRC ^= (staticCRC << 8) << 4; |
---|
| 865 | staticCRC ^= ((staticCRC & 0xff) << 4) << 1; |
---|
| 866 | } |
---|
| 867 | |
---|
| 868 | |
---|