SPS30 library
Dependents: IZU2021_SPS30 Hybrid_IZU2021_MISSION_v2 Hybrid_IZU2021_MISSION
sps30.h@7:4152825938bf, 2019-03-12 (annotated)
- Committer:
- ziqiyap
- Date:
- Tue Mar 12 08:25:10 2019 +0000
- Revision:
- 7:4152825938bf
- Parent:
- 6:83ed80f27fff
- Child:
- 8:f6b216228067
fixed minor comment typo
Who changed what in which revision?
User | Revision | Line number | New 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 | 3:adeb61dd9ceb | 7 | |
ziqiyap | 0:9221dac25d3b | 8 | #ifndef SPS30_H |
ziqiyap | 0:9221dac25d3b | 9 | #define SPS30_H |
ziqiyap | 0:9221dac25d3b | 10 | |
ziqiyap | 2:549bee9a4cd0 | 11 | #define SPS30_I2C_ADDR 0xd2 // left shifted by 1 bit from 0x69 |
ziqiyap | 0:9221dac25d3b | 12 | |
ziqiyap | 0:9221dac25d3b | 13 | #define SPS30_CMMD_STRT_MEAS 0x0010 |
ziqiyap | 0:9221dac25d3b | 14 | #define SPS30_CMMD_STOP_MEAS 0x0104 |
ziqiyap | 0:9221dac25d3b | 15 | #define SPS30_CMMD_GET_READY_STAT 0x0202 |
ziqiyap | 0:9221dac25d3b | 16 | #define SPS30_CMMD_READ_MEAS 0x0300 |
ziqiyap | 0:9221dac25d3b | 17 | |
ziqiyap | 0:9221dac25d3b | 18 | #define SPS30_CMMD_AUTO_CLEAN_INTV 0x8004 |
ziqiyap | 0:9221dac25d3b | 19 | #define SPS30_CMMD_START_FAN_CLEAN 0x5607 |
ziqiyap | 0:9221dac25d3b | 20 | |
ziqiyap | 0:9221dac25d3b | 21 | #define SPS30_CMMD_SOFT_RESET 0xD304 |
ziqiyap | 0:9221dac25d3b | 22 | |
ziqiyap | 0:9221dac25d3b | 23 | #define SPS30_CMMD_READ_SERIALNBR 0xD033 |
ziqiyap | 0:9221dac25d3b | 24 | #define SPS30_CMMD_READ_ARTICLECODE 0xD025 |
ziqiyap | 4:7558ddc3c7d6 | 25 | |
ziqiyap | 4:7558ddc3c7d6 | 26 | #define SPS30_STRT_MEAS_WRITE_DATA 0x0300 |
ziqiyap | 0:9221dac25d3b | 27 | |
ziqiyap | 2:549bee9a4cd0 | 28 | #define SPS30_POLYNOMIAL 0x31 // P(x) = x^8 + x^5 + x^4 + 1 = 100110p01 |
ziqiyap | 0:9221dac25d3b | 29 | #define SPS30_CRC_INIT 0xff |
ziqiyap | 0:9221dac25d3b | 30 | |
ziqiyap | 2:549bee9a4cd0 | 31 | #define SPS30_SN_SIZE 33 // size of the s/n ascii string + CRC values |
ziqiyap | 2:549bee9a4cd0 | 32 | #define SPS30_ACODE_SIZE 33 // size of the article code ascii string + CRC values |
ziqiyap | 0:9221dac25d3b | 33 | |
ziqiyap | 2:549bee9a4cd0 | 34 | /** Create SPS30 controller class |
ziqiyap | 2:549bee9a4cd0 | 35 | * |
ziqiyap | 2:549bee9a4cd0 | 36 | * @param sps30 class |
ziqiyap | 2:549bee9a4cd0 | 37 | * |
ziqiyap | 2:549bee9a4cd0 | 38 | */ |
ziqiyap | 0:9221dac25d3b | 39 | class sps30 { |
ziqiyap | 0:9221dac25d3b | 40 | |
ziqiyap | 0:9221dac25d3b | 41 | public: |
ziqiyap | 6:83ed80f27fff | 42 | enum SPSError { |
ziqiyap | 6:83ed80f27fff | 43 | SPSNOERROR, //all ok |
ziqiyap | 7:4152825938bf | 44 | SPSISREADY, //ready status register |
ziqiyap | 6:83ed80f27fff | 45 | SPSNOACKERROR, //no I2C ACK error |
ziqiyap | 6:83ed80f27fff | 46 | SPSCRCERROR, //CRC error, any |
ziqiyap | 0:9221dac25d3b | 47 | }; |
ziqiyap | 0:9221dac25d3b | 48 | |
ziqiyap | 2:549bee9a4cd0 | 49 | uint16_t ready; /**< 1 = ready, 0 = busy */ |
ziqiyap | 2:549bee9a4cd0 | 50 | uint32_t clean_interval_i; /** 32 unsigned bit in seconds */ |
ziqiyap | 0:9221dac25d3b | 51 | |
ziqiyap | 2:549bee9a4cd0 | 52 | float mass_1p0_f; /**< float of Mass Conc of PM1.0 */ |
ziqiyap | 2:549bee9a4cd0 | 53 | float mass_2p5_f; /**< float of Mass Conc of PM2.5 */ |
ziqiyap | 2:549bee9a4cd0 | 54 | float mass_4p0_f; /**< float of Mass Conc of PM4.0 */ |
ziqiyap | 2:549bee9a4cd0 | 55 | float mass_10p0_f; /**< float of Mass Conc of PM10 */ |
ziqiyap | 0:9221dac25d3b | 56 | |
ziqiyap | 2:549bee9a4cd0 | 57 | float num_0p5_f; /**< float of Number Conc of PM0.5 */ |
ziqiyap | 2:549bee9a4cd0 | 58 | float num_1p0_f; /**< float of Number Conc of PM1.0 */ |
ziqiyap | 2:549bee9a4cd0 | 59 | float num_2p5_f; /**< float of Number Conc of PM2.5 */ |
ziqiyap | 2:549bee9a4cd0 | 60 | float num_4p0_f; /**< float of Number Conc of PM4.0 */ |
ziqiyap | 2:549bee9a4cd0 | 61 | float num_10p0_f; /**< float of Number Conc of PM10 */ |
ziqiyap | 0:9221dac25d3b | 62 | |
ziqiyap | 2:549bee9a4cd0 | 63 | float typ_pm_size_f; /**< float of Typical Particle Size */ |
ziqiyap | 0:9221dac25d3b | 64 | |
ziqiyap | 2:549bee9a4cd0 | 65 | uint8_t acode[33]; /**< Article code number */ |
ziqiyap | 2:549bee9a4cd0 | 66 | uint8_t sn[33]; /**< ASCII Serial Number */ |
ziqiyap | 0:9221dac25d3b | 67 | |
ziqiyap | 0:9221dac25d3b | 68 | |
ziqiyap | 0:9221dac25d3b | 69 | |
ziqiyap | 0:9221dac25d3b | 70 | /** Create a SPS30 object using the specified I2C object |
ziqiyap | 0:9221dac25d3b | 71 | * @param sda - mbed I2C interface pin |
ziqiyap | 0:9221dac25d3b | 72 | * @param scl - mbed I2C interface pin |
ziqiyap | 0:9221dac25d3b | 73 | * @param I2C Frequency (in Hz) |
ziqiyap | 0:9221dac25d3b | 74 | * |
ziqiyap | 0:9221dac25d3b | 75 | * @return none |
ziqiyap | 0:9221dac25d3b | 76 | */ |
ziqiyap | 4:7558ddc3c7d6 | 77 | sps30(PinName sda, PinName scl, int i2c_frequency); |
ziqiyap | 0:9221dac25d3b | 78 | |
ziqiyap | 0:9221dac25d3b | 79 | /** Destructor |
ziqiyap | 0:9221dac25d3b | 80 | * |
ziqiyap | 0:9221dac25d3b | 81 | * @param --none-- |
ziqiyap | 0:9221dac25d3b | 82 | * |
ziqiyap | 0:9221dac25d3b | 83 | * @return none |
ziqiyap | 0:9221dac25d3b | 84 | */ |
ziqiyap | 0:9221dac25d3b | 85 | ~sps30(); |
ziqiyap | 0:9221dac25d3b | 86 | |
ziqiyap | 0:9221dac25d3b | 87 | /** Start Auto-Measurement |
ziqiyap | 0:9221dac25d3b | 88 | * |
ziqiyap | 0:9221dac25d3b | 89 | * @param --none-- |
ziqiyap | 0:9221dac25d3b | 90 | * |
ziqiyap | 0:9221dac25d3b | 91 | * @return enum SPSerror |
ziqiyap | 0:9221dac25d3b | 92 | */ |
ziqiyap | 4:7558ddc3c7d6 | 93 | uint8_t StartMeasurement(); |
ziqiyap | 0:9221dac25d3b | 94 | |
ziqiyap | 0:9221dac25d3b | 95 | /** Stop Auto-Measurement |
ziqiyap | 0:9221dac25d3b | 96 | * |
ziqiyap | 0:9221dac25d3b | 97 | * @param --none-- |
ziqiyap | 0:9221dac25d3b | 98 | * |
ziqiyap | 0:9221dac25d3b | 99 | * @return enum SPSerror |
ziqiyap | 0:9221dac25d3b | 100 | */ |
ziqiyap | 4:7558ddc3c7d6 | 101 | uint8_t StopMeasurement(); |
ziqiyap | 0:9221dac25d3b | 102 | |
ziqiyap | 0:9221dac25d3b | 103 | /** Get Ready Status register |
ziqiyap | 0:9221dac25d3b | 104 | * |
ziqiyap | 0:9221dac25d3b | 105 | * @param --none-- |
ziqiyap | 0:9221dac25d3b | 106 | * @see Ready Status result |
ziqiyap | 0:9221dac25d3b | 107 | * |
ziqiyap | 0:9221dac25d3b | 108 | * @return enum SPSerror |
ziqiyap | 0:9221dac25d3b | 109 | */ |
ziqiyap | 4:7558ddc3c7d6 | 110 | uint8_t GetReadyStatus(); |
ziqiyap | 0:9221dac25d3b | 111 | |
ziqiyap | 0:9221dac25d3b | 112 | /** Get all particulate matter parameters |
ziqiyap | 0:9221dac25d3b | 113 | * |
ziqiyap | 0:9221dac25d3b | 114 | * @param --none- |
ziqiyap | 0:9221dac25d3b | 115 | * @see Results in Public member variables |
ziqiyap | 0:9221dac25d3b | 116 | * |
ziqiyap | 0:9221dac25d3b | 117 | * @return enum SPSerror |
ziqiyap | 0:9221dac25d3b | 118 | */ |
ziqiyap | 4:7558ddc3c7d6 | 119 | uint8_t ReadMeasurement(); |
ziqiyap | 0:9221dac25d3b | 120 | |
ziqiyap | 0:9221dac25d3b | 121 | /** Calculate the SPS30 CRC value |
ziqiyap | 0:9221dac25d3b | 122 | * |
ziqiyap | 0:9221dac25d3b | 123 | * @param 16 bit value to perform a CRC check on |
ziqiyap | 0:9221dac25d3b | 124 | * |
ziqiyap | 0:9221dac25d3b | 125 | * @return 8 bit CRC value |
ziqiyap | 0:9221dac25d3b | 126 | */ |
ziqiyap | 4:7558ddc3c7d6 | 127 | uint8_t CalcCrc2b(uint16_t seed); |
ziqiyap | 0:9221dac25d3b | 128 | |
ziqiyap | 0:9221dac25d3b | 129 | /** Compare received CRC value with calculated CRC value |
ziqiyap | 0:9221dac25d3b | 130 | * |
ziqiyap | 0:9221dac25d3b | 131 | * @param 16 bit value to perform a CRC check on |
ziqiyap | 0:9221dac25d3b | 132 | * @param 8 bit value to compare CRC values |
ziqiyap | 0:9221dac25d3b | 133 | * |
ziqiyap | 0:9221dac25d3b | 134 | * @return enum SPSerror |
ziqiyap | 0:9221dac25d3b | 135 | */ |
ziqiyap | 4:7558ddc3c7d6 | 136 | uint8_t CheckCrc2b(uint16_t seed, uint8_t crc_in); |
ziqiyap | 0:9221dac25d3b | 137 | |
ziqiyap | 0:9221dac25d3b | 138 | /** Get Article Code |
ziqiyap | 0:9221dac25d3b | 139 | * |
ziqiyap | 0:9221dac25d3b | 140 | * @param --none-- |
ziqiyap | 2:549bee9a4cd0 | 141 | * @see ASCII Article Code as acode[33] (returns all 0) |
ziqiyap | 0:9221dac25d3b | 142 | * |
ziqiyap | 0:9221dac25d3b | 143 | * @return enum SPSerror |
ziqiyap | 0:9221dac25d3b | 144 | */ |
ziqiyap | 4:7558ddc3c7d6 | 145 | uint8_t GetArticleCode(); |
ziqiyap | 0:9221dac25d3b | 146 | |
ziqiyap | 0:9221dac25d3b | 147 | /** Get Serial Number |
ziqiyap | 0:9221dac25d3b | 148 | * |
ziqiyap | 0:9221dac25d3b | 149 | * @param --none-- |
ziqiyap | 2:549bee9a4cd0 | 150 | * @see ASCII Serial Number as sn[33] |
ziqiyap | 0:9221dac25d3b | 151 | * |
ziqiyap | 0:9221dac25d3b | 152 | * @return enum SPSerror |
ziqiyap | 0:9221dac25d3b | 153 | */ |
ziqiyap | 4:7558ddc3c7d6 | 154 | uint8_t GetSerialNumber(); |
ziqiyap | 2:549bee9a4cd0 | 155 | |
ziqiyap | 2:549bee9a4cd0 | 156 | /** Read Auto Cleaning Interval on the SPS30 |
ziqiyap | 2:549bee9a4cd0 | 157 | * |
ziqiyap | 2:549bee9a4cd0 | 158 | * @param --none-- |
ziqiyap | 2:549bee9a4cd0 | 159 | * |
ziqiyap | 2:549bee9a4cd0 | 160 | * @return enum SPSerror |
ziqiyap | 2:549bee9a4cd0 | 161 | */ |
ziqiyap | 4:7558ddc3c7d6 | 162 | uint8_t ReadAutoCleanInterval(); |
ziqiyap | 2:549bee9a4cd0 | 163 | |
ziqiyap | 2:549bee9a4cd0 | 164 | /** Set Auto Cleaning Interval on the SPS30 |
ziqiyap | 2:549bee9a4cd0 | 165 | * |
ziqiyap | 2:549bee9a4cd0 | 166 | * @param Auto Cleaning Interval in seconds |
ziqiyap | 2:549bee9a4cd0 | 167 | * default is 604800s = 1 week, 0 to disable auto clean |
ziqiyap | 2:549bee9a4cd0 | 168 | * |
ziqiyap | 2:549bee9a4cd0 | 169 | * @return enum SPSerror |
ziqiyap | 2:549bee9a4cd0 | 170 | */ |
ziqiyap | 4:7558ddc3c7d6 | 171 | uint8_t SetAutoCleanInterval(uint32_t set_interval = 604800); |
ziqiyap | 2:549bee9a4cd0 | 172 | |
ziqiyap | 2:549bee9a4cd0 | 173 | |
ziqiyap | 2:549bee9a4cd0 | 174 | /** Perform manual Fan Cleaning |
ziqiyap | 2:549bee9a4cd0 | 175 | * |
ziqiyap | 2:549bee9a4cd0 | 176 | * @param --none-- |
ziqiyap | 2:549bee9a4cd0 | 177 | * |
ziqiyap | 2:549bee9a4cd0 | 178 | * @return enum SPSerror |
ziqiyap | 2:549bee9a4cd0 | 179 | */ |
ziqiyap | 4:7558ddc3c7d6 | 180 | uint8_t StartFanClean(); |
ziqiyap | 2:549bee9a4cd0 | 181 | |
ziqiyap | 0:9221dac25d3b | 182 | |
ziqiyap | 0:9221dac25d3b | 183 | /** Perform a soft reset |
ziqiyap | 0:9221dac25d3b | 184 | * |
ziqiyap | 0:9221dac25d3b | 185 | * @param --none-- |
ziqiyap | 0:9221dac25d3b | 186 | * |
ziqiyap | 0:9221dac25d3b | 187 | * @return enum SPSerror |
ziqiyap | 0:9221dac25d3b | 188 | */ |
ziqiyap | 4:7558ddc3c7d6 | 189 | uint8_t SoftReset(); |
ziqiyap | 0:9221dac25d3b | 190 | |
ziqiyap | 0:9221dac25d3b | 191 | private: |
ziqiyap | 4:7558ddc3c7d6 | 192 | char i2cbuff[60]; |
ziqiyap | 2:549bee9a4cd0 | 193 | |
ziqiyap | 2:549bee9a4cd0 | 194 | uint16_t clean_interval_m; /**< High order 16 bit word of Auto Clean Interval */ |
ziqiyap | 2:549bee9a4cd0 | 195 | uint16_t clean_interval_l; /**< High order 16 bit word of Auto Clean Interval */ |
ziqiyap | 0:9221dac25d3b | 196 | |
ziqiyap | 0:9221dac25d3b | 197 | uint16_t mass_1p0_m; /**< High order 16 bit word of Mass Conc of PM1.0 */ |
ziqiyap | 0:9221dac25d3b | 198 | uint16_t mass_1p0_l; /**< Low order 16 bit word of Mass Conc of PM1.0 */ |
ziqiyap | 0:9221dac25d3b | 199 | uint16_t mass_2p5_m; /**< High order 16 bit word of Mass Conc of PM2.5 */ |
ziqiyap | 0:9221dac25d3b | 200 | uint16_t mass_2p5_l; /**< Low order 16 bit word of Mass Conc of PM2.5 */ |
ziqiyap | 0:9221dac25d3b | 201 | uint16_t mass_4p0_m; /**< High order 16 bit word of Mass Conc of PM4.0 */ |
ziqiyap | 0:9221dac25d3b | 202 | uint16_t mass_4p0_l; /**< Low order 16 bit word of Mass Conc of PM4.0 */ |
ziqiyap | 2:549bee9a4cd0 | 203 | uint16_t mass_10p0_m; /**< High order 16 bit word of Mass Conc of PM10 */ |
ziqiyap | 2:549bee9a4cd0 | 204 | uint16_t mass_10p0_l; /**< Low order 16 bit word of Mass Conc of PM10 */ |
ziqiyap | 0:9221dac25d3b | 205 | |
ziqiyap | 2:549bee9a4cd0 | 206 | uint16_t num_0p5_m; /**< High order 16 bit word of Number Conc of PM0.5 */ |
ziqiyap | 2:549bee9a4cd0 | 207 | uint16_t num_0p5_l; /**< Low order 16 bit word of Number Conc of PM0.5 */ |
ziqiyap | 2:549bee9a4cd0 | 208 | uint16_t num_1p0_m; /**< High order 16 bit word of Number Conc of PM1.0 */ |
ziqiyap | 2:549bee9a4cd0 | 209 | uint16_t num_1p0_l; /**< Low order 16 bit word of Number Conc of PM1.0 */ |
ziqiyap | 2:549bee9a4cd0 | 210 | uint16_t num_2p5_m; /**< High order 16 bit word of Number Conc of PM2.5 */ |
ziqiyap | 2:549bee9a4cd0 | 211 | uint16_t num_2p5_l; /**< Low order 16 bit word of Number Conc of PM2.5 */ |
ziqiyap | 2:549bee9a4cd0 | 212 | uint16_t num_4p0_m; /**< High order 16 bit word of Number Conc of PM4.0 */ |
ziqiyap | 2:549bee9a4cd0 | 213 | uint16_t num_4p0_l; /**< Low order 16 bit word of Number Conc of PM4.0 */ |
ziqiyap | 0:9221dac25d3b | 214 | uint16_t num_10p0_m; /**< High order 16 bit word of Number Conc of PM10 */ |
ziqiyap | 0:9221dac25d3b | 215 | uint16_t num_10p0_l; /**< Low order 16 bit word of Number Conc of PM10 */ |
ziqiyap | 0:9221dac25d3b | 216 | |
ziqiyap | 2:549bee9a4cd0 | 217 | uint16_t typ_pm_size_m; /**< High order 16 bit word of Typical Particle Size */ |
ziqiyap | 2:549bee9a4cd0 | 218 | uint16_t typ_pm_size_l; /**< Low order 16 bit word of Typical Particle Size */ |
ziqiyap | 0:9221dac25d3b | 219 | |
ziqiyap | 0:9221dac25d3b | 220 | uint32_t mass_1p0_i; /**< 32 bit int of Mass Conc of PM1.0 */ |
ziqiyap | 0:9221dac25d3b | 221 | uint32_t mass_2p5_i; /**< 32 bit int of Mass Conc of PM2.5 */ |
ziqiyap | 0:9221dac25d3b | 222 | uint32_t mass_4p0_i; /**< 32 bit int of Mass Conc of PM4.0 */ |
ziqiyap | 2:549bee9a4cd0 | 223 | uint32_t mass_10p0_i; /**< 32 bit int of Mass Conc of PM10 */ |
ziqiyap | 0:9221dac25d3b | 224 | |
ziqiyap | 2:549bee9a4cd0 | 225 | uint32_t num_0p5_i; /**< 32 bit int of Number Conc of PM0.5 */ |
ziqiyap | 2:549bee9a4cd0 | 226 | uint32_t num_1p0_i; /**< 32 bit int of Number Conc of PM1.0 */ |
ziqiyap | 2:549bee9a4cd0 | 227 | uint32_t num_2p5_i; /**< 32 bit int of Number Conc of PM2.5 */ |
ziqiyap | 2:549bee9a4cd0 | 228 | uint32_t num_4p0_i; /**< 32 bit int of Number Conc of PM4.0 */ |
ziqiyap | 0:9221dac25d3b | 229 | uint32_t num_10p0_i; /**< 32 bit int of Number Conc of PM10 */ |
ziqiyap | 0:9221dac25d3b | 230 | |
ziqiyap | 2:549bee9a4cd0 | 231 | uint32_t typ_pm_size_i; /**< 32 bit int of Typical Particle Size */ |
ziqiyap | 0:9221dac25d3b | 232 | |
ziqiyap | 0:9221dac25d3b | 233 | protected: |
ziqiyap | 0:9221dac25d3b | 234 | I2C _i2c; |
ziqiyap | 0:9221dac25d3b | 235 | |
ziqiyap | 0:9221dac25d3b | 236 | }; |
ziqiyap | 0:9221dac25d3b | 237 | #endif |
ziqiyap | 0:9221dac25d3b | 238 |