simulator_modules/mUtils.c
/**
 * calculate the CRC of a given array of byte
 * @param buf the message where a CRC needs to be generated
 * @param len length of the message
 * @param lastCRC if first run, init to 0xFFFF. Else, give the last known CRC
 * @return the computed CRC with the byte a the correct order
 */
calculate the CRC of a given array of byte
@param buf the message where a CRC needs to be generated
@param len length of the message
@param lastCRC if first run, init to 0xFFFF. Else, give the last known CRC
@return the computed CRC with the byte a the correct order
calculate the CRC of a given array of byte @param buf the message where a CRC needs to be generated @param len length of the message @param lastCRC if first run init to 0xFFFF . Else give the last known CRC @return the computed CRC with the byte a the correct order
int16_t mUtils_calc_crc(uint8_t *buf, uint32_t len, uint16_t lastCRC)
{
	uint16_t crc = (lastCRC >> 8) & 0x00FF;
	crc |= (lastCRC << 8) & 0xFF00;
	for (uint32_t pos = 0; pos < len; pos++)
	{
		crc ^= (uint16_t)buf[pos]; 
		int i;
		for (i = 8; i != 0; i--)
		{ 
			if ((crc & 0x0001) != 0)
			{              
				crc >>= 1; 
				crc ^= 0xA001;
			}
			else
				crc >>= 1; 
		}
	}
	uint16_t temp_crc = crc;
	crc = (temp_crc >> 8) & 0x00FF;
	crc |= (temp_crc << 8) & 0xFF00;
	return crc;
}
int16_t mUtils_calc_crc ( uint8_t * buf , uint32_t len , uint16_t lastCRC ) { uint16_t crc = ( lastCRC >> 8 ) & 0x00FF ; crc |= ( lastCRC << 8 ) & 0xFF00 ; for ( uint32_t pos = 0 ; pos < len ; pos ++ ) { crc ^= ( uint16_t ) buf [ pos ] ; int i ; for ( i = 8 ; i != 0 ; i -- ) { if ( ( crc & 0x0001 ) != 0 ) { crc >>= 1 ; crc ^= 0xA001 ; } else crc >>= 1 ; } } uint16_t temp_crc = crc ; crc = ( temp_crc >> 8 ) & 0x00FF ; crc |= ( temp_crc << 8 ) & 0xFF00 ; return crc ; }
calculate the CRC of a given array of byte
 @param buf the message where a CRC needs to be generated
 @param len length of the message
 @param lastCRC if first run, init to 0xFFFF.
calculate the CRC of a given array of byte @param buf the message where a CRC needs to be generated @param len length of the message @param lastCRC if first run init to 0xFFFF .
// swap crc byte // XOR byte into least sig. byte of crc // Loop over each bit // If the LSB is set // Shift right and XOR 0xA001 // Else LSB is not set // Just shift right // swap crc