/* * File: sps30.c * Description: Sensirion SPS30 sensor communication library * Author: David Zaitlik * Date: 2021-07-18 * * * Copyright (c) 2024 Veles Labs s.r.o. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. * */ #include "sps30.h" /* * Functions to be implemented by user */ /* I2C */ int8_t sps30_i2c_transmit(uint8_t address, uint8_t *buffer, int len) __attribute__((weak)); int8_t sps30_i2c_receive(uint8_t address, uint8_t *buffer, int len) __attribute__((weak)); /* CRC */ uint8_t sensirion_crc8_calculate(const uint8_t *data, uint16_t count) __attribute__((weak)); /* Interrupts */ int8_t sps30_disable_interrupts(void) __attribute__((weak)); int8_t sps30_enable_interrupts(void) __attribute__((weak)); /* delay function */ void delay_ms(int delay_ms) __attribute__((weak)); /* * Public functions */ int8_t sps30_send_cmd(sps30_cmd_t cmd) { uint8_t buffer[32]; uint8_t result; // start measurement buffer[0] = cmd >> 8; buffer[1] = cmd & 0x00ff; sps30_disable_interrupts(); result = sps30_i2c_transmit(SPS30_I2C_ADDRESS<<1, buffer, 2); sps30_enable_interrupts(); // TODO: Proc to vraci NACK? Vyresit. if (result != 0) { return SPS30_ERROR; } return SPS30_OK; } int8_t sps30_start_measurement( void ) { uint8_t buffer[5]; uint8_t result; buffer[0] = SPS30_START_MEASUREMENT >> 8; buffer[1] = SPS30_START_MEASUREMENT & 0x00ff; buffer[2] = SPS30_UINT16_FORMAT; buffer[3] = 0x00; buffer[4] = sensirion_crc8_calculate(buffer + 2, 2); sps30_disable_interrupts(); result = sps30_i2c_transmit(SPS30_I2C_ADDRESS<<1, buffer, 5); sps30_enable_interrupts(); if (result != 0) { return SPS30_ERROR; } return SPS30_OK; } int8_t sps30_stop_measurement( void ) { return sps30_send_cmd(SPS30_STOP_MEASUREMENT); } int8_t sps30_read_measured_values(sps30_data_t *measured_data) { uint8_t buffer[32]; uint8_t result; /* start measurement */ buffer[0] = SPS30_READ_MEASURED_VALUES >> 8; buffer[1] = SPS30_READ_MEASURED_VALUES & 0xFF; sps30_disable_interrupts(); result = sps30_i2c_transmit(SPS30_I2C_ADDRESS<<1, buffer, 2); sps30_enable_interrupts(); if (result != 0) { return SPS30_ERROR; } delay_ms(10); /* read out */ sps30_disable_interrupts(); result = sps30_i2c_receive(SPS30_I2C_ADDRESS<<1, buffer, 3 * SPS30_MEASURED_VALUES_COUNT); sps30_enable_interrupts(); if (result != 0) { return SPS30_ERROR; } /* check data integrity */ for (uint8_t i = 0; i < SPS30_MEASURED_VALUES_COUNT; i++) { uint8_t checksum_calculated = sensirion_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 */ /* mass concencration [ug / m^3] */ int pos = 0; for (int i = 0; i < 4; i++) { /* i + 1 because mass concentration starts at PM1.0 (there is no PM0.5) */ measured_data->mass_concentration[i + 1] = (buffer[pos] << 8) + buffer[pos + 1]; pos += 3; /* 2 B data, 1 B crc */ } /* number concentration [1 / cm^3] */ for (int i = 0; i < 5; i++) { measured_data->number_concentration[i] = (buffer[pos] << 8) + buffer[pos + 1]; pos += 3; } /* typical particle size [nm] */ measured_data->typical_particle_size = (buffer[pos] << 8) + buffer[pos + 1]; return SPS30_OK; } int8_t sps30_sleep( void ) { return sps30_send_cmd(SPS30_SLEEP); } int8_t sps30_wake_up( void ) { return sps30_send_cmd(SPS30_WAKE_UP); } int8_t sps30_start_fan_cleaning( void ) { return sps30_send_cmd(SPS30_START_FAN_CLEANING); } int8_t sps30_reset( void ) { return sps30_send_cmd(SPS30_RESET); } int8_t sps30_read_status_register ( void ) { uint8_t buffer[6]; uint8_t result; /* start measurement */ buffer[0] = SPS30_READ_DEVICE_STATUS_REGISTER >> 8; buffer[1] = SPS30_READ_DEVICE_STATUS_REGISTER & 0x00ff; sps30_disable_interrupts(); result = sps30_i2c_transmit(SPS30_I2C_ADDRESS<<1, buffer, 2); sps30_enable_interrupts(); if (result != 0) { return SPS30_ERROR; } delay_ms(10); /* read out */ sps30_disable_interrupts(); result = sps30_i2c_receive(SPS30_I2C_ADDRESS<<1, buffer, 6); sps30_enable_interrupts(); // TODO return SPS30_OK; } int8_t sps30_read_firmware_version ( uint8_t * fw_ver_hi, uint8_t * fw_ver_lo ) { uint8_t i2c_tx_buffer[2]; uint8_t i2c_rx_buffer[3]; uint8_t result; /* start measurement */ i2c_tx_buffer[0] = SPS30_READ_VERSION >> 8; i2c_tx_buffer[1] = SPS30_READ_VERSION & 0x00ff; sps30_disable_interrupts(); result = sps30_i2c_transmit(SPS30_I2C_ADDRESS<<1, i2c_tx_buffer, 2); sps30_enable_interrupts(); // TODO: Proc to vraci NACK? Vyresit. if (result != 0) { // return SPS30_ERROR; } delay_ms(1); /* read out */ sps30_disable_interrupts(); result = sps30_i2c_receive(SPS30_I2C_ADDRESS<<1, i2c_rx_buffer, 3); sps30_enable_interrupts(); if (result != 0) { // return SPS30_ERROR; } *fw_ver_hi = i2c_rx_buffer[0]; *fw_ver_lo = i2c_rx_buffer[1]; return SPS30_OK; }