Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
sps30.h
00001 /******************************************************************************* 00002 * Copyright (c) 2018-2019, Sensors and IoT Capability Centre (SIOT) at GovTech. 00003 * 00004 * Contributor(s): 00005 * Yap Zi Qi yap_zi_qi@tech.gov.sg 00006 *******************************************************************************/ 00007 #include "src/SensorDriver/sensor_driver.h" 00008 00009 #ifndef SPS30_H 00010 #define SPS30_H 00011 00012 #define SPS30_I2C_ADDR 0xd2 // left shifted by 1 bit from 0x69 00013 00014 #define SPS30_CMMD_STRT_MEAS 0x0010 00015 #define SPS30_CMMD_STOP_MEAS 0x0104 00016 #define SPS30_CMMD_GET_READY_STAT 0x0202 00017 #define SPS30_CMMD_READ_MEAS 0x0300 00018 00019 #define SPS30_CMMD_AUTO_CLEAN_INTV 0x8004 00020 #define SPS30_CMMD_START_FAN_CLEAN 0x5607 00021 00022 #define SPS30_CMMD_SOFT_RESET 0xD304 00023 00024 #define SPS30_CMMD_READ_SERIALNBR 0xD033 00025 #define SPS30_CMMD_READ_ARTICLECODE 0xD025 00026 00027 #define SPS30_STRT_MEAS_WRITE_DATA 0x0300 00028 00029 #define SPS30_POLYNOMIAL 0x31 // P(x) = x^8 + x^5 + x^4 + 1 = 100110p01 00030 #define SPS30_CRC_INIT 0xff 00031 00032 #define SPS30_SN_SIZE 33 // size of the s/n ascii string + CRC values 00033 00034 #define MASS_MAX 1000.00f 00035 #define MASS_MIN 0.00f 00036 #define NUM_MAX 3000.00f 00037 #define NUM_MIN 0.00f 00038 00039 #define I2C_FREQUENCY_STD 100000 // SPS30 uses 100MHz for I2C communication 00040 #define TIMEOUT_COUNT 3 00041 00042 /** Create SPS30 controller class 00043 * @brief inherits SensorDriver members and methods essential for Sensor Thread 00044 * 00045 * @param sps30 class 00046 * 00047 */ 00048 class Sps30: public SensorDriver { 00049 00050 public: 00051 uint16_t sps_ready; /**< 1 = ready, 0 = busy */ 00052 uint32_t clean_interval_i; /** 32 unsigned bit in seconds */ 00053 00054 /** Create a SPS30 object using the specified I2C object 00055 * @param sda - mbed I2C interface pin 00056 * @param scl - mbed I2C interface pin 00057 * @param I2C Frequency (in Hz) 00058 * 00059 * @return none 00060 */ 00061 Sps30(PinName sda, PinName scl, int i2c_frequency); 00062 00063 /** Destructor 00064 * 00065 * @param --none-- 00066 * 00067 * @return none 00068 */ 00069 ~Sps30(); 00070 00071 00072 /** Initialise SPS30 00073 * 00074 * @param --none-- 00075 * 00076 * @return enum SPSStatus 00077 */ 00078 void InitSensor(); 00079 00080 /** Poll SPS30 Data 00081 * 00082 * @param --none-- 00083 * 00084 * @return enum SPSStatus 00085 */ 00086 int PollSensor(); 00087 00088 private: 00089 00090 enum SPSError { 00091 SPSNOERROR, //all ok 00092 SPSISREADY, //ready status register 00093 SPSNOACKERROR, //no I2C ACK error 00094 SPSCRCERROR, //CRC error, any 00095 }; 00096 00097 char i2cbuff[60]; 00098 00099 bool sps_status; /**< status bit */ 00100 int t_count; /**< timeout counter */ 00101 uint8_t sn[33]; /**< ASCII Serial Number */ 00102 00103 std::vector<float> sensor_float_data; /** Vector of sensor float data **/ 00104 std::vector<std::string> sensor_data_name; /** Private list of sensor data types **/ 00105 00106 uint16_t clean_interval_m; /**< High order 16 bit word of Auto Clean Interval */ 00107 uint16_t clean_interval_l; /**< High order 16 bit word of Auto Clean Interval */ 00108 00109 uint16_t mass_1p0_m; /**< High order 16 bit word of Mass Conc of PM1.0 */ 00110 uint16_t mass_1p0_l; /**< Low order 16 bit word of Mass Conc of PM1.0 */ 00111 uint16_t mass_2p5_m; /**< High order 16 bit word of Mass Conc of PM2.5 */ 00112 uint16_t mass_2p5_l; /**< Low order 16 bit word of Mass Conc of PM2.5 */ 00113 uint16_t mass_4p0_m; /**< High order 16 bit word of Mass Conc of PM4.0 */ 00114 uint16_t mass_4p0_l; /**< Low order 16 bit word of Mass Conc of PM4.0 */ 00115 uint16_t mass_10p0_m; /**< High order 16 bit word of Mass Conc of PM10 */ 00116 uint16_t mass_10p0_l; /**< Low order 16 bit word of Mass Conc of PM10 */ 00117 00118 uint16_t num_0p5_m; /**< High order 16 bit word of Number Conc of PM0.5 */ 00119 uint16_t num_0p5_l; /**< Low order 16 bit word of Number Conc of PM0.5 */ 00120 uint16_t num_1p0_m; /**< High order 16 bit word of Number Conc of PM1.0 */ 00121 uint16_t num_1p0_l; /**< Low order 16 bit word of Number Conc of PM1.0 */ 00122 uint16_t num_2p5_m; /**< High order 16 bit word of Number Conc of PM2.5 */ 00123 uint16_t num_2p5_l; /**< Low order 16 bit word of Number Conc of PM2.5 */ 00124 uint16_t num_4p0_m; /**< High order 16 bit word of Number Conc of PM4.0 */ 00125 uint16_t num_4p0_l; /**< Low order 16 bit word of Number Conc of PM4.0 */ 00126 uint16_t num_10p0_m; /**< High order 16 bit word of Number Conc of PM10 */ 00127 uint16_t num_10p0_l; /**< Low order 16 bit word of Number Conc of PM10 */ 00128 00129 uint16_t typ_pm_size_m; /**< High order 16 bit word of Typical Particle Size */ 00130 uint16_t typ_pm_size_l; /**< Low order 16 bit word of Typical Particle Size */ 00131 00132 uint32_t mass_1p0_i; /**< 32 bit int of Mass Conc of PM1.0 */ 00133 uint32_t mass_2p5_i; /**< 32 bit int of Mass Conc of PM2.5 */ 00134 uint32_t mass_4p0_i; /**< 32 bit int of Mass Conc of PM4.0 */ 00135 uint32_t mass_10p0_i; /**< 32 bit int of Mass Conc of PM10 */ 00136 00137 uint32_t num_0p5_i; /**< 32 bit int of Number Conc of PM0.5 */ 00138 uint32_t num_1p0_i; /**< 32 bit int of Number Conc of PM1.0 */ 00139 uint32_t num_2p5_i; /**< 32 bit int of Number Conc of PM2.5 */ 00140 uint32_t num_4p0_i; /**< 32 bit int of Number Conc of PM4.0 */ 00141 uint32_t num_10p0_i; /**< 32 bit int of Number Conc of PM10 */ 00142 00143 uint32_t typ_pm_size_i; /**< 32 bit int of Typical Particle Size */ 00144 00145 float mass_1p0_f; /**< float of Mass Conc of PM1.0 */ 00146 float mass_2p5_f; /**< float of Mass Conc of PM2.5 */ 00147 float mass_4p0_f; /**< float of Mass Conc of PM4.0 */ 00148 float mass_10p0_f; /**< float of Mass Conc of PM10 */ 00149 00150 float num_0p5_f; /**< float of Number Conc of PM0.5 */ 00151 float num_1p0_f; /**< float of Number Conc of PM1.0 */ 00152 float num_2p5_f; /**< float of Number Conc of PM2.5 */ 00153 float num_4p0_f; /**< float of Number Conc of PM4.0 */ 00154 float num_10p0_f; /**< float of Number Conc of PM10 */ 00155 00156 float typ_pm_size_f; /**< float of Typical Particle Size */ 00157 00158 /** Start Auto-Measurement 00159 * 00160 * @param --none-- 00161 * 00162 * @return enum SPSerror 00163 */ 00164 uint8_t StartMeasurement(); 00165 00166 /** Stop Auto-Measurement 00167 * 00168 * @param --none-- 00169 * 00170 * @return enum SPSerror 00171 */ 00172 uint8_t StopMeasurement(); 00173 00174 /** Get Serial Number 00175 * 00176 * @param --none-- 00177 * @see ASCII Serial Number as sn[33] 00178 * 00179 * @return enum SPSerror 00180 */ 00181 int GetSerialNumber(); 00182 00183 /** Get Ready Status register 00184 * 00185 * @param --none-- 00186 * @see Ready Status result 00187 * 00188 * @return enum SPSerror 00189 */ 00190 uint8_t GetReadyStatus(); 00191 00192 /** Get all particulate matter parameters 00193 * 00194 * @param --none- 00195 * @see Results in Public member variables 00196 * 00197 * @return enum SPSerror 00198 */ 00199 uint8_t ReadMeasurement(); 00200 00201 00202 /** Calculate the SPS30 CRC value 00203 * 00204 * @param 16 bit value to perform a CRC check on 00205 * 00206 * @return 8 bit CRC value 00207 */ 00208 uint8_t CalcCrc2b(uint16_t seed); 00209 00210 /** Compare received CRC value with calculated CRC value 00211 * 00212 * @param 16 bit value to perform a CRC check on 00213 * @param 8 bit value to compare CRC values 00214 * 00215 * @return enum SPSerror 00216 */ 00217 uint8_t CheckCrc2b(uint16_t seed, uint8_t crc_in); 00218 00219 00220 /** Read Auto Cleaning Interval on the SPS30 00221 * 00222 * @param --none-- 00223 * 00224 * @return enum SPSerror 00225 */ 00226 uint8_t ReadAutoCleanInterval(); 00227 00228 /** Set Auto Cleaning Interval on the SPS30 00229 * 00230 * @param Auto Cleaning Interval in seconds 00231 * default is 604800s = 1 week, 0 to disable auto clean 00232 * 00233 * @return enum SPSerror 00234 */ 00235 uint8_t SetAutoCleanInterval(uint32_t set_interval = 604800); 00236 00237 00238 /** Perform manual Fan Cleaning 00239 * 00240 * @param --none-- 00241 * 00242 * @return enum SPSerror 00243 */ 00244 uint8_t StartFanClean(); 00245 00246 00247 /** Perform a soft reset 00248 * 00249 * @param --none-- 00250 * 00251 * @return enum SPSerror 00252 */ 00253 uint8_t SoftReset(); 00254 00255 /** Create a Sensor Data Array to be read from Sensor Thread 00256 * 00257 * @param --none-- 00258 * @see sensor_data vector pair in SensorDriver 00259 * 00260 * @return none 00261 */ 00262 void GetDataArray(); 00263 00264 protected: 00265 I2C _i2c; 00266 00267 }; 00268 #endif 00269
Generated on Sun Jul 31 2022 16:29:41 by
1.7.2