From a8377b09943b2000d40e7e8e3a5ee02cf96c08ff Mon Sep 17 00:00:00 2001 From: Zifeng1997 <58804221+xzf89718@users.noreply.github.com> Date: Mon, 14 Nov 2022 17:18:02 +0800 Subject: [PATCH 1/3] CRC8 check update --- AHT20_sensor_control/AHT20_sensor_wrapper.py | 6 +-- AHT20_sensor_control/crc8_helper.py | 49 ++++++++++++++++++-- 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/AHT20_sensor_control/AHT20_sensor_wrapper.py b/AHT20_sensor_control/AHT20_sensor_wrapper.py index 1774059..9a1517a 100644 --- a/AHT20_sensor_control/AHT20_sensor_wrapper.py +++ b/AHT20_sensor_control/AHT20_sensor_wrapper.py @@ -195,9 +195,9 @@ def CheckCRC(self): """ # Get all 6 bytes data and 1 bytes CRC code frame_for_CRC8_check = 0x00 - for i in range(0, 7): - frame_for_CRC8_check << 8 | self.all_data[i] - isCRCOK = AHT20_crc8_check(frame_for_CRC8_check) + # for i in range(0, 7): + # frame_for_CRC8_check << 8 | self.all_data[i] + isCRCOK = AHT20_crc8_check(self.all_data) if (isCRCOK): return AHT20Status.AHT20_CRCOK else: diff --git a/AHT20_sensor_control/crc8_helper.py b/AHT20_sensor_control/crc8_helper.py index 79b57cc..5401ee8 100644 --- a/AHT20_sensor_control/crc8_helper.py +++ b/AHT20_sensor_control/crc8_helper.py @@ -1,6 +1,49 @@ +# A total of 6 * 8 bits data need to check +N_DATA = 6 +# 1 * 8 bits CRC +N_CRC = 1 +# Initial value. Equal to bit negation the first data (status of AHT20) +INIT = 0xFF +# Useful value to help calculate +LAST_8_bit = 0xFF -def AHT20_crc8_check(int_data): + +# Devide number retrieve from CRC-8 MAXIM G(x) = x8 + x5 + x4 + 1 +CRC_DEVIDE_NUMBER = 0x131 + +# Data and CRC taken from AHT20, use this for testing? +TEST_DATA = [[28, 184, 245, 165, 156, 208, 163], [28, 185, 16, 149, 156, 83, 112], [ + 28, 184, 249, 85, 156, 114, 213], [28, 185, 9, 53, 156, 54, 45], [28, 185, 70, 117, 156, 189, 33] +, [28, 185, 64, 165, 156, 61, 209]] + + +def AHT20_crc8_check(all_data_int, init_value=0xFF): + """ + The input data shoule be: + Status Humidity0 Humidity1 Humidity2|Temperature0 Temperature1 Temperature2 CRCCode + In python's int64. + """ + # Preprocess all the data and CRCCode from AHT20 + DATA_FROM_AHT20 = 0x00 + # Preprocessing the first data (status) + DATA_FROM_AHT20 = DATA_FROM_AHT20 | all_data_int[0] + print(bin(DATA_FROM_AHT20)) + DATA_FROM_AHT20 = DATA_FROM_AHT20 ^ init_value + print(bin(DATA_FROM_AHT20)) + for i_data in range(1, N_DATA + N_CRC): + DATA_FROM_AHT20 = DATA_FROM_AHT20 << 8 | all_data_int[i_data] + print(bin(DATA_FROM_AHT20)) + mod_value = DATA_FROM_AHT20 % CRC_DEVIDE_NUMBER + print(mod_value) + if (mod_value == 0): + return True + else: + return False + + +def AHT20_crc8_calculate(all_data_int): pass -def AHT20_crc_calculate(int_data): - pass \ No newline at end of file +if __name__ == "__main__": + for data in TEST_DATA: + AHT20_crc8_check(data, INIT) \ No newline at end of file From d9b742c9b3294efb4c248d84242776440947f4fe Mon Sep 17 00:00:00 2001 From: Zifeng1997 <58804221+xzf89718@users.noreply.github.com> Date: Tue, 15 Nov 2022 14:17:45 +0800 Subject: [PATCH 2/3] Update crc8_helper.py --- AHT20_sensor_control/crc8_helper.py | 58 ++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/AHT20_sensor_control/crc8_helper.py b/AHT20_sensor_control/crc8_helper.py index 5401ee8..2f4bf1e 100644 --- a/AHT20_sensor_control/crc8_helper.py +++ b/AHT20_sensor_control/crc8_helper.py @@ -13,14 +13,38 @@ # Data and CRC taken from AHT20, use this for testing? TEST_DATA = [[28, 184, 245, 165, 156, 208, 163], [28, 185, 16, 149, 156, 83, 112], [ - 28, 184, 249, 85, 156, 114, 213], [28, 185, 9, 53, 156, 54, 45], [28, 185, 70, 117, 156, 189, 33] -, [28, 185, 64, 165, 156, 61, 209]] + 28, 184, 249, 85, 156, 114, 213], [28, 185, 9, 53, 156, 54, 45], [28, 185, 70, 117, 156, 189, 33], [28, 185, 64, 165, 156, 61, 209]] + + +def mod2_division_8bits(a, b, number_of_bytes, init_value): + "calculate mod2 division in 8 bits. a mod b. init_value is for crc8 init value." + head_of_a = 0x80 + # Processiong a + a = a << 8 + # Preprocessing head_of_a + for i in range(0, number_of_bytes): + head_of_a = head_of_a << 8 + b = b << 8 + init_value = init_value << 8 + a = a ^ init_value + while (head_of_a > 0x80): + # Find a 1 + if (head_of_a & a): + head_of_a = head_of_a >> 1 + b = b >> 1 + a = a ^ b + else: + head_of_a = head_of_a >> 1 + b = b >> 1 + print("a:{0}\thead of a:{1}\tb:{2}".format( + bin(a), bin(head_of_a), bin(b))) + return a def AHT20_crc8_check(all_data_int, init_value=0xFF): """ The input data shoule be: - Status Humidity0 Humidity1 Humidity2|Temperature0 Temperature1 Temperature2 CRCCode + Status Humidity0 Humidity1 Humidity2|Temperature0 Temperature1 Temperature2 CRCCode. In python's int64. """ # Preprocess all the data and CRCCode from AHT20 @@ -28,14 +52,13 @@ def AHT20_crc8_check(all_data_int, init_value=0xFF): # Preprocessing the first data (status) DATA_FROM_AHT20 = DATA_FROM_AHT20 | all_data_int[0] print(bin(DATA_FROM_AHT20)) - DATA_FROM_AHT20 = DATA_FROM_AHT20 ^ init_value - print(bin(DATA_FROM_AHT20)) - for i_data in range(1, N_DATA + N_CRC): - DATA_FROM_AHT20 = DATA_FROM_AHT20 << 8 | all_data_int[i_data] + for i_data in range(1, N_DATA): + DATA_FROM_AHT20 = (DATA_FROM_AHT20 << 8) | all_data_int[i_data] print(bin(DATA_FROM_AHT20)) - mod_value = DATA_FROM_AHT20 % CRC_DEVIDE_NUMBER + mod_value = mod2_division_8bits( + DATA_FROM_AHT20, CRC_DEVIDE_NUMBER, len(all_data_int) - 1, init_value) print(mod_value) - if (mod_value == 0): + if (mod_value == all_data_int[-1]): return True else: return False @@ -44,6 +67,21 @@ def AHT20_crc8_check(all_data_int, init_value=0xFF): def AHT20_crc8_calculate(all_data_int): pass + +def CRC8_check(all_data_int, init_value=0x00): + divider = 0x107 + DATA_FOR_CHECK = all_data_int[0] + for data in all_data_int[1:-1]: + DATA_FOR_CHECK = (DATA_FOR_CHECK << 8) | data + remainder = mod2_division_8bits( + DATA_FOR_CHECK, divider, len(all_data_int) - 1, init_value) + if (remainder == all_data_int[-1]): + return True + else: + return False + + if __name__ == "__main__": + print(CRC8_check([0x66, 0x44, 0x33, 0x22, 0x24], 0)) for data in TEST_DATA: - AHT20_crc8_check(data, INIT) \ No newline at end of file + print(AHT20_crc8_check(data, INIT)) From 00f717a7342478ff9cdf032bca9ccfdff35f3d20 Mon Sep 17 00:00:00 2001 From: Zifeng1997 <58804221+xzf89718@users.noreply.github.com> Date: Tue, 15 Nov 2022 14:53:13 +0800 Subject: [PATCH 3/3] Update crc8_helper.py --- AHT20_sensor_control/crc8_helper.py | 39 ++++++++++++++++------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/AHT20_sensor_control/crc8_helper.py b/AHT20_sensor_control/crc8_helper.py index 2f4bf1e..6543e66 100644 --- a/AHT20_sensor_control/crc8_helper.py +++ b/AHT20_sensor_control/crc8_helper.py @@ -1,4 +1,7 @@ -# A total of 6 * 8 bits data need to check +# Usage: AHT20 crc8 checker. +# A total of 6 * 8 bits data need to check. G(x) = x8 + x5 + x4 + 1 -> 0x131(0x31), Initial value = 0xFF. No XOROUT. +# Author: XU Zifeng. +# Email: zifeng.xu@foxmail.com N_DATA = 6 # 1 * 8 bits CRC N_CRC = 1 @@ -41,32 +44,34 @@ def mod2_division_8bits(a, b, number_of_bytes, init_value): return a -def AHT20_crc8_check(all_data_int, init_value=0xFF): +def AHT20_crc8_calculate(all_data_int): + init_value = INIT + # Preprocess all the data and CRCCode from AHT20 + data_from_AHT20 = 0x00 + # Preprocessing the first data (status) + # print(bin(data_from_AHT20)) + for i_data in range(0, len(all_data_int)): + data_from_AHT20 = (data_from_AHT20 << 8) | all_data_int[i_data] + # print(bin(data_from_AHT20)) + mod_value = mod2_division_8bits( + data_from_AHT20, CRC_DEVIDE_NUMBER, len(all_data_int), init_value) + # print(mod_value) + return mod_value + + +def AHT20_crc8_check(all_data_int): """ The input data shoule be: Status Humidity0 Humidity1 Humidity2|Temperature0 Temperature1 Temperature2 CRCCode. In python's int64. """ - # Preprocess all the data and CRCCode from AHT20 - DATA_FROM_AHT20 = 0x00 - # Preprocessing the first data (status) - DATA_FROM_AHT20 = DATA_FROM_AHT20 | all_data_int[0] - print(bin(DATA_FROM_AHT20)) - for i_data in range(1, N_DATA): - DATA_FROM_AHT20 = (DATA_FROM_AHT20 << 8) | all_data_int[i_data] - print(bin(DATA_FROM_AHT20)) - mod_value = mod2_division_8bits( - DATA_FROM_AHT20, CRC_DEVIDE_NUMBER, len(all_data_int) - 1, init_value) - print(mod_value) + mod_value = AHT20_crc8_calculate(all_data_int[:-1]) if (mod_value == all_data_int[-1]): return True else: return False -def AHT20_crc8_calculate(all_data_int): - pass - def CRC8_check(all_data_int, init_value=0x00): divider = 0x107 @@ -84,4 +89,4 @@ def CRC8_check(all_data_int, init_value=0x00): if __name__ == "__main__": print(CRC8_check([0x66, 0x44, 0x33, 0x22, 0x24], 0)) for data in TEST_DATA: - print(AHT20_crc8_check(data, INIT)) + print(AHT20_crc8_check(data))