Mbed library for SPS30 i2c communication (Configured to be interfaced with TresidderOS) Inherits members and methods from SensorDriver class

Dependents:   SPS30_TEST

Committer:
ziqiyap
Date:
Thu Mar 28 01:14:03 2019 +0000
Revision:
10:228c926a2416
Parent:
9:a5fe43e183e2
added test cases; added timeout counter for data not ready

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ziqiyap 3:adeb61dd9ceb 1 /*******************************************************************************
ziqiyap 3:adeb61dd9ceb 2 * Copyright (c) 2018-2019, Sensors and IoT Capability Centre (SIOT) at GovTech.
ziqiyap 3:adeb61dd9ceb 3 *
ziqiyap 3:adeb61dd9ceb 4 * Contributor(s):
ziqiyap 3:adeb61dd9ceb 5 * Yap Zi Qi yap_zi_qi@tech.gov.sg
ziqiyap 3:adeb61dd9ceb 6 *******************************************************************************/
ziqiyap 9:a5fe43e183e2 7 #include "src/SensorDriver/sensor_driver.h"
ziqiyap 3:adeb61dd9ceb 8
ziqiyap 0:9221dac25d3b 9 #ifndef SPS30_H
ziqiyap 0:9221dac25d3b 10 #define SPS30_H
ziqiyap 0:9221dac25d3b 11
ziqiyap 2:549bee9a4cd0 12 #define SPS30_I2C_ADDR 0xd2 // left shifted by 1 bit from 0x69
ziqiyap 0:9221dac25d3b 13
ziqiyap 0:9221dac25d3b 14 #define SPS30_CMMD_STRT_MEAS 0x0010
ziqiyap 0:9221dac25d3b 15 #define SPS30_CMMD_STOP_MEAS 0x0104
ziqiyap 0:9221dac25d3b 16 #define SPS30_CMMD_GET_READY_STAT 0x0202
ziqiyap 0:9221dac25d3b 17 #define SPS30_CMMD_READ_MEAS 0x0300
ziqiyap 0:9221dac25d3b 18
ziqiyap 0:9221dac25d3b 19 #define SPS30_CMMD_AUTO_CLEAN_INTV 0x8004
ziqiyap 0:9221dac25d3b 20 #define SPS30_CMMD_START_FAN_CLEAN 0x5607
ziqiyap 0:9221dac25d3b 21
ziqiyap 0:9221dac25d3b 22 #define SPS30_CMMD_SOFT_RESET 0xD304
ziqiyap 0:9221dac25d3b 23
ziqiyap 0:9221dac25d3b 24 #define SPS30_CMMD_READ_SERIALNBR 0xD033
ziqiyap 0:9221dac25d3b 25 #define SPS30_CMMD_READ_ARTICLECODE 0xD025
ziqiyap 4:7558ddc3c7d6 26
ziqiyap 4:7558ddc3c7d6 27 #define SPS30_STRT_MEAS_WRITE_DATA 0x0300
ziqiyap 0:9221dac25d3b 28
ziqiyap 2:549bee9a4cd0 29 #define SPS30_POLYNOMIAL 0x31 // P(x) = x^8 + x^5 + x^4 + 1 = 100110p01
ziqiyap 0:9221dac25d3b 30 #define SPS30_CRC_INIT 0xff
ziqiyap 0:9221dac25d3b 31
ziqiyap 2:549bee9a4cd0 32 #define SPS30_SN_SIZE 33 // size of the s/n ascii string + CRC values
ziqiyap 8:f6b216228067 33
ziqiyap 8:f6b216228067 34 #define MASS_MAX 1000.00f
ziqiyap 8:f6b216228067 35 #define MASS_MIN 0.00f
ziqiyap 8:f6b216228067 36 #define NUM_MAX 3000.00f
ziqiyap 8:f6b216228067 37 #define NUM_MIN 0.00f
ziqiyap 8:f6b216228067 38
ziqiyap 8:f6b216228067 39 #define I2C_FREQUENCY_STD 100000 // SPS30 uses 100MHz for I2C communication
ziqiyap 10:228c926a2416 40 #define TIMEOUT_COUNT 3
ziqiyap 0:9221dac25d3b 41
ziqiyap 2:549bee9a4cd0 42 /** Create SPS30 controller class
ziqiyap 9:a5fe43e183e2 43 * @brief inherits SensorDriver members and methods essential for Sensor Thread
ziqiyap 2:549bee9a4cd0 44 *
ziqiyap 2:549bee9a4cd0 45 * @param sps30 class
ziqiyap 2:549bee9a4cd0 46 *
ziqiyap 2:549bee9a4cd0 47 */
ziqiyap 9:a5fe43e183e2 48 class Sps30: public SensorDriver {
ziqiyap 0:9221dac25d3b 49
ziqiyap 0:9221dac25d3b 50 public:
ziqiyap 8:f6b216228067 51 uint16_t sps_ready; /**< 1 = ready, 0 = busy */
ziqiyap 2:549bee9a4cd0 52 uint32_t clean_interval_i; /** 32 unsigned bit in seconds */
ziqiyap 0:9221dac25d3b 53
ziqiyap 0:9221dac25d3b 54 /** Create a SPS30 object using the specified I2C object
ziqiyap 0:9221dac25d3b 55 * @param sda - mbed I2C interface pin
ziqiyap 0:9221dac25d3b 56 * @param scl - mbed I2C interface pin
ziqiyap 0:9221dac25d3b 57 * @param I2C Frequency (in Hz)
ziqiyap 0:9221dac25d3b 58 *
ziqiyap 0:9221dac25d3b 59 * @return none
ziqiyap 0:9221dac25d3b 60 */
ziqiyap 8:f6b216228067 61 Sps30(PinName sda, PinName scl, int i2c_frequency);
ziqiyap 0:9221dac25d3b 62
ziqiyap 0:9221dac25d3b 63 /** Destructor
ziqiyap 0:9221dac25d3b 64 *
ziqiyap 0:9221dac25d3b 65 * @param --none--
ziqiyap 0:9221dac25d3b 66 *
ziqiyap 0:9221dac25d3b 67 * @return none
ziqiyap 0:9221dac25d3b 68 */
ziqiyap 8:f6b216228067 69 ~Sps30();
ziqiyap 8:f6b216228067 70
ziqiyap 8:f6b216228067 71
ziqiyap 8:f6b216228067 72 /** Initialise SPS30
ziqiyap 8:f6b216228067 73 *
ziqiyap 8:f6b216228067 74 * @param --none--
ziqiyap 8:f6b216228067 75 *
ziqiyap 8:f6b216228067 76 * @return enum SPSStatus
ziqiyap 8:f6b216228067 77 */
ziqiyap 9:a5fe43e183e2 78 void InitSensor();
ziqiyap 8:f6b216228067 79
ziqiyap 8:f6b216228067 80 /** Poll SPS30 Data
ziqiyap 8:f6b216228067 81 *
ziqiyap 8:f6b216228067 82 * @param --none--
ziqiyap 8:f6b216228067 83 *
ziqiyap 8:f6b216228067 84 * @return enum SPSStatus
ziqiyap 8:f6b216228067 85 */
ziqiyap 9:a5fe43e183e2 86 int PollSensor();
ziqiyap 8:f6b216228067 87
ziqiyap 8:f6b216228067 88 private:
ziqiyap 8:f6b216228067 89
ziqiyap 8:f6b216228067 90 enum SPSError {
ziqiyap 8:f6b216228067 91 SPSNOERROR, //all ok
ziqiyap 8:f6b216228067 92 SPSISREADY, //ready status register
ziqiyap 8:f6b216228067 93 SPSNOACKERROR, //no I2C ACK error
ziqiyap 8:f6b216228067 94 SPSCRCERROR, //CRC error, any
ziqiyap 8:f6b216228067 95 };
ziqiyap 8:f6b216228067 96
ziqiyap 8:f6b216228067 97 char i2cbuff[60];
ziqiyap 10:228c926a2416 98
ziqiyap 10:228c926a2416 99 bool sps_status; /**< status bit */
ziqiyap 10:228c926a2416 100 int t_count; /**< timeout counter */
ziqiyap 10:228c926a2416 101 uint8_t sn[33]; /**< ASCII Serial Number */
ziqiyap 9:a5fe43e183e2 102
ziqiyap 9:a5fe43e183e2 103 std::vector<float> sensor_float_data; /** Vector of sensor float data **/
ziqiyap 9:a5fe43e183e2 104 std::vector<std::string> sensor_data_name; /** Private list of sensor data types **/
ziqiyap 8:f6b216228067 105
ziqiyap 8:f6b216228067 106 uint16_t clean_interval_m; /**< High order 16 bit word of Auto Clean Interval */
ziqiyap 8:f6b216228067 107 uint16_t clean_interval_l; /**< High order 16 bit word of Auto Clean Interval */
ziqiyap 8:f6b216228067 108
ziqiyap 8:f6b216228067 109 uint16_t mass_1p0_m; /**< High order 16 bit word of Mass Conc of PM1.0 */
ziqiyap 8:f6b216228067 110 uint16_t mass_1p0_l; /**< Low order 16 bit word of Mass Conc of PM1.0 */
ziqiyap 8:f6b216228067 111 uint16_t mass_2p5_m; /**< High order 16 bit word of Mass Conc of PM2.5 */
ziqiyap 8:f6b216228067 112 uint16_t mass_2p5_l; /**< Low order 16 bit word of Mass Conc of PM2.5 */
ziqiyap 8:f6b216228067 113 uint16_t mass_4p0_m; /**< High order 16 bit word of Mass Conc of PM4.0 */
ziqiyap 8:f6b216228067 114 uint16_t mass_4p0_l; /**< Low order 16 bit word of Mass Conc of PM4.0 */
ziqiyap 8:f6b216228067 115 uint16_t mass_10p0_m; /**< High order 16 bit word of Mass Conc of PM10 */
ziqiyap 8:f6b216228067 116 uint16_t mass_10p0_l; /**< Low order 16 bit word of Mass Conc of PM10 */
ziqiyap 8:f6b216228067 117
ziqiyap 8:f6b216228067 118 uint16_t num_0p5_m; /**< High order 16 bit word of Number Conc of PM0.5 */
ziqiyap 8:f6b216228067 119 uint16_t num_0p5_l; /**< Low order 16 bit word of Number Conc of PM0.5 */
ziqiyap 8:f6b216228067 120 uint16_t num_1p0_m; /**< High order 16 bit word of Number Conc of PM1.0 */
ziqiyap 8:f6b216228067 121 uint16_t num_1p0_l; /**< Low order 16 bit word of Number Conc of PM1.0 */
ziqiyap 8:f6b216228067 122 uint16_t num_2p5_m; /**< High order 16 bit word of Number Conc of PM2.5 */
ziqiyap 8:f6b216228067 123 uint16_t num_2p5_l; /**< Low order 16 bit word of Number Conc of PM2.5 */
ziqiyap 8:f6b216228067 124 uint16_t num_4p0_m; /**< High order 16 bit word of Number Conc of PM4.0 */
ziqiyap 8:f6b216228067 125 uint16_t num_4p0_l; /**< Low order 16 bit word of Number Conc of PM4.0 */
ziqiyap 8:f6b216228067 126 uint16_t num_10p0_m; /**< High order 16 bit word of Number Conc of PM10 */
ziqiyap 8:f6b216228067 127 uint16_t num_10p0_l; /**< Low order 16 bit word of Number Conc of PM10 */
ziqiyap 8:f6b216228067 128
ziqiyap 8:f6b216228067 129 uint16_t typ_pm_size_m; /**< High order 16 bit word of Typical Particle Size */
ziqiyap 8:f6b216228067 130 uint16_t typ_pm_size_l; /**< Low order 16 bit word of Typical Particle Size */
ziqiyap 8:f6b216228067 131
ziqiyap 8:f6b216228067 132 uint32_t mass_1p0_i; /**< 32 bit int of Mass Conc of PM1.0 */
ziqiyap 8:f6b216228067 133 uint32_t mass_2p5_i; /**< 32 bit int of Mass Conc of PM2.5 */
ziqiyap 8:f6b216228067 134 uint32_t mass_4p0_i; /**< 32 bit int of Mass Conc of PM4.0 */
ziqiyap 8:f6b216228067 135 uint32_t mass_10p0_i; /**< 32 bit int of Mass Conc of PM10 */
ziqiyap 8:f6b216228067 136
ziqiyap 8:f6b216228067 137 uint32_t num_0p5_i; /**< 32 bit int of Number Conc of PM0.5 */
ziqiyap 8:f6b216228067 138 uint32_t num_1p0_i; /**< 32 bit int of Number Conc of PM1.0 */
ziqiyap 8:f6b216228067 139 uint32_t num_2p5_i; /**< 32 bit int of Number Conc of PM2.5 */
ziqiyap 8:f6b216228067 140 uint32_t num_4p0_i; /**< 32 bit int of Number Conc of PM4.0 */
ziqiyap 8:f6b216228067 141 uint32_t num_10p0_i; /**< 32 bit int of Number Conc of PM10 */
ziqiyap 8:f6b216228067 142
ziqiyap 8:f6b216228067 143 uint32_t typ_pm_size_i; /**< 32 bit int of Typical Particle Size */
ziqiyap 0:9221dac25d3b 144
ziqiyap 9:a5fe43e183e2 145 float mass_1p0_f; /**< float of Mass Conc of PM1.0 */
ziqiyap 9:a5fe43e183e2 146 float mass_2p5_f; /**< float of Mass Conc of PM2.5 */
ziqiyap 9:a5fe43e183e2 147 float mass_4p0_f; /**< float of Mass Conc of PM4.0 */
ziqiyap 9:a5fe43e183e2 148 float mass_10p0_f; /**< float of Mass Conc of PM10 */
ziqiyap 9:a5fe43e183e2 149
ziqiyap 9:a5fe43e183e2 150 float num_0p5_f; /**< float of Number Conc of PM0.5 */
ziqiyap 9:a5fe43e183e2 151 float num_1p0_f; /**< float of Number Conc of PM1.0 */
ziqiyap 9:a5fe43e183e2 152 float num_2p5_f; /**< float of Number Conc of PM2.5 */
ziqiyap 9:a5fe43e183e2 153 float num_4p0_f; /**< float of Number Conc of PM4.0 */
ziqiyap 9:a5fe43e183e2 154 float num_10p0_f; /**< float of Number Conc of PM10 */
ziqiyap 9:a5fe43e183e2 155
ziqiyap 9:a5fe43e183e2 156 float typ_pm_size_f; /**< float of Typical Particle Size */
ziqiyap 9:a5fe43e183e2 157
ziqiyap 0:9221dac25d3b 158 /** Start Auto-Measurement
ziqiyap 0:9221dac25d3b 159 *
ziqiyap 0:9221dac25d3b 160 * @param --none--
ziqiyap 0:9221dac25d3b 161 *
ziqiyap 0:9221dac25d3b 162 * @return enum SPSerror
ziqiyap 0:9221dac25d3b 163 */
ziqiyap 4:7558ddc3c7d6 164 uint8_t StartMeasurement();
ziqiyap 0:9221dac25d3b 165
ziqiyap 0:9221dac25d3b 166 /** Stop Auto-Measurement
ziqiyap 0:9221dac25d3b 167 *
ziqiyap 0:9221dac25d3b 168 * @param --none--
ziqiyap 0:9221dac25d3b 169 *
ziqiyap 0:9221dac25d3b 170 * @return enum SPSerror
ziqiyap 0:9221dac25d3b 171 */
ziqiyap 4:7558ddc3c7d6 172 uint8_t StopMeasurement();
ziqiyap 0:9221dac25d3b 173
ziqiyap 9:a5fe43e183e2 174 /** Get Serial Number
ziqiyap 9:a5fe43e183e2 175 *
ziqiyap 9:a5fe43e183e2 176 * @param --none--
ziqiyap 9:a5fe43e183e2 177 * @see ASCII Serial Number as sn[33]
ziqiyap 9:a5fe43e183e2 178 *
ziqiyap 9:a5fe43e183e2 179 * @return enum SPSerror
ziqiyap 9:a5fe43e183e2 180 */
ziqiyap 9:a5fe43e183e2 181 int GetSerialNumber();
ziqiyap 9:a5fe43e183e2 182
ziqiyap 0:9221dac25d3b 183 /** Get Ready Status register
ziqiyap 0:9221dac25d3b 184 *
ziqiyap 0:9221dac25d3b 185 * @param --none--
ziqiyap 0:9221dac25d3b 186 * @see Ready Status result
ziqiyap 0:9221dac25d3b 187 *
ziqiyap 0:9221dac25d3b 188 * @return enum SPSerror
ziqiyap 0:9221dac25d3b 189 */
ziqiyap 4:7558ddc3c7d6 190 uint8_t GetReadyStatus();
ziqiyap 0:9221dac25d3b 191
ziqiyap 0:9221dac25d3b 192 /** Get all particulate matter parameters
ziqiyap 0:9221dac25d3b 193 *
ziqiyap 0:9221dac25d3b 194 * @param --none-
ziqiyap 0:9221dac25d3b 195 * @see Results in Public member variables
ziqiyap 0:9221dac25d3b 196 *
ziqiyap 0:9221dac25d3b 197 * @return enum SPSerror
ziqiyap 0:9221dac25d3b 198 */
ziqiyap 4:7558ddc3c7d6 199 uint8_t ReadMeasurement();
ziqiyap 0:9221dac25d3b 200
ziqiyap 9:a5fe43e183e2 201
ziqiyap 0:9221dac25d3b 202 /** Calculate the SPS30 CRC value
ziqiyap 0:9221dac25d3b 203 *
ziqiyap 0:9221dac25d3b 204 * @param 16 bit value to perform a CRC check on
ziqiyap 0:9221dac25d3b 205 *
ziqiyap 0:9221dac25d3b 206 * @return 8 bit CRC value
ziqiyap 0:9221dac25d3b 207 */
ziqiyap 4:7558ddc3c7d6 208 uint8_t CalcCrc2b(uint16_t seed);
ziqiyap 0:9221dac25d3b 209
ziqiyap 0:9221dac25d3b 210 /** Compare received CRC value with calculated CRC value
ziqiyap 0:9221dac25d3b 211 *
ziqiyap 0:9221dac25d3b 212 * @param 16 bit value to perform a CRC check on
ziqiyap 0:9221dac25d3b 213 * @param 8 bit value to compare CRC values
ziqiyap 0:9221dac25d3b 214 *
ziqiyap 0:9221dac25d3b 215 * @return enum SPSerror
ziqiyap 0:9221dac25d3b 216 */
ziqiyap 4:7558ddc3c7d6 217 uint8_t CheckCrc2b(uint16_t seed, uint8_t crc_in);
ziqiyap 9:a5fe43e183e2 218
ziqiyap 2:549bee9a4cd0 219
ziqiyap 2:549bee9a4cd0 220 /** Read Auto Cleaning Interval on the SPS30
ziqiyap 2:549bee9a4cd0 221 *
ziqiyap 2:549bee9a4cd0 222 * @param --none--
ziqiyap 2:549bee9a4cd0 223 *
ziqiyap 2:549bee9a4cd0 224 * @return enum SPSerror
ziqiyap 2:549bee9a4cd0 225 */
ziqiyap 4:7558ddc3c7d6 226 uint8_t ReadAutoCleanInterval();
ziqiyap 2:549bee9a4cd0 227
ziqiyap 2:549bee9a4cd0 228 /** Set Auto Cleaning Interval on the SPS30
ziqiyap 2:549bee9a4cd0 229 *
ziqiyap 2:549bee9a4cd0 230 * @param Auto Cleaning Interval in seconds
ziqiyap 2:549bee9a4cd0 231 * default is 604800s = 1 week, 0 to disable auto clean
ziqiyap 2:549bee9a4cd0 232 *
ziqiyap 2:549bee9a4cd0 233 * @return enum SPSerror
ziqiyap 2:549bee9a4cd0 234 */
ziqiyap 4:7558ddc3c7d6 235 uint8_t SetAutoCleanInterval(uint32_t set_interval = 604800);
ziqiyap 2:549bee9a4cd0 236
ziqiyap 2:549bee9a4cd0 237
ziqiyap 2:549bee9a4cd0 238 /** Perform manual Fan Cleaning
ziqiyap 2:549bee9a4cd0 239 *
ziqiyap 2:549bee9a4cd0 240 * @param --none--
ziqiyap 2:549bee9a4cd0 241 *
ziqiyap 2:549bee9a4cd0 242 * @return enum SPSerror
ziqiyap 2:549bee9a4cd0 243 */
ziqiyap 4:7558ddc3c7d6 244 uint8_t StartFanClean();
ziqiyap 2:549bee9a4cd0 245
ziqiyap 0:9221dac25d3b 246
ziqiyap 0:9221dac25d3b 247 /** Perform a soft reset
ziqiyap 0:9221dac25d3b 248 *
ziqiyap 0:9221dac25d3b 249 * @param --none--
ziqiyap 0:9221dac25d3b 250 *
ziqiyap 0:9221dac25d3b 251 * @return enum SPSerror
ziqiyap 0:9221dac25d3b 252 */
ziqiyap 4:7558ddc3c7d6 253 uint8_t SoftReset();
ziqiyap 9:a5fe43e183e2 254
ziqiyap 9:a5fe43e183e2 255 /** Create a Sensor Data Array to be read from Sensor Thread
ziqiyap 9:a5fe43e183e2 256 *
ziqiyap 9:a5fe43e183e2 257 * @param --none--
ziqiyap 9:a5fe43e183e2 258 * @see sensor_data vector pair in SensorDriver
ziqiyap 9:a5fe43e183e2 259 *
ziqiyap 9:a5fe43e183e2 260 * @return none
ziqiyap 9:a5fe43e183e2 261 */
ziqiyap 9:a5fe43e183e2 262 void GetDataArray();
ziqiyap 0:9221dac25d3b 263
ziqiyap 0:9221dac25d3b 264 protected:
ziqiyap 0:9221dac25d3b 265 I2C _i2c;
ziqiyap 0:9221dac25d3b 266
ziqiyap 0:9221dac25d3b 267 };
ziqiyap 0:9221dac25d3b 268 #endif
ziqiyap 0:9221dac25d3b 269