/* * sht30.c * * Created on: Nov 3, 2022 * Author: Administrator */ #include #include "i2c.h" #include "sht30.h" #define CONFIG_SHT30_DEBUG #ifdef CONGIF_SHT30_DEBUG #define sht30_print(format, args...) printf(format, ##args) #else #define sht30_print(format, args...) do{} while(0) #endif static int sht30_send_cmd(SHT30_CMD cmd) { uint8_t buf[2]; buf[0] = cmd >> 8; buf[1] = cmd & 0xFF; return HAL_I2C_Master_Transmit(&hi2c2, SHT30_ADDR_WR, (uint8_t*)buf, 2, 0xFFFF); } static void sht30_soft_reset(void) { sht30_send_cmd(SOFT_RESET_CMD); HAL_Delay(1); } static int sht30_single_shot_measurement(uint8_t *buf, uint8_t buf_size) { uint16_t cmd = HIGH_ENABLED_CMD; uint8_t rv; if( !buf || buf_size < SHT30_DATA_SIZE ) { sht30_print("%s(): Invalid input argument\n", __func__); return -1; } rv = sht30_send_cmd(cmd); if( rv ) { sht30_print("ERROR: HST30 send messurement command failure, rv = &d\n", rv); sht30_soft_reset(); return -2; } rv = HAL_I2C_Master_Receive(&hi2c2, SHT30_ADDR_RD, buf, SHT30_DATA_SIZE, 0xFFFF); if(rv) { sht30_print("ERROR: SHT30 read measurement result failure, rv = %d\n", rv); return -3; } return 0; } static uint8_t sht30_crc8(const uint8_t *data, int len) { const uint8_t POLYNOMIAL = 0x31; uint8_t crc = 0xFF; int i,j; for (i=0; i