int main(void) { uint8_t crc = 0; uint8_t i; for (i = 0; i < mlen; i++) crc = _crc8_ccitt_update(crc, message[i]); if (crc != 0) return __LINE__; return 0; }
uint8_t frame_crc_calc(uint8_t crc, uint8_t data) { return _crc8_ccitt_update(crc, data); }
bool _moduloTransfer( uint8_t address, uint8_t command, uint8_t *sendData, uint8_t sendLen, uint8_t *receiveData, uint8_t receiveLen, bool receiveString) { // Star the transmit CRC with the address in the upper 7 bits uint8_t crc = _crc8_ccitt_update(0, address); TWI_BEGIN_WRITE(address); // Send the command and length TWI_WRITE(command); TWI_WRITE(sendLen); crc = _crc8_ccitt_update(crc, command); crc = _crc8_ccitt_update(crc, sendLen); // Send the data for (int i=0; i < sendLen; i++) { TWI_WRITE(sendData[i]); crc = _crc8_ccitt_update(crc, sendData[i]); } // Send the CRC and end the transmission TWI_WRITE(crc); if (TWI_END_WRITE(receiveLen == 0) != 0) { return false; } if (receiveLen == 0) { return true; } while (TWI_AVAILABLE()) { TWI_READ(); } // Request receiveLen data bytes plus 1 CRC byte. if (TWI_REQUEST_FROM((int)address, (int)receiveLen+1) != receiveLen+1) { return false; } // Start the CRC with the I2C address byte (address in upper 7, 1 in lsb) crc = _crc8_ccitt_update(0, address); // Receive the data for (int i=0; i < receiveLen; i++) { receiveData[i] = TWI_READ(); // If we're receiving a string, then stop when we find a 0 byte. if (receiveString and i > 0 and receiveData[i-1] == 0) { return (receiveData[i] == crc); } crc = _crc8_ccitt_update(crc, receiveData[i]); } // Receive the CRC. uint8_t receivedCRC = TWI_READ(); // Check the CRC. return (crc == receivedCRC); }