WIP SPS30

This commit is contained in:
mj 2022-01-09 13:17:29 +01:00
parent 9cb63af5c6
commit 6ecc10c7e0
3 changed files with 43 additions and 41 deletions

View File

@ -14,8 +14,8 @@
* Definitions & macros * Definitions & macros
*/ */
#define CRC8_POLYNOMIAL 0x31 #define CRC8_POLYNOMIAL ((uint8_t)0x31)
#define CRC8_INIT 0xFF #define CRC8_INIT ((uint8_t)0xFF)
uint8_t crc8_calculate(const uint8_t *data, uint16_t count); uint8_t crc8_calculate(const uint8_t *data, uint16_t count);

View File

@ -48,13 +48,28 @@ typedef enum {
SPS30_READ_DEVICE_STATUS_REGISTER = 0xD206, SPS30_READ_DEVICE_STATUS_REGISTER = 0xD206,
SPS30_CLEAR_DEVICE_STATUS_REGISTER = 0xD210, SPS30_CLEAR_DEVICE_STATUS_REGISTER = 0xD210,
SPS30_RESET = 0xD304 SPS30_RESET = 0xD304
} sps30_cmd_t; } sps30_cmd_t;
typedef enum { typedef enum {
SPS30_FLOAT_FORMAT = 0x03, SPS30_FLOAT_FORMAT = 0x03,
SPS30_UINT16_FORMAT = 0x05 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); 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 ); 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_ */ #endif /* INC_SPS30_H_ */

View File

@ -39,7 +39,6 @@ int8_t sps30_start_measurement( void )
result = i2c_transmit(SPS30_I2C_ADDRESS<<1, i2c_tx_buffer, 5); result = i2c_transmit(SPS30_I2C_ADDRESS<<1, i2c_tx_buffer, 5);
// TODO: Proc to vraci NACK? Vyresit.
if (result != I2C_OK) { if (result != I2C_OK) {
return SPS30_ERROR; return SPS30_ERROR;
} }
@ -51,33 +50,23 @@ int8_t sps30_stop_measurement( void )
return sps30_send_cmd(SPS30_STOP_MEASUREMENT); 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)
{ {
uint8_t buffer[32];
if (measured_values_len != 10)
{
return -5;
}
uint8_t i2c_tx_buffer[2];
uint8_t i2c_rx_buffer[30];
uint8_t result; uint8_t result;
// start measurement // start measurement
i2c_tx_buffer[0] = SPS30_READ_MEASURED_VALUES >> 8; buffer[0] = SPS30_READ_MEASURED_VALUES >> 8;
i2c_tx_buffer[1] = SPS30_READ_MEASURED_VALUES & 0x00ff; buffer[1] = SPS30_READ_MEASURED_VALUES & 0xFF;
result = i2c_transmit(SPS30_I2C_ADDRESS<<1, i2c_tx_buffer, 2); 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_ERROR;
} }
return SPS30_OK;*/ LL_mDelay(10); // 10 ms should be enough
LL_mDelay(1); // 10 ms should be enough
// read out // 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) if (result != I2C_OK)
{ {
return SPS30_ERROR; 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; return SPS30_OK;
} }
//
uint8_t calculate_crc(uint8_t data[2]) //uint8_t calculate_crc(uint8_t data[2])
{ //{
uint8_t crc = 0xFF; // uint8_t crc = 0xFF;
for(uint8_t i = 0; i < 2; i++) { // for(uint8_t i = 0; i < 2; i++) {
crc ^= data[i]; // crc ^= data[i];
for(uint8_t bit = 8; bit > 0; --bit) { // for(uint8_t bit = 8; bit > 0; --bit) {
if(crc & 0x80) { // if(crc & 0x80) {
crc = (crc << 1) ^ 0x31u; // crc = (crc << 1) ^ 0x31u;
} else { // } else {
crc = (crc << 1); // crc = (crc << 1);
} // }
} // }
} // }
return crc; // return crc;
} //}