From 6ecc10c7e09b5c6659d417067e8241789e1b1fc0 Mon Sep 17 00:00:00 2001 From: mj Date: Sun, 9 Jan 2022 13:17:29 +0100 Subject: [PATCH] WIP SPS30 --- fw/Core/Inc/crc8.h | 4 +-- fw/Core/Inc/sps30.h | 21 +++++++++++++--- fw/Core/Src/sps30.c | 59 ++++++++++++++++++--------------------------- 3 files changed, 43 insertions(+), 41 deletions(-) diff --git a/fw/Core/Inc/crc8.h b/fw/Core/Inc/crc8.h index e799f7e..6820352 100644 --- a/fw/Core/Inc/crc8.h +++ b/fw/Core/Inc/crc8.h @@ -14,8 +14,8 @@ * Definitions & macros */ -#define CRC8_POLYNOMIAL 0x31 -#define CRC8_INIT 0xFF +#define CRC8_POLYNOMIAL ((uint8_t)0x31) +#define CRC8_INIT ((uint8_t)0xFF) uint8_t crc8_calculate(const uint8_t *data, uint16_t count); diff --git a/fw/Core/Inc/sps30.h b/fw/Core/Inc/sps30.h index 9ca3c45..2f6d3fb 100644 --- a/fw/Core/Inc/sps30.h +++ b/fw/Core/Inc/sps30.h @@ -48,13 +48,28 @@ typedef enum { SPS30_READ_DEVICE_STATUS_REGISTER = 0xD206, SPS30_CLEAR_DEVICE_STATUS_REGISTER = 0xD210, SPS30_RESET = 0xD304 - } sps30_cmd_t; typedef enum { SPS30_FLOAT_FORMAT = 0x03, SPS30_UINT16_FORMAT = 0x05 -} sps30_data_format; +} sps30_data_format_t; + +typedef enum { + PM0_5 = 0, /* this category is used only for number concentration */ + PM1_0, + PM2_5, + PM4_0, + PM10_0, + SPS30_PM_CATEGORIES_COUNT +} sps30_pm_categories_t; + +typedef struct { + /* PM0.5 is skipped for mass concentration */ + uint16_t mass_concentration[SPS30_PM_CATEGORIES_COUNT]; /* ug / m^3 */ + uint16_t number_concentration[SPS30_PM_CATEGORIES_COUNT]; /* 1 / cm^3 */ + uint16_t typical_particle_size; /* nm */ +} sps30_data_t; int8_t sps30_send_cmd(sps30_cmd_t cmd); @@ -73,6 +88,4 @@ int8_t sps30_read_status_register ( void ); int8_t sps30_read_firmware_version ( uint8_t * fw_ver_hi, uint8_t * fw_ver_lo ); -uint8_t calculate_crc(uint8_t data[2]); - #endif /* INC_SPS30_H_ */ diff --git a/fw/Core/Src/sps30.c b/fw/Core/Src/sps30.c index c05a273..08e0282 100644 --- a/fw/Core/Src/sps30.c +++ b/fw/Core/Src/sps30.c @@ -39,7 +39,6 @@ int8_t sps30_start_measurement( void ) result = i2c_transmit(SPS30_I2C_ADDRESS<<1, i2c_tx_buffer, 5); - // TODO: Proc to vraci NACK? Vyresit. if (result != I2C_OK) { return SPS30_ERROR; } @@ -51,33 +50,23 @@ int8_t sps30_stop_measurement( void ) return sps30_send_cmd(SPS30_STOP_MEASUREMENT); } -int8_t sps30_read_measured_values(uint16_t *measured_values, uint8_t measured_values_len) +int8_t sps30_read_measured_values(sps30_data_t *measured_data) { - - if (measured_values_len != 10) - { - return -5; - } - - uint8_t i2c_tx_buffer[2]; - uint8_t i2c_rx_buffer[30]; + uint8_t buffer[32]; uint8_t result; // start measurement - i2c_tx_buffer[0] = SPS30_READ_MEASURED_VALUES >> 8; - i2c_tx_buffer[1] = SPS30_READ_MEASURED_VALUES & 0x00ff; - result = i2c_transmit(SPS30_I2C_ADDRESS<<1, i2c_tx_buffer, 2); + buffer[0] = SPS30_READ_MEASURED_VALUES >> 8; + buffer[1] = SPS30_READ_MEASURED_VALUES & 0xFF; + result = i2c_transmit(SPS30_I2C_ADDRESS<<1, buffer, 2); - // TODO: Proc to vraci NACK? Vyresit. - /*if (result != I2C_OK) { + if (result != I2C_OK) { return SPS30_ERROR; } - return SPS30_OK;*/ - - LL_mDelay(1); // 10 ms should be enough + LL_mDelay(10); // 10 ms should be enough // read out - result = i2c_receive(SPS30_I2C_ADDRESS<<1, i2c_rx_buffer, 30); + result = i2c_receive(SPS30_I2C_ADDRESS<<1, buffer, 30); if (result != I2C_OK) { return SPS30_ERROR; @@ -175,19 +164,19 @@ int8_t sps30_read_firmware_version ( uint8_t * fw_ver_hi, uint8_t * fw_ver_lo ) return SPS30_OK; } - -uint8_t calculate_crc(uint8_t data[2]) -{ - uint8_t crc = 0xFF; - for(uint8_t i = 0; i < 2; i++) { - crc ^= data[i]; - for(uint8_t bit = 8; bit > 0; --bit) { - if(crc & 0x80) { - crc = (crc << 1) ^ 0x31u; - } else { - crc = (crc << 1); - } - } - } - return crc; -} +// +//uint8_t calculate_crc(uint8_t data[2]) +//{ +// uint8_t crc = 0xFF; +// for(uint8_t i = 0; i < 2; i++) { +// crc ^= data[i]; +// for(uint8_t bit = 8; bit > 0; --bit) { +// if(crc & 0x80) { +// crc = (crc << 1) ^ 0x31u; +// } else { +// crc = (crc << 1); +// } +// } +// } +// return crc; +//}