Fixed missing SHT4x files

This commit is contained in:
dooku 2021-07-18 11:04:08 +02:00
parent cc3d7e94f2
commit f24dd4a4d8
2 changed files with 110 additions and 0 deletions

57
fw/Core/Inc/sht4x.h Normal file
View File

@ -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_ */

53
fw/Core/Src/sht4x.c Normal file
View File

@ -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;
}