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:
Wed Mar 27 06:04:36 2019 +0000
Revision:
9:a5fe43e183e2
Parent:
8:f6b216228067
Child:
10:228c926a2416
sps30 interfaced with sensordriver

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 0:9221dac25d3b 40
ziqiyap 2:549bee9a4cd0 41 /** Create SPS30 controller class
ziqiyap 9:a5fe43e183e2 42 * @brief inherits SensorDriver members and methods essential for Sensor Thread
ziqiyap 2:549bee9a4cd0 43 *
ziqiyap 2:549bee9a4cd0 44 * @param sps30 class
ziqiyap 2:549bee9a4cd0 45 *
ziqiyap 2:549bee9a4cd0 46 */
ziqiyap 9:a5fe43e183e2 47 class Sps30: public SensorDriver {
ziqiyap 0:9221dac25d3b 48
ziqiyap 0:9221dac25d3b 49 public:
ziqiyap 8:f6b216228067 50 uint16_t sps_ready; /**< 1 = ready, 0 = busy */
ziqiyap 2:549bee9a4cd0 51 uint32_t clean_interval_i; /** 32 unsigned bit in seconds */
ziqiyap 0:9221dac25d3b 52
ziqiyap 0:9221dac25d3b 53 /** Create a SPS30 object using the specified I2C object
ziqiyap 0:9221dac25d3b 54 * @param sda - mbed I2C interface pin
ziqiyap 0:9221dac25d3b 55 * @param scl - mbed I2C interface pin
ziqiyap 0:9221dac25d3b 56 * @param I2C Frequency (in Hz)
ziqiyap 0:9221dac25d3b 57 *
ziqiyap 0:9221dac25d3b 58 * @return none
ziqiyap 0:9221dac25d3b 59 */
ziqiyap 8:f6b216228067 60 Sps30(PinName sda, PinName scl, int i2c_frequency);
ziqiyap 0:9221dac25d3b 61
ziqiyap 0:9221dac25d3b 62 /** Destructor
ziqiyap 0:9221dac25d3b 63 *
ziqiyap 0:9221dac25d3b 64 * @param --none--
ziqiyap 0:9221dac25d3b 65 *
ziqiyap 0:9221dac25d3b 66 * @return none
ziqiyap 0:9221dac25d3b 67 */
ziqiyap 8:f6b216228067 68 ~Sps30();
ziqiyap 8:f6b216228067 69
ziqiyap 8:f6b216228067 70
ziqiyap 8:f6b216228067 71 /** Initialise SPS30
ziqiyap 8:f6b216228067 72 *
ziqiyap 8:f6b216228067 73 * @param --none--
ziqiyap 8:f6b216228067 74 *
ziqiyap 8:f6b216228067 75 * @return enum SPSStatus
ziqiyap 8:f6b216228067 76 */
ziqiyap 9:a5fe43e183e2 77 void InitSensor();
ziqiyap 8:f6b216228067 78
ziqiyap 8:f6b216228067 79 /** Poll SPS30 Data
ziqiyap 8:f6b216228067 80 *
ziqiyap 8:f6b216228067 81 * @param --none--
ziqiyap 8:f6b216228067 82 *
ziqiyap 8:f6b216228067 83 * @return enum SPSStatus
ziqiyap 8:f6b216228067 84 */
ziqiyap 9:a5fe43e183e2 85 int PollSensor();
ziqiyap 8:f6b216228067 86
ziqiyap 8:f6b216228067 87 private:
ziqiyap 8:f6b216228067 88
ziqiyap 8:f6b216228067 89 enum SPSError {
ziqiyap 8:f6b216228067 90 SPSNOERROR, //all ok
ziqiyap 8:f6b216228067 91 SPSISREADY, //ready status register
ziqiyap 8:f6b216228067 92 SPSNOACKERROR, //no I2C ACK error
ziqiyap 8:f6b216228067 93 SPSCRCERROR, //CRC error, any
ziqiyap 8:f6b216228067 94 };
ziqiyap 8:f6b216228067 95
ziqiyap 8:f6b216228067 96 char i2cbuff[60];
ziqiyap 9:a5fe43e183e2 97
ziqiyap 9:a5fe43e183e2 98 uint8_t sn[33]; /**< ASCII Serial Number */
ziqiyap 9:a5fe43e183e2 99
ziqiyap 9:a5fe43e183e2 100 std::vector<float> sensor_float_data; /** Vector of sensor float data **/
ziqiyap 9:a5fe43e183e2 101 std::vector<std::string> sensor_data_name; /** Private list of sensor data types **/
ziqiyap 8:f6b216228067 102
ziqiyap 8:f6b216228067 103 uint16_t clean_interval_m; /**< High order 16 bit word of Auto Clean Interval */
ziqiyap 8:f6b216228067 104 uint16_t clean_interval_l; /**< High order 16 bit word of Auto Clean Interval */
ziqiyap 8:f6b216228067 105
ziqiyap 8:f6b216228067 106 uint16_t mass_1p0_m; /**< High order 16 bit word of Mass Conc of PM1.0 */
ziqiyap 8:f6b216228067 107 uint16_t mass_1p0_l; /**< Low order 16 bit word of Mass Conc of PM1.0 */
ziqiyap 8:f6b216228067 108 uint16_t mass_2p5_m; /**< High order 16 bit word of Mass Conc of PM2.5 */
ziqiyap 8:f6b216228067 109 uint16_t mass_2p5_l; /**< Low order 16 bit word of Mass Conc of PM2.5 */
ziqiyap 8:f6b216228067 110 uint16_t mass_4p0_m; /**< High order 16 bit word of Mass Conc of PM4.0 */
ziqiyap 8:f6b216228067 111 uint16_t mass_4p0_l; /**< Low order 16 bit word of Mass Conc of PM4.0 */
ziqiyap 8:f6b216228067 112 uint16_t mass_10p0_m; /**< High order 16 bit word of Mass Conc of PM10 */
ziqiyap 8:f6b216228067 113 uint16_t mass_10p0_l; /**< Low order 16 bit word of Mass Conc of PM10 */
ziqiyap 8:f6b216228067 114
ziqiyap 8:f6b216228067 115 uint16_t num_0p5_m; /**< High order 16 bit word of Number Conc of PM0.5 */
ziqiyap 8:f6b216228067 116 uint16_t num_0p5_l; /**< Low order 16 bit word of Number Conc of PM0.5 */
ziqiyap 8:f6b216228067 117 uint16_t num_1p0_m; /**< High order 16 bit word of Number Conc of PM1.0 */
ziqiyap 8:f6b216228067 118 uint16_t num_1p0_l; /**< Low order 16 bit word of Number Conc of PM1.0 */
ziqiyap 8:f6b216228067 119 uint16_t num_2p5_m; /**< High order 16 bit word of Number Conc of PM2.5 */
ziqiyap 8:f6b216228067 120 uint16_t num_2p5_l; /**< Low order 16 bit word of Number Conc of PM2.5 */
ziqiyap 8:f6b216228067 121 uint16_t num_4p0_m; /**< High order 16 bit word of Number Conc of PM4.0 */
ziqiyap 8:f6b216228067 122 uint16_t num_4p0_l; /**< Low order 16 bit word of Number Conc of PM4.0 */
ziqiyap 8:f6b216228067 123 uint16_t num_10p0_m; /**< High order 16 bit word of Number Conc of PM10 */
ziqiyap 8:f6b216228067 124 uint16_t num_10p0_l; /**< Low order 16 bit word of Number Conc of PM10 */
ziqiyap 8:f6b216228067 125
ziqiyap 8:f6b216228067 126 uint16_t typ_pm_size_m; /**< High order 16 bit word of Typical Particle Size */
ziqiyap 8:f6b216228067 127 uint16_t typ_pm_size_l; /**< Low order 16 bit word of Typical Particle Size */
ziqiyap 8:f6b216228067 128
ziqiyap 8:f6b216228067 129 uint32_t mass_1p0_i; /**< 32 bit int of Mass Conc of PM1.0 */
ziqiyap 8:f6b216228067 130 uint32_t mass_2p5_i; /**< 32 bit int of Mass Conc of PM2.5 */
ziqiyap 8:f6b216228067 131 uint32_t mass_4p0_i; /**< 32 bit int of Mass Conc of PM4.0 */
ziqiyap 8:f6b216228067 132 uint32_t mass_10p0_i; /**< 32 bit int of Mass Conc of PM10 */
ziqiyap 8:f6b216228067 133
ziqiyap 8:f6b216228067 134 uint32_t num_0p5_i; /**< 32 bit int of Number Conc of PM0.5 */
ziqiyap 8:f6b216228067 135 uint32_t num_1p0_i; /**< 32 bit int of Number Conc of PM1.0 */
ziqiyap 8:f6b216228067 136 uint32_t num_2p5_i; /**< 32 bit int of Number Conc of PM2.5 */
ziqiyap 8:f6b216228067 137 uint32_t num_4p0_i; /**< 32 bit int of Number Conc of PM4.0 */
ziqiyap 8:f6b216228067 138 uint32_t num_10p0_i; /**< 32 bit int of Number Conc of PM10 */
ziqiyap 8:f6b216228067 139
ziqiyap 8:f6b216228067 140 uint32_t typ_pm_size_i; /**< 32 bit int of Typical Particle Size */
ziqiyap 0:9221dac25d3b 141
ziqiyap 9:a5fe43e183e2 142 float mass_1p0_f; /**< float of Mass Conc of PM1.0 */
ziqiyap 9:a5fe43e183e2 143 float mass_2p5_f; /**< float of Mass Conc of PM2.5 */
ziqiyap 9:a5fe43e183e2 144 float mass_4p0_f; /**< float of Mass Conc of PM4.0 */
ziqiyap 9:a5fe43e183e2 145 float mass_10p0_f; /**< float of Mass Conc of PM10 */
ziqiyap 9:a5fe43e183e2 146
ziqiyap 9:a5fe43e183e2 147 float num_0p5_f; /**< float of Number Conc of PM0.5 */
ziqiyap 9:a5fe43e183e2 148 float num_1p0_f; /**< float of Number Conc of PM1.0 */
ziqiyap 9:a5fe43e183e2 149 float num_2p5_f; /**< float of Number Conc of PM2.5 */
ziqiyap 9:a5fe43e183e2 150 float num_4p0_f; /**< float of Number Conc of PM4.0 */
ziqiyap 9:a5fe43e183e2 151 float num_10p0_f; /**< float of Number Conc of PM10 */
ziqiyap 9:a5fe43e183e2 152
ziqiyap 9:a5fe43e183e2 153 float typ_pm_size_f; /**< float of Typical Particle Size */
ziqiyap 9:a5fe43e183e2 154
ziqiyap 0:9221dac25d3b 155 /** Start Auto-Measurement
ziqiyap 0:9221dac25d3b 156 *
ziqiyap 0:9221dac25d3b 157 * @param --none--
ziqiyap 0:9221dac25d3b 158 *
ziqiyap 0:9221dac25d3b 159 * @return enum SPSerror
ziqiyap 0:9221dac25d3b 160 */
ziqiyap 4:7558ddc3c7d6 161 uint8_t StartMeasurement();
ziqiyap 0:9221dac25d3b 162
ziqiyap 0:9221dac25d3b 163 /** Stop Auto-Measurement
ziqiyap 0:9221dac25d3b 164 *
ziqiyap 0:9221dac25d3b 165 * @param --none--
ziqiyap 0:9221dac25d3b 166 *
ziqiyap 0:9221dac25d3b 167 * @return enum SPSerror
ziqiyap 0:9221dac25d3b 168 */
ziqiyap 4:7558ddc3c7d6 169 uint8_t StopMeasurement();
ziqiyap 0:9221dac25d3b 170
ziqiyap 9:a5fe43e183e2 171 /** Get Serial Number
ziqiyap 9:a5fe43e183e2 172 *
ziqiyap 9:a5fe43e183e2 173 * @param --none--
ziqiyap 9:a5fe43e183e2 174 * @see ASCII Serial Number as sn[33]
ziqiyap 9:a5fe43e183e2 175 *
ziqiyap 9:a5fe43e183e2 176 * @return enum SPSerror
ziqiyap 9:a5fe43e183e2 177 */
ziqiyap 9:a5fe43e183e2 178 int GetSerialNumber();
ziqiyap 9:a5fe43e183e2 179
ziqiyap 0:9221dac25d3b 180 /** Get Ready Status register
ziqiyap 0:9221dac25d3b 181 *
ziqiyap 0:9221dac25d3b 182 * @param --none--
ziqiyap 0:9221dac25d3b 183 * @see Ready Status result
ziqiyap 0:9221dac25d3b 184 *
ziqiyap 0:9221dac25d3b 185 * @return enum SPSerror
ziqiyap 0:9221dac25d3b 186 */
ziqiyap 4:7558ddc3c7d6 187 uint8_t GetReadyStatus();
ziqiyap 0:9221dac25d3b 188
ziqiyap 0:9221dac25d3b 189 /** Get all particulate matter parameters
ziqiyap 0:9221dac25d3b 190 *
ziqiyap 0:9221dac25d3b 191 * @param --none-
ziqiyap 0:9221dac25d3b 192 * @see Results in Public member variables
ziqiyap 0:9221dac25d3b 193 *
ziqiyap 0:9221dac25d3b 194 * @return enum SPSerror
ziqiyap 0:9221dac25d3b 195 */
ziqiyap 4:7558ddc3c7d6 196 uint8_t ReadMeasurement();
ziqiyap 0:9221dac25d3b 197
ziqiyap 9:a5fe43e183e2 198
ziqiyap 0:9221dac25d3b 199 /** Calculate the SPS30 CRC value
ziqiyap 0:9221dac25d3b 200 *
ziqiyap 0:9221dac25d3b 201 * @param 16 bit value to perform a CRC check on
ziqiyap 0:9221dac25d3b 202 *
ziqiyap 0:9221dac25d3b 203 * @return 8 bit CRC value
ziqiyap 0:9221dac25d3b 204 */
ziqiyap 4:7558ddc3c7d6 205 uint8_t CalcCrc2b(uint16_t seed);
ziqiyap 0:9221dac25d3b 206
ziqiyap 0:9221dac25d3b 207 /** Compare received CRC value with calculated CRC value
ziqiyap 0:9221dac25d3b 208 *
ziqiyap 0:9221dac25d3b 209 * @param 16 bit value to perform a CRC check on
ziqiyap 0:9221dac25d3b 210 * @param 8 bit value to compare CRC values
ziqiyap 0:9221dac25d3b 211 *
ziqiyap 0:9221dac25d3b 212 * @return enum SPSerror
ziqiyap 0:9221dac25d3b 213 */
ziqiyap 4:7558ddc3c7d6 214 uint8_t CheckCrc2b(uint16_t seed, uint8_t crc_in);
ziqiyap 9:a5fe43e183e2 215
ziqiyap 2:549bee9a4cd0 216
ziqiyap 2:549bee9a4cd0 217 /** Read Auto Cleaning Interval on the SPS30
ziqiyap 2:549bee9a4cd0 218 *
ziqiyap 2:549bee9a4cd0 219 * @param --none--
ziqiyap 2:549bee9a4cd0 220 *
ziqiyap 2:549bee9a4cd0 221 * @return enum SPSerror
ziqiyap 2:549bee9a4cd0 222 */
ziqiyap 4:7558ddc3c7d6 223 uint8_t ReadAutoCleanInterval();
ziqiyap 2:549bee9a4cd0 224
ziqiyap 2:549bee9a4cd0 225 /** Set Auto Cleaning Interval on the SPS30
ziqiyap 2:549bee9a4cd0 226 *
ziqiyap 2:549bee9a4cd0 227 * @param Auto Cleaning Interval in seconds
ziqiyap 2:549bee9a4cd0 228 * default is 604800s = 1 week, 0 to disable auto clean
ziqiyap 2:549bee9a4cd0 229 *
ziqiyap 2:549bee9a4cd0 230 * @return enum SPSerror
ziqiyap 2:549bee9a4cd0 231 */
ziqiyap 4:7558ddc3c7d6 232 uint8_t SetAutoCleanInterval(uint32_t set_interval = 604800);
ziqiyap 2:549bee9a4cd0 233
ziqiyap 2:549bee9a4cd0 234
ziqiyap 2:549bee9a4cd0 235 /** Perform manual Fan Cleaning
ziqiyap 2:549bee9a4cd0 236 *
ziqiyap 2:549bee9a4cd0 237 * @param --none--
ziqiyap 2:549bee9a4cd0 238 *
ziqiyap 2:549bee9a4cd0 239 * @return enum SPSerror
ziqiyap 2:549bee9a4cd0 240 */
ziqiyap 4:7558ddc3c7d6 241 uint8_t StartFanClean();
ziqiyap 2:549bee9a4cd0 242
ziqiyap 0:9221dac25d3b 243
ziqiyap 0:9221dac25d3b 244 /** Perform a soft reset
ziqiyap 0:9221dac25d3b 245 *
ziqiyap 0:9221dac25d3b 246 * @param --none--
ziqiyap 0:9221dac25d3b 247 *
ziqiyap 0:9221dac25d3b 248 * @return enum SPSerror
ziqiyap 0:9221dac25d3b 249 */
ziqiyap 4:7558ddc3c7d6 250 uint8_t SoftReset();
ziqiyap 9:a5fe43e183e2 251
ziqiyap 9:a5fe43e183e2 252 /** Create a Sensor Data Array to be read from Sensor Thread
ziqiyap 9:a5fe43e183e2 253 *
ziqiyap 9:a5fe43e183e2 254 * @param --none--
ziqiyap 9:a5fe43e183e2 255 * @see sensor_data vector pair in SensorDriver
ziqiyap 9:a5fe43e183e2 256 *
ziqiyap 9:a5fe43e183e2 257 * @return none
ziqiyap 9:a5fe43e183e2 258 */
ziqiyap 9:a5fe43e183e2 259 void GetDataArray();
ziqiyap 0:9221dac25d3b 260
ziqiyap 0:9221dac25d3b 261 protected:
ziqiyap 0:9221dac25d3b 262 I2C _i2c;
ziqiyap 0:9221dac25d3b 263
ziqiyap 0:9221dac25d3b 264 };
ziqiyap 0:9221dac25d3b 265 #endif
ziqiyap 0:9221dac25d3b 266