Show
Ignore:
Timestamp:
09/04/10 15:30:17 (14 years ago)
Author:
phil
Message:

introduced 2 byte checksum check for rx momentary power packets, including CRC err counter on web interface.

also implemented one byte checksum (incomplete, untested!)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • webserver/example/EnergyMeters/EnergyMeters/ARM7_LPC2368_Rowley/webserver/httpd-cgi.c

    r58 r66  
    6262 
    6363 
     64extern unsigned long correctVoltWattRx; 
     65extern unsigned long failedVoltWattRx; 
    6466extern unsigned char chanVolt[6]; /* holds the voltages measured on channel 0 to 5 */ 
    6567extern int chanWatt[6]; /* holds the wattages measured on channel 0 to 5 */ 
     
    245247 
    246248 
     249 
     250        sprintf( cCountBuf, "<h2>Watt_CRCok=%u\r\nWatt_CRCfail=%u\r\n</h2>", 
     251          correctVoltWattRx, failedVoltWattRx 
     252         ); 
     253        strcat( uip_appdata, cCountBuf ); 
     254 
     255 
     256 
    247257      sprintf( cCountBuf, "HIST %u %u\r\n", 
    248258      SOLAR_WATT_HISTORY_STEPSIZE, 
  • webserver/example/EnergyMeters/Source/EnergyMeters/MetersIncludes.h

    r61 r66  
    11 
    22void initMeterItems(void); 
     3 
     4portCHAR checkRxOneByteCheckSum(void); 
     5portCHAR checkRxTwoByteCheckSum(void); 
     6 
    37unsigned long long getEpochTimeWithMs(void); 
    48 
  • webserver/example/EnergyMeters/Source/EnergyMeters/SolarCountUART.c

    r58 r66  
    1313static void advanceHistoryArray(void); 
    1414 
     15 
     16void crc16_ccitt(char ser_data); 
     17 
    1518#define solarSTACK_SIZE         configMINIMAL_STACK_SIZE+200 
    1619 
     
    2932 
    3033 
     34unsigned long correctVoltWattRx = 0; /* counter of correctly received wattage/voltage packets */ 
     35unsigned long failedVoltWattRx = 0;  /* counter of incorrectly received wattage/voltage packets */ 
     36 
     37 
     38static unsigned short staticCRC; 
    3139 
    3240 
     
    503511        if ((currentChannel >= 0) && (currentChannel < 6)) 
    504512        { 
    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 
    509534        } /* if */ 
    510535 
     
    750775} 
    751776 
     777 
     778 
     779 
     780portCHAR 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 
     826portCHAR 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 
     859void 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