WIP SPS30
This commit is contained in:
parent
6ecc10c7e0
commit
e10e7f5c31
@ -19,6 +19,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#define SPS30_I2C_ADDRESS 0x69
|
#define SPS30_I2C_ADDRESS 0x69
|
||||||
|
#define SPS30_MEASURED_VALUES_COUNT 10
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Return values
|
* Return values
|
||||||
|
@ -26,18 +26,16 @@ int8_t sps30_send_cmd(sps30_cmd_t cmd)
|
|||||||
|
|
||||||
int8_t sps30_start_measurement( void )
|
int8_t sps30_start_measurement( void )
|
||||||
{
|
{
|
||||||
uint8_t i2c_tx_buffer[5];
|
uint8_t buffer[5];
|
||||||
uint8_t data_for_crc = {SPS30_UINT16_FORMAT, 0x00};
|
|
||||||
|
|
||||||
uint8_t result;
|
uint8_t result;
|
||||||
|
|
||||||
i2c_tx_buffer[0] = SPS30_START_MEASUREMENT >> 8;
|
buffer[0] = SPS30_START_MEASUREMENT >> 8;
|
||||||
i2c_tx_buffer[1] = SPS30_START_MEASUREMENT & 0x00ff;
|
buffer[1] = SPS30_START_MEASUREMENT & 0x00ff;
|
||||||
i2c_tx_buffer[2] = SPS30_UINT16_FORMAT;
|
buffer[2] = SPS30_UINT16_FORMAT;
|
||||||
i2c_tx_buffer[3] = 0x00;
|
buffer[3] = 0x00;
|
||||||
i2c_tx_buffer[4] = calculate_crc(data_for_crc);
|
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) {
|
if (result != I2C_OK) {
|
||||||
return SPS30_ERROR;
|
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[0] = SPS30_READ_MEASURED_VALUES >> 8;
|
||||||
buffer[1] = SPS30_READ_MEASURED_VALUES & 0xFF;
|
buffer[1] = SPS30_READ_MEASURED_VALUES & 0xFF;
|
||||||
result = i2c_transmit(SPS30_I2C_ADDRESS<<1, buffer, 2);
|
result = i2c_transmit(SPS30_I2C_ADDRESS<<1, buffer, 2);
|
||||||
|
|
||||||
if (result != I2C_OK) {
|
if (result != I2C_OK) {
|
||||||
return SPS30_ERROR;
|
return SPS30_ERROR;
|
||||||
}
|
}
|
||||||
LL_mDelay(10); // 10 ms should be enough
|
LL_mDelay(10); // 10 ms should be enough
|
||||||
// read out
|
// 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)
|
if (result != I2C_OK)
|
||||||
{
|
{
|
||||||
return SPS30_ERROR;
|
return SPS30_ERROR;
|
||||||
}
|
}
|
||||||
|
/* check data integrity */
|
||||||
uint8_t checksums[10];
|
for (uint8_t i = 0; i < SPS30_MEASURED_VALUES_COUNT; i++)
|
||||||
|
|
||||||
uint8_t j = 0;
|
|
||||||
for (uint8_t i = 0; i < 10; i++)
|
|
||||||
{
|
{
|
||||||
|
// measured_values[i] = (i2c_rx_buffer[j++] << 8) + i2c_rx_buffer[j++];
|
||||||
measured_values[i] = (i2c_rx_buffer[j++] << 8) + i2c_rx_buffer[j++];
|
// checksums[i] = 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;
|
return SPS30_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user