From e10e7f5c31147fbc3761b7544a39f2ff41ec027b Mon Sep 17 00:00:00 2001 From: mj Date: Sun, 9 Jan 2022 13:47:13 +0100 Subject: [PATCH] WIP SPS30 --- fw/Core/Inc/sps30.h | 1 + fw/Core/Src/sps30.c | 38 ++++++++++++++++++-------------------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/fw/Core/Inc/sps30.h b/fw/Core/Inc/sps30.h index 2f6d3fb..819dd5b 100644 --- a/fw/Core/Inc/sps30.h +++ b/fw/Core/Inc/sps30.h @@ -19,6 +19,7 @@ */ #define SPS30_I2C_ADDRESS 0x69 +#define SPS30_MEASURED_VALUES_COUNT 10 /* * Return values diff --git a/fw/Core/Src/sps30.c b/fw/Core/Src/sps30.c index 08e0282..52826dd 100644 --- a/fw/Core/Src/sps30.c +++ b/fw/Core/Src/sps30.c @@ -26,18 +26,16 @@ int8_t sps30_send_cmd(sps30_cmd_t cmd) int8_t sps30_start_measurement( void ) { - uint8_t i2c_tx_buffer[5]; - uint8_t data_for_crc = {SPS30_UINT16_FORMAT, 0x00}; - + uint8_t buffer[5]; uint8_t result; - i2c_tx_buffer[0] = SPS30_START_MEASUREMENT >> 8; - i2c_tx_buffer[1] = SPS30_START_MEASUREMENT & 0x00ff; - i2c_tx_buffer[2] = SPS30_UINT16_FORMAT; - i2c_tx_buffer[3] = 0x00; - i2c_tx_buffer[4] = calculate_crc(data_for_crc); + buffer[0] = SPS30_START_MEASUREMENT >> 8; + buffer[1] = SPS30_START_MEASUREMENT & 0x00ff; + buffer[2] = SPS30_UINT16_FORMAT; + buffer[3] = 0x00; + buffer[4] = crc8_calculate(buffer + 2, 2); - result = i2c_transmit(SPS30_I2C_ADDRESS<<1, i2c_tx_buffer, 5); + result = i2c_transmit(SPS30_I2C_ADDRESS<<1, buffer, 5); if (result != I2C_OK) { return SPS30_ERROR; @@ -60,28 +58,28 @@ int8_t sps30_read_measured_values(sps30_data_t *measured_data) buffer[0] = SPS30_READ_MEASURED_VALUES >> 8; buffer[1] = SPS30_READ_MEASURED_VALUES & 0xFF; result = i2c_transmit(SPS30_I2C_ADDRESS<<1, buffer, 2); - if (result != I2C_OK) { return SPS30_ERROR; } LL_mDelay(10); // 10 ms should be enough // read out - result = i2c_receive(SPS30_I2C_ADDRESS<<1, buffer, 30); + result = i2c_receive(SPS30_I2C_ADDRESS<<1, buffer, 3 * SPS30_MEASURED_VALUES_COUNT); if (result != I2C_OK) { return SPS30_ERROR; } - - uint8_t checksums[10]; - - uint8_t j = 0; - for (uint8_t i = 0; i < 10; i++) + /* check data integrity */ + for (uint8_t i = 0; i < SPS30_MEASURED_VALUES_COUNT; i++) { - - measured_values[i] = (i2c_rx_buffer[j++] << 8) + i2c_rx_buffer[j++]; - checksums[i] = i2c_rx_buffer[j++]; +// measured_values[i] = (i2c_rx_buffer[j++] << 8) + i2c_rx_buffer[j++]; +// checksums[i] = i2c_rx_buffer[j++]; + uint8_t checksum_calculated = crc8_calculate(buffer + 3*i, 2); + uint8_t checksum_received = buffer[3*i + 2]; + if (checksum_calculated != checksum_received) { + return SPS30_CRC8_ERROR; + } } - + /* copy to output struct */ return SPS30_OK; }