From f24dd4a4d8be303095b5f31feb8eb17b29f3798f Mon Sep 17 00:00:00 2001 From: dooku Date: Sun, 18 Jul 2021 11:04:08 +0200 Subject: [PATCH] Fixed missing SHT4x files --- fw/Core/Inc/sht4x.h | 57 +++++++++++++++++++++++++++++++++++++++++++++ fw/Core/Src/sht4x.c | 53 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 fw/Core/Inc/sht4x.h create mode 100644 fw/Core/Src/sht4x.c diff --git a/fw/Core/Inc/sht4x.h b/fw/Core/Inc/sht4x.h new file mode 100644 index 0000000..a33e6b2 --- /dev/null +++ b/fw/Core/Inc/sht4x.h @@ -0,0 +1,57 @@ +/* + * sht4x.h + * + * Created on: Jun 8, 2021 + * Author: user + */ + +#ifndef INC_SHT4X_H_ +#define INC_SHT4X_H_ + +#include "stdint.h" +#include "stm32l0xx_ll_i2c.h" +#include "stm32l0xx_ll_utils.h" +#include "i2c.h" +#include "crc8.h" + +/* + * Defines & macros + */ + +#define SHT4X_I2C_ADDRESS 0x44 + +/* + * Return values + */ + +#define SHT4X_OK 0 +#define SHT4X_ERROR -1 // generic error +#define SHT4X_CRC8_ERROR -2 // checksum failed + +/* + * Data types + */ + +typedef enum { + START_MEAS_HIGH_PRECISION = 0xFD, + START_MEAS_MEDIUM_PRECISION = 0xF6, + START_MEAS_LOW_PRECISION = 0xE0, + READ_SERIAL = 0x89, + SOFT_RESET = 0x94, + HEATER_200_mW_1_s = 0x39, + HEATER_200_mW_01_s = 0x32, + HEATER_110_mW_1_s = 0x2F, + HEATER_110_mW_01_s = 0x24, + HEATER_20_mW_1_s = 0x1E, + HEATER_20_mW_01_s = 0x15 +} sht4x_cmd_t; + +/* + * Function prototypes + */ + +int8_t sht4x_send_cmd(sht4x_cmd_t cmd); +int8_t sht4x_read_data(uint8_t *buffer, int len); +int8_t sht4x_measure(int *temperature, int *relative_humidity); + +#endif /* INC_SHT4X_H_ */ diff --git a/fw/Core/Src/sht4x.c b/fw/Core/Src/sht4x.c new file mode 100644 index 0000000..31923f9 --- /dev/null +++ b/fw/Core/Src/sht4x.c @@ -0,0 +1,53 @@ +/* + * sht4x.c + * + * Created on: Jun 8, 2021 + * Author: user + */ + +#include "sht4x.h" + +int8_t sht4x_send_cmd(sht4x_cmd_t cmd) +{ + +} + +int8_t sht4x_read_data(uint8_t *buffer, int len) +{ + +} + +int8_t sht4x_measure(int *temperature, int *relative_humidity) +{ + uint8_t buffer[32]; + int result; + + // start measurement + buffer[0] = START_MEAS_HIGH_PRECISION; + result = i2c_transmit(SHT4X_I2C_ADDRESS<<1, buffer, 1); + if (result != I2C_OK) { + return SHT4X_ERROR; + } + LL_mDelay(100); // 10 ms should be enough + // read out + result = i2c_receive(SHT4X_I2C_ADDRESS<<1, buffer, 6); + if (result != I2C_OK) { + return SHT4X_ERROR; + } + // TODO checksum + // Convert to T and RH; taken directly from pseudocode in SHT4x datasheet, page 3 + uint32_t t_ticks = (buffer[0] << 8) + buffer[1]; + uint32_t rh_ticks = (buffer[3] << 8) + buffer[4]; + int t_degC = -45 + 175 * t_ticks / 65535; + int rh_pRH = -6 + 125 * rh_ticks / 65535; + if (rh_pRH > 100) { + rh_pRH = 100; + } + if (rh_pRH < 0) { + rh_pRH = 0; + } + *temperature = t_degC; + *relative_humidity = rh_pRH; + + return SHT4X_OK; +}