WIP SHT CRC
This commit is contained in:
parent
e6e97a2547
commit
ef18028fe7
@ -50,6 +50,7 @@ int8_t i2c_receive(uint8_t address, uint8_t *buffer, int len)
|
|||||||
{
|
{
|
||||||
LL_I2C_HandleTransfer(i2c_context->i2c, address, LL_I2C_ADDRSLAVE_7BIT, len,
|
LL_I2C_HandleTransfer(i2c_context->i2c, address, LL_I2C_ADDRSLAVE_7BIT, len,
|
||||||
LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_READ);
|
LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_READ);
|
||||||
|
LL_I2C_ClearFlag_STOP(i2c_context->i2c);
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (!LL_I2C_IsActiveFlag_STOP(i2c_context->i2c)) {
|
while (!LL_I2C_IsActiveFlag_STOP(i2c_context->i2c)) {
|
||||||
if (LL_I2C_IsActiveFlag_RXNE(i2c_context->i2c)) {
|
if (LL_I2C_IsActiveFlag_RXNE(i2c_context->i2c)) {
|
||||||
|
@ -100,7 +100,6 @@ int8_t scd4x_read_measurement(uint16_t *co2, int16_t *temperature, uint16_t *rel
|
|||||||
uint8_t t_crc = buffer[5];
|
uint8_t t_crc = buffer[5];
|
||||||
uint32_t rh_ticks = (buffer[6] << 8) + buffer[7];
|
uint32_t rh_ticks = (buffer[6] << 8) + buffer[7];
|
||||||
uint8_t rh_crc = buffer[8];
|
uint8_t rh_crc = buffer[8];
|
||||||
|
|
||||||
/* check CRC-8 checksum */
|
/* check CRC-8 checksum */
|
||||||
uint8_t crc_correct = crc8_calculate(buffer, 2) == co2_crc;
|
uint8_t crc_correct = crc8_calculate(buffer, 2) == co2_crc;
|
||||||
crc_correct &= crc8_calculate(buffer + 3, 2) == t_crc;
|
crc_correct &= crc8_calculate(buffer + 3, 2) == t_crc;
|
||||||
|
@ -23,29 +23,35 @@ int8_t sht4x_measure(int16_t *temperature, uint16_t *relative_humidity)
|
|||||||
uint8_t buffer[32];
|
uint8_t buffer[32];
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
// disable interrupts to prevent modbus/i2c conflict
|
/* disable interrupts to prevent modbus/i2c conflict */
|
||||||
uart_disable_interrupts();
|
uart_disable_interrupts();
|
||||||
// start measurement
|
// start measurement
|
||||||
buffer[0] = SHT4X_START_MEAS_HIGH_PRECISION;
|
buffer[0] = SHT4X_START_MEAS_HIGH_PRECISION;
|
||||||
result = i2c_transmit(SHT4X_I2C_ADDRESS<<1, buffer, 1);
|
result = i2c_transmit(SHT4X_I2C_ADDRESS<<1, buffer, 1);
|
||||||
// TODO: Proc to vraci NACK? Vyresit.
|
|
||||||
/*
|
|
||||||
if (result != I2C_OK) {
|
if (result != I2C_OK) {
|
||||||
return SHT4X_ERROR;
|
return SHT4X_ERROR;
|
||||||
}*/
|
}
|
||||||
uart_enable_interrupts();
|
uart_enable_interrupts();
|
||||||
LL_mDelay(10); // 10 ms should be enough
|
LL_mDelay(10); /* 10 ms should be enough */
|
||||||
uart_disable_interrupts();
|
uart_disable_interrupts();
|
||||||
// read out
|
/* read out */
|
||||||
result = i2c_receive(SHT4X_I2C_ADDRESS<<1, buffer, 6);
|
result = i2c_receive(SHT4X_I2C_ADDRESS<<1, buffer, 6);
|
||||||
uart_enable_interrupts();
|
uart_enable_interrupts();
|
||||||
if (result != I2C_OK) {
|
if (result != I2C_OK) {
|
||||||
return SHT4X_ERROR;
|
return SHT4X_ERROR;
|
||||||
}
|
}
|
||||||
// TODO checksum
|
/* Convert to T and RH; taken directly from pseudocode in SHT4x datasheet, page 3 */
|
||||||
// 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 t_ticks = (buffer[0] << 8) + buffer[1];
|
||||||
|
uint8_t t_crc = buffer[2];
|
||||||
uint32_t rh_ticks = (buffer[3] << 8) + buffer[4];
|
uint32_t rh_ticks = (buffer[3] << 8) + buffer[4];
|
||||||
|
uint8_t rh_crc = buffer[5];
|
||||||
|
/* check CRC-8 checksum */
|
||||||
|
uint8_t crc_correct = crc8_calculate(buffer, 2) == t_crc;
|
||||||
|
crc_correct &= crc8_calculate(buffer + 3, 2) == rh_crc;
|
||||||
|
if (!crc_correct) {
|
||||||
|
return SHT4X_CRC8_ERROR;
|
||||||
|
}
|
||||||
|
/* copy data to output variables */
|
||||||
int t_degC = -450 + 10 * 175 * t_ticks / 65535; /* temperature * 10 */
|
int t_degC = -450 + 10 * 175 * t_ticks / 65535; /* temperature * 10 */
|
||||||
int rh_pRH = -6 + 125 * rh_ticks / 65535;
|
int rh_pRH = -6 + 125 * rh_ticks / 65535;
|
||||||
if (rh_pRH > 100) {
|
if (rh_pRH > 100) {
|
||||||
|
Loading…
Reference in New Issue
Block a user