/********************************************************************** * Copyright: (C)2023 LingYun IoT System Studio * Author: GuoWenxue QQ: 281143292 * Description: ISKBoard SHT20 R&H sensor Hardware Abstract Layer driver * * ChangeLog: * Version Date Author Description * V1.0.0 2023.04.3 GuoWenxue Release initial version ***********************************************************************/ #include #include #include #define delay_ms(x) HAL_Delay(x) enum { TRIG_T_MEASUREMENT_HM = 0xE3, // command trig. temp meas. hold master TRIG_RH_MEASUREMENT_HM = 0xE5, // command trig. humidity meas. hold master TRIG_T_MEASUREMENT_POLL = 0xF3, // command trig. temp meas. no hold master TRIG_RH_MEASUREMENT_POLL = 0xF5, // command trig. humidity meas. no hold master USER_REG_W = 0xE6, // command writing user register USER_REG_R = 0xE7, // command reading user register SOFT_RESET = 0xFE // command soft reset }; //#define CONFIG_SHT2X_DEBUG #ifdef CONFIG_SHT2X_DEBUG static inline void dump_buf(uint8_t *buf, uint32_t size) { int i; if(!buf) return ; for(i=0; i0; --bit) { if (crc & 0x80) crc = (crc << 1) ^ 0x0131; //POLYNOMIAL; else crc = (crc << 1); } } if (crc != checksum) { return -1; } else { return 0; } } static int sht2x_measure_value(uint8_t command, uint16_t *val) { uint8_t buf[3]; /* I2C������buffer */ int count=4; /* �ܹ����Ĵ��� */ if( !val ) { sht2x_print("SHT2X invalid input arguments\r\n"); return -1; } if(TRIG_T_MEASUREMENT_POLL!=command && TRIG_RH_MEASUREMENT_POLL !=command ) { sht2x_print("SHT2X unsupport command: 0x%0x\r\n", command); return -2; } if( NO_ERROR != i2c_write(SHT2X_I2CBUS, &command, 1) ) { sht2x_print("SHT2X send measure command 0x%0x failure\r\n", command); return -3; } if(TRIG_T_MEASUREMENT_POLL == command) delay_ms(85); else delay_ms(29); while(count--) { memset(buf, 0, 3); if( !i2c_read(SHT2X_I2CBUS, buf, 3) ) { break; } delay_ms(5); } if( sht2x_checkcrc(buf, 2, buf[2])< 0 ) { #ifdef CONFIG_SHT2X_DEBUG sht2x_print("Measurement data checksum failure:\r\n"); dump_buf(buf, 3); #endif return -4; } if(TRIG_T_MEASUREMENT_POLL == command) *val = buf[0]<<8|(buf[1]&0xFC); //14bits(1111 1100) else *val = buf[0]<<8|(buf[1]&0xFF); //12bits(1100 0000) sht2x_print("Measurement temperature value: 0x%04x\r\n", *val); return 0; }